Merge trunk version 195937 into gupc branch.
[official-gcc.git] / gcc / dwarf2out.c
blobbe2a39b6af69915f776729c117822dff36cb992f
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "tree.h"
63 #include "version.h"
64 #include "flags.h"
65 #include "rtl.h"
66 #include "hard-reg-set.h"
67 #include "regs.h"
68 #include "insn-config.h"
69 #include "reload.h"
70 #include "function.h"
71 #include "output.h"
72 #include "expr.h"
73 #include "except.h"
74 #include "dwarf2.h"
75 #include "dwarf2out.h"
76 #include "dwarf2asm.h"
77 #include "toplev.h"
78 #include "ggc.h"
79 #include "md5.h"
80 #include "tm_p.h"
81 #include "diagnostic.h"
82 #include "tree-pretty-print.h"
83 #include "debug.h"
84 #include "target.h"
85 #include "common/common-target.h"
86 #include "langhooks.h"
87 #include "hashtab.h"
88 #include "cgraph.h"
89 #include "input.h"
90 #include "gimple.h"
91 #include "ira.h"
92 #include "lra.h"
93 #include "dumpfile.h"
94 #include "opts.h"
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98 static rtx cached_next_real_insn;
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103 /* Define this macro to be a nonzero value if the directory specifications
104 which are output in the debug info should end with a separator. */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107 from generating indirect strings in DWARF2 debug information, for instance
108 if your target is stuck with an old version of GDB that is unable to
109 process them properly or uses VMS Debug. */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
116 /* ??? Poison these here until it can be done generically. They've been
117 totally replaced in this file; make sure it stays that way. */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
124 /* The size of the target's pointer type. */
125 #ifndef PTR_SIZE
126 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
127 #endif
129 /* Array of RTXes referenced by the debugging information, which therefore
130 must be kept around forever. */
131 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
133 /* A pointer to the base of a list of incomplete types which might be
134 completed at some later time. incomplete_types_list needs to be a
135 vec<tree, va_gc> *because we want to tell the garbage collector about
136 it. */
137 static GTY(()) vec<tree, va_gc> *incomplete_types;
139 /* A pointer to the base of a table of references to declaration
140 scopes. This table is a display which tracks the nesting
141 of declaration scopes at the current scope and containing
142 scopes. This table is used to find the proper place to
143 define type declaration DIE's. */
144 static GTY(()) vec<tree, va_gc> *decl_scope_table;
146 /* Pointers to various DWARF2 sections. */
147 static GTY(()) section *debug_info_section;
148 static GTY(()) section *debug_skeleton_info_section;
149 static GTY(()) section *debug_abbrev_section;
150 static GTY(()) section *debug_skeleton_abbrev_section;
151 static GTY(()) section *debug_aranges_section;
152 static GTY(()) section *debug_addr_section;
153 static GTY(()) section *debug_macinfo_section;
154 static GTY(()) section *debug_line_section;
155 static GTY(()) section *debug_skeleton_line_section;
156 static GTY(()) section *debug_loc_section;
157 static GTY(()) section *debug_pubnames_section;
158 static GTY(()) section *debug_pubtypes_section;
159 static GTY(()) section *debug_str_section;
160 static GTY(()) section *debug_str_offsets_section;
161 static GTY(()) section *debug_ranges_section;
162 static GTY(()) section *debug_frame_section;
164 /* Maximum size (in bytes) of an artificially generated label. */
165 #define MAX_ARTIFICIAL_LABEL_BYTES 30
167 /* According to the (draft) DWARF 3 specification, the initial length
168 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
169 bytes are 0xffffffff, followed by the length stored in the next 8
170 bytes.
172 However, the SGI/MIPS ABI uses an initial length which is equal to
173 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
175 #ifndef DWARF_INITIAL_LENGTH_SIZE
176 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
177 #endif
179 /* Round SIZE up to the nearest BOUNDARY. */
180 #define DWARF_ROUND(SIZE,BOUNDARY) \
181 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
183 /* CIE identifier. */
184 #if HOST_BITS_PER_WIDE_INT >= 64
185 #define DWARF_CIE_ID \
186 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
187 #else
188 #define DWARF_CIE_ID DW_CIE_ID
189 #endif
192 /* A vector for a table that contains frame description
193 information for each routine. */
194 #define NOT_INDEXED (-1U)
195 #define NO_INDEX_ASSIGNED (-2U)
197 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
199 struct GTY(()) indirect_string_node {
200 const char *str;
201 unsigned int refcount;
202 enum dwarf_form form;
203 char *label;
204 unsigned int index;
207 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
209 static GTY(()) int dw2_string_counter;
211 /* True if the compilation unit places functions in more than one section. */
212 static GTY(()) bool have_multiple_function_sections = false;
214 /* Whether the default text and cold text sections have been used at all. */
216 static GTY(()) bool text_section_used = false;
217 static GTY(()) bool cold_text_section_used = false;
219 /* The default cold text section. */
220 static GTY(()) section *cold_text_section;
222 /* Forward declarations for functions defined in this file. */
224 static char *stripattributes (const char *);
225 static void output_call_frame_info (int);
226 static void dwarf2out_note_section_used (void);
228 /* Personality decl of current unit. Used only when assembler does not support
229 personality CFI. */
230 static GTY(()) rtx current_unit_personality;
232 /* Data and reference forms for relocatable data. */
233 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
234 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
236 #ifndef DEBUG_FRAME_SECTION
237 #define DEBUG_FRAME_SECTION ".debug_frame"
238 #endif
240 #ifndef FUNC_BEGIN_LABEL
241 #define FUNC_BEGIN_LABEL "LFB"
242 #endif
244 #ifndef FUNC_END_LABEL
245 #define FUNC_END_LABEL "LFE"
246 #endif
248 #ifndef PROLOGUE_END_LABEL
249 #define PROLOGUE_END_LABEL "LPE"
250 #endif
252 #ifndef EPILOGUE_BEGIN_LABEL
253 #define EPILOGUE_BEGIN_LABEL "LEB"
254 #endif
256 #ifndef FRAME_BEGIN_LABEL
257 #define FRAME_BEGIN_LABEL "Lframe"
258 #endif
259 #define CIE_AFTER_SIZE_LABEL "LSCIE"
260 #define CIE_END_LABEL "LECIE"
261 #define FDE_LABEL "LSFDE"
262 #define FDE_AFTER_SIZE_LABEL "LASFDE"
263 #define FDE_END_LABEL "LEFDE"
264 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
265 #define LINE_NUMBER_END_LABEL "LELT"
266 #define LN_PROLOG_AS_LABEL "LASLTP"
267 #define LN_PROLOG_END_LABEL "LELTP"
268 #define DIE_LABEL_PREFIX "DW"
270 /* Match the base name of a file to the base name of a compilation unit. */
272 static int
273 matches_main_base (const char *path)
275 /* Cache the last query. */
276 static const char *last_path = NULL;
277 static int last_match = 0;
278 if (path != last_path)
280 const char *base;
281 int length = base_of_path (path, &base);
282 last_path = path;
283 last_match = (length == main_input_baselength
284 && memcmp (base, main_input_basename, length) == 0);
286 return last_match;
289 #ifdef DEBUG_DEBUG_STRUCT
291 static int
292 dump_struct_debug (tree type, enum debug_info_usage usage,
293 enum debug_struct_file criterion, int generic,
294 int matches, int result)
296 /* Find the type name. */
297 tree type_decl = TYPE_STUB_DECL (type);
298 tree t = type_decl;
299 const char *name = 0;
300 if (TREE_CODE (t) == TYPE_DECL)
301 t = DECL_NAME (t);
302 if (t)
303 name = IDENTIFIER_POINTER (t);
305 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
306 criterion,
307 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
308 matches ? "bas" : "hdr",
309 generic ? "gen" : "ord",
310 usage == DINFO_USAGE_DFN ? ";" :
311 usage == DINFO_USAGE_DIR_USE ? "." : "*",
312 result,
313 (void*) type_decl, name);
314 return result;
316 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
317 dump_struct_debug (type, usage, criterion, generic, matches, result)
319 #else
321 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
322 (result)
324 #endif
326 static bool
327 should_emit_struct_debug (tree type, enum debug_info_usage usage)
329 enum debug_struct_file criterion;
330 tree type_decl;
331 bool generic = lang_hooks.types.generic_p (type);
333 if (generic)
334 criterion = debug_struct_generic[usage];
335 else
336 criterion = debug_struct_ordinary[usage];
338 if (criterion == DINFO_STRUCT_FILE_NONE)
339 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
340 if (criterion == DINFO_STRUCT_FILE_ANY)
341 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
343 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
345 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
346 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
348 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
349 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
350 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
353 /* Return a pointer to a copy of the section string name S with all
354 attributes stripped off, and an asterisk prepended (for assemble_name). */
356 static inline char *
357 stripattributes (const char *s)
359 char *stripped = XNEWVEC (char, strlen (s) + 2);
360 char *p = stripped;
362 *p++ = '*';
364 while (*s && *s != ',')
365 *p++ = *s++;
367 *p = '\0';
368 return stripped;
371 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
372 switch to the data section instead, and write out a synthetic start label
373 for collect2 the first time around. */
375 static void
376 switch_to_eh_frame_section (bool back)
378 tree label;
380 #ifdef EH_FRAME_SECTION_NAME
381 if (eh_frame_section == 0)
383 int flags;
385 if (EH_TABLES_CAN_BE_READ_ONLY)
387 int fde_encoding;
388 int per_encoding;
389 int lsda_encoding;
391 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
392 /*global=*/0);
393 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
394 /*global=*/1);
395 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
396 /*global=*/0);
397 flags = ((! flag_pic
398 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
399 && (fde_encoding & 0x70) != DW_EH_PE_aligned
400 && (per_encoding & 0x70) != DW_EH_PE_absptr
401 && (per_encoding & 0x70) != DW_EH_PE_aligned
402 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
403 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
404 ? 0 : SECTION_WRITE);
406 else
407 flags = SECTION_WRITE;
408 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
410 #endif /* EH_FRAME_SECTION_NAME */
412 if (eh_frame_section)
413 switch_to_section (eh_frame_section);
414 else
416 /* We have no special eh_frame section. Put the information in
417 the data section and emit special labels to guide collect2. */
418 switch_to_section (data_section);
420 if (!back)
422 label = get_file_function_name ("F");
423 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
424 targetm.asm_out.globalize_label (asm_out_file,
425 IDENTIFIER_POINTER (label));
426 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
431 /* Switch [BACK] to the eh or debug frame table section, depending on
432 FOR_EH. */
434 static void
435 switch_to_frame_table_section (int for_eh, bool back)
437 if (for_eh)
438 switch_to_eh_frame_section (back);
439 else
441 if (!debug_frame_section)
442 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
443 SECTION_DEBUG, NULL);
444 switch_to_section (debug_frame_section);
448 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
450 enum dw_cfi_oprnd_type
451 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
453 switch (cfi)
455 case DW_CFA_nop:
456 case DW_CFA_GNU_window_save:
457 case DW_CFA_remember_state:
458 case DW_CFA_restore_state:
459 return dw_cfi_oprnd_unused;
461 case DW_CFA_set_loc:
462 case DW_CFA_advance_loc1:
463 case DW_CFA_advance_loc2:
464 case DW_CFA_advance_loc4:
465 case DW_CFA_MIPS_advance_loc8:
466 return dw_cfi_oprnd_addr;
468 case DW_CFA_offset:
469 case DW_CFA_offset_extended:
470 case DW_CFA_def_cfa:
471 case DW_CFA_offset_extended_sf:
472 case DW_CFA_def_cfa_sf:
473 case DW_CFA_restore:
474 case DW_CFA_restore_extended:
475 case DW_CFA_undefined:
476 case DW_CFA_same_value:
477 case DW_CFA_def_cfa_register:
478 case DW_CFA_register:
479 case DW_CFA_expression:
480 return dw_cfi_oprnd_reg_num;
482 case DW_CFA_def_cfa_offset:
483 case DW_CFA_GNU_args_size:
484 case DW_CFA_def_cfa_offset_sf:
485 return dw_cfi_oprnd_offset;
487 case DW_CFA_def_cfa_expression:
488 return dw_cfi_oprnd_loc;
490 default:
491 gcc_unreachable ();
495 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
497 enum dw_cfi_oprnd_type
498 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
500 switch (cfi)
502 case DW_CFA_def_cfa:
503 case DW_CFA_def_cfa_sf:
504 case DW_CFA_offset:
505 case DW_CFA_offset_extended_sf:
506 case DW_CFA_offset_extended:
507 return dw_cfi_oprnd_offset;
509 case DW_CFA_register:
510 return dw_cfi_oprnd_reg_num;
512 case DW_CFA_expression:
513 return dw_cfi_oprnd_loc;
515 default:
516 return dw_cfi_oprnd_unused;
520 /* Output one FDE. */
522 static void
523 output_fde (dw_fde_ref fde, bool for_eh, bool second,
524 char *section_start_label, int fde_encoding, char *augmentation,
525 bool any_lsda_needed, int lsda_encoding)
527 const char *begin, *end;
528 static unsigned int j;
529 char l1[20], l2[20];
531 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
532 /* empty */ 0);
533 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
534 for_eh + j);
535 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
536 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
537 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
538 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
539 " indicating 64-bit DWARF extension");
540 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
541 "FDE Length");
542 ASM_OUTPUT_LABEL (asm_out_file, l1);
544 if (for_eh)
545 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
546 else
547 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
548 debug_frame_section, "FDE CIE offset");
550 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
551 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
553 if (for_eh)
555 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
556 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
557 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
558 "FDE initial location");
559 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
560 end, begin, "FDE address range");
562 else
564 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
565 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
568 if (augmentation[0])
570 if (any_lsda_needed)
572 int size = size_of_encoded_value (lsda_encoding);
574 if (lsda_encoding == DW_EH_PE_aligned)
576 int offset = ( 4 /* Length */
577 + 4 /* CIE offset */
578 + 2 * size_of_encoded_value (fde_encoding)
579 + 1 /* Augmentation size */ );
580 int pad = -offset & (PTR_SIZE - 1);
582 size += pad;
583 gcc_assert (size_of_uleb128 (size) == 1);
586 dw2_asm_output_data_uleb128 (size, "Augmentation size");
588 if (fde->uses_eh_lsda)
590 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
591 fde->funcdef_number);
592 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
593 gen_rtx_SYMBOL_REF (Pmode, l1),
594 false,
595 "Language Specific Data Area");
597 else
599 if (lsda_encoding == DW_EH_PE_aligned)
600 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
601 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
602 "Language Specific Data Area (none)");
605 else
606 dw2_asm_output_data_uleb128 (0, "Augmentation size");
609 /* Loop through the Call Frame Instructions associated with this FDE. */
610 fde->dw_fde_current_label = begin;
612 size_t from, until, i;
614 from = 0;
615 until = vec_safe_length (fde->dw_fde_cfi);
617 if (fde->dw_fde_second_begin == NULL)
619 else if (!second)
620 until = fde->dw_fde_switch_cfi_index;
621 else
622 from = fde->dw_fde_switch_cfi_index;
624 for (i = from; i < until; i++)
625 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
628 /* If we are to emit a ref/link from function bodies to their frame tables,
629 do it now. This is typically performed to make sure that tables
630 associated with functions are dragged with them and not discarded in
631 garbage collecting links. We need to do this on a per function basis to
632 cope with -ffunction-sections. */
634 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
635 /* Switch to the function section, emit the ref to the tables, and
636 switch *back* into the table section. */
637 switch_to_section (function_section (fde->decl));
638 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
639 switch_to_frame_table_section (for_eh, true);
640 #endif
642 /* Pad the FDE out to an address sized boundary. */
643 ASM_OUTPUT_ALIGN (asm_out_file,
644 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
645 ASM_OUTPUT_LABEL (asm_out_file, l2);
647 j += 2;
650 /* Return true if frame description entry FDE is needed for EH. */
652 static bool
653 fde_needed_for_eh_p (dw_fde_ref fde)
655 if (flag_asynchronous_unwind_tables)
656 return true;
658 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
659 return true;
661 if (fde->uses_eh_lsda)
662 return true;
664 /* If exceptions are enabled, we have collected nothrow info. */
665 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
666 return false;
668 return true;
671 /* Output the call frame information used to record information
672 that relates to calculating the frame pointer, and records the
673 location of saved registers. */
675 static void
676 output_call_frame_info (int for_eh)
678 unsigned int i;
679 dw_fde_ref fde;
680 dw_cfi_ref cfi;
681 char l1[20], l2[20], section_start_label[20];
682 bool any_lsda_needed = false;
683 char augmentation[6];
684 int augmentation_size;
685 int fde_encoding = DW_EH_PE_absptr;
686 int per_encoding = DW_EH_PE_absptr;
687 int lsda_encoding = DW_EH_PE_absptr;
688 int return_reg;
689 rtx personality = NULL;
690 int dw_cie_version;
692 /* Don't emit a CIE if there won't be any FDEs. */
693 if (!fde_vec)
694 return;
696 /* Nothing to do if the assembler's doing it all. */
697 if (dwarf2out_do_cfi_asm ())
698 return;
700 /* If we don't have any functions we'll want to unwind out of, don't emit
701 any EH unwind information. If we make FDEs linkonce, we may have to
702 emit an empty label for an FDE that wouldn't otherwise be emitted. We
703 want to avoid having an FDE kept around when the function it refers to
704 is discarded. Example where this matters: a primary function template
705 in C++ requires EH information, an explicit specialization doesn't. */
706 if (for_eh)
708 bool any_eh_needed = false;
710 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
712 if (fde->uses_eh_lsda)
713 any_eh_needed = any_lsda_needed = true;
714 else if (fde_needed_for_eh_p (fde))
715 any_eh_needed = true;
716 else if (TARGET_USES_WEAK_UNWIND_INFO)
717 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
720 if (!any_eh_needed)
721 return;
724 /* We're going to be generating comments, so turn on app. */
725 if (flag_debug_asm)
726 app_enable ();
728 /* Switch to the proper frame section, first time. */
729 switch_to_frame_table_section (for_eh, false);
731 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
732 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
734 /* Output the CIE. */
735 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
736 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
737 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
738 dw2_asm_output_data (4, 0xffffffff,
739 "Initial length escape value indicating 64-bit DWARF extension");
740 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
741 "Length of Common Information Entry");
742 ASM_OUTPUT_LABEL (asm_out_file, l1);
744 /* Now that the CIE pointer is PC-relative for EH,
745 use 0 to identify the CIE. */
746 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
747 (for_eh ? 0 : DWARF_CIE_ID),
748 "CIE Identifier Tag");
750 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
751 use CIE version 1, unless that would produce incorrect results
752 due to overflowing the return register column. */
753 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
754 dw_cie_version = 1;
755 if (return_reg >= 256 || dwarf_version > 2)
756 dw_cie_version = 3;
757 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
759 augmentation[0] = 0;
760 augmentation_size = 0;
762 personality = current_unit_personality;
763 if (for_eh)
765 char *p;
767 /* Augmentation:
768 z Indicates that a uleb128 is present to size the
769 augmentation section.
770 L Indicates the encoding (and thus presence) of
771 an LSDA pointer in the FDE augmentation.
772 R Indicates a non-default pointer encoding for
773 FDE code pointers.
774 P Indicates the presence of an encoding + language
775 personality routine in the CIE augmentation. */
777 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
778 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
779 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
781 p = augmentation + 1;
782 if (personality)
784 *p++ = 'P';
785 augmentation_size += 1 + size_of_encoded_value (per_encoding);
786 assemble_external_libcall (personality);
788 if (any_lsda_needed)
790 *p++ = 'L';
791 augmentation_size += 1;
793 if (fde_encoding != DW_EH_PE_absptr)
795 *p++ = 'R';
796 augmentation_size += 1;
798 if (p > augmentation + 1)
800 augmentation[0] = 'z';
801 *p = '\0';
804 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
805 if (personality && per_encoding == DW_EH_PE_aligned)
807 int offset = ( 4 /* Length */
808 + 4 /* CIE Id */
809 + 1 /* CIE version */
810 + strlen (augmentation) + 1 /* Augmentation */
811 + size_of_uleb128 (1) /* Code alignment */
812 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
813 + 1 /* RA column */
814 + 1 /* Augmentation size */
815 + 1 /* Personality encoding */ );
816 int pad = -offset & (PTR_SIZE - 1);
818 augmentation_size += pad;
820 /* Augmentations should be small, so there's scarce need to
821 iterate for a solution. Die if we exceed one uleb128 byte. */
822 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
826 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
827 if (dw_cie_version >= 4)
829 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
830 dw2_asm_output_data (1, 0, "CIE Segment Size");
832 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
833 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
834 "CIE Data Alignment Factor");
836 if (dw_cie_version == 1)
837 dw2_asm_output_data (1, return_reg, "CIE RA Column");
838 else
839 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
841 if (augmentation[0])
843 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
844 if (personality)
846 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
847 eh_data_format_name (per_encoding));
848 dw2_asm_output_encoded_addr_rtx (per_encoding,
849 personality,
850 true, NULL);
853 if (any_lsda_needed)
854 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
855 eh_data_format_name (lsda_encoding));
857 if (fde_encoding != DW_EH_PE_absptr)
858 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
859 eh_data_format_name (fde_encoding));
862 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
863 output_cfi (cfi, NULL, for_eh);
865 /* Pad the CIE out to an address sized boundary. */
866 ASM_OUTPUT_ALIGN (asm_out_file,
867 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
868 ASM_OUTPUT_LABEL (asm_out_file, l2);
870 /* Loop through all of the FDE's. */
871 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
873 unsigned int k;
875 /* Don't emit EH unwind info for leaf functions that don't need it. */
876 if (for_eh && !fde_needed_for_eh_p (fde))
877 continue;
879 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
880 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
881 augmentation, any_lsda_needed, lsda_encoding);
884 if (for_eh && targetm.terminate_dw2_eh_frame_info)
885 dw2_asm_output_data (4, 0, "End of Table");
887 /* Turn off app to make assembly quicker. */
888 if (flag_debug_asm)
889 app_disable ();
892 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
894 static void
895 dwarf2out_do_cfi_startproc (bool second)
897 int enc;
898 rtx ref;
899 rtx personality = get_personality_function (current_function_decl);
901 fprintf (asm_out_file, "\t.cfi_startproc\n");
903 if (personality)
905 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
906 ref = personality;
908 /* ??? The GAS support isn't entirely consistent. We have to
909 handle indirect support ourselves, but PC-relative is done
910 in the assembler. Further, the assembler can't handle any
911 of the weirder relocation types. */
912 if (enc & DW_EH_PE_indirect)
913 ref = dw2_force_const_mem (ref, true);
915 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
916 output_addr_const (asm_out_file, ref);
917 fputc ('\n', asm_out_file);
920 if (crtl->uses_eh_lsda)
922 char lab[20];
924 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
925 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
926 current_function_funcdef_no);
927 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
928 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
930 if (enc & DW_EH_PE_indirect)
931 ref = dw2_force_const_mem (ref, true);
933 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
934 output_addr_const (asm_out_file, ref);
935 fputc ('\n', asm_out_file);
939 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
940 this allocation may be done before pass_final. */
942 dw_fde_ref
943 dwarf2out_alloc_current_fde (void)
945 dw_fde_ref fde;
947 fde = ggc_alloc_cleared_dw_fde_node ();
948 fde->decl = current_function_decl;
949 fde->funcdef_number = current_function_funcdef_no;
950 fde->fde_index = vec_safe_length (fde_vec);
951 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
952 fde->uses_eh_lsda = crtl->uses_eh_lsda;
953 fde->nothrow = crtl->nothrow;
954 fde->drap_reg = INVALID_REGNUM;
955 fde->vdrap_reg = INVALID_REGNUM;
957 /* Record the FDE associated with this function. */
958 cfun->fde = fde;
959 vec_safe_push (fde_vec, fde);
961 return fde;
964 /* Output a marker (i.e. a label) for the beginning of a function, before
965 the prologue. */
967 void
968 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
969 const char *file ATTRIBUTE_UNUSED)
971 char label[MAX_ARTIFICIAL_LABEL_BYTES];
972 char * dup_label;
973 dw_fde_ref fde;
974 section *fnsec;
975 bool do_frame;
977 current_function_func_begin_label = NULL;
979 do_frame = dwarf2out_do_frame ();
981 /* ??? current_function_func_begin_label is also used by except.c for
982 call-site information. We must emit this label if it might be used. */
983 if (!do_frame
984 && (!flag_exceptions
985 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
986 return;
988 fnsec = function_section (current_function_decl);
989 switch_to_section (fnsec);
990 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
991 current_function_funcdef_no);
992 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
993 current_function_funcdef_no);
994 dup_label = xstrdup (label);
995 current_function_func_begin_label = dup_label;
997 /* We can elide the fde allocation if we're not emitting debug info. */
998 if (!do_frame)
999 return;
1001 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1002 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1003 would include pass_dwarf2_frame. If we've not created the FDE yet,
1004 do so now. */
1005 fde = cfun->fde;
1006 if (fde == NULL)
1007 fde = dwarf2out_alloc_current_fde ();
1009 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1010 fde->dw_fde_begin = dup_label;
1011 fde->dw_fde_current_label = dup_label;
1012 fde->in_std_section = (fnsec == text_section
1013 || (cold_text_section && fnsec == cold_text_section));
1015 /* We only want to output line number information for the genuine dwarf2
1016 prologue case, not the eh frame case. */
1017 #ifdef DWARF2_DEBUGGING_INFO
1018 if (file)
1019 dwarf2out_source_line (line, file, 0, true);
1020 #endif
1022 if (dwarf2out_do_cfi_asm ())
1023 dwarf2out_do_cfi_startproc (false);
1024 else
1026 rtx personality = get_personality_function (current_function_decl);
1027 if (!current_unit_personality)
1028 current_unit_personality = personality;
1030 /* We cannot keep a current personality per function as without CFI
1031 asm, at the point where we emit the CFI data, there is no current
1032 function anymore. */
1033 if (personality && current_unit_personality != personality)
1034 sorry ("multiple EH personalities are supported only with assemblers "
1035 "supporting .cfi_personality directive");
1039 /* Output a marker (i.e. a label) for the end of the generated code
1040 for a function prologue. This gets called *after* the prologue code has
1041 been generated. */
1043 void
1044 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1045 const char *file ATTRIBUTE_UNUSED)
1047 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1049 /* Output a label to mark the endpoint of the code generated for this
1050 function. */
1051 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1052 current_function_funcdef_no);
1053 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1054 current_function_funcdef_no);
1055 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1058 /* Output a marker (i.e. a label) for the beginning of the generated code
1059 for a function epilogue. This gets called *before* the prologue code has
1060 been generated. */
1062 void
1063 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1064 const char *file ATTRIBUTE_UNUSED)
1066 dw_fde_ref fde = cfun->fde;
1067 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1069 if (fde->dw_fde_vms_begin_epilogue)
1070 return;
1072 /* Output a label to mark the endpoint of the code generated for this
1073 function. */
1074 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1075 current_function_funcdef_no);
1076 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1077 current_function_funcdef_no);
1078 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1081 /* Output a marker (i.e. a label) for the absolute end of the generated code
1082 for a function definition. This gets called *after* the epilogue code has
1083 been generated. */
1085 void
1086 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1087 const char *file ATTRIBUTE_UNUSED)
1089 dw_fde_ref fde;
1090 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1092 last_var_location_insn = NULL_RTX;
1093 cached_next_real_insn = NULL_RTX;
1095 if (dwarf2out_do_cfi_asm ())
1096 fprintf (asm_out_file, "\t.cfi_endproc\n");
1098 /* Output a label to mark the endpoint of the code generated for this
1099 function. */
1100 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1101 current_function_funcdef_no);
1102 ASM_OUTPUT_LABEL (asm_out_file, label);
1103 fde = cfun->fde;
1104 gcc_assert (fde != NULL);
1105 if (fde->dw_fde_second_begin == NULL)
1106 fde->dw_fde_end = xstrdup (label);
1109 void
1110 dwarf2out_frame_finish (void)
1112 /* Output call frame information. */
1113 if (targetm.debug_unwind_info () == UI_DWARF2)
1114 output_call_frame_info (0);
1116 /* Output another copy for the unwinder. */
1117 if ((flag_unwind_tables || flag_exceptions)
1118 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1119 output_call_frame_info (1);
1122 /* Note that the current function section is being used for code. */
1124 static void
1125 dwarf2out_note_section_used (void)
1127 section *sec = current_function_section ();
1128 if (sec == text_section)
1129 text_section_used = true;
1130 else if (sec == cold_text_section)
1131 cold_text_section_used = true;
1134 static void var_location_switch_text_section (void);
1135 static void set_cur_line_info_table (section *);
1137 void
1138 dwarf2out_switch_text_section (void)
1140 section *sect;
1141 dw_fde_ref fde = cfun->fde;
1143 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1145 if (!in_cold_section_p)
1147 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1148 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1149 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1151 else
1153 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1154 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1155 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1157 have_multiple_function_sections = true;
1159 /* There is no need to mark used sections when not debugging. */
1160 if (cold_text_section != NULL)
1161 dwarf2out_note_section_used ();
1163 if (dwarf2out_do_cfi_asm ())
1164 fprintf (asm_out_file, "\t.cfi_endproc\n");
1166 /* Now do the real section switch. */
1167 sect = current_function_section ();
1168 switch_to_section (sect);
1170 fde->second_in_std_section
1171 = (sect == text_section
1172 || (cold_text_section && sect == cold_text_section));
1174 if (dwarf2out_do_cfi_asm ())
1175 dwarf2out_do_cfi_startproc (true);
1177 var_location_switch_text_section ();
1179 if (cold_text_section != NULL)
1180 set_cur_line_info_table (sect);
1183 /* And now, the subset of the debugging information support code necessary
1184 for emitting location expressions. */
1186 /* Data about a single source file. */
1187 struct GTY(()) dwarf_file_data {
1188 const char * filename;
1189 int emitted_number;
1192 typedef struct GTY(()) deferred_locations_struct
1194 tree variable;
1195 dw_die_ref die;
1196 } deferred_locations;
1199 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1202 /* Describe an entry into the .debug_addr section. */
1204 enum ate_kind {
1205 ate_kind_rtx,
1206 ate_kind_rtx_dtprel,
1207 ate_kind_label
1210 typedef struct GTY(()) addr_table_entry_struct {
1211 enum ate_kind kind;
1212 unsigned int refcount;
1213 unsigned int index;
1214 union addr_table_entry_struct_union
1216 rtx GTY ((tag ("0"))) rtl;
1217 char * GTY ((tag ("1"))) label;
1219 GTY ((desc ("%1.kind"))) addr;
1221 addr_table_entry;
1223 /* Location lists are ranges + location descriptions for that range,
1224 so you can track variables that are in different places over
1225 their entire life. */
1226 typedef struct GTY(()) dw_loc_list_struct {
1227 dw_loc_list_ref dw_loc_next;
1228 const char *begin; /* Label and addr_entry for start of range */
1229 addr_table_entry *begin_entry;
1230 const char *end; /* Label for end of range */
1231 char *ll_symbol; /* Label for beginning of location list.
1232 Only on head of list */
1233 const char *section; /* Section this loclist is relative to */
1234 dw_loc_descr_ref expr;
1235 hashval_t hash;
1236 /* True if all addresses in this and subsequent lists are known to be
1237 resolved. */
1238 bool resolved_addr;
1239 /* True if this list has been replaced by dw_loc_next. */
1240 bool replaced;
1241 bool emitted;
1242 /* True if the range should be emitted even if begin and end
1243 are the same. */
1244 bool force;
1245 } dw_loc_list_node;
1247 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1249 /* Convert a DWARF stack opcode into its string name. */
1251 static const char *
1252 dwarf_stack_op_name (unsigned int op)
1254 const char *name = get_DW_OP_name (op);
1256 if (name != NULL)
1257 return name;
1259 return "OP_<unknown>";
1262 /* Return a pointer to a newly allocated location description. Location
1263 descriptions are simple expression terms that can be strung
1264 together to form more complicated location (address) descriptions. */
1266 static inline dw_loc_descr_ref
1267 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1268 unsigned HOST_WIDE_INT oprnd2)
1270 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1272 descr->dw_loc_opc = op;
1273 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1274 descr->dw_loc_oprnd1.val_entry = NULL;
1275 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1276 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1277 descr->dw_loc_oprnd2.val_entry = NULL;
1278 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1280 return descr;
1283 /* Return a pointer to a newly allocated location description for
1284 REG and OFFSET. */
1286 static inline dw_loc_descr_ref
1287 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1289 if (reg <= 31)
1290 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1291 offset, 0);
1292 else
1293 return new_loc_descr (DW_OP_bregx, reg, offset);
1296 /* Add a location description term to a location description expression. */
1298 static inline void
1299 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1301 dw_loc_descr_ref *d;
1303 /* Find the end of the chain. */
1304 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1307 *d = descr;
1310 /* Compare two location operands for exact equality. */
1312 static bool
1313 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1315 if (a->val_class != b->val_class)
1316 return false;
1317 switch (a->val_class)
1319 case dw_val_class_none:
1320 return true;
1321 case dw_val_class_addr:
1322 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1324 case dw_val_class_offset:
1325 case dw_val_class_unsigned_const:
1326 case dw_val_class_const:
1327 case dw_val_class_range_list:
1328 case dw_val_class_lineptr:
1329 case dw_val_class_macptr:
1330 /* These are all HOST_WIDE_INT, signed or unsigned. */
1331 return a->v.val_unsigned == b->v.val_unsigned;
1333 case dw_val_class_loc:
1334 return a->v.val_loc == b->v.val_loc;
1335 case dw_val_class_loc_list:
1336 return a->v.val_loc_list == b->v.val_loc_list;
1337 case dw_val_class_die_ref:
1338 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1339 case dw_val_class_fde_ref:
1340 return a->v.val_fde_index == b->v.val_fde_index;
1341 case dw_val_class_lbl_id:
1342 case dw_val_class_high_pc:
1343 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1344 case dw_val_class_str:
1345 return a->v.val_str == b->v.val_str;
1346 case dw_val_class_flag:
1347 return a->v.val_flag == b->v.val_flag;
1348 case dw_val_class_file:
1349 return a->v.val_file == b->v.val_file;
1350 case dw_val_class_decl_ref:
1351 return a->v.val_decl_ref == b->v.val_decl_ref;
1353 case dw_val_class_const_double:
1354 return (a->v.val_double.high == b->v.val_double.high
1355 && a->v.val_double.low == b->v.val_double.low);
1357 case dw_val_class_vec:
1359 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1360 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1362 return (a_len == b_len
1363 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1366 case dw_val_class_data8:
1367 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1369 case dw_val_class_vms_delta:
1370 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1371 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1373 gcc_unreachable ();
1376 /* Compare two location atoms for exact equality. */
1378 static bool
1379 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1381 if (a->dw_loc_opc != b->dw_loc_opc)
1382 return false;
1384 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1385 address size, but since we always allocate cleared storage it
1386 should be zero for other types of locations. */
1387 if (a->dtprel != b->dtprel)
1388 return false;
1390 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1391 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1394 /* Compare two complete location expressions for exact equality. */
1396 bool
1397 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1399 while (1)
1401 if (a == b)
1402 return true;
1403 if (a == NULL || b == NULL)
1404 return false;
1405 if (!loc_descr_equal_p_1 (a, b))
1406 return false;
1408 a = a->dw_loc_next;
1409 b = b->dw_loc_next;
1414 /* Add a constant OFFSET to a location expression. */
1416 static void
1417 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1419 dw_loc_descr_ref loc;
1420 HOST_WIDE_INT *p;
1422 gcc_assert (*list_head != NULL);
1424 if (!offset)
1425 return;
1427 /* Find the end of the chain. */
1428 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1431 p = NULL;
1432 if (loc->dw_loc_opc == DW_OP_fbreg
1433 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1434 p = &loc->dw_loc_oprnd1.v.val_int;
1435 else if (loc->dw_loc_opc == DW_OP_bregx)
1436 p = &loc->dw_loc_oprnd2.v.val_int;
1438 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1439 offset. Don't optimize if an signed integer overflow would happen. */
1440 if (p != NULL
1441 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1442 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1443 *p += offset;
1445 else if (offset > 0)
1446 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1448 else
1450 loc->dw_loc_next = int_loc_descriptor (-offset);
1451 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1455 /* Add a constant OFFSET to a location list. */
1457 static void
1458 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1460 dw_loc_list_ref d;
1461 for (d = list_head; d != NULL; d = d->dw_loc_next)
1462 loc_descr_plus_const (&d->expr, offset);
1465 #define DWARF_REF_SIZE \
1466 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1468 static unsigned long int get_base_type_offset (dw_die_ref);
1470 /* Return the size of a location descriptor. */
1472 static unsigned long
1473 size_of_loc_descr (dw_loc_descr_ref loc)
1475 unsigned long size = 1;
1477 switch (loc->dw_loc_opc)
1479 case DW_OP_addr:
1480 size += DWARF2_ADDR_SIZE;
1481 break;
1482 case DW_OP_GNU_addr_index:
1483 case DW_OP_GNU_const_index:
1484 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1485 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1486 break;
1487 case DW_OP_const1u:
1488 case DW_OP_const1s:
1489 size += 1;
1490 break;
1491 case DW_OP_const2u:
1492 case DW_OP_const2s:
1493 size += 2;
1494 break;
1495 case DW_OP_const4u:
1496 case DW_OP_const4s:
1497 size += 4;
1498 break;
1499 case DW_OP_const8u:
1500 case DW_OP_const8s:
1501 size += 8;
1502 break;
1503 case DW_OP_constu:
1504 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1505 break;
1506 case DW_OP_consts:
1507 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1508 break;
1509 case DW_OP_pick:
1510 size += 1;
1511 break;
1512 case DW_OP_plus_uconst:
1513 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1514 break;
1515 case DW_OP_skip:
1516 case DW_OP_bra:
1517 size += 2;
1518 break;
1519 case DW_OP_breg0:
1520 case DW_OP_breg1:
1521 case DW_OP_breg2:
1522 case DW_OP_breg3:
1523 case DW_OP_breg4:
1524 case DW_OP_breg5:
1525 case DW_OP_breg6:
1526 case DW_OP_breg7:
1527 case DW_OP_breg8:
1528 case DW_OP_breg9:
1529 case DW_OP_breg10:
1530 case DW_OP_breg11:
1531 case DW_OP_breg12:
1532 case DW_OP_breg13:
1533 case DW_OP_breg14:
1534 case DW_OP_breg15:
1535 case DW_OP_breg16:
1536 case DW_OP_breg17:
1537 case DW_OP_breg18:
1538 case DW_OP_breg19:
1539 case DW_OP_breg20:
1540 case DW_OP_breg21:
1541 case DW_OP_breg22:
1542 case DW_OP_breg23:
1543 case DW_OP_breg24:
1544 case DW_OP_breg25:
1545 case DW_OP_breg26:
1546 case DW_OP_breg27:
1547 case DW_OP_breg28:
1548 case DW_OP_breg29:
1549 case DW_OP_breg30:
1550 case DW_OP_breg31:
1551 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1552 break;
1553 case DW_OP_regx:
1554 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1555 break;
1556 case DW_OP_fbreg:
1557 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1558 break;
1559 case DW_OP_bregx:
1560 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1561 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1562 break;
1563 case DW_OP_piece:
1564 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1565 break;
1566 case DW_OP_bit_piece:
1567 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1568 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1569 break;
1570 case DW_OP_deref_size:
1571 case DW_OP_xderef_size:
1572 size += 1;
1573 break;
1574 case DW_OP_call2:
1575 size += 2;
1576 break;
1577 case DW_OP_call4:
1578 size += 4;
1579 break;
1580 case DW_OP_call_ref:
1581 size += DWARF_REF_SIZE;
1582 break;
1583 case DW_OP_implicit_value:
1584 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1585 + loc->dw_loc_oprnd1.v.val_unsigned;
1586 break;
1587 case DW_OP_GNU_implicit_pointer:
1588 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1589 break;
1590 case DW_OP_GNU_entry_value:
1592 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1593 size += size_of_uleb128 (op_size) + op_size;
1594 break;
1596 case DW_OP_GNU_const_type:
1598 unsigned long o
1599 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1600 size += size_of_uleb128 (o) + 1;
1601 switch (loc->dw_loc_oprnd2.val_class)
1603 case dw_val_class_vec:
1604 size += loc->dw_loc_oprnd2.v.val_vec.length
1605 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1606 break;
1607 case dw_val_class_const:
1608 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1609 break;
1610 case dw_val_class_const_double:
1611 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1612 break;
1613 default:
1614 gcc_unreachable ();
1616 break;
1618 case DW_OP_GNU_regval_type:
1620 unsigned long o
1621 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1623 + size_of_uleb128 (o);
1625 break;
1626 case DW_OP_GNU_deref_type:
1628 unsigned long o
1629 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1630 size += 1 + size_of_uleb128 (o);
1632 break;
1633 case DW_OP_GNU_convert:
1634 case DW_OP_GNU_reinterpret:
1635 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1636 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1637 else
1639 unsigned long o
1640 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1641 size += size_of_uleb128 (o);
1643 break;
1644 case DW_OP_GNU_parameter_ref:
1645 size += 4;
1646 break;
1647 default:
1648 break;
1651 return size;
1654 /* Return the size of a series of location descriptors. */
1656 unsigned long
1657 size_of_locs (dw_loc_descr_ref loc)
1659 dw_loc_descr_ref l;
1660 unsigned long size;
1662 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1663 field, to avoid writing to a PCH file. */
1664 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1666 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1667 break;
1668 size += size_of_loc_descr (l);
1670 if (! l)
1671 return size;
1673 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1675 l->dw_loc_addr = size;
1676 size += size_of_loc_descr (l);
1679 return size;
1682 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1683 static void get_ref_die_offset_label (char *, dw_die_ref);
1684 static unsigned long int get_ref_die_offset (dw_die_ref);
1686 /* Output location description stack opcode's operands (if any).
1687 The for_eh_or_skip parameter controls whether register numbers are
1688 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1689 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1690 info). This should be suppressed for the cases that have not been converted
1691 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1693 static void
1694 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1696 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1697 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1699 switch (loc->dw_loc_opc)
1701 #ifdef DWARF2_DEBUGGING_INFO
1702 case DW_OP_const2u:
1703 case DW_OP_const2s:
1704 dw2_asm_output_data (2, val1->v.val_int, NULL);
1705 break;
1706 case DW_OP_const4u:
1707 if (loc->dtprel)
1709 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1710 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1711 val1->v.val_addr);
1712 fputc ('\n', asm_out_file);
1713 break;
1715 /* FALLTHRU */
1716 case DW_OP_const4s:
1717 dw2_asm_output_data (4, val1->v.val_int, NULL);
1718 break;
1719 case DW_OP_const8u:
1720 if (loc->dtprel)
1722 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1723 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1724 val1->v.val_addr);
1725 fputc ('\n', asm_out_file);
1726 break;
1728 /* FALLTHRU */
1729 case DW_OP_const8s:
1730 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1731 dw2_asm_output_data (8, val1->v.val_int, NULL);
1732 break;
1733 case DW_OP_skip:
1734 case DW_OP_bra:
1736 int offset;
1738 gcc_assert (val1->val_class == dw_val_class_loc);
1739 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1741 dw2_asm_output_data (2, offset, NULL);
1743 break;
1744 case DW_OP_implicit_value:
1745 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1746 switch (val2->val_class)
1748 case dw_val_class_const:
1749 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1750 break;
1751 case dw_val_class_vec:
1753 unsigned int elt_size = val2->v.val_vec.elt_size;
1754 unsigned int len = val2->v.val_vec.length;
1755 unsigned int i;
1756 unsigned char *p;
1758 if (elt_size > sizeof (HOST_WIDE_INT))
1760 elt_size /= 2;
1761 len *= 2;
1763 for (i = 0, p = val2->v.val_vec.array;
1764 i < len;
1765 i++, p += elt_size)
1766 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1767 "fp or vector constant word %u", i);
1769 break;
1770 case dw_val_class_const_double:
1772 unsigned HOST_WIDE_INT first, second;
1774 if (WORDS_BIG_ENDIAN)
1776 first = val2->v.val_double.high;
1777 second = val2->v.val_double.low;
1779 else
1781 first = val2->v.val_double.low;
1782 second = val2->v.val_double.high;
1784 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1785 first, NULL);
1786 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1787 second, NULL);
1789 break;
1790 case dw_val_class_addr:
1791 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1792 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1793 break;
1794 default:
1795 gcc_unreachable ();
1797 break;
1798 #else
1799 case DW_OP_const2u:
1800 case DW_OP_const2s:
1801 case DW_OP_const4u:
1802 case DW_OP_const4s:
1803 case DW_OP_const8u:
1804 case DW_OP_const8s:
1805 case DW_OP_skip:
1806 case DW_OP_bra:
1807 case DW_OP_implicit_value:
1808 /* We currently don't make any attempt to make sure these are
1809 aligned properly like we do for the main unwind info, so
1810 don't support emitting things larger than a byte if we're
1811 only doing unwinding. */
1812 gcc_unreachable ();
1813 #endif
1814 case DW_OP_const1u:
1815 case DW_OP_const1s:
1816 dw2_asm_output_data (1, val1->v.val_int, NULL);
1817 break;
1818 case DW_OP_constu:
1819 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1820 break;
1821 case DW_OP_consts:
1822 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1823 break;
1824 case DW_OP_pick:
1825 dw2_asm_output_data (1, val1->v.val_int, NULL);
1826 break;
1827 case DW_OP_plus_uconst:
1828 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1829 break;
1830 case DW_OP_breg0:
1831 case DW_OP_breg1:
1832 case DW_OP_breg2:
1833 case DW_OP_breg3:
1834 case DW_OP_breg4:
1835 case DW_OP_breg5:
1836 case DW_OP_breg6:
1837 case DW_OP_breg7:
1838 case DW_OP_breg8:
1839 case DW_OP_breg9:
1840 case DW_OP_breg10:
1841 case DW_OP_breg11:
1842 case DW_OP_breg12:
1843 case DW_OP_breg13:
1844 case DW_OP_breg14:
1845 case DW_OP_breg15:
1846 case DW_OP_breg16:
1847 case DW_OP_breg17:
1848 case DW_OP_breg18:
1849 case DW_OP_breg19:
1850 case DW_OP_breg20:
1851 case DW_OP_breg21:
1852 case DW_OP_breg22:
1853 case DW_OP_breg23:
1854 case DW_OP_breg24:
1855 case DW_OP_breg25:
1856 case DW_OP_breg26:
1857 case DW_OP_breg27:
1858 case DW_OP_breg28:
1859 case DW_OP_breg29:
1860 case DW_OP_breg30:
1861 case DW_OP_breg31:
1862 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1863 break;
1864 case DW_OP_regx:
1866 unsigned r = val1->v.val_unsigned;
1867 if (for_eh_or_skip >= 0)
1868 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1869 gcc_assert (size_of_uleb128 (r)
1870 == size_of_uleb128 (val1->v.val_unsigned));
1871 dw2_asm_output_data_uleb128 (r, NULL);
1873 break;
1874 case DW_OP_fbreg:
1875 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1876 break;
1877 case DW_OP_bregx:
1879 unsigned r = val1->v.val_unsigned;
1880 if (for_eh_or_skip >= 0)
1881 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1882 gcc_assert (size_of_uleb128 (r)
1883 == size_of_uleb128 (val1->v.val_unsigned));
1884 dw2_asm_output_data_uleb128 (r, NULL);
1885 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1887 break;
1888 case DW_OP_piece:
1889 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1890 break;
1891 case DW_OP_bit_piece:
1892 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1893 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1894 break;
1895 case DW_OP_deref_size:
1896 case DW_OP_xderef_size:
1897 dw2_asm_output_data (1, val1->v.val_int, NULL);
1898 break;
1900 case DW_OP_addr:
1901 if (loc->dtprel)
1903 if (targetm.asm_out.output_dwarf_dtprel)
1905 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1906 DWARF2_ADDR_SIZE,
1907 val1->v.val_addr);
1908 fputc ('\n', asm_out_file);
1910 else
1911 gcc_unreachable ();
1913 else
1915 #ifdef DWARF2_DEBUGGING_INFO
1916 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1917 #else
1918 gcc_unreachable ();
1919 #endif
1921 break;
1923 case DW_OP_GNU_addr_index:
1924 case DW_OP_GNU_const_index:
1925 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1926 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1927 "(index into .debug_addr)");
1928 break;
1930 case DW_OP_GNU_implicit_pointer:
1932 char label[MAX_ARTIFICIAL_LABEL_BYTES
1933 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1934 gcc_assert (val1->val_class == dw_val_class_die_ref);
1935 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1936 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1937 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1939 break;
1941 case DW_OP_GNU_entry_value:
1942 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1943 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1944 break;
1946 case DW_OP_GNU_const_type:
1948 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1949 gcc_assert (o);
1950 dw2_asm_output_data_uleb128 (o, NULL);
1951 switch (val2->val_class)
1953 case dw_val_class_const:
1954 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1955 dw2_asm_output_data (1, l, NULL);
1956 dw2_asm_output_data (l, val2->v.val_int, NULL);
1957 break;
1958 case dw_val_class_vec:
1960 unsigned int elt_size = val2->v.val_vec.elt_size;
1961 unsigned int len = val2->v.val_vec.length;
1962 unsigned int i;
1963 unsigned char *p;
1965 l = len * elt_size;
1966 dw2_asm_output_data (1, l, NULL);
1967 if (elt_size > sizeof (HOST_WIDE_INT))
1969 elt_size /= 2;
1970 len *= 2;
1972 for (i = 0, p = val2->v.val_vec.array;
1973 i < len;
1974 i++, p += elt_size)
1975 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1976 "fp or vector constant word %u", i);
1978 break;
1979 case dw_val_class_const_double:
1981 unsigned HOST_WIDE_INT first, second;
1982 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1984 dw2_asm_output_data (1, 2 * l, NULL);
1985 if (WORDS_BIG_ENDIAN)
1987 first = val2->v.val_double.high;
1988 second = val2->v.val_double.low;
1990 else
1992 first = val2->v.val_double.low;
1993 second = val2->v.val_double.high;
1995 dw2_asm_output_data (l, first, NULL);
1996 dw2_asm_output_data (l, second, NULL);
1998 break;
1999 default:
2000 gcc_unreachable ();
2003 break;
2004 case DW_OP_GNU_regval_type:
2006 unsigned r = val1->v.val_unsigned;
2007 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2008 gcc_assert (o);
2009 if (for_eh_or_skip >= 0)
2011 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2012 gcc_assert (size_of_uleb128 (r)
2013 == size_of_uleb128 (val1->v.val_unsigned));
2015 dw2_asm_output_data_uleb128 (r, NULL);
2016 dw2_asm_output_data_uleb128 (o, NULL);
2018 break;
2019 case DW_OP_GNU_deref_type:
2021 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2022 gcc_assert (o);
2023 dw2_asm_output_data (1, val1->v.val_int, NULL);
2024 dw2_asm_output_data_uleb128 (o, NULL);
2026 break;
2027 case DW_OP_GNU_convert:
2028 case DW_OP_GNU_reinterpret:
2029 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2030 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2031 else
2033 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2034 gcc_assert (o);
2035 dw2_asm_output_data_uleb128 (o, NULL);
2037 break;
2039 case DW_OP_GNU_parameter_ref:
2041 unsigned long o;
2042 gcc_assert (val1->val_class == dw_val_class_die_ref);
2043 o = get_ref_die_offset (val1->v.val_die_ref.die);
2044 dw2_asm_output_data (4, o, NULL);
2046 break;
2048 default:
2049 /* Other codes have no operands. */
2050 break;
2054 /* Output a sequence of location operations.
2055 The for_eh_or_skip parameter controls whether register numbers are
2056 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2057 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2058 info). This should be suppressed for the cases that have not been converted
2059 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2061 void
2062 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2064 for (; loc != NULL; loc = loc->dw_loc_next)
2066 enum dwarf_location_atom opc = loc->dw_loc_opc;
2067 /* Output the opcode. */
2068 if (for_eh_or_skip >= 0
2069 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2071 unsigned r = (opc - DW_OP_breg0);
2072 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2073 gcc_assert (r <= 31);
2074 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2076 else if (for_eh_or_skip >= 0
2077 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2079 unsigned r = (opc - DW_OP_reg0);
2080 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2081 gcc_assert (r <= 31);
2082 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2085 dw2_asm_output_data (1, opc,
2086 "%s", dwarf_stack_op_name (opc));
2088 /* Output the operand(s) (if any). */
2089 output_loc_operands (loc, for_eh_or_skip);
2093 /* Output location description stack opcode's operands (if any).
2094 The output is single bytes on a line, suitable for .cfi_escape. */
2096 static void
2097 output_loc_operands_raw (dw_loc_descr_ref loc)
2099 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2100 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2102 switch (loc->dw_loc_opc)
2104 case DW_OP_addr:
2105 case DW_OP_GNU_addr_index:
2106 case DW_OP_GNU_const_index:
2107 case DW_OP_implicit_value:
2108 /* We cannot output addresses in .cfi_escape, only bytes. */
2109 gcc_unreachable ();
2111 case DW_OP_const1u:
2112 case DW_OP_const1s:
2113 case DW_OP_pick:
2114 case DW_OP_deref_size:
2115 case DW_OP_xderef_size:
2116 fputc (',', asm_out_file);
2117 dw2_asm_output_data_raw (1, val1->v.val_int);
2118 break;
2120 case DW_OP_const2u:
2121 case DW_OP_const2s:
2122 fputc (',', asm_out_file);
2123 dw2_asm_output_data_raw (2, val1->v.val_int);
2124 break;
2126 case DW_OP_const4u:
2127 case DW_OP_const4s:
2128 fputc (',', asm_out_file);
2129 dw2_asm_output_data_raw (4, val1->v.val_int);
2130 break;
2132 case DW_OP_const8u:
2133 case DW_OP_const8s:
2134 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2135 fputc (',', asm_out_file);
2136 dw2_asm_output_data_raw (8, val1->v.val_int);
2137 break;
2139 case DW_OP_skip:
2140 case DW_OP_bra:
2142 int offset;
2144 gcc_assert (val1->val_class == dw_val_class_loc);
2145 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2147 fputc (',', asm_out_file);
2148 dw2_asm_output_data_raw (2, offset);
2150 break;
2152 case DW_OP_regx:
2154 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2155 gcc_assert (size_of_uleb128 (r)
2156 == size_of_uleb128 (val1->v.val_unsigned));
2157 fputc (',', asm_out_file);
2158 dw2_asm_output_data_uleb128_raw (r);
2160 break;
2162 case DW_OP_constu:
2163 case DW_OP_plus_uconst:
2164 case DW_OP_piece:
2165 fputc (',', asm_out_file);
2166 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2167 break;
2169 case DW_OP_bit_piece:
2170 fputc (',', asm_out_file);
2171 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2172 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2173 break;
2175 case DW_OP_consts:
2176 case DW_OP_breg0:
2177 case DW_OP_breg1:
2178 case DW_OP_breg2:
2179 case DW_OP_breg3:
2180 case DW_OP_breg4:
2181 case DW_OP_breg5:
2182 case DW_OP_breg6:
2183 case DW_OP_breg7:
2184 case DW_OP_breg8:
2185 case DW_OP_breg9:
2186 case DW_OP_breg10:
2187 case DW_OP_breg11:
2188 case DW_OP_breg12:
2189 case DW_OP_breg13:
2190 case DW_OP_breg14:
2191 case DW_OP_breg15:
2192 case DW_OP_breg16:
2193 case DW_OP_breg17:
2194 case DW_OP_breg18:
2195 case DW_OP_breg19:
2196 case DW_OP_breg20:
2197 case DW_OP_breg21:
2198 case DW_OP_breg22:
2199 case DW_OP_breg23:
2200 case DW_OP_breg24:
2201 case DW_OP_breg25:
2202 case DW_OP_breg26:
2203 case DW_OP_breg27:
2204 case DW_OP_breg28:
2205 case DW_OP_breg29:
2206 case DW_OP_breg30:
2207 case DW_OP_breg31:
2208 case DW_OP_fbreg:
2209 fputc (',', asm_out_file);
2210 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2211 break;
2213 case DW_OP_bregx:
2215 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2216 gcc_assert (size_of_uleb128 (r)
2217 == size_of_uleb128 (val1->v.val_unsigned));
2218 fputc (',', asm_out_file);
2219 dw2_asm_output_data_uleb128_raw (r);
2220 fputc (',', asm_out_file);
2221 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2223 break;
2225 case DW_OP_GNU_implicit_pointer:
2226 case DW_OP_GNU_entry_value:
2227 case DW_OP_GNU_const_type:
2228 case DW_OP_GNU_regval_type:
2229 case DW_OP_GNU_deref_type:
2230 case DW_OP_GNU_convert:
2231 case DW_OP_GNU_reinterpret:
2232 case DW_OP_GNU_parameter_ref:
2233 gcc_unreachable ();
2234 break;
2236 default:
2237 /* Other codes have no operands. */
2238 break;
2242 void
2243 output_loc_sequence_raw (dw_loc_descr_ref loc)
2245 while (1)
2247 enum dwarf_location_atom opc = loc->dw_loc_opc;
2248 /* Output the opcode. */
2249 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2251 unsigned r = (opc - DW_OP_breg0);
2252 r = DWARF2_FRAME_REG_OUT (r, 1);
2253 gcc_assert (r <= 31);
2254 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2256 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2258 unsigned r = (opc - DW_OP_reg0);
2259 r = DWARF2_FRAME_REG_OUT (r, 1);
2260 gcc_assert (r <= 31);
2261 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2263 /* Output the opcode. */
2264 fprintf (asm_out_file, "%#x", opc);
2265 output_loc_operands_raw (loc);
2267 if (!loc->dw_loc_next)
2268 break;
2269 loc = loc->dw_loc_next;
2271 fputc (',', asm_out_file);
2275 /* This function builds a dwarf location descriptor sequence from a
2276 dw_cfa_location, adding the given OFFSET to the result of the
2277 expression. */
2279 struct dw_loc_descr_struct *
2280 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2282 struct dw_loc_descr_struct *head, *tmp;
2284 offset += cfa->offset;
2286 if (cfa->indirect)
2288 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2289 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2290 head->dw_loc_oprnd1.val_entry = NULL;
2291 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2292 add_loc_descr (&head, tmp);
2293 if (offset != 0)
2295 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2296 add_loc_descr (&head, tmp);
2299 else
2300 head = new_reg_loc_descr (cfa->reg, offset);
2302 return head;
2305 /* This function builds a dwarf location descriptor sequence for
2306 the address at OFFSET from the CFA when stack is aligned to
2307 ALIGNMENT byte. */
2309 struct dw_loc_descr_struct *
2310 build_cfa_aligned_loc (dw_cfa_location *cfa,
2311 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2313 struct dw_loc_descr_struct *head;
2314 unsigned int dwarf_fp
2315 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2317 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2318 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2320 head = new_reg_loc_descr (dwarf_fp, 0);
2321 add_loc_descr (&head, int_loc_descriptor (alignment));
2322 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2323 loc_descr_plus_const (&head, offset);
2325 else
2326 head = new_reg_loc_descr (dwarf_fp, offset);
2327 return head;
2330 /* And now, the support for symbolic debugging information. */
2332 /* .debug_str support. */
2333 static int output_indirect_string (void **, void *);
2335 static void dwarf2out_init (const char *);
2336 static void dwarf2out_finish (const char *);
2337 static void dwarf2out_assembly_start (void);
2338 static void dwarf2out_define (unsigned int, const char *);
2339 static void dwarf2out_undef (unsigned int, const char *);
2340 static void dwarf2out_start_source_file (unsigned, const char *);
2341 static void dwarf2out_end_source_file (unsigned);
2342 static void dwarf2out_function_decl (tree);
2343 static void dwarf2out_begin_block (unsigned, unsigned);
2344 static void dwarf2out_end_block (unsigned, unsigned);
2345 static bool dwarf2out_ignore_block (const_tree);
2346 static void dwarf2out_global_decl (tree);
2347 static void dwarf2out_type_decl (tree, int);
2348 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2349 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2350 dw_die_ref);
2351 static void dwarf2out_abstract_function (tree);
2352 static void dwarf2out_var_location (rtx);
2353 static void dwarf2out_begin_function (tree);
2354 static void dwarf2out_end_function (unsigned int);
2355 static void dwarf2out_set_name (tree, tree);
2357 /* The debug hooks structure. */
2359 const struct gcc_debug_hooks dwarf2_debug_hooks =
2361 dwarf2out_init,
2362 dwarf2out_finish,
2363 dwarf2out_assembly_start,
2364 dwarf2out_define,
2365 dwarf2out_undef,
2366 dwarf2out_start_source_file,
2367 dwarf2out_end_source_file,
2368 dwarf2out_begin_block,
2369 dwarf2out_end_block,
2370 dwarf2out_ignore_block,
2371 dwarf2out_source_line,
2372 dwarf2out_begin_prologue,
2373 #if VMS_DEBUGGING_INFO
2374 dwarf2out_vms_end_prologue,
2375 dwarf2out_vms_begin_epilogue,
2376 #else
2377 debug_nothing_int_charstar,
2378 debug_nothing_int_charstar,
2379 #endif
2380 dwarf2out_end_epilogue,
2381 dwarf2out_begin_function,
2382 dwarf2out_end_function, /* end_function */
2383 dwarf2out_function_decl, /* function_decl */
2384 dwarf2out_global_decl,
2385 dwarf2out_type_decl, /* type_decl */
2386 dwarf2out_imported_module_or_decl,
2387 debug_nothing_tree, /* deferred_inline_function */
2388 /* The DWARF 2 backend tries to reduce debugging bloat by not
2389 emitting the abstract description of inline functions until
2390 something tries to reference them. */
2391 dwarf2out_abstract_function, /* outlining_inline_function */
2392 debug_nothing_rtx, /* label */
2393 debug_nothing_int, /* handle_pch */
2394 dwarf2out_var_location,
2395 dwarf2out_switch_text_section,
2396 dwarf2out_set_name,
2397 1, /* start_end_main_source_file */
2398 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2401 /* NOTE: In the comments in this file, many references are made to
2402 "Debugging Information Entries". This term is abbreviated as `DIE'
2403 throughout the remainder of this file. */
2405 /* An internal representation of the DWARF output is built, and then
2406 walked to generate the DWARF debugging info. The walk of the internal
2407 representation is done after the entire program has been compiled.
2408 The types below are used to describe the internal representation. */
2410 /* Whether to put type DIEs into their own section .debug_types instead
2411 of making them part of the .debug_info section. Only supported for
2412 Dwarf V4 or higher and the user didn't disable them through
2413 -fno-debug-types-section. It is more efficient to put them in a
2414 separate comdat sections since the linker will then be able to
2415 remove duplicates. But not all tools support .debug_types sections
2416 yet. */
2418 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2420 /* Various DIE's use offsets relative to the beginning of the
2421 .debug_info section to refer to each other. */
2423 typedef long int dw_offset;
2425 /* Define typedefs here to avoid circular dependencies. */
2427 typedef struct dw_attr_struct *dw_attr_ref;
2428 typedef struct dw_line_info_struct *dw_line_info_ref;
2429 typedef struct pubname_struct *pubname_ref;
2430 typedef struct dw_ranges_struct *dw_ranges_ref;
2431 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2432 typedef struct comdat_type_struct *comdat_type_node_ref;
2434 /* The entries in the line_info table more-or-less mirror the opcodes
2435 that are used in the real dwarf line table. Arrays of these entries
2436 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2437 supported. */
2439 enum dw_line_info_opcode {
2440 /* Emit DW_LNE_set_address; the operand is the label index. */
2441 LI_set_address,
2443 /* Emit a row to the matrix with the given line. This may be done
2444 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2445 special opcodes. */
2446 LI_set_line,
2448 /* Emit a DW_LNS_set_file. */
2449 LI_set_file,
2451 /* Emit a DW_LNS_set_column. */
2452 LI_set_column,
2454 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2455 LI_negate_stmt,
2457 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2458 LI_set_prologue_end,
2459 LI_set_epilogue_begin,
2461 /* Emit a DW_LNE_set_discriminator. */
2462 LI_set_discriminator
2465 typedef struct GTY(()) dw_line_info_struct {
2466 enum dw_line_info_opcode opcode;
2467 unsigned int val;
2468 } dw_line_info_entry;
2471 typedef struct GTY(()) dw_line_info_table_struct {
2472 /* The label that marks the end of this section. */
2473 const char *end_label;
2475 /* The values for the last row of the matrix, as collected in the table.
2476 These are used to minimize the changes to the next row. */
2477 unsigned int file_num;
2478 unsigned int line_num;
2479 unsigned int column_num;
2480 int discrim_num;
2481 bool is_stmt;
2482 bool in_use;
2484 vec<dw_line_info_entry, va_gc> *entries;
2485 } dw_line_info_table;
2487 typedef dw_line_info_table *dw_line_info_table_p;
2490 /* Each DIE attribute has a field specifying the attribute kind,
2491 a link to the next attribute in the chain, and an attribute value.
2492 Attributes are typically linked below the DIE they modify. */
2494 typedef struct GTY(()) dw_attr_struct {
2495 enum dwarf_attribute dw_attr;
2496 dw_val_node dw_attr_val;
2498 dw_attr_node;
2501 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2502 The children of each node form a circular list linked by
2503 die_sib. die_child points to the node *before* the "first" child node. */
2505 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2506 union die_symbol_or_type_node
2508 const char * GTY ((tag ("0"))) die_symbol;
2509 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2511 GTY ((desc ("%0.comdat_type_p"))) die_id;
2512 vec<dw_attr_node, va_gc> *die_attr;
2513 dw_die_ref die_parent;
2514 dw_die_ref die_child;
2515 dw_die_ref die_sib;
2516 dw_die_ref die_definition; /* ref from a specification to its definition */
2517 dw_offset die_offset;
2518 unsigned long die_abbrev;
2519 int die_mark;
2520 unsigned int decl_id;
2521 enum dwarf_tag die_tag;
2522 /* Die is used and must not be pruned as unused. */
2523 BOOL_BITFIELD die_perennial_p : 1;
2524 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2525 /* Lots of spare bits. */
2527 die_node;
2529 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2530 #define FOR_EACH_CHILD(die, c, expr) do { \
2531 c = die->die_child; \
2532 if (c) do { \
2533 c = c->die_sib; \
2534 expr; \
2535 } while (c != die->die_child); \
2536 } while (0)
2538 /* The pubname structure */
2540 typedef struct GTY(()) pubname_struct {
2541 dw_die_ref die;
2542 const char *name;
2544 pubname_entry;
2547 struct GTY(()) dw_ranges_struct {
2548 /* If this is positive, it's a block number, otherwise it's a
2549 bitwise-negated index into dw_ranges_by_label. */
2550 int num;
2553 /* A structure to hold a macinfo entry. */
2555 typedef struct GTY(()) macinfo_struct {
2556 unsigned char code;
2557 unsigned HOST_WIDE_INT lineno;
2558 const char *info;
2560 macinfo_entry;
2563 struct GTY(()) dw_ranges_by_label_struct {
2564 const char *begin;
2565 const char *end;
2568 /* The comdat type node structure. */
2569 typedef struct GTY(()) comdat_type_struct
2571 dw_die_ref root_die;
2572 dw_die_ref type_die;
2573 dw_die_ref skeleton_die;
2574 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2575 struct comdat_type_struct *next;
2577 comdat_type_node;
2579 /* The limbo die list structure. */
2580 typedef struct GTY(()) limbo_die_struct {
2581 dw_die_ref die;
2582 tree created_for;
2583 struct limbo_die_struct *next;
2585 limbo_die_node;
2587 typedef struct skeleton_chain_struct
2589 dw_die_ref old_die;
2590 dw_die_ref new_die;
2591 struct skeleton_chain_struct *parent;
2593 skeleton_chain_node;
2595 /* Define a macro which returns nonzero for a TYPE_DECL which was
2596 implicitly generated for a type.
2598 Note that, unlike the C front-end (which generates a NULL named
2599 TYPE_DECL node for each complete tagged type, each array type,
2600 and each function type node created) the C++ front-end generates
2601 a _named_ TYPE_DECL node for each tagged type node created.
2602 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2603 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2604 front-end, but for each type, tagged or not. */
2606 #define TYPE_DECL_IS_STUB(decl) \
2607 (DECL_NAME (decl) == NULL_TREE \
2608 || (DECL_ARTIFICIAL (decl) \
2609 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2610 /* This is necessary for stub decls that \
2611 appear in nested inline functions. */ \
2612 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2613 && (decl_ultimate_origin (decl) \
2614 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2616 /* Information concerning the compilation unit's programming
2617 language, and compiler version. */
2619 /* Fixed size portion of the DWARF compilation unit header. */
2620 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2621 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2623 /* Fixed size portion of the DWARF comdat type unit header. */
2624 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2625 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2626 + DWARF_OFFSET_SIZE)
2628 /* Fixed size portion of public names info. */
2629 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2631 /* Fixed size portion of the address range info. */
2632 #define DWARF_ARANGES_HEADER_SIZE \
2633 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2634 DWARF2_ADDR_SIZE * 2) \
2635 - DWARF_INITIAL_LENGTH_SIZE)
2637 /* Size of padding portion in the address range info. It must be
2638 aligned to twice the pointer size. */
2639 #define DWARF_ARANGES_PAD_SIZE \
2640 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2641 DWARF2_ADDR_SIZE * 2) \
2642 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2644 /* Use assembler line directives if available. */
2645 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2646 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2647 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2648 #else
2649 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2650 #endif
2651 #endif
2653 /* Minimum line offset in a special line info. opcode.
2654 This value was chosen to give a reasonable range of values. */
2655 #define DWARF_LINE_BASE -10
2657 /* First special line opcode - leave room for the standard opcodes. */
2658 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2660 /* Range of line offsets in a special line info. opcode. */
2661 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2663 /* Flag that indicates the initial value of the is_stmt_start flag.
2664 In the present implementation, we do not mark any lines as
2665 the beginning of a source statement, because that information
2666 is not made available by the GCC front-end. */
2667 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2669 /* Maximum number of operations per instruction bundle. */
2670 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2671 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2672 #endif
2674 /* This location is used by calc_die_sizes() to keep track
2675 the offset of each DIE within the .debug_info section. */
2676 static unsigned long next_die_offset;
2678 /* Record the root of the DIE's built for the current compilation unit. */
2679 static GTY(()) dw_die_ref single_comp_unit_die;
2681 /* A list of type DIEs that have been separated into comdat sections. */
2682 static GTY(()) comdat_type_node *comdat_type_list;
2684 /* A list of DIEs with a NULL parent waiting to be relocated. */
2685 static GTY(()) limbo_die_node *limbo_die_list;
2687 /* A list of DIEs for which we may have to generate
2688 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2689 static GTY(()) limbo_die_node *deferred_asm_name;
2691 /* Filenames referenced by this compilation unit. */
2692 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2694 /* A hash table of references to DIE's that describe declarations.
2695 The key is a DECL_UID() which is a unique number identifying each decl. */
2696 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2698 /* A hash table of references to DIE's that describe COMMON blocks.
2699 The key is DECL_UID() ^ die_parent. */
2700 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2702 typedef struct GTY(()) die_arg_entry_struct {
2703 dw_die_ref die;
2704 tree arg;
2705 } die_arg_entry;
2708 /* Node of the variable location list. */
2709 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2710 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2711 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2712 in mode of the EXPR_LIST node and first EXPR_LIST operand
2713 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2714 location or NULL for padding. For larger bitsizes,
2715 mode is 0 and first operand is a CONCAT with bitsize
2716 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2717 NULL as second operand. */
2718 rtx GTY (()) loc;
2719 const char * GTY (()) label;
2720 struct var_loc_node * GTY (()) next;
2723 /* Variable location list. */
2724 struct GTY (()) var_loc_list_def {
2725 struct var_loc_node * GTY (()) first;
2727 /* Pointer to the last but one or last element of the
2728 chained list. If the list is empty, both first and
2729 last are NULL, if the list contains just one node
2730 or the last node certainly is not redundant, it points
2731 to the last node, otherwise points to the last but one.
2732 Do not mark it for GC because it is marked through the chain. */
2733 struct var_loc_node * GTY ((skip ("%h"))) last;
2735 /* Pointer to the last element before section switch,
2736 if NULL, either sections weren't switched or first
2737 is after section switch. */
2738 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2740 /* DECL_UID of the variable decl. */
2741 unsigned int decl_id;
2743 typedef struct var_loc_list_def var_loc_list;
2745 /* Call argument location list. */
2746 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2747 rtx GTY (()) call_arg_loc_note;
2748 const char * GTY (()) label;
2749 tree GTY (()) block;
2750 bool tail_call_p;
2751 rtx GTY (()) symbol_ref;
2752 struct call_arg_loc_node * GTY (()) next;
2756 /* Table of decl location linked lists. */
2757 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2759 /* Head and tail of call_arg_loc chain. */
2760 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2761 static struct call_arg_loc_node *call_arg_loc_last;
2763 /* Number of call sites in the current function. */
2764 static int call_site_count = -1;
2765 /* Number of tail call sites in the current function. */
2766 static int tail_call_site_count = -1;
2768 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2769 DIEs. */
2770 static vec<dw_die_ref> block_map;
2772 /* A cached location list. */
2773 struct GTY (()) cached_dw_loc_list_def {
2774 /* The DECL_UID of the decl that this entry describes. */
2775 unsigned int decl_id;
2777 /* The cached location list. */
2778 dw_loc_list_ref loc_list;
2780 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2782 /* Table of cached location lists. */
2783 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2785 /* A pointer to the base of a list of references to DIE's that
2786 are uniquely identified by their tag, presence/absence of
2787 children DIE's, and list of attribute/value pairs. */
2788 static GTY((length ("abbrev_die_table_allocated")))
2789 dw_die_ref *abbrev_die_table;
2791 /* Number of elements currently allocated for abbrev_die_table. */
2792 static GTY(()) unsigned abbrev_die_table_allocated;
2794 /* Number of elements in type_die_table currently in use. */
2795 static GTY(()) unsigned abbrev_die_table_in_use;
2797 /* Size (in elements) of increments by which we may expand the
2798 abbrev_die_table. */
2799 #define ABBREV_DIE_TABLE_INCREMENT 256
2801 /* A global counter for generating labels for line number data. */
2802 static unsigned int line_info_label_num;
2804 /* The current table to which we should emit line number information
2805 for the current function. This will be set up at the beginning of
2806 assembly for the function. */
2807 static dw_line_info_table *cur_line_info_table;
2809 /* The two default tables of line number info. */
2810 static GTY(()) dw_line_info_table *text_section_line_info;
2811 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2813 /* The set of all non-default tables of line number info. */
2814 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2816 /* A flag to tell pubnames/types export if there is an info section to
2817 refer to. */
2818 static bool info_section_emitted;
2820 /* A pointer to the base of a table that contains a list of publicly
2821 accessible names. */
2822 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2824 /* A pointer to the base of a table that contains a list of publicly
2825 accessible types. */
2826 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2828 /* A pointer to the base of a table that contains a list of macro
2829 defines/undefines (and file start/end markers). */
2830 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2832 /* True if .debug_macinfo or .debug_macros section is going to be
2833 emitted. */
2834 #define have_macinfo \
2835 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2836 && !macinfo_table->is_empty ())
2838 /* Array of dies for which we should generate .debug_ranges info. */
2839 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2841 /* Number of elements currently allocated for ranges_table. */
2842 static GTY(()) unsigned ranges_table_allocated;
2844 /* Number of elements in ranges_table currently in use. */
2845 static GTY(()) unsigned ranges_table_in_use;
2847 /* Array of pairs of labels referenced in ranges_table. */
2848 static GTY ((length ("ranges_by_label_allocated")))
2849 dw_ranges_by_label_ref ranges_by_label;
2851 /* Number of elements currently allocated for ranges_by_label. */
2852 static GTY(()) unsigned ranges_by_label_allocated;
2854 /* Number of elements in ranges_by_label currently in use. */
2855 static GTY(()) unsigned ranges_by_label_in_use;
2857 /* Size (in elements) of increments by which we may expand the
2858 ranges_table. */
2859 #define RANGES_TABLE_INCREMENT 64
2861 /* Whether we have location lists that need outputting */
2862 static GTY(()) bool have_location_lists;
2864 /* Unique label counter. */
2865 static GTY(()) unsigned int loclabel_num;
2867 /* Unique label counter for point-of-call tables. */
2868 static GTY(()) unsigned int poc_label_num;
2870 /* Record whether the function being analyzed contains inlined functions. */
2871 static int current_function_has_inlines;
2873 /* The last file entry emitted by maybe_emit_file(). */
2874 static GTY(()) struct dwarf_file_data * last_emitted_file;
2876 /* Number of internal labels generated by gen_internal_sym(). */
2877 static GTY(()) int label_num;
2879 /* Cached result of previous call to lookup_filename. */
2880 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2882 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2884 /* Instances of generic types for which we need to generate debug
2885 info that describe their generic parameters and arguments. That
2886 generation needs to happen once all types are properly laid out so
2887 we do it at the end of compilation. */
2888 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2890 /* Offset from the "steady-state frame pointer" to the frame base,
2891 within the current function. */
2892 static HOST_WIDE_INT frame_pointer_fb_offset;
2893 static bool frame_pointer_fb_offset_valid;
2895 static vec<dw_die_ref> base_types;
2897 /* Forward declarations for functions defined in this file. */
2899 static int is_pseudo_reg (const_rtx);
2900 static int is_tagged_type (const_tree);
2901 static const char *dwarf_tag_name (unsigned);
2902 static const char *dwarf_attr_name (unsigned);
2903 static const char *dwarf_form_name (unsigned);
2904 static tree decl_ultimate_origin (const_tree);
2905 static tree decl_class_context (tree);
2906 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2907 static inline enum dw_val_class AT_class (dw_attr_ref);
2908 static inline unsigned int AT_index (dw_attr_ref);
2909 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2910 static inline unsigned AT_flag (dw_attr_ref);
2911 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2912 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2913 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2914 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2915 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2916 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2917 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2918 unsigned int, unsigned char *);
2919 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2920 static hashval_t debug_str_do_hash (const void *);
2921 static int debug_str_eq (const void *, const void *);
2922 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2923 static inline const char *AT_string (dw_attr_ref);
2924 static enum dwarf_form AT_string_form (dw_attr_ref);
2925 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2926 static void add_AT_specification (dw_die_ref, dw_die_ref);
2927 static inline dw_die_ref AT_ref (dw_attr_ref);
2928 static inline int AT_ref_external (dw_attr_ref);
2929 static inline void set_AT_ref_external (dw_attr_ref, int);
2930 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2931 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2932 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2933 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2934 dw_loc_list_ref);
2935 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2936 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2937 static void remove_addr_table_entry (addr_table_entry *);
2938 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2939 static inline rtx AT_addr (dw_attr_ref);
2940 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2941 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2942 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2943 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2944 unsigned HOST_WIDE_INT);
2945 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2946 unsigned long, bool);
2947 static inline const char *AT_lbl (dw_attr_ref);
2948 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2949 static const char *get_AT_low_pc (dw_die_ref);
2950 static const char *get_AT_hi_pc (dw_die_ref);
2951 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2952 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2953 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2954 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2955 static bool is_cxx (void);
2956 static bool is_fortran (void);
2957 static bool is_ada (void);
2958 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2959 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2960 static void add_child_die (dw_die_ref, dw_die_ref);
2961 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2962 static dw_die_ref lookup_type_die (tree);
2963 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2964 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2965 static void equate_type_number_to_die (tree, dw_die_ref);
2966 static hashval_t decl_die_table_hash (const void *);
2967 static int decl_die_table_eq (const void *, const void *);
2968 static dw_die_ref lookup_decl_die (tree);
2969 static hashval_t common_block_die_table_hash (const void *);
2970 static int common_block_die_table_eq (const void *, const void *);
2971 static hashval_t decl_loc_table_hash (const void *);
2972 static int decl_loc_table_eq (const void *, const void *);
2973 static var_loc_list *lookup_decl_loc (const_tree);
2974 static void equate_decl_number_to_die (tree, dw_die_ref);
2975 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2976 static void print_spaces (FILE *);
2977 static void print_die (dw_die_ref, FILE *);
2978 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2979 static dw_die_ref pop_compile_unit (dw_die_ref);
2980 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2981 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2982 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2983 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2984 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2985 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2986 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2987 struct md5_ctx *, int *);
2988 struct checksum_attributes;
2989 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2990 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2991 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2992 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2993 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2994 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2995 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2996 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2997 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
2998 static void compute_section_prefix (dw_die_ref);
2999 static int is_type_die (dw_die_ref);
3000 static int is_comdat_die (dw_die_ref);
3001 static int is_symbol_die (dw_die_ref);
3002 static void assign_symbol_names (dw_die_ref);
3003 static void break_out_includes (dw_die_ref);
3004 static int is_declaration_die (dw_die_ref);
3005 static int should_move_die_to_comdat (dw_die_ref);
3006 static dw_die_ref clone_as_declaration (dw_die_ref);
3007 static dw_die_ref clone_die (dw_die_ref);
3008 static dw_die_ref clone_tree (dw_die_ref);
3009 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3010 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3011 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3012 static dw_die_ref generate_skeleton (dw_die_ref);
3013 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3014 dw_die_ref,
3015 dw_die_ref);
3016 static void break_out_comdat_types (dw_die_ref);
3017 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3018 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3019 static void copy_decls_for_unworthy_types (dw_die_ref);
3021 static hashval_t htab_cu_hash (const void *);
3022 static int htab_cu_eq (const void *, const void *);
3023 static void htab_cu_del (void *);
3024 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3025 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3026 static void add_sibling_attributes (dw_die_ref);
3027 static void build_abbrev_table (dw_die_ref, htab_t);
3028 static void output_location_lists (dw_die_ref);
3029 static int constant_size (unsigned HOST_WIDE_INT);
3030 static unsigned long size_of_die (dw_die_ref);
3031 static void calc_die_sizes (dw_die_ref);
3032 static void calc_base_type_die_sizes (void);
3033 static void mark_dies (dw_die_ref);
3034 static void unmark_dies (dw_die_ref);
3035 static void unmark_all_dies (dw_die_ref);
3036 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3037 static unsigned long size_of_aranges (void);
3038 static enum dwarf_form value_format (dw_attr_ref);
3039 static void output_value_format (dw_attr_ref);
3040 static void output_abbrev_section (void);
3041 static void output_die_abbrevs (unsigned long, dw_die_ref);
3042 static void output_die_symbol (dw_die_ref);
3043 static void output_die (dw_die_ref);
3044 static void output_compilation_unit_header (void);
3045 static void output_comp_unit (dw_die_ref, int);
3046 static void output_comdat_type_unit (comdat_type_node *);
3047 static const char *dwarf2_name (tree, int);
3048 static void add_pubname (tree, dw_die_ref);
3049 static void add_enumerator_pubname (const char *, dw_die_ref);
3050 static void add_pubname_string (const char *, dw_die_ref);
3051 static void add_pubtype (tree, dw_die_ref);
3052 static void output_pubnames (vec<pubname_entry, va_gc> *);
3053 static void output_aranges (unsigned long);
3054 static unsigned int add_ranges_num (int);
3055 static unsigned int add_ranges (const_tree);
3056 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3057 bool *, bool);
3058 static void output_ranges (void);
3059 static dw_line_info_table *new_line_info_table (void);
3060 static void output_line_info (bool);
3061 static void output_file_names (void);
3062 static dw_die_ref base_type_die (tree);
3063 static int is_base_type (tree);
3064 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3065 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3066 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3067 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3068 static int type_is_enum (const_tree);
3069 static unsigned int dbx_reg_number (const_rtx);
3070 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3071 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3072 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3073 enum var_init_status);
3074 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3075 enum var_init_status);
3076 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3077 enum var_init_status);
3078 static int is_based_loc (const_rtx);
3079 static int resolve_one_addr (rtx *, void *);
3080 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3081 enum var_init_status);
3082 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3083 enum var_init_status);
3084 static dw_loc_list_ref loc_list_from_tree (tree, int);
3085 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3086 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3087 static tree field_type (const_tree);
3088 static unsigned int simple_type_align_in_bits (const_tree);
3089 static unsigned int simple_decl_align_in_bits (const_tree);
3090 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3091 static HOST_WIDE_INT field_byte_offset (const_tree);
3092 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3093 dw_loc_list_ref);
3094 static void add_data_member_location_attribute (dw_die_ref, tree);
3095 static bool add_const_value_attribute (dw_die_ref, rtx);
3096 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3097 static void insert_double (double_int, unsigned char *);
3098 static void insert_float (const_rtx, unsigned char *);
3099 static rtx rtl_for_decl_location (tree);
3100 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3101 enum dwarf_attribute);
3102 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3103 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3104 static void add_name_attribute (dw_die_ref, const char *);
3105 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3106 static void add_comp_dir_attribute (dw_die_ref);
3107 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3108 static void add_subscript_info (dw_die_ref, tree, bool);
3109 static void add_byte_size_attribute (dw_die_ref, tree);
3110 static void add_bit_offset_attribute (dw_die_ref, tree);
3111 static void add_bit_size_attribute (dw_die_ref, tree);
3112 static void add_prototyped_attribute (dw_die_ref, tree);
3113 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3114 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3115 static void add_src_coords_attributes (dw_die_ref, tree);
3116 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3117 static void push_decl_scope (tree);
3118 static void pop_decl_scope (void);
3119 static dw_die_ref scope_die_for (tree, dw_die_ref);
3120 static inline int local_scope_p (dw_die_ref);
3121 static inline int class_scope_p (dw_die_ref);
3122 static inline int class_or_namespace_scope_p (dw_die_ref);
3123 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3124 static void add_calling_convention_attribute (dw_die_ref, tree);
3125 static const char *type_tag (const_tree);
3126 static tree member_declared_type (const_tree);
3127 #if 0
3128 static const char *decl_start_label (tree);
3129 #endif
3130 static void gen_array_type_die (tree, dw_die_ref);
3131 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3132 #if 0
3133 static void gen_entry_point_die (tree, dw_die_ref);
3134 #endif
3135 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3136 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3137 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3138 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3139 static void gen_formal_types_die (tree, dw_die_ref);
3140 static void gen_subprogram_die (tree, dw_die_ref);
3141 static void gen_variable_die (tree, tree, dw_die_ref);
3142 static void gen_const_die (tree, dw_die_ref);
3143 static void gen_label_die (tree, dw_die_ref);
3144 static void gen_lexical_block_die (tree, dw_die_ref, int);
3145 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3146 static void gen_field_die (tree, dw_die_ref);
3147 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3148 static dw_die_ref gen_compile_unit_die (const char *);
3149 static void gen_inheritance_die (tree, tree, dw_die_ref);
3150 static void gen_member_die (tree, dw_die_ref);
3151 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3152 enum debug_info_usage);
3153 static void gen_subroutine_type_die (tree, dw_die_ref);
3154 static void gen_typedef_die (tree, dw_die_ref);
3155 static void gen_type_die (tree, dw_die_ref);
3156 static void gen_block_die (tree, dw_die_ref, int);
3157 static void decls_for_scope (tree, dw_die_ref, int);
3158 static inline int is_redundant_typedef (const_tree);
3159 static bool is_naming_typedef_decl (const_tree);
3160 static inline dw_die_ref get_context_die (tree);
3161 static void gen_namespace_die (tree, dw_die_ref);
3162 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3163 static dw_die_ref force_decl_die (tree);
3164 static dw_die_ref force_type_die (tree);
3165 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3166 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3167 static struct dwarf_file_data * lookup_filename (const char *);
3168 static void retry_incomplete_types (void);
3169 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3170 static void gen_generic_params_dies (tree);
3171 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3172 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3173 static void splice_child_die (dw_die_ref, dw_die_ref);
3174 static int file_info_cmp (const void *, const void *);
3175 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3176 const char *, const char *);
3177 static void output_loc_list (dw_loc_list_ref);
3178 static char *gen_internal_sym (const char *);
3179 static bool want_pubnames (void);
3181 static void prune_unmark_dies (dw_die_ref);
3182 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3183 static void prune_unused_types_mark (dw_die_ref, int);
3184 static void prune_unused_types_walk (dw_die_ref);
3185 static void prune_unused_types_walk_attribs (dw_die_ref);
3186 static void prune_unused_types_prune (dw_die_ref);
3187 static void prune_unused_types (void);
3188 static int maybe_emit_file (struct dwarf_file_data *fd);
3189 static inline const char *AT_vms_delta1 (dw_attr_ref);
3190 static inline const char *AT_vms_delta2 (dw_attr_ref);
3191 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3192 const char *, const char *);
3193 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3194 static void gen_remaining_tmpl_value_param_die_attribute (void);
3195 static bool generic_type_p (tree);
3196 static void schedule_generic_params_dies_gen (tree t);
3197 static void gen_scheduled_generic_parms_dies (void);
3199 /* enum for tracking thread-local variables whose address is really an offset
3200 relative to the TLS pointer, which will need link-time relocation, but will
3201 not need relocation by the DWARF consumer. */
3203 enum dtprel_bool
3205 dtprel_false = 0,
3206 dtprel_true = 1
3209 /* Return the operator to use for an address of a variable. For dtprel_true, we
3210 use DW_OP_const*. For regular variables, which need both link-time
3211 relocation and consumer-level relocation (e.g., to account for shared objects
3212 loaded at a random address), we use DW_OP_addr*. */
3214 static inline enum dwarf_location_atom
3215 dw_addr_op (enum dtprel_bool dtprel)
3217 if (dtprel == dtprel_true)
3218 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3219 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3220 else
3221 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3224 /* Return a pointer to a newly allocated address location description. If
3225 dwarf_split_debug_info is true, then record the address with the appropriate
3226 relocation. */
3227 static inline dw_loc_descr_ref
3228 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3230 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3232 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3233 ref->dw_loc_oprnd1.v.val_addr = addr;
3234 ref->dtprel = dtprel;
3235 if (dwarf_split_debug_info)
3236 ref->dw_loc_oprnd1.val_entry
3237 = add_addr_table_entry (addr,
3238 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3239 else
3240 ref->dw_loc_oprnd1.val_entry = NULL;
3242 return ref;
3245 /* Section names used to hold DWARF debugging information. */
3247 #ifndef DEBUG_INFO_SECTION
3248 #define DEBUG_INFO_SECTION ".debug_info"
3249 #endif
3250 #ifndef DEBUG_DWO_INFO_SECTION
3251 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3252 #endif
3253 #ifndef DEBUG_ABBREV_SECTION
3254 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3255 #endif
3256 #ifndef DEBUG_DWO_ABBREV_SECTION
3257 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3258 #endif
3259 #ifndef DEBUG_ARANGES_SECTION
3260 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3261 #endif
3262 #ifndef DEBUG_ADDR_SECTION
3263 #define DEBUG_ADDR_SECTION ".debug_addr"
3264 #endif
3265 #ifndef DEBUG_NORM_MACINFO_SECTION
3266 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3267 #endif
3268 #ifndef DEBUG_DWO_MACINFO_SECTION
3269 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3270 #endif
3271 #ifndef DEBUG_MACINFO_SECTION
3272 #define DEBUG_MACINFO_SECTION \
3273 (!dwarf_split_debug_info \
3274 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3275 #endif
3276 #ifndef DEBUG_NORM_MACRO_SECTION
3277 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3278 #endif
3279 #ifndef DEBUG_DWO_MACRO_SECTION
3280 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3281 #endif
3282 #ifndef DEBUG_MACRO_SECTION
3283 #define DEBUG_MACRO_SECTION \
3284 (!dwarf_split_debug_info \
3285 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3286 #endif
3287 #ifndef DEBUG_LINE_SECTION
3288 #define DEBUG_LINE_SECTION ".debug_line"
3289 #endif
3290 #ifndef DEBUG_DWO_LINE_SECTION
3291 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3292 #endif
3293 #ifndef DEBUG_LOC_SECTION
3294 #define DEBUG_LOC_SECTION ".debug_loc"
3295 #endif
3296 #ifndef DEBUG_DWO_LOC_SECTION
3297 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3298 #endif
3299 #ifndef DEBUG_PUBNAMES_SECTION
3300 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3301 #endif
3302 #ifndef DEBUG_PUBTYPES_SECTION
3303 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3304 #endif
3305 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3306 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3307 #ifndef DEBUG_STR_OFFSETS_SECTION
3308 #define DEBUG_STR_OFFSETS_SECTION \
3309 (!dwarf_split_debug_info \
3310 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3311 #endif
3312 #define DEBUG_DWO_STR_SECTION ".debug_str.dwo"
3313 #define DEBUG_NORM_STR_SECTION ".debug_str"
3314 #ifndef DEBUG_STR_SECTION
3315 #define DEBUG_STR_SECTION \
3316 (!dwarf_split_debug_info ? (DEBUG_NORM_STR_SECTION) : (DEBUG_DWO_STR_SECTION))
3317 #endif
3318 #ifndef DEBUG_RANGES_SECTION
3319 #define DEBUG_RANGES_SECTION ".debug_ranges"
3320 #endif
3322 /* Standard ELF section names for compiled code and data. */
3323 #ifndef TEXT_SECTION_NAME
3324 #define TEXT_SECTION_NAME ".text"
3325 #endif
3327 /* Section flags for .debug_macinfo/.debug_macro section. */
3328 #define DEBUG_MACRO_SECTION_FLAGS \
3329 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3331 /* Section flags for .debug_str section. */
3332 #define DEBUG_STR_SECTION_FLAGS \
3333 (dwarf_split_debug_info \
3334 ? SECTION_DEBUG | SECTION_EXCLUDE \
3335 : (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3336 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3337 : SECTION_DEBUG))
3339 /* Labels we insert at beginning sections we can reference instead of
3340 the section names themselves. */
3342 #ifndef TEXT_SECTION_LABEL
3343 #define TEXT_SECTION_LABEL "Ltext"
3344 #endif
3345 #ifndef COLD_TEXT_SECTION_LABEL
3346 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3347 #endif
3348 #ifndef DEBUG_LINE_SECTION_LABEL
3349 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3350 #endif
3351 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3352 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3353 #endif
3354 #ifndef DEBUG_INFO_SECTION_LABEL
3355 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3356 #endif
3357 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3358 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3359 #endif
3360 #ifndef DEBUG_ABBREV_SECTION_LABEL
3361 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3362 #endif
3363 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3364 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3365 #endif
3366 #ifndef DEBUG_ADDR_SECTION_LABEL
3367 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3368 #endif
3369 #ifndef DEBUG_LOC_SECTION_LABEL
3370 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3371 #endif
3372 #ifndef DEBUG_RANGES_SECTION_LABEL
3373 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3374 #endif
3375 #ifndef DEBUG_MACINFO_SECTION_LABEL
3376 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3377 #endif
3378 #ifndef DEBUG_MACRO_SECTION_LABEL
3379 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3380 #endif
3381 #define SKELETON_COMP_DIE_ABBREV 1
3382 #define SKELETON_TYPE_DIE_ABBREV 2
3384 /* Definitions of defaults for formats and names of various special
3385 (artificial) labels which may be generated within this file (when the -g
3386 options is used and DWARF2_DEBUGGING_INFO is in effect.
3387 If necessary, these may be overridden from within the tm.h file, but
3388 typically, overriding these defaults is unnecessary. */
3390 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3391 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3392 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3393 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3394 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3395 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3396 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3397 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3398 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3399 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3400 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3401 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3402 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3403 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3405 #ifndef TEXT_END_LABEL
3406 #define TEXT_END_LABEL "Letext"
3407 #endif
3408 #ifndef COLD_END_LABEL
3409 #define COLD_END_LABEL "Letext_cold"
3410 #endif
3411 #ifndef BLOCK_BEGIN_LABEL
3412 #define BLOCK_BEGIN_LABEL "LBB"
3413 #endif
3414 #ifndef BLOCK_END_LABEL
3415 #define BLOCK_END_LABEL "LBE"
3416 #endif
3417 #ifndef LINE_CODE_LABEL
3418 #define LINE_CODE_LABEL "LM"
3419 #endif
3422 /* Return the root of the DIE's built for the current compilation unit. */
3423 static dw_die_ref
3424 comp_unit_die (void)
3426 if (!single_comp_unit_die)
3427 single_comp_unit_die = gen_compile_unit_die (NULL);
3428 return single_comp_unit_die;
3431 /* We allow a language front-end to designate a function that is to be
3432 called to "demangle" any name before it is put into a DIE. */
3434 static const char *(*demangle_name_func) (const char *);
3436 void
3437 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3439 demangle_name_func = func;
3442 /* Test if rtl node points to a pseudo register. */
3444 static inline int
3445 is_pseudo_reg (const_rtx rtl)
3447 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3448 || (GET_CODE (rtl) == SUBREG
3449 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3452 /* Return a reference to a type, with its const and volatile qualifiers
3453 removed. */
3455 static inline tree
3456 type_main_variant (tree type)
3458 type = TYPE_MAIN_VARIANT (type);
3460 /* ??? There really should be only one main variant among any group of
3461 variants of a given type (and all of the MAIN_VARIANT values for all
3462 members of the group should point to that one type) but sometimes the C
3463 front-end messes this up for array types, so we work around that bug
3464 here. */
3465 if (TREE_CODE (type) == ARRAY_TYPE)
3466 while (type != TYPE_MAIN_VARIANT (type))
3467 type = TYPE_MAIN_VARIANT (type);
3469 return type;
3472 /* Return nonzero if the given type node represents a tagged type. */
3474 static inline int
3475 is_tagged_type (const_tree type)
3477 enum tree_code code = TREE_CODE (type);
3479 return (code == RECORD_TYPE || code == UNION_TYPE
3480 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3483 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3485 static void
3486 get_ref_die_offset_label (char *label, dw_die_ref ref)
3488 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3491 /* Return die_offset of a DIE reference to a base type. */
3493 static unsigned long int
3494 get_base_type_offset (dw_die_ref ref)
3496 if (ref->die_offset)
3497 return ref->die_offset;
3498 if (comp_unit_die ()->die_abbrev)
3500 calc_base_type_die_sizes ();
3501 gcc_assert (ref->die_offset);
3503 return ref->die_offset;
3506 /* Return die_offset of a DIE reference other than base type. */
3508 static unsigned long int
3509 get_ref_die_offset (dw_die_ref ref)
3511 gcc_assert (ref->die_offset);
3512 return ref->die_offset;
3515 /* Convert a DIE tag into its string name. */
3517 static const char *
3518 dwarf_tag_name (unsigned int tag)
3520 const char *name = get_DW_TAG_name (tag);
3522 if (name != NULL)
3523 return name;
3525 return "DW_TAG_<unknown>";
3528 /* Convert a DWARF attribute code into its string name. */
3530 static const char *
3531 dwarf_attr_name (unsigned int attr)
3533 const char *name;
3535 switch (attr)
3537 #if VMS_DEBUGGING_INFO
3538 case DW_AT_HP_prologue:
3539 return "DW_AT_HP_prologue";
3540 #else
3541 case DW_AT_MIPS_loop_unroll_factor:
3542 return "DW_AT_MIPS_loop_unroll_factor";
3543 #endif
3545 #if VMS_DEBUGGING_INFO
3546 case DW_AT_HP_epilogue:
3547 return "DW_AT_HP_epilogue";
3548 #else
3549 case DW_AT_MIPS_stride:
3550 return "DW_AT_MIPS_stride";
3551 #endif
3554 name = get_DW_AT_name (attr);
3556 if (name != NULL)
3557 return name;
3559 return "DW_AT_<unknown>";
3562 /* Convert a DWARF value form code into its string name. */
3564 static const char *
3565 dwarf_form_name (unsigned int form)
3567 const char *name = get_DW_FORM_name (form);
3569 if (name != NULL)
3570 return name;
3572 return "DW_FORM_<unknown>";
3575 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3576 instance of an inlined instance of a decl which is local to an inline
3577 function, so we have to trace all of the way back through the origin chain
3578 to find out what sort of node actually served as the original seed for the
3579 given block. */
3581 static tree
3582 decl_ultimate_origin (const_tree decl)
3584 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3585 return NULL_TREE;
3587 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3588 nodes in the function to point to themselves; ignore that if
3589 we're trying to output the abstract instance of this function. */
3590 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3591 return NULL_TREE;
3593 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3594 most distant ancestor, this should never happen. */
3595 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3597 return DECL_ABSTRACT_ORIGIN (decl);
3600 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3601 of a virtual function may refer to a base class, so we check the 'this'
3602 parameter. */
3604 static tree
3605 decl_class_context (tree decl)
3607 tree context = NULL_TREE;
3609 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3610 context = DECL_CONTEXT (decl);
3611 else
3612 context = TYPE_MAIN_VARIANT
3613 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3615 if (context && !TYPE_P (context))
3616 context = NULL_TREE;
3618 return context;
3621 /* Add an attribute/value pair to a DIE. */
3623 static inline void
3624 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3626 /* Maybe this should be an assert? */
3627 if (die == NULL)
3628 return;
3630 vec_safe_reserve (die->die_attr, 1);
3631 vec_safe_push (die->die_attr, *attr);
3634 static inline enum dw_val_class
3635 AT_class (dw_attr_ref a)
3637 return a->dw_attr_val.val_class;
3640 /* Return the index for any attribute that will be referenced with a
3641 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3642 are stored in dw_attr_val.v.val_str for reference counting
3643 pruning. */
3645 static inline unsigned int
3646 AT_index (dw_attr_ref a)
3648 if (AT_class (a) == dw_val_class_str)
3649 return a->dw_attr_val.v.val_str->index;
3650 else if (a->dw_attr_val.val_entry != NULL)
3651 return a->dw_attr_val.val_entry->index;
3652 return NOT_INDEXED;
3655 /* Add a flag value attribute to a DIE. */
3657 static inline void
3658 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3660 dw_attr_node attr;
3662 attr.dw_attr = attr_kind;
3663 attr.dw_attr_val.val_class = dw_val_class_flag;
3664 attr.dw_attr_val.val_entry = NULL;
3665 attr.dw_attr_val.v.val_flag = flag;
3666 add_dwarf_attr (die, &attr);
3669 static inline unsigned
3670 AT_flag (dw_attr_ref a)
3672 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3673 return a->dw_attr_val.v.val_flag;
3676 /* Add a signed integer attribute value to a DIE. */
3678 static inline void
3679 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3681 dw_attr_node attr;
3683 attr.dw_attr = attr_kind;
3684 attr.dw_attr_val.val_class = dw_val_class_const;
3685 attr.dw_attr_val.val_entry = NULL;
3686 attr.dw_attr_val.v.val_int = int_val;
3687 add_dwarf_attr (die, &attr);
3690 static inline HOST_WIDE_INT
3691 AT_int (dw_attr_ref a)
3693 gcc_assert (a && AT_class (a) == dw_val_class_const);
3694 return a->dw_attr_val.v.val_int;
3697 /* Add an unsigned integer attribute value to a DIE. */
3699 static inline void
3700 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3701 unsigned HOST_WIDE_INT unsigned_val)
3703 dw_attr_node attr;
3705 attr.dw_attr = attr_kind;
3706 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3707 attr.dw_attr_val.val_entry = NULL;
3708 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3709 add_dwarf_attr (die, &attr);
3712 static inline unsigned HOST_WIDE_INT
3713 AT_unsigned (dw_attr_ref a)
3715 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3716 return a->dw_attr_val.v.val_unsigned;
3719 /* Add an unsigned double integer attribute value to a DIE. */
3721 static inline void
3722 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3723 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3725 dw_attr_node attr;
3727 attr.dw_attr = attr_kind;
3728 attr.dw_attr_val.val_class = dw_val_class_const_double;
3729 attr.dw_attr_val.val_entry = NULL;
3730 attr.dw_attr_val.v.val_double.high = high;
3731 attr.dw_attr_val.v.val_double.low = low;
3732 add_dwarf_attr (die, &attr);
3735 /* Add a floating point attribute value to a DIE and return it. */
3737 static inline void
3738 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3739 unsigned int length, unsigned int elt_size, unsigned char *array)
3741 dw_attr_node attr;
3743 attr.dw_attr = attr_kind;
3744 attr.dw_attr_val.val_class = dw_val_class_vec;
3745 attr.dw_attr_val.val_entry = NULL;
3746 attr.dw_attr_val.v.val_vec.length = length;
3747 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3748 attr.dw_attr_val.v.val_vec.array = array;
3749 add_dwarf_attr (die, &attr);
3752 /* Add an 8-byte data attribute value to a DIE. */
3754 static inline void
3755 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3756 unsigned char data8[8])
3758 dw_attr_node attr;
3760 attr.dw_attr = attr_kind;
3761 attr.dw_attr_val.val_class = dw_val_class_data8;
3762 attr.dw_attr_val.val_entry = NULL;
3763 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3764 add_dwarf_attr (die, &attr);
3767 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3768 dwarf_split_debug_info, address attributes in dies destined for the
3769 final executable have force_direct set to avoid using indexed
3770 references. */
3772 static inline void
3773 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3774 bool force_direct)
3776 dw_attr_node attr;
3777 char * lbl_id;
3779 lbl_id = xstrdup (lbl_low);
3780 attr.dw_attr = DW_AT_low_pc;
3781 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3782 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3783 if (dwarf_split_debug_info && !force_direct)
3784 attr.dw_attr_val.val_entry
3785 = add_addr_table_entry (lbl_id, ate_kind_label);
3786 else
3787 attr.dw_attr_val.val_entry = NULL;
3788 add_dwarf_attr (die, &attr);
3790 attr.dw_attr = DW_AT_high_pc;
3791 if (dwarf_version < 4)
3792 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3793 else
3794 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3795 lbl_id = xstrdup (lbl_high);
3796 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3797 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3798 && dwarf_split_debug_info && !force_direct)
3799 attr.dw_attr_val.val_entry
3800 = add_addr_table_entry (lbl_id, ate_kind_label);
3801 else
3802 attr.dw_attr_val.val_entry = NULL;
3803 add_dwarf_attr (die, &attr);
3806 /* Hash and equality functions for debug_str_hash. */
3808 static hashval_t
3809 debug_str_do_hash (const void *x)
3811 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3814 static int
3815 debug_str_eq (const void *x1, const void *x2)
3817 return strcmp ((((const struct indirect_string_node *)x1)->str),
3818 (const char *)x2) == 0;
3821 /* Add STR to the indirect string hash table. */
3823 static struct indirect_string_node *
3824 find_AT_string (const char *str)
3826 struct indirect_string_node *node;
3827 void **slot;
3829 if (! debug_str_hash)
3830 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3831 debug_str_eq, NULL);
3833 slot = htab_find_slot_with_hash (debug_str_hash, str,
3834 htab_hash_string (str), INSERT);
3835 if (*slot == NULL)
3837 node = ggc_alloc_cleared_indirect_string_node ();
3838 node->str = ggc_strdup (str);
3839 *slot = node;
3841 else
3842 node = (struct indirect_string_node *) *slot;
3844 node->refcount++;
3845 return node;
3848 /* Add a string attribute value to a DIE. */
3850 static inline void
3851 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3853 dw_attr_node attr;
3854 struct indirect_string_node *node;
3856 node = find_AT_string (str);
3858 attr.dw_attr = attr_kind;
3859 attr.dw_attr_val.val_class = dw_val_class_str;
3860 attr.dw_attr_val.val_entry = NULL;
3861 attr.dw_attr_val.v.val_str = node;
3862 add_dwarf_attr (die, &attr);
3865 static inline const char *
3866 AT_string (dw_attr_ref a)
3868 gcc_assert (a && AT_class (a) == dw_val_class_str);
3869 return a->dw_attr_val.v.val_str->str;
3872 /* Call this function directly to bypass AT_string_form's logic to put
3873 the string inline in the die. */
3875 static void
3876 set_indirect_string (struct indirect_string_node *node)
3878 char label[32];
3879 /* Already indirect is a no op. */
3880 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3882 gcc_assert (node->label);
3883 return;
3885 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3886 ++dw2_string_counter;
3887 node->label = xstrdup (label);
3889 if (!dwarf_split_debug_info)
3891 node->form = DW_FORM_strp;
3892 node->index = NOT_INDEXED;
3894 else
3896 node->form = DW_FORM_GNU_str_index;
3897 node->index = NO_INDEX_ASSIGNED;
3901 /* Find out whether a string should be output inline in DIE
3902 or out-of-line in .debug_str section. */
3904 static enum dwarf_form
3905 find_string_form (struct indirect_string_node *node)
3907 unsigned int len;
3909 if (node->form)
3910 return node->form;
3912 len = strlen (node->str) + 1;
3914 /* If the string is shorter or equal to the size of the reference, it is
3915 always better to put it inline. */
3916 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3917 return node->form = DW_FORM_string;
3919 /* If we cannot expect the linker to merge strings in .debug_str
3920 section, only put it into .debug_str if it is worth even in this
3921 single module. */
3922 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3923 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3924 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3925 return node->form = DW_FORM_string;
3927 set_indirect_string (node);
3929 return node->form;
3932 /* Find out whether the string referenced from the attribute should be
3933 output inline in DIE or out-of-line in .debug_str section. */
3935 static enum dwarf_form
3936 AT_string_form (dw_attr_ref a)
3938 gcc_assert (a && AT_class (a) == dw_val_class_str);
3939 return find_string_form (a->dw_attr_val.v.val_str);
3942 /* Add a DIE reference attribute value to a DIE. */
3944 static inline void
3945 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3947 dw_attr_node attr;
3949 #ifdef ENABLE_CHECKING
3950 gcc_assert (targ_die != NULL);
3951 #else
3952 /* With LTO we can end up trying to reference something we didn't create
3953 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3954 if (targ_die == NULL)
3955 return;
3956 #endif
3958 attr.dw_attr = attr_kind;
3959 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3960 attr.dw_attr_val.val_entry = NULL;
3961 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3962 attr.dw_attr_val.v.val_die_ref.external = 0;
3963 add_dwarf_attr (die, &attr);
3966 /* Change DIE reference REF to point to NEW_DIE instead. */
3968 static inline void
3969 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3971 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3972 ref->dw_attr_val.v.val_die_ref.die = new_die;
3973 ref->dw_attr_val.v.val_die_ref.external = 0;
3976 /* Add an AT_specification attribute to a DIE, and also make the back
3977 pointer from the specification to the definition. */
3979 static inline void
3980 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3982 add_AT_die_ref (die, DW_AT_specification, targ_die);
3983 gcc_assert (!targ_die->die_definition);
3984 targ_die->die_definition = die;
3987 static inline dw_die_ref
3988 AT_ref (dw_attr_ref a)
3990 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3991 return a->dw_attr_val.v.val_die_ref.die;
3994 static inline int
3995 AT_ref_external (dw_attr_ref a)
3997 if (a && AT_class (a) == dw_val_class_die_ref)
3998 return a->dw_attr_val.v.val_die_ref.external;
4000 return 0;
4003 static inline void
4004 set_AT_ref_external (dw_attr_ref a, int i)
4006 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4007 a->dw_attr_val.v.val_die_ref.external = i;
4010 /* Add an FDE reference attribute value to a DIE. */
4012 static inline void
4013 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4015 dw_attr_node attr;
4017 attr.dw_attr = attr_kind;
4018 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4019 attr.dw_attr_val.val_entry = NULL;
4020 attr.dw_attr_val.v.val_fde_index = targ_fde;
4021 add_dwarf_attr (die, &attr);
4024 /* Add a location description attribute value to a DIE. */
4026 static inline void
4027 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4029 dw_attr_node attr;
4031 attr.dw_attr = attr_kind;
4032 attr.dw_attr_val.val_class = dw_val_class_loc;
4033 attr.dw_attr_val.val_entry = NULL;
4034 attr.dw_attr_val.v.val_loc = loc;
4035 add_dwarf_attr (die, &attr);
4038 static inline dw_loc_descr_ref
4039 AT_loc (dw_attr_ref a)
4041 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4042 return a->dw_attr_val.v.val_loc;
4045 static inline void
4046 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4048 dw_attr_node attr;
4050 attr.dw_attr = attr_kind;
4051 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4052 attr.dw_attr_val.val_entry = NULL;
4053 attr.dw_attr_val.v.val_loc_list = loc_list;
4054 add_dwarf_attr (die, &attr);
4055 have_location_lists = true;
4058 static inline dw_loc_list_ref
4059 AT_loc_list (dw_attr_ref a)
4061 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4062 return a->dw_attr_val.v.val_loc_list;
4065 static inline dw_loc_list_ref *
4066 AT_loc_list_ptr (dw_attr_ref a)
4068 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4069 return &a->dw_attr_val.v.val_loc_list;
4072 /* Table of entries into the .debug_addr section. */
4074 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4076 /* Hash an address_table_entry. */
4078 static hashval_t
4079 addr_table_entry_do_hash (const void *x)
4081 const addr_table_entry *a = (const addr_table_entry *) x;
4082 switch (a->kind)
4084 case ate_kind_rtx:
4085 return iterative_hash_rtx (a->addr.rtl, 0);
4086 case ate_kind_rtx_dtprel:
4087 return iterative_hash_rtx (a->addr.rtl, 1);
4088 case ate_kind_label:
4089 return htab_hash_string (a->addr.label);
4090 default:
4091 gcc_unreachable ();
4095 /* Determine equality for two address_table_entries. */
4097 static int
4098 addr_table_entry_eq (const void *x1, const void *x2)
4100 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4101 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4103 if (a1->kind != a2->kind)
4104 return 0;
4105 switch (a1->kind)
4107 case ate_kind_rtx:
4108 case ate_kind_rtx_dtprel:
4109 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4110 case ate_kind_label:
4111 return strcmp (a1->addr.label, a2->addr.label) == 0;
4112 default:
4113 gcc_unreachable ();
4117 /* Initialize an addr_table_entry. */
4119 void
4120 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4122 e->kind = kind;
4123 switch (kind)
4125 case ate_kind_rtx:
4126 case ate_kind_rtx_dtprel:
4127 e->addr.rtl = (rtx) addr;
4128 break;
4129 case ate_kind_label:
4130 e->addr.label = (char *) addr;
4131 break;
4133 e->refcount = 0;
4134 e->index = NO_INDEX_ASSIGNED;
4137 /* Add attr to the address table entry to the table. Defer setting an
4138 index until output time. */
4140 static addr_table_entry *
4141 add_addr_table_entry (void *addr, enum ate_kind kind)
4143 addr_table_entry *node;
4144 addr_table_entry finder;
4145 void **slot;
4147 gcc_assert (dwarf_split_debug_info);
4148 if (! addr_index_table)
4149 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4150 addr_table_entry_eq, NULL);
4151 init_addr_table_entry (&finder, kind, addr);
4152 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4154 if (*slot == HTAB_EMPTY_ENTRY)
4156 node = ggc_alloc_cleared_addr_table_entry ();
4157 init_addr_table_entry (node, kind, addr);
4158 *slot = node;
4160 else
4161 node = (addr_table_entry *) *slot;
4163 node->refcount++;
4164 return node;
4167 /* Remove an entry from the addr table by decrementing its refcount.
4168 Strictly, decrementing the refcount would be enough, but the
4169 assertion that the entry is actually in the table has found
4170 bugs. */
4172 static void
4173 remove_addr_table_entry (addr_table_entry *entry)
4175 addr_table_entry *node;
4177 gcc_assert (dwarf_split_debug_info && addr_index_table);
4178 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4179 /* After an index is assigned, the table is frozen. */
4180 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4181 node->refcount--;
4184 /* Given a location list, remove all addresses it refers to from the
4185 address_table. */
4187 static void
4188 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4190 for (; descr; descr = descr->dw_loc_next)
4191 if (descr->dw_loc_oprnd1.val_entry != NULL)
4193 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4194 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4198 /* A helper function for dwarf2out_finish called through
4199 htab_traverse. Assign an addr_table_entry its index. All entries
4200 must be collected into the table when this function is called,
4201 because the indexing code relies on htab_traverse to traverse nodes
4202 in the same order for each run. */
4204 static int
4205 index_addr_table_entry (void **h, void *v)
4207 addr_table_entry *node = (addr_table_entry *) *h;
4208 unsigned int *index = (unsigned int *) v;
4210 /* Don't index unreferenced nodes. */
4211 if (node->refcount == 0)
4212 return 1;
4214 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4215 node->index = *index;
4216 *index += 1;
4218 return 1;
4221 /* Add an address constant attribute value to a DIE. When using
4222 dwarf_split_debug_info, address attributes in dies destined for the
4223 final executable should be direct references--setting the parameter
4224 force_direct ensures this behavior. */
4226 static inline void
4227 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4228 bool force_direct)
4230 dw_attr_node attr;
4232 attr.dw_attr = attr_kind;
4233 attr.dw_attr_val.val_class = dw_val_class_addr;
4234 attr.dw_attr_val.v.val_addr = addr;
4235 if (dwarf_split_debug_info && !force_direct)
4236 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4237 else
4238 attr.dw_attr_val.val_entry = NULL;
4239 add_dwarf_attr (die, &attr);
4242 /* Get the RTX from to an address DIE attribute. */
4244 static inline rtx
4245 AT_addr (dw_attr_ref a)
4247 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4248 return a->dw_attr_val.v.val_addr;
4251 /* Add a file attribute value to a DIE. */
4253 static inline void
4254 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4255 struct dwarf_file_data *fd)
4257 dw_attr_node attr;
4259 attr.dw_attr = attr_kind;
4260 attr.dw_attr_val.val_class = dw_val_class_file;
4261 attr.dw_attr_val.val_entry = NULL;
4262 attr.dw_attr_val.v.val_file = fd;
4263 add_dwarf_attr (die, &attr);
4266 /* Get the dwarf_file_data from a file DIE attribute. */
4268 static inline struct dwarf_file_data *
4269 AT_file (dw_attr_ref a)
4271 gcc_assert (a && AT_class (a) == dw_val_class_file);
4272 return a->dw_attr_val.v.val_file;
4275 /* Add a vms delta attribute value to a DIE. */
4277 static inline void
4278 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4279 const char *lbl1, const char *lbl2)
4281 dw_attr_node attr;
4283 attr.dw_attr = attr_kind;
4284 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4285 attr.dw_attr_val.val_entry = NULL;
4286 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4287 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4288 add_dwarf_attr (die, &attr);
4291 /* Add a label identifier attribute value to a DIE. */
4293 static inline void
4294 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4295 const char *lbl_id)
4297 dw_attr_node attr;
4299 attr.dw_attr = attr_kind;
4300 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4301 attr.dw_attr_val.val_entry = NULL;
4302 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4303 if (dwarf_split_debug_info)
4304 attr.dw_attr_val.val_entry
4305 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4306 ate_kind_label);
4307 add_dwarf_attr (die, &attr);
4310 /* Add a section offset attribute value to a DIE, an offset into the
4311 debug_line section. */
4313 static inline void
4314 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4315 const char *label)
4317 dw_attr_node attr;
4319 attr.dw_attr = attr_kind;
4320 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4321 attr.dw_attr_val.val_entry = NULL;
4322 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4323 add_dwarf_attr (die, &attr);
4326 /* Add a section offset attribute value to a DIE, an offset into the
4327 debug_macinfo section. */
4329 static inline void
4330 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4331 const char *label)
4333 dw_attr_node attr;
4335 attr.dw_attr = attr_kind;
4336 attr.dw_attr_val.val_class = dw_val_class_macptr;
4337 attr.dw_attr_val.val_entry = NULL;
4338 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4339 add_dwarf_attr (die, &attr);
4342 /* Add an offset attribute value to a DIE. */
4344 static inline void
4345 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4346 unsigned HOST_WIDE_INT offset)
4348 dw_attr_node attr;
4350 attr.dw_attr = attr_kind;
4351 attr.dw_attr_val.val_class = dw_val_class_offset;
4352 attr.dw_attr_val.val_entry = NULL;
4353 attr.dw_attr_val.v.val_offset = offset;
4354 add_dwarf_attr (die, &attr);
4357 /* Add a range_list attribute value to a DIE. When using
4358 dwarf_split_debug_info, address attributes in dies destined for the
4359 final executable should be direct references--setting the parameter
4360 force_direct ensures this behavior. */
4362 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4363 #define RELOCATED_OFFSET (NULL)
4365 static void
4366 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4367 long unsigned int offset, bool force_direct)
4369 dw_attr_node attr;
4371 attr.dw_attr = attr_kind;
4372 attr.dw_attr_val.val_class = dw_val_class_range_list;
4373 /* For the range_list attribute, use val_entry to store whether the
4374 offset should follow split-debug-info or normal semantics. This
4375 value is read in output_range_list_offset. */
4376 if (dwarf_split_debug_info && !force_direct)
4377 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4378 else
4379 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4380 attr.dw_attr_val.v.val_offset = offset;
4381 add_dwarf_attr (die, &attr);
4384 /* Return the start label of a delta attribute. */
4386 static inline const char *
4387 AT_vms_delta1 (dw_attr_ref a)
4389 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4390 return a->dw_attr_val.v.val_vms_delta.lbl1;
4393 /* Return the end label of a delta attribute. */
4395 static inline const char *
4396 AT_vms_delta2 (dw_attr_ref a)
4398 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4399 return a->dw_attr_val.v.val_vms_delta.lbl2;
4402 static inline const char *
4403 AT_lbl (dw_attr_ref a)
4405 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4406 || AT_class (a) == dw_val_class_lineptr
4407 || AT_class (a) == dw_val_class_macptr
4408 || AT_class (a) == dw_val_class_high_pc));
4409 return a->dw_attr_val.v.val_lbl_id;
4412 /* Get the attribute of type attr_kind. */
4414 static dw_attr_ref
4415 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4417 dw_attr_ref a;
4418 unsigned ix;
4419 dw_die_ref spec = NULL;
4421 if (! die)
4422 return NULL;
4424 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4425 if (a->dw_attr == attr_kind)
4426 return a;
4427 else if (a->dw_attr == DW_AT_specification
4428 || a->dw_attr == DW_AT_abstract_origin)
4429 spec = AT_ref (a);
4431 if (spec)
4432 return get_AT (spec, attr_kind);
4434 return NULL;
4437 /* Returns the parent of the declaration of DIE. */
4439 static dw_die_ref
4440 get_die_parent (dw_die_ref die)
4442 dw_die_ref t;
4444 if (!die)
4445 return NULL;
4447 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4448 || (t = get_AT_ref (die, DW_AT_specification)))
4449 die = t;
4451 return die->die_parent;
4454 /* Return the "low pc" attribute value, typically associated with a subprogram
4455 DIE. Return null if the "low pc" attribute is either not present, or if it
4456 cannot be represented as an assembler label identifier. */
4458 static inline const char *
4459 get_AT_low_pc (dw_die_ref die)
4461 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4463 return a ? AT_lbl (a) : NULL;
4466 /* Return the "high pc" attribute value, typically associated with a subprogram
4467 DIE. Return null if the "high pc" attribute is either not present, or if it
4468 cannot be represented as an assembler label identifier. */
4470 static inline const char *
4471 get_AT_hi_pc (dw_die_ref die)
4473 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4475 return a ? AT_lbl (a) : NULL;
4478 /* Return the value of the string attribute designated by ATTR_KIND, or
4479 NULL if it is not present. */
4481 static inline const char *
4482 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4484 dw_attr_ref a = get_AT (die, attr_kind);
4486 return a ? AT_string (a) : NULL;
4489 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4490 if it is not present. */
4492 static inline int
4493 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4495 dw_attr_ref a = get_AT (die, attr_kind);
4497 return a ? AT_flag (a) : 0;
4500 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4501 if it is not present. */
4503 static inline unsigned
4504 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4506 dw_attr_ref a = get_AT (die, attr_kind);
4508 return a ? AT_unsigned (a) : 0;
4511 static inline dw_die_ref
4512 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4514 dw_attr_ref a = get_AT (die, attr_kind);
4516 return a ? AT_ref (a) : NULL;
4519 static inline struct dwarf_file_data *
4520 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4522 dw_attr_ref a = get_AT (die, attr_kind);
4524 return a ? AT_file (a) : NULL;
4527 /* Return TRUE if the language is C++. */
4529 static inline bool
4530 is_cxx (void)
4532 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4534 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4537 /* Return TRUE if the language is Fortran. */
4539 static inline bool
4540 is_fortran (void)
4542 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4544 return (lang == DW_LANG_Fortran77
4545 || lang == DW_LANG_Fortran90
4546 || lang == DW_LANG_Fortran95);
4549 /* Return TRUE if the language is Ada. */
4551 static inline bool
4552 is_ada (void)
4554 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4556 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4559 /* Remove the specified attribute if present. */
4561 static void
4562 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4564 dw_attr_ref a;
4565 unsigned ix;
4567 if (! die)
4568 return;
4570 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4571 if (a->dw_attr == attr_kind)
4573 if (AT_class (a) == dw_val_class_str)
4574 if (a->dw_attr_val.v.val_str->refcount)
4575 a->dw_attr_val.v.val_str->refcount--;
4577 /* vec::ordered_remove should help reduce the number of abbrevs
4578 that are needed. */
4579 die->die_attr->ordered_remove (ix);
4580 return;
4584 /* Remove CHILD from its parent. PREV must have the property that
4585 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4587 static void
4588 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4590 gcc_assert (child->die_parent == prev->die_parent);
4591 gcc_assert (prev->die_sib == child);
4592 if (prev == child)
4594 gcc_assert (child->die_parent->die_child == child);
4595 prev = NULL;
4597 else
4598 prev->die_sib = child->die_sib;
4599 if (child->die_parent->die_child == child)
4600 child->die_parent->die_child = prev;
4603 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4604 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4606 static void
4607 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4609 dw_die_ref parent = old_child->die_parent;
4611 gcc_assert (parent == prev->die_parent);
4612 gcc_assert (prev->die_sib == old_child);
4614 new_child->die_parent = parent;
4615 if (prev == old_child)
4617 gcc_assert (parent->die_child == old_child);
4618 new_child->die_sib = new_child;
4620 else
4622 prev->die_sib = new_child;
4623 new_child->die_sib = old_child->die_sib;
4625 if (old_child->die_parent->die_child == old_child)
4626 old_child->die_parent->die_child = new_child;
4629 /* Move all children from OLD_PARENT to NEW_PARENT. */
4631 static void
4632 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4634 dw_die_ref c;
4635 new_parent->die_child = old_parent->die_child;
4636 old_parent->die_child = NULL;
4637 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4640 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4641 matches TAG. */
4643 static void
4644 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4646 dw_die_ref c;
4648 c = die->die_child;
4649 if (c) do {
4650 dw_die_ref prev = c;
4651 c = c->die_sib;
4652 while (c->die_tag == tag)
4654 remove_child_with_prev (c, prev);
4655 /* Might have removed every child. */
4656 if (c == c->die_sib)
4657 return;
4658 c = c->die_sib;
4660 } while (c != die->die_child);
4663 /* Add a CHILD_DIE as the last child of DIE. */
4665 static void
4666 add_child_die (dw_die_ref die, dw_die_ref child_die)
4668 /* FIXME this should probably be an assert. */
4669 if (! die || ! child_die)
4670 return;
4671 gcc_assert (die != child_die);
4673 child_die->die_parent = die;
4674 if (die->die_child)
4676 child_die->die_sib = die->die_child->die_sib;
4677 die->die_child->die_sib = child_die;
4679 else
4680 child_die->die_sib = child_die;
4681 die->die_child = child_die;
4684 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4685 is the specification, to the end of PARENT's list of children.
4686 This is done by removing and re-adding it. */
4688 static void
4689 splice_child_die (dw_die_ref parent, dw_die_ref child)
4691 dw_die_ref p;
4693 /* We want the declaration DIE from inside the class, not the
4694 specification DIE at toplevel. */
4695 if (child->die_parent != parent)
4697 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4699 if (tmp)
4700 child = tmp;
4703 gcc_assert (child->die_parent == parent
4704 || (child->die_parent
4705 == get_AT_ref (parent, DW_AT_specification)));
4707 for (p = child->die_parent->die_child; ; p = p->die_sib)
4708 if (p->die_sib == child)
4710 remove_child_with_prev (child, p);
4711 break;
4714 add_child_die (parent, child);
4717 /* Return a pointer to a newly created DIE node. */
4719 static inline dw_die_ref
4720 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4722 dw_die_ref die = ggc_alloc_cleared_die_node ();
4724 die->die_tag = tag_value;
4726 if (parent_die != NULL)
4727 add_child_die (parent_die, die);
4728 else
4730 limbo_die_node *limbo_node;
4732 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4733 limbo_node->die = die;
4734 limbo_node->created_for = t;
4735 limbo_node->next = limbo_die_list;
4736 limbo_die_list = limbo_node;
4739 return die;
4742 /* Return the DIE associated with the given type specifier. */
4744 static inline dw_die_ref
4745 lookup_type_die (tree type)
4747 return TYPE_SYMTAB_DIE (type);
4750 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4751 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4752 anonymous type instead the one of the naming typedef. */
4754 static inline dw_die_ref
4755 strip_naming_typedef (tree type, dw_die_ref type_die)
4757 if (type
4758 && TREE_CODE (type) == RECORD_TYPE
4759 && type_die
4760 && type_die->die_tag == DW_TAG_typedef
4761 && is_naming_typedef_decl (TYPE_NAME (type)))
4762 type_die = get_AT_ref (type_die, DW_AT_type);
4763 return type_die;
4766 /* Like lookup_type_die, but if type is an anonymous type named by a
4767 typedef[1], return the DIE of the anonymous type instead the one of
4768 the naming typedef. This is because in gen_typedef_die, we did
4769 equate the anonymous struct named by the typedef with the DIE of
4770 the naming typedef. So by default, lookup_type_die on an anonymous
4771 struct yields the DIE of the naming typedef.
4773 [1]: Read the comment of is_naming_typedef_decl to learn about what
4774 a naming typedef is. */
4776 static inline dw_die_ref
4777 lookup_type_die_strip_naming_typedef (tree type)
4779 dw_die_ref die = lookup_type_die (type);
4780 return strip_naming_typedef (type, die);
4783 /* Equate a DIE to a given type specifier. */
4785 static inline void
4786 equate_type_number_to_die (tree type, dw_die_ref type_die)
4788 TYPE_SYMTAB_DIE (type) = type_die;
4791 /* Returns a hash value for X (which really is a die_struct). */
4793 static hashval_t
4794 decl_die_table_hash (const void *x)
4796 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4799 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4801 static int
4802 decl_die_table_eq (const void *x, const void *y)
4804 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4807 /* Return the DIE associated with a given declaration. */
4809 static inline dw_die_ref
4810 lookup_decl_die (tree decl)
4812 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4815 /* Returns a hash value for X (which really is a var_loc_list). */
4817 static hashval_t
4818 decl_loc_table_hash (const void *x)
4820 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4823 /* Return nonzero if decl_id of var_loc_list X is the same as
4824 UID of decl *Y. */
4826 static int
4827 decl_loc_table_eq (const void *x, const void *y)
4829 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4832 /* Return the var_loc list associated with a given declaration. */
4834 static inline var_loc_list *
4835 lookup_decl_loc (const_tree decl)
4837 if (!decl_loc_table)
4838 return NULL;
4839 return (var_loc_list *)
4840 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4843 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4845 static hashval_t
4846 cached_dw_loc_list_table_hash (const void *x)
4848 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4851 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4852 UID of decl *Y. */
4854 static int
4855 cached_dw_loc_list_table_eq (const void *x, const void *y)
4857 return (((const cached_dw_loc_list *) x)->decl_id
4858 == DECL_UID ((const_tree) y));
4861 /* Equate a DIE to a particular declaration. */
4863 static void
4864 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4866 unsigned int decl_id = DECL_UID (decl);
4867 void **slot;
4869 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4870 *slot = decl_die;
4871 decl_die->decl_id = decl_id;
4874 /* Return how many bits covers PIECE EXPR_LIST. */
4876 static int
4877 decl_piece_bitsize (rtx piece)
4879 int ret = (int) GET_MODE (piece);
4880 if (ret)
4881 return ret;
4882 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4883 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4884 return INTVAL (XEXP (XEXP (piece, 0), 0));
4887 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4889 static rtx *
4890 decl_piece_varloc_ptr (rtx piece)
4892 if ((int) GET_MODE (piece))
4893 return &XEXP (piece, 0);
4894 else
4895 return &XEXP (XEXP (piece, 0), 1);
4898 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4899 Next is the chain of following piece nodes. */
4901 static rtx
4902 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4904 if (bitsize <= (int) MAX_MACHINE_MODE)
4905 return alloc_EXPR_LIST (bitsize, loc_note, next);
4906 else
4907 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4908 GEN_INT (bitsize),
4909 loc_note), next);
4912 /* Return rtx that should be stored into loc field for
4913 LOC_NOTE and BITPOS/BITSIZE. */
4915 static rtx
4916 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4917 HOST_WIDE_INT bitsize)
4919 if (bitsize != -1)
4921 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4922 if (bitpos != 0)
4923 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4925 return loc_note;
4928 /* This function either modifies location piece list *DEST in
4929 place (if SRC and INNER is NULL), or copies location piece list
4930 *SRC to *DEST while modifying it. Location BITPOS is modified
4931 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4932 not copied and if needed some padding around it is added.
4933 When modifying in place, DEST should point to EXPR_LIST where
4934 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4935 to the start of the whole list and INNER points to the EXPR_LIST
4936 where earlier pieces cover PIECE_BITPOS bits. */
4938 static void
4939 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4940 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4941 HOST_WIDE_INT bitsize, rtx loc_note)
4943 int diff;
4944 bool copy = inner != NULL;
4946 if (copy)
4948 /* First copy all nodes preceding the current bitpos. */
4949 while (src != inner)
4951 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4952 decl_piece_bitsize (*src), NULL_RTX);
4953 dest = &XEXP (*dest, 1);
4954 src = &XEXP (*src, 1);
4957 /* Add padding if needed. */
4958 if (bitpos != piece_bitpos)
4960 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4961 copy ? NULL_RTX : *dest);
4962 dest = &XEXP (*dest, 1);
4964 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4966 gcc_assert (!copy);
4967 /* A piece with correct bitpos and bitsize already exist,
4968 just update the location for it and return. */
4969 *decl_piece_varloc_ptr (*dest) = loc_note;
4970 return;
4972 /* Add the piece that changed. */
4973 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4974 dest = &XEXP (*dest, 1);
4975 /* Skip over pieces that overlap it. */
4976 diff = bitpos - piece_bitpos + bitsize;
4977 if (!copy)
4978 src = dest;
4979 while (diff > 0 && *src)
4981 rtx piece = *src;
4982 diff -= decl_piece_bitsize (piece);
4983 if (copy)
4984 src = &XEXP (piece, 1);
4985 else
4987 *src = XEXP (piece, 1);
4988 free_EXPR_LIST_node (piece);
4991 /* Add padding if needed. */
4992 if (diff < 0 && *src)
4994 if (!copy)
4995 dest = src;
4996 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4997 dest = &XEXP (*dest, 1);
4999 if (!copy)
5000 return;
5001 /* Finally copy all nodes following it. */
5002 while (*src)
5004 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5005 decl_piece_bitsize (*src), NULL_RTX);
5006 dest = &XEXP (*dest, 1);
5007 src = &XEXP (*src, 1);
5011 /* Add a variable location node to the linked list for DECL. */
5013 static struct var_loc_node *
5014 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5016 unsigned int decl_id;
5017 var_loc_list *temp;
5018 void **slot;
5019 struct var_loc_node *loc = NULL;
5020 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5022 if (DECL_DEBUG_EXPR_IS_FROM (decl))
5024 tree realdecl = DECL_DEBUG_EXPR (decl);
5025 if (realdecl
5026 && (handled_component_p (realdecl)
5027 || (TREE_CODE (realdecl) == MEM_REF
5028 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR)))
5030 HOST_WIDE_INT maxsize;
5031 tree innerdecl;
5032 innerdecl
5033 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5034 if (!DECL_P (innerdecl)
5035 || DECL_IGNORED_P (innerdecl)
5036 || TREE_STATIC (innerdecl)
5037 || bitsize <= 0
5038 || bitpos + bitsize > 256
5039 || bitsize != maxsize)
5040 return NULL;
5041 decl = innerdecl;
5045 decl_id = DECL_UID (decl);
5046 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5047 if (*slot == NULL)
5049 temp = ggc_alloc_cleared_var_loc_list ();
5050 temp->decl_id = decl_id;
5051 *slot = temp;
5053 else
5054 temp = (var_loc_list *) *slot;
5056 /* For PARM_DECLs try to keep around the original incoming value,
5057 even if that means we'll emit a zero-range .debug_loc entry. */
5058 if (temp->last
5059 && temp->first == temp->last
5060 && TREE_CODE (decl) == PARM_DECL
5061 && GET_CODE (temp->first->loc) == NOTE
5062 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5063 && DECL_INCOMING_RTL (decl)
5064 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5065 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5066 == GET_CODE (DECL_INCOMING_RTL (decl))
5067 && prev_real_insn (temp->first->loc) == NULL_RTX
5068 && (bitsize != -1
5069 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5070 NOTE_VAR_LOCATION_LOC (loc_note))
5071 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5072 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5074 loc = ggc_alloc_cleared_var_loc_node ();
5075 temp->first->next = loc;
5076 temp->last = loc;
5077 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5079 else if (temp->last)
5081 struct var_loc_node *last = temp->last, *unused = NULL;
5082 rtx *piece_loc = NULL, last_loc_note;
5083 int piece_bitpos = 0;
5084 if (last->next)
5086 last = last->next;
5087 gcc_assert (last->next == NULL);
5089 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5091 piece_loc = &last->loc;
5094 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5095 if (piece_bitpos + cur_bitsize > bitpos)
5096 break;
5097 piece_bitpos += cur_bitsize;
5098 piece_loc = &XEXP (*piece_loc, 1);
5100 while (*piece_loc);
5102 /* TEMP->LAST here is either pointer to the last but one or
5103 last element in the chained list, LAST is pointer to the
5104 last element. */
5105 if (label && strcmp (last->label, label) == 0)
5107 /* For SRA optimized variables if there weren't any real
5108 insns since last note, just modify the last node. */
5109 if (piece_loc != NULL)
5111 adjust_piece_list (piece_loc, NULL, NULL,
5112 bitpos, piece_bitpos, bitsize, loc_note);
5113 return NULL;
5115 /* If the last note doesn't cover any instructions, remove it. */
5116 if (temp->last != last)
5118 temp->last->next = NULL;
5119 unused = last;
5120 last = temp->last;
5121 gcc_assert (strcmp (last->label, label) != 0);
5123 else
5125 gcc_assert (temp->first == temp->last
5126 || (temp->first->next == temp->last
5127 && TREE_CODE (decl) == PARM_DECL));
5128 memset (temp->last, '\0', sizeof (*temp->last));
5129 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5130 return temp->last;
5133 if (bitsize == -1 && NOTE_P (last->loc))
5134 last_loc_note = last->loc;
5135 else if (piece_loc != NULL
5136 && *piece_loc != NULL_RTX
5137 && piece_bitpos == bitpos
5138 && decl_piece_bitsize (*piece_loc) == bitsize)
5139 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5140 else
5141 last_loc_note = NULL_RTX;
5142 /* If the current location is the same as the end of the list,
5143 and either both or neither of the locations is uninitialized,
5144 we have nothing to do. */
5145 if (last_loc_note == NULL_RTX
5146 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5147 NOTE_VAR_LOCATION_LOC (loc_note)))
5148 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5149 != NOTE_VAR_LOCATION_STATUS (loc_note))
5150 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5151 == VAR_INIT_STATUS_UNINITIALIZED)
5152 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5153 == VAR_INIT_STATUS_UNINITIALIZED))))
5155 /* Add LOC to the end of list and update LAST. If the last
5156 element of the list has been removed above, reuse its
5157 memory for the new node, otherwise allocate a new one. */
5158 if (unused)
5160 loc = unused;
5161 memset (loc, '\0', sizeof (*loc));
5163 else
5164 loc = ggc_alloc_cleared_var_loc_node ();
5165 if (bitsize == -1 || piece_loc == NULL)
5166 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5167 else
5168 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5169 bitpos, piece_bitpos, bitsize, loc_note);
5170 last->next = loc;
5171 /* Ensure TEMP->LAST will point either to the new last but one
5172 element of the chain, or to the last element in it. */
5173 if (last != temp->last)
5174 temp->last = last;
5176 else if (unused)
5177 ggc_free (unused);
5179 else
5181 loc = ggc_alloc_cleared_var_loc_node ();
5182 temp->first = loc;
5183 temp->last = loc;
5184 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5186 return loc;
5189 /* Keep track of the number of spaces used to indent the
5190 output of the debugging routines that print the structure of
5191 the DIE internal representation. */
5192 static int print_indent;
5194 /* Indent the line the number of spaces given by print_indent. */
5196 static inline void
5197 print_spaces (FILE *outfile)
5199 fprintf (outfile, "%*s", print_indent, "");
5202 /* Print a type signature in hex. */
5204 static inline void
5205 print_signature (FILE *outfile, char *sig)
5207 int i;
5209 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5210 fprintf (outfile, "%02x", sig[i] & 0xff);
5213 /* Print the information associated with a given DIE, and its children.
5214 This routine is a debugging aid only. */
5216 static void
5217 print_die (dw_die_ref die, FILE *outfile)
5219 dw_attr_ref a;
5220 dw_die_ref c;
5221 unsigned ix;
5223 print_spaces (outfile);
5224 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5225 die->die_offset, dwarf_tag_name (die->die_tag),
5226 (void*) die);
5227 print_spaces (outfile);
5228 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5229 fprintf (outfile, " offset: %ld", die->die_offset);
5230 fprintf (outfile, " mark: %d\n", die->die_mark);
5232 if (die->comdat_type_p)
5234 print_spaces (outfile);
5235 fprintf (outfile, " signature: ");
5236 print_signature (outfile, die->die_id.die_type_node->signature);
5237 fprintf (outfile, "\n");
5240 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5242 print_spaces (outfile);
5243 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5245 switch (AT_class (a))
5247 case dw_val_class_addr:
5248 fprintf (outfile, "address");
5249 break;
5250 case dw_val_class_offset:
5251 fprintf (outfile, "offset");
5252 break;
5253 case dw_val_class_loc:
5254 fprintf (outfile, "location descriptor");
5255 break;
5256 case dw_val_class_loc_list:
5257 fprintf (outfile, "location list -> label:%s",
5258 AT_loc_list (a)->ll_symbol);
5259 break;
5260 case dw_val_class_range_list:
5261 fprintf (outfile, "range list");
5262 break;
5263 case dw_val_class_const:
5264 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5265 break;
5266 case dw_val_class_unsigned_const:
5267 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5268 break;
5269 case dw_val_class_const_double:
5270 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5271 HOST_WIDE_INT_PRINT_UNSIGNED")",
5272 a->dw_attr_val.v.val_double.high,
5273 a->dw_attr_val.v.val_double.low);
5274 break;
5275 case dw_val_class_vec:
5276 fprintf (outfile, "floating-point or vector constant");
5277 break;
5278 case dw_val_class_flag:
5279 fprintf (outfile, "%u", AT_flag (a));
5280 break;
5281 case dw_val_class_die_ref:
5282 if (AT_ref (a) != NULL)
5284 if (AT_ref (a)->comdat_type_p)
5286 fprintf (outfile, "die -> signature: ");
5287 print_signature (outfile,
5288 AT_ref (a)->die_id.die_type_node->signature);
5290 else if (AT_ref (a)->die_id.die_symbol)
5291 fprintf (outfile, "die -> label: %s",
5292 AT_ref (a)->die_id.die_symbol);
5293 else
5294 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5295 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5297 else
5298 fprintf (outfile, "die -> <null>");
5299 break;
5300 case dw_val_class_vms_delta:
5301 fprintf (outfile, "delta: @slotcount(%s-%s)",
5302 AT_vms_delta2 (a), AT_vms_delta1 (a));
5303 break;
5304 case dw_val_class_lbl_id:
5305 case dw_val_class_lineptr:
5306 case dw_val_class_macptr:
5307 case dw_val_class_high_pc:
5308 fprintf (outfile, "label: %s", AT_lbl (a));
5309 break;
5310 case dw_val_class_str:
5311 if (AT_string (a) != NULL)
5312 fprintf (outfile, "\"%s\"", AT_string (a));
5313 else
5314 fprintf (outfile, "<null>");
5315 break;
5316 case dw_val_class_file:
5317 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5318 AT_file (a)->emitted_number);
5319 break;
5320 case dw_val_class_data8:
5322 int i;
5324 for (i = 0; i < 8; i++)
5325 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5326 break;
5328 default:
5329 break;
5332 fprintf (outfile, "\n");
5335 if (die->die_child != NULL)
5337 print_indent += 4;
5338 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5339 print_indent -= 4;
5341 if (print_indent == 0)
5342 fprintf (outfile, "\n");
5345 /* Print the information collected for a given DIE. */
5347 DEBUG_FUNCTION void
5348 debug_dwarf_die (dw_die_ref die)
5350 print_die (die, stderr);
5353 /* Print all DWARF information collected for the compilation unit.
5354 This routine is a debugging aid only. */
5356 DEBUG_FUNCTION void
5357 debug_dwarf (void)
5359 print_indent = 0;
5360 print_die (comp_unit_die (), stderr);
5363 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5364 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5365 DIE that marks the start of the DIEs for this include file. */
5367 static dw_die_ref
5368 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5370 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5371 dw_die_ref new_unit = gen_compile_unit_die (filename);
5373 new_unit->die_sib = old_unit;
5374 return new_unit;
5377 /* Close an include-file CU and reopen the enclosing one. */
5379 static dw_die_ref
5380 pop_compile_unit (dw_die_ref old_unit)
5382 dw_die_ref new_unit = old_unit->die_sib;
5384 old_unit->die_sib = NULL;
5385 return new_unit;
5388 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5389 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5391 /* Calculate the checksum of a location expression. */
5393 static inline void
5394 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5396 int tem;
5398 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5399 CHECKSUM (tem);
5400 CHECKSUM (loc->dw_loc_oprnd1);
5401 CHECKSUM (loc->dw_loc_oprnd2);
5404 /* Calculate the checksum of an attribute. */
5406 static void
5407 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5409 dw_loc_descr_ref loc;
5410 rtx r;
5412 CHECKSUM (at->dw_attr);
5414 /* We don't care that this was compiled with a different compiler
5415 snapshot; if the output is the same, that's what matters. */
5416 if (at->dw_attr == DW_AT_producer)
5417 return;
5419 switch (AT_class (at))
5421 case dw_val_class_const:
5422 CHECKSUM (at->dw_attr_val.v.val_int);
5423 break;
5424 case dw_val_class_unsigned_const:
5425 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5426 break;
5427 case dw_val_class_const_double:
5428 CHECKSUM (at->dw_attr_val.v.val_double);
5429 break;
5430 case dw_val_class_vec:
5431 CHECKSUM (at->dw_attr_val.v.val_vec);
5432 break;
5433 case dw_val_class_flag:
5434 CHECKSUM (at->dw_attr_val.v.val_flag);
5435 break;
5436 case dw_val_class_str:
5437 CHECKSUM_STRING (AT_string (at));
5438 break;
5440 case dw_val_class_addr:
5441 r = AT_addr (at);
5442 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5443 CHECKSUM_STRING (XSTR (r, 0));
5444 break;
5446 case dw_val_class_offset:
5447 CHECKSUM (at->dw_attr_val.v.val_offset);
5448 break;
5450 case dw_val_class_loc:
5451 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5452 loc_checksum (loc, ctx);
5453 break;
5455 case dw_val_class_die_ref:
5456 die_checksum (AT_ref (at), ctx, mark);
5457 break;
5459 case dw_val_class_fde_ref:
5460 case dw_val_class_vms_delta:
5461 case dw_val_class_lbl_id:
5462 case dw_val_class_lineptr:
5463 case dw_val_class_macptr:
5464 case dw_val_class_high_pc:
5465 break;
5467 case dw_val_class_file:
5468 CHECKSUM_STRING (AT_file (at)->filename);
5469 break;
5471 case dw_val_class_data8:
5472 CHECKSUM (at->dw_attr_val.v.val_data8);
5473 break;
5475 default:
5476 break;
5480 /* Calculate the checksum of a DIE. */
5482 static void
5483 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5485 dw_die_ref c;
5486 dw_attr_ref a;
5487 unsigned ix;
5489 /* To avoid infinite recursion. */
5490 if (die->die_mark)
5492 CHECKSUM (die->die_mark);
5493 return;
5495 die->die_mark = ++(*mark);
5497 CHECKSUM (die->die_tag);
5499 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5500 attr_checksum (a, ctx, mark);
5502 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5505 #undef CHECKSUM
5506 #undef CHECKSUM_STRING
5508 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5509 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5510 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5511 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5512 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5513 #define CHECKSUM_ATTR(FOO) \
5514 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5516 /* Calculate the checksum of a number in signed LEB128 format. */
5518 static void
5519 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5521 unsigned char byte;
5522 bool more;
5524 while (1)
5526 byte = (value & 0x7f);
5527 value >>= 7;
5528 more = !((value == 0 && (byte & 0x40) == 0)
5529 || (value == -1 && (byte & 0x40) != 0));
5530 if (more)
5531 byte |= 0x80;
5532 CHECKSUM (byte);
5533 if (!more)
5534 break;
5538 /* Calculate the checksum of a number in unsigned LEB128 format. */
5540 static void
5541 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5543 while (1)
5545 unsigned char byte = (value & 0x7f);
5546 value >>= 7;
5547 if (value != 0)
5548 /* More bytes to follow. */
5549 byte |= 0x80;
5550 CHECKSUM (byte);
5551 if (value == 0)
5552 break;
5556 /* Checksum the context of the DIE. This adds the names of any
5557 surrounding namespaces or structures to the checksum. */
5559 static void
5560 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5562 const char *name;
5563 dw_die_ref spec;
5564 int tag = die->die_tag;
5566 if (tag != DW_TAG_namespace
5567 && tag != DW_TAG_structure_type
5568 && tag != DW_TAG_class_type)
5569 return;
5571 name = get_AT_string (die, DW_AT_name);
5573 spec = get_AT_ref (die, DW_AT_specification);
5574 if (spec != NULL)
5575 die = spec;
5577 if (die->die_parent != NULL)
5578 checksum_die_context (die->die_parent, ctx);
5580 CHECKSUM_ULEB128 ('C');
5581 CHECKSUM_ULEB128 (tag);
5582 if (name != NULL)
5583 CHECKSUM_STRING (name);
5586 /* Calculate the checksum of a location expression. */
5588 static inline void
5589 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5591 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5592 were emitted as a DW_FORM_sdata instead of a location expression. */
5593 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5595 CHECKSUM_ULEB128 (DW_FORM_sdata);
5596 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5597 return;
5600 /* Otherwise, just checksum the raw location expression. */
5601 while (loc != NULL)
5603 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5604 CHECKSUM (loc->dw_loc_oprnd1);
5605 CHECKSUM (loc->dw_loc_oprnd2);
5606 loc = loc->dw_loc_next;
5610 /* Calculate the checksum of an attribute. */
5612 static void
5613 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5614 struct md5_ctx *ctx, int *mark)
5616 dw_loc_descr_ref loc;
5617 rtx r;
5619 if (AT_class (at) == dw_val_class_die_ref)
5621 dw_die_ref target_die = AT_ref (at);
5623 /* For pointer and reference types, we checksum only the (qualified)
5624 name of the target type (if there is a name). For friend entries,
5625 we checksum only the (qualified) name of the target type or function.
5626 This allows the checksum to remain the same whether the target type
5627 is complete or not. */
5628 if ((at->dw_attr == DW_AT_type
5629 && (tag == DW_TAG_pointer_type
5630 || tag == DW_TAG_reference_type
5631 || tag == DW_TAG_rvalue_reference_type
5632 || tag == DW_TAG_ptr_to_member_type))
5633 || (at->dw_attr == DW_AT_friend
5634 && tag == DW_TAG_friend))
5636 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5638 if (name_attr != NULL)
5640 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5642 if (decl == NULL)
5643 decl = target_die;
5644 CHECKSUM_ULEB128 ('N');
5645 CHECKSUM_ULEB128 (at->dw_attr);
5646 if (decl->die_parent != NULL)
5647 checksum_die_context (decl->die_parent, ctx);
5648 CHECKSUM_ULEB128 ('E');
5649 CHECKSUM_STRING (AT_string (name_attr));
5650 return;
5654 /* For all other references to another DIE, we check to see if the
5655 target DIE has already been visited. If it has, we emit a
5656 backward reference; if not, we descend recursively. */
5657 if (target_die->die_mark > 0)
5659 CHECKSUM_ULEB128 ('R');
5660 CHECKSUM_ULEB128 (at->dw_attr);
5661 CHECKSUM_ULEB128 (target_die->die_mark);
5663 else
5665 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5667 if (decl == NULL)
5668 decl = target_die;
5669 target_die->die_mark = ++(*mark);
5670 CHECKSUM_ULEB128 ('T');
5671 CHECKSUM_ULEB128 (at->dw_attr);
5672 if (decl->die_parent != NULL)
5673 checksum_die_context (decl->die_parent, ctx);
5674 die_checksum_ordered (target_die, ctx, mark);
5676 return;
5679 CHECKSUM_ULEB128 ('A');
5680 CHECKSUM_ULEB128 (at->dw_attr);
5682 switch (AT_class (at))
5684 case dw_val_class_const:
5685 CHECKSUM_ULEB128 (DW_FORM_sdata);
5686 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5687 break;
5689 case dw_val_class_unsigned_const:
5690 CHECKSUM_ULEB128 (DW_FORM_sdata);
5691 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5692 break;
5694 case dw_val_class_const_double:
5695 CHECKSUM_ULEB128 (DW_FORM_block);
5696 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5697 CHECKSUM (at->dw_attr_val.v.val_double);
5698 break;
5700 case dw_val_class_vec:
5701 CHECKSUM_ULEB128 (DW_FORM_block);
5702 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5703 CHECKSUM (at->dw_attr_val.v.val_vec);
5704 break;
5706 case dw_val_class_flag:
5707 CHECKSUM_ULEB128 (DW_FORM_flag);
5708 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5709 break;
5711 case dw_val_class_str:
5712 CHECKSUM_ULEB128 (DW_FORM_string);
5713 CHECKSUM_STRING (AT_string (at));
5714 break;
5716 case dw_val_class_addr:
5717 r = AT_addr (at);
5718 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5719 CHECKSUM_ULEB128 (DW_FORM_string);
5720 CHECKSUM_STRING (XSTR (r, 0));
5721 break;
5723 case dw_val_class_offset:
5724 CHECKSUM_ULEB128 (DW_FORM_sdata);
5725 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5726 break;
5728 case dw_val_class_loc:
5729 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5730 loc_checksum_ordered (loc, ctx);
5731 break;
5733 case dw_val_class_fde_ref:
5734 case dw_val_class_lbl_id:
5735 case dw_val_class_lineptr:
5736 case dw_val_class_macptr:
5737 case dw_val_class_high_pc:
5738 break;
5740 case dw_val_class_file:
5741 CHECKSUM_ULEB128 (DW_FORM_string);
5742 CHECKSUM_STRING (AT_file (at)->filename);
5743 break;
5745 case dw_val_class_data8:
5746 CHECKSUM (at->dw_attr_val.v.val_data8);
5747 break;
5749 default:
5750 break;
5754 struct checksum_attributes
5756 dw_attr_ref at_name;
5757 dw_attr_ref at_type;
5758 dw_attr_ref at_friend;
5759 dw_attr_ref at_accessibility;
5760 dw_attr_ref at_address_class;
5761 dw_attr_ref at_allocated;
5762 dw_attr_ref at_artificial;
5763 dw_attr_ref at_associated;
5764 dw_attr_ref at_binary_scale;
5765 dw_attr_ref at_bit_offset;
5766 dw_attr_ref at_bit_size;
5767 dw_attr_ref at_bit_stride;
5768 dw_attr_ref at_byte_size;
5769 dw_attr_ref at_byte_stride;
5770 dw_attr_ref at_const_value;
5771 dw_attr_ref at_containing_type;
5772 dw_attr_ref at_count;
5773 dw_attr_ref at_data_location;
5774 dw_attr_ref at_data_member_location;
5775 dw_attr_ref at_decimal_scale;
5776 dw_attr_ref at_decimal_sign;
5777 dw_attr_ref at_default_value;
5778 dw_attr_ref at_digit_count;
5779 dw_attr_ref at_discr;
5780 dw_attr_ref at_discr_list;
5781 dw_attr_ref at_discr_value;
5782 dw_attr_ref at_encoding;
5783 dw_attr_ref at_endianity;
5784 dw_attr_ref at_explicit;
5785 dw_attr_ref at_is_optional;
5786 dw_attr_ref at_location;
5787 dw_attr_ref at_lower_bound;
5788 dw_attr_ref at_mutable;
5789 dw_attr_ref at_ordering;
5790 dw_attr_ref at_picture_string;
5791 dw_attr_ref at_prototyped;
5792 dw_attr_ref at_small;
5793 dw_attr_ref at_segment;
5794 dw_attr_ref at_string_length;
5795 dw_attr_ref at_threads_scaled;
5796 dw_attr_ref at_upper_bound;
5797 dw_attr_ref at_use_location;
5798 dw_attr_ref at_use_UTF8;
5799 dw_attr_ref at_variable_parameter;
5800 dw_attr_ref at_virtuality;
5801 dw_attr_ref at_visibility;
5802 dw_attr_ref at_vtable_elem_location;
5805 /* Collect the attributes that we will want to use for the checksum. */
5807 static void
5808 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5810 dw_attr_ref a;
5811 unsigned ix;
5813 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5815 switch (a->dw_attr)
5817 case DW_AT_name:
5818 attrs->at_name = a;
5819 break;
5820 case DW_AT_type:
5821 attrs->at_type = a;
5822 break;
5823 case DW_AT_friend:
5824 attrs->at_friend = a;
5825 break;
5826 case DW_AT_accessibility:
5827 attrs->at_accessibility = a;
5828 break;
5829 case DW_AT_address_class:
5830 attrs->at_address_class = a;
5831 break;
5832 case DW_AT_allocated:
5833 attrs->at_allocated = a;
5834 break;
5835 case DW_AT_artificial:
5836 attrs->at_artificial = a;
5837 break;
5838 case DW_AT_associated:
5839 attrs->at_associated = a;
5840 break;
5841 case DW_AT_binary_scale:
5842 attrs->at_binary_scale = a;
5843 break;
5844 case DW_AT_bit_offset:
5845 attrs->at_bit_offset = a;
5846 break;
5847 case DW_AT_bit_size:
5848 attrs->at_bit_size = a;
5849 break;
5850 case DW_AT_bit_stride:
5851 attrs->at_bit_stride = a;
5852 break;
5853 case DW_AT_byte_size:
5854 attrs->at_byte_size = a;
5855 break;
5856 case DW_AT_byte_stride:
5857 attrs->at_byte_stride = a;
5858 break;
5859 case DW_AT_const_value:
5860 attrs->at_const_value = a;
5861 break;
5862 case DW_AT_containing_type:
5863 attrs->at_containing_type = a;
5864 break;
5865 case DW_AT_count:
5866 attrs->at_count = a;
5867 break;
5868 case DW_AT_data_location:
5869 attrs->at_data_location = a;
5870 break;
5871 case DW_AT_data_member_location:
5872 attrs->at_data_member_location = a;
5873 break;
5874 case DW_AT_decimal_scale:
5875 attrs->at_decimal_scale = a;
5876 break;
5877 case DW_AT_decimal_sign:
5878 attrs->at_decimal_sign = a;
5879 break;
5880 case DW_AT_default_value:
5881 attrs->at_default_value = a;
5882 break;
5883 case DW_AT_digit_count:
5884 attrs->at_digit_count = a;
5885 break;
5886 case DW_AT_discr:
5887 attrs->at_discr = a;
5888 break;
5889 case DW_AT_discr_list:
5890 attrs->at_discr_list = a;
5891 break;
5892 case DW_AT_discr_value:
5893 attrs->at_discr_value = a;
5894 break;
5895 case DW_AT_encoding:
5896 attrs->at_encoding = a;
5897 break;
5898 case DW_AT_endianity:
5899 attrs->at_endianity = a;
5900 break;
5901 case DW_AT_explicit:
5902 attrs->at_explicit = a;
5903 break;
5904 case DW_AT_is_optional:
5905 attrs->at_is_optional = a;
5906 break;
5907 case DW_AT_location:
5908 attrs->at_location = a;
5909 break;
5910 case DW_AT_lower_bound:
5911 attrs->at_lower_bound = a;
5912 break;
5913 case DW_AT_mutable:
5914 attrs->at_mutable = a;
5915 break;
5916 case DW_AT_ordering:
5917 attrs->at_ordering = a;
5918 break;
5919 case DW_AT_picture_string:
5920 attrs->at_picture_string = a;
5921 break;
5922 case DW_AT_prototyped:
5923 attrs->at_prototyped = a;
5924 break;
5925 case DW_AT_small:
5926 attrs->at_small = a;
5927 break;
5928 case DW_AT_segment:
5929 attrs->at_segment = a;
5930 break;
5931 case DW_AT_string_length:
5932 attrs->at_string_length = a;
5933 break;
5934 case DW_AT_threads_scaled:
5935 attrs->at_threads_scaled = a;
5936 break;
5937 case DW_AT_upper_bound:
5938 attrs->at_upper_bound = a;
5939 break;
5940 case DW_AT_use_location:
5941 attrs->at_use_location = a;
5942 break;
5943 case DW_AT_use_UTF8:
5944 attrs->at_use_UTF8 = a;
5945 break;
5946 case DW_AT_variable_parameter:
5947 attrs->at_variable_parameter = a;
5948 break;
5949 case DW_AT_virtuality:
5950 attrs->at_virtuality = a;
5951 break;
5952 case DW_AT_visibility:
5953 attrs->at_visibility = a;
5954 break;
5955 case DW_AT_vtable_elem_location:
5956 attrs->at_vtable_elem_location = a;
5957 break;
5958 default:
5959 break;
5964 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
5966 static void
5967 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5969 dw_die_ref c;
5970 dw_die_ref decl;
5971 struct checksum_attributes attrs;
5973 CHECKSUM_ULEB128 ('D');
5974 CHECKSUM_ULEB128 (die->die_tag);
5976 memset (&attrs, 0, sizeof (attrs));
5978 decl = get_AT_ref (die, DW_AT_specification);
5979 if (decl != NULL)
5980 collect_checksum_attributes (&attrs, decl);
5981 collect_checksum_attributes (&attrs, die);
5983 CHECKSUM_ATTR (attrs.at_name);
5984 CHECKSUM_ATTR (attrs.at_accessibility);
5985 CHECKSUM_ATTR (attrs.at_address_class);
5986 CHECKSUM_ATTR (attrs.at_allocated);
5987 CHECKSUM_ATTR (attrs.at_artificial);
5988 CHECKSUM_ATTR (attrs.at_associated);
5989 CHECKSUM_ATTR (attrs.at_binary_scale);
5990 CHECKSUM_ATTR (attrs.at_bit_offset);
5991 CHECKSUM_ATTR (attrs.at_bit_size);
5992 CHECKSUM_ATTR (attrs.at_bit_stride);
5993 CHECKSUM_ATTR (attrs.at_byte_size);
5994 CHECKSUM_ATTR (attrs.at_byte_stride);
5995 CHECKSUM_ATTR (attrs.at_const_value);
5996 CHECKSUM_ATTR (attrs.at_containing_type);
5997 CHECKSUM_ATTR (attrs.at_count);
5998 CHECKSUM_ATTR (attrs.at_data_location);
5999 CHECKSUM_ATTR (attrs.at_data_member_location);
6000 CHECKSUM_ATTR (attrs.at_decimal_scale);
6001 CHECKSUM_ATTR (attrs.at_decimal_sign);
6002 CHECKSUM_ATTR (attrs.at_default_value);
6003 CHECKSUM_ATTR (attrs.at_digit_count);
6004 CHECKSUM_ATTR (attrs.at_discr);
6005 CHECKSUM_ATTR (attrs.at_discr_list);
6006 CHECKSUM_ATTR (attrs.at_discr_value);
6007 CHECKSUM_ATTR (attrs.at_encoding);
6008 CHECKSUM_ATTR (attrs.at_endianity);
6009 CHECKSUM_ATTR (attrs.at_explicit);
6010 CHECKSUM_ATTR (attrs.at_is_optional);
6011 CHECKSUM_ATTR (attrs.at_location);
6012 CHECKSUM_ATTR (attrs.at_lower_bound);
6013 CHECKSUM_ATTR (attrs.at_mutable);
6014 CHECKSUM_ATTR (attrs.at_ordering);
6015 CHECKSUM_ATTR (attrs.at_picture_string);
6016 CHECKSUM_ATTR (attrs.at_prototyped);
6017 CHECKSUM_ATTR (attrs.at_small);
6018 CHECKSUM_ATTR (attrs.at_segment);
6019 CHECKSUM_ATTR (attrs.at_string_length);
6020 CHECKSUM_ATTR (attrs.at_threads_scaled);
6021 CHECKSUM_ATTR (attrs.at_upper_bound);
6022 CHECKSUM_ATTR (attrs.at_use_location);
6023 CHECKSUM_ATTR (attrs.at_use_UTF8);
6024 CHECKSUM_ATTR (attrs.at_variable_parameter);
6025 CHECKSUM_ATTR (attrs.at_virtuality);
6026 CHECKSUM_ATTR (attrs.at_visibility);
6027 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6028 CHECKSUM_ATTR (attrs.at_type);
6029 CHECKSUM_ATTR (attrs.at_friend);
6031 /* Checksum the child DIEs, except for nested types and member functions. */
6032 c = die->die_child;
6033 if (c) do {
6034 dw_attr_ref name_attr;
6036 c = c->die_sib;
6037 name_attr = get_AT (c, DW_AT_name);
6038 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6039 && name_attr != NULL)
6041 CHECKSUM_ULEB128 ('S');
6042 CHECKSUM_ULEB128 (c->die_tag);
6043 CHECKSUM_STRING (AT_string (name_attr));
6045 else
6047 /* Mark this DIE so it gets processed when unmarking. */
6048 if (c->die_mark == 0)
6049 c->die_mark = -1;
6050 die_checksum_ordered (c, ctx, mark);
6052 } while (c != die->die_child);
6054 CHECKSUM_ULEB128 (0);
6057 #undef CHECKSUM
6058 #undef CHECKSUM_STRING
6059 #undef CHECKSUM_ATTR
6060 #undef CHECKSUM_LEB128
6061 #undef CHECKSUM_ULEB128
6063 /* Generate the type signature for DIE. This is computed by generating an
6064 MD5 checksum over the DIE's tag, its relevant attributes, and its
6065 children. Attributes that are references to other DIEs are processed
6066 by recursion, using the MARK field to prevent infinite recursion.
6067 If the DIE is nested inside a namespace or another type, we also
6068 need to include that context in the signature. The lower 64 bits
6069 of the resulting MD5 checksum comprise the signature. */
6071 static void
6072 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6074 int mark;
6075 const char *name;
6076 unsigned char checksum[16];
6077 struct md5_ctx ctx;
6078 dw_die_ref decl;
6079 dw_die_ref parent;
6081 name = get_AT_string (die, DW_AT_name);
6082 decl = get_AT_ref (die, DW_AT_specification);
6083 parent = get_die_parent (die);
6085 /* First, compute a signature for just the type name (and its surrounding
6086 context, if any. This is stored in the type unit DIE for link-time
6087 ODR (one-definition rule) checking. */
6089 if (is_cxx() && name != NULL)
6091 md5_init_ctx (&ctx);
6093 /* Checksum the names of surrounding namespaces and structures. */
6094 if (parent != NULL)
6095 checksum_die_context (parent, &ctx);
6097 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6098 md5_process_bytes (name, strlen (name) + 1, &ctx);
6099 md5_finish_ctx (&ctx, checksum);
6101 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6104 /* Next, compute the complete type signature. */
6106 md5_init_ctx (&ctx);
6107 mark = 1;
6108 die->die_mark = mark;
6110 /* Checksum the names of surrounding namespaces and structures. */
6111 if (parent != NULL)
6112 checksum_die_context (parent, &ctx);
6114 /* Checksum the DIE and its children. */
6115 die_checksum_ordered (die, &ctx, &mark);
6116 unmark_all_dies (die);
6117 md5_finish_ctx (&ctx, checksum);
6119 /* Store the signature in the type node and link the type DIE and the
6120 type node together. */
6121 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6122 DWARF_TYPE_SIGNATURE_SIZE);
6123 die->comdat_type_p = true;
6124 die->die_id.die_type_node = type_node;
6125 type_node->type_die = die;
6127 /* If the DIE is a specification, link its declaration to the type node
6128 as well. */
6129 if (decl != NULL)
6131 decl->comdat_type_p = true;
6132 decl->die_id.die_type_node = type_node;
6136 /* Do the location expressions look same? */
6137 static inline int
6138 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6140 return loc1->dw_loc_opc == loc2->dw_loc_opc
6141 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6142 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6145 /* Do the values look the same? */
6146 static int
6147 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6149 dw_loc_descr_ref loc1, loc2;
6150 rtx r1, r2;
6152 if (v1->val_class != v2->val_class)
6153 return 0;
6155 switch (v1->val_class)
6157 case dw_val_class_const:
6158 return v1->v.val_int == v2->v.val_int;
6159 case dw_val_class_unsigned_const:
6160 return v1->v.val_unsigned == v2->v.val_unsigned;
6161 case dw_val_class_const_double:
6162 return v1->v.val_double.high == v2->v.val_double.high
6163 && v1->v.val_double.low == v2->v.val_double.low;
6164 case dw_val_class_vec:
6165 if (v1->v.val_vec.length != v2->v.val_vec.length
6166 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6167 return 0;
6168 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6169 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6170 return 0;
6171 return 1;
6172 case dw_val_class_flag:
6173 return v1->v.val_flag == v2->v.val_flag;
6174 case dw_val_class_str:
6175 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6177 case dw_val_class_addr:
6178 r1 = v1->v.val_addr;
6179 r2 = v2->v.val_addr;
6180 if (GET_CODE (r1) != GET_CODE (r2))
6181 return 0;
6182 return !rtx_equal_p (r1, r2);
6184 case dw_val_class_offset:
6185 return v1->v.val_offset == v2->v.val_offset;
6187 case dw_val_class_loc:
6188 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6189 loc1 && loc2;
6190 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6191 if (!same_loc_p (loc1, loc2, mark))
6192 return 0;
6193 return !loc1 && !loc2;
6195 case dw_val_class_die_ref:
6196 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6198 case dw_val_class_fde_ref:
6199 case dw_val_class_vms_delta:
6200 case dw_val_class_lbl_id:
6201 case dw_val_class_lineptr:
6202 case dw_val_class_macptr:
6203 case dw_val_class_high_pc:
6204 return 1;
6206 case dw_val_class_file:
6207 return v1->v.val_file == v2->v.val_file;
6209 case dw_val_class_data8:
6210 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6212 default:
6213 return 1;
6217 /* Do the attributes look the same? */
6219 static int
6220 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6222 if (at1->dw_attr != at2->dw_attr)
6223 return 0;
6225 /* We don't care that this was compiled with a different compiler
6226 snapshot; if the output is the same, that's what matters. */
6227 if (at1->dw_attr == DW_AT_producer)
6228 return 1;
6230 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6233 /* Do the dies look the same? */
6235 static int
6236 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6238 dw_die_ref c1, c2;
6239 dw_attr_ref a1;
6240 unsigned ix;
6242 /* To avoid infinite recursion. */
6243 if (die1->die_mark)
6244 return die1->die_mark == die2->die_mark;
6245 die1->die_mark = die2->die_mark = ++(*mark);
6247 if (die1->die_tag != die2->die_tag)
6248 return 0;
6250 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6251 return 0;
6253 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6254 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6255 return 0;
6257 c1 = die1->die_child;
6258 c2 = die2->die_child;
6259 if (! c1)
6261 if (c2)
6262 return 0;
6264 else
6265 for (;;)
6267 if (!same_die_p (c1, c2, mark))
6268 return 0;
6269 c1 = c1->die_sib;
6270 c2 = c2->die_sib;
6271 if (c1 == die1->die_child)
6273 if (c2 == die2->die_child)
6274 break;
6275 else
6276 return 0;
6280 return 1;
6283 /* Do the dies look the same? Wrapper around same_die_p. */
6285 static int
6286 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6288 int mark = 0;
6289 int ret = same_die_p (die1, die2, &mark);
6291 unmark_all_dies (die1);
6292 unmark_all_dies (die2);
6294 return ret;
6297 /* The prefix to attach to symbols on DIEs in the current comdat debug
6298 info section. */
6299 static const char *comdat_symbol_id;
6301 /* The index of the current symbol within the current comdat CU. */
6302 static unsigned int comdat_symbol_number;
6304 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6305 children, and set comdat_symbol_id accordingly. */
6307 static void
6308 compute_section_prefix (dw_die_ref unit_die)
6310 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6311 const char *base = die_name ? lbasename (die_name) : "anonymous";
6312 char *name = XALLOCAVEC (char, strlen (base) + 64);
6313 char *p;
6314 int i, mark;
6315 unsigned char checksum[16];
6316 struct md5_ctx ctx;
6318 /* Compute the checksum of the DIE, then append part of it as hex digits to
6319 the name filename of the unit. */
6321 md5_init_ctx (&ctx);
6322 mark = 0;
6323 die_checksum (unit_die, &ctx, &mark);
6324 unmark_all_dies (unit_die);
6325 md5_finish_ctx (&ctx, checksum);
6327 sprintf (name, "%s.", base);
6328 clean_symbol_name (name);
6330 p = name + strlen (name);
6331 for (i = 0; i < 4; i++)
6333 sprintf (p, "%.2x", checksum[i]);
6334 p += 2;
6337 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6338 comdat_symbol_number = 0;
6341 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6343 static int
6344 is_type_die (dw_die_ref die)
6346 switch (die->die_tag)
6348 case DW_TAG_array_type:
6349 case DW_TAG_class_type:
6350 case DW_TAG_interface_type:
6351 case DW_TAG_enumeration_type:
6352 case DW_TAG_pointer_type:
6353 case DW_TAG_reference_type:
6354 case DW_TAG_rvalue_reference_type:
6355 case DW_TAG_string_type:
6356 case DW_TAG_structure_type:
6357 case DW_TAG_subroutine_type:
6358 case DW_TAG_union_type:
6359 case DW_TAG_ptr_to_member_type:
6360 case DW_TAG_set_type:
6361 case DW_TAG_subrange_type:
6362 case DW_TAG_base_type:
6363 case DW_TAG_const_type:
6364 case DW_TAG_file_type:
6365 case DW_TAG_packed_type:
6366 case DW_TAG_volatile_type:
6367 case DW_TAG_typedef:
6368 return 1;
6369 default:
6370 return 0;
6374 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6375 Basically, we want to choose the bits that are likely to be shared between
6376 compilations (types) and leave out the bits that are specific to individual
6377 compilations (functions). */
6379 static int
6380 is_comdat_die (dw_die_ref c)
6382 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6383 we do for stabs. The advantage is a greater likelihood of sharing between
6384 objects that don't include headers in the same order (and therefore would
6385 put the base types in a different comdat). jason 8/28/00 */
6387 if (c->die_tag == DW_TAG_base_type)
6388 return 0;
6390 if (c->die_tag == DW_TAG_pointer_type
6391 || c->die_tag == DW_TAG_reference_type
6392 || c->die_tag == DW_TAG_rvalue_reference_type
6393 || c->die_tag == DW_TAG_const_type
6394 || c->die_tag == DW_TAG_volatile_type)
6396 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6398 return t ? is_comdat_die (t) : 0;
6401 return is_type_die (c);
6404 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6405 compilation unit. */
6407 static int
6408 is_symbol_die (dw_die_ref c)
6410 return (is_type_die (c)
6411 || is_declaration_die (c)
6412 || c->die_tag == DW_TAG_namespace
6413 || c->die_tag == DW_TAG_module);
6416 /* Returns true iff C is a compile-unit DIE. */
6418 static inline bool
6419 is_cu_die (dw_die_ref c)
6421 return c && c->die_tag == DW_TAG_compile_unit;
6424 /* Returns true iff C is a unit DIE of some sort. */
6426 static inline bool
6427 is_unit_die (dw_die_ref c)
6429 return c && (c->die_tag == DW_TAG_compile_unit
6430 || c->die_tag == DW_TAG_partial_unit
6431 || c->die_tag == DW_TAG_type_unit);
6434 /* Returns true iff C is a namespace DIE. */
6436 static inline bool
6437 is_namespace_die (dw_die_ref c)
6439 return c && c->die_tag == DW_TAG_namespace;
6442 /* Returns true iff C is a class or structure DIE. */
6444 static inline bool
6445 is_class_die (dw_die_ref c)
6447 return c && (c->die_tag == DW_TAG_class_type
6448 || c->die_tag == DW_TAG_structure_type);
6451 static char *
6452 gen_internal_sym (const char *prefix)
6454 char buf[256];
6456 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6457 return xstrdup (buf);
6460 /* Assign symbols to all worthy DIEs under DIE. */
6462 static void
6463 assign_symbol_names (dw_die_ref die)
6465 dw_die_ref c;
6467 if (is_symbol_die (die) && !die->comdat_type_p)
6469 if (comdat_symbol_id)
6471 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6473 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6474 comdat_symbol_id, comdat_symbol_number++);
6475 die->die_id.die_symbol = xstrdup (p);
6477 else
6478 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6481 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6484 struct cu_hash_table_entry
6486 dw_die_ref cu;
6487 unsigned min_comdat_num, max_comdat_num;
6488 struct cu_hash_table_entry *next;
6491 /* Routines to manipulate hash table of CUs. */
6492 static hashval_t
6493 htab_cu_hash (const void *of)
6495 const struct cu_hash_table_entry *const entry =
6496 (const struct cu_hash_table_entry *) of;
6498 return htab_hash_string (entry->cu->die_id.die_symbol);
6501 static int
6502 htab_cu_eq (const void *of1, const void *of2)
6504 const struct cu_hash_table_entry *const entry1 =
6505 (const struct cu_hash_table_entry *) of1;
6506 const struct die_struct *const entry2 = (const struct die_struct *) of2;
6508 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6511 static void
6512 htab_cu_del (void *what)
6514 struct cu_hash_table_entry *next,
6515 *entry = (struct cu_hash_table_entry *) what;
6517 while (entry)
6519 next = entry->next;
6520 free (entry);
6521 entry = next;
6525 /* Check whether we have already seen this CU and set up SYM_NUM
6526 accordingly. */
6527 static int
6528 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6530 struct cu_hash_table_entry dummy;
6531 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6533 dummy.max_comdat_num = 0;
6535 slot = (struct cu_hash_table_entry **)
6536 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6537 INSERT);
6538 entry = *slot;
6540 for (; entry; last = entry, entry = entry->next)
6542 if (same_die_p_wrap (cu, entry->cu))
6543 break;
6546 if (entry)
6548 *sym_num = entry->min_comdat_num;
6549 return 1;
6552 entry = XCNEW (struct cu_hash_table_entry);
6553 entry->cu = cu;
6554 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6555 entry->next = *slot;
6556 *slot = entry;
6558 return 0;
6561 /* Record SYM_NUM to record of CU in HTABLE. */
6562 static void
6563 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6565 struct cu_hash_table_entry **slot, *entry;
6567 slot = (struct cu_hash_table_entry **)
6568 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6569 NO_INSERT);
6570 entry = *slot;
6572 entry->max_comdat_num = sym_num;
6575 /* Traverse the DIE (which is always comp_unit_die), and set up
6576 additional compilation units for each of the include files we see
6577 bracketed by BINCL/EINCL. */
6579 static void
6580 break_out_includes (dw_die_ref die)
6582 dw_die_ref c;
6583 dw_die_ref unit = NULL;
6584 limbo_die_node *node, **pnode;
6585 htab_t cu_hash_table;
6587 c = die->die_child;
6588 if (c) do {
6589 dw_die_ref prev = c;
6590 c = c->die_sib;
6591 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6592 || (unit && is_comdat_die (c)))
6594 dw_die_ref next = c->die_sib;
6596 /* This DIE is for a secondary CU; remove it from the main one. */
6597 remove_child_with_prev (c, prev);
6599 if (c->die_tag == DW_TAG_GNU_BINCL)
6600 unit = push_new_compile_unit (unit, c);
6601 else if (c->die_tag == DW_TAG_GNU_EINCL)
6602 unit = pop_compile_unit (unit);
6603 else
6604 add_child_die (unit, c);
6605 c = next;
6606 if (c == die->die_child)
6607 break;
6609 } while (c != die->die_child);
6611 #if 0
6612 /* We can only use this in debugging, since the frontend doesn't check
6613 to make sure that we leave every include file we enter. */
6614 gcc_assert (!unit);
6615 #endif
6617 assign_symbol_names (die);
6618 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6619 for (node = limbo_die_list, pnode = &limbo_die_list;
6620 node;
6621 node = node->next)
6623 int is_dupl;
6625 compute_section_prefix (node->die);
6626 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6627 &comdat_symbol_number);
6628 assign_symbol_names (node->die);
6629 if (is_dupl)
6630 *pnode = node->next;
6631 else
6633 pnode = &node->next;
6634 record_comdat_symbol_number (node->die, cu_hash_table,
6635 comdat_symbol_number);
6638 htab_delete (cu_hash_table);
6641 /* Return non-zero if this DIE is a declaration. */
6643 static int
6644 is_declaration_die (dw_die_ref die)
6646 dw_attr_ref a;
6647 unsigned ix;
6649 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6650 if (a->dw_attr == DW_AT_declaration)
6651 return 1;
6653 return 0;
6656 /* Return non-zero if this DIE is nested inside a subprogram. */
6658 static int
6659 is_nested_in_subprogram (dw_die_ref die)
6661 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6663 if (decl == NULL)
6664 decl = die;
6665 return local_scope_p (decl);
6668 /* Return non-zero if this DIE contains a defining declaration of a
6669 subprogram. */
6671 static int
6672 contains_subprogram_definition (dw_die_ref die)
6674 dw_die_ref c;
6676 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6677 return 1;
6678 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6679 return 0;
6682 /* Return non-zero if this is a type DIE that should be moved to a
6683 COMDAT .debug_types section. */
6685 static int
6686 should_move_die_to_comdat (dw_die_ref die)
6688 switch (die->die_tag)
6690 case DW_TAG_class_type:
6691 case DW_TAG_structure_type:
6692 case DW_TAG_enumeration_type:
6693 case DW_TAG_union_type:
6694 /* Don't move declarations, inlined instances, or types nested in a
6695 subprogram. */
6696 if (is_declaration_die (die)
6697 || get_AT (die, DW_AT_abstract_origin)
6698 || is_nested_in_subprogram (die))
6699 return 0;
6700 /* A type definition should never contain a subprogram definition. */
6701 gcc_assert (!contains_subprogram_definition (die));
6702 return 1;
6703 case DW_TAG_array_type:
6704 case DW_TAG_interface_type:
6705 case DW_TAG_pointer_type:
6706 case DW_TAG_reference_type:
6707 case DW_TAG_rvalue_reference_type:
6708 case DW_TAG_string_type:
6709 case DW_TAG_subroutine_type:
6710 case DW_TAG_ptr_to_member_type:
6711 case DW_TAG_set_type:
6712 case DW_TAG_subrange_type:
6713 case DW_TAG_base_type:
6714 case DW_TAG_const_type:
6715 case DW_TAG_file_type:
6716 case DW_TAG_packed_type:
6717 case DW_TAG_volatile_type:
6718 case DW_TAG_typedef:
6719 default:
6720 return 0;
6724 /* Make a clone of DIE. */
6726 static dw_die_ref
6727 clone_die (dw_die_ref die)
6729 dw_die_ref clone;
6730 dw_attr_ref a;
6731 unsigned ix;
6733 clone = ggc_alloc_cleared_die_node ();
6734 clone->die_tag = die->die_tag;
6736 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6737 add_dwarf_attr (clone, a);
6739 return clone;
6742 /* Make a clone of the tree rooted at DIE. */
6744 static dw_die_ref
6745 clone_tree (dw_die_ref die)
6747 dw_die_ref c;
6748 dw_die_ref clone = clone_die (die);
6750 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6752 return clone;
6755 /* Make a clone of DIE as a declaration. */
6757 static dw_die_ref
6758 clone_as_declaration (dw_die_ref die)
6760 dw_die_ref clone;
6761 dw_die_ref decl;
6762 dw_attr_ref a;
6763 unsigned ix;
6765 /* If the DIE is already a declaration, just clone it. */
6766 if (is_declaration_die (die))
6767 return clone_die (die);
6769 /* If the DIE is a specification, just clone its declaration DIE. */
6770 decl = get_AT_ref (die, DW_AT_specification);
6771 if (decl != NULL)
6773 clone = clone_die (decl);
6774 if (die->comdat_type_p)
6775 add_AT_die_ref (clone, DW_AT_signature, die);
6776 return clone;
6779 clone = ggc_alloc_cleared_die_node ();
6780 clone->die_tag = die->die_tag;
6782 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6784 /* We don't want to copy over all attributes.
6785 For example we don't want DW_AT_byte_size because otherwise we will no
6786 longer have a declaration and GDB will treat it as a definition. */
6788 switch (a->dw_attr)
6790 case DW_AT_artificial:
6791 case DW_AT_containing_type:
6792 case DW_AT_external:
6793 case DW_AT_name:
6794 case DW_AT_type:
6795 case DW_AT_virtuality:
6796 case DW_AT_linkage_name:
6797 case DW_AT_MIPS_linkage_name:
6798 add_dwarf_attr (clone, a);
6799 break;
6800 case DW_AT_byte_size:
6801 default:
6802 break;
6806 if (die->comdat_type_p)
6807 add_AT_die_ref (clone, DW_AT_signature, die);
6809 add_AT_flag (clone, DW_AT_declaration, 1);
6810 return clone;
6813 /* Copy the declaration context to the new type unit DIE. This includes
6814 any surrounding namespace or type declarations. If the DIE has an
6815 AT_specification attribute, it also includes attributes and children
6816 attached to the specification, and returns a pointer to the original
6817 parent of the declaration DIE. Returns NULL otherwise. */
6819 static dw_die_ref
6820 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6822 dw_die_ref decl;
6823 dw_die_ref new_decl;
6824 dw_die_ref orig_parent = NULL;
6826 decl = get_AT_ref (die, DW_AT_specification);
6827 if (decl == NULL)
6828 decl = die;
6829 else
6831 unsigned ix;
6832 dw_die_ref c;
6833 dw_attr_ref a;
6835 /* The original DIE will be changed to a declaration, and must
6836 be moved to be a child of the original declaration DIE. */
6837 orig_parent = decl->die_parent;
6839 /* Copy the type node pointer from the new DIE to the original
6840 declaration DIE so we can forward references later. */
6841 decl->comdat_type_p = true;
6842 decl->die_id.die_type_node = die->die_id.die_type_node;
6844 remove_AT (die, DW_AT_specification);
6846 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
6848 if (a->dw_attr != DW_AT_name
6849 && a->dw_attr != DW_AT_declaration
6850 && a->dw_attr != DW_AT_external)
6851 add_dwarf_attr (die, a);
6854 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6857 if (decl->die_parent != NULL
6858 && !is_unit_die (decl->die_parent))
6860 new_decl = copy_ancestor_tree (unit, decl, NULL);
6861 if (new_decl != NULL)
6863 remove_AT (new_decl, DW_AT_signature);
6864 add_AT_specification (die, new_decl);
6868 return orig_parent;
6871 /* Generate the skeleton ancestor tree for the given NODE, then clone
6872 the DIE and add the clone into the tree. */
6874 static void
6875 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6877 if (node->new_die != NULL)
6878 return;
6880 node->new_die = clone_as_declaration (node->old_die);
6882 if (node->parent != NULL)
6884 generate_skeleton_ancestor_tree (node->parent);
6885 add_child_die (node->parent->new_die, node->new_die);
6889 /* Generate a skeleton tree of DIEs containing any declarations that are
6890 found in the original tree. We traverse the tree looking for declaration
6891 DIEs, and construct the skeleton from the bottom up whenever we find one. */
6893 static void
6894 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6896 skeleton_chain_node node;
6897 dw_die_ref c;
6898 dw_die_ref first;
6899 dw_die_ref prev = NULL;
6900 dw_die_ref next = NULL;
6902 node.parent = parent;
6904 first = c = parent->old_die->die_child;
6905 if (c)
6906 next = c->die_sib;
6907 if (c) do {
6908 if (prev == NULL || prev->die_sib == c)
6909 prev = c;
6910 c = next;
6911 next = (c == first ? NULL : c->die_sib);
6912 node.old_die = c;
6913 node.new_die = NULL;
6914 if (is_declaration_die (c))
6916 /* Clone the existing DIE, move the original to the skeleton
6917 tree (which is in the main CU), and put the clone, with
6918 all the original's children, where the original came from. */
6919 dw_die_ref clone = clone_die (c);
6920 move_all_children (c, clone);
6922 replace_child (c, clone, prev);
6923 generate_skeleton_ancestor_tree (parent);
6924 add_child_die (parent->new_die, c);
6925 node.new_die = c;
6926 c = clone;
6928 generate_skeleton_bottom_up (&node);
6929 } while (next != NULL);
6932 /* Wrapper function for generate_skeleton_bottom_up. */
6934 static dw_die_ref
6935 generate_skeleton (dw_die_ref die)
6937 skeleton_chain_node node;
6939 node.old_die = die;
6940 node.new_die = NULL;
6941 node.parent = NULL;
6943 /* If this type definition is nested inside another type,
6944 always leave at least a declaration in its place. */
6945 if (die->die_parent != NULL && is_type_die (die->die_parent))
6946 node.new_die = clone_as_declaration (die);
6948 generate_skeleton_bottom_up (&node);
6949 return node.new_die;
6952 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6953 declaration. The original DIE is moved to a new compile unit so that
6954 existing references to it follow it to the new location. If any of the
6955 original DIE's descendants is a declaration, we need to replace the
6956 original DIE with a skeleton tree and move the declarations back into the
6957 skeleton tree. */
6959 static dw_die_ref
6960 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6961 dw_die_ref prev)
6963 dw_die_ref skeleton, orig_parent;
6965 /* Copy the declaration context to the type unit DIE. If the returned
6966 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6967 that DIE. */
6968 orig_parent = copy_declaration_context (unit, child);
6970 skeleton = generate_skeleton (child);
6971 if (skeleton == NULL)
6972 remove_child_with_prev (child, prev);
6973 else
6975 skeleton->comdat_type_p = true;
6976 skeleton->die_id.die_type_node = child->die_id.die_type_node;
6978 /* If the original DIE was a specification, we need to put
6979 the skeleton under the parent DIE of the declaration.
6980 This leaves the original declaration in the tree, but
6981 it will be pruned later since there are no longer any
6982 references to it. */
6983 if (orig_parent != NULL)
6985 remove_child_with_prev (child, prev);
6986 add_child_die (orig_parent, skeleton);
6988 else
6989 replace_child (child, skeleton, prev);
6992 return skeleton;
6995 /* Traverse the DIE and set up additional .debug_types sections for each
6996 type worthy of being placed in a COMDAT section. */
6998 static void
6999 break_out_comdat_types (dw_die_ref die)
7001 dw_die_ref c;
7002 dw_die_ref first;
7003 dw_die_ref prev = NULL;
7004 dw_die_ref next = NULL;
7005 dw_die_ref unit = NULL;
7007 first = c = die->die_child;
7008 if (c)
7009 next = c->die_sib;
7010 if (c) do {
7011 if (prev == NULL || prev->die_sib == c)
7012 prev = c;
7013 c = next;
7014 next = (c == first ? NULL : c->die_sib);
7015 if (should_move_die_to_comdat (c))
7017 dw_die_ref replacement;
7018 comdat_type_node_ref type_node;
7020 /* Create a new type unit DIE as the root for the new tree, and
7021 add it to the list of comdat types. */
7022 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7023 add_AT_unsigned (unit, DW_AT_language,
7024 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7025 type_node = ggc_alloc_cleared_comdat_type_node ();
7026 type_node->root_die = unit;
7027 type_node->next = comdat_type_list;
7028 comdat_type_list = type_node;
7030 /* Generate the type signature. */
7031 generate_type_signature (c, type_node);
7033 /* Copy the declaration context, attributes, and children of the
7034 declaration into the new type unit DIE, then remove this DIE
7035 from the main CU (or replace it with a skeleton if necessary). */
7036 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7037 type_node->skeleton_die = replacement;
7039 /* Break out nested types into their own type units. */
7040 break_out_comdat_types (c);
7042 /* Add the DIE to the new compunit. */
7043 add_child_die (unit, c);
7045 if (replacement != NULL)
7046 c = replacement;
7048 else if (c->die_tag == DW_TAG_namespace
7049 || c->die_tag == DW_TAG_class_type
7050 || c->die_tag == DW_TAG_structure_type
7051 || c->die_tag == DW_TAG_union_type)
7053 /* Look for nested types that can be broken out. */
7054 break_out_comdat_types (c);
7056 } while (next != NULL);
7059 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7061 struct decl_table_entry
7063 dw_die_ref orig;
7064 dw_die_ref copy;
7067 /* Routines to manipulate hash table of copied declarations. */
7069 static hashval_t
7070 htab_decl_hash (const void *of)
7072 const struct decl_table_entry *const entry =
7073 (const struct decl_table_entry *) of;
7075 return htab_hash_pointer (entry->orig);
7078 static int
7079 htab_decl_eq (const void *of1, const void *of2)
7081 const struct decl_table_entry *const entry1 =
7082 (const struct decl_table_entry *) of1;
7083 const struct die_struct *const entry2 = (const struct die_struct *) of2;
7085 return entry1->orig == entry2;
7088 static void
7089 htab_decl_del (void *what)
7091 struct decl_table_entry *entry = (struct decl_table_entry *) what;
7093 free (entry);
7096 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7097 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7098 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7099 to check if the ancestor has already been copied into UNIT. */
7101 static dw_die_ref
7102 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7104 dw_die_ref parent = die->die_parent;
7105 dw_die_ref new_parent = unit;
7106 dw_die_ref copy;
7107 void **slot = NULL;
7108 struct decl_table_entry *entry = NULL;
7110 if (decl_table)
7112 /* Check if the entry has already been copied to UNIT. */
7113 slot = htab_find_slot_with_hash (decl_table, die,
7114 htab_hash_pointer (die), INSERT);
7115 if (*slot != HTAB_EMPTY_ENTRY)
7117 entry = (struct decl_table_entry *) *slot;
7118 return entry->copy;
7121 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7122 entry = XCNEW (struct decl_table_entry);
7123 entry->orig = die;
7124 entry->copy = NULL;
7125 *slot = entry;
7128 if (parent != NULL)
7130 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7131 if (spec != NULL)
7132 parent = spec;
7133 if (!is_unit_die (parent))
7134 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7137 copy = clone_as_declaration (die);
7138 add_child_die (new_parent, copy);
7140 if (decl_table != NULL)
7142 /* Record the pointer to the copy. */
7143 entry->copy = copy;
7146 return copy;
7149 /* Like clone_tree, but additionally enter all the children into
7150 the hash table decl_table. */
7152 static dw_die_ref
7153 clone_tree_hash (dw_die_ref die, htab_t decl_table)
7155 dw_die_ref c;
7156 dw_die_ref clone = clone_die (die);
7157 struct decl_table_entry *entry;
7158 void **slot = htab_find_slot_with_hash (decl_table, die,
7159 htab_hash_pointer (die), INSERT);
7160 /* Assert that DIE isn't in the hash table yet. If it would be there
7161 before, the ancestors would be necessarily there as well, therefore
7162 clone_tree_hash wouldn't be called. */
7163 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7164 entry = XCNEW (struct decl_table_entry);
7165 entry->orig = die;
7166 entry->copy = clone;
7167 *slot = entry;
7169 FOR_EACH_CHILD (die, c,
7170 add_child_die (clone, clone_tree_hash (c, decl_table)));
7172 return clone;
7175 /* Walk the DIE and its children, looking for references to incomplete
7176 or trivial types that are unmarked (i.e., that are not in the current
7177 type_unit). */
7179 static void
7180 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7182 dw_die_ref c;
7183 dw_attr_ref a;
7184 unsigned ix;
7186 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7188 if (AT_class (a) == dw_val_class_die_ref)
7190 dw_die_ref targ = AT_ref (a);
7191 void **slot;
7192 struct decl_table_entry *entry;
7194 if (targ->die_mark != 0 || targ->comdat_type_p)
7195 continue;
7197 slot = htab_find_slot_with_hash (decl_table, targ,
7198 htab_hash_pointer (targ), INSERT);
7200 if (*slot != HTAB_EMPTY_ENTRY)
7202 /* TARG has already been copied, so we just need to
7203 modify the reference to point to the copy. */
7204 entry = (struct decl_table_entry *) *slot;
7205 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7207 else
7209 dw_die_ref parent = unit;
7210 dw_die_ref copy = clone_die (targ);
7212 /* Record in DECL_TABLE that TARG has been copied.
7213 Need to do this now, before the recursive call,
7214 because DECL_TABLE may be expanded and SLOT
7215 would no longer be a valid pointer. */
7216 entry = XCNEW (struct decl_table_entry);
7217 entry->orig = targ;
7218 entry->copy = copy;
7219 *slot = entry;
7221 FOR_EACH_CHILD (targ, c,
7222 add_child_die (copy,
7223 clone_tree_hash (c, decl_table)));
7225 /* Make sure the cloned tree is marked as part of the
7226 type unit. */
7227 mark_dies (copy);
7229 /* If TARG has surrounding context, copy its ancestor tree
7230 into the new type unit. */
7231 if (targ->die_parent != NULL
7232 && !is_unit_die (targ->die_parent))
7233 parent = copy_ancestor_tree (unit, targ->die_parent,
7234 decl_table);
7236 add_child_die (parent, copy);
7237 a->dw_attr_val.v.val_die_ref.die = copy;
7239 /* Make sure the newly-copied DIE is walked. If it was
7240 installed in a previously-added context, it won't
7241 get visited otherwise. */
7242 if (parent != unit)
7244 /* Find the highest point of the newly-added tree,
7245 mark each node along the way, and walk from there. */
7246 parent->die_mark = 1;
7247 while (parent->die_parent
7248 && parent->die_parent->die_mark == 0)
7250 parent = parent->die_parent;
7251 parent->die_mark = 1;
7253 copy_decls_walk (unit, parent, decl_table);
7259 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7262 /* Copy declarations for "unworthy" types into the new comdat section.
7263 Incomplete types, modified types, and certain other types aren't broken
7264 out into comdat sections of their own, so they don't have a signature,
7265 and we need to copy the declaration into the same section so that we
7266 don't have an external reference. */
7268 static void
7269 copy_decls_for_unworthy_types (dw_die_ref unit)
7271 htab_t decl_table;
7273 mark_dies (unit);
7274 decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7275 copy_decls_walk (unit, unit, decl_table);
7276 htab_delete (decl_table);
7277 unmark_dies (unit);
7280 /* Traverse the DIE and add a sibling attribute if it may have the
7281 effect of speeding up access to siblings. To save some space,
7282 avoid generating sibling attributes for DIE's without children. */
7284 static void
7285 add_sibling_attributes (dw_die_ref die)
7287 dw_die_ref c;
7289 if (! die->die_child)
7290 return;
7292 if (die->die_parent && die != die->die_parent->die_child)
7293 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7295 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7298 /* Output all location lists for the DIE and its children. */
7300 static void
7301 output_location_lists (dw_die_ref die)
7303 dw_die_ref c;
7304 dw_attr_ref a;
7305 unsigned ix;
7307 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7308 if (AT_class (a) == dw_val_class_loc_list)
7309 output_loc_list (AT_loc_list (a));
7311 FOR_EACH_CHILD (die, c, output_location_lists (c));
7314 /* We want to limit the number of external references, because they are
7315 larger than local references: a relocation takes multiple words, and
7316 even a sig8 reference is always eight bytes, whereas a local reference
7317 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7318 So if we encounter multiple external references to the same type DIE, we
7319 make a local typedef stub for it and redirect all references there.
7321 This is the element of the hash table for keeping track of these
7322 references. */
7324 struct external_ref
7326 dw_die_ref type;
7327 dw_die_ref stub;
7328 unsigned n_refs;
7331 /* Hash an external_ref. */
7333 static hashval_t
7334 hash_external_ref (const void *p)
7336 const struct external_ref *r = (const struct external_ref *)p;
7337 return htab_hash_pointer (r->type);
7340 /* Compare external_refs. */
7342 static int
7343 external_ref_eq (const void *p1, const void *p2)
7345 const struct external_ref *r1 = (const struct external_ref *)p1;
7346 const struct external_ref *r2 = (const struct external_ref *)p2;
7347 return r1->type == r2->type;
7350 /* Return a pointer to the external_ref for references to DIE. */
7352 static struct external_ref *
7353 lookup_external_ref (htab_t map, dw_die_ref die)
7355 struct external_ref ref, *ref_p;
7356 void ** slot;
7358 ref.type = die;
7359 slot = htab_find_slot (map, &ref, INSERT);
7360 if (*slot != HTAB_EMPTY_ENTRY)
7361 return (struct external_ref *) *slot;
7363 ref_p = XCNEW (struct external_ref);
7364 ref_p->type = die;
7365 *slot = ref_p;
7366 return ref_p;
7369 /* Subroutine of optimize_external_refs, below.
7371 If we see a type skeleton, record it as our stub. If we see external
7372 references, remember how many we've seen. */
7374 static void
7375 optimize_external_refs_1 (dw_die_ref die, htab_t map)
7377 dw_die_ref c;
7378 dw_attr_ref a;
7379 unsigned ix;
7380 struct external_ref *ref_p;
7382 if (is_type_die (die)
7383 && (c = get_AT_ref (die, DW_AT_signature)))
7385 /* This is a local skeleton; use it for local references. */
7386 ref_p = lookup_external_ref (map, c);
7387 ref_p->stub = die;
7390 /* Scan the DIE references, and remember any that refer to DIEs from
7391 other CUs (i.e. those which are not marked). */
7392 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7393 if (AT_class (a) == dw_val_class_die_ref
7394 && (c = AT_ref (a))->die_mark == 0
7395 && is_type_die (c))
7397 ref_p = lookup_external_ref (map, c);
7398 ref_p->n_refs++;
7401 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7404 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7405 points to an external_ref, DATA is the CU we're processing. If we don't
7406 already have a local stub, and we have multiple refs, build a stub. */
7408 static int
7409 build_local_stub (void **slot, void *data)
7411 struct external_ref *ref_p = (struct external_ref *)*slot;
7413 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7415 /* We have multiple references to this type, so build a small stub.
7416 Both of these forms are a bit dodgy from the perspective of the
7417 DWARF standard, since technically they should have names. */
7418 dw_die_ref cu = (dw_die_ref) data;
7419 dw_die_ref type = ref_p->type;
7420 dw_die_ref stub = NULL;
7422 if (type->comdat_type_p)
7424 /* If we refer to this type via sig8, use AT_signature. */
7425 stub = new_die (type->die_tag, cu, NULL_TREE);
7426 add_AT_die_ref (stub, DW_AT_signature, type);
7428 else
7430 /* Otherwise, use a typedef with no name. */
7431 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7432 add_AT_die_ref (stub, DW_AT_type, type);
7435 stub->die_mark++;
7436 ref_p->stub = stub;
7438 return 1;
7441 /* DIE is a unit; look through all the DIE references to see if there are
7442 any external references to types, and if so, create local stubs for
7443 them which will be applied in build_abbrev_table. This is useful because
7444 references to local DIEs are smaller. */
7446 static htab_t
7447 optimize_external_refs (dw_die_ref die)
7449 htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7450 optimize_external_refs_1 (die, map);
7451 htab_traverse (map, build_local_stub, die);
7452 return map;
7455 /* The format of each DIE (and its attribute value pairs) is encoded in an
7456 abbreviation table. This routine builds the abbreviation table and assigns
7457 a unique abbreviation id for each abbreviation entry. The children of each
7458 die are visited recursively. */
7460 static void
7461 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7463 unsigned long abbrev_id;
7464 unsigned int n_alloc;
7465 dw_die_ref c;
7466 dw_attr_ref a;
7467 unsigned ix;
7469 /* Scan the DIE references, and replace any that refer to
7470 DIEs from other CUs (i.e. those which are not marked) with
7471 the local stubs we built in optimize_external_refs. */
7472 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7473 if (AT_class (a) == dw_val_class_die_ref
7474 && (c = AT_ref (a))->die_mark == 0)
7476 struct external_ref *ref_p;
7477 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7479 ref_p = lookup_external_ref (extern_map, c);
7480 if (ref_p->stub && ref_p->stub != die)
7481 change_AT_die_ref (a, ref_p->stub);
7482 else
7483 /* We aren't changing this reference, so mark it external. */
7484 set_AT_ref_external (a, 1);
7487 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7489 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7490 dw_attr_ref die_a, abbrev_a;
7491 unsigned ix;
7492 bool ok = true;
7494 if (abbrev->die_tag != die->die_tag)
7495 continue;
7496 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7497 continue;
7499 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7500 continue;
7502 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7504 abbrev_a = &(*abbrev->die_attr)[ix];
7505 if ((abbrev_a->dw_attr != die_a->dw_attr)
7506 || (value_format (abbrev_a) != value_format (die_a)))
7508 ok = false;
7509 break;
7512 if (ok)
7513 break;
7516 if (abbrev_id >= abbrev_die_table_in_use)
7518 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7520 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7521 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7522 n_alloc);
7524 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7525 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7526 abbrev_die_table_allocated = n_alloc;
7529 ++abbrev_die_table_in_use;
7530 abbrev_die_table[abbrev_id] = die;
7533 die->die_abbrev = abbrev_id;
7534 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7537 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7539 static int
7540 constant_size (unsigned HOST_WIDE_INT value)
7542 int log;
7544 if (value == 0)
7545 log = 0;
7546 else
7547 log = floor_log2 (value);
7549 log = log / 8;
7550 log = 1 << (floor_log2 (log) + 1);
7552 return log;
7555 /* Return the size of a DIE as it is represented in the
7556 .debug_info section. */
7558 static unsigned long
7559 size_of_die (dw_die_ref die)
7561 unsigned long size = 0;
7562 dw_attr_ref a;
7563 unsigned ix;
7564 enum dwarf_form form;
7566 size += size_of_uleb128 (die->die_abbrev);
7567 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7569 switch (AT_class (a))
7571 case dw_val_class_addr:
7572 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7574 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7575 size += size_of_uleb128 (AT_index (a));
7577 else
7578 size += DWARF2_ADDR_SIZE;
7579 break;
7580 case dw_val_class_offset:
7581 size += DWARF_OFFSET_SIZE;
7582 break;
7583 case dw_val_class_loc:
7585 unsigned long lsize = size_of_locs (AT_loc (a));
7587 /* Block length. */
7588 if (dwarf_version >= 4)
7589 size += size_of_uleb128 (lsize);
7590 else
7591 size += constant_size (lsize);
7592 size += lsize;
7594 break;
7595 case dw_val_class_loc_list:
7596 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7598 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7599 size += size_of_uleb128 (AT_index (a));
7601 else
7602 size += DWARF_OFFSET_SIZE;
7603 break;
7604 case dw_val_class_range_list:
7605 size += DWARF_OFFSET_SIZE;
7606 break;
7607 case dw_val_class_const:
7608 size += size_of_sleb128 (AT_int (a));
7609 break;
7610 case dw_val_class_unsigned_const:
7612 int csize = constant_size (AT_unsigned (a));
7613 if (dwarf_version == 3
7614 && a->dw_attr == DW_AT_data_member_location
7615 && csize >= 4)
7616 size += size_of_uleb128 (AT_unsigned (a));
7617 else
7618 size += csize;
7620 break;
7621 case dw_val_class_const_double:
7622 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7623 if (HOST_BITS_PER_WIDE_INT >= 64)
7624 size++; /* block */
7625 break;
7626 case dw_val_class_vec:
7627 size += constant_size (a->dw_attr_val.v.val_vec.length
7628 * a->dw_attr_val.v.val_vec.elt_size)
7629 + a->dw_attr_val.v.val_vec.length
7630 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7631 break;
7632 case dw_val_class_flag:
7633 if (dwarf_version >= 4)
7634 /* Currently all add_AT_flag calls pass in 1 as last argument,
7635 so DW_FORM_flag_present can be used. If that ever changes,
7636 we'll need to use DW_FORM_flag and have some optimization
7637 in build_abbrev_table that will change those to
7638 DW_FORM_flag_present if it is set to 1 in all DIEs using
7639 the same abbrev entry. */
7640 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7641 else
7642 size += 1;
7643 break;
7644 case dw_val_class_die_ref:
7645 if (AT_ref_external (a))
7647 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7648 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7649 is sized by target address length, whereas in DWARF3
7650 it's always sized as an offset. */
7651 if (use_debug_types)
7652 size += DWARF_TYPE_SIGNATURE_SIZE;
7653 else if (dwarf_version == 2)
7654 size += DWARF2_ADDR_SIZE;
7655 else
7656 size += DWARF_OFFSET_SIZE;
7658 else
7659 size += DWARF_OFFSET_SIZE;
7660 break;
7661 case dw_val_class_fde_ref:
7662 size += DWARF_OFFSET_SIZE;
7663 break;
7664 case dw_val_class_lbl_id:
7665 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7667 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7668 size += size_of_uleb128 (AT_index (a));
7670 else
7671 size += DWARF2_ADDR_SIZE;
7672 break;
7673 case dw_val_class_lineptr:
7674 case dw_val_class_macptr:
7675 size += DWARF_OFFSET_SIZE;
7676 break;
7677 case dw_val_class_str:
7678 form = AT_string_form (a);
7679 if (form == DW_FORM_strp)
7680 size += DWARF_OFFSET_SIZE;
7681 else if (form == DW_FORM_GNU_str_index)
7682 size += size_of_uleb128 (AT_index (a));
7683 else
7684 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7685 break;
7686 case dw_val_class_file:
7687 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7688 break;
7689 case dw_val_class_data8:
7690 size += 8;
7691 break;
7692 case dw_val_class_vms_delta:
7693 size += DWARF_OFFSET_SIZE;
7694 break;
7695 case dw_val_class_high_pc:
7696 size += DWARF2_ADDR_SIZE;
7697 break;
7698 default:
7699 gcc_unreachable ();
7703 return size;
7706 /* Size the debugging information associated with a given DIE. Visits the
7707 DIE's children recursively. Updates the global variable next_die_offset, on
7708 each time through. Uses the current value of next_die_offset to update the
7709 die_offset field in each DIE. */
7711 static void
7712 calc_die_sizes (dw_die_ref die)
7714 dw_die_ref c;
7716 gcc_assert (die->die_offset == 0
7717 || (unsigned long int) die->die_offset == next_die_offset);
7718 die->die_offset = next_die_offset;
7719 next_die_offset += size_of_die (die);
7721 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7723 if (die->die_child != NULL)
7724 /* Count the null byte used to terminate sibling lists. */
7725 next_die_offset += 1;
7728 /* Size just the base type children at the start of the CU.
7729 This is needed because build_abbrev needs to size locs
7730 and sizing of type based stack ops needs to know die_offset
7731 values for the base types. */
7733 static void
7734 calc_base_type_die_sizes (void)
7736 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7737 unsigned int i;
7738 dw_die_ref base_type;
7739 #if ENABLE_ASSERT_CHECKING
7740 dw_die_ref prev = comp_unit_die ()->die_child;
7741 #endif
7743 die_offset += size_of_die (comp_unit_die ());
7744 for (i = 0; base_types.iterate (i, &base_type); i++)
7746 #if ENABLE_ASSERT_CHECKING
7747 gcc_assert (base_type->die_offset == 0
7748 && prev->die_sib == base_type
7749 && base_type->die_child == NULL
7750 && base_type->die_abbrev);
7751 prev = base_type;
7752 #endif
7753 base_type->die_offset = die_offset;
7754 die_offset += size_of_die (base_type);
7758 /* Set the marks for a die and its children. We do this so
7759 that we know whether or not a reference needs to use FORM_ref_addr; only
7760 DIEs in the same CU will be marked. We used to clear out the offset
7761 and use that as the flag, but ran into ordering problems. */
7763 static void
7764 mark_dies (dw_die_ref die)
7766 dw_die_ref c;
7768 gcc_assert (!die->die_mark);
7770 die->die_mark = 1;
7771 FOR_EACH_CHILD (die, c, mark_dies (c));
7774 /* Clear the marks for a die and its children. */
7776 static void
7777 unmark_dies (dw_die_ref die)
7779 dw_die_ref c;
7781 if (! use_debug_types)
7782 gcc_assert (die->die_mark);
7784 die->die_mark = 0;
7785 FOR_EACH_CHILD (die, c, unmark_dies (c));
7788 /* Clear the marks for a die, its children and referred dies. */
7790 static void
7791 unmark_all_dies (dw_die_ref die)
7793 dw_die_ref c;
7794 dw_attr_ref a;
7795 unsigned ix;
7797 if (!die->die_mark)
7798 return;
7799 die->die_mark = 0;
7801 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7803 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7804 if (AT_class (a) == dw_val_class_die_ref)
7805 unmark_all_dies (AT_ref (a));
7808 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7809 generated for the compilation unit. */
7811 static unsigned long
7812 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7814 unsigned long size;
7815 unsigned i;
7816 pubname_ref p;
7818 size = DWARF_PUBNAMES_HEADER_SIZE;
7819 FOR_EACH_VEC_ELT (*names, i, p)
7820 if (names != pubtype_table
7821 || p->die->die_offset != 0
7822 || !flag_eliminate_unused_debug_types)
7823 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7825 size += DWARF_OFFSET_SIZE;
7826 return size;
7829 /* Return the size of the information in the .debug_aranges section. */
7831 static unsigned long
7832 size_of_aranges (void)
7834 unsigned long size;
7836 size = DWARF_ARANGES_HEADER_SIZE;
7838 /* Count the address/length pair for this compilation unit. */
7839 if (text_section_used)
7840 size += 2 * DWARF2_ADDR_SIZE;
7841 if (cold_text_section_used)
7842 size += 2 * DWARF2_ADDR_SIZE;
7843 if (have_multiple_function_sections)
7845 unsigned fde_idx;
7846 dw_fde_ref fde;
7848 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7850 if (!fde->in_std_section)
7851 size += 2 * DWARF2_ADDR_SIZE;
7852 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7853 size += 2 * DWARF2_ADDR_SIZE;
7857 /* Count the two zero words used to terminated the address range table. */
7858 size += 2 * DWARF2_ADDR_SIZE;
7859 return size;
7862 /* Select the encoding of an attribute value. */
7864 static enum dwarf_form
7865 value_format (dw_attr_ref a)
7867 switch (AT_class (a))
7869 case dw_val_class_addr:
7870 /* Only very few attributes allow DW_FORM_addr. */
7871 switch (a->dw_attr)
7873 case DW_AT_low_pc:
7874 case DW_AT_high_pc:
7875 case DW_AT_entry_pc:
7876 case DW_AT_trampoline:
7877 return (AT_index (a) == NOT_INDEXED
7878 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7879 default:
7880 break;
7882 switch (DWARF2_ADDR_SIZE)
7884 case 1:
7885 return DW_FORM_data1;
7886 case 2:
7887 return DW_FORM_data2;
7888 case 4:
7889 return DW_FORM_data4;
7890 case 8:
7891 return DW_FORM_data8;
7892 default:
7893 gcc_unreachable ();
7895 case dw_val_class_range_list:
7896 case dw_val_class_loc_list:
7897 if (dwarf_version >= 4)
7898 return DW_FORM_sec_offset;
7899 /* FALLTHRU */
7900 case dw_val_class_vms_delta:
7901 case dw_val_class_offset:
7902 switch (DWARF_OFFSET_SIZE)
7904 case 4:
7905 return DW_FORM_data4;
7906 case 8:
7907 return DW_FORM_data8;
7908 default:
7909 gcc_unreachable ();
7911 case dw_val_class_loc:
7912 if (dwarf_version >= 4)
7913 return DW_FORM_exprloc;
7914 switch (constant_size (size_of_locs (AT_loc (a))))
7916 case 1:
7917 return DW_FORM_block1;
7918 case 2:
7919 return DW_FORM_block2;
7920 case 4:
7921 return DW_FORM_block4;
7922 default:
7923 gcc_unreachable ();
7925 case dw_val_class_const:
7926 return DW_FORM_sdata;
7927 case dw_val_class_unsigned_const:
7928 switch (constant_size (AT_unsigned (a)))
7930 case 1:
7931 return DW_FORM_data1;
7932 case 2:
7933 return DW_FORM_data2;
7934 case 4:
7935 /* In DWARF3 DW_AT_data_member_location with
7936 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7937 constant, so we need to use DW_FORM_udata if we need
7938 a large constant. */
7939 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7940 return DW_FORM_udata;
7941 return DW_FORM_data4;
7942 case 8:
7943 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7944 return DW_FORM_udata;
7945 return DW_FORM_data8;
7946 default:
7947 gcc_unreachable ();
7949 case dw_val_class_const_double:
7950 switch (HOST_BITS_PER_WIDE_INT)
7952 case 8:
7953 return DW_FORM_data2;
7954 case 16:
7955 return DW_FORM_data4;
7956 case 32:
7957 return DW_FORM_data8;
7958 case 64:
7959 default:
7960 return DW_FORM_block1;
7962 case dw_val_class_vec:
7963 switch (constant_size (a->dw_attr_val.v.val_vec.length
7964 * a->dw_attr_val.v.val_vec.elt_size))
7966 case 1:
7967 return DW_FORM_block1;
7968 case 2:
7969 return DW_FORM_block2;
7970 case 4:
7971 return DW_FORM_block4;
7972 default:
7973 gcc_unreachable ();
7975 case dw_val_class_flag:
7976 if (dwarf_version >= 4)
7978 /* Currently all add_AT_flag calls pass in 1 as last argument,
7979 so DW_FORM_flag_present can be used. If that ever changes,
7980 we'll need to use DW_FORM_flag and have some optimization
7981 in build_abbrev_table that will change those to
7982 DW_FORM_flag_present if it is set to 1 in all DIEs using
7983 the same abbrev entry. */
7984 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7985 return DW_FORM_flag_present;
7987 return DW_FORM_flag;
7988 case dw_val_class_die_ref:
7989 if (AT_ref_external (a))
7990 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7991 else
7992 return DW_FORM_ref;
7993 case dw_val_class_fde_ref:
7994 return DW_FORM_data;
7995 case dw_val_class_lbl_id:
7996 return (AT_index (a) == NOT_INDEXED
7997 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7998 case dw_val_class_lineptr:
7999 case dw_val_class_macptr:
8000 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8001 case dw_val_class_str:
8002 return AT_string_form (a);
8003 case dw_val_class_file:
8004 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8006 case 1:
8007 return DW_FORM_data1;
8008 case 2:
8009 return DW_FORM_data2;
8010 case 4:
8011 return DW_FORM_data4;
8012 default:
8013 gcc_unreachable ();
8016 case dw_val_class_data8:
8017 return DW_FORM_data8;
8019 case dw_val_class_high_pc:
8020 switch (DWARF2_ADDR_SIZE)
8022 case 1:
8023 return DW_FORM_data1;
8024 case 2:
8025 return DW_FORM_data2;
8026 case 4:
8027 return DW_FORM_data4;
8028 case 8:
8029 return DW_FORM_data8;
8030 default:
8031 gcc_unreachable ();
8034 default:
8035 gcc_unreachable ();
8039 /* Output the encoding of an attribute value. */
8041 static void
8042 output_value_format (dw_attr_ref a)
8044 enum dwarf_form form = value_format (a);
8046 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8049 /* Given a die and id, produce the appropriate abbreviations. */
8051 static void
8052 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8054 unsigned ix;
8055 dw_attr_ref a_attr;
8057 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8058 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8059 dwarf_tag_name (abbrev->die_tag));
8061 if (abbrev->die_child != NULL)
8062 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8063 else
8064 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8066 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8068 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8069 dwarf_attr_name (a_attr->dw_attr));
8070 output_value_format (a_attr);
8073 dw2_asm_output_data (1, 0, NULL);
8074 dw2_asm_output_data (1, 0, NULL);
8078 /* Output the .debug_abbrev section which defines the DIE abbreviation
8079 table. */
8081 static void
8082 output_abbrev_section (void)
8084 unsigned long abbrev_id;
8086 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8087 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8089 /* Terminate the table. */
8090 dw2_asm_output_data (1, 0, NULL);
8093 /* Output a symbol we can use to refer to this DIE from another CU. */
8095 static inline void
8096 output_die_symbol (dw_die_ref die)
8098 const char *sym = die->die_id.die_symbol;
8100 gcc_assert (!die->comdat_type_p);
8102 if (sym == 0)
8103 return;
8105 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8106 /* We make these global, not weak; if the target doesn't support
8107 .linkonce, it doesn't support combining the sections, so debugging
8108 will break. */
8109 targetm.asm_out.globalize_label (asm_out_file, sym);
8111 ASM_OUTPUT_LABEL (asm_out_file, sym);
8114 /* Return a new location list, given the begin and end range, and the
8115 expression. */
8117 static inline dw_loc_list_ref
8118 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8119 const char *section)
8121 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8123 retlist->begin = begin;
8124 retlist->begin_entry = NULL;
8125 retlist->end = end;
8126 retlist->expr = expr;
8127 retlist->section = section;
8129 return retlist;
8132 /* Generate a new internal symbol for this location list node, if it
8133 hasn't got one yet. */
8135 static inline void
8136 gen_llsym (dw_loc_list_ref list)
8138 gcc_assert (!list->ll_symbol);
8139 list->ll_symbol = gen_internal_sym ("LLST");
8142 /* Output the location list given to us. */
8144 static void
8145 output_loc_list (dw_loc_list_ref list_head)
8147 dw_loc_list_ref curr = list_head;
8149 if (list_head->emitted)
8150 return;
8151 list_head->emitted = true;
8153 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8155 /* Walk the location list, and output each range + expression. */
8156 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8158 unsigned long size;
8159 /* Don't output an entry that starts and ends at the same address. */
8160 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8161 continue;
8162 size = size_of_locs (curr->expr);
8163 /* If the expression is too large, drop it on the floor. We could
8164 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8165 in the expression, but >= 64KB expressions for a single value
8166 in a single range are unlikely very useful. */
8167 if (size > 0xffff)
8168 continue;
8169 if (dwarf_split_debug_info)
8171 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8172 "Location list start/length entry (%s)",
8173 list_head->ll_symbol);
8174 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8175 "Location list range start index (%s)",
8176 curr->begin);
8177 /* The length field is 4 bytes. If we ever need to support
8178 an 8-byte length, we can add a new DW_LLE code or fall back
8179 to DW_LLE_GNU_start_end_entry. */
8180 dw2_asm_output_delta (4, curr->end, curr->begin,
8181 "Location list range length (%s)",
8182 list_head->ll_symbol);
8184 else if (!have_multiple_function_sections)
8186 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8187 "Location list begin address (%s)",
8188 list_head->ll_symbol);
8189 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8190 "Location list end address (%s)",
8191 list_head->ll_symbol);
8193 else
8195 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8196 "Location list begin address (%s)",
8197 list_head->ll_symbol);
8198 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8199 "Location list end address (%s)",
8200 list_head->ll_symbol);
8203 /* Output the block length for this list of location operations. */
8204 gcc_assert (size <= 0xffff);
8205 dw2_asm_output_data (2, size, "%s", "Location expression size");
8207 output_loc_sequence (curr->expr, -1);
8210 if (dwarf_split_debug_info)
8211 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8212 "Location list terminator (%s)",
8213 list_head->ll_symbol);
8214 else
8216 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8217 "Location list terminator begin (%s)",
8218 list_head->ll_symbol);
8219 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8220 "Location list terminator end (%s)",
8221 list_head->ll_symbol);
8225 /* Output a range_list offset into the debug_range section. Emit a
8226 relocated reference if val_entry is NULL, otherwise, emit an
8227 indirect reference. */
8229 static void
8230 output_range_list_offset (dw_attr_ref a)
8232 const char *name = dwarf_attr_name (a->dw_attr);
8234 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8236 char *p = strchr (ranges_section_label, '\0');
8237 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8238 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8239 debug_ranges_section, "%s", name);
8240 *p = '\0';
8242 else
8243 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8244 "%s (offset from %s)", name, ranges_section_label);
8247 /* Output the offset into the debug_loc section. */
8249 static void
8250 output_loc_list_offset (dw_attr_ref a)
8252 char *sym = AT_loc_list (a)->ll_symbol;
8254 gcc_assert (sym);
8255 if (dwarf_split_debug_info)
8256 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8257 "%s", dwarf_attr_name (a->dw_attr));
8258 else
8259 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8260 "%s", dwarf_attr_name (a->dw_attr));
8263 /* Output an attribute's index or value appropriately. */
8265 static void
8266 output_attr_index_or_value (dw_attr_ref a)
8268 const char *name = dwarf_attr_name (a->dw_attr);
8270 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8272 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8273 return;
8275 switch (AT_class (a))
8277 case dw_val_class_addr:
8278 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8279 break;
8280 case dw_val_class_high_pc:
8281 case dw_val_class_lbl_id:
8282 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8283 break;
8284 case dw_val_class_loc_list:
8285 output_loc_list_offset (a);
8286 break;
8287 default:
8288 gcc_unreachable ();
8292 /* Output a type signature. */
8294 static inline void
8295 output_signature (const char *sig, const char *name)
8297 int i;
8299 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8300 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8303 /* Output the DIE and its attributes. Called recursively to generate
8304 the definitions of each child DIE. */
8306 static void
8307 output_die (dw_die_ref die)
8309 dw_attr_ref a;
8310 dw_die_ref c;
8311 unsigned long size;
8312 unsigned ix;
8314 /* If someone in another CU might refer to us, set up a symbol for
8315 them to point to. */
8316 if (! die->comdat_type_p && die->die_id.die_symbol)
8317 output_die_symbol (die);
8319 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8320 (unsigned long)die->die_offset,
8321 dwarf_tag_name (die->die_tag));
8323 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8325 const char *name = dwarf_attr_name (a->dw_attr);
8327 switch (AT_class (a))
8329 case dw_val_class_addr:
8330 output_attr_index_or_value (a);
8331 break;
8333 case dw_val_class_offset:
8334 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8335 "%s", name);
8336 break;
8338 case dw_val_class_range_list:
8339 output_range_list_offset (a);
8340 break;
8342 case dw_val_class_loc:
8343 size = size_of_locs (AT_loc (a));
8345 /* Output the block length for this list of location operations. */
8346 if (dwarf_version >= 4)
8347 dw2_asm_output_data_uleb128 (size, "%s", name);
8348 else
8349 dw2_asm_output_data (constant_size (size), size, "%s", name);
8351 output_loc_sequence (AT_loc (a), -1);
8352 break;
8354 case dw_val_class_const:
8355 /* ??? It would be slightly more efficient to use a scheme like is
8356 used for unsigned constants below, but gdb 4.x does not sign
8357 extend. Gdb 5.x does sign extend. */
8358 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8359 break;
8361 case dw_val_class_unsigned_const:
8363 int csize = constant_size (AT_unsigned (a));
8364 if (dwarf_version == 3
8365 && a->dw_attr == DW_AT_data_member_location
8366 && csize >= 4)
8367 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8368 else
8369 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8371 break;
8373 case dw_val_class_const_double:
8375 unsigned HOST_WIDE_INT first, second;
8377 if (HOST_BITS_PER_WIDE_INT >= 64)
8378 dw2_asm_output_data (1,
8379 HOST_BITS_PER_DOUBLE_INT
8380 / HOST_BITS_PER_CHAR,
8381 NULL);
8383 if (WORDS_BIG_ENDIAN)
8385 first = a->dw_attr_val.v.val_double.high;
8386 second = a->dw_attr_val.v.val_double.low;
8388 else
8390 first = a->dw_attr_val.v.val_double.low;
8391 second = a->dw_attr_val.v.val_double.high;
8394 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8395 first, "%s", name);
8396 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8397 second, NULL);
8399 break;
8401 case dw_val_class_vec:
8403 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8404 unsigned int len = a->dw_attr_val.v.val_vec.length;
8405 unsigned int i;
8406 unsigned char *p;
8408 dw2_asm_output_data (constant_size (len * elt_size),
8409 len * elt_size, "%s", name);
8410 if (elt_size > sizeof (HOST_WIDE_INT))
8412 elt_size /= 2;
8413 len *= 2;
8415 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8416 i < len;
8417 i++, p += elt_size)
8418 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8419 "fp or vector constant word %u", i);
8420 break;
8423 case dw_val_class_flag:
8424 if (dwarf_version >= 4)
8426 /* Currently all add_AT_flag calls pass in 1 as last argument,
8427 so DW_FORM_flag_present can be used. If that ever changes,
8428 we'll need to use DW_FORM_flag and have some optimization
8429 in build_abbrev_table that will change those to
8430 DW_FORM_flag_present if it is set to 1 in all DIEs using
8431 the same abbrev entry. */
8432 gcc_assert (AT_flag (a) == 1);
8433 if (flag_debug_asm)
8434 fprintf (asm_out_file, "\t\t\t%s %s\n",
8435 ASM_COMMENT_START, name);
8436 break;
8438 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8439 break;
8441 case dw_val_class_loc_list:
8442 output_attr_index_or_value (a);
8443 break;
8445 case dw_val_class_die_ref:
8446 if (AT_ref_external (a))
8448 if (AT_ref (a)->comdat_type_p)
8450 comdat_type_node_ref type_node =
8451 AT_ref (a)->die_id.die_type_node;
8453 gcc_assert (type_node);
8454 output_signature (type_node->signature, name);
8456 else
8458 const char *sym = AT_ref (a)->die_id.die_symbol;
8459 int size;
8461 gcc_assert (sym);
8462 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8463 length, whereas in DWARF3 it's always sized as an
8464 offset. */
8465 if (dwarf_version == 2)
8466 size = DWARF2_ADDR_SIZE;
8467 else
8468 size = DWARF_OFFSET_SIZE;
8469 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8470 name);
8473 else
8475 gcc_assert (AT_ref (a)->die_offset);
8476 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8477 "%s", name);
8479 break;
8481 case dw_val_class_fde_ref:
8483 char l1[20];
8485 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8486 a->dw_attr_val.v.val_fde_index * 2);
8487 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8488 "%s", name);
8490 break;
8492 case dw_val_class_vms_delta:
8493 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8494 AT_vms_delta2 (a), AT_vms_delta1 (a),
8495 "%s", name);
8496 break;
8498 case dw_val_class_lbl_id:
8499 output_attr_index_or_value (a);
8500 break;
8502 case dw_val_class_lineptr:
8503 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8504 debug_line_section, "%s", name);
8505 break;
8507 case dw_val_class_macptr:
8508 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8509 debug_macinfo_section, "%s", name);
8510 break;
8512 case dw_val_class_str:
8513 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8514 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8515 a->dw_attr_val.v.val_str->label,
8516 debug_str_section,
8517 "%s: \"%s\"", name, AT_string (a));
8518 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8519 dw2_asm_output_data_uleb128 (AT_index (a),
8520 "%s: \"%s\"", name, AT_string (a));
8521 else
8522 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8523 break;
8525 case dw_val_class_file:
8527 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8529 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8530 a->dw_attr_val.v.val_file->filename);
8531 break;
8534 case dw_val_class_data8:
8536 int i;
8538 for (i = 0; i < 8; i++)
8539 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8540 i == 0 ? "%s" : NULL, name);
8541 break;
8544 case dw_val_class_high_pc:
8545 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8546 get_AT_low_pc (die), "DW_AT_high_pc");
8547 break;
8549 default:
8550 gcc_unreachable ();
8554 FOR_EACH_CHILD (die, c, output_die (c));
8556 /* Add null byte to terminate sibling list. */
8557 if (die->die_child != NULL)
8558 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8559 (unsigned long) die->die_offset);
8562 /* Output the compilation unit that appears at the beginning of the
8563 .debug_info section, and precedes the DIE descriptions. */
8565 static void
8566 output_compilation_unit_header (void)
8568 int ver = dwarf_version;
8570 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8571 dw2_asm_output_data (4, 0xffffffff,
8572 "Initial length escape value indicating 64-bit DWARF extension");
8573 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8574 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8575 "Length of Compilation Unit Info");
8576 dw2_asm_output_data (2, ver, "DWARF version number");
8577 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8578 debug_abbrev_section,
8579 "Offset Into Abbrev. Section");
8580 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8583 /* Output the compilation unit DIE and its children. */
8585 static void
8586 output_comp_unit (dw_die_ref die, int output_if_empty)
8588 const char *secname, *oldsym;
8589 char *tmp;
8590 htab_t extern_map;
8592 /* Unless we are outputting main CU, we may throw away empty ones. */
8593 if (!output_if_empty && die->die_child == NULL)
8594 return;
8596 /* Even if there are no children of this DIE, we must output the information
8597 about the compilation unit. Otherwise, on an empty translation unit, we
8598 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8599 will then complain when examining the file. First mark all the DIEs in
8600 this CU so we know which get local refs. */
8601 mark_dies (die);
8603 extern_map = optimize_external_refs (die);
8605 build_abbrev_table (die, extern_map);
8607 htab_delete (extern_map);
8609 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8610 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8611 calc_die_sizes (die);
8613 oldsym = die->die_id.die_symbol;
8614 if (oldsym)
8616 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8618 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8619 secname = tmp;
8620 die->die_id.die_symbol = NULL;
8621 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8623 else
8625 switch_to_section (debug_info_section);
8626 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8627 info_section_emitted = true;
8630 /* Output debugging information. */
8631 output_compilation_unit_header ();
8632 output_die (die);
8634 /* Leave the marks on the main CU, so we can check them in
8635 output_pubnames. */
8636 if (oldsym)
8638 unmark_dies (die);
8639 die->die_id.die_symbol = oldsym;
8643 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8644 and .debug_pubtypes. This is configured per-target, but can be
8645 overridden by the -gpubnames or -gno-pubnames options. */
8647 static inline bool
8648 want_pubnames (void)
8650 return (debug_generate_pub_sections != -1
8651 ? debug_generate_pub_sections
8652 : targetm.want_debug_pub_sections);
8655 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8657 static void
8658 add_AT_pubnames (dw_die_ref die)
8660 if (want_pubnames ())
8661 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8664 /* Helper function to generate top-level dies for skeleton debug_info and
8665 debug_types. */
8667 static void
8668 add_top_level_skeleton_die_attrs (dw_die_ref die)
8670 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8671 dw_attr_ref attr;
8673 add_comp_dir_attribute (die);
8674 add_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8675 /* The specification suggests that these attributes be inline to avoid
8676 having a .debug_str section. We know that they exist in the die because
8677 we just added them. */
8678 attr = get_AT (die, DW_AT_GNU_dwo_name);
8679 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8680 attr = get_AT (die, DW_AT_comp_dir);
8681 attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8683 add_AT_pubnames (die);
8684 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8687 /* Return the single type-unit die for skeleton type units. */
8689 static dw_die_ref
8690 get_skeleton_type_unit (void)
8692 /* For dwarf_split_debug_sections with use_type info, all type units in the
8693 skeleton sections have identical dies (but different headers). This
8694 single die will be output many times. */
8696 static dw_die_ref skeleton_type_unit = NULL;
8698 if (skeleton_type_unit == NULL)
8700 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8701 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8702 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8704 return skeleton_type_unit;
8707 /* Output skeleton debug sections that point to the dwo file. */
8709 static void
8710 output_skeleton_debug_sections (dw_die_ref comp_unit)
8712 /* These attributes will be found in the full debug_info section. */
8713 remove_AT (comp_unit, DW_AT_producer);
8714 remove_AT (comp_unit, DW_AT_language);
8716 /* Add attributes common to skeleton compile_units and type_units. */
8717 add_top_level_skeleton_die_attrs (comp_unit);
8719 switch_to_section (debug_skeleton_info_section);
8720 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8722 /* Produce the skeleton compilation-unit header. This one differs enough from
8723 a normal CU header that it's better not to call output_compilation_unit
8724 header. */
8725 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8726 dw2_asm_output_data (4, 0xffffffff,
8727 "Initial length escape value indicating 64-bit DWARF extension");
8729 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8730 DWARF_COMPILE_UNIT_HEADER_SIZE
8731 - DWARF_INITIAL_LENGTH_SIZE
8732 + size_of_die (comp_unit),
8733 "Length of Compilation Unit Info");
8734 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8735 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8736 debug_abbrev_section,
8737 "Offset Into Abbrev. Section");
8738 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8740 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8741 output_die (comp_unit);
8743 /* Build the skeleton debug_abbrev section. */
8744 switch_to_section (debug_skeleton_abbrev_section);
8745 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8747 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8748 if (use_debug_types)
8749 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8751 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8754 /* Output a comdat type unit DIE and its children. */
8756 static void
8757 output_comdat_type_unit (comdat_type_node *node)
8759 const char *secname;
8760 char *tmp;
8761 int i;
8762 #if defined (OBJECT_FORMAT_ELF)
8763 tree comdat_key;
8764 #endif
8765 htab_t extern_map;
8767 /* First mark all the DIEs in this CU so we know which get local refs. */
8768 mark_dies (node->root_die);
8770 extern_map = optimize_external_refs (node->root_die);
8772 build_abbrev_table (node->root_die, extern_map);
8774 htab_delete (extern_map);
8776 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8777 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8778 calc_die_sizes (node->root_die);
8780 #if defined (OBJECT_FORMAT_ELF)
8781 if (!dwarf_split_debug_info)
8782 secname = ".debug_types";
8783 else
8784 secname = ".debug_types.dwo";
8786 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8787 sprintf (tmp, "wt.");
8788 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8789 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8790 comdat_key = get_identifier (tmp);
8791 targetm.asm_out.named_section (secname,
8792 SECTION_DEBUG | SECTION_LINKONCE,
8793 comdat_key);
8794 #else
8795 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8796 sprintf (tmp, ".gnu.linkonce.wt.");
8797 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8798 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8799 secname = tmp;
8800 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8801 #endif
8803 /* Output debugging information. */
8804 output_compilation_unit_header ();
8805 output_signature (node->signature, "Type Signature");
8806 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8807 "Offset to Type DIE");
8808 output_die (node->root_die);
8810 unmark_dies (node->root_die);
8812 #if defined (OBJECT_FORMAT_ELF)
8813 if (dwarf_split_debug_info)
8815 /* Produce the skeleton type-unit header. */
8816 const char *secname = ".debug_types";
8818 targetm.asm_out.named_section (secname,
8819 SECTION_DEBUG | SECTION_LINKONCE,
8820 comdat_key);
8821 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8822 dw2_asm_output_data (4, 0xffffffff,
8823 "Initial length escape value indicating 64-bit DWARF extension");
8825 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8826 DWARF_COMPILE_UNIT_HEADER_SIZE
8827 - DWARF_INITIAL_LENGTH_SIZE
8828 + size_of_die (get_skeleton_type_unit ())
8829 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8830 "Length of Type Unit Info");
8831 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8832 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8833 debug_skeleton_abbrev_section_label,
8834 debug_abbrev_section,
8835 "Offset Into Abbrev. Section");
8836 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8837 output_signature (node->signature, "Type Signature");
8838 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8840 output_die (get_skeleton_type_unit ());
8842 #endif
8845 /* Return the DWARF2/3 pubname associated with a decl. */
8847 static const char *
8848 dwarf2_name (tree decl, int scope)
8850 if (DECL_NAMELESS (decl))
8851 return NULL;
8852 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8855 /* Add a new entry to .debug_pubnames if appropriate. */
8857 static void
8858 add_pubname_string (const char *str, dw_die_ref die)
8860 pubname_entry e;
8862 e.die = die;
8863 e.name = xstrdup (str);
8864 vec_safe_push (pubname_table, e);
8867 static void
8868 add_pubname (tree decl, dw_die_ref die)
8870 if (!want_pubnames ())
8871 return;
8873 /* Don't add items to the table when we expect that the consumer will have
8874 just read the enclosing die. For example, if the consumer is looking at a
8875 class_member, it will either be inside the class already, or will have just
8876 looked up the class to find the member. Either way, searching the class is
8877 faster than searching the index. */
8878 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8879 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8881 const char *name = dwarf2_name (decl, 1);
8883 if (name)
8884 add_pubname_string (name, die);
8888 /* Add an enumerator to the pubnames section. */
8890 static void
8891 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8893 pubname_entry e;
8895 gcc_assert (scope_name);
8896 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8897 e.die = die;
8898 vec_safe_push (pubname_table, e);
8901 /* Add a new entry to .debug_pubtypes if appropriate. */
8903 static void
8904 add_pubtype (tree decl, dw_die_ref die)
8906 pubname_entry e;
8908 if (!want_pubnames ())
8909 return;
8911 if ((TREE_PUBLIC (decl)
8912 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8913 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8915 tree scope = NULL;
8916 const char *scope_name = "";
8917 const char *sep = is_cxx () ? "::" : ".";
8918 const char *name;
8920 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8921 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8923 scope_name = lang_hooks.dwarf_name (scope, 1);
8924 if (scope_name != NULL && scope_name[0] != '\0')
8925 scope_name = concat (scope_name, sep, NULL);
8926 else
8927 scope_name = "";
8930 if (TYPE_P (decl))
8931 name = type_tag (decl);
8932 else
8933 name = lang_hooks.dwarf_name (decl, 1);
8935 /* If we don't have a name for the type, there's no point in adding
8936 it to the table. */
8937 if (name != NULL && name[0] != '\0')
8939 e.die = die;
8940 e.name = concat (scope_name, name, NULL);
8941 vec_safe_push (pubtype_table, e);
8944 /* Although it might be more consistent to add the pubinfo for the
8945 enumerators as their dies are created, they should only be added if the
8946 enum type meets the criteria above. So rather than re-check the parent
8947 enum type whenever an enumerator die is created, just output them all
8948 here. This isn't protected by the name conditional because anonymous
8949 enums don't have names. */
8950 if (die->die_tag == DW_TAG_enumeration_type)
8952 dw_die_ref c;
8954 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8959 /* Output the public names table used to speed up access to externally
8960 visible names; or the public types table used to find type definitions. */
8962 static void
8963 output_pubnames (vec<pubname_entry, va_gc> *names)
8965 unsigned i;
8966 unsigned long pubnames_length = size_of_pubnames (names);
8967 pubname_ref pub;
8969 if (!want_pubnames () || !info_section_emitted)
8970 return;
8971 if (names == pubname_table)
8972 switch_to_section (debug_pubnames_section);
8973 else
8974 switch_to_section (debug_pubtypes_section);
8975 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8976 dw2_asm_output_data (4, 0xffffffff,
8977 "Initial length escape value indicating 64-bit DWARF extension");
8978 if (names == pubname_table)
8979 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8980 "Length of Public Names Info");
8981 else
8982 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8983 "Length of Public Type Names Info");
8984 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
8985 dw2_asm_output_data (2, 2, "DWARF Version");
8986 if (dwarf_split_debug_info)
8987 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
8988 debug_skeleton_info_section,
8989 "Offset of Compilation Unit Info");
8990 else
8991 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8992 debug_info_section,
8993 "Offset of Compilation Unit Info");
8994 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8995 "Compilation Unit Length");
8997 FOR_EACH_VEC_ELT (*names, i, pub)
8999 /* Enumerator names are part of the pubname table, but the parent
9000 DW_TAG_enumeration_type die may have been pruned. Don't output
9001 them if that is the case. */
9002 if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
9003 continue;
9005 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9006 if (names == pubname_table)
9007 gcc_assert (pub->die->die_mark);
9009 if (names != pubtype_table
9010 || pub->die->die_offset != 0
9011 || !flag_eliminate_unused_debug_types)
9013 dw_offset die_offset = pub->die->die_offset;
9015 /* If we're putting types in their own .debug_types sections,
9016 the .debug_pubtypes table will still point to the compile
9017 unit (not the type unit), so we want to use the offset of
9018 the skeleton DIE (if there is one). */
9019 if (pub->die->comdat_type_p && names == pubtype_table)
9021 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9023 if (type_node != NULL)
9024 die_offset = (type_node->skeleton_die != NULL
9025 ? type_node->skeleton_die->die_offset
9026 : 0);
9029 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9031 dw2_asm_output_nstring (pub->name, -1, "external name");
9035 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9038 /* Output the information that goes into the .debug_aranges table.
9039 Namely, define the beginning and ending address range of the
9040 text section generated for this compilation unit. */
9042 static void
9043 output_aranges (unsigned long aranges_length)
9045 unsigned i;
9047 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9048 dw2_asm_output_data (4, 0xffffffff,
9049 "Initial length escape value indicating 64-bit DWARF extension");
9050 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9051 "Length of Address Ranges Info");
9052 /* Version number for aranges is still 2, even in DWARF3. */
9053 dw2_asm_output_data (2, 2, "DWARF Version");
9054 if (dwarf_split_debug_info)
9055 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9056 debug_skeleton_info_section,
9057 "Offset of Compilation Unit Info");
9058 else
9059 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9060 debug_info_section,
9061 "Offset of Compilation Unit Info");
9062 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9063 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9065 /* We need to align to twice the pointer size here. */
9066 if (DWARF_ARANGES_PAD_SIZE)
9068 /* Pad using a 2 byte words so that padding is correct for any
9069 pointer size. */
9070 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9071 2 * DWARF2_ADDR_SIZE);
9072 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9073 dw2_asm_output_data (2, 0, NULL);
9076 /* It is necessary not to output these entries if the sections were
9077 not used; if the sections were not used, the length will be 0 and
9078 the address may end up as 0 if the section is discarded by ld
9079 --gc-sections, leaving an invalid (0, 0) entry that can be
9080 confused with the terminator. */
9081 if (text_section_used)
9083 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9084 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9085 text_section_label, "Length");
9087 if (cold_text_section_used)
9089 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9090 "Address");
9091 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9092 cold_text_section_label, "Length");
9095 if (have_multiple_function_sections)
9097 unsigned fde_idx;
9098 dw_fde_ref fde;
9100 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9102 if (DECL_IGNORED_P (fde->decl))
9103 continue;
9104 if (!fde->in_std_section)
9106 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9107 "Address");
9108 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9109 fde->dw_fde_begin, "Length");
9111 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9113 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9114 "Address");
9115 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9116 fde->dw_fde_second_begin, "Length");
9121 /* Output the terminator words. */
9122 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9123 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9126 /* Add a new entry to .debug_ranges. Return the offset at which it
9127 was placed. */
9129 static unsigned int
9130 add_ranges_num (int num)
9132 unsigned int in_use = ranges_table_in_use;
9134 if (in_use == ranges_table_allocated)
9136 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9137 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9138 ranges_table_allocated);
9139 memset (ranges_table + ranges_table_in_use, 0,
9140 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9143 ranges_table[in_use].num = num;
9144 ranges_table_in_use = in_use + 1;
9146 return in_use * 2 * DWARF2_ADDR_SIZE;
9149 /* Add a new entry to .debug_ranges corresponding to a block, or a
9150 range terminator if BLOCK is NULL. */
9152 static unsigned int
9153 add_ranges (const_tree block)
9155 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9158 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9159 When using dwarf_split_debug_info, address attributes in dies destined
9160 for the final executable should be direct references--setting the
9161 parameter force_direct ensures this behavior. */
9163 static void
9164 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9165 bool *added, bool force_direct)
9167 unsigned int in_use = ranges_by_label_in_use;
9168 unsigned int offset;
9170 if (in_use == ranges_by_label_allocated)
9172 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9173 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9174 ranges_by_label,
9175 ranges_by_label_allocated);
9176 memset (ranges_by_label + ranges_by_label_in_use, 0,
9177 RANGES_TABLE_INCREMENT
9178 * sizeof (struct dw_ranges_by_label_struct));
9181 ranges_by_label[in_use].begin = begin;
9182 ranges_by_label[in_use].end = end;
9183 ranges_by_label_in_use = in_use + 1;
9185 offset = add_ranges_num (-(int)in_use - 1);
9186 if (!*added)
9188 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9189 *added = true;
9193 static void
9194 output_ranges (void)
9196 unsigned i;
9197 static const char *const start_fmt = "Offset %#x";
9198 const char *fmt = start_fmt;
9200 for (i = 0; i < ranges_table_in_use; i++)
9202 int block_num = ranges_table[i].num;
9204 if (block_num > 0)
9206 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9207 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9209 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9210 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9212 /* If all code is in the text section, then the compilation
9213 unit base address defaults to DW_AT_low_pc, which is the
9214 base of the text section. */
9215 if (!have_multiple_function_sections)
9217 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9218 text_section_label,
9219 fmt, i * 2 * DWARF2_ADDR_SIZE);
9220 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9221 text_section_label, NULL);
9224 /* Otherwise, the compilation unit base address is zero,
9225 which allows us to use absolute addresses, and not worry
9226 about whether the target supports cross-section
9227 arithmetic. */
9228 else
9230 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9231 fmt, i * 2 * DWARF2_ADDR_SIZE);
9232 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9235 fmt = NULL;
9238 /* Negative block_num stands for an index into ranges_by_label. */
9239 else if (block_num < 0)
9241 int lab_idx = - block_num - 1;
9243 if (!have_multiple_function_sections)
9245 gcc_unreachable ();
9246 #if 0
9247 /* If we ever use add_ranges_by_labels () for a single
9248 function section, all we have to do is to take out
9249 the #if 0 above. */
9250 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9251 ranges_by_label[lab_idx].begin,
9252 text_section_label,
9253 fmt, i * 2 * DWARF2_ADDR_SIZE);
9254 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9255 ranges_by_label[lab_idx].end,
9256 text_section_label, NULL);
9257 #endif
9259 else
9261 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9262 ranges_by_label[lab_idx].begin,
9263 fmt, i * 2 * DWARF2_ADDR_SIZE);
9264 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9265 ranges_by_label[lab_idx].end,
9266 NULL);
9269 else
9271 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9272 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9273 fmt = start_fmt;
9278 /* Data structure containing information about input files. */
9279 struct file_info
9281 const char *path; /* Complete file name. */
9282 const char *fname; /* File name part. */
9283 int length; /* Length of entire string. */
9284 struct dwarf_file_data * file_idx; /* Index in input file table. */
9285 int dir_idx; /* Index in directory table. */
9288 /* Data structure containing information about directories with source
9289 files. */
9290 struct dir_info
9292 const char *path; /* Path including directory name. */
9293 int length; /* Path length. */
9294 int prefix; /* Index of directory entry which is a prefix. */
9295 int count; /* Number of files in this directory. */
9296 int dir_idx; /* Index of directory used as base. */
9299 /* Callback function for file_info comparison. We sort by looking at
9300 the directories in the path. */
9302 static int
9303 file_info_cmp (const void *p1, const void *p2)
9305 const struct file_info *const s1 = (const struct file_info *) p1;
9306 const struct file_info *const s2 = (const struct file_info *) p2;
9307 const unsigned char *cp1;
9308 const unsigned char *cp2;
9310 /* Take care of file names without directories. We need to make sure that
9311 we return consistent values to qsort since some will get confused if
9312 we return the same value when identical operands are passed in opposite
9313 orders. So if neither has a directory, return 0 and otherwise return
9314 1 or -1 depending on which one has the directory. */
9315 if ((s1->path == s1->fname || s2->path == s2->fname))
9316 return (s2->path == s2->fname) - (s1->path == s1->fname);
9318 cp1 = (const unsigned char *) s1->path;
9319 cp2 = (const unsigned char *) s2->path;
9321 while (1)
9323 ++cp1;
9324 ++cp2;
9325 /* Reached the end of the first path? If so, handle like above. */
9326 if ((cp1 == (const unsigned char *) s1->fname)
9327 || (cp2 == (const unsigned char *) s2->fname))
9328 return ((cp2 == (const unsigned char *) s2->fname)
9329 - (cp1 == (const unsigned char *) s1->fname));
9331 /* Character of current path component the same? */
9332 else if (*cp1 != *cp2)
9333 return *cp1 - *cp2;
9337 struct file_name_acquire_data
9339 struct file_info *files;
9340 int used_files;
9341 int max_files;
9344 /* Traversal function for the hash table. */
9346 static int
9347 file_name_acquire (void ** slot, void *data)
9349 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9350 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9351 struct file_info *fi;
9352 const char *f;
9354 gcc_assert (fnad->max_files >= d->emitted_number);
9356 if (! d->emitted_number)
9357 return 1;
9359 gcc_assert (fnad->max_files != fnad->used_files);
9361 fi = fnad->files + fnad->used_files++;
9363 /* Skip all leading "./". */
9364 f = d->filename;
9365 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9366 f += 2;
9368 /* Create a new array entry. */
9369 fi->path = f;
9370 fi->length = strlen (f);
9371 fi->file_idx = d;
9373 /* Search for the file name part. */
9374 f = strrchr (f, DIR_SEPARATOR);
9375 #if defined (DIR_SEPARATOR_2)
9377 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9379 if (g != NULL)
9381 if (f == NULL || f < g)
9382 f = g;
9385 #endif
9387 fi->fname = f == NULL ? fi->path : f + 1;
9388 return 1;
9391 /* Output the directory table and the file name table. We try to minimize
9392 the total amount of memory needed. A heuristic is used to avoid large
9393 slowdowns with many input files. */
9395 static void
9396 output_file_names (void)
9398 struct file_name_acquire_data fnad;
9399 int numfiles;
9400 struct file_info *files;
9401 struct dir_info *dirs;
9402 int *saved;
9403 int *savehere;
9404 int *backmap;
9405 int ndirs;
9406 int idx_offset;
9407 int i;
9409 if (!last_emitted_file)
9411 dw2_asm_output_data (1, 0, "End directory table");
9412 dw2_asm_output_data (1, 0, "End file name table");
9413 return;
9416 numfiles = last_emitted_file->emitted_number;
9418 /* Allocate the various arrays we need. */
9419 files = XALLOCAVEC (struct file_info, numfiles);
9420 dirs = XALLOCAVEC (struct dir_info, numfiles);
9422 fnad.files = files;
9423 fnad.used_files = 0;
9424 fnad.max_files = numfiles;
9425 htab_traverse (file_table, file_name_acquire, &fnad);
9426 gcc_assert (fnad.used_files == fnad.max_files);
9428 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9430 /* Find all the different directories used. */
9431 dirs[0].path = files[0].path;
9432 dirs[0].length = files[0].fname - files[0].path;
9433 dirs[0].prefix = -1;
9434 dirs[0].count = 1;
9435 dirs[0].dir_idx = 0;
9436 files[0].dir_idx = 0;
9437 ndirs = 1;
9439 for (i = 1; i < numfiles; i++)
9440 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9441 && memcmp (dirs[ndirs - 1].path, files[i].path,
9442 dirs[ndirs - 1].length) == 0)
9444 /* Same directory as last entry. */
9445 files[i].dir_idx = ndirs - 1;
9446 ++dirs[ndirs - 1].count;
9448 else
9450 int j;
9452 /* This is a new directory. */
9453 dirs[ndirs].path = files[i].path;
9454 dirs[ndirs].length = files[i].fname - files[i].path;
9455 dirs[ndirs].count = 1;
9456 dirs[ndirs].dir_idx = ndirs;
9457 files[i].dir_idx = ndirs;
9459 /* Search for a prefix. */
9460 dirs[ndirs].prefix = -1;
9461 for (j = 0; j < ndirs; j++)
9462 if (dirs[j].length < dirs[ndirs].length
9463 && dirs[j].length > 1
9464 && (dirs[ndirs].prefix == -1
9465 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9466 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9467 dirs[ndirs].prefix = j;
9469 ++ndirs;
9472 /* Now to the actual work. We have to find a subset of the directories which
9473 allow expressing the file name using references to the directory table
9474 with the least amount of characters. We do not do an exhaustive search
9475 where we would have to check out every combination of every single
9476 possible prefix. Instead we use a heuristic which provides nearly optimal
9477 results in most cases and never is much off. */
9478 saved = XALLOCAVEC (int, ndirs);
9479 savehere = XALLOCAVEC (int, ndirs);
9481 memset (saved, '\0', ndirs * sizeof (saved[0]));
9482 for (i = 0; i < ndirs; i++)
9484 int j;
9485 int total;
9487 /* We can always save some space for the current directory. But this
9488 does not mean it will be enough to justify adding the directory. */
9489 savehere[i] = dirs[i].length;
9490 total = (savehere[i] - saved[i]) * dirs[i].count;
9492 for (j = i + 1; j < ndirs; j++)
9494 savehere[j] = 0;
9495 if (saved[j] < dirs[i].length)
9497 /* Determine whether the dirs[i] path is a prefix of the
9498 dirs[j] path. */
9499 int k;
9501 k = dirs[j].prefix;
9502 while (k != -1 && k != (int) i)
9503 k = dirs[k].prefix;
9505 if (k == (int) i)
9507 /* Yes it is. We can possibly save some memory by
9508 writing the filenames in dirs[j] relative to
9509 dirs[i]. */
9510 savehere[j] = dirs[i].length;
9511 total += (savehere[j] - saved[j]) * dirs[j].count;
9516 /* Check whether we can save enough to justify adding the dirs[i]
9517 directory. */
9518 if (total > dirs[i].length + 1)
9520 /* It's worthwhile adding. */
9521 for (j = i; j < ndirs; j++)
9522 if (savehere[j] > 0)
9524 /* Remember how much we saved for this directory so far. */
9525 saved[j] = savehere[j];
9527 /* Remember the prefix directory. */
9528 dirs[j].dir_idx = i;
9533 /* Emit the directory name table. */
9534 idx_offset = dirs[0].length > 0 ? 1 : 0;
9535 for (i = 1 - idx_offset; i < ndirs; i++)
9536 dw2_asm_output_nstring (dirs[i].path,
9537 dirs[i].length
9538 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9539 "Directory Entry: %#x", i + idx_offset);
9541 dw2_asm_output_data (1, 0, "End directory table");
9543 /* We have to emit them in the order of emitted_number since that's
9544 used in the debug info generation. To do this efficiently we
9545 generate a back-mapping of the indices first. */
9546 backmap = XALLOCAVEC (int, numfiles);
9547 for (i = 0; i < numfiles; i++)
9548 backmap[files[i].file_idx->emitted_number - 1] = i;
9550 /* Now write all the file names. */
9551 for (i = 0; i < numfiles; i++)
9553 int file_idx = backmap[i];
9554 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9556 #ifdef VMS_DEBUGGING_INFO
9557 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9559 /* Setting these fields can lead to debugger miscomparisons,
9560 but VMS Debug requires them to be set correctly. */
9562 int ver;
9563 long long cdt;
9564 long siz;
9565 int maxfilelen = strlen (files[file_idx].path)
9566 + dirs[dir_idx].length
9567 + MAX_VMS_VERSION_LEN + 1;
9568 char *filebuf = XALLOCAVEC (char, maxfilelen);
9570 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9571 snprintf (filebuf, maxfilelen, "%s;%d",
9572 files[file_idx].path + dirs[dir_idx].length, ver);
9574 dw2_asm_output_nstring
9575 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9577 /* Include directory index. */
9578 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9580 /* Modification time. */
9581 dw2_asm_output_data_uleb128
9582 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9583 ? cdt : 0,
9584 NULL);
9586 /* File length in bytes. */
9587 dw2_asm_output_data_uleb128
9588 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9589 ? siz : 0,
9590 NULL);
9591 #else
9592 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9593 "File Entry: %#x", (unsigned) i + 1);
9595 /* Include directory index. */
9596 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9598 /* Modification time. */
9599 dw2_asm_output_data_uleb128 (0, NULL);
9601 /* File length in bytes. */
9602 dw2_asm_output_data_uleb128 (0, NULL);
9603 #endif /* VMS_DEBUGGING_INFO */
9606 dw2_asm_output_data (1, 0, "End file name table");
9610 /* Output one line number table into the .debug_line section. */
9612 static void
9613 output_one_line_info_table (dw_line_info_table *table)
9615 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9616 unsigned int current_line = 1;
9617 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9618 dw_line_info_entry *ent;
9619 size_t i;
9621 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9623 switch (ent->opcode)
9625 case LI_set_address:
9626 /* ??? Unfortunately, we have little choice here currently, and
9627 must always use the most general form. GCC does not know the
9628 address delta itself, so we can't use DW_LNS_advance_pc. Many
9629 ports do have length attributes which will give an upper bound
9630 on the address range. We could perhaps use length attributes
9631 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9632 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9634 /* This can handle any delta. This takes
9635 4+DWARF2_ADDR_SIZE bytes. */
9636 dw2_asm_output_data (1, 0, "set address %s", line_label);
9637 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9638 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9639 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9640 break;
9642 case LI_set_line:
9643 if (ent->val == current_line)
9645 /* We still need to start a new row, so output a copy insn. */
9646 dw2_asm_output_data (1, DW_LNS_copy,
9647 "copy line %u", current_line);
9649 else
9651 int line_offset = ent->val - current_line;
9652 int line_delta = line_offset - DWARF_LINE_BASE;
9654 current_line = ent->val;
9655 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9657 /* This can handle deltas from -10 to 234, using the current
9658 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9659 This takes 1 byte. */
9660 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9661 "line %u", current_line);
9663 else
9665 /* This can handle any delta. This takes at least 4 bytes,
9666 depending on the value being encoded. */
9667 dw2_asm_output_data (1, DW_LNS_advance_line,
9668 "advance to line %u", current_line);
9669 dw2_asm_output_data_sleb128 (line_offset, NULL);
9670 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9673 break;
9675 case LI_set_file:
9676 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9677 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9678 break;
9680 case LI_set_column:
9681 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9682 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9683 break;
9685 case LI_negate_stmt:
9686 current_is_stmt = !current_is_stmt;
9687 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9688 "is_stmt %d", current_is_stmt);
9689 break;
9691 case LI_set_prologue_end:
9692 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9693 "set prologue end");
9694 break;
9696 case LI_set_epilogue_begin:
9697 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9698 "set epilogue begin");
9699 break;
9701 case LI_set_discriminator:
9702 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9703 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9704 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9705 dw2_asm_output_data_uleb128 (ent->val, NULL);
9706 break;
9710 /* Emit debug info for the address of the end of the table. */
9711 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9712 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9713 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9714 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9716 dw2_asm_output_data (1, 0, "end sequence");
9717 dw2_asm_output_data_uleb128 (1, NULL);
9718 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9721 /* Output the source line number correspondence information. This
9722 information goes into the .debug_line section. */
9724 static void
9725 output_line_info (bool prologue_only)
9727 char l1[20], l2[20], p1[20], p2[20];
9728 int ver = dwarf_version;
9729 bool saw_one = false;
9730 int opc;
9732 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9733 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9734 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9735 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9737 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9738 dw2_asm_output_data (4, 0xffffffff,
9739 "Initial length escape value indicating 64-bit DWARF extension");
9740 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9741 "Length of Source Line Info");
9742 ASM_OUTPUT_LABEL (asm_out_file, l1);
9744 dw2_asm_output_data (2, ver, "DWARF Version");
9745 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9746 ASM_OUTPUT_LABEL (asm_out_file, p1);
9748 /* Define the architecture-dependent minimum instruction length (in bytes).
9749 In this implementation of DWARF, this field is used for information
9750 purposes only. Since GCC generates assembly language, we have no
9751 a priori knowledge of how many instruction bytes are generated for each
9752 source line, and therefore can use only the DW_LNE_set_address and
9753 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9754 this as '1', which is "correct enough" for all architectures,
9755 and don't let the target override. */
9756 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9758 if (ver >= 4)
9759 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9760 "Maximum Operations Per Instruction");
9761 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9762 "Default is_stmt_start flag");
9763 dw2_asm_output_data (1, DWARF_LINE_BASE,
9764 "Line Base Value (Special Opcodes)");
9765 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9766 "Line Range Value (Special Opcodes)");
9767 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9768 "Special Opcode Base");
9770 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9772 int n_op_args;
9773 switch (opc)
9775 case DW_LNS_advance_pc:
9776 case DW_LNS_advance_line:
9777 case DW_LNS_set_file:
9778 case DW_LNS_set_column:
9779 case DW_LNS_fixed_advance_pc:
9780 case DW_LNS_set_isa:
9781 n_op_args = 1;
9782 break;
9783 default:
9784 n_op_args = 0;
9785 break;
9788 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9789 opc, n_op_args);
9792 /* Write out the information about the files we use. */
9793 output_file_names ();
9794 ASM_OUTPUT_LABEL (asm_out_file, p2);
9795 if (prologue_only)
9797 /* Output the marker for the end of the line number info. */
9798 ASM_OUTPUT_LABEL (asm_out_file, l2);
9799 return;
9802 if (separate_line_info)
9804 dw_line_info_table *table;
9805 size_t i;
9807 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9808 if (table->in_use)
9810 output_one_line_info_table (table);
9811 saw_one = true;
9814 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9816 output_one_line_info_table (cold_text_section_line_info);
9817 saw_one = true;
9820 /* ??? Some Darwin linkers crash on a .debug_line section with no
9821 sequences. Further, merely a DW_LNE_end_sequence entry is not
9822 sufficient -- the address column must also be initialized.
9823 Make sure to output at least one set_address/end_sequence pair,
9824 choosing .text since that section is always present. */
9825 if (text_section_line_info->in_use || !saw_one)
9826 output_one_line_info_table (text_section_line_info);
9828 /* Output the marker for the end of the line number info. */
9829 ASM_OUTPUT_LABEL (asm_out_file, l2);
9832 /* Given a pointer to a tree node for some base type, return a pointer to
9833 a DIE that describes the given type.
9835 This routine must only be called for GCC type nodes that correspond to
9836 Dwarf base (fundamental) types. */
9838 static dw_die_ref
9839 base_type_die (tree type)
9841 dw_die_ref base_type_result;
9842 enum dwarf_type encoding;
9844 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9845 return 0;
9847 /* If this is a subtype that should not be emitted as a subrange type,
9848 use the base type. See subrange_type_for_debug_p. */
9849 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9850 type = TREE_TYPE (type);
9852 switch (TREE_CODE (type))
9854 case INTEGER_TYPE:
9855 if ((dwarf_version >= 4 || !dwarf_strict)
9856 && TYPE_NAME (type)
9857 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9858 && DECL_IS_BUILTIN (TYPE_NAME (type))
9859 && DECL_NAME (TYPE_NAME (type)))
9861 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9862 if (strcmp (name, "char16_t") == 0
9863 || strcmp (name, "char32_t") == 0)
9865 encoding = DW_ATE_UTF;
9866 break;
9869 if (TYPE_STRING_FLAG (type))
9871 if (TYPE_UNSIGNED (type))
9872 encoding = DW_ATE_unsigned_char;
9873 else
9874 encoding = DW_ATE_signed_char;
9876 else if (TYPE_UNSIGNED (type))
9877 encoding = DW_ATE_unsigned;
9878 else
9879 encoding = DW_ATE_signed;
9880 break;
9882 case REAL_TYPE:
9883 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9885 if (dwarf_version >= 3 || !dwarf_strict)
9886 encoding = DW_ATE_decimal_float;
9887 else
9888 encoding = DW_ATE_lo_user;
9890 else
9891 encoding = DW_ATE_float;
9892 break;
9894 case FIXED_POINT_TYPE:
9895 if (!(dwarf_version >= 3 || !dwarf_strict))
9896 encoding = DW_ATE_lo_user;
9897 else if (TYPE_UNSIGNED (type))
9898 encoding = DW_ATE_unsigned_fixed;
9899 else
9900 encoding = DW_ATE_signed_fixed;
9901 break;
9903 /* Dwarf2 doesn't know anything about complex ints, so use
9904 a user defined type for it. */
9905 case COMPLEX_TYPE:
9906 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9907 encoding = DW_ATE_complex_float;
9908 else
9909 encoding = DW_ATE_lo_user;
9910 break;
9912 case BOOLEAN_TYPE:
9913 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9914 encoding = DW_ATE_boolean;
9915 break;
9917 default:
9918 /* No other TREE_CODEs are Dwarf fundamental types. */
9919 gcc_unreachable ();
9922 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9924 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9925 int_size_in_bytes (type));
9926 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9927 add_pubtype (type, base_type_result);
9929 return base_type_result;
9932 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9933 given input type is a Dwarf "fundamental" type. Otherwise return null. */
9935 static inline int
9936 is_base_type (tree type)
9938 switch (TREE_CODE (type))
9940 case ERROR_MARK:
9941 case VOID_TYPE:
9942 case INTEGER_TYPE:
9943 case REAL_TYPE:
9944 case FIXED_POINT_TYPE:
9945 case COMPLEX_TYPE:
9946 case BOOLEAN_TYPE:
9947 return 1;
9949 case ARRAY_TYPE:
9950 case RECORD_TYPE:
9951 case UNION_TYPE:
9952 case QUAL_UNION_TYPE:
9953 case ENUMERAL_TYPE:
9954 case FUNCTION_TYPE:
9955 case METHOD_TYPE:
9956 case POINTER_TYPE:
9957 case REFERENCE_TYPE:
9958 case NULLPTR_TYPE:
9959 case OFFSET_TYPE:
9960 case LANG_TYPE:
9961 case VECTOR_TYPE:
9962 return 0;
9964 default:
9965 gcc_unreachable ();
9968 return 0;
9971 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9972 node, return the size in bits for the type if it is a constant, or else
9973 return the alignment for the type if the type's size is not constant, or
9974 else return BITS_PER_WORD if the type actually turns out to be an
9975 ERROR_MARK node. */
9977 static inline unsigned HOST_WIDE_INT
9978 simple_type_size_in_bits (const_tree type)
9980 if (TREE_CODE (type) == ERROR_MARK)
9981 return BITS_PER_WORD;
9982 else if (TYPE_SIZE (type) == NULL_TREE)
9983 return 0;
9984 else if (host_integerp (TYPE_SIZE (type), 1))
9985 return tree_low_cst (TYPE_SIZE (type), 1);
9986 else
9987 return TYPE_ALIGN (type);
9990 /* Similarly, but return a double_int instead of UHWI. */
9992 static inline double_int
9993 double_int_type_size_in_bits (const_tree type)
9995 if (TREE_CODE (type) == ERROR_MARK)
9996 return double_int::from_uhwi (BITS_PER_WORD);
9997 else if (TYPE_SIZE (type) == NULL_TREE)
9998 return double_int_zero;
9999 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10000 return tree_to_double_int (TYPE_SIZE (type));
10001 else
10002 return double_int::from_uhwi (TYPE_ALIGN (type));
10005 /* Given a pointer to a tree node for a subrange type, return a pointer
10006 to a DIE that describes the given type. */
10008 static dw_die_ref
10009 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10011 dw_die_ref subrange_die;
10012 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10014 if (context_die == NULL)
10015 context_die = comp_unit_die ();
10017 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10019 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10021 /* The size of the subrange type and its base type do not match,
10022 so we need to generate a size attribute for the subrange type. */
10023 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10026 if (low)
10027 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10028 if (high)
10029 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10031 return subrange_die;
10034 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10035 entry that chains various modifiers in front of the given type. */
10037 static dw_die_ref
10038 modified_type_die (tree type, int type_quals,
10039 dw_die_ref context_die)
10041 const int is_const_type = ((type_quals & TYPE_QUAL_CONST) != 0);
10042 const int is_volatile_type = ((type_quals & TYPE_QUAL_VOLATILE) != 0);
10043 enum tree_code code = TREE_CODE (type);
10044 dw_die_ref mod_type_die;
10045 dw_die_ref sub_die = NULL;
10046 tree item_type = NULL;
10047 tree qualified_type;
10048 tree name, low, high;
10049 dw_die_ref mod_scope;
10051 if (code == ERROR_MARK)
10052 return NULL;
10054 /* See if we already have the appropriately qualified variant of
10055 this type. */
10056 qualified_type
10057 = get_qualified_type (type, type_quals);
10059 if (qualified_type == sizetype
10060 && TYPE_NAME (qualified_type)
10061 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10063 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10065 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10066 && TYPE_PRECISION (t)
10067 == TYPE_PRECISION (qualified_type)
10068 && TYPE_UNSIGNED (t)
10069 == TYPE_UNSIGNED (qualified_type));
10070 qualified_type = t;
10073 /* If we do, then we can just use its DIE, if it exists. */
10074 if (qualified_type)
10076 mod_type_die = lookup_type_die (qualified_type);
10077 if (mod_type_die)
10078 return mod_type_die;
10081 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10083 /* Handle C typedef types. */
10084 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10085 && !DECL_ARTIFICIAL (name))
10087 tree dtype = TREE_TYPE (name);
10089 if (qualified_type == dtype)
10091 /* For a named type, use the typedef. */
10092 gen_type_die (qualified_type, context_die);
10093 return lookup_type_die (qualified_type);
10095 else if (type_quals == TYPE_QUALS (dtype))
10096 /* cv-unqualified version of named type. Just use the unnamed
10097 type to which it refers. */
10098 return modified_type_die (DECL_ORIGINAL_TYPE (name), type_quals, context_die);
10099 /* Else cv-qualified version of named type; fall through. */
10102 mod_scope = scope_die_for (type, context_die);
10104 if ((type_quals & TYPE_QUAL_CONST)
10105 /* If both is_const_type and is_volatile_type, prefer the path
10106 which leads to a qualified type. */
10107 && (!is_volatile_type
10108 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10109 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10111 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10112 sub_die = modified_type_die (type, type_quals & ~TYPE_QUAL_CONST, context_die);
10114 else if (type_quals & TYPE_QUAL_VOLATILE)
10116 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10117 sub_die = modified_type_die (type, type_quals & ~TYPE_QUAL_VOLATILE, context_die);
10119 else if (use_upc_dwarf2_extensions
10120 && (type_quals & TYPE_QUAL_SHARED))
10122 HOST_WIDE_INT block_factor = 1;
10124 /* Inside the compiler,
10125 "shared int x;" TYPE_BLOCK_FACTOR is null.
10126 "shared [] int *p;" TYPE_BLOCK_FACTOR is zero.
10127 "shared [10] int x[50];" TYPE_BLOCK_FACTOR is 10 * bitsize(int)
10128 The DWARF2 encoding is as follows:
10129 "shared int x;" DW_AT_count: 1
10130 "shared [] int *p;" <no DW_AT_count attribute>
10131 "shared [10] int x[50];" DW_AT_count: 10
10132 The logic below handles thse various contingencies. */
10134 mod_type_die = new_die (DW_TAG_upc_shared_type,
10135 comp_unit_die (), type);
10137 if (TYPE_HAS_BLOCK_FACTOR (type))
10138 block_factor = TREE_INT_CST_LOW (TYPE_BLOCK_FACTOR (type));
10140 if (block_factor != 0)
10141 add_AT_unsigned (mod_type_die, DW_AT_count, block_factor);
10143 sub_die = modified_type_die (type,
10144 type_quals & ~TYPE_QUAL_SHARED,
10145 context_die);
10147 else if (use_upc_dwarf2_extensions && type_quals & TYPE_QUAL_STRICT)
10149 mod_type_die = new_die (DW_TAG_upc_strict_type,
10150 comp_unit_die (), type);
10151 sub_die = modified_type_die (type,
10152 type_quals & ~TYPE_QUAL_STRICT,
10153 context_die);
10155 else if (use_upc_dwarf2_extensions && type_quals & TYPE_QUAL_RELAXED)
10157 mod_type_die = new_die (DW_TAG_upc_relaxed_type,
10158 comp_unit_die (), type);
10159 sub_die = modified_type_die (type,
10160 type_quals & ~TYPE_QUAL_RELAXED,
10161 context_die);
10163 else if (code == POINTER_TYPE)
10165 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10166 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10167 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10168 item_type = TREE_TYPE (type);
10169 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10170 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10171 TYPE_ADDR_SPACE (item_type));
10173 else if (code == REFERENCE_TYPE)
10175 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10176 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10177 type);
10178 else
10179 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10180 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10181 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10182 item_type = TREE_TYPE (type);
10183 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10184 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10185 TYPE_ADDR_SPACE (item_type));
10187 else if (code == INTEGER_TYPE
10188 && TREE_TYPE (type) != NULL_TREE
10189 && subrange_type_for_debug_p (type, &low, &high))
10191 mod_type_die = subrange_type_die (type, low, high, context_die);
10192 item_type = TREE_TYPE (type);
10194 else if (is_base_type (type))
10195 mod_type_die = base_type_die (type);
10196 else
10198 gen_type_die (type, context_die);
10200 /* We have to get the type_main_variant here (and pass that to the
10201 `lookup_type_die' routine) because the ..._TYPE node we have
10202 might simply be a *copy* of some original type node (where the
10203 copy was created to help us keep track of typedef names) and
10204 that copy might have a different TYPE_UID from the original
10205 ..._TYPE node. */
10206 if (TREE_CODE (type) != VECTOR_TYPE)
10207 return lookup_type_die (type_main_variant (type));
10208 else
10209 /* Vectors have the debugging information in the type,
10210 not the main variant. */
10211 return lookup_type_die (type);
10214 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10215 don't output a DW_TAG_typedef, since there isn't one in the
10216 user's program; just attach a DW_AT_name to the type.
10217 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10218 if the base type already has the same name. */
10219 if (name
10220 && ((TREE_CODE (name) != TYPE_DECL
10221 && (qualified_type == TYPE_MAIN_VARIANT (type)
10222 || (!is_const_type && !is_volatile_type)))
10223 || (TREE_CODE (name) == TYPE_DECL
10224 && TREE_TYPE (name) == qualified_type
10225 && DECL_NAME (name))))
10227 if (TREE_CODE (name) == TYPE_DECL)
10228 /* Could just call add_name_and_src_coords_attributes here,
10229 but since this is a builtin type it doesn't have any
10230 useful source coordinates anyway. */
10231 name = DECL_NAME (name);
10232 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10234 /* This probably indicates a bug. */
10235 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10237 name = TYPE_NAME (type);
10238 if (name
10239 && TREE_CODE (name) == TYPE_DECL)
10240 name = DECL_NAME (name);
10241 add_name_attribute (mod_type_die,
10242 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10245 if (qualified_type)
10246 equate_type_number_to_die (qualified_type, mod_type_die);
10248 if (item_type)
10249 /* We must do this after the equate_type_number_to_die call, in case
10250 this is a recursive type. This ensures that the modified_type_die
10251 recursion will terminate even if the type is recursive. Recursive
10252 types are possible in Ada. */
10253 sub_die = modified_type_die (item_type, TYPE_QUALS (item_type), context_die);
10255 if (sub_die != NULL)
10256 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10258 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10259 if (TYPE_ARTIFICIAL (type))
10260 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10262 return mod_type_die;
10265 /* Generate DIEs for the generic parameters of T.
10266 T must be either a generic type or a generic function.
10267 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10269 static void
10270 gen_generic_params_dies (tree t)
10272 tree parms, args;
10273 int parms_num, i;
10274 dw_die_ref die = NULL;
10276 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10277 return;
10279 if (TYPE_P (t))
10280 die = lookup_type_die (t);
10281 else if (DECL_P (t))
10282 die = lookup_decl_die (t);
10284 gcc_assert (die);
10286 parms = lang_hooks.get_innermost_generic_parms (t);
10287 if (!parms)
10288 /* T has no generic parameter. It means T is neither a generic type
10289 or function. End of story. */
10290 return;
10292 parms_num = TREE_VEC_LENGTH (parms);
10293 args = lang_hooks.get_innermost_generic_args (t);
10294 for (i = 0; i < parms_num; i++)
10296 tree parm, arg, arg_pack_elems;
10298 parm = TREE_VEC_ELT (parms, i);
10299 arg = TREE_VEC_ELT (args, i);
10300 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10301 gcc_assert (parm && TREE_VALUE (parm) && arg);
10303 if (parm && TREE_VALUE (parm) && arg)
10305 /* If PARM represents a template parameter pack,
10306 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10307 by DW_TAG_template_*_parameter DIEs for the argument
10308 pack elements of ARG. Note that ARG would then be
10309 an argument pack. */
10310 if (arg_pack_elems)
10311 template_parameter_pack_die (TREE_VALUE (parm),
10312 arg_pack_elems,
10313 die);
10314 else
10315 generic_parameter_die (TREE_VALUE (parm), arg,
10316 true /* Emit DW_AT_name */, die);
10321 /* Create and return a DIE for PARM which should be
10322 the representation of a generic type parameter.
10323 For instance, in the C++ front end, PARM would be a template parameter.
10324 ARG is the argument to PARM.
10325 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10326 name of the PARM.
10327 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10328 as a child node. */
10330 static dw_die_ref
10331 generic_parameter_die (tree parm, tree arg,
10332 bool emit_name_p,
10333 dw_die_ref parent_die)
10335 dw_die_ref tmpl_die = NULL;
10336 const char *name = NULL;
10338 if (!parm || !DECL_NAME (parm) || !arg)
10339 return NULL;
10341 /* We support non-type generic parameters and arguments,
10342 type generic parameters and arguments, as well as
10343 generic generic parameters (a.k.a. template template parameters in C++)
10344 and arguments. */
10345 if (TREE_CODE (parm) == PARM_DECL)
10346 /* PARM is a nontype generic parameter */
10347 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10348 else if (TREE_CODE (parm) == TYPE_DECL)
10349 /* PARM is a type generic parameter. */
10350 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10351 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10352 /* PARM is a generic generic parameter.
10353 Its DIE is a GNU extension. It shall have a
10354 DW_AT_name attribute to represent the name of the template template
10355 parameter, and a DW_AT_GNU_template_name attribute to represent the
10356 name of the template template argument. */
10357 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10358 parent_die, parm);
10359 else
10360 gcc_unreachable ();
10362 if (tmpl_die)
10364 tree tmpl_type;
10366 /* If PARM is a generic parameter pack, it means we are
10367 emitting debug info for a template argument pack element.
10368 In other terms, ARG is a template argument pack element.
10369 In that case, we don't emit any DW_AT_name attribute for
10370 the die. */
10371 if (emit_name_p)
10373 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10374 gcc_assert (name);
10375 add_AT_string (tmpl_die, DW_AT_name, name);
10378 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10380 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10381 TMPL_DIE should have a child DW_AT_type attribute that is set
10382 to the type of the argument to PARM, which is ARG.
10383 If PARM is a type generic parameter, TMPL_DIE should have a
10384 child DW_AT_type that is set to ARG. */
10385 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10386 add_type_attribute (tmpl_die, tmpl_type, 0,
10387 TREE_THIS_VOLATILE (tmpl_type),
10388 parent_die);
10390 else
10392 /* So TMPL_DIE is a DIE representing a
10393 a generic generic template parameter, a.k.a template template
10394 parameter in C++ and arg is a template. */
10396 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10397 to the name of the argument. */
10398 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10399 if (name)
10400 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10403 if (TREE_CODE (parm) == PARM_DECL)
10404 /* So PARM is a non-type generic parameter.
10405 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10406 attribute of TMPL_DIE which value represents the value
10407 of ARG.
10408 We must be careful here:
10409 The value of ARG might reference some function decls.
10410 We might currently be emitting debug info for a generic
10411 type and types are emitted before function decls, we don't
10412 know if the function decls referenced by ARG will actually be
10413 emitted after cgraph computations.
10414 So must defer the generation of the DW_AT_const_value to
10415 after cgraph is ready. */
10416 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10419 return tmpl_die;
10422 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10423 PARM_PACK must be a template parameter pack. The returned DIE
10424 will be child DIE of PARENT_DIE. */
10426 static dw_die_ref
10427 template_parameter_pack_die (tree parm_pack,
10428 tree parm_pack_args,
10429 dw_die_ref parent_die)
10431 dw_die_ref die;
10432 int j;
10434 gcc_assert (parent_die && parm_pack);
10436 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10437 add_name_and_src_coords_attributes (die, parm_pack);
10438 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10439 generic_parameter_die (parm_pack,
10440 TREE_VEC_ELT (parm_pack_args, j),
10441 false /* Don't emit DW_AT_name */,
10442 die);
10443 return die;
10446 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10447 an enumerated type. */
10449 static inline int
10450 type_is_enum (const_tree type)
10452 return TREE_CODE (type) == ENUMERAL_TYPE;
10455 /* Return the DBX register number described by a given RTL node. */
10457 static unsigned int
10458 dbx_reg_number (const_rtx rtl)
10460 unsigned regno = REGNO (rtl);
10462 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10464 #ifdef LEAF_REG_REMAP
10465 if (crtl->uses_only_leaf_regs)
10467 int leaf_reg = LEAF_REG_REMAP (regno);
10468 if (leaf_reg != -1)
10469 regno = (unsigned) leaf_reg;
10471 #endif
10473 regno = DBX_REGISTER_NUMBER (regno);
10474 gcc_assert (regno != INVALID_REGNUM);
10475 return regno;
10478 /* Optionally add a DW_OP_piece term to a location description expression.
10479 DW_OP_piece is only added if the location description expression already
10480 doesn't end with DW_OP_piece. */
10482 static void
10483 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10485 dw_loc_descr_ref loc;
10487 if (*list_head != NULL)
10489 /* Find the end of the chain. */
10490 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10493 if (loc->dw_loc_opc != DW_OP_piece)
10494 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10498 /* Return a location descriptor that designates a machine register or
10499 zero if there is none. */
10501 static dw_loc_descr_ref
10502 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10504 rtx regs;
10506 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10507 return 0;
10509 /* We only use "frame base" when we're sure we're talking about the
10510 post-prologue local stack frame. We do this by *not* running
10511 register elimination until this point, and recognizing the special
10512 argument pointer and soft frame pointer rtx's.
10513 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10514 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10515 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10517 dw_loc_descr_ref result = NULL;
10519 if (dwarf_version >= 4 || !dwarf_strict)
10521 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10522 initialized);
10523 if (result)
10524 add_loc_descr (&result,
10525 new_loc_descr (DW_OP_stack_value, 0, 0));
10527 return result;
10530 regs = targetm.dwarf_register_span (rtl);
10532 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10533 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10534 else
10536 unsigned int dbx_regnum = dbx_reg_number (rtl);
10537 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10538 return 0;
10539 return one_reg_loc_descriptor (dbx_regnum, initialized);
10543 /* Return a location descriptor that designates a machine register for
10544 a given hard register number. */
10546 static dw_loc_descr_ref
10547 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10549 dw_loc_descr_ref reg_loc_descr;
10551 if (regno <= 31)
10552 reg_loc_descr
10553 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10554 else
10555 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10557 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10558 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10560 return reg_loc_descr;
10563 /* Given an RTL of a register, return a location descriptor that
10564 designates a value that spans more than one register. */
10566 static dw_loc_descr_ref
10567 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10568 enum var_init_status initialized)
10570 int nregs, size, i;
10571 unsigned reg;
10572 dw_loc_descr_ref loc_result = NULL;
10574 reg = REGNO (rtl);
10575 #ifdef LEAF_REG_REMAP
10576 if (crtl->uses_only_leaf_regs)
10578 int leaf_reg = LEAF_REG_REMAP (reg);
10579 if (leaf_reg != -1)
10580 reg = (unsigned) leaf_reg;
10582 #endif
10583 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10584 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10586 /* Simple, contiguous registers. */
10587 if (regs == NULL_RTX)
10589 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10591 loc_result = NULL;
10592 while (nregs--)
10594 dw_loc_descr_ref t;
10596 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10597 VAR_INIT_STATUS_INITIALIZED);
10598 add_loc_descr (&loc_result, t);
10599 add_loc_descr_op_piece (&loc_result, size);
10600 ++reg;
10602 return loc_result;
10605 /* Now onto stupid register sets in non contiguous locations. */
10607 gcc_assert (GET_CODE (regs) == PARALLEL);
10609 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10610 loc_result = NULL;
10612 for (i = 0; i < XVECLEN (regs, 0); ++i)
10614 dw_loc_descr_ref t;
10616 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10617 VAR_INIT_STATUS_INITIALIZED);
10618 add_loc_descr (&loc_result, t);
10619 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10620 add_loc_descr_op_piece (&loc_result, size);
10623 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10624 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10625 return loc_result;
10628 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10630 /* Return a location descriptor that designates a constant i,
10631 as a compound operation from constant (i >> shift), constant shift
10632 and DW_OP_shl. */
10634 static dw_loc_descr_ref
10635 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10637 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10638 add_loc_descr (&ret, int_loc_descriptor (shift));
10639 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10640 return ret;
10643 /* Return a location descriptor that designates a constant. */
10645 static dw_loc_descr_ref
10646 int_loc_descriptor (HOST_WIDE_INT i)
10648 enum dwarf_location_atom op;
10650 /* Pick the smallest representation of a constant, rather than just
10651 defaulting to the LEB encoding. */
10652 if (i >= 0)
10654 int clz = clz_hwi (i);
10655 int ctz = ctz_hwi (i);
10656 if (i <= 31)
10657 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10658 else if (i <= 0xff)
10659 op = DW_OP_const1u;
10660 else if (i <= 0xffff)
10661 op = DW_OP_const2u;
10662 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10663 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10664 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10665 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10666 while DW_OP_const4u is 5 bytes. */
10667 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10668 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10669 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10670 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10671 while DW_OP_const4u is 5 bytes. */
10672 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10673 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10674 op = DW_OP_const4u;
10675 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10676 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10677 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10678 while DW_OP_constu of constant >= 0x100000000 takes at least
10679 6 bytes. */
10680 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10681 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10682 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10683 >= HOST_BITS_PER_WIDE_INT)
10684 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10685 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10686 while DW_OP_constu takes in this case at least 6 bytes. */
10687 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10688 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10689 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10690 && size_of_uleb128 (i) > 6)
10691 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10692 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10693 else
10694 op = DW_OP_constu;
10696 else
10698 if (i >= -0x80)
10699 op = DW_OP_const1s;
10700 else if (i >= -0x8000)
10701 op = DW_OP_const2s;
10702 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10704 if (size_of_int_loc_descriptor (i) < 5)
10706 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10707 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10708 return ret;
10710 op = DW_OP_const4s;
10712 else
10714 if (size_of_int_loc_descriptor (i)
10715 < (unsigned long) 1 + size_of_sleb128 (i))
10717 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10718 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10719 return ret;
10721 op = DW_OP_consts;
10725 return new_loc_descr (op, i, 0);
10728 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10729 without actually allocating it. */
10731 static unsigned long
10732 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10734 return size_of_int_loc_descriptor (i >> shift)
10735 + size_of_int_loc_descriptor (shift)
10736 + 1;
10739 /* Return size_of_locs (int_loc_descriptor (i)) without
10740 actually allocating it. */
10742 static unsigned long
10743 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10745 unsigned long s;
10747 if (i >= 0)
10749 int clz, ctz;
10750 if (i <= 31)
10751 return 1;
10752 else if (i <= 0xff)
10753 return 2;
10754 else if (i <= 0xffff)
10755 return 3;
10756 clz = clz_hwi (i);
10757 ctz = ctz_hwi (i);
10758 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10759 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10760 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10761 - clz - 5);
10762 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10763 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10764 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10765 - clz - 8);
10766 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10767 return 5;
10768 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10769 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10770 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10771 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10772 - clz - 8);
10773 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10774 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10775 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10776 - clz - 16);
10777 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10778 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10779 && s > 6)
10780 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10781 - clz - 32);
10782 else
10783 return 1 + s;
10785 else
10787 if (i >= -0x80)
10788 return 2;
10789 else if (i >= -0x8000)
10790 return 3;
10791 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10793 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10795 s = size_of_int_loc_descriptor (-i) + 1;
10796 if (s < 5)
10797 return s;
10799 return 5;
10801 else
10803 unsigned long r = 1 + size_of_sleb128 (i);
10804 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10806 s = size_of_int_loc_descriptor (-i) + 1;
10807 if (s < r)
10808 return s;
10810 return r;
10815 /* Return loc description representing "address" of integer value.
10816 This can appear only as toplevel expression. */
10818 static dw_loc_descr_ref
10819 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10821 int litsize;
10822 dw_loc_descr_ref loc_result = NULL;
10824 if (!(dwarf_version >= 4 || !dwarf_strict))
10825 return NULL;
10827 litsize = size_of_int_loc_descriptor (i);
10828 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10829 is more compact. For DW_OP_stack_value we need:
10830 litsize + 1 (DW_OP_stack_value)
10831 and for DW_OP_implicit_value:
10832 1 (DW_OP_implicit_value) + 1 (length) + size. */
10833 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10835 loc_result = int_loc_descriptor (i);
10836 add_loc_descr (&loc_result,
10837 new_loc_descr (DW_OP_stack_value, 0, 0));
10838 return loc_result;
10841 loc_result = new_loc_descr (DW_OP_implicit_value,
10842 size, 0);
10843 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10844 loc_result->dw_loc_oprnd2.v.val_int = i;
10845 return loc_result;
10848 /* Return a location descriptor that designates a base+offset location. */
10850 static dw_loc_descr_ref
10851 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10852 enum var_init_status initialized)
10854 unsigned int regno;
10855 dw_loc_descr_ref result;
10856 dw_fde_ref fde = cfun->fde;
10858 /* We only use "frame base" when we're sure we're talking about the
10859 post-prologue local stack frame. We do this by *not* running
10860 register elimination until this point, and recognizing the special
10861 argument pointer and soft frame pointer rtx's. */
10862 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10864 rtx elim = (ira_use_lra_p
10865 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10866 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10868 if (elim != reg)
10870 if (GET_CODE (elim) == PLUS)
10872 offset += INTVAL (XEXP (elim, 1));
10873 elim = XEXP (elim, 0);
10875 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10876 && (elim == hard_frame_pointer_rtx
10877 || elim == stack_pointer_rtx))
10878 || elim == (frame_pointer_needed
10879 ? hard_frame_pointer_rtx
10880 : stack_pointer_rtx));
10882 /* If drap register is used to align stack, use frame
10883 pointer + offset to access stack variables. If stack
10884 is aligned without drap, use stack pointer + offset to
10885 access stack variables. */
10886 if (crtl->stack_realign_tried
10887 && reg == frame_pointer_rtx)
10889 int base_reg
10890 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10891 ? HARD_FRAME_POINTER_REGNUM
10892 : REGNO (elim));
10893 return new_reg_loc_descr (base_reg, offset);
10896 gcc_assert (frame_pointer_fb_offset_valid);
10897 offset += frame_pointer_fb_offset;
10898 return new_loc_descr (DW_OP_fbreg, offset, 0);
10902 regno = REGNO (reg);
10903 #ifdef LEAF_REG_REMAP
10904 if (crtl->uses_only_leaf_regs)
10906 int leaf_reg = LEAF_REG_REMAP (regno);
10907 if (leaf_reg != -1)
10908 regno = (unsigned) leaf_reg;
10910 #endif
10911 regno = DWARF_FRAME_REGNUM (regno);
10913 if (!optimize && fde
10914 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10916 /* Use cfa+offset to represent the location of arguments passed
10917 on the stack when drap is used to align stack.
10918 Only do this when not optimizing, for optimized code var-tracking
10919 is supposed to track where the arguments live and the register
10920 used as vdrap or drap in some spot might be used for something
10921 else in other part of the routine. */
10922 return new_loc_descr (DW_OP_fbreg, offset, 0);
10925 if (regno <= 31)
10926 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10927 offset, 0);
10928 else
10929 result = new_loc_descr (DW_OP_bregx, regno, offset);
10931 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10932 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10934 return result;
10937 /* Return true if this RTL expression describes a base+offset calculation. */
10939 static inline int
10940 is_based_loc (const_rtx rtl)
10942 return (GET_CODE (rtl) == PLUS
10943 && ((REG_P (XEXP (rtl, 0))
10944 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10945 && CONST_INT_P (XEXP (rtl, 1)))));
10948 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10949 failed. */
10951 static dw_loc_descr_ref
10952 tls_mem_loc_descriptor (rtx mem)
10954 tree base;
10955 dw_loc_descr_ref loc_result;
10957 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10958 return NULL;
10960 base = get_base_address (MEM_EXPR (mem));
10961 if (base == NULL
10962 || TREE_CODE (base) != VAR_DECL
10963 || !DECL_THREAD_LOCAL_P (base))
10964 return NULL;
10966 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10967 if (loc_result == NULL)
10968 return NULL;
10970 if (MEM_OFFSET (mem))
10971 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10973 return loc_result;
10976 /* Output debug info about reason why we failed to expand expression as dwarf
10977 expression. */
10979 static void
10980 expansion_failed (tree expr, rtx rtl, char const *reason)
10982 if (dump_file && (dump_flags & TDF_DETAILS))
10984 fprintf (dump_file, "Failed to expand as dwarf: ");
10985 if (expr)
10986 print_generic_expr (dump_file, expr, dump_flags);
10987 if (rtl)
10989 fprintf (dump_file, "\n");
10990 print_rtl (dump_file, rtl);
10992 fprintf (dump_file, "\nReason: %s\n", reason);
10996 /* Helper function for const_ok_for_output, called either directly
10997 or via for_each_rtx. */
10999 static int
11000 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11002 rtx rtl = *rtlp;
11004 if (GET_CODE (rtl) == UNSPEC)
11006 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11007 we can't express it in the debug info. */
11008 #ifdef ENABLE_CHECKING
11009 /* Don't complain about TLS UNSPECs, those are just too hard to
11010 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11011 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11012 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11013 if (XVECLEN (rtl, 0) == 0
11014 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11015 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11016 inform (current_function_decl
11017 ? DECL_SOURCE_LOCATION (current_function_decl)
11018 : UNKNOWN_LOCATION,
11019 #if NUM_UNSPEC_VALUES > 0
11020 "non-delegitimized UNSPEC %s (%d) found in variable location",
11021 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11022 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11023 XINT (rtl, 1));
11024 #else
11025 "non-delegitimized UNSPEC %d found in variable location",
11026 XINT (rtl, 1));
11027 #endif
11028 #endif
11029 expansion_failed (NULL_TREE, rtl,
11030 "UNSPEC hasn't been delegitimized.\n");
11031 return 1;
11034 if (targetm.const_not_ok_for_debug_p (rtl))
11036 expansion_failed (NULL_TREE, rtl,
11037 "Expression rejected for debug by the backend.\n");
11038 return 1;
11041 if (GET_CODE (rtl) != SYMBOL_REF)
11042 return 0;
11044 if (CONSTANT_POOL_ADDRESS_P (rtl))
11046 bool marked;
11047 get_pool_constant_mark (rtl, &marked);
11048 /* If all references to this pool constant were optimized away,
11049 it was not output and thus we can't represent it. */
11050 if (!marked)
11052 expansion_failed (NULL_TREE, rtl,
11053 "Constant was removed from constant pool.\n");
11054 return 1;
11058 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11059 return 1;
11061 /* Avoid references to external symbols in debug info, on several targets
11062 the linker might even refuse to link when linking a shared library,
11063 and in many other cases the relocations for .debug_info/.debug_loc are
11064 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11065 to be defined within the same shared library or executable are fine. */
11066 if (SYMBOL_REF_EXTERNAL_P (rtl))
11068 tree decl = SYMBOL_REF_DECL (rtl);
11070 if (decl == NULL || !targetm.binds_local_p (decl))
11072 expansion_failed (NULL_TREE, rtl,
11073 "Symbol not defined in current TU.\n");
11074 return 1;
11078 return 0;
11081 /* Return true if constant RTL can be emitted in DW_OP_addr or
11082 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11083 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11085 static bool
11086 const_ok_for_output (rtx rtl)
11088 if (GET_CODE (rtl) == SYMBOL_REF)
11089 return const_ok_for_output_1 (&rtl, NULL) == 0;
11091 if (GET_CODE (rtl) == CONST)
11092 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11094 return true;
11097 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11098 if possible, NULL otherwise. */
11100 static dw_die_ref
11101 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11103 dw_die_ref type_die;
11104 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11106 if (type == NULL)
11107 return NULL;
11108 switch (TREE_CODE (type))
11110 case INTEGER_TYPE:
11111 case REAL_TYPE:
11112 break;
11113 default:
11114 return NULL;
11116 type_die = lookup_type_die (type);
11117 if (!type_die)
11118 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11119 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11120 return NULL;
11121 return type_die;
11124 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11125 type matching MODE, or, if MODE is narrower than or as wide as
11126 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11127 possible. */
11129 static dw_loc_descr_ref
11130 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11132 enum machine_mode outer_mode = mode;
11133 dw_die_ref type_die;
11134 dw_loc_descr_ref cvt;
11136 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11138 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11139 return op;
11141 type_die = base_type_for_mode (outer_mode, 1);
11142 if (type_die == NULL)
11143 return NULL;
11144 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11145 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11146 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11147 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11148 add_loc_descr (&op, cvt);
11149 return op;
11152 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11154 static dw_loc_descr_ref
11155 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11156 dw_loc_descr_ref op1)
11158 dw_loc_descr_ref ret = op0;
11159 add_loc_descr (&ret, op1);
11160 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11161 if (STORE_FLAG_VALUE != 1)
11163 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11164 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11166 return ret;
11169 /* Return location descriptor for signed comparison OP RTL. */
11171 static dw_loc_descr_ref
11172 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11173 enum machine_mode mem_mode)
11175 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11176 dw_loc_descr_ref op0, op1;
11177 int shift;
11179 if (op_mode == VOIDmode)
11180 op_mode = GET_MODE (XEXP (rtl, 1));
11181 if (op_mode == VOIDmode)
11182 return NULL;
11184 if (dwarf_strict
11185 && (GET_MODE_CLASS (op_mode) != MODE_INT
11186 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11187 return NULL;
11189 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11190 VAR_INIT_STATUS_INITIALIZED);
11191 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11192 VAR_INIT_STATUS_INITIALIZED);
11194 if (op0 == NULL || op1 == NULL)
11195 return NULL;
11197 if (GET_MODE_CLASS (op_mode) != MODE_INT
11198 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11199 return compare_loc_descriptor (op, op0, op1);
11201 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11203 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11204 dw_loc_descr_ref cvt;
11206 if (type_die == NULL)
11207 return NULL;
11208 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11209 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11210 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11211 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11212 add_loc_descr (&op0, cvt);
11213 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11214 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11215 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11216 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11217 add_loc_descr (&op1, cvt);
11218 return compare_loc_descriptor (op, op0, op1);
11221 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11222 /* For eq/ne, if the operands are known to be zero-extended,
11223 there is no need to do the fancy shifting up. */
11224 if (op == DW_OP_eq || op == DW_OP_ne)
11226 dw_loc_descr_ref last0, last1;
11227 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11229 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11231 /* deref_size zero extends, and for constants we can check
11232 whether they are zero extended or not. */
11233 if (((last0->dw_loc_opc == DW_OP_deref_size
11234 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11235 || (CONST_INT_P (XEXP (rtl, 0))
11236 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11237 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11238 && ((last1->dw_loc_opc == DW_OP_deref_size
11239 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11240 || (CONST_INT_P (XEXP (rtl, 1))
11241 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11242 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11243 return compare_loc_descriptor (op, op0, op1);
11245 /* EQ/NE comparison against constant in narrower type than
11246 DWARF2_ADDR_SIZE can be performed either as
11247 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11248 DW_OP_{eq,ne}
11250 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11251 DW_OP_{eq,ne}. Pick whatever is shorter. */
11252 if (CONST_INT_P (XEXP (rtl, 1))
11253 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11254 && (size_of_int_loc_descriptor (shift) + 1
11255 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11256 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11257 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11258 & GET_MODE_MASK (op_mode))))
11260 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11261 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11262 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11263 & GET_MODE_MASK (op_mode));
11264 return compare_loc_descriptor (op, op0, op1);
11267 add_loc_descr (&op0, int_loc_descriptor (shift));
11268 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11269 if (CONST_INT_P (XEXP (rtl, 1)))
11270 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11271 else
11273 add_loc_descr (&op1, int_loc_descriptor (shift));
11274 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11276 return compare_loc_descriptor (op, op0, op1);
11279 /* Return location descriptor for unsigned comparison OP RTL. */
11281 static dw_loc_descr_ref
11282 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11283 enum machine_mode mem_mode)
11285 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11286 dw_loc_descr_ref op0, op1;
11288 if (op_mode == VOIDmode)
11289 op_mode = GET_MODE (XEXP (rtl, 1));
11290 if (op_mode == VOIDmode)
11291 return NULL;
11292 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11293 return NULL;
11295 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11296 return NULL;
11298 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11299 VAR_INIT_STATUS_INITIALIZED);
11300 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11301 VAR_INIT_STATUS_INITIALIZED);
11303 if (op0 == NULL || op1 == NULL)
11304 return NULL;
11306 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11308 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11309 dw_loc_descr_ref last0, last1;
11310 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11312 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11314 if (CONST_INT_P (XEXP (rtl, 0)))
11315 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11316 /* deref_size zero extends, so no need to mask it again. */
11317 else if (last0->dw_loc_opc != DW_OP_deref_size
11318 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11320 add_loc_descr (&op0, int_loc_descriptor (mask));
11321 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11323 if (CONST_INT_P (XEXP (rtl, 1)))
11324 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11325 /* deref_size zero extends, so no need to mask it again. */
11326 else if (last1->dw_loc_opc != DW_OP_deref_size
11327 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11329 add_loc_descr (&op1, int_loc_descriptor (mask));
11330 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11333 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11335 HOST_WIDE_INT bias = 1;
11336 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11337 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11338 if (CONST_INT_P (XEXP (rtl, 1)))
11339 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11340 + INTVAL (XEXP (rtl, 1)));
11341 else
11342 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11343 bias, 0));
11345 return compare_loc_descriptor (op, op0, op1);
11348 /* Return location descriptor for {U,S}{MIN,MAX}. */
11350 static dw_loc_descr_ref
11351 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11352 enum machine_mode mem_mode)
11354 enum dwarf_location_atom op;
11355 dw_loc_descr_ref op0, op1, ret;
11356 dw_loc_descr_ref bra_node, drop_node;
11358 if (dwarf_strict
11359 && (GET_MODE_CLASS (mode) != MODE_INT
11360 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11361 return NULL;
11363 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11364 VAR_INIT_STATUS_INITIALIZED);
11365 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11366 VAR_INIT_STATUS_INITIALIZED);
11368 if (op0 == NULL || op1 == NULL)
11369 return NULL;
11371 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11372 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11373 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11374 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11376 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11378 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11379 add_loc_descr (&op0, int_loc_descriptor (mask));
11380 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11381 add_loc_descr (&op1, int_loc_descriptor (mask));
11382 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11384 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11386 HOST_WIDE_INT bias = 1;
11387 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11388 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11389 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11392 else if (GET_MODE_CLASS (mode) == MODE_INT
11393 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11395 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11396 add_loc_descr (&op0, int_loc_descriptor (shift));
11397 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11398 add_loc_descr (&op1, int_loc_descriptor (shift));
11399 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11401 else if (GET_MODE_CLASS (mode) == MODE_INT
11402 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11404 dw_die_ref type_die = base_type_for_mode (mode, 0);
11405 dw_loc_descr_ref cvt;
11406 if (type_die == NULL)
11407 return NULL;
11408 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11409 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11410 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11411 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11412 add_loc_descr (&op0, cvt);
11413 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11414 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11415 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11416 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11417 add_loc_descr (&op1, cvt);
11420 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11421 op = DW_OP_lt;
11422 else
11423 op = DW_OP_gt;
11424 ret = op0;
11425 add_loc_descr (&ret, op1);
11426 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11427 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11428 add_loc_descr (&ret, bra_node);
11429 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11430 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11431 add_loc_descr (&ret, drop_node);
11432 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11433 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11434 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11435 && GET_MODE_CLASS (mode) == MODE_INT
11436 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11437 ret = convert_descriptor_to_mode (mode, ret);
11438 return ret;
11441 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11442 but after converting arguments to type_die, afterwards
11443 convert back to unsigned. */
11445 static dw_loc_descr_ref
11446 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11447 enum machine_mode mode, enum machine_mode mem_mode)
11449 dw_loc_descr_ref cvt, op0, op1;
11451 if (type_die == NULL)
11452 return NULL;
11453 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11454 VAR_INIT_STATUS_INITIALIZED);
11455 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11456 VAR_INIT_STATUS_INITIALIZED);
11457 if (op0 == NULL || op1 == NULL)
11458 return NULL;
11459 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11460 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11461 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11462 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11463 add_loc_descr (&op0, cvt);
11464 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11465 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11466 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11467 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11468 add_loc_descr (&op1, cvt);
11469 add_loc_descr (&op0, op1);
11470 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11471 return convert_descriptor_to_mode (mode, op0);
11474 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11475 const0 is DW_OP_lit0 or corresponding typed constant,
11476 const1 is DW_OP_lit1 or corresponding typed constant
11477 and constMSB is constant with just the MSB bit set
11478 for the mode):
11479 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11480 L1: const0 DW_OP_swap
11481 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11482 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11483 L3: DW_OP_drop
11484 L4: DW_OP_nop
11486 CTZ is similar:
11487 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11488 L1: const0 DW_OP_swap
11489 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11490 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11491 L3: DW_OP_drop
11492 L4: DW_OP_nop
11494 FFS is similar:
11495 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11496 L1: const1 DW_OP_swap
11497 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11498 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11499 L3: DW_OP_drop
11500 L4: DW_OP_nop */
11502 static dw_loc_descr_ref
11503 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11504 enum machine_mode mem_mode)
11506 dw_loc_descr_ref op0, ret, tmp;
11507 HOST_WIDE_INT valv;
11508 dw_loc_descr_ref l1jump, l1label;
11509 dw_loc_descr_ref l2jump, l2label;
11510 dw_loc_descr_ref l3jump, l3label;
11511 dw_loc_descr_ref l4jump, l4label;
11512 rtx msb;
11514 if (GET_MODE_CLASS (mode) != MODE_INT
11515 || GET_MODE (XEXP (rtl, 0)) != mode
11516 || (GET_CODE (rtl) == CLZ
11517 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11518 return NULL;
11520 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11521 VAR_INIT_STATUS_INITIALIZED);
11522 if (op0 == NULL)
11523 return NULL;
11524 ret = op0;
11525 if (GET_CODE (rtl) == CLZ)
11527 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11528 valv = GET_MODE_BITSIZE (mode);
11530 else if (GET_CODE (rtl) == FFS)
11531 valv = 0;
11532 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11533 valv = GET_MODE_BITSIZE (mode);
11534 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11535 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11536 add_loc_descr (&ret, l1jump);
11537 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11538 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11539 VAR_INIT_STATUS_INITIALIZED);
11540 if (tmp == NULL)
11541 return NULL;
11542 add_loc_descr (&ret, tmp);
11543 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11544 add_loc_descr (&ret, l4jump);
11545 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11546 ? const1_rtx : const0_rtx,
11547 mode, mem_mode,
11548 VAR_INIT_STATUS_INITIALIZED);
11549 if (l1label == NULL)
11550 return NULL;
11551 add_loc_descr (&ret, l1label);
11552 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11553 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11554 add_loc_descr (&ret, l2label);
11555 if (GET_CODE (rtl) != CLZ)
11556 msb = const1_rtx;
11557 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11558 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11559 << (GET_MODE_BITSIZE (mode) - 1));
11560 else
11561 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11562 << (GET_MODE_BITSIZE (mode)
11563 - HOST_BITS_PER_WIDE_INT - 1), mode);
11564 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11565 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11566 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11567 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11568 else
11569 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11570 VAR_INIT_STATUS_INITIALIZED);
11571 if (tmp == NULL)
11572 return NULL;
11573 add_loc_descr (&ret, tmp);
11574 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11575 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11576 add_loc_descr (&ret, l3jump);
11577 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11578 VAR_INIT_STATUS_INITIALIZED);
11579 if (tmp == NULL)
11580 return NULL;
11581 add_loc_descr (&ret, tmp);
11582 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11583 ? DW_OP_shl : DW_OP_shr, 0, 0));
11584 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11585 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11586 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11587 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11588 add_loc_descr (&ret, l2jump);
11589 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11590 add_loc_descr (&ret, l3label);
11591 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11592 add_loc_descr (&ret, l4label);
11593 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11594 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11595 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11596 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11597 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11598 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11599 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11600 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11601 return ret;
11604 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11605 const1 is DW_OP_lit1 or corresponding typed constant):
11606 const0 DW_OP_swap
11607 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11608 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11609 L2: DW_OP_drop
11611 PARITY is similar:
11612 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11613 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11614 L2: DW_OP_drop */
11616 static dw_loc_descr_ref
11617 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11618 enum machine_mode mem_mode)
11620 dw_loc_descr_ref op0, ret, tmp;
11621 dw_loc_descr_ref l1jump, l1label;
11622 dw_loc_descr_ref l2jump, l2label;
11624 if (GET_MODE_CLASS (mode) != MODE_INT
11625 || GET_MODE (XEXP (rtl, 0)) != mode)
11626 return NULL;
11628 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11629 VAR_INIT_STATUS_INITIALIZED);
11630 if (op0 == NULL)
11631 return NULL;
11632 ret = op0;
11633 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11634 VAR_INIT_STATUS_INITIALIZED);
11635 if (tmp == NULL)
11636 return NULL;
11637 add_loc_descr (&ret, tmp);
11638 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11639 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11640 add_loc_descr (&ret, l1label);
11641 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11642 add_loc_descr (&ret, l2jump);
11643 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11644 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11645 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11646 VAR_INIT_STATUS_INITIALIZED);
11647 if (tmp == NULL)
11648 return NULL;
11649 add_loc_descr (&ret, tmp);
11650 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11651 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11652 ? DW_OP_plus : DW_OP_xor, 0, 0));
11653 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11654 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11655 VAR_INIT_STATUS_INITIALIZED);
11656 add_loc_descr (&ret, tmp);
11657 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11658 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11659 add_loc_descr (&ret, l1jump);
11660 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11661 add_loc_descr (&ret, l2label);
11662 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11663 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11664 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11665 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11666 return ret;
11669 /* BSWAP (constS is initial shift count, either 56 or 24):
11670 constS const0
11671 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11672 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11673 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11674 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11675 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11677 static dw_loc_descr_ref
11678 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11679 enum machine_mode mem_mode)
11681 dw_loc_descr_ref op0, ret, tmp;
11682 dw_loc_descr_ref l1jump, l1label;
11683 dw_loc_descr_ref l2jump, l2label;
11685 if (GET_MODE_CLASS (mode) != MODE_INT
11686 || BITS_PER_UNIT != 8
11687 || (GET_MODE_BITSIZE (mode) != 32
11688 && GET_MODE_BITSIZE (mode) != 64))
11689 return NULL;
11691 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11692 VAR_INIT_STATUS_INITIALIZED);
11693 if (op0 == NULL)
11694 return NULL;
11696 ret = op0;
11697 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11698 mode, mem_mode,
11699 VAR_INIT_STATUS_INITIALIZED);
11700 if (tmp == NULL)
11701 return NULL;
11702 add_loc_descr (&ret, tmp);
11703 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11704 VAR_INIT_STATUS_INITIALIZED);
11705 if (tmp == NULL)
11706 return NULL;
11707 add_loc_descr (&ret, tmp);
11708 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11709 add_loc_descr (&ret, l1label);
11710 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11711 mode, mem_mode,
11712 VAR_INIT_STATUS_INITIALIZED);
11713 add_loc_descr (&ret, tmp);
11714 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11715 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11716 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11717 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11718 VAR_INIT_STATUS_INITIALIZED);
11719 if (tmp == NULL)
11720 return NULL;
11721 add_loc_descr (&ret, tmp);
11722 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11723 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11724 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11725 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11726 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11727 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11728 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11729 VAR_INIT_STATUS_INITIALIZED);
11730 add_loc_descr (&ret, tmp);
11731 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11732 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11733 add_loc_descr (&ret, l2jump);
11734 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11735 VAR_INIT_STATUS_INITIALIZED);
11736 add_loc_descr (&ret, tmp);
11737 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11738 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11739 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11740 add_loc_descr (&ret, l1jump);
11741 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11742 add_loc_descr (&ret, l2label);
11743 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11744 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11745 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11746 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11747 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11748 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11749 return ret;
11752 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11753 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11754 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11755 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11757 ROTATERT is similar:
11758 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11759 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11760 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11762 static dw_loc_descr_ref
11763 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11764 enum machine_mode mem_mode)
11766 rtx rtlop1 = XEXP (rtl, 1);
11767 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11768 int i;
11770 if (GET_MODE_CLASS (mode) != MODE_INT)
11771 return NULL;
11773 if (GET_MODE (rtlop1) != VOIDmode
11774 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11775 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11776 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11777 VAR_INIT_STATUS_INITIALIZED);
11778 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11779 VAR_INIT_STATUS_INITIALIZED);
11780 if (op0 == NULL || op1 == NULL)
11781 return NULL;
11782 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11783 for (i = 0; i < 2; i++)
11785 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11786 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11787 mode, mem_mode,
11788 VAR_INIT_STATUS_INITIALIZED);
11789 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11790 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11791 ? DW_OP_const4u
11792 : HOST_BITS_PER_WIDE_INT == 64
11793 ? DW_OP_const8u : DW_OP_constu,
11794 GET_MODE_MASK (mode), 0);
11795 else
11796 mask[i] = NULL;
11797 if (mask[i] == NULL)
11798 return NULL;
11799 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11801 ret = op0;
11802 add_loc_descr (&ret, op1);
11803 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11804 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11805 if (GET_CODE (rtl) == ROTATERT)
11807 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11808 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11809 GET_MODE_BITSIZE (mode), 0));
11811 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11812 if (mask[0] != NULL)
11813 add_loc_descr (&ret, mask[0]);
11814 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11815 if (mask[1] != NULL)
11817 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11818 add_loc_descr (&ret, mask[1]);
11819 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11821 if (GET_CODE (rtl) == ROTATE)
11823 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11824 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11825 GET_MODE_BITSIZE (mode), 0));
11827 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11828 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11829 return ret;
11832 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11833 for DEBUG_PARAMETER_REF RTL. */
11835 static dw_loc_descr_ref
11836 parameter_ref_descriptor (rtx rtl)
11838 dw_loc_descr_ref ret;
11839 dw_die_ref ref;
11841 if (dwarf_strict)
11842 return NULL;
11843 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11844 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11845 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11846 if (ref)
11848 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11849 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11850 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11852 else
11854 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11855 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11857 return ret;
11860 /* The following routine converts the RTL for a variable or parameter
11861 (resident in memory) into an equivalent Dwarf representation of a
11862 mechanism for getting the address of that same variable onto the top of a
11863 hypothetical "address evaluation" stack.
11865 When creating memory location descriptors, we are effectively transforming
11866 the RTL for a memory-resident object into its Dwarf postfix expression
11867 equivalent. This routine recursively descends an RTL tree, turning
11868 it into Dwarf postfix code as it goes.
11870 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11872 MEM_MODE is the mode of the memory reference, needed to handle some
11873 autoincrement addressing modes.
11875 Return 0 if we can't represent the location. */
11877 dw_loc_descr_ref
11878 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11879 enum machine_mode mem_mode,
11880 enum var_init_status initialized)
11882 dw_loc_descr_ref mem_loc_result = NULL;
11883 enum dwarf_location_atom op;
11884 dw_loc_descr_ref op0, op1;
11885 rtx inner = NULL_RTX;
11887 if (mode == VOIDmode)
11888 mode = GET_MODE (rtl);
11890 /* Note that for a dynamically sized array, the location we will generate a
11891 description of here will be the lowest numbered location which is
11892 actually within the array. That's *not* necessarily the same as the
11893 zeroth element of the array. */
11895 rtl = targetm.delegitimize_address (rtl);
11897 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11898 return NULL;
11900 switch (GET_CODE (rtl))
11902 case POST_INC:
11903 case POST_DEC:
11904 case POST_MODIFY:
11905 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11907 case SUBREG:
11908 /* The case of a subreg may arise when we have a local (register)
11909 variable or a formal (register) parameter which doesn't quite fill
11910 up an entire register. For now, just assume that it is
11911 legitimate to make the Dwarf info refer to the whole register which
11912 contains the given subreg. */
11913 if (!subreg_lowpart_p (rtl))
11914 break;
11915 inner = SUBREG_REG (rtl);
11916 case TRUNCATE:
11917 if (inner == NULL_RTX)
11918 inner = XEXP (rtl, 0);
11919 if (GET_MODE_CLASS (mode) == MODE_INT
11920 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11921 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11922 #ifdef POINTERS_EXTEND_UNSIGNED
11923 || (mode == Pmode && mem_mode != VOIDmode)
11924 #endif
11926 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
11928 mem_loc_result = mem_loc_descriptor (inner,
11929 GET_MODE (inner),
11930 mem_mode, initialized);
11931 break;
11933 if (dwarf_strict)
11934 break;
11935 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
11936 break;
11937 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
11938 && (GET_MODE_CLASS (mode) != MODE_INT
11939 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
11940 break;
11941 else
11943 dw_die_ref type_die;
11944 dw_loc_descr_ref cvt;
11946 mem_loc_result = mem_loc_descriptor (inner,
11947 GET_MODE (inner),
11948 mem_mode, initialized);
11949 if (mem_loc_result == NULL)
11950 break;
11951 type_die = base_type_for_mode (mode,
11952 GET_MODE_CLASS (mode) == MODE_INT);
11953 if (type_die == NULL)
11955 mem_loc_result = NULL;
11956 break;
11958 if (GET_MODE_SIZE (mode)
11959 != GET_MODE_SIZE (GET_MODE (inner)))
11960 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11961 else
11962 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11963 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11964 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11965 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11966 add_loc_descr (&mem_loc_result, cvt);
11968 break;
11970 case REG:
11971 if (GET_MODE_CLASS (mode) != MODE_INT
11972 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11973 && rtl != arg_pointer_rtx
11974 && rtl != frame_pointer_rtx
11975 #ifdef POINTERS_EXTEND_UNSIGNED
11976 && (mode != Pmode || mem_mode == VOIDmode)
11977 #endif
11980 dw_die_ref type_die;
11981 unsigned int dbx_regnum;
11983 if (dwarf_strict)
11984 break;
11985 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11986 break;
11987 type_die = base_type_for_mode (mode,
11988 GET_MODE_CLASS (mode) == MODE_INT);
11989 if (type_die == NULL)
11990 break;
11992 dbx_regnum = dbx_reg_number (rtl);
11993 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11994 break;
11995 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11996 dbx_regnum, 0);
11997 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11998 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11999 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12000 break;
12002 /* Whenever a register number forms a part of the description of the
12003 method for calculating the (dynamic) address of a memory resident
12004 object, DWARF rules require the register number be referred to as
12005 a "base register". This distinction is not based in any way upon
12006 what category of register the hardware believes the given register
12007 belongs to. This is strictly DWARF terminology we're dealing with
12008 here. Note that in cases where the location of a memory-resident
12009 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12010 OP_CONST (0)) the actual DWARF location descriptor that we generate
12011 may just be OP_BASEREG (basereg). This may look deceptively like
12012 the object in question was allocated to a register (rather than in
12013 memory) so DWARF consumers need to be aware of the subtle
12014 distinction between OP_REG and OP_BASEREG. */
12015 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12016 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12017 else if (stack_realign_drap
12018 && crtl->drap_reg
12019 && crtl->args.internal_arg_pointer == rtl
12020 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12022 /* If RTL is internal_arg_pointer, which has been optimized
12023 out, use DRAP instead. */
12024 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12025 VAR_INIT_STATUS_INITIALIZED);
12027 break;
12029 case SIGN_EXTEND:
12030 case ZERO_EXTEND:
12031 if (GET_MODE_CLASS (mode) != MODE_INT)
12032 break;
12033 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12034 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12035 if (op0 == 0)
12036 break;
12037 else if (GET_CODE (rtl) == ZERO_EXTEND
12038 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12039 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12040 < HOST_BITS_PER_WIDE_INT
12041 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12042 to expand zero extend as two shifts instead of
12043 masking. */
12044 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12046 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12047 mem_loc_result = op0;
12048 add_loc_descr (&mem_loc_result,
12049 int_loc_descriptor (GET_MODE_MASK (imode)));
12050 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12052 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12054 int shift = DWARF2_ADDR_SIZE
12055 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12056 shift *= BITS_PER_UNIT;
12057 if (GET_CODE (rtl) == SIGN_EXTEND)
12058 op = DW_OP_shra;
12059 else
12060 op = DW_OP_shr;
12061 mem_loc_result = op0;
12062 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12063 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12064 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12065 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12067 else if (!dwarf_strict)
12069 dw_die_ref type_die1, type_die2;
12070 dw_loc_descr_ref cvt;
12072 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12073 GET_CODE (rtl) == ZERO_EXTEND);
12074 if (type_die1 == NULL)
12075 break;
12076 type_die2 = base_type_for_mode (mode, 1);
12077 if (type_die2 == NULL)
12078 break;
12079 mem_loc_result = op0;
12080 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12081 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12082 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12083 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12084 add_loc_descr (&mem_loc_result, cvt);
12085 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12086 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12087 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12088 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12089 add_loc_descr (&mem_loc_result, cvt);
12091 break;
12093 case MEM:
12095 rtx new_rtl = avoid_constant_pool_reference (rtl);
12096 if (new_rtl != rtl)
12098 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12099 initialized);
12100 if (mem_loc_result != NULL)
12101 return mem_loc_result;
12104 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12105 get_address_mode (rtl), mode,
12106 VAR_INIT_STATUS_INITIALIZED);
12107 if (mem_loc_result == NULL)
12108 mem_loc_result = tls_mem_loc_descriptor (rtl);
12109 if (mem_loc_result != NULL)
12111 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12112 || GET_MODE_CLASS (mode) != MODE_INT)
12114 dw_die_ref type_die;
12115 dw_loc_descr_ref deref;
12117 if (dwarf_strict)
12118 return NULL;
12119 type_die
12120 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12121 if (type_die == NULL)
12122 return NULL;
12123 deref = new_loc_descr (DW_OP_GNU_deref_type,
12124 GET_MODE_SIZE (mode), 0);
12125 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12126 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12127 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12128 add_loc_descr (&mem_loc_result, deref);
12130 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12131 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12132 else
12133 add_loc_descr (&mem_loc_result,
12134 new_loc_descr (DW_OP_deref_size,
12135 GET_MODE_SIZE (mode), 0));
12137 break;
12139 case LO_SUM:
12140 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12142 case LABEL_REF:
12143 /* Some ports can transform a symbol ref into a label ref, because
12144 the symbol ref is too far away and has to be dumped into a constant
12145 pool. */
12146 case CONST:
12147 case SYMBOL_REF:
12148 if (GET_MODE_CLASS (mode) != MODE_INT
12149 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12150 #ifdef POINTERS_EXTEND_UNSIGNED
12151 && (mode != Pmode || mem_mode == VOIDmode)
12152 #endif
12154 break;
12155 if (GET_CODE (rtl) == SYMBOL_REF
12156 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12158 dw_loc_descr_ref temp;
12160 /* If this is not defined, we have no way to emit the data. */
12161 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12162 break;
12164 temp = new_addr_loc_descr (rtl, dtprel_true);
12166 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12167 add_loc_descr (&mem_loc_result, temp);
12169 break;
12172 if (!const_ok_for_output (rtl))
12173 break;
12175 symref:
12176 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12177 vec_safe_push (used_rtx_array, rtl);
12178 break;
12180 case CONCAT:
12181 case CONCATN:
12182 case VAR_LOCATION:
12183 case DEBUG_IMPLICIT_PTR:
12184 expansion_failed (NULL_TREE, rtl,
12185 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12186 return 0;
12188 case ENTRY_VALUE:
12189 if (dwarf_strict)
12190 return NULL;
12191 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12193 if (GET_MODE_CLASS (mode) != MODE_INT
12194 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12195 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12196 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12197 else
12199 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12200 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12201 return NULL;
12202 op0 = one_reg_loc_descriptor (dbx_regnum,
12203 VAR_INIT_STATUS_INITIALIZED);
12206 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12207 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12209 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12210 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12211 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12212 return NULL;
12214 else
12215 gcc_unreachable ();
12216 if (op0 == NULL)
12217 return NULL;
12218 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12219 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12220 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12221 break;
12223 case DEBUG_PARAMETER_REF:
12224 mem_loc_result = parameter_ref_descriptor (rtl);
12225 break;
12227 case PRE_MODIFY:
12228 /* Extract the PLUS expression nested inside and fall into
12229 PLUS code below. */
12230 rtl = XEXP (rtl, 1);
12231 goto plus;
12233 case PRE_INC:
12234 case PRE_DEC:
12235 /* Turn these into a PLUS expression and fall into the PLUS code
12236 below. */
12237 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12238 GEN_INT (GET_CODE (rtl) == PRE_INC
12239 ? GET_MODE_UNIT_SIZE (mem_mode)
12240 : -GET_MODE_UNIT_SIZE (mem_mode)));
12242 /* ... fall through ... */
12244 case PLUS:
12245 plus:
12246 if (is_based_loc (rtl)
12247 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12248 || XEXP (rtl, 0) == arg_pointer_rtx
12249 || XEXP (rtl, 0) == frame_pointer_rtx)
12250 && GET_MODE_CLASS (mode) == MODE_INT)
12251 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12252 INTVAL (XEXP (rtl, 1)),
12253 VAR_INIT_STATUS_INITIALIZED);
12254 else
12256 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12257 VAR_INIT_STATUS_INITIALIZED);
12258 if (mem_loc_result == 0)
12259 break;
12261 if (CONST_INT_P (XEXP (rtl, 1))
12262 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12263 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12264 else
12266 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12267 VAR_INIT_STATUS_INITIALIZED);
12268 if (op1 == 0)
12269 break;
12270 add_loc_descr (&mem_loc_result, op1);
12271 add_loc_descr (&mem_loc_result,
12272 new_loc_descr (DW_OP_plus, 0, 0));
12275 break;
12277 /* If a pseudo-reg is optimized away, it is possible for it to
12278 be replaced with a MEM containing a multiply or shift. */
12279 case MINUS:
12280 op = DW_OP_minus;
12281 goto do_binop;
12283 case MULT:
12284 op = DW_OP_mul;
12285 goto do_binop;
12287 case DIV:
12288 if (!dwarf_strict
12289 && GET_MODE_CLASS (mode) == MODE_INT
12290 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12292 mem_loc_result = typed_binop (DW_OP_div, rtl,
12293 base_type_for_mode (mode, 0),
12294 mode, mem_mode);
12295 break;
12297 op = DW_OP_div;
12298 goto do_binop;
12300 case UMOD:
12301 op = DW_OP_mod;
12302 goto do_binop;
12304 case ASHIFT:
12305 op = DW_OP_shl;
12306 goto do_shift;
12308 case ASHIFTRT:
12309 op = DW_OP_shra;
12310 goto do_shift;
12312 case LSHIFTRT:
12313 op = DW_OP_shr;
12314 goto do_shift;
12316 do_shift:
12317 if (GET_MODE_CLASS (mode) != MODE_INT)
12318 break;
12319 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12320 VAR_INIT_STATUS_INITIALIZED);
12322 rtx rtlop1 = XEXP (rtl, 1);
12323 if (GET_MODE (rtlop1) != VOIDmode
12324 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12325 < GET_MODE_BITSIZE (mode))
12326 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12327 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12328 VAR_INIT_STATUS_INITIALIZED);
12331 if (op0 == 0 || op1 == 0)
12332 break;
12334 mem_loc_result = op0;
12335 add_loc_descr (&mem_loc_result, op1);
12336 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12337 break;
12339 case AND:
12340 op = DW_OP_and;
12341 goto do_binop;
12343 case IOR:
12344 op = DW_OP_or;
12345 goto do_binop;
12347 case XOR:
12348 op = DW_OP_xor;
12349 goto do_binop;
12351 do_binop:
12352 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12353 VAR_INIT_STATUS_INITIALIZED);
12354 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12355 VAR_INIT_STATUS_INITIALIZED);
12357 if (op0 == 0 || op1 == 0)
12358 break;
12360 mem_loc_result = op0;
12361 add_loc_descr (&mem_loc_result, op1);
12362 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12363 break;
12365 case MOD:
12366 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12368 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12369 base_type_for_mode (mode, 0),
12370 mode, mem_mode);
12371 break;
12374 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12375 VAR_INIT_STATUS_INITIALIZED);
12376 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12377 VAR_INIT_STATUS_INITIALIZED);
12379 if (op0 == 0 || op1 == 0)
12380 break;
12382 mem_loc_result = op0;
12383 add_loc_descr (&mem_loc_result, op1);
12384 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12385 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12386 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12387 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12388 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12389 break;
12391 case UDIV:
12392 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12394 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12396 op = DW_OP_div;
12397 goto do_binop;
12399 mem_loc_result = typed_binop (DW_OP_div, rtl,
12400 base_type_for_mode (mode, 1),
12401 mode, mem_mode);
12403 break;
12405 case NOT:
12406 op = DW_OP_not;
12407 goto do_unop;
12409 case ABS:
12410 op = DW_OP_abs;
12411 goto do_unop;
12413 case NEG:
12414 op = DW_OP_neg;
12415 goto do_unop;
12417 do_unop:
12418 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12419 VAR_INIT_STATUS_INITIALIZED);
12421 if (op0 == 0)
12422 break;
12424 mem_loc_result = op0;
12425 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12426 break;
12428 case CONST_INT:
12429 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12430 #ifdef POINTERS_EXTEND_UNSIGNED
12431 || (mode == Pmode
12432 && mem_mode != VOIDmode
12433 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12434 #endif
12437 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12438 break;
12440 if (!dwarf_strict
12441 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12442 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12444 dw_die_ref type_die = base_type_for_mode (mode, 1);
12445 enum machine_mode amode;
12446 if (type_die == NULL)
12447 return NULL;
12448 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12449 MODE_INT, 0);
12450 if (INTVAL (rtl) >= 0
12451 && amode != BLKmode
12452 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12453 /* const DW_OP_GNU_convert <XXX> vs.
12454 DW_OP_GNU_const_type <XXX, 1, const>. */
12455 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12456 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12458 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12459 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12460 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12461 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12462 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12463 add_loc_descr (&mem_loc_result, op0);
12464 return mem_loc_result;
12466 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12467 INTVAL (rtl));
12468 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12469 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12470 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12471 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12472 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12473 else
12475 mem_loc_result->dw_loc_oprnd2.val_class
12476 = dw_val_class_const_double;
12477 mem_loc_result->dw_loc_oprnd2.v.val_double
12478 = double_int::from_shwi (INTVAL (rtl));
12481 break;
12483 case CONST_DOUBLE:
12484 if (!dwarf_strict)
12486 dw_die_ref type_die;
12488 /* Note that a CONST_DOUBLE rtx could represent either an integer
12489 or a floating-point constant. A CONST_DOUBLE is used whenever
12490 the constant requires more than one word in order to be
12491 adequately represented. We output CONST_DOUBLEs as blocks. */
12492 if (mode == VOIDmode
12493 || (GET_MODE (rtl) == VOIDmode
12494 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12495 break;
12496 type_die = base_type_for_mode (mode,
12497 GET_MODE_CLASS (mode) == MODE_INT);
12498 if (type_die == NULL)
12499 return NULL;
12500 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12501 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12502 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12503 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12504 if (SCALAR_FLOAT_MODE_P (mode))
12506 unsigned int length = GET_MODE_SIZE (mode);
12507 unsigned char *array
12508 = (unsigned char*) ggc_alloc_atomic (length);
12510 insert_float (rtl, array);
12511 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12512 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12513 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12514 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12516 else
12518 mem_loc_result->dw_loc_oprnd2.val_class
12519 = dw_val_class_const_double;
12520 mem_loc_result->dw_loc_oprnd2.v.val_double
12521 = rtx_to_double_int (rtl);
12524 break;
12526 case EQ:
12527 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12528 break;
12530 case GE:
12531 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12532 break;
12534 case GT:
12535 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12536 break;
12538 case LE:
12539 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12540 break;
12542 case LT:
12543 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12544 break;
12546 case NE:
12547 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12548 break;
12550 case GEU:
12551 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12552 break;
12554 case GTU:
12555 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12556 break;
12558 case LEU:
12559 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12560 break;
12562 case LTU:
12563 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12564 break;
12566 case UMIN:
12567 case UMAX:
12568 if (GET_MODE_CLASS (mode) != MODE_INT)
12569 break;
12570 /* FALLTHRU */
12571 case SMIN:
12572 case SMAX:
12573 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12574 break;
12576 case ZERO_EXTRACT:
12577 case SIGN_EXTRACT:
12578 if (CONST_INT_P (XEXP (rtl, 1))
12579 && CONST_INT_P (XEXP (rtl, 2))
12580 && ((unsigned) INTVAL (XEXP (rtl, 1))
12581 + (unsigned) INTVAL (XEXP (rtl, 2))
12582 <= GET_MODE_BITSIZE (mode))
12583 && GET_MODE_CLASS (mode) == MODE_INT
12584 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12585 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12587 int shift, size;
12588 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12589 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12590 if (op0 == 0)
12591 break;
12592 if (GET_CODE (rtl) == SIGN_EXTRACT)
12593 op = DW_OP_shra;
12594 else
12595 op = DW_OP_shr;
12596 mem_loc_result = op0;
12597 size = INTVAL (XEXP (rtl, 1));
12598 shift = INTVAL (XEXP (rtl, 2));
12599 if (BITS_BIG_ENDIAN)
12600 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12601 - shift - size;
12602 if (shift + size != (int) DWARF2_ADDR_SIZE)
12604 add_loc_descr (&mem_loc_result,
12605 int_loc_descriptor (DWARF2_ADDR_SIZE
12606 - shift - size));
12607 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12609 if (size != (int) DWARF2_ADDR_SIZE)
12611 add_loc_descr (&mem_loc_result,
12612 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12613 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12616 break;
12618 case IF_THEN_ELSE:
12620 dw_loc_descr_ref op2, bra_node, drop_node;
12621 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12622 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12623 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12624 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12625 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12626 VAR_INIT_STATUS_INITIALIZED);
12627 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12628 VAR_INIT_STATUS_INITIALIZED);
12629 if (op0 == NULL || op1 == NULL || op2 == NULL)
12630 break;
12632 mem_loc_result = op1;
12633 add_loc_descr (&mem_loc_result, op2);
12634 add_loc_descr (&mem_loc_result, op0);
12635 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12636 add_loc_descr (&mem_loc_result, bra_node);
12637 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12638 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12639 add_loc_descr (&mem_loc_result, drop_node);
12640 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12641 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12643 break;
12645 case FLOAT_EXTEND:
12646 case FLOAT_TRUNCATE:
12647 case FLOAT:
12648 case UNSIGNED_FLOAT:
12649 case FIX:
12650 case UNSIGNED_FIX:
12651 if (!dwarf_strict)
12653 dw_die_ref type_die;
12654 dw_loc_descr_ref cvt;
12656 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12657 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12658 if (op0 == NULL)
12659 break;
12660 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12661 && (GET_CODE (rtl) == FLOAT
12662 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12663 <= DWARF2_ADDR_SIZE))
12665 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12666 GET_CODE (rtl) == UNSIGNED_FLOAT);
12667 if (type_die == NULL)
12668 break;
12669 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12670 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12671 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12672 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12673 add_loc_descr (&op0, cvt);
12675 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12676 if (type_die == NULL)
12677 break;
12678 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12679 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12680 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12681 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12682 add_loc_descr (&op0, cvt);
12683 if (GET_MODE_CLASS (mode) == MODE_INT
12684 && (GET_CODE (rtl) == FIX
12685 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12687 op0 = convert_descriptor_to_mode (mode, op0);
12688 if (op0 == NULL)
12689 break;
12691 mem_loc_result = op0;
12693 break;
12695 case CLZ:
12696 case CTZ:
12697 case FFS:
12698 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12699 break;
12701 case POPCOUNT:
12702 case PARITY:
12703 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12704 break;
12706 case BSWAP:
12707 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12708 break;
12710 case ROTATE:
12711 case ROTATERT:
12712 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12713 break;
12715 case COMPARE:
12716 /* In theory, we could implement the above. */
12717 /* DWARF cannot represent the unsigned compare operations
12718 natively. */
12719 case SS_MULT:
12720 case US_MULT:
12721 case SS_DIV:
12722 case US_DIV:
12723 case SS_PLUS:
12724 case US_PLUS:
12725 case SS_MINUS:
12726 case US_MINUS:
12727 case SS_NEG:
12728 case US_NEG:
12729 case SS_ABS:
12730 case SS_ASHIFT:
12731 case US_ASHIFT:
12732 case SS_TRUNCATE:
12733 case US_TRUNCATE:
12734 case UNORDERED:
12735 case ORDERED:
12736 case UNEQ:
12737 case UNGE:
12738 case UNGT:
12739 case UNLE:
12740 case UNLT:
12741 case LTGT:
12742 case FRACT_CONVERT:
12743 case UNSIGNED_FRACT_CONVERT:
12744 case SAT_FRACT:
12745 case UNSIGNED_SAT_FRACT:
12746 case SQRT:
12747 case ASM_OPERANDS:
12748 case VEC_MERGE:
12749 case VEC_SELECT:
12750 case VEC_CONCAT:
12751 case VEC_DUPLICATE:
12752 case UNSPEC:
12753 case HIGH:
12754 case FMA:
12755 case STRICT_LOW_PART:
12756 case CONST_VECTOR:
12757 case CONST_FIXED:
12758 case CLRSB:
12759 case CLOBBER:
12760 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12761 can't express it in the debug info. This can happen e.g. with some
12762 TLS UNSPECs. */
12763 break;
12765 case CONST_STRING:
12766 resolve_one_addr (&rtl, NULL);
12767 goto symref;
12769 default:
12770 #ifdef ENABLE_CHECKING
12771 print_rtl (stderr, rtl);
12772 gcc_unreachable ();
12773 #else
12774 break;
12775 #endif
12778 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12779 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12781 return mem_loc_result;
12784 /* Return a descriptor that describes the concatenation of two locations.
12785 This is typically a complex variable. */
12787 static dw_loc_descr_ref
12788 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12790 dw_loc_descr_ref cc_loc_result = NULL;
12791 dw_loc_descr_ref x0_ref
12792 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12793 dw_loc_descr_ref x1_ref
12794 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12796 if (x0_ref == 0 || x1_ref == 0)
12797 return 0;
12799 cc_loc_result = x0_ref;
12800 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12802 add_loc_descr (&cc_loc_result, x1_ref);
12803 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12805 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12806 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12808 return cc_loc_result;
12811 /* Return a descriptor that describes the concatenation of N
12812 locations. */
12814 static dw_loc_descr_ref
12815 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12817 unsigned int i;
12818 dw_loc_descr_ref cc_loc_result = NULL;
12819 unsigned int n = XVECLEN (concatn, 0);
12821 for (i = 0; i < n; ++i)
12823 dw_loc_descr_ref ref;
12824 rtx x = XVECEXP (concatn, 0, i);
12826 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12827 if (ref == NULL)
12828 return NULL;
12830 add_loc_descr (&cc_loc_result, ref);
12831 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12834 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12835 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12837 return cc_loc_result;
12840 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12841 for DEBUG_IMPLICIT_PTR RTL. */
12843 static dw_loc_descr_ref
12844 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12846 dw_loc_descr_ref ret;
12847 dw_die_ref ref;
12849 if (dwarf_strict)
12850 return NULL;
12851 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12852 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12853 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12854 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12855 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12856 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12857 if (ref)
12859 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12860 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12861 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12863 else
12865 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12866 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12868 return ret;
12871 /* Output a proper Dwarf location descriptor for a variable or parameter
12872 which is either allocated in a register or in a memory location. For a
12873 register, we just generate an OP_REG and the register number. For a
12874 memory location we provide a Dwarf postfix expression describing how to
12875 generate the (dynamic) address of the object onto the address stack.
12877 MODE is mode of the decl if this loc_descriptor is going to be used in
12878 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12879 allowed, VOIDmode otherwise.
12881 If we don't know how to describe it, return 0. */
12883 static dw_loc_descr_ref
12884 loc_descriptor (rtx rtl, enum machine_mode mode,
12885 enum var_init_status initialized)
12887 dw_loc_descr_ref loc_result = NULL;
12889 switch (GET_CODE (rtl))
12891 case SUBREG:
12892 /* The case of a subreg may arise when we have a local (register)
12893 variable or a formal (register) parameter which doesn't quite fill
12894 up an entire register. For now, just assume that it is
12895 legitimate to make the Dwarf info refer to the whole register which
12896 contains the given subreg. */
12897 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12898 loc_result = loc_descriptor (SUBREG_REG (rtl),
12899 GET_MODE (SUBREG_REG (rtl)), initialized);
12900 else
12901 goto do_default;
12902 break;
12904 case REG:
12905 loc_result = reg_loc_descriptor (rtl, initialized);
12906 break;
12908 case MEM:
12909 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12910 GET_MODE (rtl), initialized);
12911 if (loc_result == NULL)
12912 loc_result = tls_mem_loc_descriptor (rtl);
12913 if (loc_result == NULL)
12915 rtx new_rtl = avoid_constant_pool_reference (rtl);
12916 if (new_rtl != rtl)
12917 loc_result = loc_descriptor (new_rtl, mode, initialized);
12919 break;
12921 case CONCAT:
12922 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12923 initialized);
12924 break;
12926 case CONCATN:
12927 loc_result = concatn_loc_descriptor (rtl, initialized);
12928 break;
12930 case VAR_LOCATION:
12931 /* Single part. */
12932 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12934 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12935 if (GET_CODE (loc) == EXPR_LIST)
12936 loc = XEXP (loc, 0);
12937 loc_result = loc_descriptor (loc, mode, initialized);
12938 break;
12941 rtl = XEXP (rtl, 1);
12942 /* FALLTHRU */
12944 case PARALLEL:
12946 rtvec par_elems = XVEC (rtl, 0);
12947 int num_elem = GET_NUM_ELEM (par_elems);
12948 enum machine_mode mode;
12949 int i;
12951 /* Create the first one, so we have something to add to. */
12952 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12953 VOIDmode, initialized);
12954 if (loc_result == NULL)
12955 return NULL;
12956 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12957 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12958 for (i = 1; i < num_elem; i++)
12960 dw_loc_descr_ref temp;
12962 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12963 VOIDmode, initialized);
12964 if (temp == NULL)
12965 return NULL;
12966 add_loc_descr (&loc_result, temp);
12967 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12968 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12971 break;
12973 case CONST_INT:
12974 if (mode != VOIDmode && mode != BLKmode)
12975 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12976 INTVAL (rtl));
12977 break;
12979 case CONST_DOUBLE:
12980 if (mode == VOIDmode)
12981 mode = GET_MODE (rtl);
12983 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12985 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12987 /* Note that a CONST_DOUBLE rtx could represent either an integer
12988 or a floating-point constant. A CONST_DOUBLE is used whenever
12989 the constant requires more than one word in order to be
12990 adequately represented. We output CONST_DOUBLEs as blocks. */
12991 loc_result = new_loc_descr (DW_OP_implicit_value,
12992 GET_MODE_SIZE (mode), 0);
12993 if (SCALAR_FLOAT_MODE_P (mode))
12995 unsigned int length = GET_MODE_SIZE (mode);
12996 unsigned char *array
12997 = (unsigned char*) ggc_alloc_atomic (length);
12999 insert_float (rtl, array);
13000 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13001 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13002 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13003 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13005 else
13007 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13008 loc_result->dw_loc_oprnd2.v.val_double
13009 = rtx_to_double_int (rtl);
13012 break;
13014 case CONST_VECTOR:
13015 if (mode == VOIDmode)
13016 mode = GET_MODE (rtl);
13018 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13020 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13021 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13022 unsigned char *array = (unsigned char *)
13023 ggc_alloc_atomic (length * elt_size);
13024 unsigned int i;
13025 unsigned char *p;
13027 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13028 switch (GET_MODE_CLASS (mode))
13030 case MODE_VECTOR_INT:
13031 for (i = 0, p = array; i < length; i++, p += elt_size)
13033 rtx elt = CONST_VECTOR_ELT (rtl, i);
13034 double_int val = rtx_to_double_int (elt);
13036 if (elt_size <= sizeof (HOST_WIDE_INT))
13037 insert_int (val.to_shwi (), elt_size, p);
13038 else
13040 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13041 insert_double (val, p);
13044 break;
13046 case MODE_VECTOR_FLOAT:
13047 for (i = 0, p = array; i < length; i++, p += elt_size)
13049 rtx elt = CONST_VECTOR_ELT (rtl, i);
13050 insert_float (elt, p);
13052 break;
13054 default:
13055 gcc_unreachable ();
13058 loc_result = new_loc_descr (DW_OP_implicit_value,
13059 length * elt_size, 0);
13060 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13061 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13062 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13063 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13065 break;
13067 case CONST:
13068 if (mode == VOIDmode
13069 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13070 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13071 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13073 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13074 break;
13076 /* FALLTHROUGH */
13077 case SYMBOL_REF:
13078 if (!const_ok_for_output (rtl))
13079 break;
13080 case LABEL_REF:
13081 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13082 && (dwarf_version >= 4 || !dwarf_strict))
13084 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13085 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13086 vec_safe_push (used_rtx_array, rtl);
13088 break;
13090 case DEBUG_IMPLICIT_PTR:
13091 loc_result = implicit_ptr_descriptor (rtl, 0);
13092 break;
13094 case PLUS:
13095 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13096 && CONST_INT_P (XEXP (rtl, 1)))
13098 loc_result
13099 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13100 break;
13102 /* FALLTHRU */
13103 do_default:
13104 default:
13105 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13106 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13107 && dwarf_version >= 4)
13108 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13110 /* Value expression. */
13111 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13112 if (loc_result)
13113 add_loc_descr (&loc_result,
13114 new_loc_descr (DW_OP_stack_value, 0, 0));
13116 break;
13119 return loc_result;
13122 /* We need to figure out what section we should use as the base for the
13123 address ranges where a given location is valid.
13124 1. If this particular DECL has a section associated with it, use that.
13125 2. If this function has a section associated with it, use that.
13126 3. Otherwise, use the text section.
13127 XXX: If you split a variable across multiple sections, we won't notice. */
13129 static const char *
13130 secname_for_decl (const_tree decl)
13132 const char *secname;
13134 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13136 tree sectree = DECL_SECTION_NAME (decl);
13137 secname = TREE_STRING_POINTER (sectree);
13139 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13141 tree sectree = DECL_SECTION_NAME (current_function_decl);
13142 secname = TREE_STRING_POINTER (sectree);
13144 else if (cfun && in_cold_section_p)
13145 secname = crtl->subsections.cold_section_label;
13146 else
13147 secname = text_section_label;
13149 return secname;
13152 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13154 static bool
13155 decl_by_reference_p (tree decl)
13157 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13158 || TREE_CODE (decl) == VAR_DECL)
13159 && DECL_BY_REFERENCE (decl));
13162 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13163 for VARLOC. */
13165 static dw_loc_descr_ref
13166 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13167 enum var_init_status initialized)
13169 int have_address = 0;
13170 dw_loc_descr_ref descr;
13171 enum machine_mode mode;
13173 if (want_address != 2)
13175 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13176 /* Single part. */
13177 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13179 varloc = PAT_VAR_LOCATION_LOC (varloc);
13180 if (GET_CODE (varloc) == EXPR_LIST)
13181 varloc = XEXP (varloc, 0);
13182 mode = GET_MODE (varloc);
13183 if (MEM_P (varloc))
13185 rtx addr = XEXP (varloc, 0);
13186 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13187 mode, initialized);
13188 if (descr)
13189 have_address = 1;
13190 else
13192 rtx x = avoid_constant_pool_reference (varloc);
13193 if (x != varloc)
13194 descr = mem_loc_descriptor (x, mode, VOIDmode,
13195 initialized);
13198 else
13199 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13201 else
13202 return 0;
13204 else
13206 if (GET_CODE (varloc) == VAR_LOCATION)
13207 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13208 else
13209 mode = DECL_MODE (loc);
13210 descr = loc_descriptor (varloc, mode, initialized);
13211 have_address = 1;
13214 if (!descr)
13215 return 0;
13217 if (want_address == 2 && !have_address
13218 && (dwarf_version >= 4 || !dwarf_strict))
13220 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13222 expansion_failed (loc, NULL_RTX,
13223 "DWARF address size mismatch");
13224 return 0;
13226 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13227 have_address = 1;
13229 /* Show if we can't fill the request for an address. */
13230 if (want_address && !have_address)
13232 expansion_failed (loc, NULL_RTX,
13233 "Want address and only have value");
13234 return 0;
13237 /* If we've got an address and don't want one, dereference. */
13238 if (!want_address && have_address)
13240 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13241 enum dwarf_location_atom op;
13243 if (size > DWARF2_ADDR_SIZE || size == -1)
13245 expansion_failed (loc, NULL_RTX,
13246 "DWARF address size mismatch");
13247 return 0;
13249 else if (size == DWARF2_ADDR_SIZE)
13250 op = DW_OP_deref;
13251 else
13252 op = DW_OP_deref_size;
13254 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13257 return descr;
13260 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13261 if it is not possible. */
13263 static dw_loc_descr_ref
13264 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13266 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13267 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13268 else if (dwarf_version >= 3 || !dwarf_strict)
13269 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13270 else
13271 return NULL;
13274 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13275 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13277 static dw_loc_descr_ref
13278 dw_sra_loc_expr (tree decl, rtx loc)
13280 rtx p;
13281 unsigned int padsize = 0;
13282 dw_loc_descr_ref descr, *descr_tail;
13283 unsigned HOST_WIDE_INT decl_size;
13284 rtx varloc;
13285 enum var_init_status initialized;
13287 if (DECL_SIZE (decl) == NULL
13288 || !host_integerp (DECL_SIZE (decl), 1))
13289 return NULL;
13291 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13292 descr = NULL;
13293 descr_tail = &descr;
13295 for (p = loc; p; p = XEXP (p, 1))
13297 unsigned int bitsize = decl_piece_bitsize (p);
13298 rtx loc_note = *decl_piece_varloc_ptr (p);
13299 dw_loc_descr_ref cur_descr;
13300 dw_loc_descr_ref *tail, last = NULL;
13301 unsigned int opsize = 0;
13303 if (loc_note == NULL_RTX
13304 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13306 padsize += bitsize;
13307 continue;
13309 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13310 varloc = NOTE_VAR_LOCATION (loc_note);
13311 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13312 if (cur_descr == NULL)
13314 padsize += bitsize;
13315 continue;
13318 /* Check that cur_descr either doesn't use
13319 DW_OP_*piece operations, or their sum is equal
13320 to bitsize. Otherwise we can't embed it. */
13321 for (tail = &cur_descr; *tail != NULL;
13322 tail = &(*tail)->dw_loc_next)
13323 if ((*tail)->dw_loc_opc == DW_OP_piece)
13325 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13326 * BITS_PER_UNIT;
13327 last = *tail;
13329 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13331 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13332 last = *tail;
13335 if (last != NULL && opsize != bitsize)
13337 padsize += bitsize;
13338 continue;
13341 /* If there is a hole, add DW_OP_*piece after empty DWARF
13342 expression, which means that those bits are optimized out. */
13343 if (padsize)
13345 if (padsize > decl_size)
13346 return NULL;
13347 decl_size -= padsize;
13348 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13349 if (*descr_tail == NULL)
13350 return NULL;
13351 descr_tail = &(*descr_tail)->dw_loc_next;
13352 padsize = 0;
13354 *descr_tail = cur_descr;
13355 descr_tail = tail;
13356 if (bitsize > decl_size)
13357 return NULL;
13358 decl_size -= bitsize;
13359 if (last == NULL)
13361 HOST_WIDE_INT offset = 0;
13362 if (GET_CODE (varloc) == VAR_LOCATION
13363 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13365 varloc = PAT_VAR_LOCATION_LOC (varloc);
13366 if (GET_CODE (varloc) == EXPR_LIST)
13367 varloc = XEXP (varloc, 0);
13371 if (GET_CODE (varloc) == CONST
13372 || GET_CODE (varloc) == SIGN_EXTEND
13373 || GET_CODE (varloc) == ZERO_EXTEND)
13374 varloc = XEXP (varloc, 0);
13375 else if (GET_CODE (varloc) == SUBREG)
13376 varloc = SUBREG_REG (varloc);
13377 else
13378 break;
13380 while (1);
13381 /* DW_OP_bit_size offset should be zero for register
13382 or implicit location descriptions and empty location
13383 descriptions, but for memory addresses needs big endian
13384 adjustment. */
13385 if (MEM_P (varloc))
13387 unsigned HOST_WIDE_INT memsize
13388 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13389 if (memsize != bitsize)
13391 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13392 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13393 return NULL;
13394 if (memsize < bitsize)
13395 return NULL;
13396 if (BITS_BIG_ENDIAN)
13397 offset = memsize - bitsize;
13401 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13402 if (*descr_tail == NULL)
13403 return NULL;
13404 descr_tail = &(*descr_tail)->dw_loc_next;
13408 /* If there were any non-empty expressions, add padding till the end of
13409 the decl. */
13410 if (descr != NULL && decl_size != 0)
13412 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13413 if (*descr_tail == NULL)
13414 return NULL;
13416 return descr;
13419 /* Return the dwarf representation of the location list LOC_LIST of
13420 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13421 function. */
13423 static dw_loc_list_ref
13424 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13426 const char *endname, *secname;
13427 rtx varloc;
13428 enum var_init_status initialized;
13429 struct var_loc_node *node;
13430 dw_loc_descr_ref descr;
13431 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13432 dw_loc_list_ref list = NULL;
13433 dw_loc_list_ref *listp = &list;
13435 /* Now that we know what section we are using for a base,
13436 actually construct the list of locations.
13437 The first location information is what is passed to the
13438 function that creates the location list, and the remaining
13439 locations just get added on to that list.
13440 Note that we only know the start address for a location
13441 (IE location changes), so to build the range, we use
13442 the range [current location start, next location start].
13443 This means we have to special case the last node, and generate
13444 a range of [last location start, end of function label]. */
13446 secname = secname_for_decl (decl);
13448 for (node = loc_list->first; node; node = node->next)
13449 if (GET_CODE (node->loc) == EXPR_LIST
13450 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13452 if (GET_CODE (node->loc) == EXPR_LIST)
13454 /* This requires DW_OP_{,bit_}piece, which is not usable
13455 inside DWARF expressions. */
13456 if (want_address != 2)
13457 continue;
13458 descr = dw_sra_loc_expr (decl, node->loc);
13459 if (descr == NULL)
13460 continue;
13462 else
13464 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13465 varloc = NOTE_VAR_LOCATION (node->loc);
13466 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13468 if (descr)
13470 bool range_across_switch = false;
13471 /* If section switch happens in between node->label
13472 and node->next->label (or end of function) and
13473 we can't emit it as a single entry list,
13474 emit two ranges, first one ending at the end
13475 of first partition and second one starting at the
13476 beginning of second partition. */
13477 if (node == loc_list->last_before_switch
13478 && (node != loc_list->first || loc_list->first->next)
13479 && current_function_decl)
13481 endname = cfun->fde->dw_fde_end;
13482 range_across_switch = true;
13484 /* The variable has a location between NODE->LABEL and
13485 NODE->NEXT->LABEL. */
13486 else if (node->next)
13487 endname = node->next->label;
13488 /* If the variable has a location at the last label
13489 it keeps its location until the end of function. */
13490 else if (!current_function_decl)
13491 endname = text_end_label;
13492 else
13494 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13495 current_function_funcdef_no);
13496 endname = ggc_strdup (label_id);
13499 *listp = new_loc_list (descr, node->label, endname, secname);
13500 if (TREE_CODE (decl) == PARM_DECL
13501 && node == loc_list->first
13502 && GET_CODE (node->loc) == NOTE
13503 && strcmp (node->label, endname) == 0)
13504 (*listp)->force = true;
13505 listp = &(*listp)->dw_loc_next;
13507 if (range_across_switch)
13509 if (GET_CODE (node->loc) == EXPR_LIST)
13510 descr = dw_sra_loc_expr (decl, node->loc);
13511 else
13513 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13514 varloc = NOTE_VAR_LOCATION (node->loc);
13515 descr = dw_loc_list_1 (decl, varloc, want_address,
13516 initialized);
13518 gcc_assert (descr);
13519 /* The variable has a location between NODE->LABEL and
13520 NODE->NEXT->LABEL. */
13521 if (node->next)
13522 endname = node->next->label;
13523 else
13524 endname = cfun->fde->dw_fde_second_end;
13525 *listp = new_loc_list (descr,
13526 cfun->fde->dw_fde_second_begin,
13527 endname, secname);
13528 listp = &(*listp)->dw_loc_next;
13533 /* Try to avoid the overhead of a location list emitting a location
13534 expression instead, but only if we didn't have more than one
13535 location entry in the first place. If some entries were not
13536 representable, we don't want to pretend a single entry that was
13537 applies to the entire scope in which the variable is
13538 available. */
13539 if (list && loc_list->first->next)
13540 gen_llsym (list);
13542 return list;
13545 /* Return if the loc_list has only single element and thus can be represented
13546 as location description. */
13548 static bool
13549 single_element_loc_list_p (dw_loc_list_ref list)
13551 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13552 return !list->ll_symbol;
13555 /* To each location in list LIST add loc descr REF. */
13557 static void
13558 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13560 dw_loc_descr_ref copy;
13561 add_loc_descr (&list->expr, ref);
13562 list = list->dw_loc_next;
13563 while (list)
13565 copy = ggc_alloc_dw_loc_descr_node ();
13566 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13567 add_loc_descr (&list->expr, copy);
13568 while (copy->dw_loc_next)
13570 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13571 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13572 copy->dw_loc_next = new_copy;
13573 copy = new_copy;
13575 list = list->dw_loc_next;
13579 /* Given two lists RET and LIST
13580 produce location list that is result of adding expression in LIST
13581 to expression in RET on each position in program.
13582 Might be destructive on both RET and LIST.
13584 TODO: We handle only simple cases of RET or LIST having at most one
13585 element. General case would inolve sorting the lists in program order
13586 and merging them that will need some additional work.
13587 Adding that will improve quality of debug info especially for SRA-ed
13588 structures. */
13590 static void
13591 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13593 if (!list)
13594 return;
13595 if (!*ret)
13597 *ret = list;
13598 return;
13600 if (!list->dw_loc_next)
13602 add_loc_descr_to_each (*ret, list->expr);
13603 return;
13605 if (!(*ret)->dw_loc_next)
13607 add_loc_descr_to_each (list, (*ret)->expr);
13608 *ret = list;
13609 return;
13611 expansion_failed (NULL_TREE, NULL_RTX,
13612 "Don't know how to merge two non-trivial"
13613 " location lists.\n");
13614 *ret = NULL;
13615 return;
13618 /* LOC is constant expression. Try a luck, look it up in constant
13619 pool and return its loc_descr of its address. */
13621 static dw_loc_descr_ref
13622 cst_pool_loc_descr (tree loc)
13624 /* Get an RTL for this, if something has been emitted. */
13625 rtx rtl = lookup_constant_def (loc);
13627 if (!rtl || !MEM_P (rtl))
13629 gcc_assert (!rtl);
13630 return 0;
13632 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13634 /* TODO: We might get more coverage if we was actually delaying expansion
13635 of all expressions till end of compilation when constant pools are fully
13636 populated. */
13637 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13639 expansion_failed (loc, NULL_RTX,
13640 "CST value in contant pool but not marked.");
13641 return 0;
13643 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13644 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13647 /* Return dw_loc_list representing address of addr_expr LOC
13648 by looking for inner INDIRECT_REF expression and turning
13649 it into simple arithmetics. */
13651 static dw_loc_list_ref
13652 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13654 tree obj, offset;
13655 HOST_WIDE_INT bitsize, bitpos, bytepos;
13656 enum machine_mode mode;
13657 int unsignedp, volatilep = 0;
13658 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13660 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13661 &bitsize, &bitpos, &offset, &mode,
13662 &unsignedp, &volatilep, false);
13663 STRIP_NOPS (obj);
13664 if (bitpos % BITS_PER_UNIT)
13666 expansion_failed (loc, NULL_RTX, "bitfield access");
13667 return 0;
13669 if (!INDIRECT_REF_P (obj))
13671 expansion_failed (obj,
13672 NULL_RTX, "no indirect ref in inner refrence");
13673 return 0;
13675 if (!offset && !bitpos)
13676 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13677 else if (toplev
13678 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13679 && (dwarf_version >= 4 || !dwarf_strict))
13681 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13682 if (!list_ret)
13683 return 0;
13684 if (offset)
13686 /* Variable offset. */
13687 list_ret1 = loc_list_from_tree (offset, 0);
13688 if (list_ret1 == 0)
13689 return 0;
13690 add_loc_list (&list_ret, list_ret1);
13691 if (!list_ret)
13692 return 0;
13693 add_loc_descr_to_each (list_ret,
13694 new_loc_descr (DW_OP_plus, 0, 0));
13696 bytepos = bitpos / BITS_PER_UNIT;
13697 if (bytepos > 0)
13698 add_loc_descr_to_each (list_ret,
13699 new_loc_descr (DW_OP_plus_uconst,
13700 bytepos, 0));
13701 else if (bytepos < 0)
13702 loc_list_plus_const (list_ret, bytepos);
13703 add_loc_descr_to_each (list_ret,
13704 new_loc_descr (DW_OP_stack_value, 0, 0));
13706 return list_ret;
13710 /* Generate Dwarf location list representing LOC.
13711 If WANT_ADDRESS is false, expression computing LOC will be computed
13712 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13713 if WANT_ADDRESS is 2, expression computing address useable in location
13714 will be returned (i.e. DW_OP_reg can be used
13715 to refer to register values). */
13717 static dw_loc_list_ref
13718 loc_list_from_tree (tree loc, int want_address)
13720 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13721 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13722 int have_address = 0;
13723 enum dwarf_location_atom op;
13725 /* ??? Most of the time we do not take proper care for sign/zero
13726 extending the values properly. Hopefully this won't be a real
13727 problem... */
13729 switch (TREE_CODE (loc))
13731 case ERROR_MARK:
13732 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13733 return 0;
13735 case PLACEHOLDER_EXPR:
13736 /* This case involves extracting fields from an object to determine the
13737 position of other fields. We don't try to encode this here. The
13738 only user of this is Ada, which encodes the needed information using
13739 the names of types. */
13740 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13741 return 0;
13743 case CALL_EXPR:
13744 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13745 /* There are no opcodes for these operations. */
13746 return 0;
13748 case PREINCREMENT_EXPR:
13749 case PREDECREMENT_EXPR:
13750 case POSTINCREMENT_EXPR:
13751 case POSTDECREMENT_EXPR:
13752 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13753 /* There are no opcodes for these operations. */
13754 return 0;
13756 case ADDR_EXPR:
13757 /* If we already want an address, see if there is INDIRECT_REF inside
13758 e.g. for &this->field. */
13759 if (want_address)
13761 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13762 (loc, want_address == 2);
13763 if (list_ret)
13764 have_address = 1;
13765 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13766 && (ret = cst_pool_loc_descr (loc)))
13767 have_address = 1;
13769 /* Otherwise, process the argument and look for the address. */
13770 if (!list_ret && !ret)
13771 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13772 else
13774 if (want_address)
13775 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13776 return NULL;
13778 break;
13780 case VAR_DECL:
13781 if (DECL_THREAD_LOCAL_P (loc))
13783 rtx rtl;
13784 enum dwarf_location_atom tls_op;
13785 enum dtprel_bool dtprel = dtprel_false;
13787 if (targetm.have_tls)
13789 /* If this is not defined, we have no way to emit the
13790 data. */
13791 if (!targetm.asm_out.output_dwarf_dtprel)
13792 return 0;
13794 /* The way DW_OP_GNU_push_tls_address is specified, we
13795 can only look up addresses of objects in the current
13796 module. We used DW_OP_addr as first op, but that's
13797 wrong, because DW_OP_addr is relocated by the debug
13798 info consumer, while DW_OP_GNU_push_tls_address
13799 operand shouldn't be. */
13800 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13801 return 0;
13802 dtprel = dtprel_true;
13803 tls_op = DW_OP_GNU_push_tls_address;
13805 else
13807 if (!targetm.emutls.debug_form_tls_address
13808 || !(dwarf_version >= 3 || !dwarf_strict))
13809 return 0;
13810 /* We stuffed the control variable into the DECL_VALUE_EXPR
13811 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13812 no longer appear in gimple code. We used the control
13813 variable in specific so that we could pick it up here. */
13814 loc = DECL_VALUE_EXPR (loc);
13815 tls_op = DW_OP_form_tls_address;
13818 rtl = rtl_for_decl_location (loc);
13819 if (rtl == NULL_RTX)
13820 return 0;
13822 if (!MEM_P (rtl))
13823 return 0;
13824 rtl = XEXP (rtl, 0);
13825 if (! CONSTANT_P (rtl))
13826 return 0;
13828 ret = new_addr_loc_descr (rtl, dtprel);
13829 ret1 = new_loc_descr (tls_op, 0, 0);
13830 add_loc_descr (&ret, ret1);
13832 have_address = 1;
13833 break;
13835 /* FALLTHRU */
13837 case PARM_DECL:
13838 case RESULT_DECL:
13839 if (DECL_HAS_VALUE_EXPR_P (loc))
13840 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13841 want_address);
13842 /* FALLTHRU */
13844 case FUNCTION_DECL:
13846 rtx rtl;
13847 var_loc_list *loc_list = lookup_decl_loc (loc);
13849 if (loc_list && loc_list->first)
13851 list_ret = dw_loc_list (loc_list, loc, want_address);
13852 have_address = want_address != 0;
13853 break;
13855 rtl = rtl_for_decl_location (loc);
13856 if (rtl == NULL_RTX)
13858 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13859 return 0;
13861 else if (CONST_INT_P (rtl))
13863 HOST_WIDE_INT val = INTVAL (rtl);
13864 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13865 val &= GET_MODE_MASK (DECL_MODE (loc));
13866 ret = int_loc_descriptor (val);
13868 else if (GET_CODE (rtl) == CONST_STRING)
13870 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13871 return 0;
13873 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13874 ret = new_addr_loc_descr (rtl, dtprel_false);
13875 else
13877 enum machine_mode mode, mem_mode;
13879 /* Certain constructs can only be represented at top-level. */
13880 if (want_address == 2)
13882 ret = loc_descriptor (rtl, VOIDmode,
13883 VAR_INIT_STATUS_INITIALIZED);
13884 have_address = 1;
13886 else
13888 mode = GET_MODE (rtl);
13889 mem_mode = VOIDmode;
13890 if (MEM_P (rtl))
13892 mem_mode = mode;
13893 mode = get_address_mode (rtl);
13894 rtl = XEXP (rtl, 0);
13895 have_address = 1;
13897 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13898 VAR_INIT_STATUS_INITIALIZED);
13900 if (!ret)
13901 expansion_failed (loc, rtl,
13902 "failed to produce loc descriptor for rtl");
13905 break;
13907 case MEM_REF:
13908 /* ??? FIXME. */
13909 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13910 return 0;
13911 /* Fallthru. */
13912 case INDIRECT_REF:
13913 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13914 have_address = 1;
13915 break;
13917 case COMPOUND_EXPR:
13918 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13920 CASE_CONVERT:
13921 case VIEW_CONVERT_EXPR:
13922 case SAVE_EXPR:
13923 case MODIFY_EXPR:
13924 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13926 case COMPONENT_REF:
13927 case BIT_FIELD_REF:
13928 case ARRAY_REF:
13929 case ARRAY_RANGE_REF:
13930 case REALPART_EXPR:
13931 case IMAGPART_EXPR:
13933 tree obj, offset;
13934 HOST_WIDE_INT bitsize, bitpos, bytepos;
13935 enum machine_mode mode;
13936 int unsignedp, volatilep = 0;
13938 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13939 &unsignedp, &volatilep, false);
13941 gcc_assert (obj != loc);
13943 list_ret = loc_list_from_tree (obj,
13944 want_address == 2
13945 && !bitpos && !offset ? 2 : 1);
13946 /* TODO: We can extract value of the small expression via shifting even
13947 for nonzero bitpos. */
13948 if (list_ret == 0)
13949 return 0;
13950 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13952 expansion_failed (loc, NULL_RTX,
13953 "bitfield access");
13954 return 0;
13957 if (offset != NULL_TREE)
13959 /* Variable offset. */
13960 list_ret1 = loc_list_from_tree (offset, 0);
13961 if (list_ret1 == 0)
13962 return 0;
13963 add_loc_list (&list_ret, list_ret1);
13964 if (!list_ret)
13965 return 0;
13966 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13969 bytepos = bitpos / BITS_PER_UNIT;
13970 if (bytepos > 0)
13971 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13972 else if (bytepos < 0)
13973 loc_list_plus_const (list_ret, bytepos);
13975 have_address = 1;
13976 break;
13979 case INTEGER_CST:
13980 if ((want_address || !host_integerp (loc, 0))
13981 && (ret = cst_pool_loc_descr (loc)))
13982 have_address = 1;
13983 else if (want_address == 2
13984 && host_integerp (loc, 0)
13985 && (ret = address_of_int_loc_descriptor
13986 (int_size_in_bytes (TREE_TYPE (loc)),
13987 tree_low_cst (loc, 0))))
13988 have_address = 1;
13989 else if (host_integerp (loc, 0))
13990 ret = int_loc_descriptor (tree_low_cst (loc, 0));
13991 else
13993 expansion_failed (loc, NULL_RTX,
13994 "Integer operand is not host integer");
13995 return 0;
13997 break;
13999 case CONSTRUCTOR:
14000 case REAL_CST:
14001 case STRING_CST:
14002 case COMPLEX_CST:
14003 if ((ret = cst_pool_loc_descr (loc)))
14004 have_address = 1;
14005 else
14006 /* We can construct small constants here using int_loc_descriptor. */
14007 expansion_failed (loc, NULL_RTX,
14008 "constructor or constant not in constant pool");
14009 break;
14011 case TRUTH_AND_EXPR:
14012 case TRUTH_ANDIF_EXPR:
14013 case BIT_AND_EXPR:
14014 op = DW_OP_and;
14015 goto do_binop;
14017 case TRUTH_XOR_EXPR:
14018 case BIT_XOR_EXPR:
14019 op = DW_OP_xor;
14020 goto do_binop;
14022 case TRUTH_OR_EXPR:
14023 case TRUTH_ORIF_EXPR:
14024 case BIT_IOR_EXPR:
14025 op = DW_OP_or;
14026 goto do_binop;
14028 case FLOOR_DIV_EXPR:
14029 case CEIL_DIV_EXPR:
14030 case ROUND_DIV_EXPR:
14031 case TRUNC_DIV_EXPR:
14032 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14033 return 0;
14034 op = DW_OP_div;
14035 goto do_binop;
14037 case MINUS_EXPR:
14038 op = DW_OP_minus;
14039 goto do_binop;
14041 case FLOOR_MOD_EXPR:
14042 case CEIL_MOD_EXPR:
14043 case ROUND_MOD_EXPR:
14044 case TRUNC_MOD_EXPR:
14045 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14047 op = DW_OP_mod;
14048 goto do_binop;
14050 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14051 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14052 if (list_ret == 0 || list_ret1 == 0)
14053 return 0;
14055 add_loc_list (&list_ret, list_ret1);
14056 if (list_ret == 0)
14057 return 0;
14058 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14059 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14060 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14061 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14062 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14063 break;
14065 case MULT_EXPR:
14066 op = DW_OP_mul;
14067 goto do_binop;
14069 case LSHIFT_EXPR:
14070 op = DW_OP_shl;
14071 goto do_binop;
14073 case RSHIFT_EXPR:
14074 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14075 goto do_binop;
14077 case POINTER_PLUS_EXPR:
14078 case PLUS_EXPR:
14079 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14081 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14082 if (list_ret == 0)
14083 return 0;
14085 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14086 break;
14089 op = DW_OP_plus;
14090 goto do_binop;
14092 case LE_EXPR:
14093 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14094 return 0;
14096 op = DW_OP_le;
14097 goto do_binop;
14099 case GE_EXPR:
14100 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14101 return 0;
14103 op = DW_OP_ge;
14104 goto do_binop;
14106 case LT_EXPR:
14107 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14108 return 0;
14110 op = DW_OP_lt;
14111 goto do_binop;
14113 case GT_EXPR:
14114 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14115 return 0;
14117 op = DW_OP_gt;
14118 goto do_binop;
14120 case EQ_EXPR:
14121 op = DW_OP_eq;
14122 goto do_binop;
14124 case NE_EXPR:
14125 op = DW_OP_ne;
14126 goto do_binop;
14128 do_binop:
14129 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14130 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14131 if (list_ret == 0 || list_ret1 == 0)
14132 return 0;
14134 add_loc_list (&list_ret, list_ret1);
14135 if (list_ret == 0)
14136 return 0;
14137 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14138 break;
14140 case TRUTH_NOT_EXPR:
14141 case BIT_NOT_EXPR:
14142 op = DW_OP_not;
14143 goto do_unop;
14145 case ABS_EXPR:
14146 op = DW_OP_abs;
14147 goto do_unop;
14149 case NEGATE_EXPR:
14150 op = DW_OP_neg;
14151 goto do_unop;
14153 do_unop:
14154 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14155 if (list_ret == 0)
14156 return 0;
14158 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14159 break;
14161 case MIN_EXPR:
14162 case MAX_EXPR:
14164 const enum tree_code code =
14165 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14167 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14168 build2 (code, integer_type_node,
14169 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14170 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14173 /* ... fall through ... */
14175 case COND_EXPR:
14177 dw_loc_descr_ref lhs
14178 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14179 dw_loc_list_ref rhs
14180 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14181 dw_loc_descr_ref bra_node, jump_node, tmp;
14183 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14184 if (list_ret == 0 || lhs == 0 || rhs == 0)
14185 return 0;
14187 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14188 add_loc_descr_to_each (list_ret, bra_node);
14190 add_loc_list (&list_ret, rhs);
14191 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14192 add_loc_descr_to_each (list_ret, jump_node);
14194 add_loc_descr_to_each (list_ret, lhs);
14195 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14196 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14198 /* ??? Need a node to point the skip at. Use a nop. */
14199 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14200 add_loc_descr_to_each (list_ret, tmp);
14201 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14202 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14204 break;
14206 case FIX_TRUNC_EXPR:
14207 return 0;
14209 default:
14210 /* Leave front-end specific codes as simply unknown. This comes
14211 up, for instance, with the C STMT_EXPR. */
14212 if ((unsigned int) TREE_CODE (loc)
14213 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14215 expansion_failed (loc, NULL_RTX,
14216 "language specific tree node");
14217 return 0;
14220 #ifdef ENABLE_CHECKING
14221 /* Otherwise this is a generic code; we should just lists all of
14222 these explicitly. We forgot one. */
14223 gcc_unreachable ();
14224 #else
14225 /* In a release build, we want to degrade gracefully: better to
14226 generate incomplete debugging information than to crash. */
14227 return NULL;
14228 #endif
14231 if (!ret && !list_ret)
14232 return 0;
14234 if (want_address == 2 && !have_address
14235 && (dwarf_version >= 4 || !dwarf_strict))
14237 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14239 expansion_failed (loc, NULL_RTX,
14240 "DWARF address size mismatch");
14241 return 0;
14243 if (ret)
14244 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14245 else
14246 add_loc_descr_to_each (list_ret,
14247 new_loc_descr (DW_OP_stack_value, 0, 0));
14248 have_address = 1;
14250 /* Show if we can't fill the request for an address. */
14251 if (want_address && !have_address)
14253 expansion_failed (loc, NULL_RTX,
14254 "Want address and only have value");
14255 return 0;
14258 gcc_assert (!ret || !list_ret);
14260 /* If we've got an address and don't want one, dereference. */
14261 if (!want_address && have_address)
14263 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14265 if (size > DWARF2_ADDR_SIZE || size == -1)
14267 expansion_failed (loc, NULL_RTX,
14268 "DWARF address size mismatch");
14269 return 0;
14271 else if (size == DWARF2_ADDR_SIZE)
14272 op = DW_OP_deref;
14273 else
14274 op = DW_OP_deref_size;
14276 if (ret)
14277 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14278 else
14279 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14281 if (ret)
14282 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14284 return list_ret;
14287 /* Same as above but return only single location expression. */
14288 static dw_loc_descr_ref
14289 loc_descriptor_from_tree (tree loc, int want_address)
14291 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14292 if (!ret)
14293 return NULL;
14294 if (ret->dw_loc_next)
14296 expansion_failed (loc, NULL_RTX,
14297 "Location list where only loc descriptor needed");
14298 return NULL;
14300 return ret->expr;
14303 /* Given a value, round it up to the lowest multiple of `boundary'
14304 which is not less than the value itself. */
14306 static inline HOST_WIDE_INT
14307 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14309 return (((value + boundary - 1) / boundary) * boundary);
14312 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14313 pointer to the declared type for the relevant field variable, or return
14314 `integer_type_node' if the given node turns out to be an
14315 ERROR_MARK node. */
14317 static inline tree
14318 field_type (const_tree decl)
14320 tree type;
14322 if (TREE_CODE (decl) == ERROR_MARK)
14323 return integer_type_node;
14325 type = DECL_BIT_FIELD_TYPE (decl);
14326 if (type == NULL_TREE)
14327 type = TREE_TYPE (decl);
14329 return type;
14332 /* Given a pointer to a tree node, return the alignment in bits for
14333 it, or else return BITS_PER_WORD if the node actually turns out to
14334 be an ERROR_MARK node. */
14336 static inline unsigned
14337 simple_type_align_in_bits (const_tree type)
14339 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14342 static inline unsigned
14343 simple_decl_align_in_bits (const_tree decl)
14345 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14348 /* Return the result of rounding T up to ALIGN. */
14350 static inline double_int
14351 round_up_to_align (double_int t, unsigned int align)
14353 double_int alignd = double_int::from_uhwi (align);
14354 t += alignd;
14355 t += double_int_minus_one;
14356 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14357 t *= alignd;
14358 return t;
14361 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14362 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14363 or return 0 if we are unable to determine what that offset is, either
14364 because the argument turns out to be a pointer to an ERROR_MARK node, or
14365 because the offset is actually variable. (We can't handle the latter case
14366 just yet). */
14368 static HOST_WIDE_INT
14369 field_byte_offset (const_tree decl)
14371 double_int object_offset_in_bits;
14372 double_int object_offset_in_bytes;
14373 double_int bitpos_int;
14375 if (TREE_CODE (decl) == ERROR_MARK)
14376 return 0;
14378 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14380 /* We cannot yet cope with fields whose positions are variable, so
14381 for now, when we see such things, we simply return 0. Someday, we may
14382 be able to handle such cases, but it will be damn difficult. */
14383 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14384 return 0;
14386 bitpos_int = tree_to_double_int (bit_position (decl));
14388 #ifdef PCC_BITFIELD_TYPE_MATTERS
14389 if (PCC_BITFIELD_TYPE_MATTERS)
14391 tree type;
14392 tree field_size_tree;
14393 double_int deepest_bitpos;
14394 double_int field_size_in_bits;
14395 unsigned int type_align_in_bits;
14396 unsigned int decl_align_in_bits;
14397 double_int type_size_in_bits;
14399 type = field_type (decl);
14400 type_size_in_bits = double_int_type_size_in_bits (type);
14401 type_align_in_bits = simple_type_align_in_bits (type);
14403 field_size_tree = DECL_SIZE (decl);
14405 /* The size could be unspecified if there was an error, or for
14406 a flexible array member. */
14407 if (!field_size_tree)
14408 field_size_tree = bitsize_zero_node;
14410 /* If the size of the field is not constant, use the type size. */
14411 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14412 field_size_in_bits = tree_to_double_int (field_size_tree);
14413 else
14414 field_size_in_bits = type_size_in_bits;
14416 decl_align_in_bits = simple_decl_align_in_bits (decl);
14418 /* The GCC front-end doesn't make any attempt to keep track of the
14419 starting bit offset (relative to the start of the containing
14420 structure type) of the hypothetical "containing object" for a
14421 bit-field. Thus, when computing the byte offset value for the
14422 start of the "containing object" of a bit-field, we must deduce
14423 this information on our own. This can be rather tricky to do in
14424 some cases. For example, handling the following structure type
14425 definition when compiling for an i386/i486 target (which only
14426 aligns long long's to 32-bit boundaries) can be very tricky:
14428 struct S { int field1; long long field2:31; };
14430 Fortunately, there is a simple rule-of-thumb which can be used
14431 in such cases. When compiling for an i386/i486, GCC will
14432 allocate 8 bytes for the structure shown above. It decides to
14433 do this based upon one simple rule for bit-field allocation.
14434 GCC allocates each "containing object" for each bit-field at
14435 the first (i.e. lowest addressed) legitimate alignment boundary
14436 (based upon the required minimum alignment for the declared
14437 type of the field) which it can possibly use, subject to the
14438 condition that there is still enough available space remaining
14439 in the containing object (when allocated at the selected point)
14440 to fully accommodate all of the bits of the bit-field itself.
14442 This simple rule makes it obvious why GCC allocates 8 bytes for
14443 each object of the structure type shown above. When looking
14444 for a place to allocate the "containing object" for `field2',
14445 the compiler simply tries to allocate a 64-bit "containing
14446 object" at each successive 32-bit boundary (starting at zero)
14447 until it finds a place to allocate that 64- bit field such that
14448 at least 31 contiguous (and previously unallocated) bits remain
14449 within that selected 64 bit field. (As it turns out, for the
14450 example above, the compiler finds it is OK to allocate the
14451 "containing object" 64-bit field at bit-offset zero within the
14452 structure type.)
14454 Here we attempt to work backwards from the limited set of facts
14455 we're given, and we try to deduce from those facts, where GCC
14456 must have believed that the containing object started (within
14457 the structure type). The value we deduce is then used (by the
14458 callers of this routine) to generate DW_AT_location and
14459 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14460 the case of DW_AT_location, regular fields as well). */
14462 /* Figure out the bit-distance from the start of the structure to
14463 the "deepest" bit of the bit-field. */
14464 deepest_bitpos = bitpos_int + field_size_in_bits;
14466 /* This is the tricky part. Use some fancy footwork to deduce
14467 where the lowest addressed bit of the containing object must
14468 be. */
14469 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14471 /* Round up to type_align by default. This works best for
14472 bitfields. */
14473 object_offset_in_bits
14474 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14476 if (object_offset_in_bits.ugt (bitpos_int))
14478 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14480 /* Round up to decl_align instead. */
14481 object_offset_in_bits
14482 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14485 else
14486 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14487 object_offset_in_bits = bitpos_int;
14489 object_offset_in_bytes
14490 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14491 true, TRUNC_DIV_EXPR);
14492 return object_offset_in_bytes.to_shwi ();
14495 /* The following routines define various Dwarf attributes and any data
14496 associated with them. */
14498 /* Add a location description attribute value to a DIE.
14500 This emits location attributes suitable for whole variables and
14501 whole parameters. Note that the location attributes for struct fields are
14502 generated by the routine `data_member_location_attribute' below. */
14504 static inline void
14505 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14506 dw_loc_list_ref descr)
14508 if (descr == 0)
14509 return;
14510 if (single_element_loc_list_p (descr))
14511 add_AT_loc (die, attr_kind, descr->expr);
14512 else
14513 add_AT_loc_list (die, attr_kind, descr);
14516 /* Add DW_AT_accessibility attribute to DIE if needed. */
14518 static void
14519 add_accessibility_attribute (dw_die_ref die, tree decl)
14521 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14522 children, otherwise the default is DW_ACCESS_public. In DWARF2
14523 the default has always been DW_ACCESS_public. */
14524 if (TREE_PROTECTED (decl))
14525 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14526 else if (TREE_PRIVATE (decl))
14528 if (dwarf_version == 2
14529 || die->die_parent == NULL
14530 || die->die_parent->die_tag != DW_TAG_class_type)
14531 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14533 else if (dwarf_version > 2
14534 && die->die_parent
14535 && die->die_parent->die_tag == DW_TAG_class_type)
14536 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14539 /* Attach the specialized form of location attribute used for data members of
14540 struct and union types. In the special case of a FIELD_DECL node which
14541 represents a bit-field, the "offset" part of this special location
14542 descriptor must indicate the distance in bytes from the lowest-addressed
14543 byte of the containing struct or union type to the lowest-addressed byte of
14544 the "containing object" for the bit-field. (See the `field_byte_offset'
14545 function above).
14547 For any given bit-field, the "containing object" is a hypothetical object
14548 (of some integral or enum type) within which the given bit-field lives. The
14549 type of this hypothetical "containing object" is always the same as the
14550 declared type of the individual bit-field itself (for GCC anyway... the
14551 DWARF spec doesn't actually mandate this). Note that it is the size (in
14552 bytes) of the hypothetical "containing object" which will be given in the
14553 DW_AT_byte_size attribute for this bit-field. (See the
14554 `byte_size_attribute' function below.) It is also used when calculating the
14555 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14556 function below.) */
14558 static void
14559 add_data_member_location_attribute (dw_die_ref die, tree decl)
14561 HOST_WIDE_INT offset;
14562 dw_loc_descr_ref loc_descr = 0;
14564 if (TREE_CODE (decl) == TREE_BINFO)
14566 /* We're working on the TAG_inheritance for a base class. */
14567 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14569 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14570 aren't at a fixed offset from all (sub)objects of the same
14571 type. We need to extract the appropriate offset from our
14572 vtable. The following dwarf expression means
14574 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14576 This is specific to the V3 ABI, of course. */
14578 dw_loc_descr_ref tmp;
14580 /* Make a copy of the object address. */
14581 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14582 add_loc_descr (&loc_descr, tmp);
14584 /* Extract the vtable address. */
14585 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14586 add_loc_descr (&loc_descr, tmp);
14588 /* Calculate the address of the offset. */
14589 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14590 gcc_assert (offset < 0);
14592 tmp = int_loc_descriptor (-offset);
14593 add_loc_descr (&loc_descr, tmp);
14594 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14595 add_loc_descr (&loc_descr, tmp);
14597 /* Extract the offset. */
14598 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14599 add_loc_descr (&loc_descr, tmp);
14601 /* Add it to the object address. */
14602 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14603 add_loc_descr (&loc_descr, tmp);
14605 else
14606 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14608 else
14609 offset = field_byte_offset (decl);
14611 if (! loc_descr)
14613 if (dwarf_version > 2)
14615 /* Don't need to output a location expression, just the constant. */
14616 if (offset < 0)
14617 add_AT_int (die, DW_AT_data_member_location, offset);
14618 else
14619 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14620 return;
14622 else
14624 enum dwarf_location_atom op;
14626 /* The DWARF2 standard says that we should assume that the structure
14627 address is already on the stack, so we can specify a structure
14628 field address by using DW_OP_plus_uconst. */
14629 op = DW_OP_plus_uconst;
14630 loc_descr = new_loc_descr (op, offset, 0);
14634 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14637 /* Writes integer values to dw_vec_const array. */
14639 static void
14640 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14642 while (size != 0)
14644 *dest++ = val & 0xff;
14645 val >>= 8;
14646 --size;
14650 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14652 static HOST_WIDE_INT
14653 extract_int (const unsigned char *src, unsigned int size)
14655 HOST_WIDE_INT val = 0;
14657 src += size;
14658 while (size != 0)
14660 val <<= 8;
14661 val |= *--src & 0xff;
14662 --size;
14664 return val;
14667 /* Writes double_int values to dw_vec_const array. */
14669 static void
14670 insert_double (double_int val, unsigned char *dest)
14672 unsigned char *p0 = dest;
14673 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14675 if (WORDS_BIG_ENDIAN)
14677 p0 = p1;
14678 p1 = dest;
14681 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14682 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14685 /* Writes floating point values to dw_vec_const array. */
14687 static void
14688 insert_float (const_rtx rtl, unsigned char *array)
14690 REAL_VALUE_TYPE rv;
14691 long val[4];
14692 int i;
14694 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14695 real_to_target (val, &rv, GET_MODE (rtl));
14697 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14698 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14700 insert_int (val[i], 4, array);
14701 array += 4;
14705 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14706 does not have a "location" either in memory or in a register. These
14707 things can arise in GNU C when a constant is passed as an actual parameter
14708 to an inlined function. They can also arise in C++ where declared
14709 constants do not necessarily get memory "homes". */
14711 static bool
14712 add_const_value_attribute (dw_die_ref die, rtx rtl)
14714 switch (GET_CODE (rtl))
14716 case CONST_INT:
14718 HOST_WIDE_INT val = INTVAL (rtl);
14720 if (val < 0)
14721 add_AT_int (die, DW_AT_const_value, val);
14722 else
14723 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14725 return true;
14727 case CONST_DOUBLE:
14728 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14729 floating-point constant. A CONST_DOUBLE is used whenever the
14730 constant requires more than one word in order to be adequately
14731 represented. */
14733 enum machine_mode mode = GET_MODE (rtl);
14735 if (SCALAR_FLOAT_MODE_P (mode))
14737 unsigned int length = GET_MODE_SIZE (mode);
14738 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14740 insert_float (rtl, array);
14741 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14743 else
14744 add_AT_double (die, DW_AT_const_value,
14745 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14747 return true;
14749 case CONST_VECTOR:
14751 enum machine_mode mode = GET_MODE (rtl);
14752 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14753 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14754 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14755 (length * elt_size);
14756 unsigned int i;
14757 unsigned char *p;
14759 switch (GET_MODE_CLASS (mode))
14761 case MODE_VECTOR_INT:
14762 for (i = 0, p = array; i < length; i++, p += elt_size)
14764 rtx elt = CONST_VECTOR_ELT (rtl, i);
14765 double_int val = rtx_to_double_int (elt);
14767 if (elt_size <= sizeof (HOST_WIDE_INT))
14768 insert_int (val.to_shwi (), elt_size, p);
14769 else
14771 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14772 insert_double (val, p);
14775 break;
14777 case MODE_VECTOR_FLOAT:
14778 for (i = 0, p = array; i < length; i++, p += elt_size)
14780 rtx elt = CONST_VECTOR_ELT (rtl, i);
14781 insert_float (elt, p);
14783 break;
14785 default:
14786 gcc_unreachable ();
14789 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14791 return true;
14793 case CONST_STRING:
14794 if (dwarf_version >= 4 || !dwarf_strict)
14796 dw_loc_descr_ref loc_result;
14797 resolve_one_addr (&rtl, NULL);
14798 rtl_addr:
14799 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14800 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14801 add_AT_loc (die, DW_AT_location, loc_result);
14802 vec_safe_push (used_rtx_array, rtl);
14803 return true;
14805 return false;
14807 case CONST:
14808 if (CONSTANT_P (XEXP (rtl, 0)))
14809 return add_const_value_attribute (die, XEXP (rtl, 0));
14810 /* FALLTHROUGH */
14811 case SYMBOL_REF:
14812 if (!const_ok_for_output (rtl))
14813 return false;
14814 case LABEL_REF:
14815 if (dwarf_version >= 4 || !dwarf_strict)
14816 goto rtl_addr;
14817 return false;
14819 case PLUS:
14820 /* In cases where an inlined instance of an inline function is passed
14821 the address of an `auto' variable (which is local to the caller) we
14822 can get a situation where the DECL_RTL of the artificial local
14823 variable (for the inlining) which acts as a stand-in for the
14824 corresponding formal parameter (of the inline function) will look
14825 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14826 exactly a compile-time constant expression, but it isn't the address
14827 of the (artificial) local variable either. Rather, it represents the
14828 *value* which the artificial local variable always has during its
14829 lifetime. We currently have no way to represent such quasi-constant
14830 values in Dwarf, so for now we just punt and generate nothing. */
14831 return false;
14833 case HIGH:
14834 case CONST_FIXED:
14835 return false;
14837 case MEM:
14838 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14839 && MEM_READONLY_P (rtl)
14840 && GET_MODE (rtl) == BLKmode)
14842 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14843 return true;
14845 return false;
14847 default:
14848 /* No other kinds of rtx should be possible here. */
14849 gcc_unreachable ();
14851 return false;
14854 /* Determine whether the evaluation of EXPR references any variables
14855 or functions which aren't otherwise used (and therefore may not be
14856 output). */
14857 static tree
14858 reference_to_unused (tree * tp, int * walk_subtrees,
14859 void * data ATTRIBUTE_UNUSED)
14861 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14862 *walk_subtrees = 0;
14864 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14865 && ! TREE_ASM_WRITTEN (*tp))
14866 return *tp;
14867 /* ??? The C++ FE emits debug information for using decls, so
14868 putting gcc_unreachable here falls over. See PR31899. For now
14869 be conservative. */
14870 else if (!cgraph_global_info_ready
14871 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14872 return *tp;
14873 else if (TREE_CODE (*tp) == VAR_DECL)
14875 struct varpool_node *node = varpool_get_node (*tp);
14876 if (!node || !node->analyzed)
14877 return *tp;
14879 else if (TREE_CODE (*tp) == FUNCTION_DECL
14880 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14882 /* The call graph machinery must have finished analyzing,
14883 optimizing and gimplifying the CU by now.
14884 So if *TP has no call graph node associated
14885 to it, it means *TP will not be emitted. */
14886 if (!cgraph_get_node (*tp))
14887 return *tp;
14889 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14890 return *tp;
14892 return NULL_TREE;
14895 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14896 for use in a later add_const_value_attribute call. */
14898 static rtx
14899 rtl_for_decl_init (tree init, tree type)
14901 rtx rtl = NULL_RTX;
14903 STRIP_NOPS (init);
14905 /* If a variable is initialized with a string constant without embedded
14906 zeros, build CONST_STRING. */
14907 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14909 tree enttype = TREE_TYPE (type);
14910 tree domain = TYPE_DOMAIN (type);
14911 enum machine_mode mode = TYPE_MODE (enttype);
14913 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14914 && domain
14915 && integer_zerop (TYPE_MIN_VALUE (domain))
14916 && compare_tree_int (TYPE_MAX_VALUE (domain),
14917 TREE_STRING_LENGTH (init) - 1) == 0
14918 && ((size_t) TREE_STRING_LENGTH (init)
14919 == strlen (TREE_STRING_POINTER (init)) + 1))
14921 rtl = gen_rtx_CONST_STRING (VOIDmode,
14922 ggc_strdup (TREE_STRING_POINTER (init)));
14923 rtl = gen_rtx_MEM (BLKmode, rtl);
14924 MEM_READONLY_P (rtl) = 1;
14927 /* Other aggregates, and complex values, could be represented using
14928 CONCAT: FIXME! */
14929 else if (AGGREGATE_TYPE_P (type)
14930 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14931 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14932 || TREE_CODE (type) == COMPLEX_TYPE)
14934 /* Vectors only work if their mode is supported by the target.
14935 FIXME: generic vectors ought to work too. */
14936 else if (TREE_CODE (type) == VECTOR_TYPE
14937 && !VECTOR_MODE_P (TYPE_MODE (type)))
14939 /* If the initializer is something that we know will expand into an
14940 immediate RTL constant, expand it now. We must be careful not to
14941 reference variables which won't be output. */
14942 else if (initializer_constant_valid_p (init, type)
14943 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14945 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14946 possible. */
14947 if (TREE_CODE (type) == VECTOR_TYPE)
14948 switch (TREE_CODE (init))
14950 case VECTOR_CST:
14951 break;
14952 case CONSTRUCTOR:
14953 if (TREE_CONSTANT (init))
14955 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
14956 bool constant_p = true;
14957 tree value;
14958 unsigned HOST_WIDE_INT ix;
14960 /* Even when ctor is constant, it might contain non-*_CST
14961 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14962 belong into VECTOR_CST nodes. */
14963 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14964 if (!CONSTANT_CLASS_P (value))
14966 constant_p = false;
14967 break;
14970 if (constant_p)
14972 init = build_vector_from_ctor (type, elts);
14973 break;
14976 /* FALLTHRU */
14978 default:
14979 return NULL;
14982 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14984 /* If expand_expr returns a MEM, it wasn't immediate. */
14985 gcc_assert (!rtl || !MEM_P (rtl));
14988 return rtl;
14991 /* Generate RTL for the variable DECL to represent its location. */
14993 static rtx
14994 rtl_for_decl_location (tree decl)
14996 rtx rtl;
14998 /* Here we have to decide where we are going to say the parameter "lives"
14999 (as far as the debugger is concerned). We only have a couple of
15000 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15002 DECL_RTL normally indicates where the parameter lives during most of the
15003 activation of the function. If optimization is enabled however, this
15004 could be either NULL or else a pseudo-reg. Both of those cases indicate
15005 that the parameter doesn't really live anywhere (as far as the code
15006 generation parts of GCC are concerned) during most of the function's
15007 activation. That will happen (for example) if the parameter is never
15008 referenced within the function.
15010 We could just generate a location descriptor here for all non-NULL
15011 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15012 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15013 where DECL_RTL is NULL or is a pseudo-reg.
15015 Note however that we can only get away with using DECL_INCOMING_RTL as
15016 a backup substitute for DECL_RTL in certain limited cases. In cases
15017 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15018 we can be sure that the parameter was passed using the same type as it is
15019 declared to have within the function, and that its DECL_INCOMING_RTL
15020 points us to a place where a value of that type is passed.
15022 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15023 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15024 because in these cases DECL_INCOMING_RTL points us to a value of some
15025 type which is *different* from the type of the parameter itself. Thus,
15026 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15027 such cases, the debugger would end up (for example) trying to fetch a
15028 `float' from a place which actually contains the first part of a
15029 `double'. That would lead to really incorrect and confusing
15030 output at debug-time.
15032 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15033 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15034 are a couple of exceptions however. On little-endian machines we can
15035 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15036 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15037 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15038 when (on a little-endian machine) a non-prototyped function has a
15039 parameter declared to be of type `short' or `char'. In such cases,
15040 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15041 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15042 passed `int' value. If the debugger then uses that address to fetch
15043 a `short' or a `char' (on a little-endian machine) the result will be
15044 the correct data, so we allow for such exceptional cases below.
15046 Note that our goal here is to describe the place where the given formal
15047 parameter lives during most of the function's activation (i.e. between the
15048 end of the prologue and the start of the epilogue). We'll do that as best
15049 as we can. Note however that if the given formal parameter is modified
15050 sometime during the execution of the function, then a stack backtrace (at
15051 debug-time) will show the function as having been called with the *new*
15052 value rather than the value which was originally passed in. This happens
15053 rarely enough that it is not a major problem, but it *is* a problem, and
15054 I'd like to fix it.
15056 A future version of dwarf2out.c may generate two additional attributes for
15057 any given DW_TAG_formal_parameter DIE which will describe the "passed
15058 type" and the "passed location" for the given formal parameter in addition
15059 to the attributes we now generate to indicate the "declared type" and the
15060 "active location" for each parameter. This additional set of attributes
15061 could be used by debuggers for stack backtraces. Separately, note that
15062 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15063 This happens (for example) for inlined-instances of inline function formal
15064 parameters which are never referenced. This really shouldn't be
15065 happening. All PARM_DECL nodes should get valid non-NULL
15066 DECL_INCOMING_RTL values. FIXME. */
15068 /* Use DECL_RTL as the "location" unless we find something better. */
15069 rtl = DECL_RTL_IF_SET (decl);
15071 /* When generating abstract instances, ignore everything except
15072 constants, symbols living in memory, and symbols living in
15073 fixed registers. */
15074 if (! reload_completed)
15076 if (rtl
15077 && (CONSTANT_P (rtl)
15078 || (MEM_P (rtl)
15079 && CONSTANT_P (XEXP (rtl, 0)))
15080 || (REG_P (rtl)
15081 && TREE_CODE (decl) == VAR_DECL
15082 && TREE_STATIC (decl))))
15084 rtl = targetm.delegitimize_address (rtl);
15085 return rtl;
15087 rtl = NULL_RTX;
15089 else if (TREE_CODE (decl) == PARM_DECL)
15091 if (rtl == NULL_RTX
15092 || is_pseudo_reg (rtl)
15093 || (MEM_P (rtl)
15094 && is_pseudo_reg (XEXP (rtl, 0))
15095 && DECL_INCOMING_RTL (decl)
15096 && MEM_P (DECL_INCOMING_RTL (decl))
15097 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15099 tree declared_type = TREE_TYPE (decl);
15100 tree passed_type = DECL_ARG_TYPE (decl);
15101 enum machine_mode dmode = TYPE_MODE (declared_type);
15102 enum machine_mode pmode = TYPE_MODE (passed_type);
15104 /* This decl represents a formal parameter which was optimized out.
15105 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15106 all cases where (rtl == NULL_RTX) just below. */
15107 if (dmode == pmode)
15108 rtl = DECL_INCOMING_RTL (decl);
15109 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15110 && SCALAR_INT_MODE_P (dmode)
15111 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15112 && DECL_INCOMING_RTL (decl))
15114 rtx inc = DECL_INCOMING_RTL (decl);
15115 if (REG_P (inc))
15116 rtl = inc;
15117 else if (MEM_P (inc))
15119 if (BYTES_BIG_ENDIAN)
15120 rtl = adjust_address_nv (inc, dmode,
15121 GET_MODE_SIZE (pmode)
15122 - GET_MODE_SIZE (dmode));
15123 else
15124 rtl = inc;
15129 /* If the parm was passed in registers, but lives on the stack, then
15130 make a big endian correction if the mode of the type of the
15131 parameter is not the same as the mode of the rtl. */
15132 /* ??? This is the same series of checks that are made in dbxout.c before
15133 we reach the big endian correction code there. It isn't clear if all
15134 of these checks are necessary here, but keeping them all is the safe
15135 thing to do. */
15136 else if (MEM_P (rtl)
15137 && XEXP (rtl, 0) != const0_rtx
15138 && ! CONSTANT_P (XEXP (rtl, 0))
15139 /* Not passed in memory. */
15140 && !MEM_P (DECL_INCOMING_RTL (decl))
15141 /* Not passed by invisible reference. */
15142 && (!REG_P (XEXP (rtl, 0))
15143 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15144 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15145 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15146 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15147 #endif
15149 /* Big endian correction check. */
15150 && BYTES_BIG_ENDIAN
15151 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15152 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15153 < UNITS_PER_WORD))
15155 enum machine_mode addr_mode = get_address_mode (rtl);
15156 int offset = (UNITS_PER_WORD
15157 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15159 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15160 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15163 else if (TREE_CODE (decl) == VAR_DECL
15164 && rtl
15165 && MEM_P (rtl)
15166 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15167 && BYTES_BIG_ENDIAN)
15169 enum machine_mode addr_mode = get_address_mode (rtl);
15170 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15171 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15173 /* If a variable is declared "register" yet is smaller than
15174 a register, then if we store the variable to memory, it
15175 looks like we're storing a register-sized value, when in
15176 fact we are not. We need to adjust the offset of the
15177 storage location to reflect the actual value's bytes,
15178 else gdb will not be able to display it. */
15179 if (rsize > dsize)
15180 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15181 plus_constant (addr_mode, XEXP (rtl, 0),
15182 rsize - dsize));
15185 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15186 and will have been substituted directly into all expressions that use it.
15187 C does not have such a concept, but C++ and other languages do. */
15188 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15189 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15191 if (rtl)
15192 rtl = targetm.delegitimize_address (rtl);
15194 /* If we don't look past the constant pool, we risk emitting a
15195 reference to a constant pool entry that isn't referenced from
15196 code, and thus is not emitted. */
15197 if (rtl)
15198 rtl = avoid_constant_pool_reference (rtl);
15200 /* Try harder to get a rtl. If this symbol ends up not being emitted
15201 in the current CU, resolve_addr will remove the expression referencing
15202 it. */
15203 if (rtl == NULL_RTX
15204 && TREE_CODE (decl) == VAR_DECL
15205 && !DECL_EXTERNAL (decl)
15206 && TREE_STATIC (decl)
15207 && DECL_NAME (decl)
15208 && !DECL_HARD_REGISTER (decl)
15209 && DECL_MODE (decl) != VOIDmode)
15211 rtl = make_decl_rtl_for_debug (decl);
15212 if (!MEM_P (rtl)
15213 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15214 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15215 rtl = NULL_RTX;
15218 return rtl;
15221 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15222 returned. If so, the decl for the COMMON block is returned, and the
15223 value is the offset into the common block for the symbol. */
15225 static tree
15226 fortran_common (tree decl, HOST_WIDE_INT *value)
15228 tree val_expr, cvar;
15229 enum machine_mode mode;
15230 HOST_WIDE_INT bitsize, bitpos;
15231 tree offset;
15232 int unsignedp, volatilep = 0;
15234 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15235 it does not have a value (the offset into the common area), or if it
15236 is thread local (as opposed to global) then it isn't common, and shouldn't
15237 be handled as such. */
15238 if (TREE_CODE (decl) != VAR_DECL
15239 || !TREE_STATIC (decl)
15240 || !DECL_HAS_VALUE_EXPR_P (decl)
15241 || !is_fortran ())
15242 return NULL_TREE;
15244 val_expr = DECL_VALUE_EXPR (decl);
15245 if (TREE_CODE (val_expr) != COMPONENT_REF)
15246 return NULL_TREE;
15248 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15249 &mode, &unsignedp, &volatilep, true);
15251 if (cvar == NULL_TREE
15252 || TREE_CODE (cvar) != VAR_DECL
15253 || DECL_ARTIFICIAL (cvar)
15254 || !TREE_PUBLIC (cvar))
15255 return NULL_TREE;
15257 *value = 0;
15258 if (offset != NULL)
15260 if (!host_integerp (offset, 0))
15261 return NULL_TREE;
15262 *value = tree_low_cst (offset, 0);
15264 if (bitpos != 0)
15265 *value += bitpos / BITS_PER_UNIT;
15267 return cvar;
15270 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15271 data attribute for a variable or a parameter. We generate the
15272 DW_AT_const_value attribute only in those cases where the given variable
15273 or parameter does not have a true "location" either in memory or in a
15274 register. This can happen (for example) when a constant is passed as an
15275 actual argument in a call to an inline function. (It's possible that
15276 these things can crop up in other ways also.) Note that one type of
15277 constant value which can be passed into an inlined function is a constant
15278 pointer. This can happen for example if an actual argument in an inlined
15279 function call evaluates to a compile-time constant address.
15281 CACHE_P is true if it is worth caching the location list for DECL,
15282 so that future calls can reuse it rather than regenerate it from scratch.
15283 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15284 since we will need to refer to them each time the function is inlined. */
15286 static bool
15287 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15288 enum dwarf_attribute attr)
15290 rtx rtl;
15291 dw_loc_list_ref list;
15292 var_loc_list *loc_list;
15293 cached_dw_loc_list *cache;
15294 void **slot;
15296 if (TREE_CODE (decl) == ERROR_MARK)
15297 return false;
15299 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15300 || TREE_CODE (decl) == RESULT_DECL);
15302 /* Try to get some constant RTL for this decl, and use that as the value of
15303 the location. */
15305 rtl = rtl_for_decl_location (decl);
15306 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15307 && add_const_value_attribute (die, rtl))
15308 return true;
15310 /* See if we have single element location list that is equivalent to
15311 a constant value. That way we are better to use add_const_value_attribute
15312 rather than expanding constant value equivalent. */
15313 loc_list = lookup_decl_loc (decl);
15314 if (loc_list
15315 && loc_list->first
15316 && loc_list->first->next == NULL
15317 && NOTE_P (loc_list->first->loc)
15318 && NOTE_VAR_LOCATION (loc_list->first->loc)
15319 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15321 struct var_loc_node *node;
15323 node = loc_list->first;
15324 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15325 if (GET_CODE (rtl) == EXPR_LIST)
15326 rtl = XEXP (rtl, 0);
15327 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15328 && add_const_value_attribute (die, rtl))
15329 return true;
15331 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15332 list several times. See if we've already cached the contents. */
15333 list = NULL;
15334 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15335 cache_p = false;
15336 if (cache_p)
15338 cache = (cached_dw_loc_list *)
15339 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15340 if (cache)
15341 list = cache->loc_list;
15343 if (list == NULL)
15345 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15346 /* It is usually worth caching this result if the decl is from
15347 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15348 if (cache_p && list && list->dw_loc_next)
15350 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15351 DECL_UID (decl), INSERT);
15352 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15353 cache->decl_id = DECL_UID (decl);
15354 cache->loc_list = list;
15355 *slot = cache;
15358 if (list)
15360 add_AT_location_description (die, attr, list);
15361 return true;
15363 /* None of that worked, so it must not really have a location;
15364 try adding a constant value attribute from the DECL_INITIAL. */
15365 return tree_add_const_value_attribute_for_decl (die, decl);
15368 /* Add VARIABLE and DIE into deferred locations list. */
15370 static void
15371 defer_location (tree variable, dw_die_ref die)
15373 deferred_locations entry;
15374 entry.variable = variable;
15375 entry.die = die;
15376 vec_safe_push (deferred_locations_list, entry);
15379 /* Helper function for tree_add_const_value_attribute. Natively encode
15380 initializer INIT into an array. Return true if successful. */
15382 static bool
15383 native_encode_initializer (tree init, unsigned char *array, int size)
15385 tree type;
15387 if (init == NULL_TREE)
15388 return false;
15390 STRIP_NOPS (init);
15391 switch (TREE_CODE (init))
15393 case STRING_CST:
15394 type = TREE_TYPE (init);
15395 if (TREE_CODE (type) == ARRAY_TYPE)
15397 tree enttype = TREE_TYPE (type);
15398 enum machine_mode mode = TYPE_MODE (enttype);
15400 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15401 return false;
15402 if (int_size_in_bytes (type) != size)
15403 return false;
15404 if (size > TREE_STRING_LENGTH (init))
15406 memcpy (array, TREE_STRING_POINTER (init),
15407 TREE_STRING_LENGTH (init));
15408 memset (array + TREE_STRING_LENGTH (init),
15409 '\0', size - TREE_STRING_LENGTH (init));
15411 else
15412 memcpy (array, TREE_STRING_POINTER (init), size);
15413 return true;
15415 return false;
15416 case CONSTRUCTOR:
15417 type = TREE_TYPE (init);
15418 if (int_size_in_bytes (type) != size)
15419 return false;
15420 if (TREE_CODE (type) == ARRAY_TYPE)
15422 HOST_WIDE_INT min_index;
15423 unsigned HOST_WIDE_INT cnt;
15424 int curpos = 0, fieldsize;
15425 constructor_elt *ce;
15427 if (TYPE_DOMAIN (type) == NULL_TREE
15428 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15429 return false;
15431 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15432 if (fieldsize <= 0)
15433 return false;
15435 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15436 memset (array, '\0', size);
15437 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15439 tree val = ce->value;
15440 tree index = ce->index;
15441 int pos = curpos;
15442 if (index && TREE_CODE (index) == RANGE_EXPR)
15443 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15444 * fieldsize;
15445 else if (index)
15446 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15448 if (val)
15450 STRIP_NOPS (val);
15451 if (!native_encode_initializer (val, array + pos, fieldsize))
15452 return false;
15454 curpos = pos + fieldsize;
15455 if (index && TREE_CODE (index) == RANGE_EXPR)
15457 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15458 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15459 while (count-- > 0)
15461 if (val)
15462 memcpy (array + curpos, array + pos, fieldsize);
15463 curpos += fieldsize;
15466 gcc_assert (curpos <= size);
15468 return true;
15470 else if (TREE_CODE (type) == RECORD_TYPE
15471 || TREE_CODE (type) == UNION_TYPE)
15473 tree field = NULL_TREE;
15474 unsigned HOST_WIDE_INT cnt;
15475 constructor_elt *ce;
15477 if (int_size_in_bytes (type) != size)
15478 return false;
15480 if (TREE_CODE (type) == RECORD_TYPE)
15481 field = TYPE_FIELDS (type);
15483 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15485 tree val = ce->value;
15486 int pos, fieldsize;
15488 if (ce->index != 0)
15489 field = ce->index;
15491 if (val)
15492 STRIP_NOPS (val);
15494 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15495 return false;
15497 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15498 && TYPE_DOMAIN (TREE_TYPE (field))
15499 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15500 return false;
15501 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15502 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15503 return false;
15504 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15505 pos = int_byte_position (field);
15506 gcc_assert (pos + fieldsize <= size);
15507 if (val
15508 && !native_encode_initializer (val, array + pos, fieldsize))
15509 return false;
15511 return true;
15513 return false;
15514 case VIEW_CONVERT_EXPR:
15515 case NON_LVALUE_EXPR:
15516 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15517 default:
15518 return native_encode_expr (init, array, size) == size;
15522 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15523 attribute is the const value T. */
15525 static bool
15526 tree_add_const_value_attribute (dw_die_ref die, tree t)
15528 tree init;
15529 tree type = TREE_TYPE (t);
15530 rtx rtl;
15532 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15533 return false;
15535 init = t;
15536 gcc_assert (!DECL_P (init));
15538 rtl = rtl_for_decl_init (init, type);
15539 if (rtl)
15540 return add_const_value_attribute (die, rtl);
15541 /* If the host and target are sane, try harder. */
15542 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15543 && initializer_constant_valid_p (init, type))
15545 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15546 if (size > 0 && (int) size == size)
15548 unsigned char *array = (unsigned char *)
15549 ggc_alloc_cleared_atomic (size);
15551 if (native_encode_initializer (init, array, size))
15553 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15554 return true;
15558 return false;
15561 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15562 attribute is the const value of T, where T is an integral constant
15563 variable with static storage duration
15564 (so it can't be a PARM_DECL or a RESULT_DECL). */
15566 static bool
15567 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15570 if (!decl
15571 || (TREE_CODE (decl) != VAR_DECL
15572 && TREE_CODE (decl) != CONST_DECL)
15573 || (TREE_CODE (decl) == VAR_DECL
15574 && !TREE_STATIC (decl)))
15575 return false;
15577 if (TREE_READONLY (decl)
15578 && ! TREE_THIS_VOLATILE (decl)
15579 && DECL_INITIAL (decl))
15580 /* OK */;
15581 else
15582 return false;
15584 /* Don't add DW_AT_const_value if abstract origin already has one. */
15585 if (get_AT (var_die, DW_AT_const_value))
15586 return false;
15588 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15591 /* Convert the CFI instructions for the current function into a
15592 location list. This is used for DW_AT_frame_base when we targeting
15593 a dwarf2 consumer that does not support the dwarf3
15594 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15595 expressions. */
15597 static dw_loc_list_ref
15598 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15600 int ix;
15601 dw_fde_ref fde;
15602 dw_loc_list_ref list, *list_tail;
15603 dw_cfi_ref cfi;
15604 dw_cfa_location last_cfa, next_cfa;
15605 const char *start_label, *last_label, *section;
15606 dw_cfa_location remember;
15608 fde = cfun->fde;
15609 gcc_assert (fde != NULL);
15611 section = secname_for_decl (current_function_decl);
15612 list_tail = &list;
15613 list = NULL;
15615 memset (&next_cfa, 0, sizeof (next_cfa));
15616 next_cfa.reg = INVALID_REGNUM;
15617 remember = next_cfa;
15619 start_label = fde->dw_fde_begin;
15621 /* ??? Bald assumption that the CIE opcode list does not contain
15622 advance opcodes. */
15623 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15624 lookup_cfa_1 (cfi, &next_cfa, &remember);
15626 last_cfa = next_cfa;
15627 last_label = start_label;
15629 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15631 /* If the first partition contained no CFI adjustments, the
15632 CIE opcodes apply to the whole first partition. */
15633 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15634 fde->dw_fde_begin, fde->dw_fde_end, section);
15635 list_tail =&(*list_tail)->dw_loc_next;
15636 start_label = last_label = fde->dw_fde_second_begin;
15639 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15641 switch (cfi->dw_cfi_opc)
15643 case DW_CFA_set_loc:
15644 case DW_CFA_advance_loc1:
15645 case DW_CFA_advance_loc2:
15646 case DW_CFA_advance_loc4:
15647 if (!cfa_equal_p (&last_cfa, &next_cfa))
15649 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15650 start_label, last_label, section);
15652 list_tail = &(*list_tail)->dw_loc_next;
15653 last_cfa = next_cfa;
15654 start_label = last_label;
15656 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15657 break;
15659 case DW_CFA_advance_loc:
15660 /* The encoding is complex enough that we should never emit this. */
15661 gcc_unreachable ();
15663 default:
15664 lookup_cfa_1 (cfi, &next_cfa, &remember);
15665 break;
15667 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15669 if (!cfa_equal_p (&last_cfa, &next_cfa))
15671 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15672 start_label, last_label, section);
15674 list_tail = &(*list_tail)->dw_loc_next;
15675 last_cfa = next_cfa;
15676 start_label = last_label;
15678 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15679 start_label, fde->dw_fde_end, section);
15680 list_tail = &(*list_tail)->dw_loc_next;
15681 start_label = last_label = fde->dw_fde_second_begin;
15685 if (!cfa_equal_p (&last_cfa, &next_cfa))
15687 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15688 start_label, last_label, section);
15689 list_tail = &(*list_tail)->dw_loc_next;
15690 start_label = last_label;
15693 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15694 start_label,
15695 fde->dw_fde_second_begin
15696 ? fde->dw_fde_second_end : fde->dw_fde_end,
15697 section);
15699 if (list && list->dw_loc_next)
15700 gen_llsym (list);
15702 return list;
15705 /* Compute a displacement from the "steady-state frame pointer" to the
15706 frame base (often the same as the CFA), and store it in
15707 frame_pointer_fb_offset. OFFSET is added to the displacement
15708 before the latter is negated. */
15710 static void
15711 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15713 rtx reg, elim;
15715 #ifdef FRAME_POINTER_CFA_OFFSET
15716 reg = frame_pointer_rtx;
15717 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15718 #else
15719 reg = arg_pointer_rtx;
15720 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15721 #endif
15723 elim = (ira_use_lra_p
15724 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15725 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15726 if (GET_CODE (elim) == PLUS)
15728 offset += INTVAL (XEXP (elim, 1));
15729 elim = XEXP (elim, 0);
15732 frame_pointer_fb_offset = -offset;
15734 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15735 in which to eliminate. This is because it's stack pointer isn't
15736 directly accessible as a register within the ISA. To work around
15737 this, assume that while we cannot provide a proper value for
15738 frame_pointer_fb_offset, we won't need one either. */
15739 frame_pointer_fb_offset_valid
15740 = ((SUPPORTS_STACK_ALIGNMENT
15741 && (elim == hard_frame_pointer_rtx
15742 || elim == stack_pointer_rtx))
15743 || elim == (frame_pointer_needed
15744 ? hard_frame_pointer_rtx
15745 : stack_pointer_rtx));
15748 /* Generate a DW_AT_name attribute given some string value to be included as
15749 the value of the attribute. */
15751 static void
15752 add_name_attribute (dw_die_ref die, const char *name_string)
15754 if (name_string != NULL && *name_string != 0)
15756 if (demangle_name_func)
15757 name_string = (*demangle_name_func) (name_string);
15759 add_AT_string (die, DW_AT_name, name_string);
15763 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15764 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15765 of TYPE accordingly.
15767 ??? This is a temporary measure until after we're able to generate
15768 regular DWARF for the complex Ada type system. */
15770 static void
15771 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15772 dw_die_ref context_die)
15774 tree dtype;
15775 dw_die_ref dtype_die;
15777 if (!lang_hooks.types.descriptive_type)
15778 return;
15780 dtype = lang_hooks.types.descriptive_type (type);
15781 if (!dtype)
15782 return;
15784 dtype_die = lookup_type_die (dtype);
15785 if (!dtype_die)
15787 gen_type_die (dtype, context_die);
15788 dtype_die = lookup_type_die (dtype);
15789 gcc_assert (dtype_die);
15792 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15795 /* Generate a DW_AT_comp_dir attribute for DIE. */
15797 static void
15798 add_comp_dir_attribute (dw_die_ref die)
15800 const char *wd = get_src_pwd ();
15801 char *wd1;
15803 if (wd == NULL)
15804 return;
15806 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15808 int wdlen;
15810 wdlen = strlen (wd);
15811 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15812 strcpy (wd1, wd);
15813 wd1 [wdlen] = DIR_SEPARATOR;
15814 wd1 [wdlen + 1] = 0;
15815 wd = wd1;
15818 add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15821 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15822 default. */
15824 static int
15825 lower_bound_default (void)
15827 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15829 case DW_LANG_C:
15830 case DW_LANG_C89:
15831 case DW_LANG_C99:
15832 case DW_LANG_C_plus_plus:
15833 case DW_LANG_ObjC:
15834 case DW_LANG_ObjC_plus_plus:
15835 case DW_LANG_Java:
15836 return 0;
15837 case DW_LANG_Fortran77:
15838 case DW_LANG_Fortran90:
15839 case DW_LANG_Fortran95:
15840 return 1;
15841 case DW_LANG_UPC:
15842 case DW_LANG_D:
15843 case DW_LANG_Python:
15844 return dwarf_version >= 4 ? 0 : -1;
15845 case DW_LANG_Ada95:
15846 case DW_LANG_Ada83:
15847 case DW_LANG_Cobol74:
15848 case DW_LANG_Cobol85:
15849 case DW_LANG_Pascal83:
15850 case DW_LANG_Modula2:
15851 case DW_LANG_PLI:
15852 return dwarf_version >= 4 ? 1 : -1;
15853 default:
15854 return -1;
15858 /* Given a tree node describing an array bound (either lower or upper) output
15859 a representation for that bound. */
15861 static void
15862 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15864 switch (TREE_CODE (bound))
15866 case ERROR_MARK:
15867 return;
15869 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15870 case INTEGER_CST:
15872 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15873 int dflt;
15875 /* Use the default if possible. */
15876 if (bound_attr == DW_AT_lower_bound
15877 && host_integerp (bound, 0)
15878 && (dflt = lower_bound_default ()) != -1
15879 && tree_low_cst (bound, 0) == dflt)
15882 /* Otherwise represent the bound as an unsigned value with the
15883 precision of its type. The precision and signedness of the
15884 type will be necessary to re-interpret it unambiguously. */
15885 else if (prec < HOST_BITS_PER_WIDE_INT)
15887 unsigned HOST_WIDE_INT mask
15888 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15889 add_AT_unsigned (subrange_die, bound_attr,
15890 TREE_INT_CST_LOW (bound) & mask);
15892 else if (prec == HOST_BITS_PER_WIDE_INT
15893 || TREE_INT_CST_HIGH (bound) == 0)
15894 add_AT_unsigned (subrange_die, bound_attr,
15895 TREE_INT_CST_LOW (bound));
15896 else
15897 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15898 TREE_INT_CST_LOW (bound));
15900 break;
15902 CASE_CONVERT:
15903 case VIEW_CONVERT_EXPR:
15904 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15905 break;
15907 case SAVE_EXPR:
15908 break;
15910 case VAR_DECL:
15911 case PARM_DECL:
15912 case RESULT_DECL:
15914 dw_die_ref decl_die = lookup_decl_die (bound);
15916 /* ??? Can this happen, or should the variable have been bound
15917 first? Probably it can, since I imagine that we try to create
15918 the types of parameters in the order in which they exist in
15919 the list, and won't have created a forward reference to a
15920 later parameter. */
15921 if (decl_die != NULL)
15923 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15924 break;
15927 /* FALLTHRU */
15929 default:
15931 /* Otherwise try to create a stack operation procedure to
15932 evaluate the value of the array bound. */
15934 dw_die_ref ctx, decl_die;
15935 dw_loc_list_ref list;
15937 list = loc_list_from_tree (bound, 2);
15938 if (list == NULL || single_element_loc_list_p (list))
15940 /* If DW_AT_*bound is not a reference nor constant, it is
15941 a DWARF expression rather than location description.
15942 For that loc_list_from_tree (bound, 0) is needed.
15943 If that fails to give a single element list,
15944 fall back to outputting this as a reference anyway. */
15945 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15946 if (list2 && single_element_loc_list_p (list2))
15948 add_AT_loc (subrange_die, bound_attr, list2->expr);
15949 break;
15952 if (list == NULL)
15953 break;
15955 if (current_function_decl == 0)
15956 ctx = comp_unit_die ();
15957 else
15958 ctx = lookup_decl_die (current_function_decl);
15960 decl_die = new_die (DW_TAG_variable, ctx, bound);
15961 add_AT_flag (decl_die, DW_AT_artificial, 1);
15962 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15963 add_AT_location_description (decl_die, DW_AT_location, list);
15964 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15965 break;
15970 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15971 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15972 Note that the block of subscript information for an array type also
15973 includes information about the element type of the given array type. */
15975 static void
15976 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15978 unsigned dimension_number;
15979 tree lower, upper;
15980 dw_die_ref subrange_die;
15982 for (dimension_number = 0;
15983 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15984 type = TREE_TYPE (type), dimension_number++)
15986 tree domain = TYPE_DOMAIN (type);
15988 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15989 break;
15991 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15992 and (in GNU C only) variable bounds. Handle all three forms
15993 here. */
15994 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15996 if (use_upc_dwarf2_extensions && TYPE_HAS_THREADS_FACTOR (type))
15998 add_AT_flag (subrange_die, DW_AT_upc_threads_scaled, 1);
16001 if (domain)
16003 /* We have an array type with specified bounds. */
16004 lower = TYPE_MIN_VALUE (domain);
16005 upper = TYPE_MAX_VALUE (domain);
16007 /* Define the index type. */
16008 if (TREE_TYPE (domain))
16010 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16011 TREE_TYPE field. We can't emit debug info for this
16012 because it is an unnamed integral type. */
16013 if (TREE_CODE (domain) == INTEGER_TYPE
16014 && TYPE_NAME (domain) == NULL_TREE
16015 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16016 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16018 else
16019 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16020 type_die);
16023 /* ??? If upper is NULL, the array has unspecified length,
16024 but it does have a lower bound. This happens with Fortran
16025 dimension arr(N:*)
16026 Since the debugger is definitely going to need to know N
16027 to produce useful results, go ahead and output the lower
16028 bound solo, and hope the debugger can cope. */
16030 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16031 if (upper)
16032 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16035 /* Otherwise we have an array type with an unspecified length. The
16036 DWARF-2 spec does not say how to handle this; let's just leave out the
16037 bounds. */
16041 static void
16042 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16044 dw_die_ref decl_die;
16045 unsigned size;
16047 switch (TREE_CODE (tree_node))
16049 case ERROR_MARK:
16050 size = 0;
16051 break;
16052 case ENUMERAL_TYPE:
16053 case RECORD_TYPE:
16054 case UNION_TYPE:
16055 case QUAL_UNION_TYPE:
16056 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16057 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16059 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16060 return;
16062 size = int_size_in_bytes (tree_node);
16063 break;
16064 case FIELD_DECL:
16065 /* For a data member of a struct or union, the DW_AT_byte_size is
16066 generally given as the number of bytes normally allocated for an
16067 object of the *declared* type of the member itself. This is true
16068 even for bit-fields. */
16069 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16070 break;
16071 default:
16072 gcc_unreachable ();
16075 /* Note that `size' might be -1 when we get to this point. If it is, that
16076 indicates that the byte size of the entity in question is variable. We
16077 have no good way of expressing this fact in Dwarf at the present time,
16078 so just let the -1 pass on through. */
16079 add_AT_unsigned (die, DW_AT_byte_size, size);
16082 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16083 which specifies the distance in bits from the highest order bit of the
16084 "containing object" for the bit-field to the highest order bit of the
16085 bit-field itself.
16087 For any given bit-field, the "containing object" is a hypothetical object
16088 (of some integral or enum type) within which the given bit-field lives. The
16089 type of this hypothetical "containing object" is always the same as the
16090 declared type of the individual bit-field itself. The determination of the
16091 exact location of the "containing object" for a bit-field is rather
16092 complicated. It's handled by the `field_byte_offset' function (above).
16094 Note that it is the size (in bytes) of the hypothetical "containing object"
16095 which will be given in the DW_AT_byte_size attribute for this bit-field.
16096 (See `byte_size_attribute' above). */
16098 static inline void
16099 add_bit_offset_attribute (dw_die_ref die, tree decl)
16101 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16102 tree type = DECL_BIT_FIELD_TYPE (decl);
16103 HOST_WIDE_INT bitpos_int;
16104 HOST_WIDE_INT highest_order_object_bit_offset;
16105 HOST_WIDE_INT highest_order_field_bit_offset;
16106 HOST_WIDE_INT bit_offset;
16108 /* Must be a field and a bit field. */
16109 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16111 /* We can't yet handle bit-fields whose offsets are variable, so if we
16112 encounter such things, just return without generating any attribute
16113 whatsoever. Likewise for variable or too large size. */
16114 if (! host_integerp (bit_position (decl), 0)
16115 || ! host_integerp (DECL_SIZE (decl), 1))
16116 return;
16118 bitpos_int = int_bit_position (decl);
16120 /* Note that the bit offset is always the distance (in bits) from the
16121 highest-order bit of the "containing object" to the highest-order bit of
16122 the bit-field itself. Since the "high-order end" of any object or field
16123 is different on big-endian and little-endian machines, the computation
16124 below must take account of these differences. */
16125 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16126 highest_order_field_bit_offset = bitpos_int;
16128 if (! BYTES_BIG_ENDIAN)
16130 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16131 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16134 bit_offset
16135 = (! BYTES_BIG_ENDIAN
16136 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16137 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16139 if (bit_offset < 0)
16140 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16141 else
16142 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16145 /* For a FIELD_DECL node which represents a bit field, output an attribute
16146 which specifies the length in bits of the given field. */
16148 static inline void
16149 add_bit_size_attribute (dw_die_ref die, tree decl)
16151 /* Must be a field and a bit field. */
16152 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16153 && DECL_BIT_FIELD_TYPE (decl));
16155 if (host_integerp (DECL_SIZE (decl), 1))
16156 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16159 /* If the compiled language is ANSI C, then add a 'prototyped'
16160 attribute, if arg types are given for the parameters of a function. */
16162 static inline void
16163 add_prototyped_attribute (dw_die_ref die, tree func_type)
16165 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16166 && prototype_p (func_type))
16167 add_AT_flag (die, DW_AT_prototyped, 1);
16170 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16171 by looking in either the type declaration or object declaration
16172 equate table. */
16174 static inline dw_die_ref
16175 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16177 dw_die_ref origin_die = NULL;
16179 if (TREE_CODE (origin) != FUNCTION_DECL)
16181 /* We may have gotten separated from the block for the inlined
16182 function, if we're in an exception handler or some such; make
16183 sure that the abstract function has been written out.
16185 Doing this for nested functions is wrong, however; functions are
16186 distinct units, and our context might not even be inline. */
16187 tree fn = origin;
16189 if (TYPE_P (fn))
16190 fn = TYPE_STUB_DECL (fn);
16192 fn = decl_function_context (fn);
16193 if (fn)
16194 dwarf2out_abstract_function (fn);
16197 if (DECL_P (origin))
16198 origin_die = lookup_decl_die (origin);
16199 else if (TYPE_P (origin))
16200 origin_die = lookup_type_die (origin);
16202 /* XXX: Functions that are never lowered don't always have correct block
16203 trees (in the case of java, they simply have no block tree, in some other
16204 languages). For these functions, there is nothing we can really do to
16205 output correct debug info for inlined functions in all cases. Rather
16206 than die, we'll just produce deficient debug info now, in that we will
16207 have variables without a proper abstract origin. In the future, when all
16208 functions are lowered, we should re-add a gcc_assert (origin_die)
16209 here. */
16211 if (origin_die)
16212 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16213 return origin_die;
16216 /* We do not currently support the pure_virtual attribute. */
16218 static inline void
16219 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16221 if (DECL_VINDEX (func_decl))
16223 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16225 if (host_integerp (DECL_VINDEX (func_decl), 0))
16226 add_AT_loc (die, DW_AT_vtable_elem_location,
16227 new_loc_descr (DW_OP_constu,
16228 tree_low_cst (DECL_VINDEX (func_decl), 0),
16229 0));
16231 /* GNU extension: Record what type this method came from originally. */
16232 if (debug_info_level > DINFO_LEVEL_TERSE
16233 && DECL_CONTEXT (func_decl))
16234 add_AT_die_ref (die, DW_AT_containing_type,
16235 lookup_type_die (DECL_CONTEXT (func_decl)));
16239 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16240 given decl. This used to be a vendor extension until after DWARF 4
16241 standardized it. */
16243 static void
16244 add_linkage_attr (dw_die_ref die, tree decl)
16246 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16248 /* Mimic what assemble_name_raw does with a leading '*'. */
16249 if (name[0] == '*')
16250 name = &name[1];
16252 if (dwarf_version >= 4)
16253 add_AT_string (die, DW_AT_linkage_name, name);
16254 else
16255 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16258 /* Add source coordinate attributes for the given decl. */
16260 static void
16261 add_src_coords_attributes (dw_die_ref die, tree decl)
16263 expanded_location s;
16265 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16266 return;
16267 s = expand_location (DECL_SOURCE_LOCATION (decl));
16268 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16269 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16272 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16274 static void
16275 add_linkage_name (dw_die_ref die, tree decl)
16277 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16278 && TREE_PUBLIC (decl)
16279 && !DECL_ABSTRACT (decl)
16280 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16281 && die->die_tag != DW_TAG_member)
16283 /* Defer until we have an assembler name set. */
16284 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16286 limbo_die_node *asm_name;
16288 asm_name = ggc_alloc_cleared_limbo_die_node ();
16289 asm_name->die = die;
16290 asm_name->created_for = decl;
16291 asm_name->next = deferred_asm_name;
16292 deferred_asm_name = asm_name;
16294 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16295 add_linkage_attr (die, decl);
16299 /* Add a DW_AT_name attribute and source coordinate attribute for the
16300 given decl, but only if it actually has a name. */
16302 static void
16303 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16305 tree decl_name;
16307 decl_name = DECL_NAME (decl);
16308 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16310 const char *name = dwarf2_name (decl, 0);
16311 if (name)
16312 add_name_attribute (die, name);
16313 if (! DECL_ARTIFICIAL (decl))
16314 add_src_coords_attributes (die, decl);
16316 add_linkage_name (die, decl);
16319 #ifdef VMS_DEBUGGING_INFO
16320 /* Get the function's name, as described by its RTL. This may be different
16321 from the DECL_NAME name used in the source file. */
16322 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16324 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16325 XEXP (DECL_RTL (decl), 0), false);
16326 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16328 #endif /* VMS_DEBUGGING_INFO */
16331 #ifdef VMS_DEBUGGING_INFO
16332 /* Output the debug main pointer die for VMS */
16334 void
16335 dwarf2out_vms_debug_main_pointer (void)
16337 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16338 dw_die_ref die;
16340 /* Allocate the VMS debug main subprogram die. */
16341 die = ggc_alloc_cleared_die_node ();
16342 die->die_tag = DW_TAG_subprogram;
16343 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16344 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16345 current_function_funcdef_no);
16346 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16348 /* Make it the first child of comp_unit_die (). */
16349 die->die_parent = comp_unit_die ();
16350 if (comp_unit_die ()->die_child)
16352 die->die_sib = comp_unit_die ()->die_child->die_sib;
16353 comp_unit_die ()->die_child->die_sib = die;
16355 else
16357 die->die_sib = die;
16358 comp_unit_die ()->die_child = die;
16361 #endif /* VMS_DEBUGGING_INFO */
16363 /* Push a new declaration scope. */
16365 static void
16366 push_decl_scope (tree scope)
16368 vec_safe_push (decl_scope_table, scope);
16371 /* Pop a declaration scope. */
16373 static inline void
16374 pop_decl_scope (void)
16376 decl_scope_table->pop ();
16379 /* walk_tree helper function for uses_local_type, below. */
16381 static tree
16382 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16384 if (!TYPE_P (*tp))
16385 *walk_subtrees = 0;
16386 else
16388 tree name = TYPE_NAME (*tp);
16389 if (name && DECL_P (name) && decl_function_context (name))
16390 return *tp;
16392 return NULL_TREE;
16395 /* If TYPE involves a function-local type (including a local typedef to a
16396 non-local type), returns that type; otherwise returns NULL_TREE. */
16398 static tree
16399 uses_local_type (tree type)
16401 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16402 return used;
16405 /* Return the DIE for the scope that immediately contains this type.
16406 Non-named types that do not involve a function-local type get global
16407 scope. Named types nested in namespaces or other types get their
16408 containing scope. All other types (i.e. function-local named types) get
16409 the current active scope. */
16411 static dw_die_ref
16412 scope_die_for (tree t, dw_die_ref context_die)
16414 dw_die_ref scope_die = NULL;
16415 tree containing_scope;
16417 /* Non-types always go in the current scope. */
16418 gcc_assert (TYPE_P (t));
16420 /* Use the scope of the typedef, rather than the scope of the type
16421 it refers to. */
16422 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16423 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16424 else
16425 containing_scope = TYPE_CONTEXT (t);
16427 /* Use the containing namespace if there is one. */
16428 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16430 if (context_die == lookup_decl_die (containing_scope))
16431 /* OK */;
16432 else if (debug_info_level > DINFO_LEVEL_TERSE)
16433 context_die = get_context_die (containing_scope);
16434 else
16435 containing_scope = NULL_TREE;
16438 /* Ignore function type "scopes" from the C frontend. They mean that
16439 a tagged type is local to a parmlist of a function declarator, but
16440 that isn't useful to DWARF. */
16441 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16442 containing_scope = NULL_TREE;
16444 if (SCOPE_FILE_SCOPE_P (containing_scope))
16446 /* If T uses a local type keep it local as well, to avoid references
16447 to function-local DIEs from outside the function. */
16448 if (current_function_decl && uses_local_type (t))
16449 scope_die = context_die;
16450 else
16451 scope_die = comp_unit_die ();
16453 else if (TYPE_P (containing_scope))
16455 /* For types, we can just look up the appropriate DIE. */
16456 if (debug_info_level > DINFO_LEVEL_TERSE)
16457 scope_die = get_context_die (containing_scope);
16458 else
16460 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16461 if (scope_die == NULL)
16462 scope_die = comp_unit_die ();
16465 else
16466 scope_die = context_die;
16468 return scope_die;
16471 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16473 static inline int
16474 local_scope_p (dw_die_ref context_die)
16476 for (; context_die; context_die = context_die->die_parent)
16477 if (context_die->die_tag == DW_TAG_inlined_subroutine
16478 || context_die->die_tag == DW_TAG_subprogram)
16479 return 1;
16481 return 0;
16484 /* Returns nonzero if CONTEXT_DIE is a class. */
16486 static inline int
16487 class_scope_p (dw_die_ref context_die)
16489 return (context_die
16490 && (context_die->die_tag == DW_TAG_structure_type
16491 || context_die->die_tag == DW_TAG_class_type
16492 || context_die->die_tag == DW_TAG_interface_type
16493 || context_die->die_tag == DW_TAG_union_type));
16496 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16497 whether or not to treat a DIE in this context as a declaration. */
16499 static inline int
16500 class_or_namespace_scope_p (dw_die_ref context_die)
16502 return (class_scope_p (context_die)
16503 || (context_die && context_die->die_tag == DW_TAG_namespace));
16506 /* Many forms of DIEs require a "type description" attribute. This
16507 routine locates the proper "type descriptor" die for the type given
16508 by 'type', and adds a DW_AT_type attribute below the given die. */
16510 static void
16511 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16512 int decl_volatile, dw_die_ref context_die)
16514 enum tree_code code = TREE_CODE (type);
16515 dw_die_ref type_die = NULL;
16516 int type_quals;
16518 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16519 or fixed-point type, use the inner type. This is because we have no
16520 support for unnamed types in base_type_die. This can happen if this is
16521 an Ada subrange type. Correct solution is emit a subrange type die. */
16522 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16523 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16524 type = TREE_TYPE (type), code = TREE_CODE (type);
16526 if (code == ERROR_MARK
16527 /* Handle a special case. For functions whose return type is void, we
16528 generate *no* type attribute. (Note that no object may have type
16529 `void', so this only applies to function return types). */
16530 || code == VOID_TYPE)
16531 return;
16533 type_quals = TYPE_QUALS (type)
16534 | (decl_const * TYPE_QUAL_CONST)
16535 | (decl_volatile * TYPE_QUAL_VOLATILE);
16536 type_die = modified_type_die (type, type_quals, context_die);
16538 if (type_die != NULL)
16539 add_AT_die_ref (object_die, DW_AT_type, type_die);
16542 /* Given an object die, add the calling convention attribute for the
16543 function call type. */
16544 static void
16545 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16547 enum dwarf_calling_convention value = DW_CC_normal;
16549 value = ((enum dwarf_calling_convention)
16550 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16552 if (is_fortran ()
16553 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16555 /* DWARF 2 doesn't provide a way to identify a program's source-level
16556 entry point. DW_AT_calling_convention attributes are only meant
16557 to describe functions' calling conventions. However, lacking a
16558 better way to signal the Fortran main program, we used this for
16559 a long time, following existing custom. Now, DWARF 4 has
16560 DW_AT_main_subprogram, which we add below, but some tools still
16561 rely on the old way, which we thus keep. */
16562 value = DW_CC_program;
16564 if (dwarf_version >= 4 || !dwarf_strict)
16565 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16568 /* Only add the attribute if the backend requests it, and
16569 is not DW_CC_normal. */
16570 if (value && (value != DW_CC_normal))
16571 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16574 /* Given a tree pointer to a struct, class, union, or enum type node, return
16575 a pointer to the (string) tag name for the given type, or zero if the type
16576 was declared without a tag. */
16578 static const char *
16579 type_tag (const_tree type)
16581 const char *name = 0;
16583 if (TYPE_NAME (type) != 0)
16585 tree t = 0;
16587 /* Find the IDENTIFIER_NODE for the type name. */
16588 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16589 && !TYPE_NAMELESS (type))
16590 t = TYPE_NAME (type);
16592 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16593 a TYPE_DECL node, regardless of whether or not a `typedef' was
16594 involved. */
16595 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16596 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16598 /* We want to be extra verbose. Don't call dwarf_name if
16599 DECL_NAME isn't set. The default hook for decl_printable_name
16600 doesn't like that, and in this context it's correct to return
16601 0, instead of "<anonymous>" or the like. */
16602 if (DECL_NAME (TYPE_NAME (type))
16603 && !DECL_NAMELESS (TYPE_NAME (type)))
16604 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16607 /* Now get the name as a string, or invent one. */
16608 if (!name && t != 0)
16609 name = IDENTIFIER_POINTER (t);
16612 return (name == 0 || *name == '\0') ? 0 : name;
16615 /* Return the type associated with a data member, make a special check
16616 for bit field types. */
16618 static inline tree
16619 member_declared_type (const_tree member)
16621 return (DECL_BIT_FIELD_TYPE (member)
16622 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16625 /* Get the decl's label, as described by its RTL. This may be different
16626 from the DECL_NAME name used in the source file. */
16628 #if 0
16629 static const char *
16630 decl_start_label (tree decl)
16632 rtx x;
16633 const char *fnname;
16635 x = DECL_RTL (decl);
16636 gcc_assert (MEM_P (x));
16638 x = XEXP (x, 0);
16639 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16641 fnname = XSTR (x, 0);
16642 return fnname;
16644 #endif
16646 /* These routines generate the internal representation of the DIE's for
16647 the compilation unit. Debugging information is collected by walking
16648 the declaration trees passed in from dwarf2out_decl(). */
16650 static void
16651 gen_array_type_die (tree type, dw_die_ref context_die)
16653 dw_die_ref scope_die = scope_die_for (type, context_die);
16654 dw_die_ref array_die;
16656 /* GNU compilers represent multidimensional array types as sequences of one
16657 dimensional array types whose element types are themselves array types.
16658 We sometimes squish that down to a single array_type DIE with multiple
16659 subscripts in the Dwarf debugging info. The draft Dwarf specification
16660 say that we are allowed to do this kind of compression in C, because
16661 there is no difference between an array of arrays and a multidimensional
16662 array. We don't do this for Ada to remain as close as possible to the
16663 actual representation, which is especially important against the language
16664 flexibilty wrt arrays of variable size. */
16666 bool collapse_nested_arrays = !is_ada ();
16667 tree element_type;
16669 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16670 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16671 if (TYPE_STRING_FLAG (type)
16672 && TREE_CODE (type) == ARRAY_TYPE
16673 && is_fortran ()
16674 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16676 HOST_WIDE_INT size;
16678 array_die = new_die (DW_TAG_string_type, scope_die, type);
16679 add_name_attribute (array_die, type_tag (type));
16680 equate_type_number_to_die (type, array_die);
16681 size = int_size_in_bytes (type);
16682 if (size >= 0)
16683 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16684 else if (TYPE_DOMAIN (type) != NULL_TREE
16685 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16686 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16688 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16689 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16691 size = int_size_in_bytes (TREE_TYPE (szdecl));
16692 if (loc && size > 0)
16694 add_AT_location_description (array_die, DW_AT_string_length, loc);
16695 if (size != DWARF2_ADDR_SIZE)
16696 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16699 return;
16702 array_die = new_die (DW_TAG_array_type, scope_die, type);
16703 add_name_attribute (array_die, type_tag (type));
16704 equate_type_number_to_die (type, array_die);
16706 if (TREE_CODE (type) == VECTOR_TYPE)
16707 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16709 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16710 if (is_fortran ()
16711 && TREE_CODE (type) == ARRAY_TYPE
16712 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16713 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16714 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16716 #if 0
16717 /* We default the array ordering. SDB will probably do
16718 the right things even if DW_AT_ordering is not present. It's not even
16719 an issue until we start to get into multidimensional arrays anyway. If
16720 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16721 then we'll have to put the DW_AT_ordering attribute back in. (But if
16722 and when we find out that we need to put these in, we will only do so
16723 for multidimensional arrays. */
16724 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16725 #endif
16727 if (TREE_CODE (type) == VECTOR_TYPE)
16729 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16730 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16731 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16732 add_bound_info (subrange_die, DW_AT_upper_bound,
16733 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16735 else
16736 add_subscript_info (array_die, type, collapse_nested_arrays);
16738 /* Add representation of the type of the elements of this array type and
16739 emit the corresponding DIE if we haven't done it already. */
16740 element_type = TREE_TYPE (type);
16741 if (collapse_nested_arrays)
16742 while (TREE_CODE (element_type) == ARRAY_TYPE)
16744 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16745 break;
16746 element_type = TREE_TYPE (element_type);
16749 add_type_attribute (array_die, element_type, 0, 0, context_die);
16751 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16752 if (TYPE_ARTIFICIAL (type))
16753 add_AT_flag (array_die, DW_AT_artificial, 1);
16755 if (get_AT (array_die, DW_AT_name))
16756 add_pubtype (type, array_die);
16759 static dw_loc_descr_ref
16760 descr_info_loc (tree val, tree base_decl)
16762 HOST_WIDE_INT size;
16763 dw_loc_descr_ref loc, loc2;
16764 enum dwarf_location_atom op;
16766 if (val == base_decl)
16767 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16769 switch (TREE_CODE (val))
16771 CASE_CONVERT:
16772 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16773 case VAR_DECL:
16774 return loc_descriptor_from_tree (val, 0);
16775 case INTEGER_CST:
16776 if (host_integerp (val, 0))
16777 return int_loc_descriptor (tree_low_cst (val, 0));
16778 break;
16779 case INDIRECT_REF:
16780 size = int_size_in_bytes (TREE_TYPE (val));
16781 if (size < 0)
16782 break;
16783 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16784 if (!loc)
16785 break;
16786 if (size == DWARF2_ADDR_SIZE)
16787 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16788 else
16789 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16790 return loc;
16791 case POINTER_PLUS_EXPR:
16792 case PLUS_EXPR:
16793 if (host_integerp (TREE_OPERAND (val, 1), 1)
16794 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16795 < 16384)
16797 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16798 if (!loc)
16799 break;
16800 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16802 else
16804 op = DW_OP_plus;
16805 do_binop:
16806 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16807 if (!loc)
16808 break;
16809 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16810 if (!loc2)
16811 break;
16812 add_loc_descr (&loc, loc2);
16813 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16815 return loc;
16816 case MINUS_EXPR:
16817 op = DW_OP_minus;
16818 goto do_binop;
16819 case MULT_EXPR:
16820 op = DW_OP_mul;
16821 goto do_binop;
16822 case EQ_EXPR:
16823 op = DW_OP_eq;
16824 goto do_binop;
16825 case NE_EXPR:
16826 op = DW_OP_ne;
16827 goto do_binop;
16828 default:
16829 break;
16831 return NULL;
16834 static void
16835 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16836 tree val, tree base_decl)
16838 dw_loc_descr_ref loc;
16840 if (host_integerp (val, 0))
16842 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16843 return;
16846 loc = descr_info_loc (val, base_decl);
16847 if (!loc)
16848 return;
16850 add_AT_loc (die, attr, loc);
16853 /* This routine generates DIE for array with hidden descriptor, details
16854 are filled into *info by a langhook. */
16856 static void
16857 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16858 dw_die_ref context_die)
16860 dw_die_ref scope_die = scope_die_for (type, context_die);
16861 dw_die_ref array_die;
16862 int dim;
16864 array_die = new_die (DW_TAG_array_type, scope_die, type);
16865 add_name_attribute (array_die, type_tag (type));
16866 equate_type_number_to_die (type, array_die);
16868 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16869 if (is_fortran ()
16870 && info->ndimensions >= 2)
16871 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16873 if (info->data_location)
16874 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16875 info->base_decl);
16876 if (info->associated)
16877 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16878 info->base_decl);
16879 if (info->allocated)
16880 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16881 info->base_decl);
16883 for (dim = 0; dim < info->ndimensions; dim++)
16885 dw_die_ref subrange_die
16886 = new_die (DW_TAG_subrange_type, array_die, NULL);
16888 if (info->dimen[dim].lower_bound)
16890 /* If it is the default value, omit it. */
16891 int dflt;
16893 if (host_integerp (info->dimen[dim].lower_bound, 0)
16894 && (dflt = lower_bound_default ()) != -1
16895 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16897 else
16898 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16899 info->dimen[dim].lower_bound,
16900 info->base_decl);
16902 if (info->dimen[dim].upper_bound)
16903 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16904 info->dimen[dim].upper_bound,
16905 info->base_decl);
16906 if (info->dimen[dim].stride)
16907 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16908 info->dimen[dim].stride,
16909 info->base_decl);
16912 gen_type_die (info->element_type, context_die);
16913 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16915 if (get_AT (array_die, DW_AT_name))
16916 add_pubtype (type, array_die);
16919 #if 0
16920 static void
16921 gen_entry_point_die (tree decl, dw_die_ref context_die)
16923 tree origin = decl_ultimate_origin (decl);
16924 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16926 if (origin != NULL)
16927 add_abstract_origin_attribute (decl_die, origin);
16928 else
16930 add_name_and_src_coords_attributes (decl_die, decl);
16931 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16932 0, 0, context_die);
16935 if (DECL_ABSTRACT (decl))
16936 equate_decl_number_to_die (decl, decl_die);
16937 else
16938 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16940 #endif
16942 /* Walk through the list of incomplete types again, trying once more to
16943 emit full debugging info for them. */
16945 static void
16946 retry_incomplete_types (void)
16948 int i;
16950 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
16951 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
16952 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
16955 /* Determine what tag to use for a record type. */
16957 static enum dwarf_tag
16958 record_type_tag (tree type)
16960 if (! lang_hooks.types.classify_record)
16961 return DW_TAG_structure_type;
16963 switch (lang_hooks.types.classify_record (type))
16965 case RECORD_IS_STRUCT:
16966 return DW_TAG_structure_type;
16968 case RECORD_IS_CLASS:
16969 return DW_TAG_class_type;
16971 case RECORD_IS_INTERFACE:
16972 if (dwarf_version >= 3 || !dwarf_strict)
16973 return DW_TAG_interface_type;
16974 return DW_TAG_structure_type;
16976 default:
16977 gcc_unreachable ();
16981 /* Generate a DIE to represent an enumeration type. Note that these DIEs
16982 include all of the information about the enumeration values also. Each
16983 enumerated type name/value is listed as a child of the enumerated type
16984 DIE. */
16986 static dw_die_ref
16987 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16989 dw_die_ref type_die = lookup_type_die (type);
16991 if (type_die == NULL)
16993 type_die = new_die (DW_TAG_enumeration_type,
16994 scope_die_for (type, context_die), type);
16995 equate_type_number_to_die (type, type_die);
16996 add_name_attribute (type_die, type_tag (type));
16997 if (dwarf_version >= 4 || !dwarf_strict)
16999 if (ENUM_IS_SCOPED (type))
17000 add_AT_flag (type_die, DW_AT_enum_class, 1);
17001 if (ENUM_IS_OPAQUE (type))
17002 add_AT_flag (type_die, DW_AT_declaration, 1);
17005 else if (! TYPE_SIZE (type))
17006 return type_die;
17007 else
17008 remove_AT (type_die, DW_AT_declaration);
17010 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17011 given enum type is incomplete, do not generate the DW_AT_byte_size
17012 attribute or the DW_AT_element_list attribute. */
17013 if (TYPE_SIZE (type))
17015 tree link;
17017 TREE_ASM_WRITTEN (type) = 1;
17018 add_byte_size_attribute (type_die, type);
17019 if (TYPE_STUB_DECL (type) != NULL_TREE)
17021 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17022 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17025 /* If the first reference to this type was as the return type of an
17026 inline function, then it may not have a parent. Fix this now. */
17027 if (type_die->die_parent == NULL)
17028 add_child_die (scope_die_for (type, context_die), type_die);
17030 for (link = TYPE_VALUES (type);
17031 link != NULL; link = TREE_CHAIN (link))
17033 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17034 tree value = TREE_VALUE (link);
17036 add_name_attribute (enum_die,
17037 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17039 if (TREE_CODE (value) == CONST_DECL)
17040 value = DECL_INITIAL (value);
17042 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
17043 /* DWARF2 does not provide a way of indicating whether or
17044 not enumeration constants are signed or unsigned. GDB
17045 always assumes the values are signed, so we output all
17046 values as if they were signed. That means that
17047 enumeration constants with very large unsigned values
17048 will appear to have negative values in the debugger. */
17049 add_AT_int (enum_die, DW_AT_const_value,
17050 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17053 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17054 if (TYPE_ARTIFICIAL (type))
17055 add_AT_flag (type_die, DW_AT_artificial, 1);
17057 else
17058 add_AT_flag (type_die, DW_AT_declaration, 1);
17060 add_pubtype (type, type_die);
17062 return type_die;
17065 /* Generate a DIE to represent either a real live formal parameter decl or to
17066 represent just the type of some formal parameter position in some function
17067 type.
17069 Note that this routine is a bit unusual because its argument may be a
17070 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17071 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17072 node. If it's the former then this function is being called to output a
17073 DIE to represent a formal parameter object (or some inlining thereof). If
17074 it's the latter, then this function is only being called to output a
17075 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17076 argument type of some subprogram type.
17077 If EMIT_NAME_P is true, name and source coordinate attributes
17078 are emitted. */
17080 static dw_die_ref
17081 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17082 dw_die_ref context_die)
17084 tree node_or_origin = node ? node : origin;
17085 tree ultimate_origin;
17086 dw_die_ref parm_die
17087 = new_die (DW_TAG_formal_parameter, context_die, node);
17089 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17091 case tcc_declaration:
17092 ultimate_origin = decl_ultimate_origin (node_or_origin);
17093 if (node || ultimate_origin)
17094 origin = ultimate_origin;
17095 if (origin != NULL)
17096 add_abstract_origin_attribute (parm_die, origin);
17097 else if (emit_name_p)
17098 add_name_and_src_coords_attributes (parm_die, node);
17099 if (origin == NULL
17100 || (! DECL_ABSTRACT (node_or_origin)
17101 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17102 decl_function_context
17103 (node_or_origin))))
17105 tree type = TREE_TYPE (node_or_origin);
17106 if (decl_by_reference_p (node_or_origin))
17107 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17108 context_die);
17109 else
17110 add_type_attribute (parm_die, type,
17111 TREE_READONLY (node_or_origin),
17112 TREE_THIS_VOLATILE (node_or_origin),
17113 context_die);
17115 if (origin == NULL && DECL_ARTIFICIAL (node))
17116 add_AT_flag (parm_die, DW_AT_artificial, 1);
17118 if (node && node != origin)
17119 equate_decl_number_to_die (node, parm_die);
17120 if (! DECL_ABSTRACT (node_or_origin))
17121 add_location_or_const_value_attribute (parm_die, node_or_origin,
17122 node == NULL, DW_AT_location);
17124 break;
17126 case tcc_type:
17127 /* We were called with some kind of a ..._TYPE node. */
17128 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17129 break;
17131 default:
17132 gcc_unreachable ();
17135 return parm_die;
17138 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17139 children DW_TAG_formal_parameter DIEs representing the arguments of the
17140 parameter pack.
17142 PARM_PACK must be a function parameter pack.
17143 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17144 must point to the subsequent arguments of the function PACK_ARG belongs to.
17145 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17146 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17147 following the last one for which a DIE was generated. */
17149 static dw_die_ref
17150 gen_formal_parameter_pack_die (tree parm_pack,
17151 tree pack_arg,
17152 dw_die_ref subr_die,
17153 tree *next_arg)
17155 tree arg;
17156 dw_die_ref parm_pack_die;
17158 gcc_assert (parm_pack
17159 && lang_hooks.function_parameter_pack_p (parm_pack)
17160 && subr_die);
17162 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17163 add_src_coords_attributes (parm_pack_die, parm_pack);
17165 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17167 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17168 parm_pack))
17169 break;
17170 gen_formal_parameter_die (arg, NULL,
17171 false /* Don't emit name attribute. */,
17172 parm_pack_die);
17174 if (next_arg)
17175 *next_arg = arg;
17176 return parm_pack_die;
17179 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17180 at the end of an (ANSI prototyped) formal parameters list. */
17182 static void
17183 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17185 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17188 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17189 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17190 parameters as specified in some function type specification (except for
17191 those which appear as part of a function *definition*). */
17193 static void
17194 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17196 tree link;
17197 tree formal_type = NULL;
17198 tree first_parm_type;
17199 tree arg;
17201 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17203 arg = DECL_ARGUMENTS (function_or_method_type);
17204 function_or_method_type = TREE_TYPE (function_or_method_type);
17206 else
17207 arg = NULL_TREE;
17209 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17211 /* Make our first pass over the list of formal parameter types and output a
17212 DW_TAG_formal_parameter DIE for each one. */
17213 for (link = first_parm_type; link; )
17215 dw_die_ref parm_die;
17217 formal_type = TREE_VALUE (link);
17218 if (formal_type == void_type_node)
17219 break;
17221 /* Output a (nameless) DIE to represent the formal parameter itself. */
17222 parm_die = gen_formal_parameter_die (formal_type, NULL,
17223 true /* Emit name attribute. */,
17224 context_die);
17225 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17226 && link == first_parm_type)
17228 add_AT_flag (parm_die, DW_AT_artificial, 1);
17229 if (dwarf_version >= 3 || !dwarf_strict)
17230 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17232 else if (arg && DECL_ARTIFICIAL (arg))
17233 add_AT_flag (parm_die, DW_AT_artificial, 1);
17235 link = TREE_CHAIN (link);
17236 if (arg)
17237 arg = DECL_CHAIN (arg);
17240 /* If this function type has an ellipsis, add a
17241 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17242 if (formal_type != void_type_node)
17243 gen_unspecified_parameters_die (function_or_method_type, context_die);
17245 /* Make our second (and final) pass over the list of formal parameter types
17246 and output DIEs to represent those types (as necessary). */
17247 for (link = TYPE_ARG_TYPES (function_or_method_type);
17248 link && TREE_VALUE (link);
17249 link = TREE_CHAIN (link))
17250 gen_type_die (TREE_VALUE (link), context_die);
17253 /* We want to generate the DIE for TYPE so that we can generate the
17254 die for MEMBER, which has been defined; we will need to refer back
17255 to the member declaration nested within TYPE. If we're trying to
17256 generate minimal debug info for TYPE, processing TYPE won't do the
17257 trick; we need to attach the member declaration by hand. */
17259 static void
17260 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17262 gen_type_die (type, context_die);
17264 /* If we're trying to avoid duplicate debug info, we may not have
17265 emitted the member decl for this function. Emit it now. */
17266 if (TYPE_STUB_DECL (type)
17267 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17268 && ! lookup_decl_die (member))
17270 dw_die_ref type_die;
17271 gcc_assert (!decl_ultimate_origin (member));
17273 push_decl_scope (type);
17274 type_die = lookup_type_die_strip_naming_typedef (type);
17275 if (TREE_CODE (member) == FUNCTION_DECL)
17276 gen_subprogram_die (member, type_die);
17277 else if (TREE_CODE (member) == FIELD_DECL)
17279 /* Ignore the nameless fields that are used to skip bits but handle
17280 C++ anonymous unions and structs. */
17281 if (DECL_NAME (member) != NULL_TREE
17282 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17283 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17285 gen_type_die (member_declared_type (member), type_die);
17286 gen_field_die (member, type_die);
17289 else
17290 gen_variable_die (member, NULL_TREE, type_die);
17292 pop_decl_scope ();
17296 /* Forward declare these functions, because they are mutually recursive
17297 with their set_block_* pairing functions. */
17298 static void set_decl_origin_self (tree);
17299 static void set_decl_abstract_flags (tree, int);
17301 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17302 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17303 that it points to the node itself, thus indicating that the node is its
17304 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17305 the given node is NULL, recursively descend the decl/block tree which
17306 it is the root of, and for each other ..._DECL or BLOCK node contained
17307 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17308 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17309 values to point to themselves. */
17311 static void
17312 set_block_origin_self (tree stmt)
17314 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17316 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17319 tree local_decl;
17321 for (local_decl = BLOCK_VARS (stmt);
17322 local_decl != NULL_TREE;
17323 local_decl = DECL_CHAIN (local_decl))
17324 if (! DECL_EXTERNAL (local_decl))
17325 set_decl_origin_self (local_decl); /* Potential recursion. */
17329 tree subblock;
17331 for (subblock = BLOCK_SUBBLOCKS (stmt);
17332 subblock != NULL_TREE;
17333 subblock = BLOCK_CHAIN (subblock))
17334 set_block_origin_self (subblock); /* Recurse. */
17339 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17340 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17341 node to so that it points to the node itself, thus indicating that the
17342 node represents its own (abstract) origin. Additionally, if the
17343 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17344 the decl/block tree of which the given node is the root of, and for
17345 each other ..._DECL or BLOCK node contained therein whose
17346 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17347 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17348 point to themselves. */
17350 static void
17351 set_decl_origin_self (tree decl)
17353 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17355 DECL_ABSTRACT_ORIGIN (decl) = decl;
17356 if (TREE_CODE (decl) == FUNCTION_DECL)
17358 tree arg;
17360 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17361 DECL_ABSTRACT_ORIGIN (arg) = arg;
17362 if (DECL_INITIAL (decl) != NULL_TREE
17363 && DECL_INITIAL (decl) != error_mark_node)
17364 set_block_origin_self (DECL_INITIAL (decl));
17369 /* Given a pointer to some BLOCK node, and a boolean value to set the
17370 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17371 the given block, and for all local decls and all local sub-blocks
17372 (recursively) which are contained therein. */
17374 static void
17375 set_block_abstract_flags (tree stmt, int setting)
17377 tree local_decl;
17378 tree subblock;
17379 unsigned int i;
17381 BLOCK_ABSTRACT (stmt) = setting;
17383 for (local_decl = BLOCK_VARS (stmt);
17384 local_decl != NULL_TREE;
17385 local_decl = DECL_CHAIN (local_decl))
17386 if (! DECL_EXTERNAL (local_decl))
17387 set_decl_abstract_flags (local_decl, setting);
17389 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17391 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17392 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17393 || TREE_CODE (local_decl) == PARM_DECL)
17394 set_decl_abstract_flags (local_decl, setting);
17397 for (subblock = BLOCK_SUBBLOCKS (stmt);
17398 subblock != NULL_TREE;
17399 subblock = BLOCK_CHAIN (subblock))
17400 set_block_abstract_flags (subblock, setting);
17403 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17404 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17405 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17406 set the abstract flags for all of the parameters, local vars, local
17407 blocks and sub-blocks (recursively) to the same setting. */
17409 static void
17410 set_decl_abstract_flags (tree decl, int setting)
17412 DECL_ABSTRACT (decl) = setting;
17413 if (TREE_CODE (decl) == FUNCTION_DECL)
17415 tree arg;
17417 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17418 DECL_ABSTRACT (arg) = setting;
17419 if (DECL_INITIAL (decl) != NULL_TREE
17420 && DECL_INITIAL (decl) != error_mark_node)
17421 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17425 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17426 may later generate inlined and/or out-of-line instances of. */
17428 static void
17429 dwarf2out_abstract_function (tree decl)
17431 dw_die_ref old_die;
17432 tree save_fn;
17433 tree context;
17434 int was_abstract;
17435 htab_t old_decl_loc_table;
17436 htab_t old_cached_dw_loc_list_table;
17437 int old_call_site_count, old_tail_call_site_count;
17438 struct call_arg_loc_node *old_call_arg_locations;
17440 /* Make sure we have the actual abstract inline, not a clone. */
17441 decl = DECL_ORIGIN (decl);
17443 old_die = lookup_decl_die (decl);
17444 if (old_die && get_AT (old_die, DW_AT_inline))
17445 /* We've already generated the abstract instance. */
17446 return;
17448 /* We can be called while recursively when seeing block defining inlined subroutine
17449 DIE. Be sure to not clobber the outer location table nor use it or we would
17450 get locations in abstract instantces. */
17451 old_decl_loc_table = decl_loc_table;
17452 decl_loc_table = NULL;
17453 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17454 cached_dw_loc_list_table = NULL;
17455 old_call_arg_locations = call_arg_locations;
17456 call_arg_locations = NULL;
17457 old_call_site_count = call_site_count;
17458 call_site_count = -1;
17459 old_tail_call_site_count = tail_call_site_count;
17460 tail_call_site_count = -1;
17462 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17463 we don't get confused by DECL_ABSTRACT. */
17464 if (debug_info_level > DINFO_LEVEL_TERSE)
17466 context = decl_class_context (decl);
17467 if (context)
17468 gen_type_die_for_member
17469 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17472 /* Pretend we've just finished compiling this function. */
17473 save_fn = current_function_decl;
17474 current_function_decl = decl;
17476 was_abstract = DECL_ABSTRACT (decl);
17477 set_decl_abstract_flags (decl, 1);
17478 dwarf2out_decl (decl);
17479 if (! was_abstract)
17480 set_decl_abstract_flags (decl, 0);
17482 current_function_decl = save_fn;
17483 decl_loc_table = old_decl_loc_table;
17484 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17485 call_arg_locations = old_call_arg_locations;
17486 call_site_count = old_call_site_count;
17487 tail_call_site_count = old_tail_call_site_count;
17490 /* Helper function of premark_used_types() which gets called through
17491 htab_traverse.
17493 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17494 marked as unused by prune_unused_types. */
17496 static int
17497 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17499 tree type;
17500 dw_die_ref die;
17502 type = (tree) *slot;
17503 die = lookup_type_die (type);
17504 if (die != NULL)
17505 die->die_perennial_p = 1;
17506 return 1;
17509 /* Helper function of premark_types_used_by_global_vars which gets called
17510 through htab_traverse.
17512 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17513 marked as unused by prune_unused_types. The DIE of the type is marked
17514 only if the global variable using the type will actually be emitted. */
17516 static int
17517 premark_types_used_by_global_vars_helper (void **slot,
17518 void *data ATTRIBUTE_UNUSED)
17520 struct types_used_by_vars_entry *entry;
17521 dw_die_ref die;
17523 entry = (struct types_used_by_vars_entry *) *slot;
17524 gcc_assert (entry->type != NULL
17525 && entry->var_decl != NULL);
17526 die = lookup_type_die (entry->type);
17527 if (die)
17529 /* Ask cgraph if the global variable really is to be emitted.
17530 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17531 struct varpool_node *node = varpool_get_node (entry->var_decl);
17532 if (node && node->analyzed)
17534 die->die_perennial_p = 1;
17535 /* Keep the parent DIEs as well. */
17536 while ((die = die->die_parent) && die->die_perennial_p == 0)
17537 die->die_perennial_p = 1;
17540 return 1;
17543 /* Mark all members of used_types_hash as perennial. */
17545 static void
17546 premark_used_types (struct function *fun)
17548 if (fun && fun->used_types_hash)
17549 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17552 /* Mark all members of types_used_by_vars_entry as perennial. */
17554 static void
17555 premark_types_used_by_global_vars (void)
17557 if (types_used_by_vars_hash)
17558 htab_traverse (types_used_by_vars_hash,
17559 premark_types_used_by_global_vars_helper, NULL);
17562 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17563 for CA_LOC call arg loc node. */
17565 static dw_die_ref
17566 gen_call_site_die (tree decl, dw_die_ref subr_die,
17567 struct call_arg_loc_node *ca_loc)
17569 dw_die_ref stmt_die = NULL, die;
17570 tree block = ca_loc->block;
17572 while (block
17573 && block != DECL_INITIAL (decl)
17574 && TREE_CODE (block) == BLOCK)
17576 if (block_map.length () > BLOCK_NUMBER (block))
17577 stmt_die = block_map[BLOCK_NUMBER (block)];
17578 if (stmt_die)
17579 break;
17580 block = BLOCK_SUPERCONTEXT (block);
17582 if (stmt_die == NULL)
17583 stmt_die = subr_die;
17584 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17585 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17586 if (ca_loc->tail_call_p)
17587 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17588 if (ca_loc->symbol_ref)
17590 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17591 if (tdie)
17592 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17593 else
17594 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17596 return die;
17599 /* Generate a DIE to represent a declared function (either file-scope or
17600 block-local). */
17602 static void
17603 gen_subprogram_die (tree decl, dw_die_ref context_die)
17605 tree origin = decl_ultimate_origin (decl);
17606 dw_die_ref subr_die;
17607 tree outer_scope;
17608 dw_die_ref old_die = lookup_decl_die (decl);
17609 int declaration = (current_function_decl != decl
17610 || class_or_namespace_scope_p (context_die));
17612 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17614 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17615 started to generate the abstract instance of an inline, decided to output
17616 its containing class, and proceeded to emit the declaration of the inline
17617 from the member list for the class. If so, DECLARATION takes priority;
17618 we'll get back to the abstract instance when done with the class. */
17620 /* The class-scope declaration DIE must be the primary DIE. */
17621 if (origin && declaration && class_or_namespace_scope_p (context_die))
17623 origin = NULL;
17624 gcc_assert (!old_die);
17627 /* Now that the C++ front end lazily declares artificial member fns, we
17628 might need to retrofit the declaration into its class. */
17629 if (!declaration && !origin && !old_die
17630 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17631 && !class_or_namespace_scope_p (context_die)
17632 && debug_info_level > DINFO_LEVEL_TERSE)
17633 old_die = force_decl_die (decl);
17635 if (origin != NULL)
17637 gcc_assert (!declaration || local_scope_p (context_die));
17639 /* Fixup die_parent for the abstract instance of a nested
17640 inline function. */
17641 if (old_die && old_die->die_parent == NULL)
17642 add_child_die (context_die, old_die);
17644 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17645 add_abstract_origin_attribute (subr_die, origin);
17646 /* This is where the actual code for a cloned function is.
17647 Let's emit linkage name attribute for it. This helps
17648 debuggers to e.g, set breakpoints into
17649 constructors/destructors when the user asks "break
17650 K::K". */
17651 add_linkage_name (subr_die, decl);
17653 else if (old_die)
17655 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17656 struct dwarf_file_data * file_index = lookup_filename (s.file);
17658 if (!get_AT_flag (old_die, DW_AT_declaration)
17659 /* We can have a normal definition following an inline one in the
17660 case of redefinition of GNU C extern inlines.
17661 It seems reasonable to use AT_specification in this case. */
17662 && !get_AT (old_die, DW_AT_inline))
17664 /* Detect and ignore this case, where we are trying to output
17665 something we have already output. */
17666 return;
17669 /* If the definition comes from the same place as the declaration,
17670 maybe use the old DIE. We always want the DIE for this function
17671 that has the *_pc attributes to be under comp_unit_die so the
17672 debugger can find it. We also need to do this for abstract
17673 instances of inlines, since the spec requires the out-of-line copy
17674 to have the same parent. For local class methods, this doesn't
17675 apply; we just use the old DIE. */
17676 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17677 && (DECL_ARTIFICIAL (decl)
17678 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17679 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17680 == (unsigned) s.line))))
17682 subr_die = old_die;
17684 /* Clear out the declaration attribute and the formal parameters.
17685 Do not remove all children, because it is possible that this
17686 declaration die was forced using force_decl_die(). In such
17687 cases die that forced declaration die (e.g. TAG_imported_module)
17688 is one of the children that we do not want to remove. */
17689 remove_AT (subr_die, DW_AT_declaration);
17690 remove_AT (subr_die, DW_AT_object_pointer);
17691 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17693 else
17695 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17696 add_AT_specification (subr_die, old_die);
17697 add_pubname (decl, subr_die);
17698 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17699 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17700 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17701 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17704 else
17706 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17708 if (TREE_PUBLIC (decl))
17709 add_AT_flag (subr_die, DW_AT_external, 1);
17711 add_name_and_src_coords_attributes (subr_die, decl);
17712 add_pubname (decl, subr_die);
17713 if (debug_info_level > DINFO_LEVEL_TERSE)
17715 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17716 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17717 0, 0, context_die);
17720 add_pure_or_virtual_attribute (subr_die, decl);
17721 if (DECL_ARTIFICIAL (decl))
17722 add_AT_flag (subr_die, DW_AT_artificial, 1);
17724 add_accessibility_attribute (subr_die, decl);
17727 if (declaration)
17729 if (!old_die || !get_AT (old_die, DW_AT_inline))
17731 add_AT_flag (subr_die, DW_AT_declaration, 1);
17733 /* If this is an explicit function declaration then generate
17734 a DW_AT_explicit attribute. */
17735 if (lang_hooks.decls.function_decl_explicit_p (decl)
17736 && (dwarf_version >= 3 || !dwarf_strict))
17737 add_AT_flag (subr_die, DW_AT_explicit, 1);
17739 /* The first time we see a member function, it is in the context of
17740 the class to which it belongs. We make sure of this by emitting
17741 the class first. The next time is the definition, which is
17742 handled above. The two may come from the same source text.
17744 Note that force_decl_die() forces function declaration die. It is
17745 later reused to represent definition. */
17746 equate_decl_number_to_die (decl, subr_die);
17749 else if (DECL_ABSTRACT (decl))
17751 if (DECL_DECLARED_INLINE_P (decl))
17753 if (cgraph_function_possibly_inlined_p (decl))
17754 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17755 else
17756 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17758 else
17760 if (cgraph_function_possibly_inlined_p (decl))
17761 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17762 else
17763 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17766 if (DECL_DECLARED_INLINE_P (decl)
17767 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17768 add_AT_flag (subr_die, DW_AT_artificial, 1);
17770 equate_decl_number_to_die (decl, subr_die);
17772 else if (!DECL_EXTERNAL (decl))
17774 HOST_WIDE_INT cfa_fb_offset;
17775 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17777 if (!old_die || !get_AT (old_die, DW_AT_inline))
17778 equate_decl_number_to_die (decl, subr_die);
17780 gcc_checking_assert (fun);
17781 if (!flag_reorder_blocks_and_partition)
17783 dw_fde_ref fde = fun->fde;
17784 if (fde->dw_fde_begin)
17786 /* We have already generated the labels. */
17787 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17788 fde->dw_fde_end, false);
17790 else
17792 /* Create start/end labels and add the range. */
17793 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17794 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17795 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17796 current_function_funcdef_no);
17797 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17798 current_function_funcdef_no);
17799 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17800 false);
17803 #if VMS_DEBUGGING_INFO
17804 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17805 Section 2.3 Prologue and Epilogue Attributes:
17806 When a breakpoint is set on entry to a function, it is generally
17807 desirable for execution to be suspended, not on the very first
17808 instruction of the function, but rather at a point after the
17809 function's frame has been set up, after any language defined local
17810 declaration processing has been completed, and before execution of
17811 the first statement of the function begins. Debuggers generally
17812 cannot properly determine where this point is. Similarly for a
17813 breakpoint set on exit from a function. The prologue and epilogue
17814 attributes allow a compiler to communicate the location(s) to use. */
17817 if (fde->dw_fde_vms_end_prologue)
17818 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17819 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17821 if (fde->dw_fde_vms_begin_epilogue)
17822 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17823 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17825 #endif
17828 else
17830 /* Generate pubnames entries for the split function code ranges. */
17831 dw_fde_ref fde = fun->fde;
17833 if (fde->dw_fde_second_begin)
17835 if (dwarf_version >= 3 || !dwarf_strict)
17837 /* We should use ranges for non-contiguous code section
17838 addresses. Use the actual code range for the initial
17839 section, since the HOT/COLD labels might precede an
17840 alignment offset. */
17841 bool range_list_added = false;
17842 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17843 fde->dw_fde_end, &range_list_added,
17844 false);
17845 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17846 fde->dw_fde_second_end,
17847 &range_list_added, false);
17848 if (range_list_added)
17849 add_ranges (NULL);
17851 else
17853 /* There is no real support in DW2 for this .. so we make
17854 a work-around. First, emit the pub name for the segment
17855 containing the function label. Then make and emit a
17856 simplified subprogram DIE for the second segment with the
17857 name pre-fixed by __hot/cold_sect_of_. We use the same
17858 linkage name for the second die so that gdb will find both
17859 sections when given "b foo". */
17860 const char *name = NULL;
17861 tree decl_name = DECL_NAME (decl);
17862 dw_die_ref seg_die;
17864 /* Do the 'primary' section. */
17865 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17866 fde->dw_fde_end, false);
17868 /* Build a minimal DIE for the secondary section. */
17869 seg_die = new_die (DW_TAG_subprogram,
17870 subr_die->die_parent, decl);
17872 if (TREE_PUBLIC (decl))
17873 add_AT_flag (seg_die, DW_AT_external, 1);
17875 if (decl_name != NULL
17876 && IDENTIFIER_POINTER (decl_name) != NULL)
17878 name = dwarf2_name (decl, 1);
17879 if (! DECL_ARTIFICIAL (decl))
17880 add_src_coords_attributes (seg_die, decl);
17882 add_linkage_name (seg_die, decl);
17884 gcc_assert (name != NULL);
17885 add_pure_or_virtual_attribute (seg_die, decl);
17886 if (DECL_ARTIFICIAL (decl))
17887 add_AT_flag (seg_die, DW_AT_artificial, 1);
17889 name = concat ("__second_sect_of_", name, NULL);
17890 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17891 fde->dw_fde_second_end, false);
17892 add_name_attribute (seg_die, name);
17893 if (want_pubnames ())
17894 add_pubname_string (name, seg_die);
17897 else
17898 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17899 false);
17902 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17904 /* We define the "frame base" as the function's CFA. This is more
17905 convenient for several reasons: (1) It's stable across the prologue
17906 and epilogue, which makes it better than just a frame pointer,
17907 (2) With dwarf3, there exists a one-byte encoding that allows us
17908 to reference the .debug_frame data by proxy, but failing that,
17909 (3) We can at least reuse the code inspection and interpretation
17910 code that determines the CFA position at various points in the
17911 function. */
17912 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17914 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17915 add_AT_loc (subr_die, DW_AT_frame_base, op);
17917 else
17919 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17920 if (list->dw_loc_next)
17921 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17922 else
17923 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17926 /* Compute a displacement from the "steady-state frame pointer" to
17927 the CFA. The former is what all stack slots and argument slots
17928 will reference in the rtl; the latter is what we've told the
17929 debugger about. We'll need to adjust all frame_base references
17930 by this displacement. */
17931 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17933 if (fun->static_chain_decl)
17934 add_AT_location_description (subr_die, DW_AT_static_link,
17935 loc_list_from_tree (fun->static_chain_decl, 2));
17938 /* Generate child dies for template paramaters. */
17939 if (debug_info_level > DINFO_LEVEL_TERSE)
17940 gen_generic_params_dies (decl);
17942 /* Now output descriptions of the arguments for this function. This gets
17943 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17944 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17945 `...' at the end of the formal parameter list. In order to find out if
17946 there was a trailing ellipsis or not, we must instead look at the type
17947 associated with the FUNCTION_DECL. This will be a node of type
17948 FUNCTION_TYPE. If the chain of type nodes hanging off of this
17949 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17950 an ellipsis at the end. */
17952 /* In the case where we are describing a mere function declaration, all we
17953 need to do here (and all we *can* do here) is to describe the *types* of
17954 its formal parameters. */
17955 if (debug_info_level <= DINFO_LEVEL_TERSE)
17957 else if (declaration)
17958 gen_formal_types_die (decl, subr_die);
17959 else
17961 /* Generate DIEs to represent all known formal parameters. */
17962 tree parm = DECL_ARGUMENTS (decl);
17963 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17964 tree generic_decl_parm = generic_decl
17965 ? DECL_ARGUMENTS (generic_decl)
17966 : NULL;
17968 /* Now we want to walk the list of parameters of the function and
17969 emit their relevant DIEs.
17971 We consider the case of DECL being an instance of a generic function
17972 as well as it being a normal function.
17974 If DECL is an instance of a generic function we walk the
17975 parameters of the generic function declaration _and_ the parameters of
17976 DECL itself. This is useful because we want to emit specific DIEs for
17977 function parameter packs and those are declared as part of the
17978 generic function declaration. In that particular case,
17979 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17980 That DIE has children DIEs representing the set of arguments
17981 of the pack. Note that the set of pack arguments can be empty.
17982 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17983 children DIE.
17985 Otherwise, we just consider the parameters of DECL. */
17986 while (generic_decl_parm || parm)
17988 if (generic_decl_parm
17989 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17990 gen_formal_parameter_pack_die (generic_decl_parm,
17991 parm, subr_die,
17992 &parm);
17993 else if (parm)
17995 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17997 if (parm == DECL_ARGUMENTS (decl)
17998 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17999 && parm_die
18000 && (dwarf_version >= 3 || !dwarf_strict))
18001 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18003 parm = DECL_CHAIN (parm);
18006 if (generic_decl_parm)
18007 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18010 /* Decide whether we need an unspecified_parameters DIE at the end.
18011 There are 2 more cases to do this for: 1) the ansi ... declaration -
18012 this is detectable when the end of the arg list is not a
18013 void_type_node 2) an unprototyped function declaration (not a
18014 definition). This just means that we have no info about the
18015 parameters at all. */
18016 if (prototype_p (TREE_TYPE (decl)))
18018 /* This is the prototyped case, check for.... */
18019 if (stdarg_p (TREE_TYPE (decl)))
18020 gen_unspecified_parameters_die (decl, subr_die);
18022 else if (DECL_INITIAL (decl) == NULL_TREE)
18023 gen_unspecified_parameters_die (decl, subr_die);
18026 /* Output Dwarf info for all of the stuff within the body of the function
18027 (if it has one - it may be just a declaration). */
18028 outer_scope = DECL_INITIAL (decl);
18030 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18031 a function. This BLOCK actually represents the outermost binding contour
18032 for the function, i.e. the contour in which the function's formal
18033 parameters and labels get declared. Curiously, it appears that the front
18034 end doesn't actually put the PARM_DECL nodes for the current function onto
18035 the BLOCK_VARS list for this outer scope, but are strung off of the
18036 DECL_ARGUMENTS list for the function instead.
18038 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18039 the LABEL_DECL nodes for the function however, and we output DWARF info
18040 for those in decls_for_scope. Just within the `outer_scope' there will be
18041 a BLOCK node representing the function's outermost pair of curly braces,
18042 and any blocks used for the base and member initializers of a C++
18043 constructor function. */
18044 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18046 int call_site_note_count = 0;
18047 int tail_call_site_note_count = 0;
18049 /* Emit a DW_TAG_variable DIE for a named return value. */
18050 if (DECL_NAME (DECL_RESULT (decl)))
18051 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18053 current_function_has_inlines = 0;
18054 decls_for_scope (outer_scope, subr_die, 0);
18056 if (call_arg_locations && !dwarf_strict)
18058 struct call_arg_loc_node *ca_loc;
18059 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18061 dw_die_ref die = NULL;
18062 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18063 rtx arg, next_arg;
18065 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18066 arg; arg = next_arg)
18068 dw_loc_descr_ref reg, val;
18069 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18070 dw_die_ref cdie, tdie = NULL;
18072 next_arg = XEXP (arg, 1);
18073 if (REG_P (XEXP (XEXP (arg, 0), 0))
18074 && next_arg
18075 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18076 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18077 && REGNO (XEXP (XEXP (arg, 0), 0))
18078 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18079 next_arg = XEXP (next_arg, 1);
18080 if (mode == VOIDmode)
18082 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18083 if (mode == VOIDmode)
18084 mode = GET_MODE (XEXP (arg, 0));
18086 if (mode == VOIDmode || mode == BLKmode)
18087 continue;
18088 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18090 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18091 tloc = XEXP (XEXP (arg, 0), 1);
18092 continue;
18094 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18095 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18097 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18098 tlocc = XEXP (XEXP (arg, 0), 1);
18099 continue;
18101 reg = NULL;
18102 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18103 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18104 VAR_INIT_STATUS_INITIALIZED);
18105 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18107 rtx mem = XEXP (XEXP (arg, 0), 0);
18108 reg = mem_loc_descriptor (XEXP (mem, 0),
18109 get_address_mode (mem),
18110 GET_MODE (mem),
18111 VAR_INIT_STATUS_INITIALIZED);
18113 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18114 == DEBUG_PARAMETER_REF)
18116 tree tdecl
18117 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18118 tdie = lookup_decl_die (tdecl);
18119 if (tdie == NULL)
18120 continue;
18122 else
18123 continue;
18124 if (reg == NULL
18125 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18126 != DEBUG_PARAMETER_REF)
18127 continue;
18128 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18129 VOIDmode,
18130 VAR_INIT_STATUS_INITIALIZED);
18131 if (val == NULL)
18132 continue;
18133 if (die == NULL)
18134 die = gen_call_site_die (decl, subr_die, ca_loc);
18135 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18136 NULL_TREE);
18137 if (reg != NULL)
18138 add_AT_loc (cdie, DW_AT_location, reg);
18139 else if (tdie != NULL)
18140 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18141 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18142 if (next_arg != XEXP (arg, 1))
18144 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18145 if (mode == VOIDmode)
18146 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18147 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18148 0), 1),
18149 mode, VOIDmode,
18150 VAR_INIT_STATUS_INITIALIZED);
18151 if (val != NULL)
18152 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18155 if (die == NULL
18156 && (ca_loc->symbol_ref || tloc))
18157 die = gen_call_site_die (decl, subr_die, ca_loc);
18158 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18160 dw_loc_descr_ref tval = NULL;
18162 if (tloc != NULL_RTX)
18163 tval = mem_loc_descriptor (tloc,
18164 GET_MODE (tloc) == VOIDmode
18165 ? Pmode : GET_MODE (tloc),
18166 VOIDmode,
18167 VAR_INIT_STATUS_INITIALIZED);
18168 if (tval)
18169 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18170 else if (tlocc != NULL_RTX)
18172 tval = mem_loc_descriptor (tlocc,
18173 GET_MODE (tlocc) == VOIDmode
18174 ? Pmode : GET_MODE (tlocc),
18175 VOIDmode,
18176 VAR_INIT_STATUS_INITIALIZED);
18177 if (tval)
18178 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18179 tval);
18182 if (die != NULL)
18184 call_site_note_count++;
18185 if (ca_loc->tail_call_p)
18186 tail_call_site_note_count++;
18190 call_arg_locations = NULL;
18191 call_arg_loc_last = NULL;
18192 if (tail_call_site_count >= 0
18193 && tail_call_site_count == tail_call_site_note_count
18194 && !dwarf_strict)
18196 if (call_site_count >= 0
18197 && call_site_count == call_site_note_count)
18198 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18199 else
18200 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18202 call_site_count = -1;
18203 tail_call_site_count = -1;
18205 /* Add the calling convention attribute if requested. */
18206 add_calling_convention_attribute (subr_die, decl);
18210 /* Returns a hash value for X (which really is a die_struct). */
18212 static hashval_t
18213 common_block_die_table_hash (const void *x)
18215 const_dw_die_ref d = (const_dw_die_ref) x;
18216 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18219 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18220 as decl_id and die_parent of die_struct Y. */
18222 static int
18223 common_block_die_table_eq (const void *x, const void *y)
18225 const_dw_die_ref d = (const_dw_die_ref) x;
18226 const_dw_die_ref e = (const_dw_die_ref) y;
18227 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18230 /* Generate a DIE to represent a declared data object.
18231 Either DECL or ORIGIN must be non-null. */
18233 static void
18234 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18236 HOST_WIDE_INT off = 0;
18237 tree com_decl;
18238 tree decl_or_origin = decl ? decl : origin;
18239 tree ultimate_origin;
18240 dw_die_ref var_die;
18241 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18242 dw_die_ref origin_die;
18243 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18244 || class_or_namespace_scope_p (context_die));
18245 bool specialization_p = false;
18247 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18248 if (decl || ultimate_origin)
18249 origin = ultimate_origin;
18250 com_decl = fortran_common (decl_or_origin, &off);
18252 /* Symbol in common gets emitted as a child of the common block, in the form
18253 of a data member. */
18254 if (com_decl)
18256 dw_die_ref com_die;
18257 dw_loc_list_ref loc;
18258 die_node com_die_arg;
18260 var_die = lookup_decl_die (decl_or_origin);
18261 if (var_die)
18263 if (get_AT (var_die, DW_AT_location) == NULL)
18265 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18266 if (loc)
18268 if (off)
18270 /* Optimize the common case. */
18271 if (single_element_loc_list_p (loc)
18272 && loc->expr->dw_loc_opc == DW_OP_addr
18273 && loc->expr->dw_loc_next == NULL
18274 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18275 == SYMBOL_REF)
18277 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18278 loc->expr->dw_loc_oprnd1.v.val_addr
18279 = plus_constant (GET_MODE (x), x , off);
18281 else
18282 loc_list_plus_const (loc, off);
18284 add_AT_location_description (var_die, DW_AT_location, loc);
18285 remove_AT (var_die, DW_AT_declaration);
18288 return;
18291 if (common_block_die_table == NULL)
18292 common_block_die_table
18293 = htab_create_ggc (10, common_block_die_table_hash,
18294 common_block_die_table_eq, NULL);
18296 com_die_arg.decl_id = DECL_UID (com_decl);
18297 com_die_arg.die_parent = context_die;
18298 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18299 loc = loc_list_from_tree (com_decl, 2);
18300 if (com_die == NULL)
18302 const char *cnam
18303 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18304 void **slot;
18306 com_die = new_die (DW_TAG_common_block, context_die, decl);
18307 add_name_and_src_coords_attributes (com_die, com_decl);
18308 if (loc)
18310 add_AT_location_description (com_die, DW_AT_location, loc);
18311 /* Avoid sharing the same loc descriptor between
18312 DW_TAG_common_block and DW_TAG_variable. */
18313 loc = loc_list_from_tree (com_decl, 2);
18315 else if (DECL_EXTERNAL (decl))
18316 add_AT_flag (com_die, DW_AT_declaration, 1);
18317 if (want_pubnames ())
18318 add_pubname_string (cnam, com_die); /* ??? needed? */
18319 com_die->decl_id = DECL_UID (com_decl);
18320 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18321 *slot = (void *) com_die;
18323 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18325 add_AT_location_description (com_die, DW_AT_location, loc);
18326 loc = loc_list_from_tree (com_decl, 2);
18327 remove_AT (com_die, DW_AT_declaration);
18329 var_die = new_die (DW_TAG_variable, com_die, decl);
18330 add_name_and_src_coords_attributes (var_die, decl);
18331 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18332 TREE_THIS_VOLATILE (decl), context_die);
18333 add_AT_flag (var_die, DW_AT_external, 1);
18334 if (loc)
18336 if (off)
18338 /* Optimize the common case. */
18339 if (single_element_loc_list_p (loc)
18340 && loc->expr->dw_loc_opc == DW_OP_addr
18341 && loc->expr->dw_loc_next == NULL
18342 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18344 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18345 loc->expr->dw_loc_oprnd1.v.val_addr
18346 = plus_constant (GET_MODE (x), x, off);
18348 else
18349 loc_list_plus_const (loc, off);
18351 add_AT_location_description (var_die, DW_AT_location, loc);
18353 else if (DECL_EXTERNAL (decl))
18354 add_AT_flag (var_die, DW_AT_declaration, 1);
18355 equate_decl_number_to_die (decl, var_die);
18356 return;
18359 /* If the compiler emitted a definition for the DECL declaration
18360 and if we already emitted a DIE for it, don't emit a second
18361 DIE for it again. Allow re-declarations of DECLs that are
18362 inside functions, though. */
18363 if (old_die && declaration && !local_scope_p (context_die))
18364 return;
18366 /* For static data members, the declaration in the class is supposed
18367 to have DW_TAG_member tag; the specification should still be
18368 DW_TAG_variable referencing the DW_TAG_member DIE. */
18369 if (declaration && class_scope_p (context_die))
18370 var_die = new_die (DW_TAG_member, context_die, decl);
18371 else
18372 var_die = new_die (DW_TAG_variable, context_die, decl);
18374 origin_die = NULL;
18375 if (origin != NULL)
18376 origin_die = add_abstract_origin_attribute (var_die, origin);
18378 /* Loop unrolling can create multiple blocks that refer to the same
18379 static variable, so we must test for the DW_AT_declaration flag.
18381 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18382 copy decls and set the DECL_ABSTRACT flag on them instead of
18383 sharing them.
18385 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18387 ??? The declare_in_namespace support causes us to get two DIEs for one
18388 variable, both of which are declarations. We want to avoid considering
18389 one to be a specification, so we must test that this DIE is not a
18390 declaration. */
18391 else if (old_die && TREE_STATIC (decl) && ! declaration
18392 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18394 /* This is a definition of a C++ class level static. */
18395 add_AT_specification (var_die, old_die);
18396 specialization_p = true;
18397 if (DECL_NAME (decl))
18399 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18400 struct dwarf_file_data * file_index = lookup_filename (s.file);
18402 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18403 add_AT_file (var_die, DW_AT_decl_file, file_index);
18405 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18406 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18408 if (old_die->die_tag == DW_TAG_member)
18409 add_linkage_name (var_die, decl);
18412 else
18413 add_name_and_src_coords_attributes (var_die, decl);
18415 if ((origin == NULL && !specialization_p)
18416 || (origin != NULL
18417 && !DECL_ABSTRACT (decl_or_origin)
18418 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18419 decl_function_context
18420 (decl_or_origin))))
18422 tree type = TREE_TYPE (decl_or_origin);
18424 if (decl_by_reference_p (decl_or_origin))
18425 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18426 else
18427 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18428 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18431 if (origin == NULL && !specialization_p)
18433 if (TREE_PUBLIC (decl))
18434 add_AT_flag (var_die, DW_AT_external, 1);
18436 if (DECL_ARTIFICIAL (decl))
18437 add_AT_flag (var_die, DW_AT_artificial, 1);
18439 add_accessibility_attribute (var_die, decl);
18442 if (declaration)
18443 add_AT_flag (var_die, DW_AT_declaration, 1);
18445 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18446 equate_decl_number_to_die (decl, var_die);
18448 if (! declaration
18449 && (! DECL_ABSTRACT (decl_or_origin)
18450 /* Local static vars are shared between all clones/inlines,
18451 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18452 already set. */
18453 || (TREE_CODE (decl_or_origin) == VAR_DECL
18454 && TREE_STATIC (decl_or_origin)
18455 && DECL_RTL_SET_P (decl_or_origin)))
18456 /* When abstract origin already has DW_AT_location attribute, no need
18457 to add it again. */
18458 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18460 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18461 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18462 defer_location (decl_or_origin, var_die);
18463 else
18464 add_location_or_const_value_attribute (var_die, decl_or_origin,
18465 decl == NULL, DW_AT_location);
18466 add_pubname (decl_or_origin, var_die);
18468 else
18469 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18472 /* Generate a DIE to represent a named constant. */
18474 static void
18475 gen_const_die (tree decl, dw_die_ref context_die)
18477 dw_die_ref const_die;
18478 tree type = TREE_TYPE (decl);
18480 const_die = new_die (DW_TAG_constant, context_die, decl);
18481 add_name_and_src_coords_attributes (const_die, decl);
18482 add_type_attribute (const_die, type, 1, 0, context_die);
18483 if (TREE_PUBLIC (decl))
18484 add_AT_flag (const_die, DW_AT_external, 1);
18485 if (DECL_ARTIFICIAL (decl))
18486 add_AT_flag (const_die, DW_AT_artificial, 1);
18487 tree_add_const_value_attribute_for_decl (const_die, decl);
18490 /* Generate a DIE to represent a label identifier. */
18492 static void
18493 gen_label_die (tree decl, dw_die_ref context_die)
18495 tree origin = decl_ultimate_origin (decl);
18496 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18497 rtx insn;
18498 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18500 if (origin != NULL)
18501 add_abstract_origin_attribute (lbl_die, origin);
18502 else
18503 add_name_and_src_coords_attributes (lbl_die, decl);
18505 if (DECL_ABSTRACT (decl))
18506 equate_decl_number_to_die (decl, lbl_die);
18507 else
18509 insn = DECL_RTL_IF_SET (decl);
18511 /* Deleted labels are programmer specified labels which have been
18512 eliminated because of various optimizations. We still emit them
18513 here so that it is possible to put breakpoints on them. */
18514 if (insn
18515 && (LABEL_P (insn)
18516 || ((NOTE_P (insn)
18517 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18519 /* When optimization is enabled (via -O) some parts of the compiler
18520 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18521 represent source-level labels which were explicitly declared by
18522 the user. This really shouldn't be happening though, so catch
18523 it if it ever does happen. */
18524 gcc_assert (!INSN_DELETED_P (insn));
18526 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18527 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18529 else if (insn
18530 && NOTE_P (insn)
18531 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18532 && CODE_LABEL_NUMBER (insn) != -1)
18534 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18535 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18540 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18541 attributes to the DIE for a block STMT, to describe where the inlined
18542 function was called from. This is similar to add_src_coords_attributes. */
18544 static inline void
18545 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18547 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18549 if (dwarf_version >= 3 || !dwarf_strict)
18551 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18552 add_AT_unsigned (die, DW_AT_call_line, s.line);
18557 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18558 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18560 static inline void
18561 add_high_low_attributes (tree stmt, dw_die_ref die)
18563 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18565 if (BLOCK_FRAGMENT_CHAIN (stmt)
18566 && (dwarf_version >= 3 || !dwarf_strict))
18568 tree chain, superblock = NULL_TREE;
18569 dw_die_ref pdie;
18570 dw_attr_ref attr = NULL;
18572 if (inlined_function_outer_scope_p (stmt))
18574 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18575 BLOCK_NUMBER (stmt));
18576 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18579 /* Optimize duplicate .debug_ranges lists or even tails of
18580 lists. If this BLOCK has same ranges as its supercontext,
18581 lookup DW_AT_ranges attribute in the supercontext (and
18582 recursively so), verify that the ranges_table contains the
18583 right values and use it instead of adding a new .debug_range. */
18584 for (chain = stmt, pdie = die;
18585 BLOCK_SAME_RANGE (chain);
18586 chain = BLOCK_SUPERCONTEXT (chain))
18588 dw_attr_ref new_attr;
18590 pdie = pdie->die_parent;
18591 if (pdie == NULL)
18592 break;
18593 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18594 break;
18595 new_attr = get_AT (pdie, DW_AT_ranges);
18596 if (new_attr == NULL
18597 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18598 break;
18599 attr = new_attr;
18600 superblock = BLOCK_SUPERCONTEXT (chain);
18602 if (attr != NULL
18603 && (ranges_table[attr->dw_attr_val.v.val_offset
18604 / 2 / DWARF2_ADDR_SIZE].num
18605 == BLOCK_NUMBER (superblock))
18606 && BLOCK_FRAGMENT_CHAIN (superblock))
18608 unsigned long off = attr->dw_attr_val.v.val_offset
18609 / 2 / DWARF2_ADDR_SIZE;
18610 unsigned long supercnt = 0, thiscnt = 0;
18611 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18612 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18614 ++supercnt;
18615 gcc_checking_assert (ranges_table[off + supercnt].num
18616 == BLOCK_NUMBER (chain));
18618 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18619 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18620 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18621 ++thiscnt;
18622 gcc_assert (supercnt >= thiscnt);
18623 add_AT_range_list (die, DW_AT_ranges,
18624 ((off + supercnt - thiscnt)
18625 * 2 * DWARF2_ADDR_SIZE),
18626 false);
18627 return;
18630 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18632 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18635 add_ranges (chain);
18636 chain = BLOCK_FRAGMENT_CHAIN (chain);
18638 while (chain);
18639 add_ranges (NULL);
18641 else
18643 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18644 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18645 BLOCK_NUMBER (stmt));
18646 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18647 BLOCK_NUMBER (stmt));
18648 add_AT_low_high_pc (die, label, label_high, false);
18652 /* Generate a DIE for a lexical block. */
18654 static void
18655 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18657 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18659 if (call_arg_locations)
18661 if (block_map.length () <= BLOCK_NUMBER (stmt))
18662 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18663 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18666 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18667 add_high_low_attributes (stmt, stmt_die);
18669 decls_for_scope (stmt, stmt_die, depth);
18672 /* Generate a DIE for an inlined subprogram. */
18674 static void
18675 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18677 tree decl;
18679 /* The instance of function that is effectively being inlined shall not
18680 be abstract. */
18681 gcc_assert (! BLOCK_ABSTRACT (stmt));
18683 decl = block_ultimate_origin (stmt);
18685 /* Emit info for the abstract instance first, if we haven't yet. We
18686 must emit this even if the block is abstract, otherwise when we
18687 emit the block below (or elsewhere), we may end up trying to emit
18688 a die whose origin die hasn't been emitted, and crashing. */
18689 dwarf2out_abstract_function (decl);
18691 if (! BLOCK_ABSTRACT (stmt))
18693 dw_die_ref subr_die
18694 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18696 if (call_arg_locations)
18698 if (block_map.length () <= BLOCK_NUMBER (stmt))
18699 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18700 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18702 add_abstract_origin_attribute (subr_die, decl);
18703 if (TREE_ASM_WRITTEN (stmt))
18704 add_high_low_attributes (stmt, subr_die);
18705 add_call_src_coords_attributes (stmt, subr_die);
18707 decls_for_scope (stmt, subr_die, depth);
18708 current_function_has_inlines = 1;
18712 /* Generate a DIE for a field in a record, or structure. */
18714 static void
18715 gen_field_die (tree decl, dw_die_ref context_die)
18717 dw_die_ref decl_die;
18719 if (TREE_TYPE (decl) == error_mark_node)
18720 return;
18722 decl_die = new_die (DW_TAG_member, context_die, decl);
18723 add_name_and_src_coords_attributes (decl_die, decl);
18724 add_type_attribute (decl_die, member_declared_type (decl), 0, 0, context_die);
18726 if (DECL_BIT_FIELD_TYPE (decl))
18728 add_byte_size_attribute (decl_die, decl);
18729 add_bit_size_attribute (decl_die, decl);
18730 add_bit_offset_attribute (decl_die, decl);
18733 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18734 add_data_member_location_attribute (decl_die, decl);
18736 if (DECL_ARTIFICIAL (decl))
18737 add_AT_flag (decl_die, DW_AT_artificial, 1);
18739 add_accessibility_attribute (decl_die, decl);
18741 /* Equate decl number to die, so that we can look up this decl later on. */
18742 equate_decl_number_to_die (decl, decl_die);
18745 #if 0
18746 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18747 Use modified_type_die instead.
18748 We keep this code here just in case these types of DIEs may be needed to
18749 represent certain things in other languages (e.g. Pascal) someday. */
18751 static void
18752 gen_pointer_type_die (tree type, dw_die_ref context_die)
18754 dw_die_ref ptr_die
18755 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18757 equate_type_number_to_die (type, ptr_die);
18758 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18759 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18762 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18763 Use modified_type_die instead.
18764 We keep this code here just in case these types of DIEs may be needed to
18765 represent certain things in other languages (e.g. Pascal) someday. */
18767 static void
18768 gen_reference_type_die (tree type, dw_die_ref context_die)
18770 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18772 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18773 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18774 else
18775 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18777 equate_type_number_to_die (type, ref_die);
18778 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18779 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18781 #endif
18783 /* Generate a DIE for a pointer to a member type. */
18785 static void
18786 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18788 dw_die_ref ptr_die
18789 = new_die (DW_TAG_ptr_to_member_type,
18790 scope_die_for (type, context_die), type);
18792 equate_type_number_to_die (type, ptr_die);
18793 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18794 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18795 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18798 typedef const char *dchar_p; /* For DEF_VEC_P. */
18800 static char *producer_string;
18802 /* Return a heap allocated producer string including command line options
18803 if -grecord-gcc-switches. */
18805 static char *
18806 gen_producer_string (void)
18808 size_t j;
18809 vec<dchar_p> switches = vNULL;
18810 const char *language_string = lang_hooks.name;
18811 char *producer, *tail;
18812 const char *p;
18813 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18814 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18816 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18817 switch (save_decoded_options[j].opt_index)
18819 case OPT_o:
18820 case OPT_d:
18821 case OPT_dumpbase:
18822 case OPT_dumpdir:
18823 case OPT_auxbase:
18824 case OPT_auxbase_strip:
18825 case OPT_quiet:
18826 case OPT_version:
18827 case OPT_v:
18828 case OPT_w:
18829 case OPT_L:
18830 case OPT_D:
18831 case OPT_I:
18832 case OPT_U:
18833 case OPT_SPECIAL_unknown:
18834 case OPT_SPECIAL_ignore:
18835 case OPT_SPECIAL_program_name:
18836 case OPT_SPECIAL_input_file:
18837 case OPT_grecord_gcc_switches:
18838 case OPT_gno_record_gcc_switches:
18839 case OPT__output_pch_:
18840 case OPT_fdiagnostics_show_location_:
18841 case OPT_fdiagnostics_show_option:
18842 case OPT_fdiagnostics_show_caret:
18843 case OPT_fverbose_asm:
18844 case OPT____:
18845 case OPT__sysroot_:
18846 case OPT_nostdinc:
18847 case OPT_nostdinc__:
18848 /* Ignore these. */
18849 continue;
18850 default:
18851 if (cl_options[save_decoded_options[j].opt_index].flags
18852 & CL_NO_DWARF_RECORD)
18853 continue;
18854 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18855 == '-');
18856 switch (save_decoded_options[j].canonical_option[0][1])
18858 case 'M':
18859 case 'i':
18860 case 'W':
18861 continue;
18862 case 'f':
18863 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18864 "dump", 4) == 0)
18865 continue;
18866 break;
18867 default:
18868 break;
18870 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18871 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18872 break;
18875 producer = XNEWVEC (char, plen + 1 + len + 1);
18876 tail = producer;
18877 sprintf (tail, "%s %s", language_string, version_string);
18878 tail += plen;
18880 FOR_EACH_VEC_ELT (switches, j, p)
18882 len = strlen (p);
18883 *tail = ' ';
18884 memcpy (tail + 1, p, len);
18885 tail += len + 1;
18888 *tail = '\0';
18889 switches.release ();
18890 return producer;
18893 /* Generate the DIE for the compilation unit. */
18895 static dw_die_ref
18896 gen_compile_unit_die (const char *filename)
18898 dw_die_ref die;
18899 const char *language_string = lang_hooks.name;
18900 int language;
18902 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18904 if (filename)
18906 add_name_attribute (die, filename);
18907 /* Don't add cwd for <built-in>. */
18908 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18909 add_comp_dir_attribute (die);
18912 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
18914 /* If our producer is LTO try to figure out a common language to use
18915 from the global list of translation units. */
18916 if (strcmp (language_string, "GNU GIMPLE") == 0)
18918 unsigned i;
18919 tree t;
18920 const char *common_lang = NULL;
18922 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
18924 if (!TRANSLATION_UNIT_LANGUAGE (t))
18925 continue;
18926 if (!common_lang)
18927 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18928 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18930 else if (strncmp (common_lang, "GNU C", 5) == 0
18931 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18932 /* Mixing C and C++ is ok, use C++ in that case. */
18933 common_lang = "GNU C++";
18934 else
18936 /* Fall back to C. */
18937 common_lang = NULL;
18938 break;
18942 if (common_lang)
18943 language_string = common_lang;
18946 language = DW_LANG_C89;
18947 if (strcmp (language_string, "GNU C++") == 0)
18948 language = DW_LANG_C_plus_plus;
18949 else if (strcmp (language_string, "GNU F77") == 0)
18950 language = DW_LANG_Fortran77;
18951 else if (strcmp (language_string, "GNU Pascal") == 0)
18952 language = DW_LANG_Pascal83;
18953 else if (use_upc_dwarf2_extensions
18954 && (strcmp (language_string, "GNU UPC") == 0))
18955 language = DW_LANG_Upc;
18956 else if (dwarf_version >= 3 || !dwarf_strict)
18958 if (strcmp (language_string, "GNU Ada") == 0)
18959 language = DW_LANG_Ada95;
18960 else if (strcmp (language_string, "GNU Fortran") == 0)
18961 language = DW_LANG_Fortran95;
18962 else if (strcmp (language_string, "GNU Java") == 0)
18963 language = DW_LANG_Java;
18964 else if (strcmp (language_string, "GNU Objective-C") == 0)
18965 language = DW_LANG_ObjC;
18966 else if (strcmp (language_string, "GNU Objective-C++") == 0)
18967 language = DW_LANG_ObjC_plus_plus;
18968 else if (dwarf_version >= 5 || !dwarf_strict)
18970 if (strcmp (language_string, "GNU Go") == 0)
18971 language = DW_LANG_Go;
18974 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
18975 else if (strcmp (language_string, "GNU Fortran") == 0)
18976 language = DW_LANG_Fortran90;
18978 add_AT_unsigned (die, DW_AT_language, language);
18980 switch (language)
18982 case DW_LANG_Fortran77:
18983 case DW_LANG_Fortran90:
18984 case DW_LANG_Fortran95:
18985 /* Fortran has case insensitive identifiers and the front-end
18986 lowercases everything. */
18987 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18988 break;
18989 default:
18990 /* The default DW_ID_case_sensitive doesn't need to be specified. */
18991 break;
18993 return die;
18996 /* Generate the DIE for a base class. */
18998 static void
18999 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19001 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19003 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19004 add_data_member_location_attribute (die, binfo);
19006 if (BINFO_VIRTUAL_P (binfo))
19007 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19009 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19010 children, otherwise the default is DW_ACCESS_public. In DWARF2
19011 the default has always been DW_ACCESS_private. */
19012 if (access == access_public_node)
19014 if (dwarf_version == 2
19015 || context_die->die_tag == DW_TAG_class_type)
19016 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19018 else if (access == access_protected_node)
19019 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19020 else if (dwarf_version > 2
19021 && context_die->die_tag != DW_TAG_class_type)
19022 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19025 /* Generate a DIE for a class member. */
19027 static void
19028 gen_member_die (tree type, dw_die_ref context_die)
19030 tree member;
19031 tree binfo = TYPE_BINFO (type);
19032 dw_die_ref child;
19034 /* If this is not an incomplete type, output descriptions of each of its
19035 members. Note that as we output the DIEs necessary to represent the
19036 members of this record or union type, we will also be trying to output
19037 DIEs to represent the *types* of those members. However the `type'
19038 function (above) will specifically avoid generating type DIEs for member
19039 types *within* the list of member DIEs for this (containing) type except
19040 for those types (of members) which are explicitly marked as also being
19041 members of this (containing) type themselves. The g++ front- end can
19042 force any given type to be treated as a member of some other (containing)
19043 type by setting the TYPE_CONTEXT of the given (member) type to point to
19044 the TREE node representing the appropriate (containing) type. */
19046 /* First output info about the base classes. */
19047 if (binfo)
19049 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19050 int i;
19051 tree base;
19053 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19054 gen_inheritance_die (base,
19055 (accesses ? (*accesses)[i] : access_public_node),
19056 context_die);
19059 /* Now output info about the data members and type members. */
19060 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19062 /* If we thought we were generating minimal debug info for TYPE
19063 and then changed our minds, some of the member declarations
19064 may have already been defined. Don't define them again, but
19065 do put them in the right order. */
19067 child = lookup_decl_die (member);
19068 if (child)
19069 splice_child_die (context_die, child);
19070 else
19071 gen_decl_die (member, NULL, context_die);
19074 /* Now output info about the function members (if any). */
19075 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19077 /* Don't include clones in the member list. */
19078 if (DECL_ABSTRACT_ORIGIN (member))
19079 continue;
19081 child = lookup_decl_die (member);
19082 if (child)
19083 splice_child_die (context_die, child);
19084 else
19085 gen_decl_die (member, NULL, context_die);
19089 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19090 is set, we pretend that the type was never defined, so we only get the
19091 member DIEs needed by later specification DIEs. */
19093 static void
19094 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19095 enum debug_info_usage usage)
19097 dw_die_ref type_die = lookup_type_die (type);
19098 dw_die_ref scope_die = 0;
19099 int nested = 0;
19100 int complete = (TYPE_SIZE (type)
19101 && (! TYPE_STUB_DECL (type)
19102 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19103 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19104 complete = complete && should_emit_struct_debug (type, usage);
19106 if (type_die && ! complete)
19107 return;
19109 if (TYPE_CONTEXT (type) != NULL_TREE
19110 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19111 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19112 nested = 1;
19114 scope_die = scope_die_for (type, context_die);
19116 /* Generate child dies for template paramaters. */
19117 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19118 schedule_generic_params_dies_gen (type);
19120 if (! type_die || (nested && is_cu_die (scope_die)))
19121 /* First occurrence of type or toplevel definition of nested class. */
19123 dw_die_ref old_die = type_die;
19125 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19126 ? record_type_tag (type) : DW_TAG_union_type,
19127 scope_die, type);
19128 equate_type_number_to_die (type, type_die);
19129 if (old_die)
19130 add_AT_specification (type_die, old_die);
19131 else
19132 add_name_attribute (type_die, type_tag (type));
19134 else
19135 remove_AT (type_die, DW_AT_declaration);
19137 /* If this type has been completed, then give it a byte_size attribute and
19138 then give a list of members. */
19139 if (complete && !ns_decl)
19141 /* Prevent infinite recursion in cases where the type of some member of
19142 this type is expressed in terms of this type itself. */
19143 TREE_ASM_WRITTEN (type) = 1;
19144 add_byte_size_attribute (type_die, type);
19145 if (TYPE_STUB_DECL (type) != NULL_TREE)
19147 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19148 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19151 /* If the first reference to this type was as the return type of an
19152 inline function, then it may not have a parent. Fix this now. */
19153 if (type_die->die_parent == NULL)
19154 add_child_die (scope_die, type_die);
19156 push_decl_scope (type);
19157 gen_member_die (type, type_die);
19158 pop_decl_scope ();
19160 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19161 if (TYPE_ARTIFICIAL (type))
19162 add_AT_flag (type_die, DW_AT_artificial, 1);
19164 /* GNU extension: Record what type our vtable lives in. */
19165 if (TYPE_VFIELD (type))
19167 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19169 gen_type_die (vtype, context_die);
19170 add_AT_die_ref (type_die, DW_AT_containing_type,
19171 lookup_type_die (vtype));
19174 else
19176 add_AT_flag (type_die, DW_AT_declaration, 1);
19178 /* We don't need to do this for function-local types. */
19179 if (TYPE_STUB_DECL (type)
19180 && ! decl_function_context (TYPE_STUB_DECL (type)))
19181 vec_safe_push (incomplete_types, type);
19184 if (get_AT (type_die, DW_AT_name))
19185 add_pubtype (type, type_die);
19188 /* Generate a DIE for a subroutine _type_. */
19190 static void
19191 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19193 tree return_type = TREE_TYPE (type);
19194 dw_die_ref subr_die
19195 = new_die (DW_TAG_subroutine_type,
19196 scope_die_for (type, context_die), type);
19198 equate_type_number_to_die (type, subr_die);
19199 add_prototyped_attribute (subr_die, type);
19200 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19201 gen_formal_types_die (type, subr_die);
19203 if (get_AT (subr_die, DW_AT_name))
19204 add_pubtype (type, subr_die);
19207 /* Generate a DIE for a type definition. */
19209 static void
19210 gen_typedef_die (tree decl, dw_die_ref context_die)
19212 dw_die_ref type_die;
19213 tree origin;
19215 if (TREE_ASM_WRITTEN (decl))
19216 return;
19218 TREE_ASM_WRITTEN (decl) = 1;
19219 type_die = new_die (DW_TAG_typedef, context_die, decl);
19220 origin = decl_ultimate_origin (decl);
19221 if (origin != NULL)
19222 add_abstract_origin_attribute (type_die, origin);
19223 else
19225 tree type;
19227 add_name_and_src_coords_attributes (type_die, decl);
19228 if (DECL_ORIGINAL_TYPE (decl))
19230 type = DECL_ORIGINAL_TYPE (decl);
19232 gcc_assert (type != TREE_TYPE (decl));
19233 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19235 else
19237 type = TREE_TYPE (decl);
19239 if (is_naming_typedef_decl (TYPE_NAME (type)))
19241 /* Here, we are in the case of decl being a typedef naming
19242 an anonymous type, e.g:
19243 typedef struct {...} foo;
19244 In that case TREE_TYPE (decl) is not a typedef variant
19245 type and TYPE_NAME of the anonymous type is set to the
19246 TYPE_DECL of the typedef. This construct is emitted by
19247 the C++ FE.
19249 TYPE is the anonymous struct named by the typedef
19250 DECL. As we need the DW_AT_type attribute of the
19251 DW_TAG_typedef to point to the DIE of TYPE, let's
19252 generate that DIE right away. add_type_attribute
19253 called below will then pick (via lookup_type_die) that
19254 anonymous struct DIE. */
19255 if (!TREE_ASM_WRITTEN (type))
19256 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19258 /* This is a GNU Extension. We are adding a
19259 DW_AT_linkage_name attribute to the DIE of the
19260 anonymous struct TYPE. The value of that attribute
19261 is the name of the typedef decl naming the anonymous
19262 struct. This greatly eases the work of consumers of
19263 this debug info. */
19264 add_linkage_attr (lookup_type_die (type), decl);
19268 add_type_attribute (type_die, type, TREE_READONLY (decl),
19269 TREE_THIS_VOLATILE (decl), context_die);
19271 if (is_naming_typedef_decl (decl))
19272 /* We want that all subsequent calls to lookup_type_die with
19273 TYPE in argument yield the DW_TAG_typedef we have just
19274 created. */
19275 equate_type_number_to_die (type, type_die);
19277 add_accessibility_attribute (type_die, decl);
19280 if (DECL_ABSTRACT (decl))
19281 equate_decl_number_to_die (decl, type_die);
19283 if (get_AT (type_die, DW_AT_name))
19284 add_pubtype (decl, type_die);
19287 /* Generate a DIE for a struct, class, enum or union type. */
19289 static void
19290 gen_tagged_type_die (tree type,
19291 dw_die_ref context_die,
19292 enum debug_info_usage usage)
19294 int need_pop;
19296 if (type == NULL_TREE
19297 || !is_tagged_type (type))
19298 return;
19300 /* If this is a nested type whose containing class hasn't been written
19301 out yet, writing it out will cover this one, too. This does not apply
19302 to instantiations of member class templates; they need to be added to
19303 the containing class as they are generated. FIXME: This hurts the
19304 idea of combining type decls from multiple TUs, since we can't predict
19305 what set of template instantiations we'll get. */
19306 if (TYPE_CONTEXT (type)
19307 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19308 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19310 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19312 if (TREE_ASM_WRITTEN (type))
19313 return;
19315 /* If that failed, attach ourselves to the stub. */
19316 push_decl_scope (TYPE_CONTEXT (type));
19317 context_die = lookup_type_die (TYPE_CONTEXT (type));
19318 need_pop = 1;
19320 else if (TYPE_CONTEXT (type) != NULL_TREE
19321 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19323 /* If this type is local to a function that hasn't been written
19324 out yet, use a NULL context for now; it will be fixed up in
19325 decls_for_scope. */
19326 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19327 /* A declaration DIE doesn't count; nested types need to go in the
19328 specification. */
19329 if (context_die && is_declaration_die (context_die))
19330 context_die = NULL;
19331 need_pop = 0;
19333 else
19335 context_die = declare_in_namespace (type, context_die);
19336 need_pop = 0;
19339 if (TREE_CODE (type) == ENUMERAL_TYPE)
19341 /* This might have been written out by the call to
19342 declare_in_namespace. */
19343 if (!TREE_ASM_WRITTEN (type))
19344 gen_enumeration_type_die (type, context_die);
19346 else
19347 gen_struct_or_union_type_die (type, context_die, usage);
19349 if (need_pop)
19350 pop_decl_scope ();
19352 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19353 it up if it is ever completed. gen_*_type_die will set it for us
19354 when appropriate. */
19357 /* Generate a type description DIE. */
19359 static void
19360 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19361 enum debug_info_usage usage)
19363 struct array_descr_info info;
19365 if (type == NULL_TREE || type == error_mark_node)
19366 return;
19368 if (TYPE_NAME (type) != NULL_TREE
19369 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19370 && is_redundant_typedef (TYPE_NAME (type))
19371 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19372 /* The DECL of this type is a typedef we don't want to emit debug
19373 info for but we want debug info for its underlying typedef.
19374 This can happen for e.g, the injected-class-name of a C++
19375 type. */
19376 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19378 /* If TYPE is a typedef type variant, let's generate debug info
19379 for the parent typedef which TYPE is a type of. */
19380 if (typedef_variant_p (type))
19382 if (TREE_ASM_WRITTEN (type))
19383 return;
19385 /* Prevent broken recursion; we can't hand off to the same type. */
19386 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19388 /* Give typedefs the right scope. */
19389 context_die = scope_die_for (type, context_die);
19391 TREE_ASM_WRITTEN (type) = 1;
19393 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19394 return;
19397 /* If type is an anonymous tagged type named by a typedef, let's
19398 generate debug info for the typedef. */
19399 if (is_naming_typedef_decl (TYPE_NAME (type)))
19401 /* Use the DIE of the containing namespace as the parent DIE of
19402 the type description DIE we want to generate. */
19403 if (DECL_CONTEXT (TYPE_NAME (type))
19404 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19405 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19407 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19408 return;
19411 /* If this is an array type with hidden descriptor, handle it first. */
19412 if (!TREE_ASM_WRITTEN (type)
19413 && lang_hooks.types.get_array_descr_info
19414 && lang_hooks.types.get_array_descr_info (type, &info)
19415 && (dwarf_version >= 3 || !dwarf_strict))
19417 gen_descr_array_type_die (type, &info, context_die);
19418 TREE_ASM_WRITTEN (type) = 1;
19419 return;
19422 /* We are going to output a DIE to represent the unqualified version
19423 of this type (i.e. without any const or volatile qualifiers) so
19424 get the main variant (i.e. the unqualified version) of this type
19425 now. (Vectors are special because the debugging info is in the
19426 cloned type itself). */
19427 if (TREE_CODE (type) != VECTOR_TYPE)
19428 type = type_main_variant (type);
19430 if (TREE_ASM_WRITTEN (type))
19431 return;
19433 switch (TREE_CODE (type))
19435 case ERROR_MARK:
19436 break;
19438 case POINTER_TYPE:
19439 case REFERENCE_TYPE:
19440 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19441 ensures that the gen_type_die recursion will terminate even if the
19442 type is recursive. Recursive types are possible in Ada. */
19443 /* ??? We could perhaps do this for all types before the switch
19444 statement. */
19445 TREE_ASM_WRITTEN (type) = 1;
19447 /* For these types, all that is required is that we output a DIE (or a
19448 set of DIEs) to represent the "basis" type. */
19449 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19450 DINFO_USAGE_IND_USE);
19451 break;
19453 case OFFSET_TYPE:
19454 /* This code is used for C++ pointer-to-data-member types.
19455 Output a description of the relevant class type. */
19456 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19457 DINFO_USAGE_IND_USE);
19459 /* Output a description of the type of the object pointed to. */
19460 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19461 DINFO_USAGE_IND_USE);
19463 /* Now output a DIE to represent this pointer-to-data-member type
19464 itself. */
19465 gen_ptr_to_mbr_type_die (type, context_die);
19466 break;
19468 case FUNCTION_TYPE:
19469 /* Force out return type (in case it wasn't forced out already). */
19470 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19471 DINFO_USAGE_DIR_USE);
19472 gen_subroutine_type_die (type, context_die);
19473 break;
19475 case METHOD_TYPE:
19476 /* Force out return type (in case it wasn't forced out already). */
19477 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19478 DINFO_USAGE_DIR_USE);
19479 gen_subroutine_type_die (type, context_die);
19480 break;
19482 case ARRAY_TYPE:
19483 gen_array_type_die (type, context_die);
19484 break;
19486 case VECTOR_TYPE:
19487 gen_array_type_die (type, context_die);
19488 break;
19490 case ENUMERAL_TYPE:
19491 case RECORD_TYPE:
19492 case UNION_TYPE:
19493 case QUAL_UNION_TYPE:
19494 gen_tagged_type_die (type, context_die, usage);
19495 return;
19497 case VOID_TYPE:
19498 case INTEGER_TYPE:
19499 case REAL_TYPE:
19500 case FIXED_POINT_TYPE:
19501 case COMPLEX_TYPE:
19502 case BOOLEAN_TYPE:
19503 /* No DIEs needed for fundamental types. */
19504 break;
19506 case NULLPTR_TYPE:
19507 case LANG_TYPE:
19508 /* Just use DW_TAG_unspecified_type. */
19510 dw_die_ref type_die = lookup_type_die (type);
19511 if (type_die == NULL)
19513 tree name = TYPE_NAME (type);
19514 if (TREE_CODE (name) == TYPE_DECL)
19515 name = DECL_NAME (name);
19516 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19517 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19518 equate_type_number_to_die (type, type_die);
19521 break;
19523 default:
19524 gcc_unreachable ();
19527 TREE_ASM_WRITTEN (type) = 1;
19530 static void
19531 gen_type_die (tree type, dw_die_ref context_die)
19533 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19536 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19537 things which are local to the given block. */
19539 static void
19540 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19542 int must_output_die = 0;
19543 bool inlined_func;
19545 /* Ignore blocks that are NULL. */
19546 if (stmt == NULL_TREE)
19547 return;
19549 inlined_func = inlined_function_outer_scope_p (stmt);
19551 /* If the block is one fragment of a non-contiguous block, do not
19552 process the variables, since they will have been done by the
19553 origin block. Do process subblocks. */
19554 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19556 tree sub;
19558 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19559 gen_block_die (sub, context_die, depth + 1);
19561 return;
19564 /* Determine if we need to output any Dwarf DIEs at all to represent this
19565 block. */
19566 if (inlined_func)
19567 /* The outer scopes for inlinings *must* always be represented. We
19568 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19569 must_output_die = 1;
19570 else
19572 /* Determine if this block directly contains any "significant"
19573 local declarations which we will need to output DIEs for. */
19574 if (debug_info_level > DINFO_LEVEL_TERSE)
19575 /* We are not in terse mode so *any* local declaration counts
19576 as being a "significant" one. */
19577 must_output_die = ((BLOCK_VARS (stmt) != NULL
19578 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19579 && (TREE_USED (stmt)
19580 || TREE_ASM_WRITTEN (stmt)
19581 || BLOCK_ABSTRACT (stmt)));
19582 else if ((TREE_USED (stmt)
19583 || TREE_ASM_WRITTEN (stmt)
19584 || BLOCK_ABSTRACT (stmt))
19585 && !dwarf2out_ignore_block (stmt))
19586 must_output_die = 1;
19589 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19590 DIE for any block which contains no significant local declarations at
19591 all. Rather, in such cases we just call `decls_for_scope' so that any
19592 needed Dwarf info for any sub-blocks will get properly generated. Note
19593 that in terse mode, our definition of what constitutes a "significant"
19594 local declaration gets restricted to include only inlined function
19595 instances and local (nested) function definitions. */
19596 if (must_output_die)
19598 if (inlined_func)
19600 /* If STMT block is abstract, that means we have been called
19601 indirectly from dwarf2out_abstract_function.
19602 That function rightfully marks the descendent blocks (of
19603 the abstract function it is dealing with) as being abstract,
19604 precisely to prevent us from emitting any
19605 DW_TAG_inlined_subroutine DIE as a descendent
19606 of an abstract function instance. So in that case, we should
19607 not call gen_inlined_subroutine_die.
19609 Later though, when cgraph asks dwarf2out to emit info
19610 for the concrete instance of the function decl into which
19611 the concrete instance of STMT got inlined, the later will lead
19612 to the generation of a DW_TAG_inlined_subroutine DIE. */
19613 if (! BLOCK_ABSTRACT (stmt))
19614 gen_inlined_subroutine_die (stmt, context_die, depth);
19616 else
19617 gen_lexical_block_die (stmt, context_die, depth);
19619 else
19620 decls_for_scope (stmt, context_die, depth);
19623 /* Process variable DECL (or variable with origin ORIGIN) within
19624 block STMT and add it to CONTEXT_DIE. */
19625 static void
19626 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19628 dw_die_ref die;
19629 tree decl_or_origin = decl ? decl : origin;
19631 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19632 die = lookup_decl_die (decl_or_origin);
19633 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19634 && TYPE_DECL_IS_STUB (decl_or_origin))
19635 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19636 else
19637 die = NULL;
19639 if (die != NULL && die->die_parent == NULL)
19640 add_child_die (context_die, die);
19641 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19642 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19643 stmt, context_die);
19644 else
19645 gen_decl_die (decl, origin, context_die);
19648 /* Generate all of the decls declared within a given scope and (recursively)
19649 all of its sub-blocks. */
19651 static void
19652 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19654 tree decl;
19655 unsigned int i;
19656 tree subblocks;
19658 /* Ignore NULL blocks. */
19659 if (stmt == NULL_TREE)
19660 return;
19662 /* Output the DIEs to represent all of the data objects and typedefs
19663 declared directly within this block but not within any nested
19664 sub-blocks. Also, nested function and tag DIEs have been
19665 generated with a parent of NULL; fix that up now. */
19666 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19667 process_scope_var (stmt, decl, NULL_TREE, context_die);
19668 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19669 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19670 context_die);
19672 /* If we're at -g1, we're not interested in subblocks. */
19673 if (debug_info_level <= DINFO_LEVEL_TERSE)
19674 return;
19676 /* Output the DIEs to represent all sub-blocks (and the items declared
19677 therein) of this block. */
19678 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19679 subblocks != NULL;
19680 subblocks = BLOCK_CHAIN (subblocks))
19681 gen_block_die (subblocks, context_die, depth + 1);
19684 /* Is this a typedef we can avoid emitting? */
19686 static inline int
19687 is_redundant_typedef (const_tree decl)
19689 if (TYPE_DECL_IS_STUB (decl))
19690 return 1;
19692 if (DECL_ARTIFICIAL (decl)
19693 && DECL_CONTEXT (decl)
19694 && is_tagged_type (DECL_CONTEXT (decl))
19695 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19696 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19697 /* Also ignore the artificial member typedef for the class name. */
19698 return 1;
19700 return 0;
19703 /* Return TRUE if TYPE is a typedef that names a type for linkage
19704 purposes. This kind of typedefs is produced by the C++ FE for
19705 constructs like:
19707 typedef struct {...} foo;
19709 In that case, there is no typedef variant type produced for foo.
19710 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19711 struct type. */
19713 static bool
19714 is_naming_typedef_decl (const_tree decl)
19716 if (decl == NULL_TREE
19717 || TREE_CODE (decl) != TYPE_DECL
19718 || !is_tagged_type (TREE_TYPE (decl))
19719 || DECL_IS_BUILTIN (decl)
19720 || is_redundant_typedef (decl)
19721 /* It looks like Ada produces TYPE_DECLs that are very similar
19722 to C++ naming typedefs but that have different
19723 semantics. Let's be specific to c++ for now. */
19724 || !is_cxx ())
19725 return FALSE;
19727 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19728 && TYPE_NAME (TREE_TYPE (decl)) == decl
19729 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19730 != TYPE_NAME (TREE_TYPE (decl))));
19733 /* Returns the DIE for a context. */
19735 static inline dw_die_ref
19736 get_context_die (tree context)
19738 if (context)
19740 /* Find die that represents this context. */
19741 if (TYPE_P (context))
19743 context = TYPE_MAIN_VARIANT (context);
19744 return strip_naming_typedef (context, force_type_die (context));
19746 else
19747 return force_decl_die (context);
19749 return comp_unit_die ();
19752 /* Returns the DIE for decl. A DIE will always be returned. */
19754 static dw_die_ref
19755 force_decl_die (tree decl)
19757 dw_die_ref decl_die;
19758 unsigned saved_external_flag;
19759 tree save_fn = NULL_TREE;
19760 decl_die = lookup_decl_die (decl);
19761 if (!decl_die)
19763 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19765 decl_die = lookup_decl_die (decl);
19766 if (decl_die)
19767 return decl_die;
19769 switch (TREE_CODE (decl))
19771 case FUNCTION_DECL:
19772 /* Clear current_function_decl, so that gen_subprogram_die thinks
19773 that this is a declaration. At this point, we just want to force
19774 declaration die. */
19775 save_fn = current_function_decl;
19776 current_function_decl = NULL_TREE;
19777 gen_subprogram_die (decl, context_die);
19778 current_function_decl = save_fn;
19779 break;
19781 case VAR_DECL:
19782 /* Set external flag to force declaration die. Restore it after
19783 gen_decl_die() call. */
19784 saved_external_flag = DECL_EXTERNAL (decl);
19785 DECL_EXTERNAL (decl) = 1;
19786 gen_decl_die (decl, NULL, context_die);
19787 DECL_EXTERNAL (decl) = saved_external_flag;
19788 break;
19790 case NAMESPACE_DECL:
19791 if (dwarf_version >= 3 || !dwarf_strict)
19792 dwarf2out_decl (decl);
19793 else
19794 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19795 decl_die = comp_unit_die ();
19796 break;
19798 case TRANSLATION_UNIT_DECL:
19799 decl_die = comp_unit_die ();
19800 break;
19802 default:
19803 gcc_unreachable ();
19806 /* We should be able to find the DIE now. */
19807 if (!decl_die)
19808 decl_die = lookup_decl_die (decl);
19809 gcc_assert (decl_die);
19812 return decl_die;
19815 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19816 always returned. */
19818 static dw_die_ref
19819 force_type_die (tree type)
19821 dw_die_ref type_die;
19823 type_die = lookup_type_die (type);
19824 if (!type_die)
19826 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19828 type_die = modified_type_die (type, TYPE_QUALS (type), context_die);
19829 gcc_assert (type_die);
19831 return type_die;
19834 /* Force out any required namespaces to be able to output DECL,
19835 and return the new context_die for it, if it's changed. */
19837 static dw_die_ref
19838 setup_namespace_context (tree thing, dw_die_ref context_die)
19840 tree context = (DECL_P (thing)
19841 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19842 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19843 /* Force out the namespace. */
19844 context_die = force_decl_die (context);
19846 return context_die;
19849 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19850 type) within its namespace, if appropriate.
19852 For compatibility with older debuggers, namespace DIEs only contain
19853 declarations; all definitions are emitted at CU scope. */
19855 static dw_die_ref
19856 declare_in_namespace (tree thing, dw_die_ref context_die)
19858 dw_die_ref ns_context;
19860 if (debug_info_level <= DINFO_LEVEL_TERSE)
19861 return context_die;
19863 /* If this decl is from an inlined function, then don't try to emit it in its
19864 namespace, as we will get confused. It would have already been emitted
19865 when the abstract instance of the inline function was emitted anyways. */
19866 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19867 return context_die;
19869 ns_context = setup_namespace_context (thing, context_die);
19871 if (ns_context != context_die)
19873 if (is_fortran ())
19874 return ns_context;
19875 if (DECL_P (thing))
19876 gen_decl_die (thing, NULL, ns_context);
19877 else
19878 gen_type_die (thing, ns_context);
19880 return context_die;
19883 /* Generate a DIE for a namespace or namespace alias. */
19885 static void
19886 gen_namespace_die (tree decl, dw_die_ref context_die)
19888 dw_die_ref namespace_die;
19890 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19891 they are an alias of. */
19892 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19894 /* Output a real namespace or module. */
19895 context_die = setup_namespace_context (decl, comp_unit_die ());
19896 namespace_die = new_die (is_fortran ()
19897 ? DW_TAG_module : DW_TAG_namespace,
19898 context_die, decl);
19899 /* For Fortran modules defined in different CU don't add src coords. */
19900 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19902 const char *name = dwarf2_name (decl, 0);
19903 if (name)
19904 add_name_attribute (namespace_die, name);
19906 else
19907 add_name_and_src_coords_attributes (namespace_die, decl);
19908 if (DECL_EXTERNAL (decl))
19909 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19910 equate_decl_number_to_die (decl, namespace_die);
19912 else
19914 /* Output a namespace alias. */
19916 /* Force out the namespace we are an alias of, if necessary. */
19917 dw_die_ref origin_die
19918 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19920 if (DECL_FILE_SCOPE_P (decl)
19921 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19922 context_die = setup_namespace_context (decl, comp_unit_die ());
19923 /* Now create the namespace alias DIE. */
19924 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19925 add_name_and_src_coords_attributes (namespace_die, decl);
19926 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19927 equate_decl_number_to_die (decl, namespace_die);
19929 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19930 if (want_pubnames ())
19931 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19934 /* Generate Dwarf debug information for a decl described by DECL.
19935 The return value is currently only meaningful for PARM_DECLs,
19936 for all other decls it returns NULL. */
19938 static dw_die_ref
19939 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19941 tree decl_or_origin = decl ? decl : origin;
19942 tree class_origin = NULL, ultimate_origin;
19944 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19945 return NULL;
19947 switch (TREE_CODE (decl_or_origin))
19949 case ERROR_MARK:
19950 break;
19952 case CONST_DECL:
19953 if (!is_fortran () && !is_ada ())
19955 /* The individual enumerators of an enum type get output when we output
19956 the Dwarf representation of the relevant enum type itself. */
19957 break;
19960 /* Emit its type. */
19961 gen_type_die (TREE_TYPE (decl), context_die);
19963 /* And its containing namespace. */
19964 context_die = declare_in_namespace (decl, context_die);
19966 gen_const_die (decl, context_die);
19967 break;
19969 case FUNCTION_DECL:
19970 /* Don't output any DIEs to represent mere function declarations,
19971 unless they are class members or explicit block externs. */
19972 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19973 && DECL_FILE_SCOPE_P (decl_or_origin)
19974 && (current_function_decl == NULL_TREE
19975 || DECL_ARTIFICIAL (decl_or_origin)))
19976 break;
19978 #if 0
19979 /* FIXME */
19980 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19981 on local redeclarations of global functions. That seems broken. */
19982 if (current_function_decl != decl)
19983 /* This is only a declaration. */;
19984 #endif
19986 /* If we're emitting a clone, emit info for the abstract instance. */
19987 if (origin || DECL_ORIGIN (decl) != decl)
19988 dwarf2out_abstract_function (origin
19989 ? DECL_ORIGIN (origin)
19990 : DECL_ABSTRACT_ORIGIN (decl));
19992 /* If we're emitting an out-of-line copy of an inline function,
19993 emit info for the abstract instance and set up to refer to it. */
19994 else if (cgraph_function_possibly_inlined_p (decl)
19995 && ! DECL_ABSTRACT (decl)
19996 && ! class_or_namespace_scope_p (context_die)
19997 /* dwarf2out_abstract_function won't emit a die if this is just
19998 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
19999 that case, because that works only if we have a die. */
20000 && DECL_INITIAL (decl) != NULL_TREE)
20002 dwarf2out_abstract_function (decl);
20003 set_decl_origin_self (decl);
20006 /* Otherwise we're emitting the primary DIE for this decl. */
20007 else if (debug_info_level > DINFO_LEVEL_TERSE)
20009 /* Before we describe the FUNCTION_DECL itself, make sure that we
20010 have its containing type. */
20011 if (!origin)
20012 origin = decl_class_context (decl);
20013 if (origin != NULL_TREE)
20014 gen_type_die (origin, context_die);
20016 /* And its return type. */
20017 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20019 /* And its virtual context. */
20020 if (DECL_VINDEX (decl) != NULL_TREE)
20021 gen_type_die (DECL_CONTEXT (decl), context_die);
20023 /* Make sure we have a member DIE for decl. */
20024 if (origin != NULL_TREE)
20025 gen_type_die_for_member (origin, decl, context_die);
20027 /* And its containing namespace. */
20028 context_die = declare_in_namespace (decl, context_die);
20031 /* Now output a DIE to represent the function itself. */
20032 if (decl)
20033 gen_subprogram_die (decl, context_die);
20034 break;
20036 case TYPE_DECL:
20037 /* If we are in terse mode, don't generate any DIEs to represent any
20038 actual typedefs. */
20039 if (debug_info_level <= DINFO_LEVEL_TERSE)
20040 break;
20042 /* In the special case of a TYPE_DECL node representing the declaration
20043 of some type tag, if the given TYPE_DECL is marked as having been
20044 instantiated from some other (original) TYPE_DECL node (e.g. one which
20045 was generated within the original definition of an inline function) we
20046 used to generate a special (abbreviated) DW_TAG_structure_type,
20047 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20048 should be actually referencing those DIEs, as variable DIEs with that
20049 type would be emitted already in the abstract origin, so it was always
20050 removed during unused type prunning. Don't add anything in this
20051 case. */
20052 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20053 break;
20055 if (is_redundant_typedef (decl))
20056 gen_type_die (TREE_TYPE (decl), context_die);
20057 else
20058 /* Output a DIE to represent the typedef itself. */
20059 gen_typedef_die (decl, context_die);
20060 break;
20062 case LABEL_DECL:
20063 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20064 gen_label_die (decl, context_die);
20065 break;
20067 case VAR_DECL:
20068 case RESULT_DECL:
20069 /* If we are in terse mode, don't generate any DIEs to represent any
20070 variable declarations or definitions. */
20071 if (debug_info_level <= DINFO_LEVEL_TERSE)
20072 break;
20074 /* Output any DIEs that are needed to specify the type of this data
20075 object. */
20076 if (decl_by_reference_p (decl_or_origin))
20077 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20078 else
20079 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20081 /* And its containing type. */
20082 class_origin = decl_class_context (decl_or_origin);
20083 if (class_origin != NULL_TREE)
20084 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20086 /* And its containing namespace. */
20087 context_die = declare_in_namespace (decl_or_origin, context_die);
20089 /* Now output the DIE to represent the data object itself. This gets
20090 complicated because of the possibility that the VAR_DECL really
20091 represents an inlined instance of a formal parameter for an inline
20092 function. */
20093 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20094 if (ultimate_origin != NULL_TREE
20095 && TREE_CODE (ultimate_origin) == PARM_DECL)
20096 gen_formal_parameter_die (decl, origin,
20097 true /* Emit name attribute. */,
20098 context_die);
20099 else
20100 gen_variable_die (decl, origin, context_die);
20101 break;
20103 case FIELD_DECL:
20104 /* Ignore the nameless fields that are used to skip bits but handle C++
20105 anonymous unions and structs. */
20106 if (DECL_NAME (decl) != NULL_TREE
20107 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20108 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20110 gen_type_die (member_declared_type (decl), context_die);
20111 gen_field_die (decl, context_die);
20113 break;
20115 case PARM_DECL:
20116 if (DECL_BY_REFERENCE (decl_or_origin))
20117 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20118 else
20119 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20120 return gen_formal_parameter_die (decl, origin,
20121 true /* Emit name attribute. */,
20122 context_die);
20124 case NAMESPACE_DECL:
20125 case IMPORTED_DECL:
20126 if (dwarf_version >= 3 || !dwarf_strict)
20127 gen_namespace_die (decl, context_die);
20128 break;
20130 default:
20131 /* Probably some frontend-internal decl. Assume we don't care. */
20132 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20133 break;
20136 return NULL;
20139 /* Output debug information for global decl DECL. Called from toplev.c after
20140 compilation proper has finished. */
20142 static void
20143 dwarf2out_global_decl (tree decl)
20145 /* Output DWARF2 information for file-scope tentative data object
20146 declarations, file-scope (extern) function declarations (which
20147 had no corresponding body) and file-scope tagged type declarations
20148 and definitions which have not yet been forced out. */
20149 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20150 dwarf2out_decl (decl);
20153 /* Output debug information for type decl DECL. Called from toplev.c
20154 and from language front ends (to record built-in types). */
20155 static void
20156 dwarf2out_type_decl (tree decl, int local)
20158 if (!local)
20159 dwarf2out_decl (decl);
20162 /* Output debug information for imported module or decl DECL.
20163 NAME is non-NULL name in the lexical block if the decl has been renamed.
20164 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20165 that DECL belongs to.
20166 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20167 static void
20168 dwarf2out_imported_module_or_decl_1 (tree decl,
20169 tree name,
20170 tree lexical_block,
20171 dw_die_ref lexical_block_die)
20173 expanded_location xloc;
20174 dw_die_ref imported_die = NULL;
20175 dw_die_ref at_import_die;
20177 if (TREE_CODE (decl) == IMPORTED_DECL)
20179 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20180 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20181 gcc_assert (decl);
20183 else
20184 xloc = expand_location (input_location);
20186 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20188 at_import_die = force_type_die (TREE_TYPE (decl));
20189 /* For namespace N { typedef void T; } using N::T; base_type_die
20190 returns NULL, but DW_TAG_imported_declaration requires
20191 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20192 if (!at_import_die)
20194 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20195 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20196 at_import_die = lookup_type_die (TREE_TYPE (decl));
20197 gcc_assert (at_import_die);
20200 else
20202 at_import_die = lookup_decl_die (decl);
20203 if (!at_import_die)
20205 /* If we're trying to avoid duplicate debug info, we may not have
20206 emitted the member decl for this field. Emit it now. */
20207 if (TREE_CODE (decl) == FIELD_DECL)
20209 tree type = DECL_CONTEXT (decl);
20211 if (TYPE_CONTEXT (type)
20212 && TYPE_P (TYPE_CONTEXT (type))
20213 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20214 DINFO_USAGE_DIR_USE))
20215 return;
20216 gen_type_die_for_member (type, decl,
20217 get_context_die (TYPE_CONTEXT (type)));
20219 at_import_die = force_decl_die (decl);
20223 if (TREE_CODE (decl) == NAMESPACE_DECL)
20225 if (dwarf_version >= 3 || !dwarf_strict)
20226 imported_die = new_die (DW_TAG_imported_module,
20227 lexical_block_die,
20228 lexical_block);
20229 else
20230 return;
20232 else
20233 imported_die = new_die (DW_TAG_imported_declaration,
20234 lexical_block_die,
20235 lexical_block);
20237 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20238 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20239 if (name)
20240 add_AT_string (imported_die, DW_AT_name,
20241 IDENTIFIER_POINTER (name));
20242 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20245 /* Output debug information for imported module or decl DECL.
20246 NAME is non-NULL name in context if the decl has been renamed.
20247 CHILD is true if decl is one of the renamed decls as part of
20248 importing whole module. */
20250 static void
20251 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20252 bool child)
20254 /* dw_die_ref at_import_die; */
20255 dw_die_ref scope_die;
20257 if (debug_info_level <= DINFO_LEVEL_TERSE)
20258 return;
20260 gcc_assert (decl);
20262 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20263 We need decl DIE for reference and scope die. First, get DIE for the decl
20264 itself. */
20266 /* Get the scope die for decl context. Use comp_unit_die for global module
20267 or decl. If die is not found for non globals, force new die. */
20268 if (context
20269 && TYPE_P (context)
20270 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20271 return;
20273 if (!(dwarf_version >= 3 || !dwarf_strict))
20274 return;
20276 scope_die = get_context_die (context);
20278 if (child)
20280 gcc_assert (scope_die->die_child);
20281 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20282 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20283 scope_die = scope_die->die_child;
20286 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20287 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20291 /* Write the debugging output for DECL. */
20293 void
20294 dwarf2out_decl (tree decl)
20296 dw_die_ref context_die = comp_unit_die ();
20298 switch (TREE_CODE (decl))
20300 case ERROR_MARK:
20301 return;
20303 case FUNCTION_DECL:
20304 /* What we would really like to do here is to filter out all mere
20305 file-scope declarations of file-scope functions which are never
20306 referenced later within this translation unit (and keep all of ones
20307 that *are* referenced later on) but we aren't clairvoyant, so we have
20308 no idea which functions will be referenced in the future (i.e. later
20309 on within the current translation unit). So here we just ignore all
20310 file-scope function declarations which are not also definitions. If
20311 and when the debugger needs to know something about these functions,
20312 it will have to hunt around and find the DWARF information associated
20313 with the definition of the function.
20315 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20316 nodes represent definitions and which ones represent mere
20317 declarations. We have to check DECL_INITIAL instead. That's because
20318 the C front-end supports some weird semantics for "extern inline"
20319 function definitions. These can get inlined within the current
20320 translation unit (and thus, we need to generate Dwarf info for their
20321 abstract instances so that the Dwarf info for the concrete inlined
20322 instances can have something to refer to) but the compiler never
20323 generates any out-of-lines instances of such things (despite the fact
20324 that they *are* definitions).
20326 The important point is that the C front-end marks these "extern
20327 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20328 them anyway. Note that the C++ front-end also plays some similar games
20329 for inline function definitions appearing within include files which
20330 also contain `#pragma interface' pragmas.
20332 If we are called from dwarf2out_abstract_function output a DIE
20333 anyway. We can end up here this way with early inlining and LTO
20334 where the inlined function is output in a different LTRANS unit
20335 or not at all. */
20336 if (DECL_INITIAL (decl) == NULL_TREE
20337 && ! DECL_ABSTRACT (decl))
20338 return;
20340 /* If we're a nested function, initially use a parent of NULL; if we're
20341 a plain function, this will be fixed up in decls_for_scope. If
20342 we're a method, it will be ignored, since we already have a DIE. */
20343 if (decl_function_context (decl)
20344 /* But if we're in terse mode, we don't care about scope. */
20345 && debug_info_level > DINFO_LEVEL_TERSE)
20346 context_die = NULL;
20347 break;
20349 case VAR_DECL:
20350 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20351 declaration and if the declaration was never even referenced from
20352 within this entire compilation unit. We suppress these DIEs in
20353 order to save space in the .debug section (by eliminating entries
20354 which are probably useless). Note that we must not suppress
20355 block-local extern declarations (whether used or not) because that
20356 would screw-up the debugger's name lookup mechanism and cause it to
20357 miss things which really ought to be in scope at a given point. */
20358 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20359 return;
20361 /* For local statics lookup proper context die. */
20362 if (TREE_STATIC (decl)
20363 && DECL_CONTEXT (decl)
20364 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20365 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20367 /* If we are in terse mode, don't generate any DIEs to represent any
20368 variable declarations or definitions. */
20369 if (debug_info_level <= DINFO_LEVEL_TERSE)
20370 return;
20371 break;
20373 case CONST_DECL:
20374 if (debug_info_level <= DINFO_LEVEL_TERSE)
20375 return;
20376 if (!is_fortran () && !is_ada ())
20377 return;
20378 if (TREE_STATIC (decl) && decl_function_context (decl))
20379 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20380 break;
20382 case NAMESPACE_DECL:
20383 case IMPORTED_DECL:
20384 if (debug_info_level <= DINFO_LEVEL_TERSE)
20385 return;
20386 if (lookup_decl_die (decl) != NULL)
20387 return;
20388 break;
20390 case TYPE_DECL:
20391 /* Don't emit stubs for types unless they are needed by other DIEs. */
20392 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20393 return;
20395 /* Don't bother trying to generate any DIEs to represent any of the
20396 normal built-in types for the language we are compiling. */
20397 if (DECL_IS_BUILTIN (decl))
20398 return;
20400 /* If we are in terse mode, don't generate any DIEs for types. */
20401 if (debug_info_level <= DINFO_LEVEL_TERSE)
20402 return;
20404 /* If we're a function-scope tag, initially use a parent of NULL;
20405 this will be fixed up in decls_for_scope. */
20406 if (decl_function_context (decl))
20407 context_die = NULL;
20409 break;
20411 default:
20412 return;
20415 gen_decl_die (decl, NULL, context_die);
20418 /* Write the debugging output for DECL. */
20420 static void
20421 dwarf2out_function_decl (tree decl)
20423 dwarf2out_decl (decl);
20424 call_arg_locations = NULL;
20425 call_arg_loc_last = NULL;
20426 call_site_count = -1;
20427 tail_call_site_count = -1;
20428 block_map.release ();
20429 htab_empty (decl_loc_table);
20430 htab_empty (cached_dw_loc_list_table);
20433 /* Output a marker (i.e. a label) for the beginning of the generated code for
20434 a lexical block. */
20436 static void
20437 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20438 unsigned int blocknum)
20440 switch_to_section (current_function_section ());
20441 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20444 /* Output a marker (i.e. a label) for the end of the generated code for a
20445 lexical block. */
20447 static void
20448 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20450 switch_to_section (current_function_section ());
20451 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20454 /* Returns nonzero if it is appropriate not to emit any debugging
20455 information for BLOCK, because it doesn't contain any instructions.
20457 Don't allow this for blocks with nested functions or local classes
20458 as we would end up with orphans, and in the presence of scheduling
20459 we may end up calling them anyway. */
20461 static bool
20462 dwarf2out_ignore_block (const_tree block)
20464 tree decl;
20465 unsigned int i;
20467 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20468 if (TREE_CODE (decl) == FUNCTION_DECL
20469 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20470 return 0;
20471 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20473 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20474 if (TREE_CODE (decl) == FUNCTION_DECL
20475 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20476 return 0;
20479 return 1;
20482 /* Hash table routines for file_hash. */
20484 static int
20485 file_table_eq (const void *p1_p, const void *p2_p)
20487 const struct dwarf_file_data *const p1 =
20488 (const struct dwarf_file_data *) p1_p;
20489 const char *const p2 = (const char *) p2_p;
20490 return filename_cmp (p1->filename, p2) == 0;
20493 static hashval_t
20494 file_table_hash (const void *p_p)
20496 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20497 return htab_hash_string (p->filename);
20500 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20501 dwarf2out.c) and return its "index". The index of each (known) filename is
20502 just a unique number which is associated with only that one filename. We
20503 need such numbers for the sake of generating labels (in the .debug_sfnames
20504 section) and references to those files numbers (in the .debug_srcinfo
20505 and.debug_macinfo sections). If the filename given as an argument is not
20506 found in our current list, add it to the list and assign it the next
20507 available unique index number. In order to speed up searches, we remember
20508 the index of the filename was looked up last. This handles the majority of
20509 all searches. */
20511 static struct dwarf_file_data *
20512 lookup_filename (const char *file_name)
20514 void ** slot;
20515 struct dwarf_file_data * created;
20517 /* Check to see if the file name that was searched on the previous
20518 call matches this file name. If so, return the index. */
20519 if (file_table_last_lookup
20520 && (file_name == file_table_last_lookup->filename
20521 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20522 return file_table_last_lookup;
20524 /* Didn't match the previous lookup, search the table. */
20525 slot = htab_find_slot_with_hash (file_table, file_name,
20526 htab_hash_string (file_name), INSERT);
20527 if (*slot)
20528 return (struct dwarf_file_data *) *slot;
20530 created = ggc_alloc_dwarf_file_data ();
20531 created->filename = file_name;
20532 created->emitted_number = 0;
20533 *slot = created;
20534 return created;
20537 /* If the assembler will construct the file table, then translate the compiler
20538 internal file table number into the assembler file table number, and emit
20539 a .file directive if we haven't already emitted one yet. The file table
20540 numbers are different because we prune debug info for unused variables and
20541 types, which may include filenames. */
20543 static int
20544 maybe_emit_file (struct dwarf_file_data * fd)
20546 if (! fd->emitted_number)
20548 if (last_emitted_file)
20549 fd->emitted_number = last_emitted_file->emitted_number + 1;
20550 else
20551 fd->emitted_number = 1;
20552 last_emitted_file = fd;
20554 if (DWARF2_ASM_LINE_DEBUG_INFO)
20556 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20557 output_quoted_string (asm_out_file,
20558 remap_debug_filename (fd->filename));
20559 fputc ('\n', asm_out_file);
20563 return fd->emitted_number;
20566 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20567 That generation should happen after function debug info has been
20568 generated. The value of the attribute is the constant value of ARG. */
20570 static void
20571 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20573 die_arg_entry entry;
20575 if (!die || !arg)
20576 return;
20578 if (!tmpl_value_parm_die_table)
20579 vec_alloc (tmpl_value_parm_die_table, 32);
20581 entry.die = die;
20582 entry.arg = arg;
20583 vec_safe_push (tmpl_value_parm_die_table, entry);
20586 /* Return TRUE if T is an instance of generic type, FALSE
20587 otherwise. */
20589 static bool
20590 generic_type_p (tree t)
20592 if (t == NULL_TREE || !TYPE_P (t))
20593 return false;
20594 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20597 /* Schedule the generation of the generic parameter dies for the
20598 instance of generic type T. The proper generation itself is later
20599 done by gen_scheduled_generic_parms_dies. */
20601 static void
20602 schedule_generic_params_dies_gen (tree t)
20604 if (!generic_type_p (t))
20605 return;
20607 if (!generic_type_instances)
20608 vec_alloc (generic_type_instances, 256);
20610 vec_safe_push (generic_type_instances, t);
20613 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20614 by append_entry_to_tmpl_value_parm_die_table. This function must
20615 be called after function DIEs have been generated. */
20617 static void
20618 gen_remaining_tmpl_value_param_die_attribute (void)
20620 if (tmpl_value_parm_die_table)
20622 unsigned i;
20623 die_arg_entry *e;
20625 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20626 tree_add_const_value_attribute (e->die, e->arg);
20630 /* Generate generic parameters DIEs for instances of generic types
20631 that have been previously scheduled by
20632 schedule_generic_params_dies_gen. This function must be called
20633 after all the types of the CU have been laid out. */
20635 static void
20636 gen_scheduled_generic_parms_dies (void)
20638 unsigned i;
20639 tree t;
20641 if (!generic_type_instances)
20642 return;
20644 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20645 if (COMPLETE_TYPE_P (t))
20646 gen_generic_params_dies (t);
20650 /* Replace DW_AT_name for the decl with name. */
20652 static void
20653 dwarf2out_set_name (tree decl, tree name)
20655 dw_die_ref die;
20656 dw_attr_ref attr;
20657 const char *dname;
20659 die = TYPE_SYMTAB_DIE (decl);
20660 if (!die)
20661 return;
20663 dname = dwarf2_name (name, 0);
20664 if (!dname)
20665 return;
20667 attr = get_AT (die, DW_AT_name);
20668 if (attr)
20670 struct indirect_string_node *node;
20672 node = find_AT_string (dname);
20673 /* replace the string. */
20674 attr->dw_attr_val.v.val_str = node;
20677 else
20678 add_name_attribute (die, dname);
20681 /* True if before or during processing of the first function being emitted. */
20682 static bool in_first_function_p = true;
20683 /* True if loc_note during dwarf2out_var_location call might still be
20684 before first real instruction at address equal to .Ltext0. */
20685 static bool maybe_at_text_label_p = true;
20686 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20687 static unsigned int first_loclabel_num_not_at_text_label;
20689 /* Called by the final INSN scan whenever we see a var location. We
20690 use it to drop labels in the right places, and throw the location in
20691 our lookup table. */
20693 static void
20694 dwarf2out_var_location (rtx loc_note)
20696 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20697 struct var_loc_node *newloc;
20698 rtx next_real, next_note;
20699 static const char *last_label;
20700 static const char *last_postcall_label;
20701 static bool last_in_cold_section_p;
20702 static rtx expected_next_loc_note;
20703 tree decl;
20704 bool var_loc_p;
20706 if (!NOTE_P (loc_note))
20708 if (CALL_P (loc_note))
20710 call_site_count++;
20711 if (SIBLING_CALL_P (loc_note))
20712 tail_call_site_count++;
20714 return;
20717 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20718 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20719 return;
20721 /* Optimize processing a large consecutive sequence of location
20722 notes so we don't spend too much time in next_real_insn. If the
20723 next insn is another location note, remember the next_real_insn
20724 calculation for next time. */
20725 next_real = cached_next_real_insn;
20726 if (next_real)
20728 if (expected_next_loc_note != loc_note)
20729 next_real = NULL_RTX;
20732 next_note = NEXT_INSN (loc_note);
20733 if (! next_note
20734 || INSN_DELETED_P (next_note)
20735 || GET_CODE (next_note) != NOTE
20736 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20737 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20738 next_note = NULL_RTX;
20740 if (! next_real)
20741 next_real = next_real_insn (loc_note);
20743 if (next_note)
20745 expected_next_loc_note = next_note;
20746 cached_next_real_insn = next_real;
20748 else
20749 cached_next_real_insn = NULL_RTX;
20751 /* If there are no instructions which would be affected by this note,
20752 don't do anything. */
20753 if (var_loc_p
20754 && next_real == NULL_RTX
20755 && !NOTE_DURING_CALL_P (loc_note))
20756 return;
20758 if (next_real == NULL_RTX)
20759 next_real = get_last_insn ();
20761 /* If there were any real insns between note we processed last time
20762 and this note (or if it is the first note), clear
20763 last_{,postcall_}label so that they are not reused this time. */
20764 if (last_var_location_insn == NULL_RTX
20765 || last_var_location_insn != next_real
20766 || last_in_cold_section_p != in_cold_section_p)
20768 last_label = NULL;
20769 last_postcall_label = NULL;
20772 if (var_loc_p)
20774 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20775 newloc = add_var_loc_to_decl (decl, loc_note,
20776 NOTE_DURING_CALL_P (loc_note)
20777 ? last_postcall_label : last_label);
20778 if (newloc == NULL)
20779 return;
20781 else
20783 decl = NULL_TREE;
20784 newloc = NULL;
20787 /* If there were no real insns between note we processed last time
20788 and this note, use the label we emitted last time. Otherwise
20789 create a new label and emit it. */
20790 if (last_label == NULL)
20792 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20793 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20794 loclabel_num++;
20795 last_label = ggc_strdup (loclabel);
20796 /* See if loclabel might be equal to .Ltext0. If yes,
20797 bump first_loclabel_num_not_at_text_label. */
20798 if (!have_multiple_function_sections
20799 && in_first_function_p
20800 && maybe_at_text_label_p)
20802 static rtx last_start;
20803 rtx insn;
20804 for (insn = loc_note; insn; insn = previous_insn (insn))
20805 if (insn == last_start)
20806 break;
20807 else if (!NONDEBUG_INSN_P (insn))
20808 continue;
20809 else
20811 rtx body = PATTERN (insn);
20812 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
20813 continue;
20814 /* Inline asm could occupy zero bytes. */
20815 else if (GET_CODE (body) == ASM_INPUT
20816 || asm_noperands (body) >= 0)
20817 continue;
20818 #ifdef HAVE_attr_length
20819 else if (get_attr_min_length (insn) == 0)
20820 continue;
20821 #endif
20822 else
20824 /* Assume insn has non-zero length. */
20825 maybe_at_text_label_p = false;
20826 break;
20829 if (maybe_at_text_label_p)
20831 last_start = loc_note;
20832 first_loclabel_num_not_at_text_label = loclabel_num;
20837 if (!var_loc_p)
20839 struct call_arg_loc_node *ca_loc
20840 = ggc_alloc_cleared_call_arg_loc_node ();
20841 rtx prev = prev_real_insn (loc_note), x;
20842 ca_loc->call_arg_loc_note = loc_note;
20843 ca_loc->next = NULL;
20844 ca_loc->label = last_label;
20845 gcc_assert (prev
20846 && (CALL_P (prev)
20847 || (NONJUMP_INSN_P (prev)
20848 && GET_CODE (PATTERN (prev)) == SEQUENCE
20849 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20850 if (!CALL_P (prev))
20851 prev = XVECEXP (PATTERN (prev), 0, 0);
20852 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20853 x = get_call_rtx_from (PATTERN (prev));
20854 if (x)
20856 x = XEXP (XEXP (x, 0), 0);
20857 if (GET_CODE (x) == SYMBOL_REF
20858 && SYMBOL_REF_DECL (x)
20859 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20860 ca_loc->symbol_ref = x;
20862 ca_loc->block = insn_scope (prev);
20863 if (call_arg_locations)
20864 call_arg_loc_last->next = ca_loc;
20865 else
20866 call_arg_locations = ca_loc;
20867 call_arg_loc_last = ca_loc;
20869 else if (!NOTE_DURING_CALL_P (loc_note))
20870 newloc->label = last_label;
20871 else
20873 if (!last_postcall_label)
20875 sprintf (loclabel, "%s-1", last_label);
20876 last_postcall_label = ggc_strdup (loclabel);
20878 newloc->label = last_postcall_label;
20881 last_var_location_insn = next_real;
20882 last_in_cold_section_p = in_cold_section_p;
20885 /* Note in one location list that text section has changed. */
20887 static int
20888 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20890 var_loc_list *list = (var_loc_list *) *slot;
20891 if (list->first)
20892 list->last_before_switch
20893 = list->last->next ? list->last->next : list->last;
20894 return 1;
20897 /* Note in all location lists that text section has changed. */
20899 static void
20900 var_location_switch_text_section (void)
20902 if (decl_loc_table == NULL)
20903 return;
20905 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20908 /* Create a new line number table. */
20910 static dw_line_info_table *
20911 new_line_info_table (void)
20913 dw_line_info_table *table;
20915 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20916 table->file_num = 1;
20917 table->line_num = 1;
20918 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20920 return table;
20923 /* Lookup the "current" table into which we emit line info, so
20924 that we don't have to do it for every source line. */
20926 static void
20927 set_cur_line_info_table (section *sec)
20929 dw_line_info_table *table;
20931 if (sec == text_section)
20932 table = text_section_line_info;
20933 else if (sec == cold_text_section)
20935 table = cold_text_section_line_info;
20936 if (!table)
20938 cold_text_section_line_info = table = new_line_info_table ();
20939 table->end_label = cold_end_label;
20942 else
20944 const char *end_label;
20946 if (flag_reorder_blocks_and_partition)
20948 if (in_cold_section_p)
20949 end_label = crtl->subsections.cold_section_end_label;
20950 else
20951 end_label = crtl->subsections.hot_section_end_label;
20953 else
20955 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20956 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20957 current_function_funcdef_no);
20958 end_label = ggc_strdup (label);
20961 table = new_line_info_table ();
20962 table->end_label = end_label;
20964 vec_safe_push (separate_line_info, table);
20967 if (DWARF2_ASM_LINE_DEBUG_INFO)
20968 table->is_stmt = (cur_line_info_table
20969 ? cur_line_info_table->is_stmt
20970 : DWARF_LINE_DEFAULT_IS_STMT_START);
20971 cur_line_info_table = table;
20975 /* We need to reset the locations at the beginning of each
20976 function. We can't do this in the end_function hook, because the
20977 declarations that use the locations won't have been output when
20978 that hook is called. Also compute have_multiple_function_sections here. */
20980 static void
20981 dwarf2out_begin_function (tree fun)
20983 section *sec = function_section (fun);
20985 if (sec != text_section)
20986 have_multiple_function_sections = true;
20988 if (flag_reorder_blocks_and_partition && !cold_text_section)
20990 gcc_assert (current_function_decl == fun);
20991 cold_text_section = unlikely_text_section ();
20992 switch_to_section (cold_text_section);
20993 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20994 switch_to_section (sec);
20997 dwarf2out_note_section_used ();
20998 call_site_count = 0;
20999 tail_call_site_count = 0;
21001 set_cur_line_info_table (sec);
21004 /* Helper function of dwarf2out_end_function, called only after emitting
21005 the very first function into assembly. Check if some .debug_loc range
21006 might end with a .LVL* label that could be equal to .Ltext0.
21007 In that case we must force using absolute addresses in .debug_loc ranges,
21008 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21009 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21010 list terminator.
21011 Set have_multiple_function_sections to true in that case and
21012 terminate htab traversal. */
21014 static int
21015 find_empty_loc_ranges_at_text_label (void **slot, void *)
21017 var_loc_list *entry;
21018 struct var_loc_node *node;
21020 entry = (var_loc_list *) *slot;
21021 node = entry->first;
21022 if (node && node->next && node->next->label)
21024 unsigned int i;
21025 const char *label = node->next->label;
21026 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21028 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21030 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21031 if (strcmp (label, loclabel) == 0)
21033 have_multiple_function_sections = true;
21034 return 0;
21038 return 1;
21041 /* Hook called after emitting a function into assembly.
21042 This does something only for the very first function emitted. */
21044 static void
21045 dwarf2out_end_function (unsigned int)
21047 if (in_first_function_p
21048 && !have_multiple_function_sections
21049 && first_loclabel_num_not_at_text_label
21050 && decl_loc_table)
21051 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21052 NULL);
21053 in_first_function_p = false;
21054 maybe_at_text_label_p = false;
21057 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21059 static void
21060 push_dw_line_info_entry (dw_line_info_table *table,
21061 enum dw_line_info_opcode opcode, unsigned int val)
21063 dw_line_info_entry e;
21064 e.opcode = opcode;
21065 e.val = val;
21066 vec_safe_push (table->entries, e);
21069 /* Output a label to mark the beginning of a source code line entry
21070 and record information relating to this source line, in
21071 'line_info_table' for later output of the .debug_line section. */
21072 /* ??? The discriminator parameter ought to be unsigned. */
21074 static void
21075 dwarf2out_source_line (unsigned int line, const char *filename,
21076 int discriminator, bool is_stmt)
21078 unsigned int file_num;
21079 dw_line_info_table *table;
21081 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21082 return;
21084 /* The discriminator column was added in dwarf4. Simplify the below
21085 by simply removing it if we're not supposed to output it. */
21086 if (dwarf_version < 4 && dwarf_strict)
21087 discriminator = 0;
21089 table = cur_line_info_table;
21090 file_num = maybe_emit_file (lookup_filename (filename));
21092 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21093 the debugger has used the second (possibly duplicate) line number
21094 at the beginning of the function to mark the end of the prologue.
21095 We could eliminate any other duplicates within the function. For
21096 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21097 that second line number entry. */
21098 /* Recall that this end-of-prologue indication is *not* the same thing
21099 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21100 to which the hook corresponds, follows the last insn that was
21101 emitted by gen_prologue. What we need is to precede the first insn
21102 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21103 insn that corresponds to something the user wrote. These may be
21104 very different locations once scheduling is enabled. */
21106 if (0 && file_num == table->file_num
21107 && line == table->line_num
21108 && discriminator == table->discrim_num
21109 && is_stmt == table->is_stmt)
21110 return;
21112 switch_to_section (current_function_section ());
21114 /* If requested, emit something human-readable. */
21115 if (flag_debug_asm)
21116 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21118 if (DWARF2_ASM_LINE_DEBUG_INFO)
21120 /* Emit the .loc directive understood by GNU as. */
21121 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21122 file_num, line, is_stmt, discriminator */
21123 fputs ("\t.loc ", asm_out_file);
21124 fprint_ul (asm_out_file, file_num);
21125 putc (' ', asm_out_file);
21126 fprint_ul (asm_out_file, line);
21127 putc (' ', asm_out_file);
21128 putc ('0', asm_out_file);
21130 if (is_stmt != table->is_stmt)
21132 fputs (" is_stmt ", asm_out_file);
21133 putc (is_stmt ? '1' : '0', asm_out_file);
21135 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21137 gcc_assert (discriminator > 0);
21138 fputs (" discriminator ", asm_out_file);
21139 fprint_ul (asm_out_file, (unsigned long) discriminator);
21141 putc ('\n', asm_out_file);
21143 else
21145 unsigned int label_num = ++line_info_label_num;
21147 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21149 push_dw_line_info_entry (table, LI_set_address, label_num);
21150 if (file_num != table->file_num)
21151 push_dw_line_info_entry (table, LI_set_file, file_num);
21152 if (discriminator != table->discrim_num)
21153 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21154 if (is_stmt != table->is_stmt)
21155 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21156 push_dw_line_info_entry (table, LI_set_line, line);
21159 table->file_num = file_num;
21160 table->line_num = line;
21161 table->discrim_num = discriminator;
21162 table->is_stmt = is_stmt;
21163 table->in_use = true;
21166 /* Record the beginning of a new source file. */
21168 static void
21169 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21171 if (flag_eliminate_dwarf2_dups)
21173 /* Record the beginning of the file for break_out_includes. */
21174 dw_die_ref bincl_die;
21176 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21177 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21180 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21182 macinfo_entry e;
21183 e.code = DW_MACINFO_start_file;
21184 e.lineno = lineno;
21185 e.info = ggc_strdup (filename);
21186 vec_safe_push (macinfo_table, e);
21190 /* Record the end of a source file. */
21192 static void
21193 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21195 if (flag_eliminate_dwarf2_dups)
21196 /* Record the end of the file for break_out_includes. */
21197 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21199 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21201 macinfo_entry e;
21202 e.code = DW_MACINFO_end_file;
21203 e.lineno = lineno;
21204 e.info = NULL;
21205 vec_safe_push (macinfo_table, e);
21209 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21210 the tail part of the directive line, i.e. the part which is past the
21211 initial whitespace, #, whitespace, directive-name, whitespace part. */
21213 static void
21214 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21215 const char *buffer ATTRIBUTE_UNUSED)
21217 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21219 macinfo_entry e;
21220 /* Insert a dummy first entry to be able to optimize the whole
21221 predefined macro block using DW_MACRO_GNU_transparent_include. */
21222 if (macinfo_table->is_empty () && lineno <= 1)
21224 e.code = 0;
21225 e.lineno = 0;
21226 e.info = NULL;
21227 vec_safe_push (macinfo_table, e);
21229 e.code = DW_MACINFO_define;
21230 e.lineno = lineno;
21231 e.info = ggc_strdup (buffer);
21232 vec_safe_push (macinfo_table, e);
21236 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21237 the tail part of the directive line, i.e. the part which is past the
21238 initial whitespace, #, whitespace, directive-name, whitespace part. */
21240 static void
21241 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21242 const char *buffer ATTRIBUTE_UNUSED)
21244 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21246 macinfo_entry e;
21247 /* Insert a dummy first entry to be able to optimize the whole
21248 predefined macro block using DW_MACRO_GNU_transparent_include. */
21249 if (macinfo_table->is_empty () && lineno <= 1)
21251 e.code = 0;
21252 e.lineno = 0;
21253 e.info = NULL;
21254 vec_safe_push (macinfo_table, e);
21256 e.code = DW_MACINFO_undef;
21257 e.lineno = lineno;
21258 e.info = ggc_strdup (buffer);
21259 vec_safe_push (macinfo_table, e);
21263 /* Routines to manipulate hash table of CUs. */
21265 static hashval_t
21266 htab_macinfo_hash (const void *of)
21268 const macinfo_entry *const entry =
21269 (const macinfo_entry *) of;
21271 return htab_hash_string (entry->info);
21274 static int
21275 htab_macinfo_eq (const void *of1, const void *of2)
21277 const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
21278 const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
21280 return !strcmp (entry1->info, entry2->info);
21283 /* Output a single .debug_macinfo entry. */
21285 static void
21286 output_macinfo_op (macinfo_entry *ref)
21288 int file_num;
21289 size_t len;
21290 struct indirect_string_node *node;
21291 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21292 struct dwarf_file_data *fd;
21294 switch (ref->code)
21296 case DW_MACINFO_start_file:
21297 fd = lookup_filename (ref->info);
21298 file_num = maybe_emit_file (fd);
21299 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21300 dw2_asm_output_data_uleb128 (ref->lineno,
21301 "Included from line number %lu",
21302 (unsigned long) ref->lineno);
21303 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21304 break;
21305 case DW_MACINFO_end_file:
21306 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21307 break;
21308 case DW_MACINFO_define:
21309 case DW_MACINFO_undef:
21310 len = strlen (ref->info) + 1;
21311 if (!dwarf_strict
21312 && len > DWARF_OFFSET_SIZE
21313 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21314 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21316 ref->code = ref->code == DW_MACINFO_define
21317 ? DW_MACRO_GNU_define_indirect
21318 : DW_MACRO_GNU_undef_indirect;
21319 output_macinfo_op (ref);
21320 return;
21322 dw2_asm_output_data (1, ref->code,
21323 ref->code == DW_MACINFO_define
21324 ? "Define macro" : "Undefine macro");
21325 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21326 (unsigned long) ref->lineno);
21327 dw2_asm_output_nstring (ref->info, -1, "The macro");
21328 break;
21329 case DW_MACRO_GNU_define_indirect:
21330 case DW_MACRO_GNU_undef_indirect:
21331 node = find_AT_string (ref->info);
21332 gcc_assert (node
21333 && ((node->form == DW_FORM_strp)
21334 || (node->form == DW_FORM_GNU_str_index)));
21335 dw2_asm_output_data (1, ref->code,
21336 ref->code == DW_MACRO_GNU_define_indirect
21337 ? "Define macro indirect"
21338 : "Undefine macro indirect");
21339 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21340 (unsigned long) ref->lineno);
21341 if (node->form == DW_FORM_strp)
21342 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21343 debug_str_section, "The macro: \"%s\"",
21344 ref->info);
21345 else
21346 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21347 ref->info);
21348 break;
21349 case DW_MACRO_GNU_transparent_include:
21350 dw2_asm_output_data (1, ref->code, "Transparent include");
21351 ASM_GENERATE_INTERNAL_LABEL (label,
21352 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21353 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21354 break;
21355 default:
21356 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21357 ASM_COMMENT_START, (unsigned long) ref->code);
21358 break;
21362 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21363 other compilation unit .debug_macinfo sections. IDX is the first
21364 index of a define/undef, return the number of ops that should be
21365 emitted in a comdat .debug_macinfo section and emit
21366 a DW_MACRO_GNU_transparent_include entry referencing it.
21367 If the define/undef entry should be emitted normally, return 0. */
21369 static unsigned
21370 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21371 htab_t *macinfo_htab)
21373 macinfo_entry *first, *second, *cur, *inc;
21374 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21375 unsigned char checksum[16];
21376 struct md5_ctx ctx;
21377 char *grp_name, *tail;
21378 const char *base;
21379 unsigned int i, count, encoded_filename_len, linebuf_len;
21380 void **slot;
21382 first = &(*macinfo_table)[idx];
21383 second = &(*macinfo_table)[idx + 1];
21385 /* Optimize only if there are at least two consecutive define/undef ops,
21386 and either all of them are before first DW_MACINFO_start_file
21387 with lineno {0,1} (i.e. predefined macro block), or all of them are
21388 in some included header file. */
21389 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21390 return 0;
21391 if (vec_safe_is_empty (files))
21393 if (first->lineno > 1 || second->lineno > 1)
21394 return 0;
21396 else if (first->lineno == 0)
21397 return 0;
21399 /* Find the last define/undef entry that can be grouped together
21400 with first and at the same time compute md5 checksum of their
21401 codes, linenumbers and strings. */
21402 md5_init_ctx (&ctx);
21403 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21404 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21405 break;
21406 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21407 break;
21408 else
21410 unsigned char code = cur->code;
21411 md5_process_bytes (&code, 1, &ctx);
21412 checksum_uleb128 (cur->lineno, &ctx);
21413 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21415 md5_finish_ctx (&ctx, checksum);
21416 count = i - idx;
21418 /* From the containing include filename (if any) pick up just
21419 usable characters from its basename. */
21420 if (vec_safe_is_empty (files))
21421 base = "";
21422 else
21423 base = lbasename (files->last ().info);
21424 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21425 if (ISIDNUM (base[i]) || base[i] == '.')
21426 encoded_filename_len++;
21427 /* Count . at the end. */
21428 if (encoded_filename_len)
21429 encoded_filename_len++;
21431 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21432 linebuf_len = strlen (linebuf);
21434 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21435 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21436 + 16 * 2 + 1);
21437 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21438 tail = grp_name + 4;
21439 if (encoded_filename_len)
21441 for (i = 0; base[i]; i++)
21442 if (ISIDNUM (base[i]) || base[i] == '.')
21443 *tail++ = base[i];
21444 *tail++ = '.';
21446 memcpy (tail, linebuf, linebuf_len);
21447 tail += linebuf_len;
21448 *tail++ = '.';
21449 for (i = 0; i < 16; i++)
21450 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21452 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21453 in the empty vector entry before the first define/undef. */
21454 inc = &(*macinfo_table)[idx - 1];
21455 inc->code = DW_MACRO_GNU_transparent_include;
21456 inc->lineno = 0;
21457 inc->info = ggc_strdup (grp_name);
21458 if (*macinfo_htab == NULL)
21459 *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
21460 /* Avoid emitting duplicates. */
21461 slot = htab_find_slot (*macinfo_htab, inc, INSERT);
21462 if (*slot != NULL)
21464 inc->code = 0;
21465 inc->info = NULL;
21466 /* If such an entry has been used before, just emit
21467 a DW_MACRO_GNU_transparent_include op. */
21468 inc = (macinfo_entry *) *slot;
21469 output_macinfo_op (inc);
21470 /* And clear all macinfo_entry in the range to avoid emitting them
21471 in the second pass. */
21472 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21474 cur->code = 0;
21475 cur->info = NULL;
21478 else
21480 *slot = inc;
21481 inc->lineno = htab_elements (*macinfo_htab);
21482 output_macinfo_op (inc);
21484 return count;
21487 /* Save any strings needed by the macinfo table in the debug str
21488 table. All strings must be collected into the table by the time
21489 index_string is called. */
21491 static void
21492 save_macinfo_strings (void)
21494 unsigned len;
21495 unsigned i;
21496 macinfo_entry *ref;
21498 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21500 switch (ref->code)
21502 /* Match the logic in output_macinfo_op to decide on
21503 indirect strings. */
21504 case DW_MACINFO_define:
21505 case DW_MACINFO_undef:
21506 len = strlen (ref->info) + 1;
21507 if (!dwarf_strict
21508 && len > DWARF_OFFSET_SIZE
21509 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21510 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21511 set_indirect_string (find_AT_string (ref->info));
21512 break;
21513 case DW_MACRO_GNU_define_indirect:
21514 case DW_MACRO_GNU_undef_indirect:
21515 set_indirect_string (find_AT_string (ref->info));
21516 break;
21517 default:
21518 break;
21523 /* Output macinfo section(s). */
21525 static void
21526 output_macinfo (void)
21528 unsigned i;
21529 unsigned long length = vec_safe_length (macinfo_table);
21530 macinfo_entry *ref;
21531 vec<macinfo_entry, va_gc> *files = NULL;
21532 htab_t macinfo_htab = NULL;
21534 if (! length)
21535 return;
21537 /* output_macinfo* uses these interchangeably. */
21538 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21539 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21540 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21541 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21543 /* For .debug_macro emit the section header. */
21544 if (!dwarf_strict)
21546 dw2_asm_output_data (2, 4, "DWARF macro version number");
21547 if (DWARF_OFFSET_SIZE == 8)
21548 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21549 else
21550 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21551 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21552 (!dwarf_split_debug_info ? debug_line_section_label
21553 : debug_skeleton_line_section_label),
21554 debug_line_section, NULL);
21557 /* In the first loop, it emits the primary .debug_macinfo section
21558 and after each emitted op the macinfo_entry is cleared.
21559 If a longer range of define/undef ops can be optimized using
21560 DW_MACRO_GNU_transparent_include, the
21561 DW_MACRO_GNU_transparent_include op is emitted and kept in
21562 the vector before the first define/undef in the range and the
21563 whole range of define/undef ops is not emitted and kept. */
21564 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21566 switch (ref->code)
21568 case DW_MACINFO_start_file:
21569 vec_safe_push (files, *ref);
21570 break;
21571 case DW_MACINFO_end_file:
21572 if (!vec_safe_is_empty (files))
21573 files->pop ();
21574 break;
21575 case DW_MACINFO_define:
21576 case DW_MACINFO_undef:
21577 if (!dwarf_strict
21578 && HAVE_COMDAT_GROUP
21579 && vec_safe_length (files) != 1
21580 && i > 0
21581 && i + 1 < length
21582 && (*macinfo_table)[i - 1].code == 0)
21584 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21585 if (count)
21587 i += count - 1;
21588 continue;
21591 break;
21592 case 0:
21593 /* A dummy entry may be inserted at the beginning to be able
21594 to optimize the whole block of predefined macros. */
21595 if (i == 0)
21596 continue;
21597 default:
21598 break;
21600 output_macinfo_op (ref);
21601 ref->info = NULL;
21602 ref->code = 0;
21605 if (macinfo_htab == NULL)
21606 return;
21608 htab_delete (macinfo_htab);
21610 /* If any DW_MACRO_GNU_transparent_include were used, on those
21611 DW_MACRO_GNU_transparent_include entries terminate the
21612 current chain and switch to a new comdat .debug_macinfo
21613 section and emit the define/undef entries within it. */
21614 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21615 switch (ref->code)
21617 case 0:
21618 continue;
21619 case DW_MACRO_GNU_transparent_include:
21621 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21622 tree comdat_key = get_identifier (ref->info);
21623 /* Terminate the previous .debug_macinfo section. */
21624 dw2_asm_output_data (1, 0, "End compilation unit");
21625 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21626 SECTION_DEBUG
21627 | SECTION_LINKONCE,
21628 comdat_key);
21629 ASM_GENERATE_INTERNAL_LABEL (label,
21630 DEBUG_MACRO_SECTION_LABEL,
21631 ref->lineno);
21632 ASM_OUTPUT_LABEL (asm_out_file, label);
21633 ref->code = 0;
21634 ref->info = NULL;
21635 dw2_asm_output_data (2, 4, "DWARF macro version number");
21636 if (DWARF_OFFSET_SIZE == 8)
21637 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21638 else
21639 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21641 break;
21642 case DW_MACINFO_define:
21643 case DW_MACINFO_undef:
21644 output_macinfo_op (ref);
21645 ref->code = 0;
21646 ref->info = NULL;
21647 break;
21648 default:
21649 gcc_unreachable ();
21653 /* Set up for Dwarf output at the start of compilation. */
21655 static void
21656 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21658 /* Allocate the file_table. */
21659 file_table = htab_create_ggc (50, file_table_hash,
21660 file_table_eq, NULL);
21662 /* Allocate the decl_die_table. */
21663 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21664 decl_die_table_eq, NULL);
21666 /* Allocate the decl_loc_table. */
21667 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21668 decl_loc_table_eq, NULL);
21670 /* Allocate the cached_dw_loc_list_table. */
21671 cached_dw_loc_list_table
21672 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21673 cached_dw_loc_list_table_eq, NULL);
21675 /* Allocate the initial hunk of the decl_scope_table. */
21676 vec_alloc (decl_scope_table, 256);
21678 /* Allocate the initial hunk of the abbrev_die_table. */
21679 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21680 (ABBREV_DIE_TABLE_INCREMENT);
21681 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21682 /* Zero-th entry is allocated, but unused. */
21683 abbrev_die_table_in_use = 1;
21685 /* Allocate the pubtypes and pubnames vectors. */
21686 vec_alloc (pubname_table, 32);
21687 vec_alloc (pubtype_table, 32);
21689 vec_alloc (incomplete_types, 64);
21691 vec_alloc (used_rtx_array, 32);
21693 if (!dwarf_split_debug_info)
21695 debug_info_section = get_section (DEBUG_INFO_SECTION,
21696 SECTION_DEBUG, NULL);
21697 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21698 SECTION_DEBUG, NULL);
21699 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21700 SECTION_DEBUG, NULL);
21702 else
21704 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21705 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21706 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21707 SECTION_DEBUG | SECTION_EXCLUDE,
21708 NULL);
21709 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21710 SECTION_DEBUG, NULL);
21711 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21712 SECTION_DEBUG, NULL);
21713 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21714 SECTION_DEBUG, NULL);
21715 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21716 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21718 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21719 the main .o, but the skeleton_line goes into the split off dwo. */
21720 debug_skeleton_line_section
21721 = get_section (DEBUG_DWO_LINE_SECTION,
21722 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21723 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21724 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21725 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21726 SECTION_DEBUG | SECTION_EXCLUDE,
21727 NULL);
21728 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21729 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21730 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21731 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21733 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21734 SECTION_DEBUG, NULL);
21735 debug_macinfo_section = get_section (dwarf_strict
21736 ? DEBUG_MACINFO_SECTION
21737 : DEBUG_MACRO_SECTION,
21738 DEBUG_MACRO_SECTION_FLAGS, NULL);
21739 debug_line_section = get_section (DEBUG_LINE_SECTION,
21740 SECTION_DEBUG, NULL);
21741 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21742 SECTION_DEBUG, NULL);
21743 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21744 SECTION_DEBUG, NULL);
21745 debug_str_section = get_section (DEBUG_STR_SECTION,
21746 DEBUG_STR_SECTION_FLAGS, NULL);
21747 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21748 SECTION_DEBUG, NULL);
21749 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21750 SECTION_DEBUG, NULL);
21752 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21753 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21754 DEBUG_ABBREV_SECTION_LABEL, 0);
21755 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21756 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21757 COLD_TEXT_SECTION_LABEL, 0);
21758 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21760 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21761 DEBUG_INFO_SECTION_LABEL, 0);
21762 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21763 DEBUG_LINE_SECTION_LABEL, 0);
21764 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21765 DEBUG_RANGES_SECTION_LABEL, 0);
21766 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21767 DEBUG_ADDR_SECTION_LABEL, 0);
21768 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21769 dwarf_strict
21770 ? DEBUG_MACINFO_SECTION_LABEL
21771 : DEBUG_MACRO_SECTION_LABEL, 0);
21772 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21774 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21775 vec_alloc (macinfo_table, 64);
21777 switch_to_section (text_section);
21778 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21780 /* Make sure the line number table for .text always exists. */
21781 text_section_line_info = new_line_info_table ();
21782 text_section_line_info->end_label = text_end_label;
21785 /* Called before compile () starts outputtting functions, variables
21786 and toplevel asms into assembly. */
21788 static void
21789 dwarf2out_assembly_start (void)
21791 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21792 && dwarf2out_do_cfi_asm ()
21793 && (!(flag_unwind_tables || flag_exceptions)
21794 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21795 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21798 /* A helper function for dwarf2out_finish called through
21799 htab_traverse. Assign a string its index. All strings must be
21800 collected into the table by the time index_string is called,
21801 because the indexing code relies on htab_traverse to traverse nodes
21802 in the same order for each run. */
21804 static int
21805 index_string (void **h, void *v)
21807 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21808 unsigned int *index = (unsigned int *) v;
21810 find_string_form (node);
21811 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21813 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21814 node->index = *index;
21815 *index += 1;
21817 return 1;
21820 /* A helper function for output_indirect_strings called through
21821 htab_traverse. Output the offset to a string and update the
21822 current offset. */
21824 static int
21825 output_index_string_offset (void **h, void *v)
21827 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21828 unsigned int *offset = (unsigned int *) v;
21830 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21832 /* Assert that this node has been assigned an index. */
21833 gcc_assert (node->index != NO_INDEX_ASSIGNED
21834 && node->index != NOT_INDEXED);
21835 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21836 "indexed string 0x%x: %s", node->index, node->str);
21837 *offset += strlen (node->str) + 1;
21839 return 1;
21842 /* A helper function for dwarf2out_finish called through
21843 htab_traverse. Output the indexed string. */
21845 static int
21846 output_index_string (void **h, void *v)
21848 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21849 unsigned int *cur_idx = (unsigned int *) v;
21851 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21853 /* Assert that the strings are output in the same order as their
21854 indexes were assigned. */
21855 gcc_assert (*cur_idx == node->index);
21856 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21857 assemble_string (node->str, strlen (node->str) + 1);
21858 *cur_idx += 1;
21860 return 1;
21863 /* A helper function for dwarf2out_finish called through
21864 htab_traverse. Emit one queued .debug_str string. */
21866 static int
21867 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21869 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21871 if (node->form == DW_FORM_strp && node->refcount > 0)
21873 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21874 assemble_string (node->str, strlen (node->str) + 1);
21877 return 1;
21880 /* Output the indexed string table. */
21882 static void
21883 output_indirect_strings (void)
21885 if (!dwarf_split_debug_info)
21887 switch_to_section (debug_str_section);
21888 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21890 else
21892 unsigned int offset = 0;
21893 unsigned int cur_idx = 0;
21895 switch_to_section (debug_str_offsets_section);
21896 htab_traverse_noresize (debug_str_hash,
21897 output_index_string_offset,
21898 &offset);
21899 switch_to_section (debug_str_section);
21900 htab_traverse_noresize (debug_str_hash,
21901 output_index_string,
21902 &cur_idx);
21906 /* Callback for htab_traverse to assign an index to an entry in the
21907 table, and to write that entry to the .debug_addr section. */
21909 static int
21910 output_addr_table_entry (void **slot, void *data)
21912 addr_table_entry *entry = (addr_table_entry *) *slot;
21913 unsigned int *cur_index = (unsigned int *)data;
21915 if (entry->refcount == 0)
21917 gcc_assert (entry->index == NO_INDEX_ASSIGNED
21918 || entry->index == NOT_INDEXED);
21919 return 1;
21922 gcc_assert (entry->index == *cur_index);
21923 (*cur_index)++;
21925 switch (entry->kind)
21927 case ate_kind_rtx:
21928 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
21929 "0x%x", entry->index);
21930 break;
21931 case ate_kind_rtx_dtprel:
21932 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
21933 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
21934 DWARF2_ADDR_SIZE,
21935 entry->addr.rtl);
21936 fputc ('\n', asm_out_file);
21937 break;
21938 case ate_kind_label:
21939 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
21940 "0x%x", entry->index);
21941 break;
21942 default:
21943 gcc_unreachable ();
21945 return 1;
21948 /* Produce the .debug_addr section. */
21950 static void
21951 output_addr_table (void)
21953 unsigned int index = 0;
21954 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
21955 return;
21957 switch_to_section (debug_addr_section);
21958 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
21961 #if ENABLE_ASSERT_CHECKING
21962 /* Verify that all marks are clear. */
21964 static void
21965 verify_marks_clear (dw_die_ref die)
21967 dw_die_ref c;
21969 gcc_assert (! die->die_mark);
21970 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21972 #endif /* ENABLE_ASSERT_CHECKING */
21974 /* Clear the marks for a die and its children.
21975 Be cool if the mark isn't set. */
21977 static void
21978 prune_unmark_dies (dw_die_ref die)
21980 dw_die_ref c;
21982 if (die->die_mark)
21983 die->die_mark = 0;
21984 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21987 /* Given DIE that we're marking as used, find any other dies
21988 it references as attributes and mark them as used. */
21990 static void
21991 prune_unused_types_walk_attribs (dw_die_ref die)
21993 dw_attr_ref a;
21994 unsigned ix;
21996 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
21998 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22000 /* A reference to another DIE.
22001 Make sure that it will get emitted.
22002 If it was broken out into a comdat group, don't follow it. */
22003 if (! AT_ref (a)->comdat_type_p
22004 || a->dw_attr == DW_AT_specification)
22005 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22007 /* Set the string's refcount to 0 so that prune_unused_types_mark
22008 accounts properly for it. */
22009 if (AT_class (a) == dw_val_class_str)
22010 a->dw_attr_val.v.val_str->refcount = 0;
22014 /* Mark the generic parameters and arguments children DIEs of DIE. */
22016 static void
22017 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22019 dw_die_ref c;
22021 if (die == NULL || die->die_child == NULL)
22022 return;
22023 c = die->die_child;
22026 switch (c->die_tag)
22028 case DW_TAG_template_type_param:
22029 case DW_TAG_template_value_param:
22030 case DW_TAG_GNU_template_template_param:
22031 case DW_TAG_GNU_template_parameter_pack:
22032 prune_unused_types_mark (c, 1);
22033 break;
22034 default:
22035 break;
22037 c = c->die_sib;
22038 } while (c && c != die->die_child);
22041 /* Mark DIE as being used. If DOKIDS is true, then walk down
22042 to DIE's children. */
22044 static void
22045 prune_unused_types_mark (dw_die_ref die, int dokids)
22047 dw_die_ref c;
22049 if (die->die_mark == 0)
22051 /* We haven't done this node yet. Mark it as used. */
22052 die->die_mark = 1;
22053 /* If this is the DIE of a generic type instantiation,
22054 mark the children DIEs that describe its generic parms and
22055 args. */
22056 prune_unused_types_mark_generic_parms_dies (die);
22058 /* We also have to mark its parents as used.
22059 (But we don't want to mark our parent's kids due to this,
22060 unless it is a class.) */
22061 if (die->die_parent)
22062 prune_unused_types_mark (die->die_parent,
22063 class_scope_p (die->die_parent));
22065 /* Mark any referenced nodes. */
22066 prune_unused_types_walk_attribs (die);
22068 /* If this node is a specification,
22069 also mark the definition, if it exists. */
22070 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22071 prune_unused_types_mark (die->die_definition, 1);
22074 if (dokids && die->die_mark != 2)
22076 /* We need to walk the children, but haven't done so yet.
22077 Remember that we've walked the kids. */
22078 die->die_mark = 2;
22080 /* If this is an array type, we need to make sure our
22081 kids get marked, even if they're types. If we're
22082 breaking out types into comdat sections, do this
22083 for all type definitions. */
22084 if (die->die_tag == DW_TAG_array_type
22085 || (use_debug_types
22086 && is_type_die (die) && ! is_declaration_die (die)))
22087 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22088 else
22089 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22093 /* For local classes, look if any static member functions were emitted
22094 and if so, mark them. */
22096 static void
22097 prune_unused_types_walk_local_classes (dw_die_ref die)
22099 dw_die_ref c;
22101 if (die->die_mark == 2)
22102 return;
22104 switch (die->die_tag)
22106 case DW_TAG_structure_type:
22107 case DW_TAG_union_type:
22108 case DW_TAG_class_type:
22109 break;
22111 case DW_TAG_subprogram:
22112 if (!get_AT_flag (die, DW_AT_declaration)
22113 || die->die_definition != NULL)
22114 prune_unused_types_mark (die, 1);
22115 return;
22117 default:
22118 return;
22121 /* Mark children. */
22122 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22125 /* Walk the tree DIE and mark types that we actually use. */
22127 static void
22128 prune_unused_types_walk (dw_die_ref die)
22130 dw_die_ref c;
22132 /* Don't do anything if this node is already marked and
22133 children have been marked as well. */
22134 if (die->die_mark == 2)
22135 return;
22137 switch (die->die_tag)
22139 case DW_TAG_structure_type:
22140 case DW_TAG_union_type:
22141 case DW_TAG_class_type:
22142 if (die->die_perennial_p)
22143 break;
22145 for (c = die->die_parent; c; c = c->die_parent)
22146 if (c->die_tag == DW_TAG_subprogram)
22147 break;
22149 /* Finding used static member functions inside of classes
22150 is needed just for local classes, because for other classes
22151 static member function DIEs with DW_AT_specification
22152 are emitted outside of the DW_TAG_*_type. If we ever change
22153 it, we'd need to call this even for non-local classes. */
22154 if (c)
22155 prune_unused_types_walk_local_classes (die);
22157 /* It's a type node --- don't mark it. */
22158 return;
22160 case DW_TAG_const_type:
22161 case DW_TAG_packed_type:
22162 case DW_TAG_pointer_type:
22163 case DW_TAG_reference_type:
22164 case DW_TAG_rvalue_reference_type:
22165 case DW_TAG_volatile_type:
22166 case DW_TAG_typedef:
22167 case DW_TAG_array_type:
22168 case DW_TAG_interface_type:
22169 case DW_TAG_friend:
22170 case DW_TAG_variant_part:
22171 case DW_TAG_enumeration_type:
22172 case DW_TAG_subroutine_type:
22173 case DW_TAG_string_type:
22174 case DW_TAG_set_type:
22175 case DW_TAG_subrange_type:
22176 case DW_TAG_ptr_to_member_type:
22177 case DW_TAG_file_type:
22178 if (die->die_perennial_p)
22179 break;
22181 /* It's a type node --- don't mark it. */
22182 return;
22184 default:
22185 /* Mark everything else. */
22186 break;
22189 if (die->die_mark == 0)
22191 die->die_mark = 1;
22193 /* Now, mark any dies referenced from here. */
22194 prune_unused_types_walk_attribs (die);
22197 die->die_mark = 2;
22199 /* Mark children. */
22200 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22203 /* Increment the string counts on strings referred to from DIE's
22204 attributes. */
22206 static void
22207 prune_unused_types_update_strings (dw_die_ref die)
22209 dw_attr_ref a;
22210 unsigned ix;
22212 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22213 if (AT_class (a) == dw_val_class_str)
22215 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22216 s->refcount++;
22217 /* Avoid unnecessarily putting strings that are used less than
22218 twice in the hash table. */
22219 if (s->refcount
22220 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22222 void ** slot;
22223 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22224 htab_hash_string (s->str),
22225 INSERT);
22226 gcc_assert (*slot == NULL);
22227 *slot = s;
22232 /* Remove from the tree DIE any dies that aren't marked. */
22234 static void
22235 prune_unused_types_prune (dw_die_ref die)
22237 dw_die_ref c;
22239 gcc_assert (die->die_mark);
22240 prune_unused_types_update_strings (die);
22242 if (! die->die_child)
22243 return;
22245 c = die->die_child;
22246 do {
22247 dw_die_ref prev = c;
22248 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22249 if (c == die->die_child)
22251 /* No marked children between 'prev' and the end of the list. */
22252 if (prev == c)
22253 /* No marked children at all. */
22254 die->die_child = NULL;
22255 else
22257 prev->die_sib = c->die_sib;
22258 die->die_child = prev;
22260 return;
22263 if (c != prev->die_sib)
22264 prev->die_sib = c;
22265 prune_unused_types_prune (c);
22266 } while (c != die->die_child);
22269 /* Remove dies representing declarations that we never use. */
22271 static void
22272 prune_unused_types (void)
22274 unsigned int i;
22275 limbo_die_node *node;
22276 comdat_type_node *ctnode;
22277 pubname_ref pub;
22278 dw_die_ref base_type;
22280 #if ENABLE_ASSERT_CHECKING
22281 /* All the marks should already be clear. */
22282 verify_marks_clear (comp_unit_die ());
22283 for (node = limbo_die_list; node; node = node->next)
22284 verify_marks_clear (node->die);
22285 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22286 verify_marks_clear (ctnode->root_die);
22287 #endif /* ENABLE_ASSERT_CHECKING */
22289 /* Mark types that are used in global variables. */
22290 premark_types_used_by_global_vars ();
22292 /* Set the mark on nodes that are actually used. */
22293 prune_unused_types_walk (comp_unit_die ());
22294 for (node = limbo_die_list; node; node = node->next)
22295 prune_unused_types_walk (node->die);
22296 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22298 prune_unused_types_walk (ctnode->root_die);
22299 prune_unused_types_mark (ctnode->type_die, 1);
22302 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22303 are unusual in that they are pubnames that are the children of pubtypes.
22304 They should only be marked via their parent DW_TAG_enumeration_type die,
22305 not as roots in themselves. */
22306 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22307 if (pub->die->die_tag != DW_TAG_enumerator)
22308 prune_unused_types_mark (pub->die, 1);
22309 for (i = 0; base_types.iterate (i, &base_type); i++)
22310 prune_unused_types_mark (base_type, 1);
22312 if (debug_str_hash)
22313 htab_empty (debug_str_hash);
22314 prune_unused_types_prune (comp_unit_die ());
22315 for (node = limbo_die_list; node; node = node->next)
22316 prune_unused_types_prune (node->die);
22317 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22318 prune_unused_types_prune (ctnode->root_die);
22320 /* Leave the marks clear. */
22321 prune_unmark_dies (comp_unit_die ());
22322 for (node = limbo_die_list; node; node = node->next)
22323 prune_unmark_dies (node->die);
22324 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22325 prune_unmark_dies (ctnode->root_die);
22328 /* Set the parameter to true if there are any relative pathnames in
22329 the file table. */
22330 static int
22331 file_table_relative_p (void ** slot, void *param)
22333 bool *p = (bool *) param;
22334 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22335 if (!IS_ABSOLUTE_PATH (d->filename))
22337 *p = true;
22338 return 0;
22340 return 1;
22343 /* Routines to manipulate hash table of comdat type units. */
22345 static hashval_t
22346 htab_ct_hash (const void *of)
22348 hashval_t h;
22349 const comdat_type_node *const type_node = (const comdat_type_node *) of;
22351 memcpy (&h, type_node->signature, sizeof (h));
22352 return h;
22355 static int
22356 htab_ct_eq (const void *of1, const void *of2)
22358 const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22359 const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22361 return (! memcmp (type_node_1->signature, type_node_2->signature,
22362 DWARF_TYPE_SIGNATURE_SIZE));
22365 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22366 to the location it would have been added, should we know its
22367 DECL_ASSEMBLER_NAME when we added other attributes. This will
22368 probably improve compactness of debug info, removing equivalent
22369 abbrevs, and hide any differences caused by deferring the
22370 computation of the assembler name, triggered by e.g. PCH. */
22372 static inline void
22373 move_linkage_attr (dw_die_ref die)
22375 unsigned ix = vec_safe_length (die->die_attr);
22376 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22378 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22379 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22381 while (--ix > 0)
22383 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22385 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22386 break;
22389 if (ix != vec_safe_length (die->die_attr) - 1)
22391 die->die_attr->pop ();
22392 die->die_attr->quick_insert (ix, linkage);
22396 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22397 referenced from typed stack ops and count how often they are used. */
22399 static void
22400 mark_base_types (dw_loc_descr_ref loc)
22402 dw_die_ref base_type = NULL;
22404 for (; loc; loc = loc->dw_loc_next)
22406 switch (loc->dw_loc_opc)
22408 case DW_OP_GNU_regval_type:
22409 case DW_OP_GNU_deref_type:
22410 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22411 break;
22412 case DW_OP_GNU_convert:
22413 case DW_OP_GNU_reinterpret:
22414 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22415 continue;
22416 /* FALLTHRU */
22417 case DW_OP_GNU_const_type:
22418 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22419 break;
22420 case DW_OP_GNU_entry_value:
22421 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22422 continue;
22423 default:
22424 continue;
22426 gcc_assert (base_type->die_parent == comp_unit_die ());
22427 if (base_type->die_mark)
22428 base_type->die_mark++;
22429 else
22431 base_types.safe_push (base_type);
22432 base_type->die_mark = 1;
22437 /* Comparison function for sorting marked base types. */
22439 static int
22440 base_type_cmp (const void *x, const void *y)
22442 dw_die_ref dx = *(const dw_die_ref *) x;
22443 dw_die_ref dy = *(const dw_die_ref *) y;
22444 unsigned int byte_size1, byte_size2;
22445 unsigned int encoding1, encoding2;
22446 if (dx->die_mark > dy->die_mark)
22447 return -1;
22448 if (dx->die_mark < dy->die_mark)
22449 return 1;
22450 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22451 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22452 if (byte_size1 < byte_size2)
22453 return 1;
22454 if (byte_size1 > byte_size2)
22455 return -1;
22456 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22457 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22458 if (encoding1 < encoding2)
22459 return 1;
22460 if (encoding1 > encoding2)
22461 return -1;
22462 return 0;
22465 /* Move base types marked by mark_base_types as early as possible
22466 in the CU, sorted by decreasing usage count both to make the
22467 uleb128 references as small as possible and to make sure they
22468 will have die_offset already computed by calc_die_sizes when
22469 sizes of typed stack loc ops is computed. */
22471 static void
22472 move_marked_base_types (void)
22474 unsigned int i;
22475 dw_die_ref base_type, die, c;
22477 if (base_types.is_empty ())
22478 return;
22480 /* Sort by decreasing usage count, they will be added again in that
22481 order later on. */
22482 base_types.qsort (base_type_cmp);
22483 die = comp_unit_die ();
22484 c = die->die_child;
22487 dw_die_ref prev = c;
22488 c = c->die_sib;
22489 while (c->die_mark)
22491 remove_child_with_prev (c, prev);
22492 /* As base types got marked, there must be at least
22493 one node other than DW_TAG_base_type. */
22494 gcc_assert (c != c->die_sib);
22495 c = c->die_sib;
22498 while (c != die->die_child);
22499 gcc_assert (die->die_child);
22500 c = die->die_child;
22501 for (i = 0; base_types.iterate (i, &base_type); i++)
22503 base_type->die_mark = 0;
22504 base_type->die_sib = c->die_sib;
22505 c->die_sib = base_type;
22506 c = base_type;
22510 /* Helper function for resolve_addr, attempt to resolve
22511 one CONST_STRING, return non-zero if not successful. Similarly verify that
22512 SYMBOL_REFs refer to variables emitted in the current CU. */
22514 static int
22515 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22517 rtx rtl = *addr;
22519 if (GET_CODE (rtl) == CONST_STRING)
22521 size_t len = strlen (XSTR (rtl, 0)) + 1;
22522 tree t = build_string (len, XSTR (rtl, 0));
22523 tree tlen = size_int (len - 1);
22524 TREE_TYPE (t)
22525 = build_array_type (char_type_node, build_index_type (tlen));
22526 rtl = lookup_constant_def (t);
22527 if (!rtl || !MEM_P (rtl))
22528 return 1;
22529 rtl = XEXP (rtl, 0);
22530 vec_safe_push (used_rtx_array, rtl);
22531 *addr = rtl;
22532 return 0;
22535 if (GET_CODE (rtl) == SYMBOL_REF
22536 && SYMBOL_REF_DECL (rtl))
22538 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22540 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22541 return 1;
22543 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22544 return 1;
22547 if (GET_CODE (rtl) == CONST
22548 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22549 return 1;
22551 return 0;
22554 /* Helper function for resolve_addr, handle one location
22555 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22556 the location list couldn't be resolved. */
22558 static bool
22559 resolve_addr_in_expr (dw_loc_descr_ref loc)
22561 dw_loc_descr_ref keep = NULL;
22562 for (; loc; loc = loc->dw_loc_next)
22563 switch (loc->dw_loc_opc)
22565 case DW_OP_addr:
22566 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22567 return false;
22568 break;
22569 case DW_OP_GNU_addr_index:
22570 case DW_OP_GNU_const_index:
22572 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22573 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22574 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22575 NULL))
22576 return false;
22578 break;
22579 case DW_OP_const4u:
22580 case DW_OP_const8u:
22581 if (loc->dtprel
22582 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22583 return false;
22584 break;
22585 case DW_OP_plus_uconst:
22586 if (size_of_loc_descr (loc)
22587 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22589 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22591 dw_loc_descr_ref repl
22592 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22593 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22594 add_loc_descr (&repl, loc->dw_loc_next);
22595 *loc = *repl;
22597 break;
22598 case DW_OP_implicit_value:
22599 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22600 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22601 return false;
22602 break;
22603 case DW_OP_GNU_implicit_pointer:
22604 case DW_OP_GNU_parameter_ref:
22605 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22607 dw_die_ref ref
22608 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22609 if (ref == NULL)
22610 return false;
22611 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22612 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22613 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22615 break;
22616 case DW_OP_GNU_const_type:
22617 case DW_OP_GNU_regval_type:
22618 case DW_OP_GNU_deref_type:
22619 case DW_OP_GNU_convert:
22620 case DW_OP_GNU_reinterpret:
22621 while (loc->dw_loc_next
22622 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22624 dw_die_ref base1, base2;
22625 unsigned enc1, enc2, size1, size2;
22626 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22627 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22628 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22629 else if (loc->dw_loc_oprnd1.val_class
22630 == dw_val_class_unsigned_const)
22631 break;
22632 else
22633 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22634 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22635 == dw_val_class_unsigned_const)
22636 break;
22637 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22638 gcc_assert (base1->die_tag == DW_TAG_base_type
22639 && base2->die_tag == DW_TAG_base_type);
22640 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22641 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22642 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22643 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22644 if (size1 == size2
22645 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22646 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22647 && loc != keep)
22648 || enc1 == enc2))
22650 /* Optimize away next DW_OP_GNU_convert after
22651 adjusting LOC's base type die reference. */
22652 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22653 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22654 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22655 else
22656 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22657 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22658 continue;
22660 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22661 point typed stack entry. */
22662 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22663 keep = loc->dw_loc_next;
22664 break;
22666 break;
22667 default:
22668 break;
22670 return true;
22673 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22674 an address in .rodata section if the string literal is emitted there,
22675 or remove the containing location list or replace DW_AT_const_value
22676 with DW_AT_location and empty location expression, if it isn't found
22677 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22678 to something that has been emitted in the current CU. */
22680 static void
22681 resolve_addr (dw_die_ref die)
22683 dw_die_ref c;
22684 dw_attr_ref a;
22685 dw_loc_list_ref *curr, *start, loc;
22686 unsigned ix;
22688 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22689 switch (AT_class (a))
22691 case dw_val_class_loc_list:
22692 start = curr = AT_loc_list_ptr (a);
22693 loc = *curr;
22694 gcc_assert (loc);
22695 /* The same list can be referenced more than once. See if we have
22696 already recorded the result from a previous pass. */
22697 if (loc->replaced)
22698 *curr = loc->dw_loc_next;
22699 else if (!loc->resolved_addr)
22701 /* As things stand, we do not expect or allow one die to
22702 reference a suffix of another die's location list chain.
22703 References must be identical or completely separate.
22704 There is therefore no need to cache the result of this
22705 pass on any list other than the first; doing so
22706 would lead to unnecessary writes. */
22707 while (*curr)
22709 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22710 if (!resolve_addr_in_expr ((*curr)->expr))
22712 dw_loc_list_ref next = (*curr)->dw_loc_next;
22713 dw_loc_descr_ref l = (*curr)->expr;
22715 if (next && (*curr)->ll_symbol)
22717 gcc_assert (!next->ll_symbol);
22718 next->ll_symbol = (*curr)->ll_symbol;
22720 if (dwarf_split_debug_info)
22721 remove_loc_list_addr_table_entries (l);
22722 *curr = next;
22724 else
22726 mark_base_types ((*curr)->expr);
22727 curr = &(*curr)->dw_loc_next;
22730 if (loc == *start)
22731 loc->resolved_addr = 1;
22732 else
22734 loc->replaced = 1;
22735 if (dwarf_split_debug_info)
22736 remove_loc_list_addr_table_entries (loc->expr);
22737 loc->dw_loc_next = *start;
22740 if (!*start)
22742 remove_AT (die, a->dw_attr);
22743 ix--;
22745 break;
22746 case dw_val_class_loc:
22748 dw_loc_descr_ref l = AT_loc (a);
22749 /* For -gdwarf-2 don't attempt to optimize
22750 DW_AT_data_member_location containing
22751 DW_OP_plus_uconst - older consumers might
22752 rely on it being that op instead of a more complex,
22753 but shorter, location description. */
22754 if ((dwarf_version > 2
22755 || a->dw_attr != DW_AT_data_member_location
22756 || l == NULL
22757 || l->dw_loc_opc != DW_OP_plus_uconst
22758 || l->dw_loc_next != NULL)
22759 && !resolve_addr_in_expr (l))
22761 if (dwarf_split_debug_info)
22762 remove_loc_list_addr_table_entries (l);
22763 remove_AT (die, a->dw_attr);
22764 ix--;
22766 else
22767 mark_base_types (l);
22769 break;
22770 case dw_val_class_addr:
22771 if (a->dw_attr == DW_AT_const_value
22772 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22774 if (AT_index (a) != NOT_INDEXED)
22775 remove_addr_table_entry (a->dw_attr_val.val_entry);
22776 remove_AT (die, a->dw_attr);
22777 ix--;
22779 if (die->die_tag == DW_TAG_GNU_call_site
22780 && a->dw_attr == DW_AT_abstract_origin)
22782 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22783 dw_die_ref tdie = lookup_decl_die (tdecl);
22784 if (tdie == NULL
22785 && DECL_EXTERNAL (tdecl)
22786 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
22788 force_decl_die (tdecl);
22789 tdie = lookup_decl_die (tdecl);
22791 if (tdie)
22793 a->dw_attr_val.val_class = dw_val_class_die_ref;
22794 a->dw_attr_val.v.val_die_ref.die = tdie;
22795 a->dw_attr_val.v.val_die_ref.external = 0;
22797 else
22799 if (AT_index (a) != NOT_INDEXED)
22800 remove_addr_table_entry (a->dw_attr_val.val_entry);
22801 remove_AT (die, a->dw_attr);
22802 ix--;
22805 break;
22806 default:
22807 break;
22810 FOR_EACH_CHILD (die, c, resolve_addr (c));
22813 /* Helper routines for optimize_location_lists.
22814 This pass tries to share identical local lists in .debug_loc
22815 section. */
22817 /* Iteratively hash operands of LOC opcode. */
22819 static inline hashval_t
22820 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22822 dw_val_ref val1 = &loc->dw_loc_oprnd1;
22823 dw_val_ref val2 = &loc->dw_loc_oprnd2;
22825 switch (loc->dw_loc_opc)
22827 case DW_OP_const4u:
22828 case DW_OP_const8u:
22829 if (loc->dtprel)
22830 goto hash_addr;
22831 /* FALLTHRU */
22832 case DW_OP_const1u:
22833 case DW_OP_const1s:
22834 case DW_OP_const2u:
22835 case DW_OP_const2s:
22836 case DW_OP_const4s:
22837 case DW_OP_const8s:
22838 case DW_OP_constu:
22839 case DW_OP_consts:
22840 case DW_OP_pick:
22841 case DW_OP_plus_uconst:
22842 case DW_OP_breg0:
22843 case DW_OP_breg1:
22844 case DW_OP_breg2:
22845 case DW_OP_breg3:
22846 case DW_OP_breg4:
22847 case DW_OP_breg5:
22848 case DW_OP_breg6:
22849 case DW_OP_breg7:
22850 case DW_OP_breg8:
22851 case DW_OP_breg9:
22852 case DW_OP_breg10:
22853 case DW_OP_breg11:
22854 case DW_OP_breg12:
22855 case DW_OP_breg13:
22856 case DW_OP_breg14:
22857 case DW_OP_breg15:
22858 case DW_OP_breg16:
22859 case DW_OP_breg17:
22860 case DW_OP_breg18:
22861 case DW_OP_breg19:
22862 case DW_OP_breg20:
22863 case DW_OP_breg21:
22864 case DW_OP_breg22:
22865 case DW_OP_breg23:
22866 case DW_OP_breg24:
22867 case DW_OP_breg25:
22868 case DW_OP_breg26:
22869 case DW_OP_breg27:
22870 case DW_OP_breg28:
22871 case DW_OP_breg29:
22872 case DW_OP_breg30:
22873 case DW_OP_breg31:
22874 case DW_OP_regx:
22875 case DW_OP_fbreg:
22876 case DW_OP_piece:
22877 case DW_OP_deref_size:
22878 case DW_OP_xderef_size:
22879 hash = iterative_hash_object (val1->v.val_int, hash);
22880 break;
22881 case DW_OP_skip:
22882 case DW_OP_bra:
22884 int offset;
22886 gcc_assert (val1->val_class == dw_val_class_loc);
22887 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22888 hash = iterative_hash_object (offset, hash);
22890 break;
22891 case DW_OP_implicit_value:
22892 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22893 switch (val2->val_class)
22895 case dw_val_class_const:
22896 hash = iterative_hash_object (val2->v.val_int, hash);
22897 break;
22898 case dw_val_class_vec:
22900 unsigned int elt_size = val2->v.val_vec.elt_size;
22901 unsigned int len = val2->v.val_vec.length;
22903 hash = iterative_hash_object (elt_size, hash);
22904 hash = iterative_hash_object (len, hash);
22905 hash = iterative_hash (val2->v.val_vec.array,
22906 len * elt_size, hash);
22908 break;
22909 case dw_val_class_const_double:
22910 hash = iterative_hash_object (val2->v.val_double.low, hash);
22911 hash = iterative_hash_object (val2->v.val_double.high, hash);
22912 break;
22913 case dw_val_class_addr:
22914 hash = iterative_hash_rtx (val2->v.val_addr, hash);
22915 break;
22916 default:
22917 gcc_unreachable ();
22919 break;
22920 case DW_OP_bregx:
22921 case DW_OP_bit_piece:
22922 hash = iterative_hash_object (val1->v.val_int, hash);
22923 hash = iterative_hash_object (val2->v.val_int, hash);
22924 break;
22925 case DW_OP_addr:
22926 hash_addr:
22927 if (loc->dtprel)
22929 unsigned char dtprel = 0xd1;
22930 hash = iterative_hash_object (dtprel, hash);
22932 hash = iterative_hash_rtx (val1->v.val_addr, hash);
22933 break;
22934 case DW_OP_GNU_addr_index:
22935 case DW_OP_GNU_const_index:
22937 if (loc->dtprel)
22939 unsigned char dtprel = 0xd1;
22940 hash = iterative_hash_object (dtprel, hash);
22942 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
22944 break;
22945 case DW_OP_GNU_implicit_pointer:
22946 hash = iterative_hash_object (val2->v.val_int, hash);
22947 break;
22948 case DW_OP_GNU_entry_value:
22949 hash = hash_loc_operands (val1->v.val_loc, hash);
22950 break;
22951 case DW_OP_GNU_regval_type:
22952 case DW_OP_GNU_deref_type:
22954 unsigned int byte_size
22955 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22956 unsigned int encoding
22957 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22958 hash = iterative_hash_object (val1->v.val_int, hash);
22959 hash = iterative_hash_object (byte_size, hash);
22960 hash = iterative_hash_object (encoding, hash);
22962 break;
22963 case DW_OP_GNU_convert:
22964 case DW_OP_GNU_reinterpret:
22965 if (val1->val_class == dw_val_class_unsigned_const)
22967 hash = iterative_hash_object (val1->v.val_unsigned, hash);
22968 break;
22970 /* FALLTHRU */
22971 case DW_OP_GNU_const_type:
22973 unsigned int byte_size
22974 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22975 unsigned int encoding
22976 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22977 hash = iterative_hash_object (byte_size, hash);
22978 hash = iterative_hash_object (encoding, hash);
22979 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22980 break;
22981 hash = iterative_hash_object (val2->val_class, hash);
22982 switch (val2->val_class)
22984 case dw_val_class_const:
22985 hash = iterative_hash_object (val2->v.val_int, hash);
22986 break;
22987 case dw_val_class_vec:
22989 unsigned int elt_size = val2->v.val_vec.elt_size;
22990 unsigned int len = val2->v.val_vec.length;
22992 hash = iterative_hash_object (elt_size, hash);
22993 hash = iterative_hash_object (len, hash);
22994 hash = iterative_hash (val2->v.val_vec.array,
22995 len * elt_size, hash);
22997 break;
22998 case dw_val_class_const_double:
22999 hash = iterative_hash_object (val2->v.val_double.low, hash);
23000 hash = iterative_hash_object (val2->v.val_double.high, hash);
23001 break;
23002 default:
23003 gcc_unreachable ();
23006 break;
23008 default:
23009 /* Other codes have no operands. */
23010 break;
23012 return hash;
23015 /* Iteratively hash the whole DWARF location expression LOC. */
23017 static inline hashval_t
23018 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23020 dw_loc_descr_ref l;
23021 bool sizes_computed = false;
23022 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23023 size_of_locs (loc);
23025 for (l = loc; l != NULL; l = l->dw_loc_next)
23027 enum dwarf_location_atom opc = l->dw_loc_opc;
23028 hash = iterative_hash_object (opc, hash);
23029 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23031 size_of_locs (loc);
23032 sizes_computed = true;
23034 hash = hash_loc_operands (l, hash);
23036 return hash;
23039 /* Compute hash of the whole location list LIST_HEAD. */
23041 static inline void
23042 hash_loc_list (dw_loc_list_ref list_head)
23044 dw_loc_list_ref curr = list_head;
23045 hashval_t hash = 0;
23047 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23049 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23050 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23051 if (curr->section)
23052 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23053 hash);
23054 hash = hash_locs (curr->expr, hash);
23056 list_head->hash = hash;
23059 /* Return true if X and Y opcodes have the same operands. */
23061 static inline bool
23062 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23064 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23065 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23066 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23067 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23069 switch (x->dw_loc_opc)
23071 case DW_OP_const4u:
23072 case DW_OP_const8u:
23073 if (x->dtprel)
23074 goto hash_addr;
23075 /* FALLTHRU */
23076 case DW_OP_const1u:
23077 case DW_OP_const1s:
23078 case DW_OP_const2u:
23079 case DW_OP_const2s:
23080 case DW_OP_const4s:
23081 case DW_OP_const8s:
23082 case DW_OP_constu:
23083 case DW_OP_consts:
23084 case DW_OP_pick:
23085 case DW_OP_plus_uconst:
23086 case DW_OP_breg0:
23087 case DW_OP_breg1:
23088 case DW_OP_breg2:
23089 case DW_OP_breg3:
23090 case DW_OP_breg4:
23091 case DW_OP_breg5:
23092 case DW_OP_breg6:
23093 case DW_OP_breg7:
23094 case DW_OP_breg8:
23095 case DW_OP_breg9:
23096 case DW_OP_breg10:
23097 case DW_OP_breg11:
23098 case DW_OP_breg12:
23099 case DW_OP_breg13:
23100 case DW_OP_breg14:
23101 case DW_OP_breg15:
23102 case DW_OP_breg16:
23103 case DW_OP_breg17:
23104 case DW_OP_breg18:
23105 case DW_OP_breg19:
23106 case DW_OP_breg20:
23107 case DW_OP_breg21:
23108 case DW_OP_breg22:
23109 case DW_OP_breg23:
23110 case DW_OP_breg24:
23111 case DW_OP_breg25:
23112 case DW_OP_breg26:
23113 case DW_OP_breg27:
23114 case DW_OP_breg28:
23115 case DW_OP_breg29:
23116 case DW_OP_breg30:
23117 case DW_OP_breg31:
23118 case DW_OP_regx:
23119 case DW_OP_fbreg:
23120 case DW_OP_piece:
23121 case DW_OP_deref_size:
23122 case DW_OP_xderef_size:
23123 return valx1->v.val_int == valy1->v.val_int;
23124 case DW_OP_skip:
23125 case DW_OP_bra:
23126 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23127 can cause irrelevant differences in dw_loc_addr. */
23128 gcc_assert (valx1->val_class == dw_val_class_loc
23129 && valy1->val_class == dw_val_class_loc
23130 && (dwarf_split_debug_info
23131 || x->dw_loc_addr == y->dw_loc_addr));
23132 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23133 case DW_OP_implicit_value:
23134 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23135 || valx2->val_class != valy2->val_class)
23136 return false;
23137 switch (valx2->val_class)
23139 case dw_val_class_const:
23140 return valx2->v.val_int == valy2->v.val_int;
23141 case dw_val_class_vec:
23142 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23143 && valx2->v.val_vec.length == valy2->v.val_vec.length
23144 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23145 valx2->v.val_vec.elt_size
23146 * valx2->v.val_vec.length) == 0;
23147 case dw_val_class_const_double:
23148 return valx2->v.val_double.low == valy2->v.val_double.low
23149 && valx2->v.val_double.high == valy2->v.val_double.high;
23150 case dw_val_class_addr:
23151 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23152 default:
23153 gcc_unreachable ();
23155 case DW_OP_bregx:
23156 case DW_OP_bit_piece:
23157 return valx1->v.val_int == valy1->v.val_int
23158 && valx2->v.val_int == valy2->v.val_int;
23159 case DW_OP_addr:
23160 hash_addr:
23161 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23162 case DW_OP_GNU_addr_index:
23163 case DW_OP_GNU_const_index:
23165 rtx ax1 = valx1->val_entry->addr.rtl;
23166 rtx ay1 = valy1->val_entry->addr.rtl;
23167 return rtx_equal_p (ax1, ay1);
23169 case DW_OP_GNU_implicit_pointer:
23170 return valx1->val_class == dw_val_class_die_ref
23171 && valx1->val_class == valy1->val_class
23172 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23173 && valx2->v.val_int == valy2->v.val_int;
23174 case DW_OP_GNU_entry_value:
23175 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23176 case DW_OP_GNU_const_type:
23177 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23178 || valx2->val_class != valy2->val_class)
23179 return false;
23180 switch (valx2->val_class)
23182 case dw_val_class_const:
23183 return valx2->v.val_int == valy2->v.val_int;
23184 case dw_val_class_vec:
23185 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23186 && valx2->v.val_vec.length == valy2->v.val_vec.length
23187 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23188 valx2->v.val_vec.elt_size
23189 * valx2->v.val_vec.length) == 0;
23190 case dw_val_class_const_double:
23191 return valx2->v.val_double.low == valy2->v.val_double.low
23192 && valx2->v.val_double.high == valy2->v.val_double.high;
23193 default:
23194 gcc_unreachable ();
23196 case DW_OP_GNU_regval_type:
23197 case DW_OP_GNU_deref_type:
23198 return valx1->v.val_int == valy1->v.val_int
23199 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23200 case DW_OP_GNU_convert:
23201 case DW_OP_GNU_reinterpret:
23202 if (valx1->val_class != valy1->val_class)
23203 return false;
23204 if (valx1->val_class == dw_val_class_unsigned_const)
23205 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23206 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23207 case DW_OP_GNU_parameter_ref:
23208 return valx1->val_class == dw_val_class_die_ref
23209 && valx1->val_class == valy1->val_class
23210 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23211 default:
23212 /* Other codes have no operands. */
23213 return true;
23217 /* Return true if DWARF location expressions X and Y are the same. */
23219 static inline bool
23220 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23222 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23223 if (x->dw_loc_opc != y->dw_loc_opc
23224 || x->dtprel != y->dtprel
23225 || !compare_loc_operands (x, y))
23226 break;
23227 return x == NULL && y == NULL;
23230 /* Return precomputed hash of location list X. */
23232 static hashval_t
23233 loc_list_hash (const void *x)
23235 return ((const struct dw_loc_list_struct *) x)->hash;
23238 /* Return 1 if location lists X and Y are the same. */
23240 static int
23241 loc_list_eq (const void *x, const void *y)
23243 const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23244 const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23245 if (a == b)
23246 return 1;
23247 if (a->hash != b->hash)
23248 return 0;
23249 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23250 if (strcmp (a->begin, b->begin) != 0
23251 || strcmp (a->end, b->end) != 0
23252 || (a->section == NULL) != (b->section == NULL)
23253 || (a->section && strcmp (a->section, b->section) != 0)
23254 || !compare_locs (a->expr, b->expr))
23255 break;
23256 return a == NULL && b == NULL;
23259 /* Recursively optimize location lists referenced from DIE
23260 children and share them whenever possible. */
23262 static void
23263 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23265 dw_die_ref c;
23266 dw_attr_ref a;
23267 unsigned ix;
23268 void **slot;
23270 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23271 if (AT_class (a) == dw_val_class_loc_list)
23273 dw_loc_list_ref list = AT_loc_list (a);
23274 /* TODO: perform some optimizations here, before hashing
23275 it and storing into the hash table. */
23276 hash_loc_list (list);
23277 slot = htab_find_slot_with_hash (htab, list, list->hash,
23278 INSERT);
23279 if (*slot == NULL)
23280 *slot = (void *) list;
23281 else
23282 a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23285 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23289 /* Recursively assign each location list a unique index into the debug_addr
23290 section. */
23292 static void
23293 index_location_lists (dw_die_ref die)
23295 dw_die_ref c;
23296 dw_attr_ref a;
23297 unsigned ix;
23299 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23300 if (AT_class (a) == dw_val_class_loc_list)
23302 dw_loc_list_ref list = AT_loc_list (a);
23303 dw_loc_list_ref curr;
23304 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23306 /* Don't index an entry that has already been indexed
23307 or won't be output. */
23308 if (curr->begin_entry != NULL
23309 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23310 continue;
23312 curr->begin_entry
23313 = add_addr_table_entry (xstrdup (curr->begin),
23314 ate_kind_label);
23318 FOR_EACH_CHILD (die, c, index_location_lists (c));
23321 /* Optimize location lists referenced from DIE
23322 children and share them whenever possible. */
23324 static void
23325 optimize_location_lists (dw_die_ref die)
23327 htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23328 optimize_location_lists_1 (die, htab);
23329 htab_delete (htab);
23332 /* Output stuff that dwarf requires at the end of every file,
23333 and generate the DWARF-2 debugging info. */
23335 static void
23336 dwarf2out_finish (const char *filename)
23338 limbo_die_node *node, *next_node;
23339 comdat_type_node *ctnode;
23340 htab_t comdat_type_table;
23341 unsigned int i;
23342 dw_die_ref main_comp_unit_die;
23344 /* PCH might result in DW_AT_producer string being restored from the
23345 header compilation, so always fill it with empty string initially
23346 and overwrite only here. */
23347 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23348 producer_string = gen_producer_string ();
23349 producer->dw_attr_val.v.val_str->refcount--;
23350 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23352 gen_scheduled_generic_parms_dies ();
23353 gen_remaining_tmpl_value_param_die_attribute ();
23355 /* Add the name for the main input file now. We delayed this from
23356 dwarf2out_init to avoid complications with PCH. */
23357 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23358 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23359 add_comp_dir_attribute (comp_unit_die ());
23360 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23362 bool p = false;
23363 htab_traverse (file_table, file_table_relative_p, &p);
23364 if (p)
23365 add_comp_dir_attribute (comp_unit_die ());
23368 if (deferred_locations_list)
23369 for (i = 0; i < deferred_locations_list->length (); i++)
23371 add_location_or_const_value_attribute (
23372 (*deferred_locations_list)[i].die,
23373 (*deferred_locations_list)[i].variable,
23374 false,
23375 DW_AT_location);
23378 /* Traverse the limbo die list, and add parent/child links. The only
23379 dies without parents that should be here are concrete instances of
23380 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23381 For concrete instances, we can get the parent die from the abstract
23382 instance. */
23383 for (node = limbo_die_list; node; node = next_node)
23385 dw_die_ref die = node->die;
23386 next_node = node->next;
23388 if (die->die_parent == NULL)
23390 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23392 if (origin && origin->die_parent)
23393 add_child_die (origin->die_parent, die);
23394 else if (is_cu_die (die))
23396 else if (seen_error ())
23397 /* It's OK to be confused by errors in the input. */
23398 add_child_die (comp_unit_die (), die);
23399 else
23401 /* In certain situations, the lexical block containing a
23402 nested function can be optimized away, which results
23403 in the nested function die being orphaned. Likewise
23404 with the return type of that nested function. Force
23405 this to be a child of the containing function.
23407 It may happen that even the containing function got fully
23408 inlined and optimized out. In that case we are lost and
23409 assign the empty child. This should not be big issue as
23410 the function is likely unreachable too. */
23411 gcc_assert (node->created_for);
23413 if (DECL_P (node->created_for))
23414 origin = get_context_die (DECL_CONTEXT (node->created_for));
23415 else if (TYPE_P (node->created_for))
23416 origin = scope_die_for (node->created_for, comp_unit_die ());
23417 else
23418 origin = comp_unit_die ();
23420 add_child_die (origin, die);
23425 limbo_die_list = NULL;
23427 #if ENABLE_ASSERT_CHECKING
23429 dw_die_ref die = comp_unit_die (), c;
23430 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23432 #endif
23433 resolve_addr (comp_unit_die ());
23434 move_marked_base_types ();
23436 for (node = deferred_asm_name; node; node = node->next)
23438 tree decl = node->created_for;
23439 /* When generating LTO bytecode we can not generate new assembler
23440 names at this point and all important decls got theirs via
23441 free-lang-data. */
23442 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23443 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23445 add_linkage_attr (node->die, decl);
23446 move_linkage_attr (node->die);
23450 deferred_asm_name = NULL;
23452 /* Walk through the list of incomplete types again, trying once more to
23453 emit full debugging info for them. */
23454 retry_incomplete_types ();
23456 if (flag_eliminate_unused_debug_types)
23457 prune_unused_types ();
23459 /* Generate separate COMDAT sections for type DIEs. */
23460 if (use_debug_types)
23462 break_out_comdat_types (comp_unit_die ());
23464 /* Each new type_unit DIE was added to the limbo die list when created.
23465 Since these have all been added to comdat_type_list, clear the
23466 limbo die list. */
23467 limbo_die_list = NULL;
23469 /* For each new comdat type unit, copy declarations for incomplete
23470 types to make the new unit self-contained (i.e., no direct
23471 references to the main compile unit). */
23472 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23473 copy_decls_for_unworthy_types (ctnode->root_die);
23474 copy_decls_for_unworthy_types (comp_unit_die ());
23476 /* In the process of copying declarations from one unit to another,
23477 we may have left some declarations behind that are no longer
23478 referenced. Prune them. */
23479 prune_unused_types ();
23482 /* Generate separate CUs for each of the include files we've seen.
23483 They will go into limbo_die_list. */
23484 if (flag_eliminate_dwarf2_dups)
23485 break_out_includes (comp_unit_die ());
23487 /* Traverse the DIE's and add add sibling attributes to those DIE's
23488 that have children. */
23489 add_sibling_attributes (comp_unit_die ());
23490 for (node = limbo_die_list; node; node = node->next)
23491 add_sibling_attributes (node->die);
23492 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23493 add_sibling_attributes (ctnode->root_die);
23495 /* When splitting DWARF info, we put some attributes in the
23496 skeleton compile_unit DIE that remains in the .o, while
23497 most attributes go in the DWO compile_unit_die. */
23498 if (dwarf_split_debug_info)
23499 main_comp_unit_die = gen_compile_unit_die (NULL);
23500 else
23501 main_comp_unit_die = comp_unit_die ();
23503 /* Output a terminator label for the .text section. */
23504 switch_to_section (text_section);
23505 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23506 if (cold_text_section)
23508 switch_to_section (cold_text_section);
23509 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23512 /* We can only use the low/high_pc attributes if all of the code was
23513 in .text. */
23514 if (!have_multiple_function_sections
23515 || (dwarf_version < 3 && dwarf_strict))
23517 /* Don't add if the CU has no associated code. */
23518 if (text_section_used)
23519 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23520 text_end_label, true);
23522 else
23524 unsigned fde_idx;
23525 dw_fde_ref fde;
23526 bool range_list_added = false;
23528 if (text_section_used)
23529 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23530 text_end_label, &range_list_added, true);
23531 if (cold_text_section_used)
23532 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23533 cold_end_label, &range_list_added, true);
23535 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23537 if (DECL_IGNORED_P (fde->decl))
23538 continue;
23539 if (!fde->in_std_section)
23540 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23541 fde->dw_fde_end, &range_list_added,
23542 true);
23543 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23544 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23545 fde->dw_fde_second_end, &range_list_added,
23546 true);
23549 if (range_list_added)
23551 /* We need to give .debug_loc and .debug_ranges an appropriate
23552 "base address". Use zero so that these addresses become
23553 absolute. Historically, we've emitted the unexpected
23554 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23555 Emit both to give time for other tools to adapt. */
23556 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23557 if (! dwarf_strict && dwarf_version < 4)
23558 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23560 add_ranges (NULL);
23564 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23565 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23566 debug_line_section_label);
23568 if (have_macinfo)
23569 add_AT_macptr (comp_unit_die (),
23570 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23571 macinfo_section_label);
23573 if (dwarf_split_debug_info && addr_index_table != NULL)
23575 /* optimize_location_lists calculates the size of the lists,
23576 so index them first, and assign indices to the entries.
23577 Although optimize_location_lists will remove entries from
23578 the table, it only does so for duplicates, and therefore
23579 only reduces ref_counts to 1. */
23580 unsigned int index = 0;
23581 index_location_lists (comp_unit_die ());
23582 htab_traverse_noresize (addr_index_table,
23583 index_addr_table_entry, &index);
23585 if (have_location_lists)
23586 optimize_location_lists (comp_unit_die ());
23588 save_macinfo_strings ();
23589 if (dwarf_split_debug_info)
23591 unsigned int index = 0;
23592 htab_traverse_noresize (debug_str_hash, index_string, &index);
23595 /* Output all of the compilation units. We put the main one last so that
23596 the offsets are available to output_pubnames. */
23597 for (node = limbo_die_list; node; node = node->next)
23598 output_comp_unit (node->die, 0);
23600 comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23601 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23603 void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23605 /* Don't output duplicate types. */
23606 if (*slot != HTAB_EMPTY_ENTRY)
23607 continue;
23609 /* Add a pointer to the line table for the main compilation unit
23610 so that the debugger can make sense of DW_AT_decl_file
23611 attributes. */
23612 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23613 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23614 (!dwarf_split_debug_info
23615 ? debug_line_section_label
23616 : debug_skeleton_line_section_label));
23618 output_comdat_type_unit (ctnode);
23619 *slot = ctnode;
23621 htab_delete (comdat_type_table);
23623 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23624 both the main_cu and all skeleton TUs. Making this call unconditional
23625 would end up either adding a second copy of the AT_pubnames attribute, or
23626 requiring a special case in add_top_level_skeleton_die_attrs. */
23627 if (!dwarf_split_debug_info)
23628 add_AT_pubnames (comp_unit_die ());
23630 if (dwarf_split_debug_info)
23632 int mark;
23633 unsigned char checksum[16];
23634 struct md5_ctx ctx;
23636 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23637 md5_init_ctx (&ctx);
23638 mark = 0;
23639 die_checksum (comp_unit_die (), &ctx, &mark);
23640 unmark_all_dies (comp_unit_die ());
23641 md5_finish_ctx (&ctx, checksum);
23643 /* Use the first 8 bytes of the checksum as the dwo_id,
23644 and add it to both comp-unit DIEs. */
23645 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23646 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23648 /* Add the base offset of the ranges table to the skeleton
23649 comp-unit DIE. */
23650 if (ranges_table_in_use)
23651 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23652 ranges_section_label);
23654 switch_to_section (debug_addr_section);
23655 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23656 output_addr_table ();
23659 /* Output the main compilation unit if non-empty or if .debug_macinfo
23660 or .debug_macro will be emitted. */
23661 output_comp_unit (comp_unit_die (), have_macinfo);
23663 if (dwarf_split_debug_info && info_section_emitted)
23664 output_skeleton_debug_sections (main_comp_unit_die);
23666 /* Output the abbreviation table. */
23667 if (abbrev_die_table_in_use != 1)
23669 switch_to_section (debug_abbrev_section);
23670 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23671 output_abbrev_section ();
23674 /* Output location list section if necessary. */
23675 if (have_location_lists)
23677 /* Output the location lists info. */
23678 switch_to_section (debug_loc_section);
23679 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23680 output_location_lists (comp_unit_die ());
23683 /* Output public names and types tables if necessary. */
23684 output_pubnames (pubname_table);
23685 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23686 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23687 simply won't look for the section. */
23688 output_pubnames (pubtype_table);
23690 /* Output the address range information if a CU (.debug_info section)
23691 was emitted. We output an empty table even if we had no functions
23692 to put in it. This because the consumer has no way to tell the
23693 difference between an empty table that we omitted and failure to
23694 generate a table that would have contained data. */
23695 if (info_section_emitted)
23697 unsigned long aranges_length = size_of_aranges ();
23699 switch_to_section (debug_aranges_section);
23700 output_aranges (aranges_length);
23703 /* Output ranges section if necessary. */
23704 if (ranges_table_in_use)
23706 switch_to_section (debug_ranges_section);
23707 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23708 output_ranges ();
23711 /* Have to end the macro section. */
23712 if (have_macinfo)
23714 switch_to_section (debug_macinfo_section);
23715 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23716 output_macinfo ();
23717 dw2_asm_output_data (1, 0, "End compilation unit");
23720 /* Output the source line correspondence table. We must do this
23721 even if there is no line information. Otherwise, on an empty
23722 translation unit, we will generate a present, but empty,
23723 .debug_info section. IRIX 6.5 `nm' will then complain when
23724 examining the file. This is done late so that any filenames
23725 used by the debug_info section are marked as 'used'. */
23726 switch_to_section (debug_line_section);
23727 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23728 if (! DWARF2_ASM_LINE_DEBUG_INFO)
23729 output_line_info (false);
23731 if (dwarf_split_debug_info && info_section_emitted)
23733 switch_to_section (debug_skeleton_line_section);
23734 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
23735 output_line_info (true);
23738 /* If we emitted any indirect strings, output the string table too. */
23739 if (debug_str_hash)
23740 output_indirect_strings ();
23743 #include "gt-dwarf2out.h"