2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / dwarf2out.c
blob23cd72666c1af197e2f49167d55fe41fce1f9a1a
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 "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "gimple.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "ggc.h"
85 #include "md5.h"
86 #include "tm_p.h"
87 #include "diagnostic.h"
88 #include "tree-pretty-print.h"
89 #include "debug.h"
90 #include "target.h"
91 #include "common/common-target.h"
92 #include "langhooks.h"
93 #include "hash-table.h"
94 #include "cgraph.h"
95 #include "input.h"
96 #include "ira.h"
97 #include "lra.h"
98 #include "dumpfile.h"
99 #include "opts.h"
100 #include "tree-dfa.h"
101 #include "gdb/gdb-index.h"
103 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
104 static rtx last_var_location_insn;
105 static rtx cached_next_real_insn;
107 #ifdef VMS_DEBUGGING_INFO
108 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
110 /* Define this macro to be a nonzero value if the directory specifications
111 which are output in the debug info should end with a separator. */
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
113 /* Define this macro to evaluate to a nonzero value if GCC should refrain
114 from generating indirect strings in DWARF2 debug information, for instance
115 if your target is stuck with an old version of GDB that is unable to
116 process them properly or uses VMS Debug. */
117 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
118 #else
119 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
120 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
121 #endif
123 /* ??? Poison these here until it can be done generically. They've been
124 totally replaced in this file; make sure it stays that way. */
125 #undef DWARF2_UNWIND_INFO
126 #undef DWARF2_FRAME_INFO
127 #if (GCC_VERSION >= 3000)
128 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
129 #endif
131 /* The size of the target's pointer type. */
132 #ifndef PTR_SIZE
133 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
134 #endif
136 /* Array of RTXes referenced by the debugging information, which therefore
137 must be kept around forever. */
138 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
140 /* A pointer to the base of a list of incomplete types which might be
141 completed at some later time. incomplete_types_list needs to be a
142 vec<tree, va_gc> *because we want to tell the garbage collector about
143 it. */
144 static GTY(()) vec<tree, va_gc> *incomplete_types;
146 /* A pointer to the base of a table of references to declaration
147 scopes. This table is a display which tracks the nesting
148 of declaration scopes at the current scope and containing
149 scopes. This table is used to find the proper place to
150 define type declaration DIE's. */
151 static GTY(()) vec<tree, va_gc> *decl_scope_table;
153 /* Pointers to various DWARF2 sections. */
154 static GTY(()) section *debug_info_section;
155 static GTY(()) section *debug_skeleton_info_section;
156 static GTY(()) section *debug_abbrev_section;
157 static GTY(()) section *debug_skeleton_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_addr_section;
160 static GTY(()) section *debug_macinfo_section;
161 static GTY(()) section *debug_line_section;
162 static GTY(()) section *debug_skeleton_line_section;
163 static GTY(()) section *debug_loc_section;
164 static GTY(()) section *debug_pubnames_section;
165 static GTY(()) section *debug_pubtypes_section;
166 static GTY(()) section *debug_str_section;
167 static GTY(()) section *debug_str_dwo_section;
168 static GTY(()) section *debug_str_offsets_section;
169 static GTY(()) section *debug_ranges_section;
170 static GTY(()) section *debug_frame_section;
172 /* Maximum size (in bytes) of an artificially generated label. */
173 #define MAX_ARTIFICIAL_LABEL_BYTES 30
175 /* According to the (draft) DWARF 3 specification, the initial length
176 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
177 bytes are 0xffffffff, followed by the length stored in the next 8
178 bytes.
180 However, the SGI/MIPS ABI uses an initial length which is equal to
181 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
183 #ifndef DWARF_INITIAL_LENGTH_SIZE
184 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
185 #endif
187 /* Round SIZE up to the nearest BOUNDARY. */
188 #define DWARF_ROUND(SIZE,BOUNDARY) \
189 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
191 /* CIE identifier. */
192 #if HOST_BITS_PER_WIDE_INT >= 64
193 #define DWARF_CIE_ID \
194 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
195 #else
196 #define DWARF_CIE_ID DW_CIE_ID
197 #endif
200 /* A vector for a table that contains frame description
201 information for each routine. */
202 #define NOT_INDEXED (-1U)
203 #define NO_INDEX_ASSIGNED (-2U)
205 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
207 struct GTY(()) indirect_string_node {
208 const char *str;
209 unsigned int refcount;
210 enum dwarf_form form;
211 char *label;
212 unsigned int index;
215 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
217 /* With split_debug_info, both the comp_dir and dwo_name go in the
218 main object file, rather than the dwo, similar to the force_direct
219 parameter elsewhere but with additional complications:
221 1) The string is needed in both the main object file and the dwo.
222 That is, the comp_dir and dwo_name will appear in both places.
224 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
225 DW_FORM_GNU_str_index.
227 3) GCC chooses the form to use late, depending on the size and
228 reference count.
230 Rather than forcing the all debug string handling functions and
231 callers to deal with these complications, simply use a separate,
232 special-cased string table for any attribute that should go in the
233 main object file. This limits the complexity to just the places
234 that need it. */
236 static GTY ((param_is (struct indirect_string_node)))
237 htab_t skeleton_debug_str_hash;
239 static GTY(()) int dw2_string_counter;
241 /* True if the compilation unit places functions in more than one section. */
242 static GTY(()) bool have_multiple_function_sections = false;
244 /* Whether the default text and cold text sections have been used at all. */
246 static GTY(()) bool text_section_used = false;
247 static GTY(()) bool cold_text_section_used = false;
249 /* The default cold text section. */
250 static GTY(()) section *cold_text_section;
252 /* Forward declarations for functions defined in this file. */
254 static char *stripattributes (const char *);
255 static void output_call_frame_info (int);
256 static void dwarf2out_note_section_used (void);
258 /* Personality decl of current unit. Used only when assembler does not support
259 personality CFI. */
260 static GTY(()) rtx current_unit_personality;
262 /* Data and reference forms for relocatable data. */
263 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
264 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
266 #ifndef DEBUG_FRAME_SECTION
267 #define DEBUG_FRAME_SECTION ".debug_frame"
268 #endif
270 #ifndef FUNC_BEGIN_LABEL
271 #define FUNC_BEGIN_LABEL "LFB"
272 #endif
274 #ifndef FUNC_END_LABEL
275 #define FUNC_END_LABEL "LFE"
276 #endif
278 #ifndef PROLOGUE_END_LABEL
279 #define PROLOGUE_END_LABEL "LPE"
280 #endif
282 #ifndef EPILOGUE_BEGIN_LABEL
283 #define EPILOGUE_BEGIN_LABEL "LEB"
284 #endif
286 #ifndef FRAME_BEGIN_LABEL
287 #define FRAME_BEGIN_LABEL "Lframe"
288 #endif
289 #define CIE_AFTER_SIZE_LABEL "LSCIE"
290 #define CIE_END_LABEL "LECIE"
291 #define FDE_LABEL "LSFDE"
292 #define FDE_AFTER_SIZE_LABEL "LASFDE"
293 #define FDE_END_LABEL "LEFDE"
294 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
295 #define LINE_NUMBER_END_LABEL "LELT"
296 #define LN_PROLOG_AS_LABEL "LASLTP"
297 #define LN_PROLOG_END_LABEL "LELTP"
298 #define DIE_LABEL_PREFIX "DW"
300 /* Match the base name of a file to the base name of a compilation unit. */
302 static int
303 matches_main_base (const char *path)
305 /* Cache the last query. */
306 static const char *last_path = NULL;
307 static int last_match = 0;
308 if (path != last_path)
310 const char *base;
311 int length = base_of_path (path, &base);
312 last_path = path;
313 last_match = (length == main_input_baselength
314 && memcmp (base, main_input_basename, length) == 0);
316 return last_match;
319 #ifdef DEBUG_DEBUG_STRUCT
321 static int
322 dump_struct_debug (tree type, enum debug_info_usage usage,
323 enum debug_struct_file criterion, int generic,
324 int matches, int result)
326 /* Find the type name. */
327 tree type_decl = TYPE_STUB_DECL (type);
328 tree t = type_decl;
329 const char *name = 0;
330 if (TREE_CODE (t) == TYPE_DECL)
331 t = DECL_NAME (t);
332 if (t)
333 name = IDENTIFIER_POINTER (t);
335 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
336 criterion,
337 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
338 matches ? "bas" : "hdr",
339 generic ? "gen" : "ord",
340 usage == DINFO_USAGE_DFN ? ";" :
341 usage == DINFO_USAGE_DIR_USE ? "." : "*",
342 result,
343 (void*) type_decl, name);
344 return result;
346 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
347 dump_struct_debug (type, usage, criterion, generic, matches, result)
349 #else
351 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
352 (result)
354 #endif
356 static bool
357 should_emit_struct_debug (tree type, enum debug_info_usage usage)
359 enum debug_struct_file criterion;
360 tree type_decl;
361 bool generic = lang_hooks.types.generic_p (type);
363 if (generic)
364 criterion = debug_struct_generic[usage];
365 else
366 criterion = debug_struct_ordinary[usage];
368 if (criterion == DINFO_STRUCT_FILE_NONE)
369 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
370 if (criterion == DINFO_STRUCT_FILE_ANY)
371 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
373 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
375 if (type_decl != NULL)
377 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
378 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
380 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
381 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
384 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
387 /* Return a pointer to a copy of the section string name S with all
388 attributes stripped off, and an asterisk prepended (for assemble_name). */
390 static inline char *
391 stripattributes (const char *s)
393 char *stripped = XNEWVEC (char, strlen (s) + 2);
394 char *p = stripped;
396 *p++ = '*';
398 while (*s && *s != ',')
399 *p++ = *s++;
401 *p = '\0';
402 return stripped;
405 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
406 switch to the data section instead, and write out a synthetic start label
407 for collect2 the first time around. */
409 static void
410 switch_to_eh_frame_section (bool back)
412 tree label;
414 #ifdef EH_FRAME_SECTION_NAME
415 if (eh_frame_section == 0)
417 int flags;
419 if (EH_TABLES_CAN_BE_READ_ONLY)
421 int fde_encoding;
422 int per_encoding;
423 int lsda_encoding;
425 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
426 /*global=*/0);
427 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
428 /*global=*/1);
429 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
430 /*global=*/0);
431 flags = ((! flag_pic
432 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
433 && (fde_encoding & 0x70) != DW_EH_PE_aligned
434 && (per_encoding & 0x70) != DW_EH_PE_absptr
435 && (per_encoding & 0x70) != DW_EH_PE_aligned
436 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
437 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
438 ? 0 : SECTION_WRITE);
440 else
441 flags = SECTION_WRITE;
442 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
444 #endif /* EH_FRAME_SECTION_NAME */
446 if (eh_frame_section)
447 switch_to_section (eh_frame_section);
448 else
450 /* We have no special eh_frame section. Put the information in
451 the data section and emit special labels to guide collect2. */
452 switch_to_section (data_section);
454 if (!back)
456 label = get_file_function_name ("F");
457 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
458 targetm.asm_out.globalize_label (asm_out_file,
459 IDENTIFIER_POINTER (label));
460 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
465 /* Switch [BACK] to the eh or debug frame table section, depending on
466 FOR_EH. */
468 static void
469 switch_to_frame_table_section (int for_eh, bool back)
471 if (for_eh)
472 switch_to_eh_frame_section (back);
473 else
475 if (!debug_frame_section)
476 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
477 SECTION_DEBUG, NULL);
478 switch_to_section (debug_frame_section);
482 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
484 enum dw_cfi_oprnd_type
485 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
487 switch (cfi)
489 case DW_CFA_nop:
490 case DW_CFA_GNU_window_save:
491 case DW_CFA_remember_state:
492 case DW_CFA_restore_state:
493 return dw_cfi_oprnd_unused;
495 case DW_CFA_set_loc:
496 case DW_CFA_advance_loc1:
497 case DW_CFA_advance_loc2:
498 case DW_CFA_advance_loc4:
499 case DW_CFA_MIPS_advance_loc8:
500 return dw_cfi_oprnd_addr;
502 case DW_CFA_offset:
503 case DW_CFA_offset_extended:
504 case DW_CFA_def_cfa:
505 case DW_CFA_offset_extended_sf:
506 case DW_CFA_def_cfa_sf:
507 case DW_CFA_restore:
508 case DW_CFA_restore_extended:
509 case DW_CFA_undefined:
510 case DW_CFA_same_value:
511 case DW_CFA_def_cfa_register:
512 case DW_CFA_register:
513 case DW_CFA_expression:
514 return dw_cfi_oprnd_reg_num;
516 case DW_CFA_def_cfa_offset:
517 case DW_CFA_GNU_args_size:
518 case DW_CFA_def_cfa_offset_sf:
519 return dw_cfi_oprnd_offset;
521 case DW_CFA_def_cfa_expression:
522 return dw_cfi_oprnd_loc;
524 default:
525 gcc_unreachable ();
529 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
531 enum dw_cfi_oprnd_type
532 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
534 switch (cfi)
536 case DW_CFA_def_cfa:
537 case DW_CFA_def_cfa_sf:
538 case DW_CFA_offset:
539 case DW_CFA_offset_extended_sf:
540 case DW_CFA_offset_extended:
541 return dw_cfi_oprnd_offset;
543 case DW_CFA_register:
544 return dw_cfi_oprnd_reg_num;
546 case DW_CFA_expression:
547 return dw_cfi_oprnd_loc;
549 default:
550 return dw_cfi_oprnd_unused;
554 /* Output one FDE. */
556 static void
557 output_fde (dw_fde_ref fde, bool for_eh, bool second,
558 char *section_start_label, int fde_encoding, char *augmentation,
559 bool any_lsda_needed, int lsda_encoding)
561 const char *begin, *end;
562 static unsigned int j;
563 char l1[20], l2[20];
565 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
566 /* empty */ 0);
567 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
568 for_eh + j);
569 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
570 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
571 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
572 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
573 " indicating 64-bit DWARF extension");
574 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
575 "FDE Length");
576 ASM_OUTPUT_LABEL (asm_out_file, l1);
578 if (for_eh)
579 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
580 else
581 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
582 debug_frame_section, "FDE CIE offset");
584 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
585 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
587 if (for_eh)
589 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
590 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
591 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
592 "FDE initial location");
593 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
594 end, begin, "FDE address range");
596 else
598 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
599 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
602 if (augmentation[0])
604 if (any_lsda_needed)
606 int size = size_of_encoded_value (lsda_encoding);
608 if (lsda_encoding == DW_EH_PE_aligned)
610 int offset = ( 4 /* Length */
611 + 4 /* CIE offset */
612 + 2 * size_of_encoded_value (fde_encoding)
613 + 1 /* Augmentation size */ );
614 int pad = -offset & (PTR_SIZE - 1);
616 size += pad;
617 gcc_assert (size_of_uleb128 (size) == 1);
620 dw2_asm_output_data_uleb128 (size, "Augmentation size");
622 if (fde->uses_eh_lsda)
624 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
625 fde->funcdef_number);
626 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
627 gen_rtx_SYMBOL_REF (Pmode, l1),
628 false,
629 "Language Specific Data Area");
631 else
633 if (lsda_encoding == DW_EH_PE_aligned)
634 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
635 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
636 "Language Specific Data Area (none)");
639 else
640 dw2_asm_output_data_uleb128 (0, "Augmentation size");
643 /* Loop through the Call Frame Instructions associated with this FDE. */
644 fde->dw_fde_current_label = begin;
646 size_t from, until, i;
648 from = 0;
649 until = vec_safe_length (fde->dw_fde_cfi);
651 if (fde->dw_fde_second_begin == NULL)
653 else if (!second)
654 until = fde->dw_fde_switch_cfi_index;
655 else
656 from = fde->dw_fde_switch_cfi_index;
658 for (i = from; i < until; i++)
659 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
662 /* If we are to emit a ref/link from function bodies to their frame tables,
663 do it now. This is typically performed to make sure that tables
664 associated with functions are dragged with them and not discarded in
665 garbage collecting links. We need to do this on a per function basis to
666 cope with -ffunction-sections. */
668 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
669 /* Switch to the function section, emit the ref to the tables, and
670 switch *back* into the table section. */
671 switch_to_section (function_section (fde->decl));
672 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
673 switch_to_frame_table_section (for_eh, true);
674 #endif
676 /* Pad the FDE out to an address sized boundary. */
677 ASM_OUTPUT_ALIGN (asm_out_file,
678 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
679 ASM_OUTPUT_LABEL (asm_out_file, l2);
681 j += 2;
684 /* Return true if frame description entry FDE is needed for EH. */
686 static bool
687 fde_needed_for_eh_p (dw_fde_ref fde)
689 if (flag_asynchronous_unwind_tables)
690 return true;
692 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
693 return true;
695 if (fde->uses_eh_lsda)
696 return true;
698 /* If exceptions are enabled, we have collected nothrow info. */
699 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
700 return false;
702 return true;
705 /* Output the call frame information used to record information
706 that relates to calculating the frame pointer, and records the
707 location of saved registers. */
709 static void
710 output_call_frame_info (int for_eh)
712 unsigned int i;
713 dw_fde_ref fde;
714 dw_cfi_ref cfi;
715 char l1[20], l2[20], section_start_label[20];
716 bool any_lsda_needed = false;
717 char augmentation[6];
718 int augmentation_size;
719 int fde_encoding = DW_EH_PE_absptr;
720 int per_encoding = DW_EH_PE_absptr;
721 int lsda_encoding = DW_EH_PE_absptr;
722 int return_reg;
723 rtx personality = NULL;
724 int dw_cie_version;
726 /* Don't emit a CIE if there won't be any FDEs. */
727 if (!fde_vec)
728 return;
730 /* Nothing to do if the assembler's doing it all. */
731 if (dwarf2out_do_cfi_asm ())
732 return;
734 /* If we don't have any functions we'll want to unwind out of, don't emit
735 any EH unwind information. If we make FDEs linkonce, we may have to
736 emit an empty label for an FDE that wouldn't otherwise be emitted. We
737 want to avoid having an FDE kept around when the function it refers to
738 is discarded. Example where this matters: a primary function template
739 in C++ requires EH information, an explicit specialization doesn't. */
740 if (for_eh)
742 bool any_eh_needed = false;
744 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
746 if (fde->uses_eh_lsda)
747 any_eh_needed = any_lsda_needed = true;
748 else if (fde_needed_for_eh_p (fde))
749 any_eh_needed = true;
750 else if (TARGET_USES_WEAK_UNWIND_INFO)
751 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
754 if (!any_eh_needed)
755 return;
758 /* We're going to be generating comments, so turn on app. */
759 if (flag_debug_asm)
760 app_enable ();
762 /* Switch to the proper frame section, first time. */
763 switch_to_frame_table_section (for_eh, false);
765 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
766 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
768 /* Output the CIE. */
769 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
770 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
771 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
772 dw2_asm_output_data (4, 0xffffffff,
773 "Initial length escape value indicating 64-bit DWARF extension");
774 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
775 "Length of Common Information Entry");
776 ASM_OUTPUT_LABEL (asm_out_file, l1);
778 /* Now that the CIE pointer is PC-relative for EH,
779 use 0 to identify the CIE. */
780 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
781 (for_eh ? 0 : DWARF_CIE_ID),
782 "CIE Identifier Tag");
784 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
785 use CIE version 1, unless that would produce incorrect results
786 due to overflowing the return register column. */
787 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
788 dw_cie_version = 1;
789 if (return_reg >= 256 || dwarf_version > 2)
790 dw_cie_version = 3;
791 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
793 augmentation[0] = 0;
794 augmentation_size = 0;
796 personality = current_unit_personality;
797 if (for_eh)
799 char *p;
801 /* Augmentation:
802 z Indicates that a uleb128 is present to size the
803 augmentation section.
804 L Indicates the encoding (and thus presence) of
805 an LSDA pointer in the FDE augmentation.
806 R Indicates a non-default pointer encoding for
807 FDE code pointers.
808 P Indicates the presence of an encoding + language
809 personality routine in the CIE augmentation. */
811 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
812 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
813 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
815 p = augmentation + 1;
816 if (personality)
818 *p++ = 'P';
819 augmentation_size += 1 + size_of_encoded_value (per_encoding);
820 assemble_external_libcall (personality);
822 if (any_lsda_needed)
824 *p++ = 'L';
825 augmentation_size += 1;
827 if (fde_encoding != DW_EH_PE_absptr)
829 *p++ = 'R';
830 augmentation_size += 1;
832 if (p > augmentation + 1)
834 augmentation[0] = 'z';
835 *p = '\0';
838 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
839 if (personality && per_encoding == DW_EH_PE_aligned)
841 int offset = ( 4 /* Length */
842 + 4 /* CIE Id */
843 + 1 /* CIE version */
844 + strlen (augmentation) + 1 /* Augmentation */
845 + size_of_uleb128 (1) /* Code alignment */
846 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
847 + 1 /* RA column */
848 + 1 /* Augmentation size */
849 + 1 /* Personality encoding */ );
850 int pad = -offset & (PTR_SIZE - 1);
852 augmentation_size += pad;
854 /* Augmentations should be small, so there's scarce need to
855 iterate for a solution. Die if we exceed one uleb128 byte. */
856 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
860 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
861 if (dw_cie_version >= 4)
863 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
864 dw2_asm_output_data (1, 0, "CIE Segment Size");
866 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
867 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
868 "CIE Data Alignment Factor");
870 if (dw_cie_version == 1)
871 dw2_asm_output_data (1, return_reg, "CIE RA Column");
872 else
873 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
875 if (augmentation[0])
877 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
878 if (personality)
880 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
881 eh_data_format_name (per_encoding));
882 dw2_asm_output_encoded_addr_rtx (per_encoding,
883 personality,
884 true, NULL);
887 if (any_lsda_needed)
888 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
889 eh_data_format_name (lsda_encoding));
891 if (fde_encoding != DW_EH_PE_absptr)
892 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
893 eh_data_format_name (fde_encoding));
896 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
897 output_cfi (cfi, NULL, for_eh);
899 /* Pad the CIE out to an address sized boundary. */
900 ASM_OUTPUT_ALIGN (asm_out_file,
901 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
902 ASM_OUTPUT_LABEL (asm_out_file, l2);
904 /* Loop through all of the FDE's. */
905 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
907 unsigned int k;
909 /* Don't emit EH unwind info for leaf functions that don't need it. */
910 if (for_eh && !fde_needed_for_eh_p (fde))
911 continue;
913 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
914 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
915 augmentation, any_lsda_needed, lsda_encoding);
918 if (for_eh && targetm.terminate_dw2_eh_frame_info)
919 dw2_asm_output_data (4, 0, "End of Table");
921 /* Turn off app to make assembly quicker. */
922 if (flag_debug_asm)
923 app_disable ();
926 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
928 static void
929 dwarf2out_do_cfi_startproc (bool second)
931 int enc;
932 rtx ref;
933 rtx personality = get_personality_function (current_function_decl);
935 fprintf (asm_out_file, "\t.cfi_startproc\n");
937 if (personality)
939 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
940 ref = personality;
942 /* ??? The GAS support isn't entirely consistent. We have to
943 handle indirect support ourselves, but PC-relative is done
944 in the assembler. Further, the assembler can't handle any
945 of the weirder relocation types. */
946 if (enc & DW_EH_PE_indirect)
947 ref = dw2_force_const_mem (ref, true);
949 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
950 output_addr_const (asm_out_file, ref);
951 fputc ('\n', asm_out_file);
954 if (crtl->uses_eh_lsda)
956 char lab[20];
958 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
959 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
960 current_function_funcdef_no);
961 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
962 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
964 if (enc & DW_EH_PE_indirect)
965 ref = dw2_force_const_mem (ref, true);
967 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
968 output_addr_const (asm_out_file, ref);
969 fputc ('\n', asm_out_file);
973 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
974 this allocation may be done before pass_final. */
976 dw_fde_ref
977 dwarf2out_alloc_current_fde (void)
979 dw_fde_ref fde;
981 fde = ggc_alloc_cleared_dw_fde_node ();
982 fde->decl = current_function_decl;
983 fde->funcdef_number = current_function_funcdef_no;
984 fde->fde_index = vec_safe_length (fde_vec);
985 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
986 fde->uses_eh_lsda = crtl->uses_eh_lsda;
987 fde->nothrow = crtl->nothrow;
988 fde->drap_reg = INVALID_REGNUM;
989 fde->vdrap_reg = INVALID_REGNUM;
991 /* Record the FDE associated with this function. */
992 cfun->fde = fde;
993 vec_safe_push (fde_vec, fde);
995 return fde;
998 /* Output a marker (i.e. a label) for the beginning of a function, before
999 the prologue. */
1001 void
1002 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1003 const char *file ATTRIBUTE_UNUSED)
1005 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1006 char * dup_label;
1007 dw_fde_ref fde;
1008 section *fnsec;
1009 bool do_frame;
1011 current_function_func_begin_label = NULL;
1013 do_frame = dwarf2out_do_frame ();
1015 /* ??? current_function_func_begin_label is also used by except.c for
1016 call-site information. We must emit this label if it might be used. */
1017 if (!do_frame
1018 && (!flag_exceptions
1019 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1020 return;
1022 fnsec = function_section (current_function_decl);
1023 switch_to_section (fnsec);
1024 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1025 current_function_funcdef_no);
1026 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1027 current_function_funcdef_no);
1028 dup_label = xstrdup (label);
1029 current_function_func_begin_label = dup_label;
1031 /* We can elide the fde allocation if we're not emitting debug info. */
1032 if (!do_frame)
1033 return;
1035 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1036 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1037 would include pass_dwarf2_frame. If we've not created the FDE yet,
1038 do so now. */
1039 fde = cfun->fde;
1040 if (fde == NULL)
1041 fde = dwarf2out_alloc_current_fde ();
1043 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1044 fde->dw_fde_begin = dup_label;
1045 fde->dw_fde_current_label = dup_label;
1046 fde->in_std_section = (fnsec == text_section
1047 || (cold_text_section && fnsec == cold_text_section));
1049 /* We only want to output line number information for the genuine dwarf2
1050 prologue case, not the eh frame case. */
1051 #ifdef DWARF2_DEBUGGING_INFO
1052 if (file)
1053 dwarf2out_source_line (line, file, 0, true);
1054 #endif
1056 if (dwarf2out_do_cfi_asm ())
1057 dwarf2out_do_cfi_startproc (false);
1058 else
1060 rtx personality = get_personality_function (current_function_decl);
1061 if (!current_unit_personality)
1062 current_unit_personality = personality;
1064 /* We cannot keep a current personality per function as without CFI
1065 asm, at the point where we emit the CFI data, there is no current
1066 function anymore. */
1067 if (personality && current_unit_personality != personality)
1068 sorry ("multiple EH personalities are supported only with assemblers "
1069 "supporting .cfi_personality directive");
1073 /* Output a marker (i.e. a label) for the end of the generated code
1074 for a function prologue. This gets called *after* the prologue code has
1075 been generated. */
1077 void
1078 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1079 const char *file ATTRIBUTE_UNUSED)
1081 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1083 /* Output a label to mark the endpoint of the code generated for this
1084 function. */
1085 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1086 current_function_funcdef_no);
1087 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1088 current_function_funcdef_no);
1089 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1092 /* Output a marker (i.e. a label) for the beginning of the generated code
1093 for a function epilogue. This gets called *before* the prologue code has
1094 been generated. */
1096 void
1097 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1098 const char *file ATTRIBUTE_UNUSED)
1100 dw_fde_ref fde = cfun->fde;
1101 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1103 if (fde->dw_fde_vms_begin_epilogue)
1104 return;
1106 /* Output a label to mark the endpoint of the code generated for this
1107 function. */
1108 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1109 current_function_funcdef_no);
1110 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1111 current_function_funcdef_no);
1112 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1115 /* Output a marker (i.e. a label) for the absolute end of the generated code
1116 for a function definition. This gets called *after* the epilogue code has
1117 been generated. */
1119 void
1120 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1121 const char *file ATTRIBUTE_UNUSED)
1123 dw_fde_ref fde;
1124 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1126 last_var_location_insn = NULL_RTX;
1127 cached_next_real_insn = NULL_RTX;
1129 if (dwarf2out_do_cfi_asm ())
1130 fprintf (asm_out_file, "\t.cfi_endproc\n");
1132 /* Output a label to mark the endpoint of the code generated for this
1133 function. */
1134 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1135 current_function_funcdef_no);
1136 ASM_OUTPUT_LABEL (asm_out_file, label);
1137 fde = cfun->fde;
1138 gcc_assert (fde != NULL);
1139 if (fde->dw_fde_second_begin == NULL)
1140 fde->dw_fde_end = xstrdup (label);
1143 void
1144 dwarf2out_frame_finish (void)
1146 /* Output call frame information. */
1147 if (targetm.debug_unwind_info () == UI_DWARF2)
1148 output_call_frame_info (0);
1150 /* Output another copy for the unwinder. */
1151 if ((flag_unwind_tables || flag_exceptions)
1152 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1153 output_call_frame_info (1);
1156 /* Note that the current function section is being used for code. */
1158 static void
1159 dwarf2out_note_section_used (void)
1161 section *sec = current_function_section ();
1162 if (sec == text_section)
1163 text_section_used = true;
1164 else if (sec == cold_text_section)
1165 cold_text_section_used = true;
1168 static void var_location_switch_text_section (void);
1169 static void set_cur_line_info_table (section *);
1171 void
1172 dwarf2out_switch_text_section (void)
1174 section *sect;
1175 dw_fde_ref fde = cfun->fde;
1177 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1179 if (!in_cold_section_p)
1181 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1182 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1183 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1185 else
1187 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1188 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1189 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1191 have_multiple_function_sections = true;
1193 /* There is no need to mark used sections when not debugging. */
1194 if (cold_text_section != NULL)
1195 dwarf2out_note_section_used ();
1197 if (dwarf2out_do_cfi_asm ())
1198 fprintf (asm_out_file, "\t.cfi_endproc\n");
1200 /* Now do the real section switch. */
1201 sect = current_function_section ();
1202 switch_to_section (sect);
1204 fde->second_in_std_section
1205 = (sect == text_section
1206 || (cold_text_section && sect == cold_text_section));
1208 if (dwarf2out_do_cfi_asm ())
1209 dwarf2out_do_cfi_startproc (true);
1211 var_location_switch_text_section ();
1213 if (cold_text_section != NULL)
1214 set_cur_line_info_table (sect);
1217 /* And now, the subset of the debugging information support code necessary
1218 for emitting location expressions. */
1220 /* Data about a single source file. */
1221 struct GTY(()) dwarf_file_data {
1222 const char * filename;
1223 int emitted_number;
1226 typedef struct GTY(()) deferred_locations_struct
1228 tree variable;
1229 dw_die_ref die;
1230 } deferred_locations;
1233 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1236 /* Describe an entry into the .debug_addr section. */
1238 enum ate_kind {
1239 ate_kind_rtx,
1240 ate_kind_rtx_dtprel,
1241 ate_kind_label
1244 typedef struct GTY(()) addr_table_entry_struct {
1245 enum ate_kind kind;
1246 unsigned int refcount;
1247 unsigned int index;
1248 union addr_table_entry_struct_union
1250 rtx GTY ((tag ("0"))) rtl;
1251 char * GTY ((tag ("1"))) label;
1253 GTY ((desc ("%1.kind"))) addr;
1255 addr_table_entry;
1257 /* Location lists are ranges + location descriptions for that range,
1258 so you can track variables that are in different places over
1259 their entire life. */
1260 typedef struct GTY(()) dw_loc_list_struct {
1261 dw_loc_list_ref dw_loc_next;
1262 const char *begin; /* Label and addr_entry for start of range */
1263 addr_table_entry *begin_entry;
1264 const char *end; /* Label for end of range */
1265 char *ll_symbol; /* Label for beginning of location list.
1266 Only on head of list */
1267 const char *section; /* Section this loclist is relative to */
1268 dw_loc_descr_ref expr;
1269 hashval_t hash;
1270 /* True if all addresses in this and subsequent lists are known to be
1271 resolved. */
1272 bool resolved_addr;
1273 /* True if this list has been replaced by dw_loc_next. */
1274 bool replaced;
1275 bool emitted;
1276 /* True if the range should be emitted even if begin and end
1277 are the same. */
1278 bool force;
1279 } dw_loc_list_node;
1281 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1283 /* Convert a DWARF stack opcode into its string name. */
1285 static const char *
1286 dwarf_stack_op_name (unsigned int op)
1288 const char *name = get_DW_OP_name (op);
1290 if (name != NULL)
1291 return name;
1293 return "OP_<unknown>";
1296 /* Return a pointer to a newly allocated location description. Location
1297 descriptions are simple expression terms that can be strung
1298 together to form more complicated location (address) descriptions. */
1300 static inline dw_loc_descr_ref
1301 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1302 unsigned HOST_WIDE_INT oprnd2)
1304 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1306 descr->dw_loc_opc = op;
1307 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1308 descr->dw_loc_oprnd1.val_entry = NULL;
1309 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1310 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1311 descr->dw_loc_oprnd2.val_entry = NULL;
1312 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1314 return descr;
1317 /* Return a pointer to a newly allocated location description for
1318 REG and OFFSET. */
1320 static inline dw_loc_descr_ref
1321 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1323 if (reg <= 31)
1324 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1325 offset, 0);
1326 else
1327 return new_loc_descr (DW_OP_bregx, reg, offset);
1330 /* Add a location description term to a location description expression. */
1332 static inline void
1333 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1335 dw_loc_descr_ref *d;
1337 /* Find the end of the chain. */
1338 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1341 *d = descr;
1344 /* Compare two location operands for exact equality. */
1346 static bool
1347 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1349 if (a->val_class != b->val_class)
1350 return false;
1351 switch (a->val_class)
1353 case dw_val_class_none:
1354 return true;
1355 case dw_val_class_addr:
1356 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1358 case dw_val_class_offset:
1359 case dw_val_class_unsigned_const:
1360 case dw_val_class_const:
1361 case dw_val_class_range_list:
1362 case dw_val_class_lineptr:
1363 case dw_val_class_macptr:
1364 /* These are all HOST_WIDE_INT, signed or unsigned. */
1365 return a->v.val_unsigned == b->v.val_unsigned;
1367 case dw_val_class_loc:
1368 return a->v.val_loc == b->v.val_loc;
1369 case dw_val_class_loc_list:
1370 return a->v.val_loc_list == b->v.val_loc_list;
1371 case dw_val_class_die_ref:
1372 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1373 case dw_val_class_fde_ref:
1374 return a->v.val_fde_index == b->v.val_fde_index;
1375 case dw_val_class_lbl_id:
1376 case dw_val_class_high_pc:
1377 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1378 case dw_val_class_str:
1379 return a->v.val_str == b->v.val_str;
1380 case dw_val_class_flag:
1381 return a->v.val_flag == b->v.val_flag;
1382 case dw_val_class_file:
1383 return a->v.val_file == b->v.val_file;
1384 case dw_val_class_decl_ref:
1385 return a->v.val_decl_ref == b->v.val_decl_ref;
1387 case dw_val_class_const_double:
1388 return (a->v.val_double.high == b->v.val_double.high
1389 && a->v.val_double.low == b->v.val_double.low);
1391 case dw_val_class_vec:
1393 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1394 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1396 return (a_len == b_len
1397 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1400 case dw_val_class_data8:
1401 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1403 case dw_val_class_vms_delta:
1404 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1405 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1407 gcc_unreachable ();
1410 /* Compare two location atoms for exact equality. */
1412 static bool
1413 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1415 if (a->dw_loc_opc != b->dw_loc_opc)
1416 return false;
1418 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1419 address size, but since we always allocate cleared storage it
1420 should be zero for other types of locations. */
1421 if (a->dtprel != b->dtprel)
1422 return false;
1424 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1425 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1428 /* Compare two complete location expressions for exact equality. */
1430 bool
1431 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1433 while (1)
1435 if (a == b)
1436 return true;
1437 if (a == NULL || b == NULL)
1438 return false;
1439 if (!loc_descr_equal_p_1 (a, b))
1440 return false;
1442 a = a->dw_loc_next;
1443 b = b->dw_loc_next;
1448 /* Add a constant OFFSET to a location expression. */
1450 static void
1451 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1453 dw_loc_descr_ref loc;
1454 HOST_WIDE_INT *p;
1456 gcc_assert (*list_head != NULL);
1458 if (!offset)
1459 return;
1461 /* Find the end of the chain. */
1462 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1465 p = NULL;
1466 if (loc->dw_loc_opc == DW_OP_fbreg
1467 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1468 p = &loc->dw_loc_oprnd1.v.val_int;
1469 else if (loc->dw_loc_opc == DW_OP_bregx)
1470 p = &loc->dw_loc_oprnd2.v.val_int;
1472 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1473 offset. Don't optimize if an signed integer overflow would happen. */
1474 if (p != NULL
1475 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1476 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1477 *p += offset;
1479 else if (offset > 0)
1480 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1482 else
1484 loc->dw_loc_next = int_loc_descriptor (-offset);
1485 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1489 /* Add a constant OFFSET to a location list. */
1491 static void
1492 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1494 dw_loc_list_ref d;
1495 for (d = list_head; d != NULL; d = d->dw_loc_next)
1496 loc_descr_plus_const (&d->expr, offset);
1499 #define DWARF_REF_SIZE \
1500 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1502 static unsigned long int get_base_type_offset (dw_die_ref);
1504 /* Return the size of a location descriptor. */
1506 static unsigned long
1507 size_of_loc_descr (dw_loc_descr_ref loc)
1509 unsigned long size = 1;
1511 switch (loc->dw_loc_opc)
1513 case DW_OP_addr:
1514 size += DWARF2_ADDR_SIZE;
1515 break;
1516 case DW_OP_GNU_addr_index:
1517 case DW_OP_GNU_const_index:
1518 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1519 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1520 break;
1521 case DW_OP_const1u:
1522 case DW_OP_const1s:
1523 size += 1;
1524 break;
1525 case DW_OP_const2u:
1526 case DW_OP_const2s:
1527 size += 2;
1528 break;
1529 case DW_OP_const4u:
1530 case DW_OP_const4s:
1531 size += 4;
1532 break;
1533 case DW_OP_const8u:
1534 case DW_OP_const8s:
1535 size += 8;
1536 break;
1537 case DW_OP_constu:
1538 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1539 break;
1540 case DW_OP_consts:
1541 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1542 break;
1543 case DW_OP_pick:
1544 size += 1;
1545 break;
1546 case DW_OP_plus_uconst:
1547 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1548 break;
1549 case DW_OP_skip:
1550 case DW_OP_bra:
1551 size += 2;
1552 break;
1553 case DW_OP_breg0:
1554 case DW_OP_breg1:
1555 case DW_OP_breg2:
1556 case DW_OP_breg3:
1557 case DW_OP_breg4:
1558 case DW_OP_breg5:
1559 case DW_OP_breg6:
1560 case DW_OP_breg7:
1561 case DW_OP_breg8:
1562 case DW_OP_breg9:
1563 case DW_OP_breg10:
1564 case DW_OP_breg11:
1565 case DW_OP_breg12:
1566 case DW_OP_breg13:
1567 case DW_OP_breg14:
1568 case DW_OP_breg15:
1569 case DW_OP_breg16:
1570 case DW_OP_breg17:
1571 case DW_OP_breg18:
1572 case DW_OP_breg19:
1573 case DW_OP_breg20:
1574 case DW_OP_breg21:
1575 case DW_OP_breg22:
1576 case DW_OP_breg23:
1577 case DW_OP_breg24:
1578 case DW_OP_breg25:
1579 case DW_OP_breg26:
1580 case DW_OP_breg27:
1581 case DW_OP_breg28:
1582 case DW_OP_breg29:
1583 case DW_OP_breg30:
1584 case DW_OP_breg31:
1585 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1586 break;
1587 case DW_OP_regx:
1588 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1589 break;
1590 case DW_OP_fbreg:
1591 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1592 break;
1593 case DW_OP_bregx:
1594 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1595 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1596 break;
1597 case DW_OP_piece:
1598 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1599 break;
1600 case DW_OP_bit_piece:
1601 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1602 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1603 break;
1604 case DW_OP_deref_size:
1605 case DW_OP_xderef_size:
1606 size += 1;
1607 break;
1608 case DW_OP_call2:
1609 size += 2;
1610 break;
1611 case DW_OP_call4:
1612 size += 4;
1613 break;
1614 case DW_OP_call_ref:
1615 size += DWARF_REF_SIZE;
1616 break;
1617 case DW_OP_implicit_value:
1618 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1619 + loc->dw_loc_oprnd1.v.val_unsigned;
1620 break;
1621 case DW_OP_GNU_implicit_pointer:
1622 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1623 break;
1624 case DW_OP_GNU_entry_value:
1626 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1627 size += size_of_uleb128 (op_size) + op_size;
1628 break;
1630 case DW_OP_GNU_const_type:
1632 unsigned long o
1633 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1634 size += size_of_uleb128 (o) + 1;
1635 switch (loc->dw_loc_oprnd2.val_class)
1637 case dw_val_class_vec:
1638 size += loc->dw_loc_oprnd2.v.val_vec.length
1639 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1640 break;
1641 case dw_val_class_const:
1642 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1643 break;
1644 case dw_val_class_const_double:
1645 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1646 break;
1647 default:
1648 gcc_unreachable ();
1650 break;
1652 case DW_OP_GNU_regval_type:
1654 unsigned long o
1655 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1656 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1657 + size_of_uleb128 (o);
1659 break;
1660 case DW_OP_GNU_deref_type:
1662 unsigned long o
1663 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1664 size += 1 + size_of_uleb128 (o);
1666 break;
1667 case DW_OP_GNU_convert:
1668 case DW_OP_GNU_reinterpret:
1669 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1670 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1671 else
1673 unsigned long o
1674 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1675 size += size_of_uleb128 (o);
1677 break;
1678 case DW_OP_GNU_parameter_ref:
1679 size += 4;
1680 break;
1681 default:
1682 break;
1685 return size;
1688 /* Return the size of a series of location descriptors. */
1690 unsigned long
1691 size_of_locs (dw_loc_descr_ref loc)
1693 dw_loc_descr_ref l;
1694 unsigned long size;
1696 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1697 field, to avoid writing to a PCH file. */
1698 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1700 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1701 break;
1702 size += size_of_loc_descr (l);
1704 if (! l)
1705 return size;
1707 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1709 l->dw_loc_addr = size;
1710 size += size_of_loc_descr (l);
1713 return size;
1716 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1717 static void get_ref_die_offset_label (char *, dw_die_ref);
1718 static unsigned long int get_ref_die_offset (dw_die_ref);
1720 /* Output location description stack opcode's operands (if any).
1721 The for_eh_or_skip parameter controls whether register numbers are
1722 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1723 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1724 info). This should be suppressed for the cases that have not been converted
1725 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1727 static void
1728 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1730 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1731 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1733 switch (loc->dw_loc_opc)
1735 #ifdef DWARF2_DEBUGGING_INFO
1736 case DW_OP_const2u:
1737 case DW_OP_const2s:
1738 dw2_asm_output_data (2, val1->v.val_int, NULL);
1739 break;
1740 case DW_OP_const4u:
1741 if (loc->dtprel)
1743 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1744 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1745 val1->v.val_addr);
1746 fputc ('\n', asm_out_file);
1747 break;
1749 /* FALLTHRU */
1750 case DW_OP_const4s:
1751 dw2_asm_output_data (4, val1->v.val_int, NULL);
1752 break;
1753 case DW_OP_const8u:
1754 if (loc->dtprel)
1756 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1757 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1758 val1->v.val_addr);
1759 fputc ('\n', asm_out_file);
1760 break;
1762 /* FALLTHRU */
1763 case DW_OP_const8s:
1764 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1765 dw2_asm_output_data (8, val1->v.val_int, NULL);
1766 break;
1767 case DW_OP_skip:
1768 case DW_OP_bra:
1770 int offset;
1772 gcc_assert (val1->val_class == dw_val_class_loc);
1773 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1775 dw2_asm_output_data (2, offset, NULL);
1777 break;
1778 case DW_OP_implicit_value:
1779 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1780 switch (val2->val_class)
1782 case dw_val_class_const:
1783 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1784 break;
1785 case dw_val_class_vec:
1787 unsigned int elt_size = val2->v.val_vec.elt_size;
1788 unsigned int len = val2->v.val_vec.length;
1789 unsigned int i;
1790 unsigned char *p;
1792 if (elt_size > sizeof (HOST_WIDE_INT))
1794 elt_size /= 2;
1795 len *= 2;
1797 for (i = 0, p = val2->v.val_vec.array;
1798 i < len;
1799 i++, p += elt_size)
1800 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1801 "fp or vector constant word %u", i);
1803 break;
1804 case dw_val_class_const_double:
1806 unsigned HOST_WIDE_INT first, second;
1808 if (WORDS_BIG_ENDIAN)
1810 first = val2->v.val_double.high;
1811 second = val2->v.val_double.low;
1813 else
1815 first = val2->v.val_double.low;
1816 second = val2->v.val_double.high;
1818 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1819 first, NULL);
1820 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1821 second, NULL);
1823 break;
1824 case dw_val_class_addr:
1825 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1826 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1827 break;
1828 default:
1829 gcc_unreachable ();
1831 break;
1832 #else
1833 case DW_OP_const2u:
1834 case DW_OP_const2s:
1835 case DW_OP_const4u:
1836 case DW_OP_const4s:
1837 case DW_OP_const8u:
1838 case DW_OP_const8s:
1839 case DW_OP_skip:
1840 case DW_OP_bra:
1841 case DW_OP_implicit_value:
1842 /* We currently don't make any attempt to make sure these are
1843 aligned properly like we do for the main unwind info, so
1844 don't support emitting things larger than a byte if we're
1845 only doing unwinding. */
1846 gcc_unreachable ();
1847 #endif
1848 case DW_OP_const1u:
1849 case DW_OP_const1s:
1850 dw2_asm_output_data (1, val1->v.val_int, NULL);
1851 break;
1852 case DW_OP_constu:
1853 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1854 break;
1855 case DW_OP_consts:
1856 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1857 break;
1858 case DW_OP_pick:
1859 dw2_asm_output_data (1, val1->v.val_int, NULL);
1860 break;
1861 case DW_OP_plus_uconst:
1862 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1863 break;
1864 case DW_OP_breg0:
1865 case DW_OP_breg1:
1866 case DW_OP_breg2:
1867 case DW_OP_breg3:
1868 case DW_OP_breg4:
1869 case DW_OP_breg5:
1870 case DW_OP_breg6:
1871 case DW_OP_breg7:
1872 case DW_OP_breg8:
1873 case DW_OP_breg9:
1874 case DW_OP_breg10:
1875 case DW_OP_breg11:
1876 case DW_OP_breg12:
1877 case DW_OP_breg13:
1878 case DW_OP_breg14:
1879 case DW_OP_breg15:
1880 case DW_OP_breg16:
1881 case DW_OP_breg17:
1882 case DW_OP_breg18:
1883 case DW_OP_breg19:
1884 case DW_OP_breg20:
1885 case DW_OP_breg21:
1886 case DW_OP_breg22:
1887 case DW_OP_breg23:
1888 case DW_OP_breg24:
1889 case DW_OP_breg25:
1890 case DW_OP_breg26:
1891 case DW_OP_breg27:
1892 case DW_OP_breg28:
1893 case DW_OP_breg29:
1894 case DW_OP_breg30:
1895 case DW_OP_breg31:
1896 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1897 break;
1898 case DW_OP_regx:
1900 unsigned r = val1->v.val_unsigned;
1901 if (for_eh_or_skip >= 0)
1902 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1903 gcc_assert (size_of_uleb128 (r)
1904 == size_of_uleb128 (val1->v.val_unsigned));
1905 dw2_asm_output_data_uleb128 (r, NULL);
1907 break;
1908 case DW_OP_fbreg:
1909 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1910 break;
1911 case DW_OP_bregx:
1913 unsigned r = val1->v.val_unsigned;
1914 if (for_eh_or_skip >= 0)
1915 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1916 gcc_assert (size_of_uleb128 (r)
1917 == size_of_uleb128 (val1->v.val_unsigned));
1918 dw2_asm_output_data_uleb128 (r, NULL);
1919 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1921 break;
1922 case DW_OP_piece:
1923 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1924 break;
1925 case DW_OP_bit_piece:
1926 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1927 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1928 break;
1929 case DW_OP_deref_size:
1930 case DW_OP_xderef_size:
1931 dw2_asm_output_data (1, val1->v.val_int, NULL);
1932 break;
1934 case DW_OP_addr:
1935 if (loc->dtprel)
1937 if (targetm.asm_out.output_dwarf_dtprel)
1939 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1940 DWARF2_ADDR_SIZE,
1941 val1->v.val_addr);
1942 fputc ('\n', asm_out_file);
1944 else
1945 gcc_unreachable ();
1947 else
1949 #ifdef DWARF2_DEBUGGING_INFO
1950 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1951 #else
1952 gcc_unreachable ();
1953 #endif
1955 break;
1957 case DW_OP_GNU_addr_index:
1958 case DW_OP_GNU_const_index:
1959 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1960 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1961 "(index into .debug_addr)");
1962 break;
1964 case DW_OP_GNU_implicit_pointer:
1966 char label[MAX_ARTIFICIAL_LABEL_BYTES
1967 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1968 gcc_assert (val1->val_class == dw_val_class_die_ref);
1969 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1970 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1971 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1973 break;
1975 case DW_OP_GNU_entry_value:
1976 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1977 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1978 break;
1980 case DW_OP_GNU_const_type:
1982 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1983 gcc_assert (o);
1984 dw2_asm_output_data_uleb128 (o, NULL);
1985 switch (val2->val_class)
1987 case dw_val_class_const:
1988 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1989 dw2_asm_output_data (1, l, NULL);
1990 dw2_asm_output_data (l, val2->v.val_int, NULL);
1991 break;
1992 case dw_val_class_vec:
1994 unsigned int elt_size = val2->v.val_vec.elt_size;
1995 unsigned int len = val2->v.val_vec.length;
1996 unsigned int i;
1997 unsigned char *p;
1999 l = len * elt_size;
2000 dw2_asm_output_data (1, l, NULL);
2001 if (elt_size > sizeof (HOST_WIDE_INT))
2003 elt_size /= 2;
2004 len *= 2;
2006 for (i = 0, p = val2->v.val_vec.array;
2007 i < len;
2008 i++, p += elt_size)
2009 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2010 "fp or vector constant word %u", i);
2012 break;
2013 case dw_val_class_const_double:
2015 unsigned HOST_WIDE_INT first, second;
2016 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2018 dw2_asm_output_data (1, 2 * l, NULL);
2019 if (WORDS_BIG_ENDIAN)
2021 first = val2->v.val_double.high;
2022 second = val2->v.val_double.low;
2024 else
2026 first = val2->v.val_double.low;
2027 second = val2->v.val_double.high;
2029 dw2_asm_output_data (l, first, NULL);
2030 dw2_asm_output_data (l, second, NULL);
2032 break;
2033 default:
2034 gcc_unreachable ();
2037 break;
2038 case DW_OP_GNU_regval_type:
2040 unsigned r = val1->v.val_unsigned;
2041 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2042 gcc_assert (o);
2043 if (for_eh_or_skip >= 0)
2045 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2046 gcc_assert (size_of_uleb128 (r)
2047 == size_of_uleb128 (val1->v.val_unsigned));
2049 dw2_asm_output_data_uleb128 (r, NULL);
2050 dw2_asm_output_data_uleb128 (o, NULL);
2052 break;
2053 case DW_OP_GNU_deref_type:
2055 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2056 gcc_assert (o);
2057 dw2_asm_output_data (1, val1->v.val_int, NULL);
2058 dw2_asm_output_data_uleb128 (o, NULL);
2060 break;
2061 case DW_OP_GNU_convert:
2062 case DW_OP_GNU_reinterpret:
2063 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2064 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2065 else
2067 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2068 gcc_assert (o);
2069 dw2_asm_output_data_uleb128 (o, NULL);
2071 break;
2073 case DW_OP_GNU_parameter_ref:
2075 unsigned long o;
2076 gcc_assert (val1->val_class == dw_val_class_die_ref);
2077 o = get_ref_die_offset (val1->v.val_die_ref.die);
2078 dw2_asm_output_data (4, o, NULL);
2080 break;
2082 default:
2083 /* Other codes have no operands. */
2084 break;
2088 /* Output a sequence of location operations.
2089 The for_eh_or_skip parameter controls whether register numbers are
2090 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2091 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2092 info). This should be suppressed for the cases that have not been converted
2093 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2095 void
2096 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2098 for (; loc != NULL; loc = loc->dw_loc_next)
2100 enum dwarf_location_atom opc = loc->dw_loc_opc;
2101 /* Output the opcode. */
2102 if (for_eh_or_skip >= 0
2103 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2105 unsigned r = (opc - DW_OP_breg0);
2106 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2107 gcc_assert (r <= 31);
2108 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2110 else if (for_eh_or_skip >= 0
2111 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2113 unsigned r = (opc - DW_OP_reg0);
2114 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2115 gcc_assert (r <= 31);
2116 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2119 dw2_asm_output_data (1, opc,
2120 "%s", dwarf_stack_op_name (opc));
2122 /* Output the operand(s) (if any). */
2123 output_loc_operands (loc, for_eh_or_skip);
2127 /* Output location description stack opcode's operands (if any).
2128 The output is single bytes on a line, suitable for .cfi_escape. */
2130 static void
2131 output_loc_operands_raw (dw_loc_descr_ref loc)
2133 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2134 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2136 switch (loc->dw_loc_opc)
2138 case DW_OP_addr:
2139 case DW_OP_GNU_addr_index:
2140 case DW_OP_GNU_const_index:
2141 case DW_OP_implicit_value:
2142 /* We cannot output addresses in .cfi_escape, only bytes. */
2143 gcc_unreachable ();
2145 case DW_OP_const1u:
2146 case DW_OP_const1s:
2147 case DW_OP_pick:
2148 case DW_OP_deref_size:
2149 case DW_OP_xderef_size:
2150 fputc (',', asm_out_file);
2151 dw2_asm_output_data_raw (1, val1->v.val_int);
2152 break;
2154 case DW_OP_const2u:
2155 case DW_OP_const2s:
2156 fputc (',', asm_out_file);
2157 dw2_asm_output_data_raw (2, val1->v.val_int);
2158 break;
2160 case DW_OP_const4u:
2161 case DW_OP_const4s:
2162 fputc (',', asm_out_file);
2163 dw2_asm_output_data_raw (4, val1->v.val_int);
2164 break;
2166 case DW_OP_const8u:
2167 case DW_OP_const8s:
2168 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2169 fputc (',', asm_out_file);
2170 dw2_asm_output_data_raw (8, val1->v.val_int);
2171 break;
2173 case DW_OP_skip:
2174 case DW_OP_bra:
2176 int offset;
2178 gcc_assert (val1->val_class == dw_val_class_loc);
2179 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2181 fputc (',', asm_out_file);
2182 dw2_asm_output_data_raw (2, offset);
2184 break;
2186 case DW_OP_regx:
2188 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2189 gcc_assert (size_of_uleb128 (r)
2190 == size_of_uleb128 (val1->v.val_unsigned));
2191 fputc (',', asm_out_file);
2192 dw2_asm_output_data_uleb128_raw (r);
2194 break;
2196 case DW_OP_constu:
2197 case DW_OP_plus_uconst:
2198 case DW_OP_piece:
2199 fputc (',', asm_out_file);
2200 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2201 break;
2203 case DW_OP_bit_piece:
2204 fputc (',', asm_out_file);
2205 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2206 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2207 break;
2209 case DW_OP_consts:
2210 case DW_OP_breg0:
2211 case DW_OP_breg1:
2212 case DW_OP_breg2:
2213 case DW_OP_breg3:
2214 case DW_OP_breg4:
2215 case DW_OP_breg5:
2216 case DW_OP_breg6:
2217 case DW_OP_breg7:
2218 case DW_OP_breg8:
2219 case DW_OP_breg9:
2220 case DW_OP_breg10:
2221 case DW_OP_breg11:
2222 case DW_OP_breg12:
2223 case DW_OP_breg13:
2224 case DW_OP_breg14:
2225 case DW_OP_breg15:
2226 case DW_OP_breg16:
2227 case DW_OP_breg17:
2228 case DW_OP_breg18:
2229 case DW_OP_breg19:
2230 case DW_OP_breg20:
2231 case DW_OP_breg21:
2232 case DW_OP_breg22:
2233 case DW_OP_breg23:
2234 case DW_OP_breg24:
2235 case DW_OP_breg25:
2236 case DW_OP_breg26:
2237 case DW_OP_breg27:
2238 case DW_OP_breg28:
2239 case DW_OP_breg29:
2240 case DW_OP_breg30:
2241 case DW_OP_breg31:
2242 case DW_OP_fbreg:
2243 fputc (',', asm_out_file);
2244 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2245 break;
2247 case DW_OP_bregx:
2249 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2250 gcc_assert (size_of_uleb128 (r)
2251 == size_of_uleb128 (val1->v.val_unsigned));
2252 fputc (',', asm_out_file);
2253 dw2_asm_output_data_uleb128_raw (r);
2254 fputc (',', asm_out_file);
2255 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2257 break;
2259 case DW_OP_GNU_implicit_pointer:
2260 case DW_OP_GNU_entry_value:
2261 case DW_OP_GNU_const_type:
2262 case DW_OP_GNU_regval_type:
2263 case DW_OP_GNU_deref_type:
2264 case DW_OP_GNU_convert:
2265 case DW_OP_GNU_reinterpret:
2266 case DW_OP_GNU_parameter_ref:
2267 gcc_unreachable ();
2268 break;
2270 default:
2271 /* Other codes have no operands. */
2272 break;
2276 void
2277 output_loc_sequence_raw (dw_loc_descr_ref loc)
2279 while (1)
2281 enum dwarf_location_atom opc = loc->dw_loc_opc;
2282 /* Output the opcode. */
2283 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2285 unsigned r = (opc - DW_OP_breg0);
2286 r = DWARF2_FRAME_REG_OUT (r, 1);
2287 gcc_assert (r <= 31);
2288 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2290 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2292 unsigned r = (opc - DW_OP_reg0);
2293 r = DWARF2_FRAME_REG_OUT (r, 1);
2294 gcc_assert (r <= 31);
2295 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2297 /* Output the opcode. */
2298 fprintf (asm_out_file, "%#x", opc);
2299 output_loc_operands_raw (loc);
2301 if (!loc->dw_loc_next)
2302 break;
2303 loc = loc->dw_loc_next;
2305 fputc (',', asm_out_file);
2309 /* This function builds a dwarf location descriptor sequence from a
2310 dw_cfa_location, adding the given OFFSET to the result of the
2311 expression. */
2313 struct dw_loc_descr_struct *
2314 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2316 struct dw_loc_descr_struct *head, *tmp;
2318 offset += cfa->offset;
2320 if (cfa->indirect)
2322 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2323 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2324 head->dw_loc_oprnd1.val_entry = NULL;
2325 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2326 add_loc_descr (&head, tmp);
2327 if (offset != 0)
2329 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2330 add_loc_descr (&head, tmp);
2333 else
2334 head = new_reg_loc_descr (cfa->reg, offset);
2336 return head;
2339 /* This function builds a dwarf location descriptor sequence for
2340 the address at OFFSET from the CFA when stack is aligned to
2341 ALIGNMENT byte. */
2343 struct dw_loc_descr_struct *
2344 build_cfa_aligned_loc (dw_cfa_location *cfa,
2345 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2347 struct dw_loc_descr_struct *head;
2348 unsigned int dwarf_fp
2349 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2351 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2352 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2354 head = new_reg_loc_descr (dwarf_fp, 0);
2355 add_loc_descr (&head, int_loc_descriptor (alignment));
2356 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2357 loc_descr_plus_const (&head, offset);
2359 else
2360 head = new_reg_loc_descr (dwarf_fp, offset);
2361 return head;
2364 /* And now, the support for symbolic debugging information. */
2366 /* .debug_str support. */
2367 static int output_indirect_string (void **, void *);
2369 static void dwarf2out_init (const char *);
2370 static void dwarf2out_finish (const char *);
2371 static void dwarf2out_assembly_start (void);
2372 static void dwarf2out_define (unsigned int, const char *);
2373 static void dwarf2out_undef (unsigned int, const char *);
2374 static void dwarf2out_start_source_file (unsigned, const char *);
2375 static void dwarf2out_end_source_file (unsigned);
2376 static void dwarf2out_function_decl (tree);
2377 static void dwarf2out_begin_block (unsigned, unsigned);
2378 static void dwarf2out_end_block (unsigned, unsigned);
2379 static bool dwarf2out_ignore_block (const_tree);
2380 static void dwarf2out_global_decl (tree);
2381 static void dwarf2out_type_decl (tree, int);
2382 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2383 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2384 dw_die_ref);
2385 static void dwarf2out_abstract_function (tree);
2386 static void dwarf2out_var_location (rtx);
2387 static void dwarf2out_begin_function (tree);
2388 static void dwarf2out_end_function (unsigned int);
2389 static void dwarf2out_set_name (tree, tree);
2391 /* The debug hooks structure. */
2393 const struct gcc_debug_hooks dwarf2_debug_hooks =
2395 dwarf2out_init,
2396 dwarf2out_finish,
2397 dwarf2out_assembly_start,
2398 dwarf2out_define,
2399 dwarf2out_undef,
2400 dwarf2out_start_source_file,
2401 dwarf2out_end_source_file,
2402 dwarf2out_begin_block,
2403 dwarf2out_end_block,
2404 dwarf2out_ignore_block,
2405 dwarf2out_source_line,
2406 dwarf2out_begin_prologue,
2407 #if VMS_DEBUGGING_INFO
2408 dwarf2out_vms_end_prologue,
2409 dwarf2out_vms_begin_epilogue,
2410 #else
2411 debug_nothing_int_charstar,
2412 debug_nothing_int_charstar,
2413 #endif
2414 dwarf2out_end_epilogue,
2415 dwarf2out_begin_function,
2416 dwarf2out_end_function, /* end_function */
2417 dwarf2out_function_decl, /* function_decl */
2418 dwarf2out_global_decl,
2419 dwarf2out_type_decl, /* type_decl */
2420 dwarf2out_imported_module_or_decl,
2421 debug_nothing_tree, /* deferred_inline_function */
2422 /* The DWARF 2 backend tries to reduce debugging bloat by not
2423 emitting the abstract description of inline functions until
2424 something tries to reference them. */
2425 dwarf2out_abstract_function, /* outlining_inline_function */
2426 debug_nothing_rtx, /* label */
2427 debug_nothing_int, /* handle_pch */
2428 dwarf2out_var_location,
2429 dwarf2out_switch_text_section,
2430 dwarf2out_set_name,
2431 1, /* start_end_main_source_file */
2432 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2435 /* NOTE: In the comments in this file, many references are made to
2436 "Debugging Information Entries". This term is abbreviated as `DIE'
2437 throughout the remainder of this file. */
2439 /* An internal representation of the DWARF output is built, and then
2440 walked to generate the DWARF debugging info. The walk of the internal
2441 representation is done after the entire program has been compiled.
2442 The types below are used to describe the internal representation. */
2444 /* Whether to put type DIEs into their own section .debug_types instead
2445 of making them part of the .debug_info section. Only supported for
2446 Dwarf V4 or higher and the user didn't disable them through
2447 -fno-debug-types-section. It is more efficient to put them in a
2448 separate comdat sections since the linker will then be able to
2449 remove duplicates. But not all tools support .debug_types sections
2450 yet. */
2452 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2454 /* Various DIE's use offsets relative to the beginning of the
2455 .debug_info section to refer to each other. */
2457 typedef long int dw_offset;
2459 /* Define typedefs here to avoid circular dependencies. */
2461 typedef struct dw_attr_struct *dw_attr_ref;
2462 typedef struct dw_line_info_struct *dw_line_info_ref;
2463 typedef struct pubname_struct *pubname_ref;
2464 typedef struct dw_ranges_struct *dw_ranges_ref;
2465 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2466 typedef struct comdat_type_struct *comdat_type_node_ref;
2468 /* The entries in the line_info table more-or-less mirror the opcodes
2469 that are used in the real dwarf line table. Arrays of these entries
2470 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2471 supported. */
2473 enum dw_line_info_opcode {
2474 /* Emit DW_LNE_set_address; the operand is the label index. */
2475 LI_set_address,
2477 /* Emit a row to the matrix with the given line. This may be done
2478 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2479 special opcodes. */
2480 LI_set_line,
2482 /* Emit a DW_LNS_set_file. */
2483 LI_set_file,
2485 /* Emit a DW_LNS_set_column. */
2486 LI_set_column,
2488 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2489 LI_negate_stmt,
2491 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2492 LI_set_prologue_end,
2493 LI_set_epilogue_begin,
2495 /* Emit a DW_LNE_set_discriminator. */
2496 LI_set_discriminator
2499 typedef struct GTY(()) dw_line_info_struct {
2500 enum dw_line_info_opcode opcode;
2501 unsigned int val;
2502 } dw_line_info_entry;
2505 typedef struct GTY(()) dw_line_info_table_struct {
2506 /* The label that marks the end of this section. */
2507 const char *end_label;
2509 /* The values for the last row of the matrix, as collected in the table.
2510 These are used to minimize the changes to the next row. */
2511 unsigned int file_num;
2512 unsigned int line_num;
2513 unsigned int column_num;
2514 int discrim_num;
2515 bool is_stmt;
2516 bool in_use;
2518 vec<dw_line_info_entry, va_gc> *entries;
2519 } dw_line_info_table;
2521 typedef dw_line_info_table *dw_line_info_table_p;
2524 /* Each DIE attribute has a field specifying the attribute kind,
2525 a link to the next attribute in the chain, and an attribute value.
2526 Attributes are typically linked below the DIE they modify. */
2528 typedef struct GTY(()) dw_attr_struct {
2529 enum dwarf_attribute dw_attr;
2530 dw_val_node dw_attr_val;
2532 dw_attr_node;
2535 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2536 The children of each node form a circular list linked by
2537 die_sib. die_child points to the node *before* the "first" child node. */
2539 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2540 union die_symbol_or_type_node
2542 const char * GTY ((tag ("0"))) die_symbol;
2543 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2545 GTY ((desc ("%0.comdat_type_p"))) die_id;
2546 vec<dw_attr_node, va_gc> *die_attr;
2547 dw_die_ref die_parent;
2548 dw_die_ref die_child;
2549 dw_die_ref die_sib;
2550 dw_die_ref die_definition; /* ref from a specification to its definition */
2551 dw_offset die_offset;
2552 unsigned long die_abbrev;
2553 int die_mark;
2554 unsigned int decl_id;
2555 enum dwarf_tag die_tag;
2556 /* Die is used and must not be pruned as unused. */
2557 BOOL_BITFIELD die_perennial_p : 1;
2558 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2559 /* Lots of spare bits. */
2561 die_node;
2563 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2564 #define FOR_EACH_CHILD(die, c, expr) do { \
2565 c = die->die_child; \
2566 if (c) do { \
2567 c = c->die_sib; \
2568 expr; \
2569 } while (c != die->die_child); \
2570 } while (0)
2572 /* The pubname structure */
2574 typedef struct GTY(()) pubname_struct {
2575 dw_die_ref die;
2576 const char *name;
2578 pubname_entry;
2581 struct GTY(()) dw_ranges_struct {
2582 /* If this is positive, it's a block number, otherwise it's a
2583 bitwise-negated index into dw_ranges_by_label. */
2584 int num;
2587 /* A structure to hold a macinfo entry. */
2589 typedef struct GTY(()) macinfo_struct {
2590 unsigned char code;
2591 unsigned HOST_WIDE_INT lineno;
2592 const char *info;
2594 macinfo_entry;
2597 struct GTY(()) dw_ranges_by_label_struct {
2598 const char *begin;
2599 const char *end;
2602 /* The comdat type node structure. */
2603 typedef struct GTY(()) comdat_type_struct
2605 dw_die_ref root_die;
2606 dw_die_ref type_die;
2607 dw_die_ref skeleton_die;
2608 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2609 struct comdat_type_struct *next;
2611 comdat_type_node;
2613 /* The limbo die list structure. */
2614 typedef struct GTY(()) limbo_die_struct {
2615 dw_die_ref die;
2616 tree created_for;
2617 struct limbo_die_struct *next;
2619 limbo_die_node;
2621 typedef struct skeleton_chain_struct
2623 dw_die_ref old_die;
2624 dw_die_ref new_die;
2625 struct skeleton_chain_struct *parent;
2627 skeleton_chain_node;
2629 /* Define a macro which returns nonzero for a TYPE_DECL which was
2630 implicitly generated for a type.
2632 Note that, unlike the C front-end (which generates a NULL named
2633 TYPE_DECL node for each complete tagged type, each array type,
2634 and each function type node created) the C++ front-end generates
2635 a _named_ TYPE_DECL node for each tagged type node created.
2636 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2637 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2638 front-end, but for each type, tagged or not. */
2640 #define TYPE_DECL_IS_STUB(decl) \
2641 (DECL_NAME (decl) == NULL_TREE \
2642 || (DECL_ARTIFICIAL (decl) \
2643 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2644 /* This is necessary for stub decls that \
2645 appear in nested inline functions. */ \
2646 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2647 && (decl_ultimate_origin (decl) \
2648 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2650 /* Information concerning the compilation unit's programming
2651 language, and compiler version. */
2653 /* Fixed size portion of the DWARF compilation unit header. */
2654 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2655 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2657 /* Fixed size portion of the DWARF comdat type unit header. */
2658 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2659 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2660 + DWARF_OFFSET_SIZE)
2662 /* Fixed size portion of public names info. */
2663 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2665 /* Fixed size portion of the address range info. */
2666 #define DWARF_ARANGES_HEADER_SIZE \
2667 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2668 DWARF2_ADDR_SIZE * 2) \
2669 - DWARF_INITIAL_LENGTH_SIZE)
2671 /* Size of padding portion in the address range info. It must be
2672 aligned to twice the pointer size. */
2673 #define DWARF_ARANGES_PAD_SIZE \
2674 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2675 DWARF2_ADDR_SIZE * 2) \
2676 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2678 /* Use assembler line directives if available. */
2679 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2680 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2681 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2682 #else
2683 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2684 #endif
2685 #endif
2687 /* Minimum line offset in a special line info. opcode.
2688 This value was chosen to give a reasonable range of values. */
2689 #define DWARF_LINE_BASE -10
2691 /* First special line opcode - leave room for the standard opcodes. */
2692 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2694 /* Range of line offsets in a special line info. opcode. */
2695 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2697 /* Flag that indicates the initial value of the is_stmt_start flag.
2698 In the present implementation, we do not mark any lines as
2699 the beginning of a source statement, because that information
2700 is not made available by the GCC front-end. */
2701 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2703 /* Maximum number of operations per instruction bundle. */
2704 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2705 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2706 #endif
2708 /* This location is used by calc_die_sizes() to keep track
2709 the offset of each DIE within the .debug_info section. */
2710 static unsigned long next_die_offset;
2712 /* Record the root of the DIE's built for the current compilation unit. */
2713 static GTY(()) dw_die_ref single_comp_unit_die;
2715 /* A list of type DIEs that have been separated into comdat sections. */
2716 static GTY(()) comdat_type_node *comdat_type_list;
2718 /* A list of DIEs with a NULL parent waiting to be relocated. */
2719 static GTY(()) limbo_die_node *limbo_die_list;
2721 /* A list of DIEs for which we may have to generate
2722 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2723 static GTY(()) limbo_die_node *deferred_asm_name;
2725 /* Filenames referenced by this compilation unit. */
2726 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2728 /* A hash table of references to DIE's that describe declarations.
2729 The key is a DECL_UID() which is a unique number identifying each decl. */
2730 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2732 /* A hash table of references to DIE's that describe COMMON blocks.
2733 The key is DECL_UID() ^ die_parent. */
2734 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2736 typedef struct GTY(()) die_arg_entry_struct {
2737 dw_die_ref die;
2738 tree arg;
2739 } die_arg_entry;
2742 /* Node of the variable location list. */
2743 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2744 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2745 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2746 in mode of the EXPR_LIST node and first EXPR_LIST operand
2747 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2748 location or NULL for padding. For larger bitsizes,
2749 mode is 0 and first operand is a CONCAT with bitsize
2750 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2751 NULL as second operand. */
2752 rtx GTY (()) loc;
2753 const char * GTY (()) label;
2754 struct var_loc_node * GTY (()) next;
2757 /* Variable location list. */
2758 struct GTY (()) var_loc_list_def {
2759 struct var_loc_node * GTY (()) first;
2761 /* Pointer to the last but one or last element of the
2762 chained list. If the list is empty, both first and
2763 last are NULL, if the list contains just one node
2764 or the last node certainly is not redundant, it points
2765 to the last node, otherwise points to the last but one.
2766 Do not mark it for GC because it is marked through the chain. */
2767 struct var_loc_node * GTY ((skip ("%h"))) last;
2769 /* Pointer to the last element before section switch,
2770 if NULL, either sections weren't switched or first
2771 is after section switch. */
2772 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2774 /* DECL_UID of the variable decl. */
2775 unsigned int decl_id;
2777 typedef struct var_loc_list_def var_loc_list;
2779 /* Call argument location list. */
2780 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2781 rtx GTY (()) call_arg_loc_note;
2782 const char * GTY (()) label;
2783 tree GTY (()) block;
2784 bool tail_call_p;
2785 rtx GTY (()) symbol_ref;
2786 struct call_arg_loc_node * GTY (()) next;
2790 /* Table of decl location linked lists. */
2791 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2793 /* Head and tail of call_arg_loc chain. */
2794 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2795 static struct call_arg_loc_node *call_arg_loc_last;
2797 /* Number of call sites in the current function. */
2798 static int call_site_count = -1;
2799 /* Number of tail call sites in the current function. */
2800 static int tail_call_site_count = -1;
2802 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2803 DIEs. */
2804 static vec<dw_die_ref> block_map;
2806 /* A cached location list. */
2807 struct GTY (()) cached_dw_loc_list_def {
2808 /* The DECL_UID of the decl that this entry describes. */
2809 unsigned int decl_id;
2811 /* The cached location list. */
2812 dw_loc_list_ref loc_list;
2814 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2816 /* Table of cached location lists. */
2817 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2819 /* A pointer to the base of a list of references to DIE's that
2820 are uniquely identified by their tag, presence/absence of
2821 children DIE's, and list of attribute/value pairs. */
2822 static GTY((length ("abbrev_die_table_allocated")))
2823 dw_die_ref *abbrev_die_table;
2825 /* Number of elements currently allocated for abbrev_die_table. */
2826 static GTY(()) unsigned abbrev_die_table_allocated;
2828 /* Number of elements in type_die_table currently in use. */
2829 static GTY(()) unsigned abbrev_die_table_in_use;
2831 /* Size (in elements) of increments by which we may expand the
2832 abbrev_die_table. */
2833 #define ABBREV_DIE_TABLE_INCREMENT 256
2835 /* A global counter for generating labels for line number data. */
2836 static unsigned int line_info_label_num;
2838 /* The current table to which we should emit line number information
2839 for the current function. This will be set up at the beginning of
2840 assembly for the function. */
2841 static dw_line_info_table *cur_line_info_table;
2843 /* The two default tables of line number info. */
2844 static GTY(()) dw_line_info_table *text_section_line_info;
2845 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2847 /* The set of all non-default tables of line number info. */
2848 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2850 /* A flag to tell pubnames/types export if there is an info section to
2851 refer to. */
2852 static bool info_section_emitted;
2854 /* A pointer to the base of a table that contains a list of publicly
2855 accessible names. */
2856 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2858 /* A pointer to the base of a table that contains a list of publicly
2859 accessible types. */
2860 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2862 /* A pointer to the base of a table that contains a list of macro
2863 defines/undefines (and file start/end markers). */
2864 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2866 /* True if .debug_macinfo or .debug_macros section is going to be
2867 emitted. */
2868 #define have_macinfo \
2869 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2870 && !macinfo_table->is_empty ())
2872 /* Array of dies for which we should generate .debug_ranges info. */
2873 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2875 /* Number of elements currently allocated for ranges_table. */
2876 static GTY(()) unsigned ranges_table_allocated;
2878 /* Number of elements in ranges_table currently in use. */
2879 static GTY(()) unsigned ranges_table_in_use;
2881 /* Array of pairs of labels referenced in ranges_table. */
2882 static GTY ((length ("ranges_by_label_allocated")))
2883 dw_ranges_by_label_ref ranges_by_label;
2885 /* Number of elements currently allocated for ranges_by_label. */
2886 static GTY(()) unsigned ranges_by_label_allocated;
2888 /* Number of elements in ranges_by_label currently in use. */
2889 static GTY(()) unsigned ranges_by_label_in_use;
2891 /* Size (in elements) of increments by which we may expand the
2892 ranges_table. */
2893 #define RANGES_TABLE_INCREMENT 64
2895 /* Whether we have location lists that need outputting */
2896 static GTY(()) bool have_location_lists;
2898 /* Unique label counter. */
2899 static GTY(()) unsigned int loclabel_num;
2901 /* Unique label counter for point-of-call tables. */
2902 static GTY(()) unsigned int poc_label_num;
2904 /* Record whether the function being analyzed contains inlined functions. */
2905 static int current_function_has_inlines;
2907 /* The last file entry emitted by maybe_emit_file(). */
2908 static GTY(()) struct dwarf_file_data * last_emitted_file;
2910 /* Number of internal labels generated by gen_internal_sym(). */
2911 static GTY(()) int label_num;
2913 /* Cached result of previous call to lookup_filename. */
2914 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2916 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2918 /* Instances of generic types for which we need to generate debug
2919 info that describe their generic parameters and arguments. That
2920 generation needs to happen once all types are properly laid out so
2921 we do it at the end of compilation. */
2922 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2924 /* Offset from the "steady-state frame pointer" to the frame base,
2925 within the current function. */
2926 static HOST_WIDE_INT frame_pointer_fb_offset;
2927 static bool frame_pointer_fb_offset_valid;
2929 static vec<dw_die_ref> base_types;
2931 /* Forward declarations for functions defined in this file. */
2933 static int is_pseudo_reg (const_rtx);
2934 static tree type_main_variant (tree);
2935 static int is_tagged_type (const_tree);
2936 static const char *dwarf_tag_name (unsigned);
2937 static const char *dwarf_attr_name (unsigned);
2938 static const char *dwarf_form_name (unsigned);
2939 static tree decl_ultimate_origin (const_tree);
2940 static tree decl_class_context (tree);
2941 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2942 static inline enum dw_val_class AT_class (dw_attr_ref);
2943 static inline unsigned int AT_index (dw_attr_ref);
2944 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2945 static inline unsigned AT_flag (dw_attr_ref);
2946 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2947 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2948 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2949 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2950 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2951 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2952 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2953 unsigned int, unsigned char *);
2954 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2955 static hashval_t debug_str_do_hash (const void *);
2956 static int debug_str_eq (const void *, const void *);
2957 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2958 static inline const char *AT_string (dw_attr_ref);
2959 static enum dwarf_form AT_string_form (dw_attr_ref);
2960 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2961 static void add_AT_specification (dw_die_ref, dw_die_ref);
2962 static inline dw_die_ref AT_ref (dw_attr_ref);
2963 static inline int AT_ref_external (dw_attr_ref);
2964 static inline void set_AT_ref_external (dw_attr_ref, int);
2965 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2966 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2967 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2968 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2969 dw_loc_list_ref);
2970 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2971 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2972 static void remove_addr_table_entry (addr_table_entry *);
2973 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2974 static inline rtx AT_addr (dw_attr_ref);
2975 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2976 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2977 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2978 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2979 unsigned HOST_WIDE_INT);
2980 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2981 unsigned long, bool);
2982 static inline const char *AT_lbl (dw_attr_ref);
2983 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2984 static const char *get_AT_low_pc (dw_die_ref);
2985 static const char *get_AT_hi_pc (dw_die_ref);
2986 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2987 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2988 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2989 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2990 static bool is_cxx (void);
2991 static bool is_fortran (void);
2992 static bool is_ada (void);
2993 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2994 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2995 static void add_child_die (dw_die_ref, dw_die_ref);
2996 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2997 static dw_die_ref lookup_type_die (tree);
2998 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2999 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3000 static void equate_type_number_to_die (tree, dw_die_ref);
3001 static hashval_t decl_die_table_hash (const void *);
3002 static int decl_die_table_eq (const void *, const void *);
3003 static dw_die_ref lookup_decl_die (tree);
3004 static hashval_t common_block_die_table_hash (const void *);
3005 static int common_block_die_table_eq (const void *, const void *);
3006 static hashval_t decl_loc_table_hash (const void *);
3007 static int decl_loc_table_eq (const void *, const void *);
3008 static var_loc_list *lookup_decl_loc (const_tree);
3009 static void equate_decl_number_to_die (tree, dw_die_ref);
3010 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3011 static void print_spaces (FILE *);
3012 static void print_die (dw_die_ref, FILE *);
3013 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3014 static dw_die_ref pop_compile_unit (dw_die_ref);
3015 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3016 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3017 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3018 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3019 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3020 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3021 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3022 struct md5_ctx *, int *);
3023 struct checksum_attributes;
3024 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3025 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3026 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3027 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3028 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3029 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3030 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3031 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3032 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3033 static void compute_section_prefix (dw_die_ref);
3034 static int is_type_die (dw_die_ref);
3035 static int is_comdat_die (dw_die_ref);
3036 static int is_symbol_die (dw_die_ref);
3037 static inline bool is_template_instantiation (dw_die_ref);
3038 static void assign_symbol_names (dw_die_ref);
3039 static void break_out_includes (dw_die_ref);
3040 static int is_declaration_die (dw_die_ref);
3041 static int should_move_die_to_comdat (dw_die_ref);
3042 static dw_die_ref clone_as_declaration (dw_die_ref);
3043 static dw_die_ref clone_die (dw_die_ref);
3044 static dw_die_ref clone_tree (dw_die_ref);
3045 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3046 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3047 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3048 static dw_die_ref generate_skeleton (dw_die_ref);
3049 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3050 dw_die_ref,
3051 dw_die_ref);
3052 static void break_out_comdat_types (dw_die_ref);
3053 static void copy_decls_for_unworthy_types (dw_die_ref);
3055 static void add_sibling_attributes (dw_die_ref);
3056 static void output_location_lists (dw_die_ref);
3057 static int constant_size (unsigned HOST_WIDE_INT);
3058 static unsigned long size_of_die (dw_die_ref);
3059 static void calc_die_sizes (dw_die_ref);
3060 static void calc_base_type_die_sizes (void);
3061 static void mark_dies (dw_die_ref);
3062 static void unmark_dies (dw_die_ref);
3063 static void unmark_all_dies (dw_die_ref);
3064 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3065 static unsigned long size_of_aranges (void);
3066 static enum dwarf_form value_format (dw_attr_ref);
3067 static void output_value_format (dw_attr_ref);
3068 static void output_abbrev_section (void);
3069 static void output_die_abbrevs (unsigned long, dw_die_ref);
3070 static void output_die_symbol (dw_die_ref);
3071 static void output_die (dw_die_ref);
3072 static void output_compilation_unit_header (void);
3073 static void output_comp_unit (dw_die_ref, int);
3074 static void output_comdat_type_unit (comdat_type_node *);
3075 static const char *dwarf2_name (tree, int);
3076 static void add_pubname (tree, dw_die_ref);
3077 static void add_enumerator_pubname (const char *, dw_die_ref);
3078 static void add_pubname_string (const char *, dw_die_ref);
3079 static void add_pubtype (tree, dw_die_ref);
3080 static void output_pubnames (vec<pubname_entry, va_gc> *);
3081 static void output_aranges (unsigned long);
3082 static unsigned int add_ranges_num (int);
3083 static unsigned int add_ranges (const_tree);
3084 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3085 bool *, bool);
3086 static void output_ranges (void);
3087 static dw_line_info_table *new_line_info_table (void);
3088 static void output_line_info (bool);
3089 static void output_file_names (void);
3090 static dw_die_ref base_type_die (tree);
3091 static int is_base_type (tree);
3092 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3093 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3094 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3095 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3096 static int type_is_enum (const_tree);
3097 static unsigned int dbx_reg_number (const_rtx);
3098 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3099 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3100 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3101 enum var_init_status);
3102 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3103 enum var_init_status);
3104 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3105 enum var_init_status);
3106 static int is_based_loc (const_rtx);
3107 static int resolve_one_addr (rtx *, void *);
3108 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3109 enum var_init_status);
3110 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3111 enum var_init_status);
3112 static dw_loc_list_ref loc_list_from_tree (tree, int);
3113 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3114 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3115 static tree field_type (const_tree);
3116 static unsigned int simple_type_align_in_bits (const_tree);
3117 static unsigned int simple_decl_align_in_bits (const_tree);
3118 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3119 static HOST_WIDE_INT field_byte_offset (const_tree);
3120 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3121 dw_loc_list_ref);
3122 static void add_data_member_location_attribute (dw_die_ref, tree);
3123 static bool add_const_value_attribute (dw_die_ref, rtx);
3124 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3125 static void insert_double (double_int, unsigned char *);
3126 static void insert_float (const_rtx, unsigned char *);
3127 static rtx rtl_for_decl_location (tree);
3128 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3129 enum dwarf_attribute);
3130 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3131 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3132 static void add_name_attribute (dw_die_ref, const char *);
3133 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3134 static void add_comp_dir_attribute (dw_die_ref);
3135 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3136 static void add_subscript_info (dw_die_ref, tree, bool);
3137 static void add_byte_size_attribute (dw_die_ref, tree);
3138 static void add_bit_offset_attribute (dw_die_ref, tree);
3139 static void add_bit_size_attribute (dw_die_ref, tree);
3140 static void add_prototyped_attribute (dw_die_ref, tree);
3141 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3142 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3143 static void add_src_coords_attributes (dw_die_ref, tree);
3144 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3145 static void push_decl_scope (tree);
3146 static void pop_decl_scope (void);
3147 static dw_die_ref scope_die_for (tree, dw_die_ref);
3148 static inline int local_scope_p (dw_die_ref);
3149 static inline int class_scope_p (dw_die_ref);
3150 static inline int class_or_namespace_scope_p (dw_die_ref);
3151 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3152 static void add_calling_convention_attribute (dw_die_ref, tree);
3153 static const char *type_tag (const_tree);
3154 static tree member_declared_type (const_tree);
3155 #if 0
3156 static const char *decl_start_label (tree);
3157 #endif
3158 static void gen_array_type_die (tree, dw_die_ref);
3159 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3160 #if 0
3161 static void gen_entry_point_die (tree, dw_die_ref);
3162 #endif
3163 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3164 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3165 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3166 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3167 static void gen_formal_types_die (tree, dw_die_ref);
3168 static void gen_subprogram_die (tree, dw_die_ref);
3169 static void gen_variable_die (tree, tree, dw_die_ref);
3170 static void gen_const_die (tree, dw_die_ref);
3171 static void gen_label_die (tree, dw_die_ref);
3172 static void gen_lexical_block_die (tree, dw_die_ref, int);
3173 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3174 static void gen_field_die (tree, dw_die_ref);
3175 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3176 static dw_die_ref gen_compile_unit_die (const char *);
3177 static void gen_inheritance_die (tree, tree, dw_die_ref);
3178 static void gen_member_die (tree, dw_die_ref);
3179 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3180 enum debug_info_usage);
3181 static void gen_subroutine_type_die (tree, dw_die_ref);
3182 static void gen_typedef_die (tree, dw_die_ref);
3183 static void gen_type_die (tree, dw_die_ref);
3184 static void gen_block_die (tree, dw_die_ref, int);
3185 static void decls_for_scope (tree, dw_die_ref, int);
3186 static inline int is_redundant_typedef (const_tree);
3187 static bool is_naming_typedef_decl (const_tree);
3188 static inline dw_die_ref get_context_die (tree);
3189 static void gen_namespace_die (tree, dw_die_ref);
3190 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3191 static dw_die_ref force_decl_die (tree);
3192 static dw_die_ref force_type_die (tree);
3193 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3194 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3195 static struct dwarf_file_data * lookup_filename (const char *);
3196 static void retry_incomplete_types (void);
3197 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3198 static void gen_generic_params_dies (tree);
3199 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3200 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3201 static void splice_child_die (dw_die_ref, dw_die_ref);
3202 static int file_info_cmp (const void *, const void *);
3203 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3204 const char *, const char *);
3205 static void output_loc_list (dw_loc_list_ref);
3206 static char *gen_internal_sym (const char *);
3207 static bool want_pubnames (void);
3209 static void prune_unmark_dies (dw_die_ref);
3210 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3211 static void prune_unused_types_mark (dw_die_ref, int);
3212 static void prune_unused_types_walk (dw_die_ref);
3213 static void prune_unused_types_walk_attribs (dw_die_ref);
3214 static void prune_unused_types_prune (dw_die_ref);
3215 static void prune_unused_types (void);
3216 static int maybe_emit_file (struct dwarf_file_data *fd);
3217 static inline const char *AT_vms_delta1 (dw_attr_ref);
3218 static inline const char *AT_vms_delta2 (dw_attr_ref);
3219 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3220 const char *, const char *);
3221 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3222 static void gen_remaining_tmpl_value_param_die_attribute (void);
3223 static bool generic_type_p (tree);
3224 static void schedule_generic_params_dies_gen (tree t);
3225 static void gen_scheduled_generic_parms_dies (void);
3227 static const char *comp_dir_string (void);
3229 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3231 /* enum for tracking thread-local variables whose address is really an offset
3232 relative to the TLS pointer, which will need link-time relocation, but will
3233 not need relocation by the DWARF consumer. */
3235 enum dtprel_bool
3237 dtprel_false = 0,
3238 dtprel_true = 1
3241 /* Return the operator to use for an address of a variable. For dtprel_true, we
3242 use DW_OP_const*. For regular variables, which need both link-time
3243 relocation and consumer-level relocation (e.g., to account for shared objects
3244 loaded at a random address), we use DW_OP_addr*. */
3246 static inline enum dwarf_location_atom
3247 dw_addr_op (enum dtprel_bool dtprel)
3249 if (dtprel == dtprel_true)
3250 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3251 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3252 else
3253 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3256 /* Return a pointer to a newly allocated address location description. If
3257 dwarf_split_debug_info is true, then record the address with the appropriate
3258 relocation. */
3259 static inline dw_loc_descr_ref
3260 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3262 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3264 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3265 ref->dw_loc_oprnd1.v.val_addr = addr;
3266 ref->dtprel = dtprel;
3267 if (dwarf_split_debug_info)
3268 ref->dw_loc_oprnd1.val_entry
3269 = add_addr_table_entry (addr,
3270 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3271 else
3272 ref->dw_loc_oprnd1.val_entry = NULL;
3274 return ref;
3277 /* Section names used to hold DWARF debugging information. */
3279 #ifndef DEBUG_INFO_SECTION
3280 #define DEBUG_INFO_SECTION ".debug_info"
3281 #endif
3282 #ifndef DEBUG_DWO_INFO_SECTION
3283 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3284 #endif
3285 #ifndef DEBUG_ABBREV_SECTION
3286 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3287 #endif
3288 #ifndef DEBUG_DWO_ABBREV_SECTION
3289 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3290 #endif
3291 #ifndef DEBUG_ARANGES_SECTION
3292 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3293 #endif
3294 #ifndef DEBUG_ADDR_SECTION
3295 #define DEBUG_ADDR_SECTION ".debug_addr"
3296 #endif
3297 #ifndef DEBUG_NORM_MACINFO_SECTION
3298 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3299 #endif
3300 #ifndef DEBUG_DWO_MACINFO_SECTION
3301 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3302 #endif
3303 #ifndef DEBUG_MACINFO_SECTION
3304 #define DEBUG_MACINFO_SECTION \
3305 (!dwarf_split_debug_info \
3306 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3307 #endif
3308 #ifndef DEBUG_NORM_MACRO_SECTION
3309 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3310 #endif
3311 #ifndef DEBUG_DWO_MACRO_SECTION
3312 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3313 #endif
3314 #ifndef DEBUG_MACRO_SECTION
3315 #define DEBUG_MACRO_SECTION \
3316 (!dwarf_split_debug_info \
3317 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3318 #endif
3319 #ifndef DEBUG_LINE_SECTION
3320 #define DEBUG_LINE_SECTION ".debug_line"
3321 #endif
3322 #ifndef DEBUG_DWO_LINE_SECTION
3323 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3324 #endif
3325 #ifndef DEBUG_LOC_SECTION
3326 #define DEBUG_LOC_SECTION ".debug_loc"
3327 #endif
3328 #ifndef DEBUG_DWO_LOC_SECTION
3329 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3330 #endif
3331 #ifndef DEBUG_PUBNAMES_SECTION
3332 #define DEBUG_PUBNAMES_SECTION \
3333 ((debug_generate_pub_sections == 2) \
3334 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3335 #endif
3336 #ifndef DEBUG_PUBTYPES_SECTION
3337 #define DEBUG_PUBTYPES_SECTION \
3338 ((debug_generate_pub_sections == 2) \
3339 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3340 #endif
3341 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3342 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3343 #ifndef DEBUG_STR_OFFSETS_SECTION
3344 #define DEBUG_STR_OFFSETS_SECTION \
3345 (!dwarf_split_debug_info \
3346 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3347 #endif
3348 #ifndef DEBUG_STR_DWO_SECTION
3349 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3350 #endif
3351 #ifndef DEBUG_STR_SECTION
3352 #define DEBUG_STR_SECTION ".debug_str"
3353 #endif
3354 #ifndef DEBUG_RANGES_SECTION
3355 #define DEBUG_RANGES_SECTION ".debug_ranges"
3356 #endif
3358 /* Standard ELF section names for compiled code and data. */
3359 #ifndef TEXT_SECTION_NAME
3360 #define TEXT_SECTION_NAME ".text"
3361 #endif
3363 /* Section flags for .debug_macinfo/.debug_macro section. */
3364 #define DEBUG_MACRO_SECTION_FLAGS \
3365 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3367 /* Section flags for .debug_str section. */
3368 #define DEBUG_STR_SECTION_FLAGS \
3369 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3370 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3371 : SECTION_DEBUG)
3373 /* Section flags for .debug_str.dwo section. */
3374 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3376 /* Labels we insert at beginning sections we can reference instead of
3377 the section names themselves. */
3379 #ifndef TEXT_SECTION_LABEL
3380 #define TEXT_SECTION_LABEL "Ltext"
3381 #endif
3382 #ifndef COLD_TEXT_SECTION_LABEL
3383 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3384 #endif
3385 #ifndef DEBUG_LINE_SECTION_LABEL
3386 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3387 #endif
3388 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3389 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3390 #endif
3391 #ifndef DEBUG_INFO_SECTION_LABEL
3392 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3393 #endif
3394 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3395 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3396 #endif
3397 #ifndef DEBUG_ABBREV_SECTION_LABEL
3398 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3399 #endif
3400 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3401 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3402 #endif
3403 #ifndef DEBUG_ADDR_SECTION_LABEL
3404 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3405 #endif
3406 #ifndef DEBUG_LOC_SECTION_LABEL
3407 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3408 #endif
3409 #ifndef DEBUG_RANGES_SECTION_LABEL
3410 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3411 #endif
3412 #ifndef DEBUG_MACINFO_SECTION_LABEL
3413 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3414 #endif
3415 #ifndef DEBUG_MACRO_SECTION_LABEL
3416 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3417 #endif
3418 #define SKELETON_COMP_DIE_ABBREV 1
3419 #define SKELETON_TYPE_DIE_ABBREV 2
3421 /* Definitions of defaults for formats and names of various special
3422 (artificial) labels which may be generated within this file (when the -g
3423 options is used and DWARF2_DEBUGGING_INFO is in effect.
3424 If necessary, these may be overridden from within the tm.h file, but
3425 typically, overriding these defaults is unnecessary. */
3427 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3428 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3429 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3430 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3431 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3432 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3433 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3435 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3436 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3437 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3438 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3439 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3440 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3442 #ifndef TEXT_END_LABEL
3443 #define TEXT_END_LABEL "Letext"
3444 #endif
3445 #ifndef COLD_END_LABEL
3446 #define COLD_END_LABEL "Letext_cold"
3447 #endif
3448 #ifndef BLOCK_BEGIN_LABEL
3449 #define BLOCK_BEGIN_LABEL "LBB"
3450 #endif
3451 #ifndef BLOCK_END_LABEL
3452 #define BLOCK_END_LABEL "LBE"
3453 #endif
3454 #ifndef LINE_CODE_LABEL
3455 #define LINE_CODE_LABEL "LM"
3456 #endif
3459 /* Return the root of the DIE's built for the current compilation unit. */
3460 static dw_die_ref
3461 comp_unit_die (void)
3463 if (!single_comp_unit_die)
3464 single_comp_unit_die = gen_compile_unit_die (NULL);
3465 return single_comp_unit_die;
3468 /* We allow a language front-end to designate a function that is to be
3469 called to "demangle" any name before it is put into a DIE. */
3471 static const char *(*demangle_name_func) (const char *);
3473 void
3474 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3476 demangle_name_func = func;
3479 /* Test if rtl node points to a pseudo register. */
3481 static inline int
3482 is_pseudo_reg (const_rtx rtl)
3484 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3485 || (GET_CODE (rtl) == SUBREG
3486 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3489 /* Return a reference to a type, with its const and volatile qualifiers
3490 removed. */
3492 static inline tree
3493 type_main_variant (tree type)
3495 type = TYPE_MAIN_VARIANT (type);
3497 /* ??? There really should be only one main variant among any group of
3498 variants of a given type (and all of the MAIN_VARIANT values for all
3499 members of the group should point to that one type) but sometimes the C
3500 front-end messes this up for array types, so we work around that bug
3501 here. */
3502 if (TREE_CODE (type) == ARRAY_TYPE)
3503 while (type != TYPE_MAIN_VARIANT (type))
3504 type = TYPE_MAIN_VARIANT (type);
3506 return type;
3509 /* Return nonzero if the given type node represents a tagged type. */
3511 static inline int
3512 is_tagged_type (const_tree type)
3514 enum tree_code code = TREE_CODE (type);
3516 return (code == RECORD_TYPE || code == UNION_TYPE
3517 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3520 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3522 static void
3523 get_ref_die_offset_label (char *label, dw_die_ref ref)
3525 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3528 /* Return die_offset of a DIE reference to a base type. */
3530 static unsigned long int
3531 get_base_type_offset (dw_die_ref ref)
3533 if (ref->die_offset)
3534 return ref->die_offset;
3535 if (comp_unit_die ()->die_abbrev)
3537 calc_base_type_die_sizes ();
3538 gcc_assert (ref->die_offset);
3540 return ref->die_offset;
3543 /* Return die_offset of a DIE reference other than base type. */
3545 static unsigned long int
3546 get_ref_die_offset (dw_die_ref ref)
3548 gcc_assert (ref->die_offset);
3549 return ref->die_offset;
3552 /* Convert a DIE tag into its string name. */
3554 static const char *
3555 dwarf_tag_name (unsigned int tag)
3557 const char *name = get_DW_TAG_name (tag);
3559 if (name != NULL)
3560 return name;
3562 return "DW_TAG_<unknown>";
3565 /* Convert a DWARF attribute code into its string name. */
3567 static const char *
3568 dwarf_attr_name (unsigned int attr)
3570 const char *name;
3572 switch (attr)
3574 #if VMS_DEBUGGING_INFO
3575 case DW_AT_HP_prologue:
3576 return "DW_AT_HP_prologue";
3577 #else
3578 case DW_AT_MIPS_loop_unroll_factor:
3579 return "DW_AT_MIPS_loop_unroll_factor";
3580 #endif
3582 #if VMS_DEBUGGING_INFO
3583 case DW_AT_HP_epilogue:
3584 return "DW_AT_HP_epilogue";
3585 #else
3586 case DW_AT_MIPS_stride:
3587 return "DW_AT_MIPS_stride";
3588 #endif
3591 name = get_DW_AT_name (attr);
3593 if (name != NULL)
3594 return name;
3596 return "DW_AT_<unknown>";
3599 /* Convert a DWARF value form code into its string name. */
3601 static const char *
3602 dwarf_form_name (unsigned int form)
3604 const char *name = get_DW_FORM_name (form);
3606 if (name != NULL)
3607 return name;
3609 return "DW_FORM_<unknown>";
3612 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3613 instance of an inlined instance of a decl which is local to an inline
3614 function, so we have to trace all of the way back through the origin chain
3615 to find out what sort of node actually served as the original seed for the
3616 given block. */
3618 static tree
3619 decl_ultimate_origin (const_tree decl)
3621 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3622 return NULL_TREE;
3624 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3625 nodes in the function to point to themselves; ignore that if
3626 we're trying to output the abstract instance of this function. */
3627 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3628 return NULL_TREE;
3630 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3631 most distant ancestor, this should never happen. */
3632 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3634 return DECL_ABSTRACT_ORIGIN (decl);
3637 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3638 of a virtual function may refer to a base class, so we check the 'this'
3639 parameter. */
3641 static tree
3642 decl_class_context (tree decl)
3644 tree context = NULL_TREE;
3646 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3647 context = DECL_CONTEXT (decl);
3648 else
3649 context = TYPE_MAIN_VARIANT
3650 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3652 if (context && !TYPE_P (context))
3653 context = NULL_TREE;
3655 return context;
3658 /* Add an attribute/value pair to a DIE. */
3660 static inline void
3661 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3663 /* Maybe this should be an assert? */
3664 if (die == NULL)
3665 return;
3667 vec_safe_reserve (die->die_attr, 1);
3668 vec_safe_push (die->die_attr, *attr);
3671 static inline enum dw_val_class
3672 AT_class (dw_attr_ref a)
3674 return a->dw_attr_val.val_class;
3677 /* Return the index for any attribute that will be referenced with a
3678 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3679 are stored in dw_attr_val.v.val_str for reference counting
3680 pruning. */
3682 static inline unsigned int
3683 AT_index (dw_attr_ref a)
3685 if (AT_class (a) == dw_val_class_str)
3686 return a->dw_attr_val.v.val_str->index;
3687 else if (a->dw_attr_val.val_entry != NULL)
3688 return a->dw_attr_val.val_entry->index;
3689 return NOT_INDEXED;
3692 /* Add a flag value attribute to a DIE. */
3694 static inline void
3695 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3697 dw_attr_node attr;
3699 attr.dw_attr = attr_kind;
3700 attr.dw_attr_val.val_class = dw_val_class_flag;
3701 attr.dw_attr_val.val_entry = NULL;
3702 attr.dw_attr_val.v.val_flag = flag;
3703 add_dwarf_attr (die, &attr);
3706 static inline unsigned
3707 AT_flag (dw_attr_ref a)
3709 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3710 return a->dw_attr_val.v.val_flag;
3713 /* Add a signed integer attribute value to a DIE. */
3715 static inline void
3716 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3718 dw_attr_node attr;
3720 attr.dw_attr = attr_kind;
3721 attr.dw_attr_val.val_class = dw_val_class_const;
3722 attr.dw_attr_val.val_entry = NULL;
3723 attr.dw_attr_val.v.val_int = int_val;
3724 add_dwarf_attr (die, &attr);
3727 static inline HOST_WIDE_INT
3728 AT_int (dw_attr_ref a)
3730 gcc_assert (a && AT_class (a) == dw_val_class_const);
3731 return a->dw_attr_val.v.val_int;
3734 /* Add an unsigned integer attribute value to a DIE. */
3736 static inline void
3737 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3738 unsigned HOST_WIDE_INT unsigned_val)
3740 dw_attr_node attr;
3742 attr.dw_attr = attr_kind;
3743 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3744 attr.dw_attr_val.val_entry = NULL;
3745 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3746 add_dwarf_attr (die, &attr);
3749 static inline unsigned HOST_WIDE_INT
3750 AT_unsigned (dw_attr_ref a)
3752 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3753 return a->dw_attr_val.v.val_unsigned;
3756 /* Add an unsigned double integer attribute value to a DIE. */
3758 static inline void
3759 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3760 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3762 dw_attr_node attr;
3764 attr.dw_attr = attr_kind;
3765 attr.dw_attr_val.val_class = dw_val_class_const_double;
3766 attr.dw_attr_val.val_entry = NULL;
3767 attr.dw_attr_val.v.val_double.high = high;
3768 attr.dw_attr_val.v.val_double.low = low;
3769 add_dwarf_attr (die, &attr);
3772 /* Add a floating point attribute value to a DIE and return it. */
3774 static inline void
3775 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3776 unsigned int length, unsigned int elt_size, unsigned char *array)
3778 dw_attr_node attr;
3780 attr.dw_attr = attr_kind;
3781 attr.dw_attr_val.val_class = dw_val_class_vec;
3782 attr.dw_attr_val.val_entry = NULL;
3783 attr.dw_attr_val.v.val_vec.length = length;
3784 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3785 attr.dw_attr_val.v.val_vec.array = array;
3786 add_dwarf_attr (die, &attr);
3789 /* Add an 8-byte data attribute value to a DIE. */
3791 static inline void
3792 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3793 unsigned char data8[8])
3795 dw_attr_node attr;
3797 attr.dw_attr = attr_kind;
3798 attr.dw_attr_val.val_class = dw_val_class_data8;
3799 attr.dw_attr_val.val_entry = NULL;
3800 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3801 add_dwarf_attr (die, &attr);
3804 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3805 dwarf_split_debug_info, address attributes in dies destined for the
3806 final executable have force_direct set to avoid using indexed
3807 references. */
3809 static inline void
3810 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3811 bool force_direct)
3813 dw_attr_node attr;
3814 char * lbl_id;
3816 lbl_id = xstrdup (lbl_low);
3817 attr.dw_attr = DW_AT_low_pc;
3818 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3819 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3820 if (dwarf_split_debug_info && !force_direct)
3821 attr.dw_attr_val.val_entry
3822 = add_addr_table_entry (lbl_id, ate_kind_label);
3823 else
3824 attr.dw_attr_val.val_entry = NULL;
3825 add_dwarf_attr (die, &attr);
3827 attr.dw_attr = DW_AT_high_pc;
3828 if (dwarf_version < 4)
3829 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3830 else
3831 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3832 lbl_id = xstrdup (lbl_high);
3833 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3834 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3835 && dwarf_split_debug_info && !force_direct)
3836 attr.dw_attr_val.val_entry
3837 = add_addr_table_entry (lbl_id, ate_kind_label);
3838 else
3839 attr.dw_attr_val.val_entry = NULL;
3840 add_dwarf_attr (die, &attr);
3843 /* Hash and equality functions for debug_str_hash. */
3845 static hashval_t
3846 debug_str_do_hash (const void *x)
3848 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3851 static int
3852 debug_str_eq (const void *x1, const void *x2)
3854 return strcmp ((((const struct indirect_string_node *)x1)->str),
3855 (const char *)x2) == 0;
3858 /* Add STR to the given string hash table. */
3860 static struct indirect_string_node *
3861 find_AT_string_in_table (const char *str, htab_t table)
3863 struct indirect_string_node *node;
3864 void **slot;
3866 slot = htab_find_slot_with_hash (table, str,
3867 htab_hash_string (str), INSERT);
3868 if (*slot == NULL)
3870 node = ggc_alloc_cleared_indirect_string_node ();
3871 node->str = ggc_strdup (str);
3872 *slot = node;
3874 else
3875 node = (struct indirect_string_node *) *slot;
3877 node->refcount++;
3878 return node;
3881 /* Add STR to the indirect string hash table. */
3883 static struct indirect_string_node *
3884 find_AT_string (const char *str)
3886 if (! debug_str_hash)
3887 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3888 debug_str_eq, NULL);
3890 return find_AT_string_in_table (str, debug_str_hash);
3893 /* Add a string attribute value to a DIE. */
3895 static inline void
3896 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3898 dw_attr_node attr;
3899 struct indirect_string_node *node;
3901 node = find_AT_string (str);
3903 attr.dw_attr = attr_kind;
3904 attr.dw_attr_val.val_class = dw_val_class_str;
3905 attr.dw_attr_val.val_entry = NULL;
3906 attr.dw_attr_val.v.val_str = node;
3907 add_dwarf_attr (die, &attr);
3910 static inline const char *
3911 AT_string (dw_attr_ref a)
3913 gcc_assert (a && AT_class (a) == dw_val_class_str);
3914 return a->dw_attr_val.v.val_str->str;
3917 /* Call this function directly to bypass AT_string_form's logic to put
3918 the string inline in the die. */
3920 static void
3921 set_indirect_string (struct indirect_string_node *node)
3923 char label[32];
3924 /* Already indirect is a no op. */
3925 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3927 gcc_assert (node->label);
3928 return;
3930 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3931 ++dw2_string_counter;
3932 node->label = xstrdup (label);
3934 if (!dwarf_split_debug_info)
3936 node->form = DW_FORM_strp;
3937 node->index = NOT_INDEXED;
3939 else
3941 node->form = DW_FORM_GNU_str_index;
3942 node->index = NO_INDEX_ASSIGNED;
3946 /* Find out whether a string should be output inline in DIE
3947 or out-of-line in .debug_str section. */
3949 static enum dwarf_form
3950 find_string_form (struct indirect_string_node *node)
3952 unsigned int len;
3954 if (node->form)
3955 return node->form;
3957 len = strlen (node->str) + 1;
3959 /* If the string is shorter or equal to the size of the reference, it is
3960 always better to put it inline. */
3961 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3962 return node->form = DW_FORM_string;
3964 /* If we cannot expect the linker to merge strings in .debug_str
3965 section, only put it into .debug_str if it is worth even in this
3966 single module. */
3967 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3968 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3969 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3970 return node->form = DW_FORM_string;
3972 set_indirect_string (node);
3974 return node->form;
3977 /* Find out whether the string referenced from the attribute should be
3978 output inline in DIE or out-of-line in .debug_str section. */
3980 static enum dwarf_form
3981 AT_string_form (dw_attr_ref a)
3983 gcc_assert (a && AT_class (a) == dw_val_class_str);
3984 return find_string_form (a->dw_attr_val.v.val_str);
3987 /* Add a DIE reference attribute value to a DIE. */
3989 static inline void
3990 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3992 dw_attr_node attr;
3994 #ifdef ENABLE_CHECKING
3995 gcc_assert (targ_die != NULL);
3996 #else
3997 /* With LTO we can end up trying to reference something we didn't create
3998 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3999 if (targ_die == NULL)
4000 return;
4001 #endif
4003 attr.dw_attr = attr_kind;
4004 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4005 attr.dw_attr_val.val_entry = NULL;
4006 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4007 attr.dw_attr_val.v.val_die_ref.external = 0;
4008 add_dwarf_attr (die, &attr);
4011 /* Change DIE reference REF to point to NEW_DIE instead. */
4013 static inline void
4014 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4016 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4017 ref->dw_attr_val.v.val_die_ref.die = new_die;
4018 ref->dw_attr_val.v.val_die_ref.external = 0;
4021 /* Add an AT_specification attribute to a DIE, and also make the back
4022 pointer from the specification to the definition. */
4024 static inline void
4025 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4027 add_AT_die_ref (die, DW_AT_specification, targ_die);
4028 gcc_assert (!targ_die->die_definition);
4029 targ_die->die_definition = die;
4032 static inline dw_die_ref
4033 AT_ref (dw_attr_ref a)
4035 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4036 return a->dw_attr_val.v.val_die_ref.die;
4039 static inline int
4040 AT_ref_external (dw_attr_ref a)
4042 if (a && AT_class (a) == dw_val_class_die_ref)
4043 return a->dw_attr_val.v.val_die_ref.external;
4045 return 0;
4048 static inline void
4049 set_AT_ref_external (dw_attr_ref a, int i)
4051 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4052 a->dw_attr_val.v.val_die_ref.external = i;
4055 /* Add an FDE reference attribute value to a DIE. */
4057 static inline void
4058 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4060 dw_attr_node attr;
4062 attr.dw_attr = attr_kind;
4063 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4064 attr.dw_attr_val.val_entry = NULL;
4065 attr.dw_attr_val.v.val_fde_index = targ_fde;
4066 add_dwarf_attr (die, &attr);
4069 /* Add a location description attribute value to a DIE. */
4071 static inline void
4072 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4074 dw_attr_node attr;
4076 attr.dw_attr = attr_kind;
4077 attr.dw_attr_val.val_class = dw_val_class_loc;
4078 attr.dw_attr_val.val_entry = NULL;
4079 attr.dw_attr_val.v.val_loc = loc;
4080 add_dwarf_attr (die, &attr);
4083 static inline dw_loc_descr_ref
4084 AT_loc (dw_attr_ref a)
4086 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4087 return a->dw_attr_val.v.val_loc;
4090 static inline void
4091 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4093 dw_attr_node attr;
4095 attr.dw_attr = attr_kind;
4096 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4097 attr.dw_attr_val.val_entry = NULL;
4098 attr.dw_attr_val.v.val_loc_list = loc_list;
4099 add_dwarf_attr (die, &attr);
4100 have_location_lists = true;
4103 static inline dw_loc_list_ref
4104 AT_loc_list (dw_attr_ref a)
4106 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4107 return a->dw_attr_val.v.val_loc_list;
4110 static inline dw_loc_list_ref *
4111 AT_loc_list_ptr (dw_attr_ref a)
4113 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4114 return &a->dw_attr_val.v.val_loc_list;
4117 /* Table of entries into the .debug_addr section. */
4119 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4121 /* Hash an address_table_entry. */
4123 static hashval_t
4124 addr_table_entry_do_hash (const void *x)
4126 const addr_table_entry *a = (const addr_table_entry *) x;
4127 switch (a->kind)
4129 case ate_kind_rtx:
4130 return iterative_hash_rtx (a->addr.rtl, 0);
4131 case ate_kind_rtx_dtprel:
4132 return iterative_hash_rtx (a->addr.rtl, 1);
4133 case ate_kind_label:
4134 return htab_hash_string (a->addr.label);
4135 default:
4136 gcc_unreachable ();
4140 /* Determine equality for two address_table_entries. */
4142 static int
4143 addr_table_entry_eq (const void *x1, const void *x2)
4145 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4146 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4148 if (a1->kind != a2->kind)
4149 return 0;
4150 switch (a1->kind)
4152 case ate_kind_rtx:
4153 case ate_kind_rtx_dtprel:
4154 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4155 case ate_kind_label:
4156 return strcmp (a1->addr.label, a2->addr.label) == 0;
4157 default:
4158 gcc_unreachable ();
4162 /* Initialize an addr_table_entry. */
4164 void
4165 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4167 e->kind = kind;
4168 switch (kind)
4170 case ate_kind_rtx:
4171 case ate_kind_rtx_dtprel:
4172 e->addr.rtl = (rtx) addr;
4173 break;
4174 case ate_kind_label:
4175 e->addr.label = (char *) addr;
4176 break;
4178 e->refcount = 0;
4179 e->index = NO_INDEX_ASSIGNED;
4182 /* Add attr to the address table entry to the table. Defer setting an
4183 index until output time. */
4185 static addr_table_entry *
4186 add_addr_table_entry (void *addr, enum ate_kind kind)
4188 addr_table_entry *node;
4189 addr_table_entry finder;
4190 void **slot;
4192 gcc_assert (dwarf_split_debug_info);
4193 if (! addr_index_table)
4194 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4195 addr_table_entry_eq, NULL);
4196 init_addr_table_entry (&finder, kind, addr);
4197 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4199 if (*slot == HTAB_EMPTY_ENTRY)
4201 node = ggc_alloc_cleared_addr_table_entry ();
4202 init_addr_table_entry (node, kind, addr);
4203 *slot = node;
4205 else
4206 node = (addr_table_entry *) *slot;
4208 node->refcount++;
4209 return node;
4212 /* Remove an entry from the addr table by decrementing its refcount.
4213 Strictly, decrementing the refcount would be enough, but the
4214 assertion that the entry is actually in the table has found
4215 bugs. */
4217 static void
4218 remove_addr_table_entry (addr_table_entry *entry)
4220 addr_table_entry *node;
4222 gcc_assert (dwarf_split_debug_info && addr_index_table);
4223 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4224 /* After an index is assigned, the table is frozen. */
4225 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4226 node->refcount--;
4229 /* Given a location list, remove all addresses it refers to from the
4230 address_table. */
4232 static void
4233 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4235 for (; descr; descr = descr->dw_loc_next)
4236 if (descr->dw_loc_oprnd1.val_entry != NULL)
4238 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4239 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4243 /* A helper function for dwarf2out_finish called through
4244 htab_traverse. Assign an addr_table_entry its index. All entries
4245 must be collected into the table when this function is called,
4246 because the indexing code relies on htab_traverse to traverse nodes
4247 in the same order for each run. */
4249 static int
4250 index_addr_table_entry (void **h, void *v)
4252 addr_table_entry *node = (addr_table_entry *) *h;
4253 unsigned int *index = (unsigned int *) v;
4255 /* Don't index unreferenced nodes. */
4256 if (node->refcount == 0)
4257 return 1;
4259 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4260 node->index = *index;
4261 *index += 1;
4263 return 1;
4266 /* Add an address constant attribute value to a DIE. When using
4267 dwarf_split_debug_info, address attributes in dies destined for the
4268 final executable should be direct references--setting the parameter
4269 force_direct ensures this behavior. */
4271 static inline void
4272 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4273 bool force_direct)
4275 dw_attr_node attr;
4277 attr.dw_attr = attr_kind;
4278 attr.dw_attr_val.val_class = dw_val_class_addr;
4279 attr.dw_attr_val.v.val_addr = addr;
4280 if (dwarf_split_debug_info && !force_direct)
4281 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4282 else
4283 attr.dw_attr_val.val_entry = NULL;
4284 add_dwarf_attr (die, &attr);
4287 /* Get the RTX from to an address DIE attribute. */
4289 static inline rtx
4290 AT_addr (dw_attr_ref a)
4292 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4293 return a->dw_attr_val.v.val_addr;
4296 /* Add a file attribute value to a DIE. */
4298 static inline void
4299 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4300 struct dwarf_file_data *fd)
4302 dw_attr_node attr;
4304 attr.dw_attr = attr_kind;
4305 attr.dw_attr_val.val_class = dw_val_class_file;
4306 attr.dw_attr_val.val_entry = NULL;
4307 attr.dw_attr_val.v.val_file = fd;
4308 add_dwarf_attr (die, &attr);
4311 /* Get the dwarf_file_data from a file DIE attribute. */
4313 static inline struct dwarf_file_data *
4314 AT_file (dw_attr_ref a)
4316 gcc_assert (a && AT_class (a) == dw_val_class_file);
4317 return a->dw_attr_val.v.val_file;
4320 /* Add a vms delta attribute value to a DIE. */
4322 static inline void
4323 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4324 const char *lbl1, const char *lbl2)
4326 dw_attr_node attr;
4328 attr.dw_attr = attr_kind;
4329 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4330 attr.dw_attr_val.val_entry = NULL;
4331 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4332 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4333 add_dwarf_attr (die, &attr);
4336 /* Add a label identifier attribute value to a DIE. */
4338 static inline void
4339 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4340 const char *lbl_id)
4342 dw_attr_node attr;
4344 attr.dw_attr = attr_kind;
4345 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4346 attr.dw_attr_val.val_entry = NULL;
4347 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4348 if (dwarf_split_debug_info)
4349 attr.dw_attr_val.val_entry
4350 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4351 ate_kind_label);
4352 add_dwarf_attr (die, &attr);
4355 /* Add a section offset attribute value to a DIE, an offset into the
4356 debug_line section. */
4358 static inline void
4359 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4360 const char *label)
4362 dw_attr_node attr;
4364 attr.dw_attr = attr_kind;
4365 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4366 attr.dw_attr_val.val_entry = NULL;
4367 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4368 add_dwarf_attr (die, &attr);
4371 /* Add a section offset attribute value to a DIE, an offset into the
4372 debug_macinfo section. */
4374 static inline void
4375 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4376 const char *label)
4378 dw_attr_node attr;
4380 attr.dw_attr = attr_kind;
4381 attr.dw_attr_val.val_class = dw_val_class_macptr;
4382 attr.dw_attr_val.val_entry = NULL;
4383 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4384 add_dwarf_attr (die, &attr);
4387 /* Add an offset attribute value to a DIE. */
4389 static inline void
4390 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4391 unsigned HOST_WIDE_INT offset)
4393 dw_attr_node attr;
4395 attr.dw_attr = attr_kind;
4396 attr.dw_attr_val.val_class = dw_val_class_offset;
4397 attr.dw_attr_val.val_entry = NULL;
4398 attr.dw_attr_val.v.val_offset = offset;
4399 add_dwarf_attr (die, &attr);
4402 /* Add a range_list attribute value to a DIE. When using
4403 dwarf_split_debug_info, address attributes in dies destined for the
4404 final executable should be direct references--setting the parameter
4405 force_direct ensures this behavior. */
4407 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4408 #define RELOCATED_OFFSET (NULL)
4410 static void
4411 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4412 long unsigned int offset, bool force_direct)
4414 dw_attr_node attr;
4416 attr.dw_attr = attr_kind;
4417 attr.dw_attr_val.val_class = dw_val_class_range_list;
4418 /* For the range_list attribute, use val_entry to store whether the
4419 offset should follow split-debug-info or normal semantics. This
4420 value is read in output_range_list_offset. */
4421 if (dwarf_split_debug_info && !force_direct)
4422 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4423 else
4424 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4425 attr.dw_attr_val.v.val_offset = offset;
4426 add_dwarf_attr (die, &attr);
4429 /* Return the start label of a delta attribute. */
4431 static inline const char *
4432 AT_vms_delta1 (dw_attr_ref a)
4434 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4435 return a->dw_attr_val.v.val_vms_delta.lbl1;
4438 /* Return the end label of a delta attribute. */
4440 static inline const char *
4441 AT_vms_delta2 (dw_attr_ref a)
4443 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4444 return a->dw_attr_val.v.val_vms_delta.lbl2;
4447 static inline const char *
4448 AT_lbl (dw_attr_ref a)
4450 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4451 || AT_class (a) == dw_val_class_lineptr
4452 || AT_class (a) == dw_val_class_macptr
4453 || AT_class (a) == dw_val_class_high_pc));
4454 return a->dw_attr_val.v.val_lbl_id;
4457 /* Get the attribute of type attr_kind. */
4459 static dw_attr_ref
4460 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4462 dw_attr_ref a;
4463 unsigned ix;
4464 dw_die_ref spec = NULL;
4466 if (! die)
4467 return NULL;
4469 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4470 if (a->dw_attr == attr_kind)
4471 return a;
4472 else if (a->dw_attr == DW_AT_specification
4473 || a->dw_attr == DW_AT_abstract_origin)
4474 spec = AT_ref (a);
4476 if (spec)
4477 return get_AT (spec, attr_kind);
4479 return NULL;
4482 /* Returns the parent of the declaration of DIE. */
4484 static dw_die_ref
4485 get_die_parent (dw_die_ref die)
4487 dw_die_ref t;
4489 if (!die)
4490 return NULL;
4492 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4493 || (t = get_AT_ref (die, DW_AT_specification)))
4494 die = t;
4496 return die->die_parent;
4499 /* Return the "low pc" attribute value, typically associated with a subprogram
4500 DIE. Return null if the "low pc" attribute is either not present, or if it
4501 cannot be represented as an assembler label identifier. */
4503 static inline const char *
4504 get_AT_low_pc (dw_die_ref die)
4506 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4508 return a ? AT_lbl (a) : NULL;
4511 /* Return the "high pc" attribute value, typically associated with a subprogram
4512 DIE. Return null if the "high pc" attribute is either not present, or if it
4513 cannot be represented as an assembler label identifier. */
4515 static inline const char *
4516 get_AT_hi_pc (dw_die_ref die)
4518 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4520 return a ? AT_lbl (a) : NULL;
4523 /* Return the value of the string attribute designated by ATTR_KIND, or
4524 NULL if it is not present. */
4526 static inline const char *
4527 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4529 dw_attr_ref a = get_AT (die, attr_kind);
4531 return a ? AT_string (a) : NULL;
4534 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4535 if it is not present. */
4537 static inline int
4538 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4540 dw_attr_ref a = get_AT (die, attr_kind);
4542 return a ? AT_flag (a) : 0;
4545 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4546 if it is not present. */
4548 static inline unsigned
4549 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4551 dw_attr_ref a = get_AT (die, attr_kind);
4553 return a ? AT_unsigned (a) : 0;
4556 static inline dw_die_ref
4557 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4559 dw_attr_ref a = get_AT (die, attr_kind);
4561 return a ? AT_ref (a) : NULL;
4564 static inline struct dwarf_file_data *
4565 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4567 dw_attr_ref a = get_AT (die, attr_kind);
4569 return a ? AT_file (a) : NULL;
4572 /* Return TRUE if the language is C++. */
4574 static inline bool
4575 is_cxx (void)
4577 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4579 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4582 /* Return TRUE if the language is Java. */
4584 static inline bool
4585 is_java (void)
4587 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4589 return lang == DW_LANG_Java;
4592 /* Return TRUE if the language is Fortran. */
4594 static inline bool
4595 is_fortran (void)
4597 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4599 return (lang == DW_LANG_Fortran77
4600 || lang == DW_LANG_Fortran90
4601 || lang == DW_LANG_Fortran95);
4604 /* Return TRUE if the language is Ada. */
4606 static inline bool
4607 is_ada (void)
4609 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4611 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4614 /* Remove the specified attribute if present. */
4616 static void
4617 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4619 dw_attr_ref a;
4620 unsigned ix;
4622 if (! die)
4623 return;
4625 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4626 if (a->dw_attr == attr_kind)
4628 if (AT_class (a) == dw_val_class_str)
4629 if (a->dw_attr_val.v.val_str->refcount)
4630 a->dw_attr_val.v.val_str->refcount--;
4632 /* vec::ordered_remove should help reduce the number of abbrevs
4633 that are needed. */
4634 die->die_attr->ordered_remove (ix);
4635 return;
4639 /* Remove CHILD from its parent. PREV must have the property that
4640 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4642 static void
4643 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4645 gcc_assert (child->die_parent == prev->die_parent);
4646 gcc_assert (prev->die_sib == child);
4647 if (prev == child)
4649 gcc_assert (child->die_parent->die_child == child);
4650 prev = NULL;
4652 else
4653 prev->die_sib = child->die_sib;
4654 if (child->die_parent->die_child == child)
4655 child->die_parent->die_child = prev;
4658 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4659 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4661 static void
4662 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4664 dw_die_ref parent = old_child->die_parent;
4666 gcc_assert (parent == prev->die_parent);
4667 gcc_assert (prev->die_sib == old_child);
4669 new_child->die_parent = parent;
4670 if (prev == old_child)
4672 gcc_assert (parent->die_child == old_child);
4673 new_child->die_sib = new_child;
4675 else
4677 prev->die_sib = new_child;
4678 new_child->die_sib = old_child->die_sib;
4680 if (old_child->die_parent->die_child == old_child)
4681 old_child->die_parent->die_child = new_child;
4684 /* Move all children from OLD_PARENT to NEW_PARENT. */
4686 static void
4687 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4689 dw_die_ref c;
4690 new_parent->die_child = old_parent->die_child;
4691 old_parent->die_child = NULL;
4692 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4695 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4696 matches TAG. */
4698 static void
4699 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4701 dw_die_ref c;
4703 c = die->die_child;
4704 if (c) do {
4705 dw_die_ref prev = c;
4706 c = c->die_sib;
4707 while (c->die_tag == tag)
4709 remove_child_with_prev (c, prev);
4710 /* Might have removed every child. */
4711 if (c == c->die_sib)
4712 return;
4713 c = c->die_sib;
4715 } while (c != die->die_child);
4718 /* Add a CHILD_DIE as the last child of DIE. */
4720 static void
4721 add_child_die (dw_die_ref die, dw_die_ref child_die)
4723 /* FIXME this should probably be an assert. */
4724 if (! die || ! child_die)
4725 return;
4726 gcc_assert (die != child_die);
4728 child_die->die_parent = die;
4729 if (die->die_child)
4731 child_die->die_sib = die->die_child->die_sib;
4732 die->die_child->die_sib = child_die;
4734 else
4735 child_die->die_sib = child_die;
4736 die->die_child = child_die;
4739 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4740 is the specification, to the end of PARENT's list of children.
4741 This is done by removing and re-adding it. */
4743 static void
4744 splice_child_die (dw_die_ref parent, dw_die_ref child)
4746 dw_die_ref p;
4748 /* We want the declaration DIE from inside the class, not the
4749 specification DIE at toplevel. */
4750 if (child->die_parent != parent)
4752 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4754 if (tmp)
4755 child = tmp;
4758 gcc_assert (child->die_parent == parent
4759 || (child->die_parent
4760 == get_AT_ref (parent, DW_AT_specification)));
4762 for (p = child->die_parent->die_child; ; p = p->die_sib)
4763 if (p->die_sib == child)
4765 remove_child_with_prev (child, p);
4766 break;
4769 add_child_die (parent, child);
4772 /* Return a pointer to a newly created DIE node. */
4774 static inline dw_die_ref
4775 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4777 dw_die_ref die = ggc_alloc_cleared_die_node ();
4779 die->die_tag = tag_value;
4781 if (parent_die != NULL)
4782 add_child_die (parent_die, die);
4783 else
4785 limbo_die_node *limbo_node;
4787 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4788 limbo_node->die = die;
4789 limbo_node->created_for = t;
4790 limbo_node->next = limbo_die_list;
4791 limbo_die_list = limbo_node;
4794 return die;
4797 /* Return the DIE associated with the given type specifier. */
4799 static inline dw_die_ref
4800 lookup_type_die (tree type)
4802 return TYPE_SYMTAB_DIE (type);
4805 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4806 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4807 anonymous type instead the one of the naming typedef. */
4809 static inline dw_die_ref
4810 strip_naming_typedef (tree type, dw_die_ref type_die)
4812 if (type
4813 && TREE_CODE (type) == RECORD_TYPE
4814 && type_die
4815 && type_die->die_tag == DW_TAG_typedef
4816 && is_naming_typedef_decl (TYPE_NAME (type)))
4817 type_die = get_AT_ref (type_die, DW_AT_type);
4818 return type_die;
4821 /* Like lookup_type_die, but if type is an anonymous type named by a
4822 typedef[1], return the DIE of the anonymous type instead the one of
4823 the naming typedef. This is because in gen_typedef_die, we did
4824 equate the anonymous struct named by the typedef with the DIE of
4825 the naming typedef. So by default, lookup_type_die on an anonymous
4826 struct yields the DIE of the naming typedef.
4828 [1]: Read the comment of is_naming_typedef_decl to learn about what
4829 a naming typedef is. */
4831 static inline dw_die_ref
4832 lookup_type_die_strip_naming_typedef (tree type)
4834 dw_die_ref die = lookup_type_die (type);
4835 return strip_naming_typedef (type, die);
4838 /* Equate a DIE to a given type specifier. */
4840 static inline void
4841 equate_type_number_to_die (tree type, dw_die_ref type_die)
4843 TYPE_SYMTAB_DIE (type) = type_die;
4846 /* Returns a hash value for X (which really is a die_struct). */
4848 static hashval_t
4849 decl_die_table_hash (const void *x)
4851 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4854 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4856 static int
4857 decl_die_table_eq (const void *x, const void *y)
4859 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4862 /* Return the DIE associated with a given declaration. */
4864 static inline dw_die_ref
4865 lookup_decl_die (tree decl)
4867 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4870 /* Returns a hash value for X (which really is a var_loc_list). */
4872 static hashval_t
4873 decl_loc_table_hash (const void *x)
4875 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4878 /* Return nonzero if decl_id of var_loc_list X is the same as
4879 UID of decl *Y. */
4881 static int
4882 decl_loc_table_eq (const void *x, const void *y)
4884 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4887 /* Return the var_loc list associated with a given declaration. */
4889 static inline var_loc_list *
4890 lookup_decl_loc (const_tree decl)
4892 if (!decl_loc_table)
4893 return NULL;
4894 return (var_loc_list *)
4895 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4898 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4900 static hashval_t
4901 cached_dw_loc_list_table_hash (const void *x)
4903 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4906 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4907 UID of decl *Y. */
4909 static int
4910 cached_dw_loc_list_table_eq (const void *x, const void *y)
4912 return (((const cached_dw_loc_list *) x)->decl_id
4913 == DECL_UID ((const_tree) y));
4916 /* Equate a DIE to a particular declaration. */
4918 static void
4919 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4921 unsigned int decl_id = DECL_UID (decl);
4922 void **slot;
4924 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4925 *slot = decl_die;
4926 decl_die->decl_id = decl_id;
4929 /* Return how many bits covers PIECE EXPR_LIST. */
4931 static int
4932 decl_piece_bitsize (rtx piece)
4934 int ret = (int) GET_MODE (piece);
4935 if (ret)
4936 return ret;
4937 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4938 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4939 return INTVAL (XEXP (XEXP (piece, 0), 0));
4942 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4944 static rtx *
4945 decl_piece_varloc_ptr (rtx piece)
4947 if ((int) GET_MODE (piece))
4948 return &XEXP (piece, 0);
4949 else
4950 return &XEXP (XEXP (piece, 0), 1);
4953 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4954 Next is the chain of following piece nodes. */
4956 static rtx
4957 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4959 if (bitsize <= (int) MAX_MACHINE_MODE)
4960 return alloc_EXPR_LIST (bitsize, loc_note, next);
4961 else
4962 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4963 GEN_INT (bitsize),
4964 loc_note), next);
4967 /* Return rtx that should be stored into loc field for
4968 LOC_NOTE and BITPOS/BITSIZE. */
4970 static rtx
4971 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4972 HOST_WIDE_INT bitsize)
4974 if (bitsize != -1)
4976 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4977 if (bitpos != 0)
4978 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4980 return loc_note;
4983 /* This function either modifies location piece list *DEST in
4984 place (if SRC and INNER is NULL), or copies location piece list
4985 *SRC to *DEST while modifying it. Location BITPOS is modified
4986 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4987 not copied and if needed some padding around it is added.
4988 When modifying in place, DEST should point to EXPR_LIST where
4989 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4990 to the start of the whole list and INNER points to the EXPR_LIST
4991 where earlier pieces cover PIECE_BITPOS bits. */
4993 static void
4994 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4995 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4996 HOST_WIDE_INT bitsize, rtx loc_note)
4998 int diff;
4999 bool copy = inner != NULL;
5001 if (copy)
5003 /* First copy all nodes preceding the current bitpos. */
5004 while (src != inner)
5006 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5007 decl_piece_bitsize (*src), NULL_RTX);
5008 dest = &XEXP (*dest, 1);
5009 src = &XEXP (*src, 1);
5012 /* Add padding if needed. */
5013 if (bitpos != piece_bitpos)
5015 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5016 copy ? NULL_RTX : *dest);
5017 dest = &XEXP (*dest, 1);
5019 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5021 gcc_assert (!copy);
5022 /* A piece with correct bitpos and bitsize already exist,
5023 just update the location for it and return. */
5024 *decl_piece_varloc_ptr (*dest) = loc_note;
5025 return;
5027 /* Add the piece that changed. */
5028 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5029 dest = &XEXP (*dest, 1);
5030 /* Skip over pieces that overlap it. */
5031 diff = bitpos - piece_bitpos + bitsize;
5032 if (!copy)
5033 src = dest;
5034 while (diff > 0 && *src)
5036 rtx piece = *src;
5037 diff -= decl_piece_bitsize (piece);
5038 if (copy)
5039 src = &XEXP (piece, 1);
5040 else
5042 *src = XEXP (piece, 1);
5043 free_EXPR_LIST_node (piece);
5046 /* Add padding if needed. */
5047 if (diff < 0 && *src)
5049 if (!copy)
5050 dest = src;
5051 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5052 dest = &XEXP (*dest, 1);
5054 if (!copy)
5055 return;
5056 /* Finally copy all nodes following it. */
5057 while (*src)
5059 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5060 decl_piece_bitsize (*src), NULL_RTX);
5061 dest = &XEXP (*dest, 1);
5062 src = &XEXP (*src, 1);
5066 /* Add a variable location node to the linked list for DECL. */
5068 static struct var_loc_node *
5069 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5071 unsigned int decl_id;
5072 var_loc_list *temp;
5073 void **slot;
5074 struct var_loc_node *loc = NULL;
5075 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5077 if (TREE_CODE (decl) == VAR_DECL
5078 && DECL_HAS_DEBUG_EXPR_P (decl))
5080 tree realdecl = DECL_DEBUG_EXPR (decl);
5081 if (handled_component_p (realdecl)
5082 || (TREE_CODE (realdecl) == MEM_REF
5083 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5085 HOST_WIDE_INT maxsize;
5086 tree innerdecl;
5087 innerdecl
5088 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5089 if (!DECL_P (innerdecl)
5090 || DECL_IGNORED_P (innerdecl)
5091 || TREE_STATIC (innerdecl)
5092 || bitsize <= 0
5093 || bitpos + bitsize > 256
5094 || bitsize != maxsize)
5095 return NULL;
5096 decl = innerdecl;
5100 decl_id = DECL_UID (decl);
5101 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5102 if (*slot == NULL)
5104 temp = ggc_alloc_cleared_var_loc_list ();
5105 temp->decl_id = decl_id;
5106 *slot = temp;
5108 else
5109 temp = (var_loc_list *) *slot;
5111 /* For PARM_DECLs try to keep around the original incoming value,
5112 even if that means we'll emit a zero-range .debug_loc entry. */
5113 if (temp->last
5114 && temp->first == temp->last
5115 && TREE_CODE (decl) == PARM_DECL
5116 && NOTE_P (temp->first->loc)
5117 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5118 && DECL_INCOMING_RTL (decl)
5119 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5120 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5121 == GET_CODE (DECL_INCOMING_RTL (decl))
5122 && prev_real_insn (temp->first->loc) == NULL_RTX
5123 && (bitsize != -1
5124 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5125 NOTE_VAR_LOCATION_LOC (loc_note))
5126 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5127 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5129 loc = ggc_alloc_cleared_var_loc_node ();
5130 temp->first->next = loc;
5131 temp->last = loc;
5132 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5134 else if (temp->last)
5136 struct var_loc_node *last = temp->last, *unused = NULL;
5137 rtx *piece_loc = NULL, last_loc_note;
5138 int piece_bitpos = 0;
5139 if (last->next)
5141 last = last->next;
5142 gcc_assert (last->next == NULL);
5144 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5146 piece_loc = &last->loc;
5149 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5150 if (piece_bitpos + cur_bitsize > bitpos)
5151 break;
5152 piece_bitpos += cur_bitsize;
5153 piece_loc = &XEXP (*piece_loc, 1);
5155 while (*piece_loc);
5157 /* TEMP->LAST here is either pointer to the last but one or
5158 last element in the chained list, LAST is pointer to the
5159 last element. */
5160 if (label && strcmp (last->label, label) == 0)
5162 /* For SRA optimized variables if there weren't any real
5163 insns since last note, just modify the last node. */
5164 if (piece_loc != NULL)
5166 adjust_piece_list (piece_loc, NULL, NULL,
5167 bitpos, piece_bitpos, bitsize, loc_note);
5168 return NULL;
5170 /* If the last note doesn't cover any instructions, remove it. */
5171 if (temp->last != last)
5173 temp->last->next = NULL;
5174 unused = last;
5175 last = temp->last;
5176 gcc_assert (strcmp (last->label, label) != 0);
5178 else
5180 gcc_assert (temp->first == temp->last
5181 || (temp->first->next == temp->last
5182 && TREE_CODE (decl) == PARM_DECL));
5183 memset (temp->last, '\0', sizeof (*temp->last));
5184 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5185 return temp->last;
5188 if (bitsize == -1 && NOTE_P (last->loc))
5189 last_loc_note = last->loc;
5190 else if (piece_loc != NULL
5191 && *piece_loc != NULL_RTX
5192 && piece_bitpos == bitpos
5193 && decl_piece_bitsize (*piece_loc) == bitsize)
5194 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5195 else
5196 last_loc_note = NULL_RTX;
5197 /* If the current location is the same as the end of the list,
5198 and either both or neither of the locations is uninitialized,
5199 we have nothing to do. */
5200 if (last_loc_note == NULL_RTX
5201 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5202 NOTE_VAR_LOCATION_LOC (loc_note)))
5203 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5204 != NOTE_VAR_LOCATION_STATUS (loc_note))
5205 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5206 == VAR_INIT_STATUS_UNINITIALIZED)
5207 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5208 == VAR_INIT_STATUS_UNINITIALIZED))))
5210 /* Add LOC to the end of list and update LAST. If the last
5211 element of the list has been removed above, reuse its
5212 memory for the new node, otherwise allocate a new one. */
5213 if (unused)
5215 loc = unused;
5216 memset (loc, '\0', sizeof (*loc));
5218 else
5219 loc = ggc_alloc_cleared_var_loc_node ();
5220 if (bitsize == -1 || piece_loc == NULL)
5221 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5222 else
5223 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5224 bitpos, piece_bitpos, bitsize, loc_note);
5225 last->next = loc;
5226 /* Ensure TEMP->LAST will point either to the new last but one
5227 element of the chain, or to the last element in it. */
5228 if (last != temp->last)
5229 temp->last = last;
5231 else if (unused)
5232 ggc_free (unused);
5234 else
5236 loc = ggc_alloc_cleared_var_loc_node ();
5237 temp->first = loc;
5238 temp->last = loc;
5239 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5241 return loc;
5244 /* Keep track of the number of spaces used to indent the
5245 output of the debugging routines that print the structure of
5246 the DIE internal representation. */
5247 static int print_indent;
5249 /* Indent the line the number of spaces given by print_indent. */
5251 static inline void
5252 print_spaces (FILE *outfile)
5254 fprintf (outfile, "%*s", print_indent, "");
5257 /* Print a type signature in hex. */
5259 static inline void
5260 print_signature (FILE *outfile, char *sig)
5262 int i;
5264 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5265 fprintf (outfile, "%02x", sig[i] & 0xff);
5268 /* Print the information associated with a given DIE, and its children.
5269 This routine is a debugging aid only. */
5271 static void
5272 print_die (dw_die_ref die, FILE *outfile)
5274 dw_attr_ref a;
5275 dw_die_ref c;
5276 unsigned ix;
5278 print_spaces (outfile);
5279 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5280 die->die_offset, dwarf_tag_name (die->die_tag),
5281 (void*) die);
5282 print_spaces (outfile);
5283 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5284 fprintf (outfile, " offset: %ld", die->die_offset);
5285 fprintf (outfile, " mark: %d\n", die->die_mark);
5287 if (die->comdat_type_p)
5289 print_spaces (outfile);
5290 fprintf (outfile, " signature: ");
5291 print_signature (outfile, die->die_id.die_type_node->signature);
5292 fprintf (outfile, "\n");
5295 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5297 print_spaces (outfile);
5298 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5300 switch (AT_class (a))
5302 case dw_val_class_addr:
5303 fprintf (outfile, "address");
5304 break;
5305 case dw_val_class_offset:
5306 fprintf (outfile, "offset");
5307 break;
5308 case dw_val_class_loc:
5309 fprintf (outfile, "location descriptor");
5310 break;
5311 case dw_val_class_loc_list:
5312 fprintf (outfile, "location list -> label:%s",
5313 AT_loc_list (a)->ll_symbol);
5314 break;
5315 case dw_val_class_range_list:
5316 fprintf (outfile, "range list");
5317 break;
5318 case dw_val_class_const:
5319 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5320 break;
5321 case dw_val_class_unsigned_const:
5322 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5323 break;
5324 case dw_val_class_const_double:
5325 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5326 HOST_WIDE_INT_PRINT_UNSIGNED")",
5327 a->dw_attr_val.v.val_double.high,
5328 a->dw_attr_val.v.val_double.low);
5329 break;
5330 case dw_val_class_vec:
5331 fprintf (outfile, "floating-point or vector constant");
5332 break;
5333 case dw_val_class_flag:
5334 fprintf (outfile, "%u", AT_flag (a));
5335 break;
5336 case dw_val_class_die_ref:
5337 if (AT_ref (a) != NULL)
5339 if (AT_ref (a)->comdat_type_p)
5341 fprintf (outfile, "die -> signature: ");
5342 print_signature (outfile,
5343 AT_ref (a)->die_id.die_type_node->signature);
5345 else if (AT_ref (a)->die_id.die_symbol)
5346 fprintf (outfile, "die -> label: %s",
5347 AT_ref (a)->die_id.die_symbol);
5348 else
5349 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5350 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5352 else
5353 fprintf (outfile, "die -> <null>");
5354 break;
5355 case dw_val_class_vms_delta:
5356 fprintf (outfile, "delta: @slotcount(%s-%s)",
5357 AT_vms_delta2 (a), AT_vms_delta1 (a));
5358 break;
5359 case dw_val_class_lbl_id:
5360 case dw_val_class_lineptr:
5361 case dw_val_class_macptr:
5362 case dw_val_class_high_pc:
5363 fprintf (outfile, "label: %s", AT_lbl (a));
5364 break;
5365 case dw_val_class_str:
5366 if (AT_string (a) != NULL)
5367 fprintf (outfile, "\"%s\"", AT_string (a));
5368 else
5369 fprintf (outfile, "<null>");
5370 break;
5371 case dw_val_class_file:
5372 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5373 AT_file (a)->emitted_number);
5374 break;
5375 case dw_val_class_data8:
5377 int i;
5379 for (i = 0; i < 8; i++)
5380 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5381 break;
5383 default:
5384 break;
5387 fprintf (outfile, "\n");
5390 if (die->die_child != NULL)
5392 print_indent += 4;
5393 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5394 print_indent -= 4;
5396 if (print_indent == 0)
5397 fprintf (outfile, "\n");
5400 /* Print the information collected for a given DIE. */
5402 DEBUG_FUNCTION void
5403 debug_dwarf_die (dw_die_ref die)
5405 print_die (die, stderr);
5408 DEBUG_FUNCTION void
5409 debug (die_struct &ref)
5411 print_die (&ref, stderr);
5414 DEBUG_FUNCTION void
5415 debug (die_struct *ptr)
5417 if (ptr)
5418 debug (*ptr);
5419 else
5420 fprintf (stderr, "<nil>\n");
5424 /* Print all DWARF information collected for the compilation unit.
5425 This routine is a debugging aid only. */
5427 DEBUG_FUNCTION void
5428 debug_dwarf (void)
5430 print_indent = 0;
5431 print_die (comp_unit_die (), stderr);
5434 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5435 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5436 DIE that marks the start of the DIEs for this include file. */
5438 static dw_die_ref
5439 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5441 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5442 dw_die_ref new_unit = gen_compile_unit_die (filename);
5444 new_unit->die_sib = old_unit;
5445 return new_unit;
5448 /* Close an include-file CU and reopen the enclosing one. */
5450 static dw_die_ref
5451 pop_compile_unit (dw_die_ref old_unit)
5453 dw_die_ref new_unit = old_unit->die_sib;
5455 old_unit->die_sib = NULL;
5456 return new_unit;
5459 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5460 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5461 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5463 /* Calculate the checksum of a location expression. */
5465 static inline void
5466 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5468 int tem;
5469 hashval_t hash = 0;
5471 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5472 CHECKSUM (tem);
5473 hash = hash_loc_operands (loc, hash);
5474 CHECKSUM (hash);
5477 /* Calculate the checksum of an attribute. */
5479 static void
5480 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5482 dw_loc_descr_ref loc;
5483 rtx r;
5485 CHECKSUM (at->dw_attr);
5487 /* We don't care that this was compiled with a different compiler
5488 snapshot; if the output is the same, that's what matters. */
5489 if (at->dw_attr == DW_AT_producer)
5490 return;
5492 switch (AT_class (at))
5494 case dw_val_class_const:
5495 CHECKSUM (at->dw_attr_val.v.val_int);
5496 break;
5497 case dw_val_class_unsigned_const:
5498 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5499 break;
5500 case dw_val_class_const_double:
5501 CHECKSUM (at->dw_attr_val.v.val_double);
5502 break;
5503 case dw_val_class_vec:
5504 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5505 (at->dw_attr_val.v.val_vec.length
5506 * at->dw_attr_val.v.val_vec.elt_size));
5507 break;
5508 case dw_val_class_flag:
5509 CHECKSUM (at->dw_attr_val.v.val_flag);
5510 break;
5511 case dw_val_class_str:
5512 CHECKSUM_STRING (AT_string (at));
5513 break;
5515 case dw_val_class_addr:
5516 r = AT_addr (at);
5517 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5518 CHECKSUM_STRING (XSTR (r, 0));
5519 break;
5521 case dw_val_class_offset:
5522 CHECKSUM (at->dw_attr_val.v.val_offset);
5523 break;
5525 case dw_val_class_loc:
5526 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5527 loc_checksum (loc, ctx);
5528 break;
5530 case dw_val_class_die_ref:
5531 die_checksum (AT_ref (at), ctx, mark);
5532 break;
5534 case dw_val_class_fde_ref:
5535 case dw_val_class_vms_delta:
5536 case dw_val_class_lbl_id:
5537 case dw_val_class_lineptr:
5538 case dw_val_class_macptr:
5539 case dw_val_class_high_pc:
5540 break;
5542 case dw_val_class_file:
5543 CHECKSUM_STRING (AT_file (at)->filename);
5544 break;
5546 case dw_val_class_data8:
5547 CHECKSUM (at->dw_attr_val.v.val_data8);
5548 break;
5550 default:
5551 break;
5555 /* Calculate the checksum of a DIE. */
5557 static void
5558 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5560 dw_die_ref c;
5561 dw_attr_ref a;
5562 unsigned ix;
5564 /* To avoid infinite recursion. */
5565 if (die->die_mark)
5567 CHECKSUM (die->die_mark);
5568 return;
5570 die->die_mark = ++(*mark);
5572 CHECKSUM (die->die_tag);
5574 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5575 attr_checksum (a, ctx, mark);
5577 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5580 #undef CHECKSUM
5581 #undef CHECKSUM_BLOCK
5582 #undef CHECKSUM_STRING
5584 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5585 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5586 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5587 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5588 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5589 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5590 #define CHECKSUM_ATTR(FOO) \
5591 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5593 /* Calculate the checksum of a number in signed LEB128 format. */
5595 static void
5596 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5598 unsigned char byte;
5599 bool more;
5601 while (1)
5603 byte = (value & 0x7f);
5604 value >>= 7;
5605 more = !((value == 0 && (byte & 0x40) == 0)
5606 || (value == -1 && (byte & 0x40) != 0));
5607 if (more)
5608 byte |= 0x80;
5609 CHECKSUM (byte);
5610 if (!more)
5611 break;
5615 /* Calculate the checksum of a number in unsigned LEB128 format. */
5617 static void
5618 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5620 while (1)
5622 unsigned char byte = (value & 0x7f);
5623 value >>= 7;
5624 if (value != 0)
5625 /* More bytes to follow. */
5626 byte |= 0x80;
5627 CHECKSUM (byte);
5628 if (value == 0)
5629 break;
5633 /* Checksum the context of the DIE. This adds the names of any
5634 surrounding namespaces or structures to the checksum. */
5636 static void
5637 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5639 const char *name;
5640 dw_die_ref spec;
5641 int tag = die->die_tag;
5643 if (tag != DW_TAG_namespace
5644 && tag != DW_TAG_structure_type
5645 && tag != DW_TAG_class_type)
5646 return;
5648 name = get_AT_string (die, DW_AT_name);
5650 spec = get_AT_ref (die, DW_AT_specification);
5651 if (spec != NULL)
5652 die = spec;
5654 if (die->die_parent != NULL)
5655 checksum_die_context (die->die_parent, ctx);
5657 CHECKSUM_ULEB128 ('C');
5658 CHECKSUM_ULEB128 (tag);
5659 if (name != NULL)
5660 CHECKSUM_STRING (name);
5663 /* Calculate the checksum of a location expression. */
5665 static inline void
5666 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5668 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5669 were emitted as a DW_FORM_sdata instead of a location expression. */
5670 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5672 CHECKSUM_ULEB128 (DW_FORM_sdata);
5673 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5674 return;
5677 /* Otherwise, just checksum the raw location expression. */
5678 while (loc != NULL)
5680 hashval_t hash = 0;
5682 CHECKSUM_ULEB128 (loc->dtprel);
5683 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5684 hash = hash_loc_operands (loc, hash);
5685 CHECKSUM (hash);
5686 loc = loc->dw_loc_next;
5690 /* Calculate the checksum of an attribute. */
5692 static void
5693 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5694 struct md5_ctx *ctx, int *mark)
5696 dw_loc_descr_ref loc;
5697 rtx r;
5699 if (AT_class (at) == dw_val_class_die_ref)
5701 dw_die_ref target_die = AT_ref (at);
5703 /* For pointer and reference types, we checksum only the (qualified)
5704 name of the target type (if there is a name). For friend entries,
5705 we checksum only the (qualified) name of the target type or function.
5706 This allows the checksum to remain the same whether the target type
5707 is complete or not. */
5708 if ((at->dw_attr == DW_AT_type
5709 && (tag == DW_TAG_pointer_type
5710 || tag == DW_TAG_reference_type
5711 || tag == DW_TAG_rvalue_reference_type
5712 || tag == DW_TAG_ptr_to_member_type))
5713 || (at->dw_attr == DW_AT_friend
5714 && tag == DW_TAG_friend))
5716 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5718 if (name_attr != NULL)
5720 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5722 if (decl == NULL)
5723 decl = target_die;
5724 CHECKSUM_ULEB128 ('N');
5725 CHECKSUM_ULEB128 (at->dw_attr);
5726 if (decl->die_parent != NULL)
5727 checksum_die_context (decl->die_parent, ctx);
5728 CHECKSUM_ULEB128 ('E');
5729 CHECKSUM_STRING (AT_string (name_attr));
5730 return;
5734 /* For all other references to another DIE, we check to see if the
5735 target DIE has already been visited. If it has, we emit a
5736 backward reference; if not, we descend recursively. */
5737 if (target_die->die_mark > 0)
5739 CHECKSUM_ULEB128 ('R');
5740 CHECKSUM_ULEB128 (at->dw_attr);
5741 CHECKSUM_ULEB128 (target_die->die_mark);
5743 else
5745 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5747 if (decl == NULL)
5748 decl = target_die;
5749 target_die->die_mark = ++(*mark);
5750 CHECKSUM_ULEB128 ('T');
5751 CHECKSUM_ULEB128 (at->dw_attr);
5752 if (decl->die_parent != NULL)
5753 checksum_die_context (decl->die_parent, ctx);
5754 die_checksum_ordered (target_die, ctx, mark);
5756 return;
5759 CHECKSUM_ULEB128 ('A');
5760 CHECKSUM_ULEB128 (at->dw_attr);
5762 switch (AT_class (at))
5764 case dw_val_class_const:
5765 CHECKSUM_ULEB128 (DW_FORM_sdata);
5766 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5767 break;
5769 case dw_val_class_unsigned_const:
5770 CHECKSUM_ULEB128 (DW_FORM_sdata);
5771 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5772 break;
5774 case dw_val_class_const_double:
5775 CHECKSUM_ULEB128 (DW_FORM_block);
5776 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5777 CHECKSUM (at->dw_attr_val.v.val_double);
5778 break;
5780 case dw_val_class_vec:
5781 CHECKSUM_ULEB128 (DW_FORM_block);
5782 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5783 * at->dw_attr_val.v.val_vec.elt_size);
5784 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5785 (at->dw_attr_val.v.val_vec.length
5786 * at->dw_attr_val.v.val_vec.elt_size));
5787 break;
5789 case dw_val_class_flag:
5790 CHECKSUM_ULEB128 (DW_FORM_flag);
5791 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5792 break;
5794 case dw_val_class_str:
5795 CHECKSUM_ULEB128 (DW_FORM_string);
5796 CHECKSUM_STRING (AT_string (at));
5797 break;
5799 case dw_val_class_addr:
5800 r = AT_addr (at);
5801 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5802 CHECKSUM_ULEB128 (DW_FORM_string);
5803 CHECKSUM_STRING (XSTR (r, 0));
5804 break;
5806 case dw_val_class_offset:
5807 CHECKSUM_ULEB128 (DW_FORM_sdata);
5808 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5809 break;
5811 case dw_val_class_loc:
5812 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5813 loc_checksum_ordered (loc, ctx);
5814 break;
5816 case dw_val_class_fde_ref:
5817 case dw_val_class_lbl_id:
5818 case dw_val_class_lineptr:
5819 case dw_val_class_macptr:
5820 case dw_val_class_high_pc:
5821 break;
5823 case dw_val_class_file:
5824 CHECKSUM_ULEB128 (DW_FORM_string);
5825 CHECKSUM_STRING (AT_file (at)->filename);
5826 break;
5828 case dw_val_class_data8:
5829 CHECKSUM (at->dw_attr_val.v.val_data8);
5830 break;
5832 default:
5833 break;
5837 struct checksum_attributes
5839 dw_attr_ref at_name;
5840 dw_attr_ref at_type;
5841 dw_attr_ref at_friend;
5842 dw_attr_ref at_accessibility;
5843 dw_attr_ref at_address_class;
5844 dw_attr_ref at_allocated;
5845 dw_attr_ref at_artificial;
5846 dw_attr_ref at_associated;
5847 dw_attr_ref at_binary_scale;
5848 dw_attr_ref at_bit_offset;
5849 dw_attr_ref at_bit_size;
5850 dw_attr_ref at_bit_stride;
5851 dw_attr_ref at_byte_size;
5852 dw_attr_ref at_byte_stride;
5853 dw_attr_ref at_const_value;
5854 dw_attr_ref at_containing_type;
5855 dw_attr_ref at_count;
5856 dw_attr_ref at_data_location;
5857 dw_attr_ref at_data_member_location;
5858 dw_attr_ref at_decimal_scale;
5859 dw_attr_ref at_decimal_sign;
5860 dw_attr_ref at_default_value;
5861 dw_attr_ref at_digit_count;
5862 dw_attr_ref at_discr;
5863 dw_attr_ref at_discr_list;
5864 dw_attr_ref at_discr_value;
5865 dw_attr_ref at_encoding;
5866 dw_attr_ref at_endianity;
5867 dw_attr_ref at_explicit;
5868 dw_attr_ref at_is_optional;
5869 dw_attr_ref at_location;
5870 dw_attr_ref at_lower_bound;
5871 dw_attr_ref at_mutable;
5872 dw_attr_ref at_ordering;
5873 dw_attr_ref at_picture_string;
5874 dw_attr_ref at_prototyped;
5875 dw_attr_ref at_small;
5876 dw_attr_ref at_segment;
5877 dw_attr_ref at_string_length;
5878 dw_attr_ref at_threads_scaled;
5879 dw_attr_ref at_upper_bound;
5880 dw_attr_ref at_use_location;
5881 dw_attr_ref at_use_UTF8;
5882 dw_attr_ref at_variable_parameter;
5883 dw_attr_ref at_virtuality;
5884 dw_attr_ref at_visibility;
5885 dw_attr_ref at_vtable_elem_location;
5888 /* Collect the attributes that we will want to use for the checksum. */
5890 static void
5891 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5893 dw_attr_ref a;
5894 unsigned ix;
5896 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5898 switch (a->dw_attr)
5900 case DW_AT_name:
5901 attrs->at_name = a;
5902 break;
5903 case DW_AT_type:
5904 attrs->at_type = a;
5905 break;
5906 case DW_AT_friend:
5907 attrs->at_friend = a;
5908 break;
5909 case DW_AT_accessibility:
5910 attrs->at_accessibility = a;
5911 break;
5912 case DW_AT_address_class:
5913 attrs->at_address_class = a;
5914 break;
5915 case DW_AT_allocated:
5916 attrs->at_allocated = a;
5917 break;
5918 case DW_AT_artificial:
5919 attrs->at_artificial = a;
5920 break;
5921 case DW_AT_associated:
5922 attrs->at_associated = a;
5923 break;
5924 case DW_AT_binary_scale:
5925 attrs->at_binary_scale = a;
5926 break;
5927 case DW_AT_bit_offset:
5928 attrs->at_bit_offset = a;
5929 break;
5930 case DW_AT_bit_size:
5931 attrs->at_bit_size = a;
5932 break;
5933 case DW_AT_bit_stride:
5934 attrs->at_bit_stride = a;
5935 break;
5936 case DW_AT_byte_size:
5937 attrs->at_byte_size = a;
5938 break;
5939 case DW_AT_byte_stride:
5940 attrs->at_byte_stride = a;
5941 break;
5942 case DW_AT_const_value:
5943 attrs->at_const_value = a;
5944 break;
5945 case DW_AT_containing_type:
5946 attrs->at_containing_type = a;
5947 break;
5948 case DW_AT_count:
5949 attrs->at_count = a;
5950 break;
5951 case DW_AT_data_location:
5952 attrs->at_data_location = a;
5953 break;
5954 case DW_AT_data_member_location:
5955 attrs->at_data_member_location = a;
5956 break;
5957 case DW_AT_decimal_scale:
5958 attrs->at_decimal_scale = a;
5959 break;
5960 case DW_AT_decimal_sign:
5961 attrs->at_decimal_sign = a;
5962 break;
5963 case DW_AT_default_value:
5964 attrs->at_default_value = a;
5965 break;
5966 case DW_AT_digit_count:
5967 attrs->at_digit_count = a;
5968 break;
5969 case DW_AT_discr:
5970 attrs->at_discr = a;
5971 break;
5972 case DW_AT_discr_list:
5973 attrs->at_discr_list = a;
5974 break;
5975 case DW_AT_discr_value:
5976 attrs->at_discr_value = a;
5977 break;
5978 case DW_AT_encoding:
5979 attrs->at_encoding = a;
5980 break;
5981 case DW_AT_endianity:
5982 attrs->at_endianity = a;
5983 break;
5984 case DW_AT_explicit:
5985 attrs->at_explicit = a;
5986 break;
5987 case DW_AT_is_optional:
5988 attrs->at_is_optional = a;
5989 break;
5990 case DW_AT_location:
5991 attrs->at_location = a;
5992 break;
5993 case DW_AT_lower_bound:
5994 attrs->at_lower_bound = a;
5995 break;
5996 case DW_AT_mutable:
5997 attrs->at_mutable = a;
5998 break;
5999 case DW_AT_ordering:
6000 attrs->at_ordering = a;
6001 break;
6002 case DW_AT_picture_string:
6003 attrs->at_picture_string = a;
6004 break;
6005 case DW_AT_prototyped:
6006 attrs->at_prototyped = a;
6007 break;
6008 case DW_AT_small:
6009 attrs->at_small = a;
6010 break;
6011 case DW_AT_segment:
6012 attrs->at_segment = a;
6013 break;
6014 case DW_AT_string_length:
6015 attrs->at_string_length = a;
6016 break;
6017 case DW_AT_threads_scaled:
6018 attrs->at_threads_scaled = a;
6019 break;
6020 case DW_AT_upper_bound:
6021 attrs->at_upper_bound = a;
6022 break;
6023 case DW_AT_use_location:
6024 attrs->at_use_location = a;
6025 break;
6026 case DW_AT_use_UTF8:
6027 attrs->at_use_UTF8 = a;
6028 break;
6029 case DW_AT_variable_parameter:
6030 attrs->at_variable_parameter = a;
6031 break;
6032 case DW_AT_virtuality:
6033 attrs->at_virtuality = a;
6034 break;
6035 case DW_AT_visibility:
6036 attrs->at_visibility = a;
6037 break;
6038 case DW_AT_vtable_elem_location:
6039 attrs->at_vtable_elem_location = a;
6040 break;
6041 default:
6042 break;
6047 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6049 static void
6050 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6052 dw_die_ref c;
6053 dw_die_ref decl;
6054 struct checksum_attributes attrs;
6056 CHECKSUM_ULEB128 ('D');
6057 CHECKSUM_ULEB128 (die->die_tag);
6059 memset (&attrs, 0, sizeof (attrs));
6061 decl = get_AT_ref (die, DW_AT_specification);
6062 if (decl != NULL)
6063 collect_checksum_attributes (&attrs, decl);
6064 collect_checksum_attributes (&attrs, die);
6066 CHECKSUM_ATTR (attrs.at_name);
6067 CHECKSUM_ATTR (attrs.at_accessibility);
6068 CHECKSUM_ATTR (attrs.at_address_class);
6069 CHECKSUM_ATTR (attrs.at_allocated);
6070 CHECKSUM_ATTR (attrs.at_artificial);
6071 CHECKSUM_ATTR (attrs.at_associated);
6072 CHECKSUM_ATTR (attrs.at_binary_scale);
6073 CHECKSUM_ATTR (attrs.at_bit_offset);
6074 CHECKSUM_ATTR (attrs.at_bit_size);
6075 CHECKSUM_ATTR (attrs.at_bit_stride);
6076 CHECKSUM_ATTR (attrs.at_byte_size);
6077 CHECKSUM_ATTR (attrs.at_byte_stride);
6078 CHECKSUM_ATTR (attrs.at_const_value);
6079 CHECKSUM_ATTR (attrs.at_containing_type);
6080 CHECKSUM_ATTR (attrs.at_count);
6081 CHECKSUM_ATTR (attrs.at_data_location);
6082 CHECKSUM_ATTR (attrs.at_data_member_location);
6083 CHECKSUM_ATTR (attrs.at_decimal_scale);
6084 CHECKSUM_ATTR (attrs.at_decimal_sign);
6085 CHECKSUM_ATTR (attrs.at_default_value);
6086 CHECKSUM_ATTR (attrs.at_digit_count);
6087 CHECKSUM_ATTR (attrs.at_discr);
6088 CHECKSUM_ATTR (attrs.at_discr_list);
6089 CHECKSUM_ATTR (attrs.at_discr_value);
6090 CHECKSUM_ATTR (attrs.at_encoding);
6091 CHECKSUM_ATTR (attrs.at_endianity);
6092 CHECKSUM_ATTR (attrs.at_explicit);
6093 CHECKSUM_ATTR (attrs.at_is_optional);
6094 CHECKSUM_ATTR (attrs.at_location);
6095 CHECKSUM_ATTR (attrs.at_lower_bound);
6096 CHECKSUM_ATTR (attrs.at_mutable);
6097 CHECKSUM_ATTR (attrs.at_ordering);
6098 CHECKSUM_ATTR (attrs.at_picture_string);
6099 CHECKSUM_ATTR (attrs.at_prototyped);
6100 CHECKSUM_ATTR (attrs.at_small);
6101 CHECKSUM_ATTR (attrs.at_segment);
6102 CHECKSUM_ATTR (attrs.at_string_length);
6103 CHECKSUM_ATTR (attrs.at_threads_scaled);
6104 CHECKSUM_ATTR (attrs.at_upper_bound);
6105 CHECKSUM_ATTR (attrs.at_use_location);
6106 CHECKSUM_ATTR (attrs.at_use_UTF8);
6107 CHECKSUM_ATTR (attrs.at_variable_parameter);
6108 CHECKSUM_ATTR (attrs.at_virtuality);
6109 CHECKSUM_ATTR (attrs.at_visibility);
6110 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6111 CHECKSUM_ATTR (attrs.at_type);
6112 CHECKSUM_ATTR (attrs.at_friend);
6114 /* Checksum the child DIEs. */
6115 c = die->die_child;
6116 if (c) do {
6117 dw_attr_ref name_attr;
6119 c = c->die_sib;
6120 name_attr = get_AT (c, DW_AT_name);
6121 if (is_template_instantiation (c))
6123 /* Ignore instantiations of member type and function templates. */
6125 else if (name_attr != NULL
6126 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6128 /* Use a shallow checksum for named nested types and member
6129 functions. */
6130 CHECKSUM_ULEB128 ('S');
6131 CHECKSUM_ULEB128 (c->die_tag);
6132 CHECKSUM_STRING (AT_string (name_attr));
6134 else
6136 /* Use a deep checksum for other children. */
6137 /* Mark this DIE so it gets processed when unmarking. */
6138 if (c->die_mark == 0)
6139 c->die_mark = -1;
6140 die_checksum_ordered (c, ctx, mark);
6142 } while (c != die->die_child);
6144 CHECKSUM_ULEB128 (0);
6147 /* Add a type name and tag to a hash. */
6148 static void
6149 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6151 CHECKSUM_ULEB128 (tag);
6152 CHECKSUM_STRING (name);
6155 #undef CHECKSUM
6156 #undef CHECKSUM_STRING
6157 #undef CHECKSUM_ATTR
6158 #undef CHECKSUM_LEB128
6159 #undef CHECKSUM_ULEB128
6161 /* Generate the type signature for DIE. This is computed by generating an
6162 MD5 checksum over the DIE's tag, its relevant attributes, and its
6163 children. Attributes that are references to other DIEs are processed
6164 by recursion, using the MARK field to prevent infinite recursion.
6165 If the DIE is nested inside a namespace or another type, we also
6166 need to include that context in the signature. The lower 64 bits
6167 of the resulting MD5 checksum comprise the signature. */
6169 static void
6170 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6172 int mark;
6173 const char *name;
6174 unsigned char checksum[16];
6175 struct md5_ctx ctx;
6176 dw_die_ref decl;
6177 dw_die_ref parent;
6179 name = get_AT_string (die, DW_AT_name);
6180 decl = get_AT_ref (die, DW_AT_specification);
6181 parent = get_die_parent (die);
6183 /* First, compute a signature for just the type name (and its surrounding
6184 context, if any. This is stored in the type unit DIE for link-time
6185 ODR (one-definition rule) checking. */
6187 if (is_cxx () && name != NULL)
6189 md5_init_ctx (&ctx);
6191 /* Checksum the names of surrounding namespaces and structures. */
6192 if (parent != NULL)
6193 checksum_die_context (parent, &ctx);
6195 /* Checksum the current DIE. */
6196 die_odr_checksum (die->die_tag, name, &ctx);
6197 md5_finish_ctx (&ctx, checksum);
6199 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6202 /* Next, compute the complete type signature. */
6204 md5_init_ctx (&ctx);
6205 mark = 1;
6206 die->die_mark = mark;
6208 /* Checksum the names of surrounding namespaces and structures. */
6209 if (parent != NULL)
6210 checksum_die_context (parent, &ctx);
6212 /* Checksum the DIE and its children. */
6213 die_checksum_ordered (die, &ctx, &mark);
6214 unmark_all_dies (die);
6215 md5_finish_ctx (&ctx, checksum);
6217 /* Store the signature in the type node and link the type DIE and the
6218 type node together. */
6219 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6220 DWARF_TYPE_SIGNATURE_SIZE);
6221 die->comdat_type_p = true;
6222 die->die_id.die_type_node = type_node;
6223 type_node->type_die = die;
6225 /* If the DIE is a specification, link its declaration to the type node
6226 as well. */
6227 if (decl != NULL)
6229 decl->comdat_type_p = true;
6230 decl->die_id.die_type_node = type_node;
6234 /* Do the location expressions look same? */
6235 static inline int
6236 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6238 return loc1->dw_loc_opc == loc2->dw_loc_opc
6239 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6240 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6243 /* Do the values look the same? */
6244 static int
6245 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6247 dw_loc_descr_ref loc1, loc2;
6248 rtx r1, r2;
6250 if (v1->val_class != v2->val_class)
6251 return 0;
6253 switch (v1->val_class)
6255 case dw_val_class_const:
6256 return v1->v.val_int == v2->v.val_int;
6257 case dw_val_class_unsigned_const:
6258 return v1->v.val_unsigned == v2->v.val_unsigned;
6259 case dw_val_class_const_double:
6260 return v1->v.val_double.high == v2->v.val_double.high
6261 && v1->v.val_double.low == v2->v.val_double.low;
6262 case dw_val_class_vec:
6263 if (v1->v.val_vec.length != v2->v.val_vec.length
6264 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6265 return 0;
6266 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6267 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6268 return 0;
6269 return 1;
6270 case dw_val_class_flag:
6271 return v1->v.val_flag == v2->v.val_flag;
6272 case dw_val_class_str:
6273 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6275 case dw_val_class_addr:
6276 r1 = v1->v.val_addr;
6277 r2 = v2->v.val_addr;
6278 if (GET_CODE (r1) != GET_CODE (r2))
6279 return 0;
6280 return !rtx_equal_p (r1, r2);
6282 case dw_val_class_offset:
6283 return v1->v.val_offset == v2->v.val_offset;
6285 case dw_val_class_loc:
6286 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6287 loc1 && loc2;
6288 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6289 if (!same_loc_p (loc1, loc2, mark))
6290 return 0;
6291 return !loc1 && !loc2;
6293 case dw_val_class_die_ref:
6294 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6296 case dw_val_class_fde_ref:
6297 case dw_val_class_vms_delta:
6298 case dw_val_class_lbl_id:
6299 case dw_val_class_lineptr:
6300 case dw_val_class_macptr:
6301 case dw_val_class_high_pc:
6302 return 1;
6304 case dw_val_class_file:
6305 return v1->v.val_file == v2->v.val_file;
6307 case dw_val_class_data8:
6308 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6310 default:
6311 return 1;
6315 /* Do the attributes look the same? */
6317 static int
6318 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6320 if (at1->dw_attr != at2->dw_attr)
6321 return 0;
6323 /* We don't care that this was compiled with a different compiler
6324 snapshot; if the output is the same, that's what matters. */
6325 if (at1->dw_attr == DW_AT_producer)
6326 return 1;
6328 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6331 /* Do the dies look the same? */
6333 static int
6334 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6336 dw_die_ref c1, c2;
6337 dw_attr_ref a1;
6338 unsigned ix;
6340 /* To avoid infinite recursion. */
6341 if (die1->die_mark)
6342 return die1->die_mark == die2->die_mark;
6343 die1->die_mark = die2->die_mark = ++(*mark);
6345 if (die1->die_tag != die2->die_tag)
6346 return 0;
6348 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6349 return 0;
6351 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6352 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6353 return 0;
6355 c1 = die1->die_child;
6356 c2 = die2->die_child;
6357 if (! c1)
6359 if (c2)
6360 return 0;
6362 else
6363 for (;;)
6365 if (!same_die_p (c1, c2, mark))
6366 return 0;
6367 c1 = c1->die_sib;
6368 c2 = c2->die_sib;
6369 if (c1 == die1->die_child)
6371 if (c2 == die2->die_child)
6372 break;
6373 else
6374 return 0;
6378 return 1;
6381 /* Do the dies look the same? Wrapper around same_die_p. */
6383 static int
6384 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6386 int mark = 0;
6387 int ret = same_die_p (die1, die2, &mark);
6389 unmark_all_dies (die1);
6390 unmark_all_dies (die2);
6392 return ret;
6395 /* The prefix to attach to symbols on DIEs in the current comdat debug
6396 info section. */
6397 static const char *comdat_symbol_id;
6399 /* The index of the current symbol within the current comdat CU. */
6400 static unsigned int comdat_symbol_number;
6402 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6403 children, and set comdat_symbol_id accordingly. */
6405 static void
6406 compute_section_prefix (dw_die_ref unit_die)
6408 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6409 const char *base = die_name ? lbasename (die_name) : "anonymous";
6410 char *name = XALLOCAVEC (char, strlen (base) + 64);
6411 char *p;
6412 int i, mark;
6413 unsigned char checksum[16];
6414 struct md5_ctx ctx;
6416 /* Compute the checksum of the DIE, then append part of it as hex digits to
6417 the name filename of the unit. */
6419 md5_init_ctx (&ctx);
6420 mark = 0;
6421 die_checksum (unit_die, &ctx, &mark);
6422 unmark_all_dies (unit_die);
6423 md5_finish_ctx (&ctx, checksum);
6425 sprintf (name, "%s.", base);
6426 clean_symbol_name (name);
6428 p = name + strlen (name);
6429 for (i = 0; i < 4; i++)
6431 sprintf (p, "%.2x", checksum[i]);
6432 p += 2;
6435 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6436 comdat_symbol_number = 0;
6439 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6441 static int
6442 is_type_die (dw_die_ref die)
6444 switch (die->die_tag)
6446 case DW_TAG_array_type:
6447 case DW_TAG_class_type:
6448 case DW_TAG_interface_type:
6449 case DW_TAG_enumeration_type:
6450 case DW_TAG_pointer_type:
6451 case DW_TAG_reference_type:
6452 case DW_TAG_rvalue_reference_type:
6453 case DW_TAG_string_type:
6454 case DW_TAG_structure_type:
6455 case DW_TAG_subroutine_type:
6456 case DW_TAG_union_type:
6457 case DW_TAG_ptr_to_member_type:
6458 case DW_TAG_set_type:
6459 case DW_TAG_subrange_type:
6460 case DW_TAG_base_type:
6461 case DW_TAG_const_type:
6462 case DW_TAG_file_type:
6463 case DW_TAG_packed_type:
6464 case DW_TAG_volatile_type:
6465 case DW_TAG_typedef:
6466 return 1;
6467 default:
6468 return 0;
6472 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6473 Basically, we want to choose the bits that are likely to be shared between
6474 compilations (types) and leave out the bits that are specific to individual
6475 compilations (functions). */
6477 static int
6478 is_comdat_die (dw_die_ref c)
6480 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6481 we do for stabs. The advantage is a greater likelihood of sharing between
6482 objects that don't include headers in the same order (and therefore would
6483 put the base types in a different comdat). jason 8/28/00 */
6485 if (c->die_tag == DW_TAG_base_type)
6486 return 0;
6488 if (c->die_tag == DW_TAG_pointer_type
6489 || c->die_tag == DW_TAG_reference_type
6490 || c->die_tag == DW_TAG_rvalue_reference_type
6491 || c->die_tag == DW_TAG_const_type
6492 || c->die_tag == DW_TAG_volatile_type)
6494 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6496 return t ? is_comdat_die (t) : 0;
6499 return is_type_die (c);
6502 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6503 compilation unit. */
6505 static int
6506 is_symbol_die (dw_die_ref c)
6508 return (is_type_die (c)
6509 || is_declaration_die (c)
6510 || c->die_tag == DW_TAG_namespace
6511 || c->die_tag == DW_TAG_module);
6514 /* Returns true iff C is a compile-unit DIE. */
6516 static inline bool
6517 is_cu_die (dw_die_ref c)
6519 return c && c->die_tag == DW_TAG_compile_unit;
6522 /* Returns true iff C is a unit DIE of some sort. */
6524 static inline bool
6525 is_unit_die (dw_die_ref c)
6527 return c && (c->die_tag == DW_TAG_compile_unit
6528 || c->die_tag == DW_TAG_partial_unit
6529 || c->die_tag == DW_TAG_type_unit);
6532 /* Returns true iff C is a namespace DIE. */
6534 static inline bool
6535 is_namespace_die (dw_die_ref c)
6537 return c && c->die_tag == DW_TAG_namespace;
6540 /* Returns true iff C is a class or structure DIE. */
6542 static inline bool
6543 is_class_die (dw_die_ref c)
6545 return c && (c->die_tag == DW_TAG_class_type
6546 || c->die_tag == DW_TAG_structure_type);
6549 /* Return non-zero if this DIE is a template parameter. */
6551 static inline bool
6552 is_template_parameter (dw_die_ref die)
6554 switch (die->die_tag)
6556 case DW_TAG_template_type_param:
6557 case DW_TAG_template_value_param:
6558 case DW_TAG_GNU_template_template_param:
6559 case DW_TAG_GNU_template_parameter_pack:
6560 return true;
6561 default:
6562 return false;
6566 /* Return non-zero if this DIE represents a template instantiation. */
6568 static inline bool
6569 is_template_instantiation (dw_die_ref die)
6571 dw_die_ref c;
6573 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6574 return false;
6575 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6576 return false;
6579 static char *
6580 gen_internal_sym (const char *prefix)
6582 char buf[256];
6584 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6585 return xstrdup (buf);
6588 /* Assign symbols to all worthy DIEs under DIE. */
6590 static void
6591 assign_symbol_names (dw_die_ref die)
6593 dw_die_ref c;
6595 if (is_symbol_die (die) && !die->comdat_type_p)
6597 if (comdat_symbol_id)
6599 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6601 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6602 comdat_symbol_id, comdat_symbol_number++);
6603 die->die_id.die_symbol = xstrdup (p);
6605 else
6606 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6609 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6612 struct cu_hash_table_entry
6614 dw_die_ref cu;
6615 unsigned min_comdat_num, max_comdat_num;
6616 struct cu_hash_table_entry *next;
6619 /* Helpers to manipulate hash table of CUs. */
6621 struct cu_hash_table_entry_hasher
6623 typedef cu_hash_table_entry value_type;
6624 typedef die_struct compare_type;
6625 static inline hashval_t hash (const value_type *);
6626 static inline bool equal (const value_type *, const compare_type *);
6627 static inline void remove (value_type *);
6630 inline hashval_t
6631 cu_hash_table_entry_hasher::hash (const value_type *entry)
6633 return htab_hash_string (entry->cu->die_id.die_symbol);
6636 inline bool
6637 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6638 const compare_type *entry2)
6640 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6643 inline void
6644 cu_hash_table_entry_hasher::remove (value_type *entry)
6646 struct cu_hash_table_entry *next;
6648 while (entry)
6650 next = entry->next;
6651 free (entry);
6652 entry = next;
6656 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6658 /* Check whether we have already seen this CU and set up SYM_NUM
6659 accordingly. */
6660 static int
6661 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6663 struct cu_hash_table_entry dummy;
6664 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6666 dummy.max_comdat_num = 0;
6668 slot = htable.find_slot_with_hash (cu,
6669 htab_hash_string (cu->die_id.die_symbol),
6670 INSERT);
6671 entry = *slot;
6673 for (; entry; last = entry, entry = entry->next)
6675 if (same_die_p_wrap (cu, entry->cu))
6676 break;
6679 if (entry)
6681 *sym_num = entry->min_comdat_num;
6682 return 1;
6685 entry = XCNEW (struct cu_hash_table_entry);
6686 entry->cu = cu;
6687 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6688 entry->next = *slot;
6689 *slot = entry;
6691 return 0;
6694 /* Record SYM_NUM to record of CU in HTABLE. */
6695 static void
6696 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6697 unsigned int sym_num)
6699 struct cu_hash_table_entry **slot, *entry;
6701 slot = htable.find_slot_with_hash (cu,
6702 htab_hash_string (cu->die_id.die_symbol),
6703 NO_INSERT);
6704 entry = *slot;
6706 entry->max_comdat_num = sym_num;
6709 /* Traverse the DIE (which is always comp_unit_die), and set up
6710 additional compilation units for each of the include files we see
6711 bracketed by BINCL/EINCL. */
6713 static void
6714 break_out_includes (dw_die_ref die)
6716 dw_die_ref c;
6717 dw_die_ref unit = NULL;
6718 limbo_die_node *node, **pnode;
6719 cu_hash_type cu_hash_table;
6721 c = die->die_child;
6722 if (c) do {
6723 dw_die_ref prev = c;
6724 c = c->die_sib;
6725 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6726 || (unit && is_comdat_die (c)))
6728 dw_die_ref next = c->die_sib;
6730 /* This DIE is for a secondary CU; remove it from the main one. */
6731 remove_child_with_prev (c, prev);
6733 if (c->die_tag == DW_TAG_GNU_BINCL)
6734 unit = push_new_compile_unit (unit, c);
6735 else if (c->die_tag == DW_TAG_GNU_EINCL)
6736 unit = pop_compile_unit (unit);
6737 else
6738 add_child_die (unit, c);
6739 c = next;
6740 if (c == die->die_child)
6741 break;
6743 } while (c != die->die_child);
6745 #if 0
6746 /* We can only use this in debugging, since the frontend doesn't check
6747 to make sure that we leave every include file we enter. */
6748 gcc_assert (!unit);
6749 #endif
6751 assign_symbol_names (die);
6752 cu_hash_table.create (10);
6753 for (node = limbo_die_list, pnode = &limbo_die_list;
6754 node;
6755 node = node->next)
6757 int is_dupl;
6759 compute_section_prefix (node->die);
6760 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6761 &comdat_symbol_number);
6762 assign_symbol_names (node->die);
6763 if (is_dupl)
6764 *pnode = node->next;
6765 else
6767 pnode = &node->next;
6768 record_comdat_symbol_number (node->die, cu_hash_table,
6769 comdat_symbol_number);
6772 cu_hash_table.dispose ();
6775 /* Return non-zero if this DIE is a declaration. */
6777 static int
6778 is_declaration_die (dw_die_ref die)
6780 dw_attr_ref a;
6781 unsigned ix;
6783 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6784 if (a->dw_attr == DW_AT_declaration)
6785 return 1;
6787 return 0;
6790 /* Return non-zero if this DIE is nested inside a subprogram. */
6792 static int
6793 is_nested_in_subprogram (dw_die_ref die)
6795 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6797 if (decl == NULL)
6798 decl = die;
6799 return local_scope_p (decl);
6802 /* Return non-zero if this DIE contains a defining declaration of a
6803 subprogram. */
6805 static int
6806 contains_subprogram_definition (dw_die_ref die)
6808 dw_die_ref c;
6810 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6811 return 1;
6812 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6813 return 0;
6816 /* Return non-zero if this is a type DIE that should be moved to a
6817 COMDAT .debug_types section. */
6819 static int
6820 should_move_die_to_comdat (dw_die_ref die)
6822 switch (die->die_tag)
6824 case DW_TAG_class_type:
6825 case DW_TAG_structure_type:
6826 case DW_TAG_enumeration_type:
6827 case DW_TAG_union_type:
6828 /* Don't move declarations, inlined instances, or types nested in a
6829 subprogram. */
6830 if (is_declaration_die (die)
6831 || get_AT (die, DW_AT_abstract_origin)
6832 || is_nested_in_subprogram (die))
6833 return 0;
6834 /* A type definition should never contain a subprogram definition. */
6835 gcc_assert (!contains_subprogram_definition (die));
6836 return 1;
6837 case DW_TAG_array_type:
6838 case DW_TAG_interface_type:
6839 case DW_TAG_pointer_type:
6840 case DW_TAG_reference_type:
6841 case DW_TAG_rvalue_reference_type:
6842 case DW_TAG_string_type:
6843 case DW_TAG_subroutine_type:
6844 case DW_TAG_ptr_to_member_type:
6845 case DW_TAG_set_type:
6846 case DW_TAG_subrange_type:
6847 case DW_TAG_base_type:
6848 case DW_TAG_const_type:
6849 case DW_TAG_file_type:
6850 case DW_TAG_packed_type:
6851 case DW_TAG_volatile_type:
6852 case DW_TAG_typedef:
6853 default:
6854 return 0;
6858 /* Make a clone of DIE. */
6860 static dw_die_ref
6861 clone_die (dw_die_ref die)
6863 dw_die_ref clone;
6864 dw_attr_ref a;
6865 unsigned ix;
6867 clone = ggc_alloc_cleared_die_node ();
6868 clone->die_tag = die->die_tag;
6870 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6871 add_dwarf_attr (clone, a);
6873 return clone;
6876 /* Make a clone of the tree rooted at DIE. */
6878 static dw_die_ref
6879 clone_tree (dw_die_ref die)
6881 dw_die_ref c;
6882 dw_die_ref clone = clone_die (die);
6884 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6886 return clone;
6889 /* Make a clone of DIE as a declaration. */
6891 static dw_die_ref
6892 clone_as_declaration (dw_die_ref die)
6894 dw_die_ref clone;
6895 dw_die_ref decl;
6896 dw_attr_ref a;
6897 unsigned ix;
6899 /* If the DIE is already a declaration, just clone it. */
6900 if (is_declaration_die (die))
6901 return clone_die (die);
6903 /* If the DIE is a specification, just clone its declaration DIE. */
6904 decl = get_AT_ref (die, DW_AT_specification);
6905 if (decl != NULL)
6907 clone = clone_die (decl);
6908 if (die->comdat_type_p)
6909 add_AT_die_ref (clone, DW_AT_signature, die);
6910 return clone;
6913 clone = ggc_alloc_cleared_die_node ();
6914 clone->die_tag = die->die_tag;
6916 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6918 /* We don't want to copy over all attributes.
6919 For example we don't want DW_AT_byte_size because otherwise we will no
6920 longer have a declaration and GDB will treat it as a definition. */
6922 switch (a->dw_attr)
6924 case DW_AT_artificial:
6925 case DW_AT_containing_type:
6926 case DW_AT_external:
6927 case DW_AT_name:
6928 case DW_AT_type:
6929 case DW_AT_virtuality:
6930 case DW_AT_linkage_name:
6931 case DW_AT_MIPS_linkage_name:
6932 add_dwarf_attr (clone, a);
6933 break;
6934 case DW_AT_byte_size:
6935 default:
6936 break;
6940 if (die->comdat_type_p)
6941 add_AT_die_ref (clone, DW_AT_signature, die);
6943 add_AT_flag (clone, DW_AT_declaration, 1);
6944 return clone;
6948 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6950 struct decl_table_entry
6952 dw_die_ref orig;
6953 dw_die_ref copy;
6956 /* Helpers to manipulate hash table of copied declarations. */
6958 /* Hashtable helpers. */
6960 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6962 typedef decl_table_entry value_type;
6963 typedef die_struct compare_type;
6964 static inline hashval_t hash (const value_type *);
6965 static inline bool equal (const value_type *, const compare_type *);
6968 inline hashval_t
6969 decl_table_entry_hasher::hash (const value_type *entry)
6971 return htab_hash_pointer (entry->orig);
6974 inline bool
6975 decl_table_entry_hasher::equal (const value_type *entry1,
6976 const compare_type *entry2)
6978 return entry1->orig == entry2;
6981 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6983 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6984 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6985 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6986 to check if the ancestor has already been copied into UNIT. */
6988 static dw_die_ref
6989 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6991 dw_die_ref parent = die->die_parent;
6992 dw_die_ref new_parent = unit;
6993 dw_die_ref copy;
6994 decl_table_entry **slot = NULL;
6995 struct decl_table_entry *entry = NULL;
6997 if (decl_table.is_created ())
6999 /* Check if the entry has already been copied to UNIT. */
7000 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7001 INSERT);
7002 if (*slot != HTAB_EMPTY_ENTRY)
7004 entry = *slot;
7005 return entry->copy;
7008 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7009 entry = XCNEW (struct decl_table_entry);
7010 entry->orig = die;
7011 entry->copy = NULL;
7012 *slot = entry;
7015 if (parent != NULL)
7017 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7018 if (spec != NULL)
7019 parent = spec;
7020 if (!is_unit_die (parent))
7021 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7024 copy = clone_as_declaration (die);
7025 add_child_die (new_parent, copy);
7027 if (decl_table.is_created ())
7029 /* Record the pointer to the copy. */
7030 entry->copy = copy;
7033 return copy;
7035 /* Copy the declaration context to the new type unit DIE. This includes
7036 any surrounding namespace or type declarations. If the DIE has an
7037 AT_specification attribute, it also includes attributes and children
7038 attached to the specification, and returns a pointer to the original
7039 parent of the declaration DIE. Returns NULL otherwise. */
7041 static dw_die_ref
7042 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7044 dw_die_ref decl;
7045 dw_die_ref new_decl;
7046 dw_die_ref orig_parent = NULL;
7048 decl = get_AT_ref (die, DW_AT_specification);
7049 if (decl == NULL)
7050 decl = die;
7051 else
7053 unsigned ix;
7054 dw_die_ref c;
7055 dw_attr_ref a;
7057 /* The original DIE will be changed to a declaration, and must
7058 be moved to be a child of the original declaration DIE. */
7059 orig_parent = decl->die_parent;
7061 /* Copy the type node pointer from the new DIE to the original
7062 declaration DIE so we can forward references later. */
7063 decl->comdat_type_p = true;
7064 decl->die_id.die_type_node = die->die_id.die_type_node;
7066 remove_AT (die, DW_AT_specification);
7068 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7070 if (a->dw_attr != DW_AT_name
7071 && a->dw_attr != DW_AT_declaration
7072 && a->dw_attr != DW_AT_external)
7073 add_dwarf_attr (die, a);
7076 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7079 if (decl->die_parent != NULL
7080 && !is_unit_die (decl->die_parent))
7082 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7083 if (new_decl != NULL)
7085 remove_AT (new_decl, DW_AT_signature);
7086 add_AT_specification (die, new_decl);
7090 return orig_parent;
7093 /* Generate the skeleton ancestor tree for the given NODE, then clone
7094 the DIE and add the clone into the tree. */
7096 static void
7097 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7099 if (node->new_die != NULL)
7100 return;
7102 node->new_die = clone_as_declaration (node->old_die);
7104 if (node->parent != NULL)
7106 generate_skeleton_ancestor_tree (node->parent);
7107 add_child_die (node->parent->new_die, node->new_die);
7111 /* Generate a skeleton tree of DIEs containing any declarations that are
7112 found in the original tree. We traverse the tree looking for declaration
7113 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7115 static void
7116 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7118 skeleton_chain_node node;
7119 dw_die_ref c;
7120 dw_die_ref first;
7121 dw_die_ref prev = NULL;
7122 dw_die_ref next = NULL;
7124 node.parent = parent;
7126 first = c = parent->old_die->die_child;
7127 if (c)
7128 next = c->die_sib;
7129 if (c) do {
7130 if (prev == NULL || prev->die_sib == c)
7131 prev = c;
7132 c = next;
7133 next = (c == first ? NULL : c->die_sib);
7134 node.old_die = c;
7135 node.new_die = NULL;
7136 if (is_declaration_die (c))
7138 if (is_template_instantiation (c))
7140 /* Instantiated templates do not need to be cloned into the
7141 type unit. Just move the DIE and its children back to
7142 the skeleton tree (in the main CU). */
7143 remove_child_with_prev (c, prev);
7144 add_child_die (parent->new_die, c);
7145 c = prev;
7147 else
7149 /* Clone the existing DIE, move the original to the skeleton
7150 tree (which is in the main CU), and put the clone, with
7151 all the original's children, where the original came from
7152 (which is about to be moved to the type unit). */
7153 dw_die_ref clone = clone_die (c);
7154 move_all_children (c, clone);
7156 replace_child (c, clone, prev);
7157 generate_skeleton_ancestor_tree (parent);
7158 add_child_die (parent->new_die, c);
7159 node.new_die = c;
7160 c = clone;
7163 generate_skeleton_bottom_up (&node);
7164 } while (next != NULL);
7167 /* Wrapper function for generate_skeleton_bottom_up. */
7169 static dw_die_ref
7170 generate_skeleton (dw_die_ref die)
7172 skeleton_chain_node node;
7174 node.old_die = die;
7175 node.new_die = NULL;
7176 node.parent = NULL;
7178 /* If this type definition is nested inside another type,
7179 and is not an instantiation of a template, always leave
7180 at least a declaration in its place. */
7181 if (die->die_parent != NULL
7182 && is_type_die (die->die_parent)
7183 && !is_template_instantiation (die))
7184 node.new_die = clone_as_declaration (die);
7186 generate_skeleton_bottom_up (&node);
7187 return node.new_die;
7190 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7191 declaration. The original DIE is moved to a new compile unit so that
7192 existing references to it follow it to the new location. If any of the
7193 original DIE's descendants is a declaration, we need to replace the
7194 original DIE with a skeleton tree and move the declarations back into the
7195 skeleton tree. */
7197 static dw_die_ref
7198 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7199 dw_die_ref prev)
7201 dw_die_ref skeleton, orig_parent;
7203 /* Copy the declaration context to the type unit DIE. If the returned
7204 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7205 that DIE. */
7206 orig_parent = copy_declaration_context (unit, child);
7208 skeleton = generate_skeleton (child);
7209 if (skeleton == NULL)
7210 remove_child_with_prev (child, prev);
7211 else
7213 skeleton->comdat_type_p = true;
7214 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7216 /* If the original DIE was a specification, we need to put
7217 the skeleton under the parent DIE of the declaration.
7218 This leaves the original declaration in the tree, but
7219 it will be pruned later since there are no longer any
7220 references to it. */
7221 if (orig_parent != NULL)
7223 remove_child_with_prev (child, prev);
7224 add_child_die (orig_parent, skeleton);
7226 else
7227 replace_child (child, skeleton, prev);
7230 return skeleton;
7233 /* Traverse the DIE and set up additional .debug_types sections for each
7234 type worthy of being placed in a COMDAT section. */
7236 static void
7237 break_out_comdat_types (dw_die_ref die)
7239 dw_die_ref c;
7240 dw_die_ref first;
7241 dw_die_ref prev = NULL;
7242 dw_die_ref next = NULL;
7243 dw_die_ref unit = NULL;
7245 first = c = die->die_child;
7246 if (c)
7247 next = c->die_sib;
7248 if (c) do {
7249 if (prev == NULL || prev->die_sib == c)
7250 prev = c;
7251 c = next;
7252 next = (c == first ? NULL : c->die_sib);
7253 if (should_move_die_to_comdat (c))
7255 dw_die_ref replacement;
7256 comdat_type_node_ref type_node;
7258 /* Break out nested types into their own type units. */
7259 break_out_comdat_types (c);
7261 /* Create a new type unit DIE as the root for the new tree, and
7262 add it to the list of comdat types. */
7263 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7264 add_AT_unsigned (unit, DW_AT_language,
7265 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7266 type_node = ggc_alloc_cleared_comdat_type_node ();
7267 type_node->root_die = unit;
7268 type_node->next = comdat_type_list;
7269 comdat_type_list = type_node;
7271 /* Generate the type signature. */
7272 generate_type_signature (c, type_node);
7274 /* Copy the declaration context, attributes, and children of the
7275 declaration into the new type unit DIE, then remove this DIE
7276 from the main CU (or replace it with a skeleton if necessary). */
7277 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7278 type_node->skeleton_die = replacement;
7280 /* Add the DIE to the new compunit. */
7281 add_child_die (unit, c);
7283 if (replacement != NULL)
7284 c = replacement;
7286 else if (c->die_tag == DW_TAG_namespace
7287 || c->die_tag == DW_TAG_class_type
7288 || c->die_tag == DW_TAG_structure_type
7289 || c->die_tag == DW_TAG_union_type)
7291 /* Look for nested types that can be broken out. */
7292 break_out_comdat_types (c);
7294 } while (next != NULL);
7297 /* Like clone_tree, but additionally enter all the children into
7298 the hash table decl_table. */
7300 static dw_die_ref
7301 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7303 dw_die_ref c;
7304 dw_die_ref clone = clone_die (die);
7305 struct decl_table_entry *entry;
7306 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7307 htab_hash_pointer (die), INSERT);
7308 /* Assert that DIE isn't in the hash table yet. If it would be there
7309 before, the ancestors would be necessarily there as well, therefore
7310 clone_tree_hash wouldn't be called. */
7311 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7312 entry = XCNEW (struct decl_table_entry);
7313 entry->orig = die;
7314 entry->copy = clone;
7315 *slot = entry;
7317 FOR_EACH_CHILD (die, c,
7318 add_child_die (clone, clone_tree_hash (c, decl_table)));
7320 return clone;
7323 /* Walk the DIE and its children, looking for references to incomplete
7324 or trivial types that are unmarked (i.e., that are not in the current
7325 type_unit). */
7327 static void
7328 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7330 dw_die_ref c;
7331 dw_attr_ref a;
7332 unsigned ix;
7334 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7336 if (AT_class (a) == dw_val_class_die_ref)
7338 dw_die_ref targ = AT_ref (a);
7339 decl_table_entry **slot;
7340 struct decl_table_entry *entry;
7342 if (targ->die_mark != 0 || targ->comdat_type_p)
7343 continue;
7345 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7346 INSERT);
7348 if (*slot != HTAB_EMPTY_ENTRY)
7350 /* TARG has already been copied, so we just need to
7351 modify the reference to point to the copy. */
7352 entry = *slot;
7353 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7355 else
7357 dw_die_ref parent = unit;
7358 dw_die_ref copy = clone_die (targ);
7360 /* Record in DECL_TABLE that TARG has been copied.
7361 Need to do this now, before the recursive call,
7362 because DECL_TABLE may be expanded and SLOT
7363 would no longer be a valid pointer. */
7364 entry = XCNEW (struct decl_table_entry);
7365 entry->orig = targ;
7366 entry->copy = copy;
7367 *slot = entry;
7369 FOR_EACH_CHILD (targ, c,
7370 add_child_die (copy,
7371 clone_tree_hash (c, decl_table)));
7373 /* Make sure the cloned tree is marked as part of the
7374 type unit. */
7375 mark_dies (copy);
7377 /* If TARG has surrounding context, copy its ancestor tree
7378 into the new type unit. */
7379 if (targ->die_parent != NULL
7380 && !is_unit_die (targ->die_parent))
7381 parent = copy_ancestor_tree (unit, targ->die_parent,
7382 decl_table);
7384 add_child_die (parent, copy);
7385 a->dw_attr_val.v.val_die_ref.die = copy;
7387 /* Make sure the newly-copied DIE is walked. If it was
7388 installed in a previously-added context, it won't
7389 get visited otherwise. */
7390 if (parent != unit)
7392 /* Find the highest point of the newly-added tree,
7393 mark each node along the way, and walk from there. */
7394 parent->die_mark = 1;
7395 while (parent->die_parent
7396 && parent->die_parent->die_mark == 0)
7398 parent = parent->die_parent;
7399 parent->die_mark = 1;
7401 copy_decls_walk (unit, parent, decl_table);
7407 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7410 /* Copy declarations for "unworthy" types into the new comdat section.
7411 Incomplete types, modified types, and certain other types aren't broken
7412 out into comdat sections of their own, so they don't have a signature,
7413 and we need to copy the declaration into the same section so that we
7414 don't have an external reference. */
7416 static void
7417 copy_decls_for_unworthy_types (dw_die_ref unit)
7419 decl_hash_type decl_table;
7421 mark_dies (unit);
7422 decl_table.create (10);
7423 copy_decls_walk (unit, unit, decl_table);
7424 decl_table.dispose ();
7425 unmark_dies (unit);
7428 /* Traverse the DIE and add a sibling attribute if it may have the
7429 effect of speeding up access to siblings. To save some space,
7430 avoid generating sibling attributes for DIE's without children. */
7432 static void
7433 add_sibling_attributes (dw_die_ref die)
7435 dw_die_ref c;
7437 if (! die->die_child)
7438 return;
7440 if (die->die_parent && die != die->die_parent->die_child)
7441 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7443 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7446 /* Output all location lists for the DIE and its children. */
7448 static void
7449 output_location_lists (dw_die_ref die)
7451 dw_die_ref c;
7452 dw_attr_ref a;
7453 unsigned ix;
7455 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7456 if (AT_class (a) == dw_val_class_loc_list)
7457 output_loc_list (AT_loc_list (a));
7459 FOR_EACH_CHILD (die, c, output_location_lists (c));
7462 /* We want to limit the number of external references, because they are
7463 larger than local references: a relocation takes multiple words, and
7464 even a sig8 reference is always eight bytes, whereas a local reference
7465 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7466 So if we encounter multiple external references to the same type DIE, we
7467 make a local typedef stub for it and redirect all references there.
7469 This is the element of the hash table for keeping track of these
7470 references. */
7472 struct external_ref
7474 dw_die_ref type;
7475 dw_die_ref stub;
7476 unsigned n_refs;
7479 /* Hashtable helpers. */
7481 struct external_ref_hasher : typed_free_remove <external_ref>
7483 typedef external_ref value_type;
7484 typedef external_ref compare_type;
7485 static inline hashval_t hash (const value_type *);
7486 static inline bool equal (const value_type *, const compare_type *);
7489 inline hashval_t
7490 external_ref_hasher::hash (const value_type *r)
7492 dw_die_ref die = r->type;
7493 hashval_t h = 0;
7495 /* We can't use the address of the DIE for hashing, because
7496 that will make the order of the stub DIEs non-deterministic. */
7497 if (! die->comdat_type_p)
7498 /* We have a symbol; use it to compute a hash. */
7499 h = htab_hash_string (die->die_id.die_symbol);
7500 else
7502 /* We have a type signature; use a subset of the bits as the hash.
7503 The 8-byte signature is at least as large as hashval_t. */
7504 comdat_type_node_ref type_node = die->die_id.die_type_node;
7505 memcpy (&h, type_node->signature, sizeof (h));
7507 return h;
7510 inline bool
7511 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7513 return r1->type == r2->type;
7516 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7518 /* Return a pointer to the external_ref for references to DIE. */
7520 static struct external_ref *
7521 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7523 struct external_ref ref, *ref_p;
7524 external_ref **slot;
7526 ref.type = die;
7527 slot = map.find_slot (&ref, INSERT);
7528 if (*slot != HTAB_EMPTY_ENTRY)
7529 return *slot;
7531 ref_p = XCNEW (struct external_ref);
7532 ref_p->type = die;
7533 *slot = ref_p;
7534 return ref_p;
7537 /* Subroutine of optimize_external_refs, below.
7539 If we see a type skeleton, record it as our stub. If we see external
7540 references, remember how many we've seen. */
7542 static void
7543 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7545 dw_die_ref c;
7546 dw_attr_ref a;
7547 unsigned ix;
7548 struct external_ref *ref_p;
7550 if (is_type_die (die)
7551 && (c = get_AT_ref (die, DW_AT_signature)))
7553 /* This is a local skeleton; use it for local references. */
7554 ref_p = lookup_external_ref (map, c);
7555 ref_p->stub = die;
7558 /* Scan the DIE references, and remember any that refer to DIEs from
7559 other CUs (i.e. those which are not marked). */
7560 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7561 if (AT_class (a) == dw_val_class_die_ref
7562 && (c = AT_ref (a))->die_mark == 0
7563 && is_type_die (c))
7565 ref_p = lookup_external_ref (map, c);
7566 ref_p->n_refs++;
7569 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7572 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7573 points to an external_ref, DATA is the CU we're processing. If we don't
7574 already have a local stub, and we have multiple refs, build a stub. */
7577 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7579 struct external_ref *ref_p = *slot;
7581 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7583 /* We have multiple references to this type, so build a small stub.
7584 Both of these forms are a bit dodgy from the perspective of the
7585 DWARF standard, since technically they should have names. */
7586 dw_die_ref cu = data;
7587 dw_die_ref type = ref_p->type;
7588 dw_die_ref stub = NULL;
7590 if (type->comdat_type_p)
7592 /* If we refer to this type via sig8, use AT_signature. */
7593 stub = new_die (type->die_tag, cu, NULL_TREE);
7594 add_AT_die_ref (stub, DW_AT_signature, type);
7596 else
7598 /* Otherwise, use a typedef with no name. */
7599 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7600 add_AT_die_ref (stub, DW_AT_type, type);
7603 stub->die_mark++;
7604 ref_p->stub = stub;
7606 return 1;
7609 /* DIE is a unit; look through all the DIE references to see if there are
7610 any external references to types, and if so, create local stubs for
7611 them which will be applied in build_abbrev_table. This is useful because
7612 references to local DIEs are smaller. */
7614 static external_ref_hash_type
7615 optimize_external_refs (dw_die_ref die)
7617 external_ref_hash_type map;
7618 map.create (10);
7619 optimize_external_refs_1 (die, map);
7620 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7621 return map;
7624 /* The format of each DIE (and its attribute value pairs) is encoded in an
7625 abbreviation table. This routine builds the abbreviation table and assigns
7626 a unique abbreviation id for each abbreviation entry. The children of each
7627 die are visited recursively. */
7629 static void
7630 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7632 unsigned long abbrev_id;
7633 unsigned int n_alloc;
7634 dw_die_ref c;
7635 dw_attr_ref a;
7636 unsigned ix;
7638 /* Scan the DIE references, and replace any that refer to
7639 DIEs from other CUs (i.e. those which are not marked) with
7640 the local stubs we built in optimize_external_refs. */
7641 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7642 if (AT_class (a) == dw_val_class_die_ref
7643 && (c = AT_ref (a))->die_mark == 0)
7645 struct external_ref *ref_p;
7646 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7648 ref_p = lookup_external_ref (extern_map, c);
7649 if (ref_p->stub && ref_p->stub != die)
7650 change_AT_die_ref (a, ref_p->stub);
7651 else
7652 /* We aren't changing this reference, so mark it external. */
7653 set_AT_ref_external (a, 1);
7656 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7658 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7659 dw_attr_ref die_a, abbrev_a;
7660 unsigned ix;
7661 bool ok = true;
7663 if (abbrev->die_tag != die->die_tag)
7664 continue;
7665 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7666 continue;
7668 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7669 continue;
7671 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7673 abbrev_a = &(*abbrev->die_attr)[ix];
7674 if ((abbrev_a->dw_attr != die_a->dw_attr)
7675 || (value_format (abbrev_a) != value_format (die_a)))
7677 ok = false;
7678 break;
7681 if (ok)
7682 break;
7685 if (abbrev_id >= abbrev_die_table_in_use)
7687 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7689 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7690 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7691 n_alloc);
7693 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7694 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7695 abbrev_die_table_allocated = n_alloc;
7698 ++abbrev_die_table_in_use;
7699 abbrev_die_table[abbrev_id] = die;
7702 die->die_abbrev = abbrev_id;
7703 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7706 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7708 static int
7709 constant_size (unsigned HOST_WIDE_INT value)
7711 int log;
7713 if (value == 0)
7714 log = 0;
7715 else
7716 log = floor_log2 (value);
7718 log = log / 8;
7719 log = 1 << (floor_log2 (log) + 1);
7721 return log;
7724 /* Return the size of a DIE as it is represented in the
7725 .debug_info section. */
7727 static unsigned long
7728 size_of_die (dw_die_ref die)
7730 unsigned long size = 0;
7731 dw_attr_ref a;
7732 unsigned ix;
7733 enum dwarf_form form;
7735 size += size_of_uleb128 (die->die_abbrev);
7736 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7738 switch (AT_class (a))
7740 case dw_val_class_addr:
7741 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7743 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7744 size += size_of_uleb128 (AT_index (a));
7746 else
7747 size += DWARF2_ADDR_SIZE;
7748 break;
7749 case dw_val_class_offset:
7750 size += DWARF_OFFSET_SIZE;
7751 break;
7752 case dw_val_class_loc:
7754 unsigned long lsize = size_of_locs (AT_loc (a));
7756 /* Block length. */
7757 if (dwarf_version >= 4)
7758 size += size_of_uleb128 (lsize);
7759 else
7760 size += constant_size (lsize);
7761 size += lsize;
7763 break;
7764 case dw_val_class_loc_list:
7765 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7767 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7768 size += size_of_uleb128 (AT_index (a));
7770 else
7771 size += DWARF_OFFSET_SIZE;
7772 break;
7773 case dw_val_class_range_list:
7774 size += DWARF_OFFSET_SIZE;
7775 break;
7776 case dw_val_class_const:
7777 size += size_of_sleb128 (AT_int (a));
7778 break;
7779 case dw_val_class_unsigned_const:
7781 int csize = constant_size (AT_unsigned (a));
7782 if (dwarf_version == 3
7783 && a->dw_attr == DW_AT_data_member_location
7784 && csize >= 4)
7785 size += size_of_uleb128 (AT_unsigned (a));
7786 else
7787 size += csize;
7789 break;
7790 case dw_val_class_const_double:
7791 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7792 if (HOST_BITS_PER_WIDE_INT >= 64)
7793 size++; /* block */
7794 break;
7795 case dw_val_class_vec:
7796 size += constant_size (a->dw_attr_val.v.val_vec.length
7797 * a->dw_attr_val.v.val_vec.elt_size)
7798 + a->dw_attr_val.v.val_vec.length
7799 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7800 break;
7801 case dw_val_class_flag:
7802 if (dwarf_version >= 4)
7803 /* Currently all add_AT_flag calls pass in 1 as last argument,
7804 so DW_FORM_flag_present can be used. If that ever changes,
7805 we'll need to use DW_FORM_flag and have some optimization
7806 in build_abbrev_table that will change those to
7807 DW_FORM_flag_present if it is set to 1 in all DIEs using
7808 the same abbrev entry. */
7809 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7810 else
7811 size += 1;
7812 break;
7813 case dw_val_class_die_ref:
7814 if (AT_ref_external (a))
7816 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7817 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7818 is sized by target address length, whereas in DWARF3
7819 it's always sized as an offset. */
7820 if (use_debug_types)
7821 size += DWARF_TYPE_SIGNATURE_SIZE;
7822 else if (dwarf_version == 2)
7823 size += DWARF2_ADDR_SIZE;
7824 else
7825 size += DWARF_OFFSET_SIZE;
7827 else
7828 size += DWARF_OFFSET_SIZE;
7829 break;
7830 case dw_val_class_fde_ref:
7831 size += DWARF_OFFSET_SIZE;
7832 break;
7833 case dw_val_class_lbl_id:
7834 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7836 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7837 size += size_of_uleb128 (AT_index (a));
7839 else
7840 size += DWARF2_ADDR_SIZE;
7841 break;
7842 case dw_val_class_lineptr:
7843 case dw_val_class_macptr:
7844 size += DWARF_OFFSET_SIZE;
7845 break;
7846 case dw_val_class_str:
7847 form = AT_string_form (a);
7848 if (form == DW_FORM_strp)
7849 size += DWARF_OFFSET_SIZE;
7850 else if (form == DW_FORM_GNU_str_index)
7851 size += size_of_uleb128 (AT_index (a));
7852 else
7853 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7854 break;
7855 case dw_val_class_file:
7856 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7857 break;
7858 case dw_val_class_data8:
7859 size += 8;
7860 break;
7861 case dw_val_class_vms_delta:
7862 size += DWARF_OFFSET_SIZE;
7863 break;
7864 case dw_val_class_high_pc:
7865 size += DWARF2_ADDR_SIZE;
7866 break;
7867 default:
7868 gcc_unreachable ();
7872 return size;
7875 /* Size the debugging information associated with a given DIE. Visits the
7876 DIE's children recursively. Updates the global variable next_die_offset, on
7877 each time through. Uses the current value of next_die_offset to update the
7878 die_offset field in each DIE. */
7880 static void
7881 calc_die_sizes (dw_die_ref die)
7883 dw_die_ref c;
7885 gcc_assert (die->die_offset == 0
7886 || (unsigned long int) die->die_offset == next_die_offset);
7887 die->die_offset = next_die_offset;
7888 next_die_offset += size_of_die (die);
7890 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7892 if (die->die_child != NULL)
7893 /* Count the null byte used to terminate sibling lists. */
7894 next_die_offset += 1;
7897 /* Size just the base type children at the start of the CU.
7898 This is needed because build_abbrev needs to size locs
7899 and sizing of type based stack ops needs to know die_offset
7900 values for the base types. */
7902 static void
7903 calc_base_type_die_sizes (void)
7905 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7906 unsigned int i;
7907 dw_die_ref base_type;
7908 #if ENABLE_ASSERT_CHECKING
7909 dw_die_ref prev = comp_unit_die ()->die_child;
7910 #endif
7912 die_offset += size_of_die (comp_unit_die ());
7913 for (i = 0; base_types.iterate (i, &base_type); i++)
7915 #if ENABLE_ASSERT_CHECKING
7916 gcc_assert (base_type->die_offset == 0
7917 && prev->die_sib == base_type
7918 && base_type->die_child == NULL
7919 && base_type->die_abbrev);
7920 prev = base_type;
7921 #endif
7922 base_type->die_offset = die_offset;
7923 die_offset += size_of_die (base_type);
7927 /* Set the marks for a die and its children. We do this so
7928 that we know whether or not a reference needs to use FORM_ref_addr; only
7929 DIEs in the same CU will be marked. We used to clear out the offset
7930 and use that as the flag, but ran into ordering problems. */
7932 static void
7933 mark_dies (dw_die_ref die)
7935 dw_die_ref c;
7937 gcc_assert (!die->die_mark);
7939 die->die_mark = 1;
7940 FOR_EACH_CHILD (die, c, mark_dies (c));
7943 /* Clear the marks for a die and its children. */
7945 static void
7946 unmark_dies (dw_die_ref die)
7948 dw_die_ref c;
7950 if (! use_debug_types)
7951 gcc_assert (die->die_mark);
7953 die->die_mark = 0;
7954 FOR_EACH_CHILD (die, c, unmark_dies (c));
7957 /* Clear the marks for a die, its children and referred dies. */
7959 static void
7960 unmark_all_dies (dw_die_ref die)
7962 dw_die_ref c;
7963 dw_attr_ref a;
7964 unsigned ix;
7966 if (!die->die_mark)
7967 return;
7968 die->die_mark = 0;
7970 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7972 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7973 if (AT_class (a) == dw_val_class_die_ref)
7974 unmark_all_dies (AT_ref (a));
7977 /* Calculate if the entry should appear in the final output file. It may be
7978 from a pruned a type. */
7980 static bool
7981 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7983 /* By limiting gnu pubnames to definitions only, gold can generate a
7984 gdb index without entries for declarations, which don't include
7985 enough information to be useful. */
7986 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7987 return false;
7989 if (table == pubname_table)
7991 /* Enumerator names are part of the pubname table, but the
7992 parent DW_TAG_enumeration_type die may have been pruned.
7993 Don't output them if that is the case. */
7994 if (p->die->die_tag == DW_TAG_enumerator &&
7995 (p->die->die_parent == NULL
7996 || !p->die->die_parent->die_perennial_p))
7997 return false;
7999 /* Everything else in the pubname table is included. */
8000 return true;
8003 /* The pubtypes table shouldn't include types that have been
8004 pruned. */
8005 return (p->die->die_offset != 0
8006 || !flag_eliminate_unused_debug_types);
8009 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8010 generated for the compilation unit. */
8012 static unsigned long
8013 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8015 unsigned long size;
8016 unsigned i;
8017 pubname_ref p;
8018 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8020 size = DWARF_PUBNAMES_HEADER_SIZE;
8021 FOR_EACH_VEC_ELT (*names, i, p)
8022 if (include_pubname_in_output (names, p))
8023 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8025 size += DWARF_OFFSET_SIZE;
8026 return size;
8029 /* Return the size of the information in the .debug_aranges section. */
8031 static unsigned long
8032 size_of_aranges (void)
8034 unsigned long size;
8036 size = DWARF_ARANGES_HEADER_SIZE;
8038 /* Count the address/length pair for this compilation unit. */
8039 if (text_section_used)
8040 size += 2 * DWARF2_ADDR_SIZE;
8041 if (cold_text_section_used)
8042 size += 2 * DWARF2_ADDR_SIZE;
8043 if (have_multiple_function_sections)
8045 unsigned fde_idx;
8046 dw_fde_ref fde;
8048 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8050 if (DECL_IGNORED_P (fde->decl))
8051 continue;
8052 if (!fde->in_std_section)
8053 size += 2 * DWARF2_ADDR_SIZE;
8054 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8055 size += 2 * DWARF2_ADDR_SIZE;
8059 /* Count the two zero words used to terminated the address range table. */
8060 size += 2 * DWARF2_ADDR_SIZE;
8061 return size;
8064 /* Select the encoding of an attribute value. */
8066 static enum dwarf_form
8067 value_format (dw_attr_ref a)
8069 switch (AT_class (a))
8071 case dw_val_class_addr:
8072 /* Only very few attributes allow DW_FORM_addr. */
8073 switch (a->dw_attr)
8075 case DW_AT_low_pc:
8076 case DW_AT_high_pc:
8077 case DW_AT_entry_pc:
8078 case DW_AT_trampoline:
8079 return (AT_index (a) == NOT_INDEXED
8080 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8081 default:
8082 break;
8084 switch (DWARF2_ADDR_SIZE)
8086 case 1:
8087 return DW_FORM_data1;
8088 case 2:
8089 return DW_FORM_data2;
8090 case 4:
8091 return DW_FORM_data4;
8092 case 8:
8093 return DW_FORM_data8;
8094 default:
8095 gcc_unreachable ();
8097 case dw_val_class_range_list:
8098 case dw_val_class_loc_list:
8099 if (dwarf_version >= 4)
8100 return DW_FORM_sec_offset;
8101 /* FALLTHRU */
8102 case dw_val_class_vms_delta:
8103 case dw_val_class_offset:
8104 switch (DWARF_OFFSET_SIZE)
8106 case 4:
8107 return DW_FORM_data4;
8108 case 8:
8109 return DW_FORM_data8;
8110 default:
8111 gcc_unreachable ();
8113 case dw_val_class_loc:
8114 if (dwarf_version >= 4)
8115 return DW_FORM_exprloc;
8116 switch (constant_size (size_of_locs (AT_loc (a))))
8118 case 1:
8119 return DW_FORM_block1;
8120 case 2:
8121 return DW_FORM_block2;
8122 case 4:
8123 return DW_FORM_block4;
8124 default:
8125 gcc_unreachable ();
8127 case dw_val_class_const:
8128 return DW_FORM_sdata;
8129 case dw_val_class_unsigned_const:
8130 switch (constant_size (AT_unsigned (a)))
8132 case 1:
8133 return DW_FORM_data1;
8134 case 2:
8135 return DW_FORM_data2;
8136 case 4:
8137 /* In DWARF3 DW_AT_data_member_location with
8138 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8139 constant, so we need to use DW_FORM_udata if we need
8140 a large constant. */
8141 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8142 return DW_FORM_udata;
8143 return DW_FORM_data4;
8144 case 8:
8145 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8146 return DW_FORM_udata;
8147 return DW_FORM_data8;
8148 default:
8149 gcc_unreachable ();
8151 case dw_val_class_const_double:
8152 switch (HOST_BITS_PER_WIDE_INT)
8154 case 8:
8155 return DW_FORM_data2;
8156 case 16:
8157 return DW_FORM_data4;
8158 case 32:
8159 return DW_FORM_data8;
8160 case 64:
8161 default:
8162 return DW_FORM_block1;
8164 case dw_val_class_vec:
8165 switch (constant_size (a->dw_attr_val.v.val_vec.length
8166 * a->dw_attr_val.v.val_vec.elt_size))
8168 case 1:
8169 return DW_FORM_block1;
8170 case 2:
8171 return DW_FORM_block2;
8172 case 4:
8173 return DW_FORM_block4;
8174 default:
8175 gcc_unreachable ();
8177 case dw_val_class_flag:
8178 if (dwarf_version >= 4)
8180 /* Currently all add_AT_flag calls pass in 1 as last argument,
8181 so DW_FORM_flag_present can be used. If that ever changes,
8182 we'll need to use DW_FORM_flag and have some optimization
8183 in build_abbrev_table that will change those to
8184 DW_FORM_flag_present if it is set to 1 in all DIEs using
8185 the same abbrev entry. */
8186 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8187 return DW_FORM_flag_present;
8189 return DW_FORM_flag;
8190 case dw_val_class_die_ref:
8191 if (AT_ref_external (a))
8192 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8193 else
8194 return DW_FORM_ref;
8195 case dw_val_class_fde_ref:
8196 return DW_FORM_data;
8197 case dw_val_class_lbl_id:
8198 return (AT_index (a) == NOT_INDEXED
8199 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8200 case dw_val_class_lineptr:
8201 case dw_val_class_macptr:
8202 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8203 case dw_val_class_str:
8204 return AT_string_form (a);
8205 case dw_val_class_file:
8206 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8208 case 1:
8209 return DW_FORM_data1;
8210 case 2:
8211 return DW_FORM_data2;
8212 case 4:
8213 return DW_FORM_data4;
8214 default:
8215 gcc_unreachable ();
8218 case dw_val_class_data8:
8219 return DW_FORM_data8;
8221 case dw_val_class_high_pc:
8222 switch (DWARF2_ADDR_SIZE)
8224 case 1:
8225 return DW_FORM_data1;
8226 case 2:
8227 return DW_FORM_data2;
8228 case 4:
8229 return DW_FORM_data4;
8230 case 8:
8231 return DW_FORM_data8;
8232 default:
8233 gcc_unreachable ();
8236 default:
8237 gcc_unreachable ();
8241 /* Output the encoding of an attribute value. */
8243 static void
8244 output_value_format (dw_attr_ref a)
8246 enum dwarf_form form = value_format (a);
8248 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8251 /* Given a die and id, produce the appropriate abbreviations. */
8253 static void
8254 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8256 unsigned ix;
8257 dw_attr_ref a_attr;
8259 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8260 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8261 dwarf_tag_name (abbrev->die_tag));
8263 if (abbrev->die_child != NULL)
8264 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8265 else
8266 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8268 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8270 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8271 dwarf_attr_name (a_attr->dw_attr));
8272 output_value_format (a_attr);
8275 dw2_asm_output_data (1, 0, NULL);
8276 dw2_asm_output_data (1, 0, NULL);
8280 /* Output the .debug_abbrev section which defines the DIE abbreviation
8281 table. */
8283 static void
8284 output_abbrev_section (void)
8286 unsigned long abbrev_id;
8288 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8289 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8291 /* Terminate the table. */
8292 dw2_asm_output_data (1, 0, NULL);
8295 /* Output a symbol we can use to refer to this DIE from another CU. */
8297 static inline void
8298 output_die_symbol (dw_die_ref die)
8300 const char *sym = die->die_id.die_symbol;
8302 gcc_assert (!die->comdat_type_p);
8304 if (sym == 0)
8305 return;
8307 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8308 /* We make these global, not weak; if the target doesn't support
8309 .linkonce, it doesn't support combining the sections, so debugging
8310 will break. */
8311 targetm.asm_out.globalize_label (asm_out_file, sym);
8313 ASM_OUTPUT_LABEL (asm_out_file, sym);
8316 /* Return a new location list, given the begin and end range, and the
8317 expression. */
8319 static inline dw_loc_list_ref
8320 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8321 const char *section)
8323 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8325 retlist->begin = begin;
8326 retlist->begin_entry = NULL;
8327 retlist->end = end;
8328 retlist->expr = expr;
8329 retlist->section = section;
8331 return retlist;
8334 /* Generate a new internal symbol for this location list node, if it
8335 hasn't got one yet. */
8337 static inline void
8338 gen_llsym (dw_loc_list_ref list)
8340 gcc_assert (!list->ll_symbol);
8341 list->ll_symbol = gen_internal_sym ("LLST");
8344 /* Output the location list given to us. */
8346 static void
8347 output_loc_list (dw_loc_list_ref list_head)
8349 dw_loc_list_ref curr = list_head;
8351 if (list_head->emitted)
8352 return;
8353 list_head->emitted = true;
8355 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8357 /* Walk the location list, and output each range + expression. */
8358 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8360 unsigned long size;
8361 /* Don't output an entry that starts and ends at the same address. */
8362 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8363 continue;
8364 size = size_of_locs (curr->expr);
8365 /* If the expression is too large, drop it on the floor. We could
8366 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8367 in the expression, but >= 64KB expressions for a single value
8368 in a single range are unlikely very useful. */
8369 if (size > 0xffff)
8370 continue;
8371 if (dwarf_split_debug_info)
8373 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8374 "Location list start/length entry (%s)",
8375 list_head->ll_symbol);
8376 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8377 "Location list range start index (%s)",
8378 curr->begin);
8379 /* The length field is 4 bytes. If we ever need to support
8380 an 8-byte length, we can add a new DW_LLE code or fall back
8381 to DW_LLE_GNU_start_end_entry. */
8382 dw2_asm_output_delta (4, curr->end, curr->begin,
8383 "Location list range length (%s)",
8384 list_head->ll_symbol);
8386 else if (!have_multiple_function_sections)
8388 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8389 "Location list begin address (%s)",
8390 list_head->ll_symbol);
8391 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8392 "Location list end address (%s)",
8393 list_head->ll_symbol);
8395 else
8397 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8398 "Location list begin address (%s)",
8399 list_head->ll_symbol);
8400 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8401 "Location list end address (%s)",
8402 list_head->ll_symbol);
8405 /* Output the block length for this list of location operations. */
8406 gcc_assert (size <= 0xffff);
8407 dw2_asm_output_data (2, size, "%s", "Location expression size");
8409 output_loc_sequence (curr->expr, -1);
8412 if (dwarf_split_debug_info)
8413 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8414 "Location list terminator (%s)",
8415 list_head->ll_symbol);
8416 else
8418 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8419 "Location list terminator begin (%s)",
8420 list_head->ll_symbol);
8421 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8422 "Location list terminator end (%s)",
8423 list_head->ll_symbol);
8427 /* Output a range_list offset into the debug_range section. Emit a
8428 relocated reference if val_entry is NULL, otherwise, emit an
8429 indirect reference. */
8431 static void
8432 output_range_list_offset (dw_attr_ref a)
8434 const char *name = dwarf_attr_name (a->dw_attr);
8436 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8438 char *p = strchr (ranges_section_label, '\0');
8439 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8440 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8441 debug_ranges_section, "%s", name);
8442 *p = '\0';
8444 else
8445 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8446 "%s (offset from %s)", name, ranges_section_label);
8449 /* Output the offset into the debug_loc section. */
8451 static void
8452 output_loc_list_offset (dw_attr_ref a)
8454 char *sym = AT_loc_list (a)->ll_symbol;
8456 gcc_assert (sym);
8457 if (dwarf_split_debug_info)
8458 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8459 "%s", dwarf_attr_name (a->dw_attr));
8460 else
8461 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8462 "%s", dwarf_attr_name (a->dw_attr));
8465 /* Output an attribute's index or value appropriately. */
8467 static void
8468 output_attr_index_or_value (dw_attr_ref a)
8470 const char *name = dwarf_attr_name (a->dw_attr);
8472 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8474 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8475 return;
8477 switch (AT_class (a))
8479 case dw_val_class_addr:
8480 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8481 break;
8482 case dw_val_class_high_pc:
8483 case dw_val_class_lbl_id:
8484 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8485 break;
8486 case dw_val_class_loc_list:
8487 output_loc_list_offset (a);
8488 break;
8489 default:
8490 gcc_unreachable ();
8494 /* Output a type signature. */
8496 static inline void
8497 output_signature (const char *sig, const char *name)
8499 int i;
8501 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8502 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8505 /* Output the DIE and its attributes. Called recursively to generate
8506 the definitions of each child DIE. */
8508 static void
8509 output_die (dw_die_ref die)
8511 dw_attr_ref a;
8512 dw_die_ref c;
8513 unsigned long size;
8514 unsigned ix;
8516 /* If someone in another CU might refer to us, set up a symbol for
8517 them to point to. */
8518 if (! die->comdat_type_p && die->die_id.die_symbol)
8519 output_die_symbol (die);
8521 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8522 (unsigned long)die->die_offset,
8523 dwarf_tag_name (die->die_tag));
8525 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8527 const char *name = dwarf_attr_name (a->dw_attr);
8529 switch (AT_class (a))
8531 case dw_val_class_addr:
8532 output_attr_index_or_value (a);
8533 break;
8535 case dw_val_class_offset:
8536 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8537 "%s", name);
8538 break;
8540 case dw_val_class_range_list:
8541 output_range_list_offset (a);
8542 break;
8544 case dw_val_class_loc:
8545 size = size_of_locs (AT_loc (a));
8547 /* Output the block length for this list of location operations. */
8548 if (dwarf_version >= 4)
8549 dw2_asm_output_data_uleb128 (size, "%s", name);
8550 else
8551 dw2_asm_output_data (constant_size (size), size, "%s", name);
8553 output_loc_sequence (AT_loc (a), -1);
8554 break;
8556 case dw_val_class_const:
8557 /* ??? It would be slightly more efficient to use a scheme like is
8558 used for unsigned constants below, but gdb 4.x does not sign
8559 extend. Gdb 5.x does sign extend. */
8560 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8561 break;
8563 case dw_val_class_unsigned_const:
8565 int csize = constant_size (AT_unsigned (a));
8566 if (dwarf_version == 3
8567 && a->dw_attr == DW_AT_data_member_location
8568 && csize >= 4)
8569 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8570 else
8571 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8573 break;
8575 case dw_val_class_const_double:
8577 unsigned HOST_WIDE_INT first, second;
8579 if (HOST_BITS_PER_WIDE_INT >= 64)
8580 dw2_asm_output_data (1,
8581 HOST_BITS_PER_DOUBLE_INT
8582 / HOST_BITS_PER_CHAR,
8583 NULL);
8585 if (WORDS_BIG_ENDIAN)
8587 first = a->dw_attr_val.v.val_double.high;
8588 second = a->dw_attr_val.v.val_double.low;
8590 else
8592 first = a->dw_attr_val.v.val_double.low;
8593 second = a->dw_attr_val.v.val_double.high;
8596 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8597 first, "%s", name);
8598 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8599 second, NULL);
8601 break;
8603 case dw_val_class_vec:
8605 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8606 unsigned int len = a->dw_attr_val.v.val_vec.length;
8607 unsigned int i;
8608 unsigned char *p;
8610 dw2_asm_output_data (constant_size (len * elt_size),
8611 len * elt_size, "%s", name);
8612 if (elt_size > sizeof (HOST_WIDE_INT))
8614 elt_size /= 2;
8615 len *= 2;
8617 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8618 i < len;
8619 i++, p += elt_size)
8620 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8621 "fp or vector constant word %u", i);
8622 break;
8625 case dw_val_class_flag:
8626 if (dwarf_version >= 4)
8628 /* Currently all add_AT_flag calls pass in 1 as last argument,
8629 so DW_FORM_flag_present can be used. If that ever changes,
8630 we'll need to use DW_FORM_flag and have some optimization
8631 in build_abbrev_table that will change those to
8632 DW_FORM_flag_present if it is set to 1 in all DIEs using
8633 the same abbrev entry. */
8634 gcc_assert (AT_flag (a) == 1);
8635 if (flag_debug_asm)
8636 fprintf (asm_out_file, "\t\t\t%s %s\n",
8637 ASM_COMMENT_START, name);
8638 break;
8640 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8641 break;
8643 case dw_val_class_loc_list:
8644 output_attr_index_or_value (a);
8645 break;
8647 case dw_val_class_die_ref:
8648 if (AT_ref_external (a))
8650 if (AT_ref (a)->comdat_type_p)
8652 comdat_type_node_ref type_node =
8653 AT_ref (a)->die_id.die_type_node;
8655 gcc_assert (type_node);
8656 output_signature (type_node->signature, name);
8658 else
8660 const char *sym = AT_ref (a)->die_id.die_symbol;
8661 int size;
8663 gcc_assert (sym);
8664 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8665 length, whereas in DWARF3 it's always sized as an
8666 offset. */
8667 if (dwarf_version == 2)
8668 size = DWARF2_ADDR_SIZE;
8669 else
8670 size = DWARF_OFFSET_SIZE;
8671 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8672 name);
8675 else
8677 gcc_assert (AT_ref (a)->die_offset);
8678 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8679 "%s", name);
8681 break;
8683 case dw_val_class_fde_ref:
8685 char l1[20];
8687 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8688 a->dw_attr_val.v.val_fde_index * 2);
8689 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8690 "%s", name);
8692 break;
8694 case dw_val_class_vms_delta:
8695 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8696 AT_vms_delta2 (a), AT_vms_delta1 (a),
8697 "%s", name);
8698 break;
8700 case dw_val_class_lbl_id:
8701 output_attr_index_or_value (a);
8702 break;
8704 case dw_val_class_lineptr:
8705 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8706 debug_line_section, "%s", name);
8707 break;
8709 case dw_val_class_macptr:
8710 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8711 debug_macinfo_section, "%s", name);
8712 break;
8714 case dw_val_class_str:
8715 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8716 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8717 a->dw_attr_val.v.val_str->label,
8718 debug_str_section,
8719 "%s: \"%s\"", name, AT_string (a));
8720 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8721 dw2_asm_output_data_uleb128 (AT_index (a),
8722 "%s: \"%s\"", name, AT_string (a));
8723 else
8724 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8725 break;
8727 case dw_val_class_file:
8729 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8731 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8732 a->dw_attr_val.v.val_file->filename);
8733 break;
8736 case dw_val_class_data8:
8738 int i;
8740 for (i = 0; i < 8; i++)
8741 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8742 i == 0 ? "%s" : NULL, name);
8743 break;
8746 case dw_val_class_high_pc:
8747 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8748 get_AT_low_pc (die), "DW_AT_high_pc");
8749 break;
8751 default:
8752 gcc_unreachable ();
8756 FOR_EACH_CHILD (die, c, output_die (c));
8758 /* Add null byte to terminate sibling list. */
8759 if (die->die_child != NULL)
8760 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8761 (unsigned long) die->die_offset);
8764 /* Output the compilation unit that appears at the beginning of the
8765 .debug_info section, and precedes the DIE descriptions. */
8767 static void
8768 output_compilation_unit_header (void)
8770 int ver = dwarf_version;
8772 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8773 dw2_asm_output_data (4, 0xffffffff,
8774 "Initial length escape value indicating 64-bit DWARF extension");
8775 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8776 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8777 "Length of Compilation Unit Info");
8778 dw2_asm_output_data (2, ver, "DWARF version number");
8779 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8780 debug_abbrev_section,
8781 "Offset Into Abbrev. Section");
8782 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8785 /* Output the compilation unit DIE and its children. */
8787 static void
8788 output_comp_unit (dw_die_ref die, int output_if_empty)
8790 const char *secname, *oldsym;
8791 char *tmp;
8792 external_ref_hash_type extern_map;
8794 /* Unless we are outputting main CU, we may throw away empty ones. */
8795 if (!output_if_empty && die->die_child == NULL)
8796 return;
8798 /* Even if there are no children of this DIE, we must output the information
8799 about the compilation unit. Otherwise, on an empty translation unit, we
8800 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8801 will then complain when examining the file. First mark all the DIEs in
8802 this CU so we know which get local refs. */
8803 mark_dies (die);
8805 extern_map = optimize_external_refs (die);
8807 build_abbrev_table (die, extern_map);
8809 extern_map.dispose ();
8811 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8812 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8813 calc_die_sizes (die);
8815 oldsym = die->die_id.die_symbol;
8816 if (oldsym)
8818 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8820 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8821 secname = tmp;
8822 die->die_id.die_symbol = NULL;
8823 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8825 else
8827 switch_to_section (debug_info_section);
8828 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8829 info_section_emitted = true;
8832 /* Output debugging information. */
8833 output_compilation_unit_header ();
8834 output_die (die);
8836 /* Leave the marks on the main CU, so we can check them in
8837 output_pubnames. */
8838 if (oldsym)
8840 unmark_dies (die);
8841 die->die_id.die_symbol = oldsym;
8845 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8846 and .debug_pubtypes. This is configured per-target, but can be
8847 overridden by the -gpubnames or -gno-pubnames options. */
8849 static inline bool
8850 want_pubnames (void)
8852 if (debug_generate_pub_sections != -1)
8853 return debug_generate_pub_sections;
8854 return targetm.want_debug_pub_sections;
8857 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8859 static void
8860 add_AT_pubnames (dw_die_ref die)
8862 if (want_pubnames ())
8863 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8866 /* Add a string attribute value to a skeleton DIE. */
8868 static inline void
8869 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8870 const char *str)
8872 dw_attr_node attr;
8873 struct indirect_string_node *node;
8875 if (! skeleton_debug_str_hash)
8876 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8877 debug_str_eq, NULL);
8879 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8880 find_string_form (node);
8881 if (node->form == DW_FORM_GNU_str_index)
8882 node->form = DW_FORM_strp;
8884 attr.dw_attr = attr_kind;
8885 attr.dw_attr_val.val_class = dw_val_class_str;
8886 attr.dw_attr_val.val_entry = NULL;
8887 attr.dw_attr_val.v.val_str = node;
8888 add_dwarf_attr (die, &attr);
8891 /* Helper function to generate top-level dies for skeleton debug_info and
8892 debug_types. */
8894 static void
8895 add_top_level_skeleton_die_attrs (dw_die_ref die)
8897 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8898 const char *comp_dir = comp_dir_string ();
8900 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8901 if (comp_dir != NULL)
8902 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8903 add_AT_pubnames (die);
8904 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8907 /* Return the single type-unit die for skeleton type units. */
8909 static dw_die_ref
8910 get_skeleton_type_unit (void)
8912 /* For dwarf_split_debug_sections with use_type info, all type units in the
8913 skeleton sections have identical dies (but different headers). This
8914 single die will be output many times. */
8916 static dw_die_ref skeleton_type_unit = NULL;
8918 if (skeleton_type_unit == NULL)
8920 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8921 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8922 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8924 return skeleton_type_unit;
8927 /* Output skeleton debug sections that point to the dwo file. */
8929 static void
8930 output_skeleton_debug_sections (dw_die_ref comp_unit)
8932 /* These attributes will be found in the full debug_info section. */
8933 remove_AT (comp_unit, DW_AT_producer);
8934 remove_AT (comp_unit, DW_AT_language);
8936 switch_to_section (debug_skeleton_info_section);
8937 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8939 /* Produce the skeleton compilation-unit header. This one differs enough from
8940 a normal CU header that it's better not to call output_compilation_unit
8941 header. */
8942 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8943 dw2_asm_output_data (4, 0xffffffff,
8944 "Initial length escape value indicating 64-bit DWARF extension");
8946 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8947 DWARF_COMPILE_UNIT_HEADER_SIZE
8948 - DWARF_INITIAL_LENGTH_SIZE
8949 + size_of_die (comp_unit),
8950 "Length of Compilation Unit Info");
8951 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8952 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8953 debug_abbrev_section,
8954 "Offset Into Abbrev. Section");
8955 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8957 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8958 output_die (comp_unit);
8960 /* Build the skeleton debug_abbrev section. */
8961 switch_to_section (debug_skeleton_abbrev_section);
8962 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8964 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8965 if (use_debug_types)
8966 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8968 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8971 /* Output a comdat type unit DIE and its children. */
8973 static void
8974 output_comdat_type_unit (comdat_type_node *node)
8976 const char *secname;
8977 char *tmp;
8978 int i;
8979 #if defined (OBJECT_FORMAT_ELF)
8980 tree comdat_key;
8981 #endif
8982 external_ref_hash_type extern_map;
8984 /* First mark all the DIEs in this CU so we know which get local refs. */
8985 mark_dies (node->root_die);
8987 extern_map = optimize_external_refs (node->root_die);
8989 build_abbrev_table (node->root_die, extern_map);
8991 extern_map.dispose ();
8993 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8994 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8995 calc_die_sizes (node->root_die);
8997 #if defined (OBJECT_FORMAT_ELF)
8998 if (!dwarf_split_debug_info)
8999 secname = ".debug_types";
9000 else
9001 secname = ".debug_types.dwo";
9003 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9004 sprintf (tmp, "wt.");
9005 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9006 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9007 comdat_key = get_identifier (tmp);
9008 targetm.asm_out.named_section (secname,
9009 SECTION_DEBUG | SECTION_LINKONCE,
9010 comdat_key);
9011 #else
9012 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9013 sprintf (tmp, ".gnu.linkonce.wt.");
9014 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9015 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9016 secname = tmp;
9017 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9018 #endif
9020 /* Output debugging information. */
9021 output_compilation_unit_header ();
9022 output_signature (node->signature, "Type Signature");
9023 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9024 "Offset to Type DIE");
9025 output_die (node->root_die);
9027 unmark_dies (node->root_die);
9029 #if defined (OBJECT_FORMAT_ELF)
9030 if (dwarf_split_debug_info)
9032 /* Produce the skeleton type-unit header. */
9033 const char *secname = ".debug_types";
9035 targetm.asm_out.named_section (secname,
9036 SECTION_DEBUG | SECTION_LINKONCE,
9037 comdat_key);
9038 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9039 dw2_asm_output_data (4, 0xffffffff,
9040 "Initial length escape value indicating 64-bit DWARF extension");
9042 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9043 DWARF_COMPILE_UNIT_HEADER_SIZE
9044 - DWARF_INITIAL_LENGTH_SIZE
9045 + size_of_die (get_skeleton_type_unit ())
9046 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9047 "Length of Type Unit Info");
9048 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9049 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9050 debug_skeleton_abbrev_section_label,
9051 debug_abbrev_section,
9052 "Offset Into Abbrev. Section");
9053 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9054 output_signature (node->signature, "Type Signature");
9055 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9057 output_die (get_skeleton_type_unit ());
9059 #endif
9062 /* Return the DWARF2/3 pubname associated with a decl. */
9064 static const char *
9065 dwarf2_name (tree decl, int scope)
9067 if (DECL_NAMELESS (decl))
9068 return NULL;
9069 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9072 /* Add a new entry to .debug_pubnames if appropriate. */
9074 static void
9075 add_pubname_string (const char *str, dw_die_ref die)
9077 pubname_entry e;
9079 e.die = die;
9080 e.name = xstrdup (str);
9081 vec_safe_push (pubname_table, e);
9084 static void
9085 add_pubname (tree decl, dw_die_ref die)
9087 if (!want_pubnames ())
9088 return;
9090 /* Don't add items to the table when we expect that the consumer will have
9091 just read the enclosing die. For example, if the consumer is looking at a
9092 class_member, it will either be inside the class already, or will have just
9093 looked up the class to find the member. Either way, searching the class is
9094 faster than searching the index. */
9095 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9096 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9098 const char *name = dwarf2_name (decl, 1);
9100 if (name)
9101 add_pubname_string (name, die);
9105 /* Add an enumerator to the pubnames section. */
9107 static void
9108 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9110 pubname_entry e;
9112 gcc_assert (scope_name);
9113 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9114 e.die = die;
9115 vec_safe_push (pubname_table, e);
9118 /* Add a new entry to .debug_pubtypes if appropriate. */
9120 static void
9121 add_pubtype (tree decl, dw_die_ref die)
9123 pubname_entry e;
9125 if (!want_pubnames ())
9126 return;
9128 if ((TREE_PUBLIC (decl)
9129 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9130 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9132 tree scope = NULL;
9133 const char *scope_name = "";
9134 const char *sep = is_cxx () ? "::" : ".";
9135 const char *name;
9137 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9138 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9140 scope_name = lang_hooks.dwarf_name (scope, 1);
9141 if (scope_name != NULL && scope_name[0] != '\0')
9142 scope_name = concat (scope_name, sep, NULL);
9143 else
9144 scope_name = "";
9147 if (TYPE_P (decl))
9148 name = type_tag (decl);
9149 else
9150 name = lang_hooks.dwarf_name (decl, 1);
9152 /* If we don't have a name for the type, there's no point in adding
9153 it to the table. */
9154 if (name != NULL && name[0] != '\0')
9156 e.die = die;
9157 e.name = concat (scope_name, name, NULL);
9158 vec_safe_push (pubtype_table, e);
9161 /* Although it might be more consistent to add the pubinfo for the
9162 enumerators as their dies are created, they should only be added if the
9163 enum type meets the criteria above. So rather than re-check the parent
9164 enum type whenever an enumerator die is created, just output them all
9165 here. This isn't protected by the name conditional because anonymous
9166 enums don't have names. */
9167 if (die->die_tag == DW_TAG_enumeration_type)
9169 dw_die_ref c;
9171 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9176 /* Output a single entry in the pubnames table. */
9178 static void
9179 output_pubname (dw_offset die_offset, pubname_entry *entry)
9181 dw_die_ref die = entry->die;
9182 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9184 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9186 if (debug_generate_pub_sections == 2)
9188 /* This logic follows gdb's method for determining the value of the flag
9189 byte. */
9190 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9191 switch (die->die_tag)
9193 case DW_TAG_typedef:
9194 case DW_TAG_base_type:
9195 case DW_TAG_subrange_type:
9196 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9197 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9198 break;
9199 case DW_TAG_enumerator:
9200 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9201 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9202 if (!is_cxx () && !is_java ())
9203 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9204 break;
9205 case DW_TAG_subprogram:
9206 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9207 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9208 if (!is_ada ())
9209 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9210 break;
9211 case DW_TAG_constant:
9212 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9213 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9214 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9215 break;
9216 case DW_TAG_variable:
9217 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9218 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9219 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9220 break;
9221 case DW_TAG_namespace:
9222 case DW_TAG_imported_declaration:
9223 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9224 break;
9225 case DW_TAG_class_type:
9226 case DW_TAG_interface_type:
9227 case DW_TAG_structure_type:
9228 case DW_TAG_union_type:
9229 case DW_TAG_enumeration_type:
9230 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9231 if (!is_cxx () && !is_java ())
9232 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9233 break;
9234 default:
9235 /* An unusual tag. Leave the flag-byte empty. */
9236 break;
9238 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9239 "GDB-index flags");
9242 dw2_asm_output_nstring (entry->name, -1, "external name");
9246 /* Output the public names table used to speed up access to externally
9247 visible names; or the public types table used to find type definitions. */
9249 static void
9250 output_pubnames (vec<pubname_entry, va_gc> *names)
9252 unsigned i;
9253 unsigned long pubnames_length = size_of_pubnames (names);
9254 pubname_ref pub;
9256 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9257 dw2_asm_output_data (4, 0xffffffff,
9258 "Initial length escape value indicating 64-bit DWARF extension");
9259 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9261 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9262 dw2_asm_output_data (2, 2, "DWARF Version");
9264 if (dwarf_split_debug_info)
9265 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9266 debug_skeleton_info_section,
9267 "Offset of Compilation Unit Info");
9268 else
9269 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9270 debug_info_section,
9271 "Offset of Compilation Unit Info");
9272 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9273 "Compilation Unit Length");
9275 FOR_EACH_VEC_ELT (*names, i, pub)
9277 if (include_pubname_in_output (names, pub))
9279 dw_offset die_offset = pub->die->die_offset;
9281 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9282 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9283 gcc_assert (pub->die->die_mark);
9285 /* If we're putting types in their own .debug_types sections,
9286 the .debug_pubtypes table will still point to the compile
9287 unit (not the type unit), so we want to use the offset of
9288 the skeleton DIE (if there is one). */
9289 if (pub->die->comdat_type_p && names == pubtype_table)
9291 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9293 if (type_node != NULL)
9294 die_offset = (type_node->skeleton_die != NULL
9295 ? type_node->skeleton_die->die_offset
9296 : 0);
9299 output_pubname (die_offset, pub);
9303 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9306 /* Output public names and types tables if necessary. */
9308 static void
9309 output_pubtables (void)
9311 if (!want_pubnames () || !info_section_emitted)
9312 return;
9314 switch_to_section (debug_pubnames_section);
9315 output_pubnames (pubname_table);
9316 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9317 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9318 simply won't look for the section. */
9319 switch_to_section (debug_pubtypes_section);
9320 output_pubnames (pubtype_table);
9324 /* Output the information that goes into the .debug_aranges table.
9325 Namely, define the beginning and ending address range of the
9326 text section generated for this compilation unit. */
9328 static void
9329 output_aranges (unsigned long aranges_length)
9331 unsigned i;
9333 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9334 dw2_asm_output_data (4, 0xffffffff,
9335 "Initial length escape value indicating 64-bit DWARF extension");
9336 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9337 "Length of Address Ranges Info");
9338 /* Version number for aranges is still 2, even in DWARF3. */
9339 dw2_asm_output_data (2, 2, "DWARF Version");
9340 if (dwarf_split_debug_info)
9341 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9342 debug_skeleton_info_section,
9343 "Offset of Compilation Unit Info");
9344 else
9345 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9346 debug_info_section,
9347 "Offset of Compilation Unit Info");
9348 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9349 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9351 /* We need to align to twice the pointer size here. */
9352 if (DWARF_ARANGES_PAD_SIZE)
9354 /* Pad using a 2 byte words so that padding is correct for any
9355 pointer size. */
9356 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9357 2 * DWARF2_ADDR_SIZE);
9358 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9359 dw2_asm_output_data (2, 0, NULL);
9362 /* It is necessary not to output these entries if the sections were
9363 not used; if the sections were not used, the length will be 0 and
9364 the address may end up as 0 if the section is discarded by ld
9365 --gc-sections, leaving an invalid (0, 0) entry that can be
9366 confused with the terminator. */
9367 if (text_section_used)
9369 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9370 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9371 text_section_label, "Length");
9373 if (cold_text_section_used)
9375 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9376 "Address");
9377 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9378 cold_text_section_label, "Length");
9381 if (have_multiple_function_sections)
9383 unsigned fde_idx;
9384 dw_fde_ref fde;
9386 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9388 if (DECL_IGNORED_P (fde->decl))
9389 continue;
9390 if (!fde->in_std_section)
9392 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9393 "Address");
9394 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9395 fde->dw_fde_begin, "Length");
9397 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9399 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9400 "Address");
9401 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9402 fde->dw_fde_second_begin, "Length");
9407 /* Output the terminator words. */
9408 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9409 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9412 /* Add a new entry to .debug_ranges. Return the offset at which it
9413 was placed. */
9415 static unsigned int
9416 add_ranges_num (int num)
9418 unsigned int in_use = ranges_table_in_use;
9420 if (in_use == ranges_table_allocated)
9422 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9423 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9424 ranges_table_allocated);
9425 memset (ranges_table + ranges_table_in_use, 0,
9426 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9429 ranges_table[in_use].num = num;
9430 ranges_table_in_use = in_use + 1;
9432 return in_use * 2 * DWARF2_ADDR_SIZE;
9435 /* Add a new entry to .debug_ranges corresponding to a block, or a
9436 range terminator if BLOCK is NULL. */
9438 static unsigned int
9439 add_ranges (const_tree block)
9441 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9444 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9445 When using dwarf_split_debug_info, address attributes in dies destined
9446 for the final executable should be direct references--setting the
9447 parameter force_direct ensures this behavior. */
9449 static void
9450 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9451 bool *added, bool force_direct)
9453 unsigned int in_use = ranges_by_label_in_use;
9454 unsigned int offset;
9456 if (in_use == ranges_by_label_allocated)
9458 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9459 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9460 ranges_by_label,
9461 ranges_by_label_allocated);
9462 memset (ranges_by_label + ranges_by_label_in_use, 0,
9463 RANGES_TABLE_INCREMENT
9464 * sizeof (struct dw_ranges_by_label_struct));
9467 ranges_by_label[in_use].begin = begin;
9468 ranges_by_label[in_use].end = end;
9469 ranges_by_label_in_use = in_use + 1;
9471 offset = add_ranges_num (-(int)in_use - 1);
9472 if (!*added)
9474 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9475 *added = true;
9479 static void
9480 output_ranges (void)
9482 unsigned i;
9483 static const char *const start_fmt = "Offset %#x";
9484 const char *fmt = start_fmt;
9486 for (i = 0; i < ranges_table_in_use; i++)
9488 int block_num = ranges_table[i].num;
9490 if (block_num > 0)
9492 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9493 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9495 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9496 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9498 /* If all code is in the text section, then the compilation
9499 unit base address defaults to DW_AT_low_pc, which is the
9500 base of the text section. */
9501 if (!have_multiple_function_sections)
9503 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9504 text_section_label,
9505 fmt, i * 2 * DWARF2_ADDR_SIZE);
9506 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9507 text_section_label, NULL);
9510 /* Otherwise, the compilation unit base address is zero,
9511 which allows us to use absolute addresses, and not worry
9512 about whether the target supports cross-section
9513 arithmetic. */
9514 else
9516 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9517 fmt, i * 2 * DWARF2_ADDR_SIZE);
9518 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9521 fmt = NULL;
9524 /* Negative block_num stands for an index into ranges_by_label. */
9525 else if (block_num < 0)
9527 int lab_idx = - block_num - 1;
9529 if (!have_multiple_function_sections)
9531 gcc_unreachable ();
9532 #if 0
9533 /* If we ever use add_ranges_by_labels () for a single
9534 function section, all we have to do is to take out
9535 the #if 0 above. */
9536 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9537 ranges_by_label[lab_idx].begin,
9538 text_section_label,
9539 fmt, i * 2 * DWARF2_ADDR_SIZE);
9540 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9541 ranges_by_label[lab_idx].end,
9542 text_section_label, NULL);
9543 #endif
9545 else
9547 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9548 ranges_by_label[lab_idx].begin,
9549 fmt, i * 2 * DWARF2_ADDR_SIZE);
9550 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9551 ranges_by_label[lab_idx].end,
9552 NULL);
9555 else
9557 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9558 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9559 fmt = start_fmt;
9564 /* Data structure containing information about input files. */
9565 struct file_info
9567 const char *path; /* Complete file name. */
9568 const char *fname; /* File name part. */
9569 int length; /* Length of entire string. */
9570 struct dwarf_file_data * file_idx; /* Index in input file table. */
9571 int dir_idx; /* Index in directory table. */
9574 /* Data structure containing information about directories with source
9575 files. */
9576 struct dir_info
9578 const char *path; /* Path including directory name. */
9579 int length; /* Path length. */
9580 int prefix; /* Index of directory entry which is a prefix. */
9581 int count; /* Number of files in this directory. */
9582 int dir_idx; /* Index of directory used as base. */
9585 /* Callback function for file_info comparison. We sort by looking at
9586 the directories in the path. */
9588 static int
9589 file_info_cmp (const void *p1, const void *p2)
9591 const struct file_info *const s1 = (const struct file_info *) p1;
9592 const struct file_info *const s2 = (const struct file_info *) p2;
9593 const unsigned char *cp1;
9594 const unsigned char *cp2;
9596 /* Take care of file names without directories. We need to make sure that
9597 we return consistent values to qsort since some will get confused if
9598 we return the same value when identical operands are passed in opposite
9599 orders. So if neither has a directory, return 0 and otherwise return
9600 1 or -1 depending on which one has the directory. */
9601 if ((s1->path == s1->fname || s2->path == s2->fname))
9602 return (s2->path == s2->fname) - (s1->path == s1->fname);
9604 cp1 = (const unsigned char *) s1->path;
9605 cp2 = (const unsigned char *) s2->path;
9607 while (1)
9609 ++cp1;
9610 ++cp2;
9611 /* Reached the end of the first path? If so, handle like above. */
9612 if ((cp1 == (const unsigned char *) s1->fname)
9613 || (cp2 == (const unsigned char *) s2->fname))
9614 return ((cp2 == (const unsigned char *) s2->fname)
9615 - (cp1 == (const unsigned char *) s1->fname));
9617 /* Character of current path component the same? */
9618 else if (*cp1 != *cp2)
9619 return *cp1 - *cp2;
9623 struct file_name_acquire_data
9625 struct file_info *files;
9626 int used_files;
9627 int max_files;
9630 /* Traversal function for the hash table. */
9632 static int
9633 file_name_acquire (void ** slot, void *data)
9635 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9636 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9637 struct file_info *fi;
9638 const char *f;
9640 gcc_assert (fnad->max_files >= d->emitted_number);
9642 if (! d->emitted_number)
9643 return 1;
9645 gcc_assert (fnad->max_files != fnad->used_files);
9647 fi = fnad->files + fnad->used_files++;
9649 /* Skip all leading "./". */
9650 f = d->filename;
9651 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9652 f += 2;
9654 /* Create a new array entry. */
9655 fi->path = f;
9656 fi->length = strlen (f);
9657 fi->file_idx = d;
9659 /* Search for the file name part. */
9660 f = strrchr (f, DIR_SEPARATOR);
9661 #if defined (DIR_SEPARATOR_2)
9663 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9665 if (g != NULL)
9667 if (f == NULL || f < g)
9668 f = g;
9671 #endif
9673 fi->fname = f == NULL ? fi->path : f + 1;
9674 return 1;
9677 /* Output the directory table and the file name table. We try to minimize
9678 the total amount of memory needed. A heuristic is used to avoid large
9679 slowdowns with many input files. */
9681 static void
9682 output_file_names (void)
9684 struct file_name_acquire_data fnad;
9685 int numfiles;
9686 struct file_info *files;
9687 struct dir_info *dirs;
9688 int *saved;
9689 int *savehere;
9690 int *backmap;
9691 int ndirs;
9692 int idx_offset;
9693 int i;
9695 if (!last_emitted_file)
9697 dw2_asm_output_data (1, 0, "End directory table");
9698 dw2_asm_output_data (1, 0, "End file name table");
9699 return;
9702 numfiles = last_emitted_file->emitted_number;
9704 /* Allocate the various arrays we need. */
9705 files = XALLOCAVEC (struct file_info, numfiles);
9706 dirs = XALLOCAVEC (struct dir_info, numfiles);
9708 fnad.files = files;
9709 fnad.used_files = 0;
9710 fnad.max_files = numfiles;
9711 htab_traverse (file_table, file_name_acquire, &fnad);
9712 gcc_assert (fnad.used_files == fnad.max_files);
9714 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9716 /* Find all the different directories used. */
9717 dirs[0].path = files[0].path;
9718 dirs[0].length = files[0].fname - files[0].path;
9719 dirs[0].prefix = -1;
9720 dirs[0].count = 1;
9721 dirs[0].dir_idx = 0;
9722 files[0].dir_idx = 0;
9723 ndirs = 1;
9725 for (i = 1; i < numfiles; i++)
9726 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9727 && memcmp (dirs[ndirs - 1].path, files[i].path,
9728 dirs[ndirs - 1].length) == 0)
9730 /* Same directory as last entry. */
9731 files[i].dir_idx = ndirs - 1;
9732 ++dirs[ndirs - 1].count;
9734 else
9736 int j;
9738 /* This is a new directory. */
9739 dirs[ndirs].path = files[i].path;
9740 dirs[ndirs].length = files[i].fname - files[i].path;
9741 dirs[ndirs].count = 1;
9742 dirs[ndirs].dir_idx = ndirs;
9743 files[i].dir_idx = ndirs;
9745 /* Search for a prefix. */
9746 dirs[ndirs].prefix = -1;
9747 for (j = 0; j < ndirs; j++)
9748 if (dirs[j].length < dirs[ndirs].length
9749 && dirs[j].length > 1
9750 && (dirs[ndirs].prefix == -1
9751 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9752 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9753 dirs[ndirs].prefix = j;
9755 ++ndirs;
9758 /* Now to the actual work. We have to find a subset of the directories which
9759 allow expressing the file name using references to the directory table
9760 with the least amount of characters. We do not do an exhaustive search
9761 where we would have to check out every combination of every single
9762 possible prefix. Instead we use a heuristic which provides nearly optimal
9763 results in most cases and never is much off. */
9764 saved = XALLOCAVEC (int, ndirs);
9765 savehere = XALLOCAVEC (int, ndirs);
9767 memset (saved, '\0', ndirs * sizeof (saved[0]));
9768 for (i = 0; i < ndirs; i++)
9770 int j;
9771 int total;
9773 /* We can always save some space for the current directory. But this
9774 does not mean it will be enough to justify adding the directory. */
9775 savehere[i] = dirs[i].length;
9776 total = (savehere[i] - saved[i]) * dirs[i].count;
9778 for (j = i + 1; j < ndirs; j++)
9780 savehere[j] = 0;
9781 if (saved[j] < dirs[i].length)
9783 /* Determine whether the dirs[i] path is a prefix of the
9784 dirs[j] path. */
9785 int k;
9787 k = dirs[j].prefix;
9788 while (k != -1 && k != (int) i)
9789 k = dirs[k].prefix;
9791 if (k == (int) i)
9793 /* Yes it is. We can possibly save some memory by
9794 writing the filenames in dirs[j] relative to
9795 dirs[i]. */
9796 savehere[j] = dirs[i].length;
9797 total += (savehere[j] - saved[j]) * dirs[j].count;
9802 /* Check whether we can save enough to justify adding the dirs[i]
9803 directory. */
9804 if (total > dirs[i].length + 1)
9806 /* It's worthwhile adding. */
9807 for (j = i; j < ndirs; j++)
9808 if (savehere[j] > 0)
9810 /* Remember how much we saved for this directory so far. */
9811 saved[j] = savehere[j];
9813 /* Remember the prefix directory. */
9814 dirs[j].dir_idx = i;
9819 /* Emit the directory name table. */
9820 idx_offset = dirs[0].length > 0 ? 1 : 0;
9821 for (i = 1 - idx_offset; i < ndirs; i++)
9822 dw2_asm_output_nstring (dirs[i].path,
9823 dirs[i].length
9824 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9825 "Directory Entry: %#x", i + idx_offset);
9827 dw2_asm_output_data (1, 0, "End directory table");
9829 /* We have to emit them in the order of emitted_number since that's
9830 used in the debug info generation. To do this efficiently we
9831 generate a back-mapping of the indices first. */
9832 backmap = XALLOCAVEC (int, numfiles);
9833 for (i = 0; i < numfiles; i++)
9834 backmap[files[i].file_idx->emitted_number - 1] = i;
9836 /* Now write all the file names. */
9837 for (i = 0; i < numfiles; i++)
9839 int file_idx = backmap[i];
9840 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9842 #ifdef VMS_DEBUGGING_INFO
9843 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9845 /* Setting these fields can lead to debugger miscomparisons,
9846 but VMS Debug requires them to be set correctly. */
9848 int ver;
9849 long long cdt;
9850 long siz;
9851 int maxfilelen = strlen (files[file_idx].path)
9852 + dirs[dir_idx].length
9853 + MAX_VMS_VERSION_LEN + 1;
9854 char *filebuf = XALLOCAVEC (char, maxfilelen);
9856 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9857 snprintf (filebuf, maxfilelen, "%s;%d",
9858 files[file_idx].path + dirs[dir_idx].length, ver);
9860 dw2_asm_output_nstring
9861 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9863 /* Include directory index. */
9864 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9866 /* Modification time. */
9867 dw2_asm_output_data_uleb128
9868 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9869 ? cdt : 0,
9870 NULL);
9872 /* File length in bytes. */
9873 dw2_asm_output_data_uleb128
9874 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9875 ? siz : 0,
9876 NULL);
9877 #else
9878 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9879 "File Entry: %#x", (unsigned) i + 1);
9881 /* Include directory index. */
9882 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9884 /* Modification time. */
9885 dw2_asm_output_data_uleb128 (0, NULL);
9887 /* File length in bytes. */
9888 dw2_asm_output_data_uleb128 (0, NULL);
9889 #endif /* VMS_DEBUGGING_INFO */
9892 dw2_asm_output_data (1, 0, "End file name table");
9896 /* Output one line number table into the .debug_line section. */
9898 static void
9899 output_one_line_info_table (dw_line_info_table *table)
9901 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9902 unsigned int current_line = 1;
9903 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9904 dw_line_info_entry *ent;
9905 size_t i;
9907 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9909 switch (ent->opcode)
9911 case LI_set_address:
9912 /* ??? Unfortunately, we have little choice here currently, and
9913 must always use the most general form. GCC does not know the
9914 address delta itself, so we can't use DW_LNS_advance_pc. Many
9915 ports do have length attributes which will give an upper bound
9916 on the address range. We could perhaps use length attributes
9917 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9918 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9920 /* This can handle any delta. This takes
9921 4+DWARF2_ADDR_SIZE bytes. */
9922 dw2_asm_output_data (1, 0, "set address %s", line_label);
9923 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9924 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9925 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9926 break;
9928 case LI_set_line:
9929 if (ent->val == current_line)
9931 /* We still need to start a new row, so output a copy insn. */
9932 dw2_asm_output_data (1, DW_LNS_copy,
9933 "copy line %u", current_line);
9935 else
9937 int line_offset = ent->val - current_line;
9938 int line_delta = line_offset - DWARF_LINE_BASE;
9940 current_line = ent->val;
9941 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9943 /* This can handle deltas from -10 to 234, using the current
9944 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9945 This takes 1 byte. */
9946 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9947 "line %u", current_line);
9949 else
9951 /* This can handle any delta. This takes at least 4 bytes,
9952 depending on the value being encoded. */
9953 dw2_asm_output_data (1, DW_LNS_advance_line,
9954 "advance to line %u", current_line);
9955 dw2_asm_output_data_sleb128 (line_offset, NULL);
9956 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9959 break;
9961 case LI_set_file:
9962 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9963 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9964 break;
9966 case LI_set_column:
9967 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9968 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9969 break;
9971 case LI_negate_stmt:
9972 current_is_stmt = !current_is_stmt;
9973 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9974 "is_stmt %d", current_is_stmt);
9975 break;
9977 case LI_set_prologue_end:
9978 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9979 "set prologue end");
9980 break;
9982 case LI_set_epilogue_begin:
9983 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9984 "set epilogue begin");
9985 break;
9987 case LI_set_discriminator:
9988 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9989 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9990 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9991 dw2_asm_output_data_uleb128 (ent->val, NULL);
9992 break;
9996 /* Emit debug info for the address of the end of the table. */
9997 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9998 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9999 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10000 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10002 dw2_asm_output_data (1, 0, "end sequence");
10003 dw2_asm_output_data_uleb128 (1, NULL);
10004 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10007 /* Output the source line number correspondence information. This
10008 information goes into the .debug_line section. */
10010 static void
10011 output_line_info (bool prologue_only)
10013 char l1[20], l2[20], p1[20], p2[20];
10014 int ver = dwarf_version;
10015 bool saw_one = false;
10016 int opc;
10018 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10019 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10020 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10021 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10023 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10024 dw2_asm_output_data (4, 0xffffffff,
10025 "Initial length escape value indicating 64-bit DWARF extension");
10026 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10027 "Length of Source Line Info");
10028 ASM_OUTPUT_LABEL (asm_out_file, l1);
10030 dw2_asm_output_data (2, ver, "DWARF Version");
10031 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10032 ASM_OUTPUT_LABEL (asm_out_file, p1);
10034 /* Define the architecture-dependent minimum instruction length (in bytes).
10035 In this implementation of DWARF, this field is used for information
10036 purposes only. Since GCC generates assembly language, we have no
10037 a priori knowledge of how many instruction bytes are generated for each
10038 source line, and therefore can use only the DW_LNE_set_address and
10039 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10040 this as '1', which is "correct enough" for all architectures,
10041 and don't let the target override. */
10042 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10044 if (ver >= 4)
10045 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10046 "Maximum Operations Per Instruction");
10047 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10048 "Default is_stmt_start flag");
10049 dw2_asm_output_data (1, DWARF_LINE_BASE,
10050 "Line Base Value (Special Opcodes)");
10051 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10052 "Line Range Value (Special Opcodes)");
10053 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10054 "Special Opcode Base");
10056 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10058 int n_op_args;
10059 switch (opc)
10061 case DW_LNS_advance_pc:
10062 case DW_LNS_advance_line:
10063 case DW_LNS_set_file:
10064 case DW_LNS_set_column:
10065 case DW_LNS_fixed_advance_pc:
10066 case DW_LNS_set_isa:
10067 n_op_args = 1;
10068 break;
10069 default:
10070 n_op_args = 0;
10071 break;
10074 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10075 opc, n_op_args);
10078 /* Write out the information about the files we use. */
10079 output_file_names ();
10080 ASM_OUTPUT_LABEL (asm_out_file, p2);
10081 if (prologue_only)
10083 /* Output the marker for the end of the line number info. */
10084 ASM_OUTPUT_LABEL (asm_out_file, l2);
10085 return;
10088 if (separate_line_info)
10090 dw_line_info_table *table;
10091 size_t i;
10093 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10094 if (table->in_use)
10096 output_one_line_info_table (table);
10097 saw_one = true;
10100 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10102 output_one_line_info_table (cold_text_section_line_info);
10103 saw_one = true;
10106 /* ??? Some Darwin linkers crash on a .debug_line section with no
10107 sequences. Further, merely a DW_LNE_end_sequence entry is not
10108 sufficient -- the address column must also be initialized.
10109 Make sure to output at least one set_address/end_sequence pair,
10110 choosing .text since that section is always present. */
10111 if (text_section_line_info->in_use || !saw_one)
10112 output_one_line_info_table (text_section_line_info);
10114 /* Output the marker for the end of the line number info. */
10115 ASM_OUTPUT_LABEL (asm_out_file, l2);
10118 /* Given a pointer to a tree node for some base type, return a pointer to
10119 a DIE that describes the given type.
10121 This routine must only be called for GCC type nodes that correspond to
10122 Dwarf base (fundamental) types. */
10124 static dw_die_ref
10125 base_type_die (tree type)
10127 dw_die_ref base_type_result;
10128 enum dwarf_type encoding;
10130 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10131 return 0;
10133 /* If this is a subtype that should not be emitted as a subrange type,
10134 use the base type. See subrange_type_for_debug_p. */
10135 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10136 type = TREE_TYPE (type);
10138 switch (TREE_CODE (type))
10140 case INTEGER_TYPE:
10141 if ((dwarf_version >= 4 || !dwarf_strict)
10142 && TYPE_NAME (type)
10143 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10144 && DECL_IS_BUILTIN (TYPE_NAME (type))
10145 && DECL_NAME (TYPE_NAME (type)))
10147 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10148 if (strcmp (name, "char16_t") == 0
10149 || strcmp (name, "char32_t") == 0)
10151 encoding = DW_ATE_UTF;
10152 break;
10155 if (TYPE_STRING_FLAG (type))
10157 if (TYPE_UNSIGNED (type))
10158 encoding = DW_ATE_unsigned_char;
10159 else
10160 encoding = DW_ATE_signed_char;
10162 else if (TYPE_UNSIGNED (type))
10163 encoding = DW_ATE_unsigned;
10164 else
10165 encoding = DW_ATE_signed;
10166 break;
10168 case REAL_TYPE:
10169 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10171 if (dwarf_version >= 3 || !dwarf_strict)
10172 encoding = DW_ATE_decimal_float;
10173 else
10174 encoding = DW_ATE_lo_user;
10176 else
10177 encoding = DW_ATE_float;
10178 break;
10180 case FIXED_POINT_TYPE:
10181 if (!(dwarf_version >= 3 || !dwarf_strict))
10182 encoding = DW_ATE_lo_user;
10183 else if (TYPE_UNSIGNED (type))
10184 encoding = DW_ATE_unsigned_fixed;
10185 else
10186 encoding = DW_ATE_signed_fixed;
10187 break;
10189 /* Dwarf2 doesn't know anything about complex ints, so use
10190 a user defined type for it. */
10191 case COMPLEX_TYPE:
10192 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10193 encoding = DW_ATE_complex_float;
10194 else
10195 encoding = DW_ATE_lo_user;
10196 break;
10198 case BOOLEAN_TYPE:
10199 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10200 encoding = DW_ATE_boolean;
10201 break;
10203 default:
10204 /* No other TREE_CODEs are Dwarf fundamental types. */
10205 gcc_unreachable ();
10208 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10210 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10211 int_size_in_bytes (type));
10212 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10213 add_pubtype (type, base_type_result);
10215 return base_type_result;
10218 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10219 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10221 static inline int
10222 is_base_type (tree type)
10224 switch (TREE_CODE (type))
10226 case ERROR_MARK:
10227 case VOID_TYPE:
10228 case INTEGER_TYPE:
10229 case REAL_TYPE:
10230 case FIXED_POINT_TYPE:
10231 case COMPLEX_TYPE:
10232 case BOOLEAN_TYPE:
10233 return 1;
10235 case ARRAY_TYPE:
10236 case RECORD_TYPE:
10237 case UNION_TYPE:
10238 case QUAL_UNION_TYPE:
10239 case ENUMERAL_TYPE:
10240 case FUNCTION_TYPE:
10241 case METHOD_TYPE:
10242 case POINTER_TYPE:
10243 case REFERENCE_TYPE:
10244 case NULLPTR_TYPE:
10245 case OFFSET_TYPE:
10246 case LANG_TYPE:
10247 case VECTOR_TYPE:
10248 return 0;
10250 default:
10251 gcc_unreachable ();
10254 return 0;
10257 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10258 node, return the size in bits for the type if it is a constant, or else
10259 return the alignment for the type if the type's size is not constant, or
10260 else return BITS_PER_WORD if the type actually turns out to be an
10261 ERROR_MARK node. */
10263 static inline unsigned HOST_WIDE_INT
10264 simple_type_size_in_bits (const_tree type)
10266 if (TREE_CODE (type) == ERROR_MARK)
10267 return BITS_PER_WORD;
10268 else if (TYPE_SIZE (type) == NULL_TREE)
10269 return 0;
10270 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10271 return tree_to_uhwi (TYPE_SIZE (type));
10272 else
10273 return TYPE_ALIGN (type);
10276 /* Similarly, but return a double_int instead of UHWI. */
10278 static inline double_int
10279 double_int_type_size_in_bits (const_tree type)
10281 if (TREE_CODE (type) == ERROR_MARK)
10282 return double_int::from_uhwi (BITS_PER_WORD);
10283 else if (TYPE_SIZE (type) == NULL_TREE)
10284 return double_int_zero;
10285 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10286 return tree_to_double_int (TYPE_SIZE (type));
10287 else
10288 return double_int::from_uhwi (TYPE_ALIGN (type));
10291 /* Given a pointer to a tree node for a subrange type, return a pointer
10292 to a DIE that describes the given type. */
10294 static dw_die_ref
10295 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10297 dw_die_ref subrange_die;
10298 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10300 if (context_die == NULL)
10301 context_die = comp_unit_die ();
10303 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10305 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10307 /* The size of the subrange type and its base type do not match,
10308 so we need to generate a size attribute for the subrange type. */
10309 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10312 if (low)
10313 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10314 if (high)
10315 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10317 return subrange_die;
10320 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10321 entry that chains various modifiers in front of the given type. */
10323 static dw_die_ref
10324 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10325 dw_die_ref context_die)
10327 enum tree_code code = TREE_CODE (type);
10328 dw_die_ref mod_type_die;
10329 dw_die_ref sub_die = NULL;
10330 tree item_type = NULL;
10331 tree qualified_type;
10332 tree name, low, high;
10333 dw_die_ref mod_scope;
10335 if (code == ERROR_MARK)
10336 return NULL;
10338 /* See if we already have the appropriately qualified variant of
10339 this type. */
10340 qualified_type
10341 = get_qualified_type (type,
10342 ((is_const_type ? TYPE_QUAL_CONST : 0)
10343 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10345 if (qualified_type == sizetype
10346 && TYPE_NAME (qualified_type)
10347 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10349 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10351 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10352 && TYPE_PRECISION (t)
10353 == TYPE_PRECISION (qualified_type)
10354 && TYPE_UNSIGNED (t)
10355 == TYPE_UNSIGNED (qualified_type));
10356 qualified_type = t;
10359 /* If we do, then we can just use its DIE, if it exists. */
10360 if (qualified_type)
10362 mod_type_die = lookup_type_die (qualified_type);
10363 if (mod_type_die)
10364 return mod_type_die;
10367 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10369 /* Handle C typedef types. */
10370 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10371 && !DECL_ARTIFICIAL (name))
10373 tree dtype = TREE_TYPE (name);
10375 if (qualified_type == dtype)
10377 /* For a named type, use the typedef. */
10378 gen_type_die (qualified_type, context_die);
10379 return lookup_type_die (qualified_type);
10381 else if (is_const_type < TYPE_READONLY (dtype)
10382 || is_volatile_type < TYPE_VOLATILE (dtype)
10383 || (is_const_type <= TYPE_READONLY (dtype)
10384 && is_volatile_type <= TYPE_VOLATILE (dtype)
10385 && DECL_ORIGINAL_TYPE (name) != type))
10386 /* cv-unqualified version of named type. Just use the unnamed
10387 type to which it refers. */
10388 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10389 is_const_type, is_volatile_type,
10390 context_die);
10391 /* Else cv-qualified version of named type; fall through. */
10394 mod_scope = scope_die_for (type, context_die);
10396 if (is_const_type
10397 /* If both is_const_type and is_volatile_type, prefer the path
10398 which leads to a qualified type. */
10399 && (!is_volatile_type
10400 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10401 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10403 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10404 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10406 else if (is_volatile_type)
10408 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10409 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10411 else if (code == POINTER_TYPE)
10413 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10414 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10415 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10416 item_type = TREE_TYPE (type);
10417 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10418 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10419 TYPE_ADDR_SPACE (item_type));
10421 else if (code == REFERENCE_TYPE)
10423 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10424 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10425 type);
10426 else
10427 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10428 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10429 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10430 item_type = TREE_TYPE (type);
10431 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10432 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10433 TYPE_ADDR_SPACE (item_type));
10435 else if (code == INTEGER_TYPE
10436 && TREE_TYPE (type) != NULL_TREE
10437 && subrange_type_for_debug_p (type, &low, &high))
10439 mod_type_die = subrange_type_die (type, low, high, context_die);
10440 item_type = TREE_TYPE (type);
10442 else if (is_base_type (type))
10443 mod_type_die = base_type_die (type);
10444 else
10446 gen_type_die (type, context_die);
10448 /* We have to get the type_main_variant here (and pass that to the
10449 `lookup_type_die' routine) because the ..._TYPE node we have
10450 might simply be a *copy* of some original type node (where the
10451 copy was created to help us keep track of typedef names) and
10452 that copy might have a different TYPE_UID from the original
10453 ..._TYPE node. */
10454 if (TREE_CODE (type) != VECTOR_TYPE)
10455 return lookup_type_die (type_main_variant (type));
10456 else
10457 /* Vectors have the debugging information in the type,
10458 not the main variant. */
10459 return lookup_type_die (type);
10462 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10463 don't output a DW_TAG_typedef, since there isn't one in the
10464 user's program; just attach a DW_AT_name to the type.
10465 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10466 if the base type already has the same name. */
10467 if (name
10468 && ((TREE_CODE (name) != TYPE_DECL
10469 && (qualified_type == TYPE_MAIN_VARIANT (type)
10470 || (!is_const_type && !is_volatile_type)))
10471 || (TREE_CODE (name) == TYPE_DECL
10472 && TREE_TYPE (name) == qualified_type
10473 && DECL_NAME (name))))
10475 if (TREE_CODE (name) == TYPE_DECL)
10476 /* Could just call add_name_and_src_coords_attributes here,
10477 but since this is a builtin type it doesn't have any
10478 useful source coordinates anyway. */
10479 name = DECL_NAME (name);
10480 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10482 /* This probably indicates a bug. */
10483 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10485 name = TYPE_NAME (type);
10486 if (name
10487 && TREE_CODE (name) == TYPE_DECL)
10488 name = DECL_NAME (name);
10489 add_name_attribute (mod_type_die,
10490 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10493 if (qualified_type)
10494 equate_type_number_to_die (qualified_type, mod_type_die);
10496 if (item_type)
10497 /* We must do this after the equate_type_number_to_die call, in case
10498 this is a recursive type. This ensures that the modified_type_die
10499 recursion will terminate even if the type is recursive. Recursive
10500 types are possible in Ada. */
10501 sub_die = modified_type_die (item_type,
10502 TYPE_READONLY (item_type),
10503 TYPE_VOLATILE (item_type),
10504 context_die);
10506 if (sub_die != NULL)
10507 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10509 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10510 if (TYPE_ARTIFICIAL (type))
10511 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10513 return mod_type_die;
10516 /* Generate DIEs for the generic parameters of T.
10517 T must be either a generic type or a generic function.
10518 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10520 static void
10521 gen_generic_params_dies (tree t)
10523 tree parms, args;
10524 int parms_num, i;
10525 dw_die_ref die = NULL;
10526 int non_default;
10528 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10529 return;
10531 if (TYPE_P (t))
10532 die = lookup_type_die (t);
10533 else if (DECL_P (t))
10534 die = lookup_decl_die (t);
10536 gcc_assert (die);
10538 parms = lang_hooks.get_innermost_generic_parms (t);
10539 if (!parms)
10540 /* T has no generic parameter. It means T is neither a generic type
10541 or function. End of story. */
10542 return;
10544 parms_num = TREE_VEC_LENGTH (parms);
10545 args = lang_hooks.get_innermost_generic_args (t);
10546 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10547 non_default = int_cst_value (TREE_CHAIN (args));
10548 else
10549 non_default = TREE_VEC_LENGTH (args);
10550 for (i = 0; i < parms_num; i++)
10552 tree parm, arg, arg_pack_elems;
10553 dw_die_ref parm_die;
10555 parm = TREE_VEC_ELT (parms, i);
10556 arg = TREE_VEC_ELT (args, i);
10557 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10558 gcc_assert (parm && TREE_VALUE (parm) && arg);
10560 if (parm && TREE_VALUE (parm) && arg)
10562 /* If PARM represents a template parameter pack,
10563 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10564 by DW_TAG_template_*_parameter DIEs for the argument
10565 pack elements of ARG. Note that ARG would then be
10566 an argument pack. */
10567 if (arg_pack_elems)
10568 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10569 arg_pack_elems,
10570 die);
10571 else
10572 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10573 true /* emit name */, die);
10574 if (i >= non_default)
10575 add_AT_flag (parm_die, DW_AT_default_value, 1);
10580 /* Create and return a DIE for PARM which should be
10581 the representation of a generic type parameter.
10582 For instance, in the C++ front end, PARM would be a template parameter.
10583 ARG is the argument to PARM.
10584 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10585 name of the PARM.
10586 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10587 as a child node. */
10589 static dw_die_ref
10590 generic_parameter_die (tree parm, tree arg,
10591 bool emit_name_p,
10592 dw_die_ref parent_die)
10594 dw_die_ref tmpl_die = NULL;
10595 const char *name = NULL;
10597 if (!parm || !DECL_NAME (parm) || !arg)
10598 return NULL;
10600 /* We support non-type generic parameters and arguments,
10601 type generic parameters and arguments, as well as
10602 generic generic parameters (a.k.a. template template parameters in C++)
10603 and arguments. */
10604 if (TREE_CODE (parm) == PARM_DECL)
10605 /* PARM is a nontype generic parameter */
10606 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10607 else if (TREE_CODE (parm) == TYPE_DECL)
10608 /* PARM is a type generic parameter. */
10609 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10610 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10611 /* PARM is a generic generic parameter.
10612 Its DIE is a GNU extension. It shall have a
10613 DW_AT_name attribute to represent the name of the template template
10614 parameter, and a DW_AT_GNU_template_name attribute to represent the
10615 name of the template template argument. */
10616 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10617 parent_die, parm);
10618 else
10619 gcc_unreachable ();
10621 if (tmpl_die)
10623 tree tmpl_type;
10625 /* If PARM is a generic parameter pack, it means we are
10626 emitting debug info for a template argument pack element.
10627 In other terms, ARG is a template argument pack element.
10628 In that case, we don't emit any DW_AT_name attribute for
10629 the die. */
10630 if (emit_name_p)
10632 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10633 gcc_assert (name);
10634 add_AT_string (tmpl_die, DW_AT_name, name);
10637 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10639 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10640 TMPL_DIE should have a child DW_AT_type attribute that is set
10641 to the type of the argument to PARM, which is ARG.
10642 If PARM is a type generic parameter, TMPL_DIE should have a
10643 child DW_AT_type that is set to ARG. */
10644 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10645 add_type_attribute (tmpl_die, tmpl_type, 0,
10646 TREE_THIS_VOLATILE (tmpl_type),
10647 parent_die);
10649 else
10651 /* So TMPL_DIE is a DIE representing a
10652 a generic generic template parameter, a.k.a template template
10653 parameter in C++ and arg is a template. */
10655 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10656 to the name of the argument. */
10657 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10658 if (name)
10659 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10662 if (TREE_CODE (parm) == PARM_DECL)
10663 /* So PARM is a non-type generic parameter.
10664 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10665 attribute of TMPL_DIE which value represents the value
10666 of ARG.
10667 We must be careful here:
10668 The value of ARG might reference some function decls.
10669 We might currently be emitting debug info for a generic
10670 type and types are emitted before function decls, we don't
10671 know if the function decls referenced by ARG will actually be
10672 emitted after cgraph computations.
10673 So must defer the generation of the DW_AT_const_value to
10674 after cgraph is ready. */
10675 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10678 return tmpl_die;
10681 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10682 PARM_PACK must be a template parameter pack. The returned DIE
10683 will be child DIE of PARENT_DIE. */
10685 static dw_die_ref
10686 template_parameter_pack_die (tree parm_pack,
10687 tree parm_pack_args,
10688 dw_die_ref parent_die)
10690 dw_die_ref die;
10691 int j;
10693 gcc_assert (parent_die && parm_pack);
10695 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10696 add_name_and_src_coords_attributes (die, parm_pack);
10697 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10698 generic_parameter_die (parm_pack,
10699 TREE_VEC_ELT (parm_pack_args, j),
10700 false /* Don't emit DW_AT_name */,
10701 die);
10702 return die;
10705 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10706 an enumerated type. */
10708 static inline int
10709 type_is_enum (const_tree type)
10711 return TREE_CODE (type) == ENUMERAL_TYPE;
10714 /* Return the DBX register number described by a given RTL node. */
10716 static unsigned int
10717 dbx_reg_number (const_rtx rtl)
10719 unsigned regno = REGNO (rtl);
10721 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10723 #ifdef LEAF_REG_REMAP
10724 if (crtl->uses_only_leaf_regs)
10726 int leaf_reg = LEAF_REG_REMAP (regno);
10727 if (leaf_reg != -1)
10728 regno = (unsigned) leaf_reg;
10730 #endif
10732 regno = DBX_REGISTER_NUMBER (regno);
10733 gcc_assert (regno != INVALID_REGNUM);
10734 return regno;
10737 /* Optionally add a DW_OP_piece term to a location description expression.
10738 DW_OP_piece is only added if the location description expression already
10739 doesn't end with DW_OP_piece. */
10741 static void
10742 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10744 dw_loc_descr_ref loc;
10746 if (*list_head != NULL)
10748 /* Find the end of the chain. */
10749 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10752 if (loc->dw_loc_opc != DW_OP_piece)
10753 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10757 /* Return a location descriptor that designates a machine register or
10758 zero if there is none. */
10760 static dw_loc_descr_ref
10761 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10763 rtx regs;
10765 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10766 return 0;
10768 /* We only use "frame base" when we're sure we're talking about the
10769 post-prologue local stack frame. We do this by *not* running
10770 register elimination until this point, and recognizing the special
10771 argument pointer and soft frame pointer rtx's.
10772 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10773 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10774 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10776 dw_loc_descr_ref result = NULL;
10778 if (dwarf_version >= 4 || !dwarf_strict)
10780 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10781 initialized);
10782 if (result)
10783 add_loc_descr (&result,
10784 new_loc_descr (DW_OP_stack_value, 0, 0));
10786 return result;
10789 regs = targetm.dwarf_register_span (rtl);
10791 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10792 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10793 else
10795 unsigned int dbx_regnum = dbx_reg_number (rtl);
10796 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10797 return 0;
10798 return one_reg_loc_descriptor (dbx_regnum, initialized);
10802 /* Return a location descriptor that designates a machine register for
10803 a given hard register number. */
10805 static dw_loc_descr_ref
10806 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10808 dw_loc_descr_ref reg_loc_descr;
10810 if (regno <= 31)
10811 reg_loc_descr
10812 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10813 else
10814 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10816 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10817 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10819 return reg_loc_descr;
10822 /* Given an RTL of a register, return a location descriptor that
10823 designates a value that spans more than one register. */
10825 static dw_loc_descr_ref
10826 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10827 enum var_init_status initialized)
10829 int size, i;
10830 dw_loc_descr_ref loc_result = NULL;
10832 /* Simple, contiguous registers. */
10833 if (regs == NULL_RTX)
10835 unsigned reg = REGNO (rtl);
10836 int nregs;
10838 #ifdef LEAF_REG_REMAP
10839 if (crtl->uses_only_leaf_regs)
10841 int leaf_reg = LEAF_REG_REMAP (reg);
10842 if (leaf_reg != -1)
10843 reg = (unsigned) leaf_reg;
10845 #endif
10847 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10848 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10850 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10852 loc_result = NULL;
10853 while (nregs--)
10855 dw_loc_descr_ref t;
10857 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10858 VAR_INIT_STATUS_INITIALIZED);
10859 add_loc_descr (&loc_result, t);
10860 add_loc_descr_op_piece (&loc_result, size);
10861 ++reg;
10863 return loc_result;
10866 /* Now onto stupid register sets in non contiguous locations. */
10868 gcc_assert (GET_CODE (regs) == PARALLEL);
10870 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10871 loc_result = NULL;
10873 for (i = 0; i < XVECLEN (regs, 0); ++i)
10875 dw_loc_descr_ref t;
10877 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10878 VAR_INIT_STATUS_INITIALIZED);
10879 add_loc_descr (&loc_result, t);
10880 add_loc_descr_op_piece (&loc_result, size);
10883 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10884 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10885 return loc_result;
10888 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10890 /* Return a location descriptor that designates a constant i,
10891 as a compound operation from constant (i >> shift), constant shift
10892 and DW_OP_shl. */
10894 static dw_loc_descr_ref
10895 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10897 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10898 add_loc_descr (&ret, int_loc_descriptor (shift));
10899 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10900 return ret;
10903 /* Return a location descriptor that designates a constant. */
10905 static dw_loc_descr_ref
10906 int_loc_descriptor (HOST_WIDE_INT i)
10908 enum dwarf_location_atom op;
10910 /* Pick the smallest representation of a constant, rather than just
10911 defaulting to the LEB encoding. */
10912 if (i >= 0)
10914 int clz = clz_hwi (i);
10915 int ctz = ctz_hwi (i);
10916 if (i <= 31)
10917 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10918 else if (i <= 0xff)
10919 op = DW_OP_const1u;
10920 else if (i <= 0xffff)
10921 op = DW_OP_const2u;
10922 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10923 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10924 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10925 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10926 while DW_OP_const4u is 5 bytes. */
10927 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10928 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10929 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10930 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10931 while DW_OP_const4u is 5 bytes. */
10932 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10933 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10934 op = DW_OP_const4u;
10935 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10936 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10937 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10938 while DW_OP_constu of constant >= 0x100000000 takes at least
10939 6 bytes. */
10940 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10941 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10942 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10943 >= HOST_BITS_PER_WIDE_INT)
10944 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10945 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10946 while DW_OP_constu takes in this case at least 6 bytes. */
10947 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10948 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10949 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10950 && size_of_uleb128 (i) > 6)
10951 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10952 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10953 else
10954 op = DW_OP_constu;
10956 else
10958 if (i >= -0x80)
10959 op = DW_OP_const1s;
10960 else if (i >= -0x8000)
10961 op = DW_OP_const2s;
10962 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10964 if (size_of_int_loc_descriptor (i) < 5)
10966 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10967 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10968 return ret;
10970 op = DW_OP_const4s;
10972 else
10974 if (size_of_int_loc_descriptor (i)
10975 < (unsigned long) 1 + size_of_sleb128 (i))
10977 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10978 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10979 return ret;
10981 op = DW_OP_consts;
10985 return new_loc_descr (op, i, 0);
10988 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10989 without actually allocating it. */
10991 static unsigned long
10992 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10994 return size_of_int_loc_descriptor (i >> shift)
10995 + size_of_int_loc_descriptor (shift)
10996 + 1;
10999 /* Return size_of_locs (int_loc_descriptor (i)) without
11000 actually allocating it. */
11002 static unsigned long
11003 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11005 unsigned long s;
11007 if (i >= 0)
11009 int clz, ctz;
11010 if (i <= 31)
11011 return 1;
11012 else if (i <= 0xff)
11013 return 2;
11014 else if (i <= 0xffff)
11015 return 3;
11016 clz = clz_hwi (i);
11017 ctz = ctz_hwi (i);
11018 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11019 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11020 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11021 - clz - 5);
11022 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11023 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11024 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11025 - clz - 8);
11026 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11027 return 5;
11028 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11029 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11030 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11031 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11032 - clz - 8);
11033 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11034 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11035 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11036 - clz - 16);
11037 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11038 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11039 && s > 6)
11040 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11041 - clz - 32);
11042 else
11043 return 1 + s;
11045 else
11047 if (i >= -0x80)
11048 return 2;
11049 else if (i >= -0x8000)
11050 return 3;
11051 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11053 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11055 s = size_of_int_loc_descriptor (-i) + 1;
11056 if (s < 5)
11057 return s;
11059 return 5;
11061 else
11063 unsigned long r = 1 + size_of_sleb128 (i);
11064 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11066 s = size_of_int_loc_descriptor (-i) + 1;
11067 if (s < r)
11068 return s;
11070 return r;
11075 /* Return loc description representing "address" of integer value.
11076 This can appear only as toplevel expression. */
11078 static dw_loc_descr_ref
11079 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11081 int litsize;
11082 dw_loc_descr_ref loc_result = NULL;
11084 if (!(dwarf_version >= 4 || !dwarf_strict))
11085 return NULL;
11087 litsize = size_of_int_loc_descriptor (i);
11088 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11089 is more compact. For DW_OP_stack_value we need:
11090 litsize + 1 (DW_OP_stack_value)
11091 and for DW_OP_implicit_value:
11092 1 (DW_OP_implicit_value) + 1 (length) + size. */
11093 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11095 loc_result = int_loc_descriptor (i);
11096 add_loc_descr (&loc_result,
11097 new_loc_descr (DW_OP_stack_value, 0, 0));
11098 return loc_result;
11101 loc_result = new_loc_descr (DW_OP_implicit_value,
11102 size, 0);
11103 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11104 loc_result->dw_loc_oprnd2.v.val_int = i;
11105 return loc_result;
11108 /* Return a location descriptor that designates a base+offset location. */
11110 static dw_loc_descr_ref
11111 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11112 enum var_init_status initialized)
11114 unsigned int regno;
11115 dw_loc_descr_ref result;
11116 dw_fde_ref fde = cfun->fde;
11118 /* We only use "frame base" when we're sure we're talking about the
11119 post-prologue local stack frame. We do this by *not* running
11120 register elimination until this point, and recognizing the special
11121 argument pointer and soft frame pointer rtx's. */
11122 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11124 rtx elim = (ira_use_lra_p
11125 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11126 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11128 if (elim != reg)
11130 if (GET_CODE (elim) == PLUS)
11132 offset += INTVAL (XEXP (elim, 1));
11133 elim = XEXP (elim, 0);
11135 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11136 && (elim == hard_frame_pointer_rtx
11137 || elim == stack_pointer_rtx))
11138 || elim == (frame_pointer_needed
11139 ? hard_frame_pointer_rtx
11140 : stack_pointer_rtx));
11142 /* If drap register is used to align stack, use frame
11143 pointer + offset to access stack variables. If stack
11144 is aligned without drap, use stack pointer + offset to
11145 access stack variables. */
11146 if (crtl->stack_realign_tried
11147 && reg == frame_pointer_rtx)
11149 int base_reg
11150 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11151 ? HARD_FRAME_POINTER_REGNUM
11152 : REGNO (elim));
11153 return new_reg_loc_descr (base_reg, offset);
11156 gcc_assert (frame_pointer_fb_offset_valid);
11157 offset += frame_pointer_fb_offset;
11158 return new_loc_descr (DW_OP_fbreg, offset, 0);
11162 regno = REGNO (reg);
11163 #ifdef LEAF_REG_REMAP
11164 if (crtl->uses_only_leaf_regs)
11166 int leaf_reg = LEAF_REG_REMAP (regno);
11167 if (leaf_reg != -1)
11168 regno = (unsigned) leaf_reg;
11170 #endif
11171 regno = DWARF_FRAME_REGNUM (regno);
11173 if (!optimize && fde
11174 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11176 /* Use cfa+offset to represent the location of arguments passed
11177 on the stack when drap is used to align stack.
11178 Only do this when not optimizing, for optimized code var-tracking
11179 is supposed to track where the arguments live and the register
11180 used as vdrap or drap in some spot might be used for something
11181 else in other part of the routine. */
11182 return new_loc_descr (DW_OP_fbreg, offset, 0);
11185 if (regno <= 31)
11186 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11187 offset, 0);
11188 else
11189 result = new_loc_descr (DW_OP_bregx, regno, offset);
11191 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11192 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11194 return result;
11197 /* Return true if this RTL expression describes a base+offset calculation. */
11199 static inline int
11200 is_based_loc (const_rtx rtl)
11202 return (GET_CODE (rtl) == PLUS
11203 && ((REG_P (XEXP (rtl, 0))
11204 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11205 && CONST_INT_P (XEXP (rtl, 1)))));
11208 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11209 failed. */
11211 static dw_loc_descr_ref
11212 tls_mem_loc_descriptor (rtx mem)
11214 tree base;
11215 dw_loc_descr_ref loc_result;
11217 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11218 return NULL;
11220 base = get_base_address (MEM_EXPR (mem));
11221 if (base == NULL
11222 || TREE_CODE (base) != VAR_DECL
11223 || !DECL_THREAD_LOCAL_P (base))
11224 return NULL;
11226 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11227 if (loc_result == NULL)
11228 return NULL;
11230 if (MEM_OFFSET (mem))
11231 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11233 return loc_result;
11236 /* Output debug info about reason why we failed to expand expression as dwarf
11237 expression. */
11239 static void
11240 expansion_failed (tree expr, rtx rtl, char const *reason)
11242 if (dump_file && (dump_flags & TDF_DETAILS))
11244 fprintf (dump_file, "Failed to expand as dwarf: ");
11245 if (expr)
11246 print_generic_expr (dump_file, expr, dump_flags);
11247 if (rtl)
11249 fprintf (dump_file, "\n");
11250 print_rtl (dump_file, rtl);
11252 fprintf (dump_file, "\nReason: %s\n", reason);
11256 /* Helper function for const_ok_for_output, called either directly
11257 or via for_each_rtx. */
11259 static int
11260 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11262 rtx rtl = *rtlp;
11264 if (GET_CODE (rtl) == UNSPEC)
11266 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11267 we can't express it in the debug info. */
11268 #ifdef ENABLE_CHECKING
11269 /* Don't complain about TLS UNSPECs, those are just too hard to
11270 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11271 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11272 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11273 if (XVECLEN (rtl, 0) == 0
11274 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11275 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11276 inform (current_function_decl
11277 ? DECL_SOURCE_LOCATION (current_function_decl)
11278 : UNKNOWN_LOCATION,
11279 #if NUM_UNSPEC_VALUES > 0
11280 "non-delegitimized UNSPEC %s (%d) found in variable location",
11281 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11282 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11283 XINT (rtl, 1));
11284 #else
11285 "non-delegitimized UNSPEC %d found in variable location",
11286 XINT (rtl, 1));
11287 #endif
11288 #endif
11289 expansion_failed (NULL_TREE, rtl,
11290 "UNSPEC hasn't been delegitimized.\n");
11291 return 1;
11294 if (targetm.const_not_ok_for_debug_p (rtl))
11296 expansion_failed (NULL_TREE, rtl,
11297 "Expression rejected for debug by the backend.\n");
11298 return 1;
11301 if (GET_CODE (rtl) != SYMBOL_REF)
11302 return 0;
11304 if (CONSTANT_POOL_ADDRESS_P (rtl))
11306 bool marked;
11307 get_pool_constant_mark (rtl, &marked);
11308 /* If all references to this pool constant were optimized away,
11309 it was not output and thus we can't represent it. */
11310 if (!marked)
11312 expansion_failed (NULL_TREE, rtl,
11313 "Constant was removed from constant pool.\n");
11314 return 1;
11318 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11319 return 1;
11321 /* Avoid references to external symbols in debug info, on several targets
11322 the linker might even refuse to link when linking a shared library,
11323 and in many other cases the relocations for .debug_info/.debug_loc are
11324 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11325 to be defined within the same shared library or executable are fine. */
11326 if (SYMBOL_REF_EXTERNAL_P (rtl))
11328 tree decl = SYMBOL_REF_DECL (rtl);
11330 if (decl == NULL || !targetm.binds_local_p (decl))
11332 expansion_failed (NULL_TREE, rtl,
11333 "Symbol not defined in current TU.\n");
11334 return 1;
11338 return 0;
11341 /* Return true if constant RTL can be emitted in DW_OP_addr or
11342 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11343 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11345 static bool
11346 const_ok_for_output (rtx rtl)
11348 if (GET_CODE (rtl) == SYMBOL_REF)
11349 return const_ok_for_output_1 (&rtl, NULL) == 0;
11351 if (GET_CODE (rtl) == CONST)
11352 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11354 return true;
11357 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11358 if possible, NULL otherwise. */
11360 static dw_die_ref
11361 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11363 dw_die_ref type_die;
11364 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11366 if (type == NULL)
11367 return NULL;
11368 switch (TREE_CODE (type))
11370 case INTEGER_TYPE:
11371 case REAL_TYPE:
11372 break;
11373 default:
11374 return NULL;
11376 type_die = lookup_type_die (type);
11377 if (!type_die)
11378 type_die = modified_type_die (type, false, false, comp_unit_die ());
11379 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11380 return NULL;
11381 return type_die;
11384 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11385 type matching MODE, or, if MODE is narrower than or as wide as
11386 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11387 possible. */
11389 static dw_loc_descr_ref
11390 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11392 enum machine_mode outer_mode = mode;
11393 dw_die_ref type_die;
11394 dw_loc_descr_ref cvt;
11396 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11398 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11399 return op;
11401 type_die = base_type_for_mode (outer_mode, 1);
11402 if (type_die == NULL)
11403 return NULL;
11404 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11405 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11406 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11407 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11408 add_loc_descr (&op, cvt);
11409 return op;
11412 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11414 static dw_loc_descr_ref
11415 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11416 dw_loc_descr_ref op1)
11418 dw_loc_descr_ref ret = op0;
11419 add_loc_descr (&ret, op1);
11420 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11421 if (STORE_FLAG_VALUE != 1)
11423 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11424 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11426 return ret;
11429 /* Return location descriptor for signed comparison OP RTL. */
11431 static dw_loc_descr_ref
11432 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11433 enum machine_mode mem_mode)
11435 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11436 dw_loc_descr_ref op0, op1;
11437 int shift;
11439 if (op_mode == VOIDmode)
11440 op_mode = GET_MODE (XEXP (rtl, 1));
11441 if (op_mode == VOIDmode)
11442 return NULL;
11444 if (dwarf_strict
11445 && (GET_MODE_CLASS (op_mode) != MODE_INT
11446 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11447 return NULL;
11449 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11450 VAR_INIT_STATUS_INITIALIZED);
11451 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11452 VAR_INIT_STATUS_INITIALIZED);
11454 if (op0 == NULL || op1 == NULL)
11455 return NULL;
11457 if (GET_MODE_CLASS (op_mode) != MODE_INT
11458 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11459 return compare_loc_descriptor (op, op0, op1);
11461 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11463 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11464 dw_loc_descr_ref cvt;
11466 if (type_die == NULL)
11467 return NULL;
11468 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11469 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11470 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11471 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11472 add_loc_descr (&op0, cvt);
11473 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11474 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11475 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11476 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11477 add_loc_descr (&op1, cvt);
11478 return compare_loc_descriptor (op, op0, op1);
11481 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11482 /* For eq/ne, if the operands are known to be zero-extended,
11483 there is no need to do the fancy shifting up. */
11484 if (op == DW_OP_eq || op == DW_OP_ne)
11486 dw_loc_descr_ref last0, last1;
11487 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11489 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11491 /* deref_size zero extends, and for constants we can check
11492 whether they are zero extended or not. */
11493 if (((last0->dw_loc_opc == DW_OP_deref_size
11494 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11495 || (CONST_INT_P (XEXP (rtl, 0))
11496 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11497 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11498 && ((last1->dw_loc_opc == DW_OP_deref_size
11499 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11500 || (CONST_INT_P (XEXP (rtl, 1))
11501 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11502 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11503 return compare_loc_descriptor (op, op0, op1);
11505 /* EQ/NE comparison against constant in narrower type than
11506 DWARF2_ADDR_SIZE can be performed either as
11507 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11508 DW_OP_{eq,ne}
11510 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11511 DW_OP_{eq,ne}. Pick whatever is shorter. */
11512 if (CONST_INT_P (XEXP (rtl, 1))
11513 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11514 && (size_of_int_loc_descriptor (shift) + 1
11515 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11516 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11517 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11518 & GET_MODE_MASK (op_mode))))
11520 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11521 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11522 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11523 & GET_MODE_MASK (op_mode));
11524 return compare_loc_descriptor (op, op0, op1);
11527 add_loc_descr (&op0, int_loc_descriptor (shift));
11528 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11529 if (CONST_INT_P (XEXP (rtl, 1)))
11530 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11531 else
11533 add_loc_descr (&op1, int_loc_descriptor (shift));
11534 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11536 return compare_loc_descriptor (op, op0, op1);
11539 /* Return location descriptor for unsigned comparison OP RTL. */
11541 static dw_loc_descr_ref
11542 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11543 enum machine_mode mem_mode)
11545 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11546 dw_loc_descr_ref op0, op1;
11548 if (op_mode == VOIDmode)
11549 op_mode = GET_MODE (XEXP (rtl, 1));
11550 if (op_mode == VOIDmode)
11551 return NULL;
11552 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11553 return NULL;
11555 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11556 return NULL;
11558 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11559 VAR_INIT_STATUS_INITIALIZED);
11560 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11561 VAR_INIT_STATUS_INITIALIZED);
11563 if (op0 == NULL || op1 == NULL)
11564 return NULL;
11566 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11568 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11569 dw_loc_descr_ref last0, last1;
11570 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11572 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11574 if (CONST_INT_P (XEXP (rtl, 0)))
11575 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11576 /* deref_size zero extends, so no need to mask it again. */
11577 else if (last0->dw_loc_opc != DW_OP_deref_size
11578 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11580 add_loc_descr (&op0, int_loc_descriptor (mask));
11581 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11583 if (CONST_INT_P (XEXP (rtl, 1)))
11584 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11585 /* deref_size zero extends, so no need to mask it again. */
11586 else if (last1->dw_loc_opc != DW_OP_deref_size
11587 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11589 add_loc_descr (&op1, int_loc_descriptor (mask));
11590 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11593 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11595 HOST_WIDE_INT bias = 1;
11596 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11597 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11598 if (CONST_INT_P (XEXP (rtl, 1)))
11599 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11600 + INTVAL (XEXP (rtl, 1)));
11601 else
11602 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11603 bias, 0));
11605 return compare_loc_descriptor (op, op0, op1);
11608 /* Return location descriptor for {U,S}{MIN,MAX}. */
11610 static dw_loc_descr_ref
11611 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11612 enum machine_mode mem_mode)
11614 enum dwarf_location_atom op;
11615 dw_loc_descr_ref op0, op1, ret;
11616 dw_loc_descr_ref bra_node, drop_node;
11618 if (dwarf_strict
11619 && (GET_MODE_CLASS (mode) != MODE_INT
11620 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11621 return NULL;
11623 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11624 VAR_INIT_STATUS_INITIALIZED);
11625 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11626 VAR_INIT_STATUS_INITIALIZED);
11628 if (op0 == NULL || op1 == NULL)
11629 return NULL;
11631 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11632 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11633 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11634 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11636 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11638 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11639 add_loc_descr (&op0, int_loc_descriptor (mask));
11640 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11641 add_loc_descr (&op1, int_loc_descriptor (mask));
11642 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11644 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11646 HOST_WIDE_INT bias = 1;
11647 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11648 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11649 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11652 else if (GET_MODE_CLASS (mode) == MODE_INT
11653 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11655 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11656 add_loc_descr (&op0, int_loc_descriptor (shift));
11657 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11658 add_loc_descr (&op1, int_loc_descriptor (shift));
11659 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11661 else if (GET_MODE_CLASS (mode) == MODE_INT
11662 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11664 dw_die_ref type_die = base_type_for_mode (mode, 0);
11665 dw_loc_descr_ref cvt;
11666 if (type_die == NULL)
11667 return NULL;
11668 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11669 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11670 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11671 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11672 add_loc_descr (&op0, cvt);
11673 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11674 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11675 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11676 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11677 add_loc_descr (&op1, cvt);
11680 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11681 op = DW_OP_lt;
11682 else
11683 op = DW_OP_gt;
11684 ret = op0;
11685 add_loc_descr (&ret, op1);
11686 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11687 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11688 add_loc_descr (&ret, bra_node);
11689 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11690 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11691 add_loc_descr (&ret, drop_node);
11692 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11693 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11694 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11695 && GET_MODE_CLASS (mode) == MODE_INT
11696 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11697 ret = convert_descriptor_to_mode (mode, ret);
11698 return ret;
11701 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11702 but after converting arguments to type_die, afterwards
11703 convert back to unsigned. */
11705 static dw_loc_descr_ref
11706 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11707 enum machine_mode mode, enum machine_mode mem_mode)
11709 dw_loc_descr_ref cvt, op0, op1;
11711 if (type_die == NULL)
11712 return NULL;
11713 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11714 VAR_INIT_STATUS_INITIALIZED);
11715 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11716 VAR_INIT_STATUS_INITIALIZED);
11717 if (op0 == NULL || op1 == NULL)
11718 return NULL;
11719 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11720 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11721 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11722 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11723 add_loc_descr (&op0, cvt);
11724 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11725 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11726 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11727 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11728 add_loc_descr (&op1, cvt);
11729 add_loc_descr (&op0, op1);
11730 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11731 return convert_descriptor_to_mode (mode, op0);
11734 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11735 const0 is DW_OP_lit0 or corresponding typed constant,
11736 const1 is DW_OP_lit1 or corresponding typed constant
11737 and constMSB is constant with just the MSB bit set
11738 for the mode):
11739 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11740 L1: const0 DW_OP_swap
11741 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11742 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11743 L3: DW_OP_drop
11744 L4: DW_OP_nop
11746 CTZ is similar:
11747 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11748 L1: const0 DW_OP_swap
11749 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11750 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11751 L3: DW_OP_drop
11752 L4: DW_OP_nop
11754 FFS is similar:
11755 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11756 L1: const1 DW_OP_swap
11757 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11758 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11759 L3: DW_OP_drop
11760 L4: DW_OP_nop */
11762 static dw_loc_descr_ref
11763 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11764 enum machine_mode mem_mode)
11766 dw_loc_descr_ref op0, ret, tmp;
11767 HOST_WIDE_INT valv;
11768 dw_loc_descr_ref l1jump, l1label;
11769 dw_loc_descr_ref l2jump, l2label;
11770 dw_loc_descr_ref l3jump, l3label;
11771 dw_loc_descr_ref l4jump, l4label;
11772 rtx msb;
11774 if (GET_MODE_CLASS (mode) != MODE_INT
11775 || GET_MODE (XEXP (rtl, 0)) != mode
11776 || (GET_CODE (rtl) == CLZ
11777 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11778 return NULL;
11780 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11781 VAR_INIT_STATUS_INITIALIZED);
11782 if (op0 == NULL)
11783 return NULL;
11784 ret = op0;
11785 if (GET_CODE (rtl) == CLZ)
11787 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11788 valv = GET_MODE_BITSIZE (mode);
11790 else if (GET_CODE (rtl) == FFS)
11791 valv = 0;
11792 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11793 valv = GET_MODE_BITSIZE (mode);
11794 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11795 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11796 add_loc_descr (&ret, l1jump);
11797 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11798 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11799 VAR_INIT_STATUS_INITIALIZED);
11800 if (tmp == NULL)
11801 return NULL;
11802 add_loc_descr (&ret, tmp);
11803 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11804 add_loc_descr (&ret, l4jump);
11805 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11806 ? const1_rtx : const0_rtx,
11807 mode, mem_mode,
11808 VAR_INIT_STATUS_INITIALIZED);
11809 if (l1label == NULL)
11810 return NULL;
11811 add_loc_descr (&ret, l1label);
11812 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11813 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11814 add_loc_descr (&ret, l2label);
11815 if (GET_CODE (rtl) != CLZ)
11816 msb = const1_rtx;
11817 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11818 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11819 << (GET_MODE_BITSIZE (mode) - 1));
11820 else
11821 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11822 << (GET_MODE_BITSIZE (mode)
11823 - HOST_BITS_PER_WIDE_INT - 1), mode);
11824 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11825 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11826 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11827 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11828 else
11829 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11830 VAR_INIT_STATUS_INITIALIZED);
11831 if (tmp == NULL)
11832 return NULL;
11833 add_loc_descr (&ret, tmp);
11834 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11835 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11836 add_loc_descr (&ret, l3jump);
11837 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11838 VAR_INIT_STATUS_INITIALIZED);
11839 if (tmp == NULL)
11840 return NULL;
11841 add_loc_descr (&ret, tmp);
11842 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11843 ? DW_OP_shl : DW_OP_shr, 0, 0));
11844 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11845 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11846 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11847 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11848 add_loc_descr (&ret, l2jump);
11849 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11850 add_loc_descr (&ret, l3label);
11851 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11852 add_loc_descr (&ret, l4label);
11853 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11854 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11855 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11856 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11857 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11858 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11859 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11860 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11861 return ret;
11864 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11865 const1 is DW_OP_lit1 or corresponding typed constant):
11866 const0 DW_OP_swap
11867 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11868 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11869 L2: DW_OP_drop
11871 PARITY is similar:
11872 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11873 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11874 L2: DW_OP_drop */
11876 static dw_loc_descr_ref
11877 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11878 enum machine_mode mem_mode)
11880 dw_loc_descr_ref op0, ret, tmp;
11881 dw_loc_descr_ref l1jump, l1label;
11882 dw_loc_descr_ref l2jump, l2label;
11884 if (GET_MODE_CLASS (mode) != MODE_INT
11885 || GET_MODE (XEXP (rtl, 0)) != mode)
11886 return NULL;
11888 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11889 VAR_INIT_STATUS_INITIALIZED);
11890 if (op0 == NULL)
11891 return NULL;
11892 ret = op0;
11893 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11894 VAR_INIT_STATUS_INITIALIZED);
11895 if (tmp == NULL)
11896 return NULL;
11897 add_loc_descr (&ret, tmp);
11898 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11899 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11900 add_loc_descr (&ret, l1label);
11901 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11902 add_loc_descr (&ret, l2jump);
11903 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11904 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11905 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11906 VAR_INIT_STATUS_INITIALIZED);
11907 if (tmp == NULL)
11908 return NULL;
11909 add_loc_descr (&ret, tmp);
11910 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11911 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11912 ? DW_OP_plus : DW_OP_xor, 0, 0));
11913 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11914 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11915 VAR_INIT_STATUS_INITIALIZED);
11916 add_loc_descr (&ret, tmp);
11917 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11918 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11919 add_loc_descr (&ret, l1jump);
11920 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11921 add_loc_descr (&ret, l2label);
11922 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11923 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11924 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11925 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11926 return ret;
11929 /* BSWAP (constS is initial shift count, either 56 or 24):
11930 constS const0
11931 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11932 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11933 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11934 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11935 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11937 static dw_loc_descr_ref
11938 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11939 enum machine_mode mem_mode)
11941 dw_loc_descr_ref op0, ret, tmp;
11942 dw_loc_descr_ref l1jump, l1label;
11943 dw_loc_descr_ref l2jump, l2label;
11945 if (GET_MODE_CLASS (mode) != MODE_INT
11946 || BITS_PER_UNIT != 8
11947 || (GET_MODE_BITSIZE (mode) != 32
11948 && GET_MODE_BITSIZE (mode) != 64))
11949 return NULL;
11951 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11952 VAR_INIT_STATUS_INITIALIZED);
11953 if (op0 == NULL)
11954 return NULL;
11956 ret = op0;
11957 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11958 mode, mem_mode,
11959 VAR_INIT_STATUS_INITIALIZED);
11960 if (tmp == NULL)
11961 return NULL;
11962 add_loc_descr (&ret, tmp);
11963 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11964 VAR_INIT_STATUS_INITIALIZED);
11965 if (tmp == NULL)
11966 return NULL;
11967 add_loc_descr (&ret, tmp);
11968 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11969 add_loc_descr (&ret, l1label);
11970 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11971 mode, mem_mode,
11972 VAR_INIT_STATUS_INITIALIZED);
11973 add_loc_descr (&ret, tmp);
11974 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11975 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11976 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11977 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11978 VAR_INIT_STATUS_INITIALIZED);
11979 if (tmp == NULL)
11980 return NULL;
11981 add_loc_descr (&ret, tmp);
11982 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11983 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11984 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11985 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11986 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11987 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11988 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11989 VAR_INIT_STATUS_INITIALIZED);
11990 add_loc_descr (&ret, tmp);
11991 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11992 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11993 add_loc_descr (&ret, l2jump);
11994 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11995 VAR_INIT_STATUS_INITIALIZED);
11996 add_loc_descr (&ret, tmp);
11997 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11998 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11999 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12000 add_loc_descr (&ret, l1jump);
12001 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12002 add_loc_descr (&ret, l2label);
12003 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12004 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12005 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12006 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12007 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12008 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12009 return ret;
12012 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12013 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12014 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12015 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12017 ROTATERT is similar:
12018 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12019 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12020 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12022 static dw_loc_descr_ref
12023 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12024 enum machine_mode mem_mode)
12026 rtx rtlop1 = XEXP (rtl, 1);
12027 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12028 int i;
12030 if (GET_MODE_CLASS (mode) != MODE_INT)
12031 return NULL;
12033 if (GET_MODE (rtlop1) != VOIDmode
12034 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12035 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12036 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12037 VAR_INIT_STATUS_INITIALIZED);
12038 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12039 VAR_INIT_STATUS_INITIALIZED);
12040 if (op0 == NULL || op1 == NULL)
12041 return NULL;
12042 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12043 for (i = 0; i < 2; i++)
12045 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12046 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12047 mode, mem_mode,
12048 VAR_INIT_STATUS_INITIALIZED);
12049 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12050 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12051 ? DW_OP_const4u
12052 : HOST_BITS_PER_WIDE_INT == 64
12053 ? DW_OP_const8u : DW_OP_constu,
12054 GET_MODE_MASK (mode), 0);
12055 else
12056 mask[i] = NULL;
12057 if (mask[i] == NULL)
12058 return NULL;
12059 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12061 ret = op0;
12062 add_loc_descr (&ret, op1);
12063 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12064 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12065 if (GET_CODE (rtl) == ROTATERT)
12067 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12068 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12069 GET_MODE_BITSIZE (mode), 0));
12071 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12072 if (mask[0] != NULL)
12073 add_loc_descr (&ret, mask[0]);
12074 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12075 if (mask[1] != NULL)
12077 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12078 add_loc_descr (&ret, mask[1]);
12079 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12081 if (GET_CODE (rtl) == ROTATE)
12083 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12084 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12085 GET_MODE_BITSIZE (mode), 0));
12087 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12088 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12089 return ret;
12092 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12093 for DEBUG_PARAMETER_REF RTL. */
12095 static dw_loc_descr_ref
12096 parameter_ref_descriptor (rtx rtl)
12098 dw_loc_descr_ref ret;
12099 dw_die_ref ref;
12101 if (dwarf_strict)
12102 return NULL;
12103 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12104 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12105 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12106 if (ref)
12108 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12109 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12110 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12112 else
12114 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12115 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12117 return ret;
12120 /* The following routine converts the RTL for a variable or parameter
12121 (resident in memory) into an equivalent Dwarf representation of a
12122 mechanism for getting the address of that same variable onto the top of a
12123 hypothetical "address evaluation" stack.
12125 When creating memory location descriptors, we are effectively transforming
12126 the RTL for a memory-resident object into its Dwarf postfix expression
12127 equivalent. This routine recursively descends an RTL tree, turning
12128 it into Dwarf postfix code as it goes.
12130 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12132 MEM_MODE is the mode of the memory reference, needed to handle some
12133 autoincrement addressing modes.
12135 Return 0 if we can't represent the location. */
12137 dw_loc_descr_ref
12138 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12139 enum machine_mode mem_mode,
12140 enum var_init_status initialized)
12142 dw_loc_descr_ref mem_loc_result = NULL;
12143 enum dwarf_location_atom op;
12144 dw_loc_descr_ref op0, op1;
12145 rtx inner = NULL_RTX;
12147 if (mode == VOIDmode)
12148 mode = GET_MODE (rtl);
12150 /* Note that for a dynamically sized array, the location we will generate a
12151 description of here will be the lowest numbered location which is
12152 actually within the array. That's *not* necessarily the same as the
12153 zeroth element of the array. */
12155 rtl = targetm.delegitimize_address (rtl);
12157 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12158 return NULL;
12160 switch (GET_CODE (rtl))
12162 case POST_INC:
12163 case POST_DEC:
12164 case POST_MODIFY:
12165 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12167 case SUBREG:
12168 /* The case of a subreg may arise when we have a local (register)
12169 variable or a formal (register) parameter which doesn't quite fill
12170 up an entire register. For now, just assume that it is
12171 legitimate to make the Dwarf info refer to the whole register which
12172 contains the given subreg. */
12173 if (!subreg_lowpart_p (rtl))
12174 break;
12175 inner = SUBREG_REG (rtl);
12176 case TRUNCATE:
12177 if (inner == NULL_RTX)
12178 inner = XEXP (rtl, 0);
12179 if (GET_MODE_CLASS (mode) == MODE_INT
12180 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12181 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12182 #ifdef POINTERS_EXTEND_UNSIGNED
12183 || (mode == Pmode && mem_mode != VOIDmode)
12184 #endif
12186 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12188 mem_loc_result = mem_loc_descriptor (inner,
12189 GET_MODE (inner),
12190 mem_mode, initialized);
12191 break;
12193 if (dwarf_strict)
12194 break;
12195 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12196 break;
12197 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12198 && (GET_MODE_CLASS (mode) != MODE_INT
12199 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12200 break;
12201 else
12203 dw_die_ref type_die;
12204 dw_loc_descr_ref cvt;
12206 mem_loc_result = mem_loc_descriptor (inner,
12207 GET_MODE (inner),
12208 mem_mode, initialized);
12209 if (mem_loc_result == NULL)
12210 break;
12211 type_die = base_type_for_mode (mode,
12212 GET_MODE_CLASS (mode) == MODE_INT);
12213 if (type_die == NULL)
12215 mem_loc_result = NULL;
12216 break;
12218 if (GET_MODE_SIZE (mode)
12219 != GET_MODE_SIZE (GET_MODE (inner)))
12220 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12221 else
12222 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12223 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12224 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12225 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12226 add_loc_descr (&mem_loc_result, cvt);
12228 break;
12230 case REG:
12231 if (GET_MODE_CLASS (mode) != MODE_INT
12232 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12233 && rtl != arg_pointer_rtx
12234 && rtl != frame_pointer_rtx
12235 #ifdef POINTERS_EXTEND_UNSIGNED
12236 && (mode != Pmode || mem_mode == VOIDmode)
12237 #endif
12240 dw_die_ref type_die;
12241 unsigned int dbx_regnum;
12243 if (dwarf_strict)
12244 break;
12245 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12246 break;
12247 type_die = base_type_for_mode (mode,
12248 GET_MODE_CLASS (mode) == MODE_INT);
12249 if (type_die == NULL)
12250 break;
12252 dbx_regnum = dbx_reg_number (rtl);
12253 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12254 break;
12255 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12256 dbx_regnum, 0);
12257 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12258 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12259 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12260 break;
12262 /* Whenever a register number forms a part of the description of the
12263 method for calculating the (dynamic) address of a memory resident
12264 object, DWARF rules require the register number be referred to as
12265 a "base register". This distinction is not based in any way upon
12266 what category of register the hardware believes the given register
12267 belongs to. This is strictly DWARF terminology we're dealing with
12268 here. Note that in cases where the location of a memory-resident
12269 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12270 OP_CONST (0)) the actual DWARF location descriptor that we generate
12271 may just be OP_BASEREG (basereg). This may look deceptively like
12272 the object in question was allocated to a register (rather than in
12273 memory) so DWARF consumers need to be aware of the subtle
12274 distinction between OP_REG and OP_BASEREG. */
12275 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12276 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12277 else if (stack_realign_drap
12278 && crtl->drap_reg
12279 && crtl->args.internal_arg_pointer == rtl
12280 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12282 /* If RTL is internal_arg_pointer, which has been optimized
12283 out, use DRAP instead. */
12284 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12285 VAR_INIT_STATUS_INITIALIZED);
12287 break;
12289 case SIGN_EXTEND:
12290 case ZERO_EXTEND:
12291 if (GET_MODE_CLASS (mode) != MODE_INT)
12292 break;
12293 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12294 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12295 if (op0 == 0)
12296 break;
12297 else if (GET_CODE (rtl) == ZERO_EXTEND
12298 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12299 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12300 < HOST_BITS_PER_WIDE_INT
12301 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12302 to expand zero extend as two shifts instead of
12303 masking. */
12304 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12306 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12307 mem_loc_result = op0;
12308 add_loc_descr (&mem_loc_result,
12309 int_loc_descriptor (GET_MODE_MASK (imode)));
12310 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12312 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12314 int shift = DWARF2_ADDR_SIZE
12315 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12316 shift *= BITS_PER_UNIT;
12317 if (GET_CODE (rtl) == SIGN_EXTEND)
12318 op = DW_OP_shra;
12319 else
12320 op = DW_OP_shr;
12321 mem_loc_result = op0;
12322 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12323 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12324 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12325 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12327 else if (!dwarf_strict)
12329 dw_die_ref type_die1, type_die2;
12330 dw_loc_descr_ref cvt;
12332 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12333 GET_CODE (rtl) == ZERO_EXTEND);
12334 if (type_die1 == NULL)
12335 break;
12336 type_die2 = base_type_for_mode (mode, 1);
12337 if (type_die2 == NULL)
12338 break;
12339 mem_loc_result = op0;
12340 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12341 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12342 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12343 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12344 add_loc_descr (&mem_loc_result, cvt);
12345 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12346 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12347 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12348 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12349 add_loc_descr (&mem_loc_result, cvt);
12351 break;
12353 case MEM:
12355 rtx new_rtl = avoid_constant_pool_reference (rtl);
12356 if (new_rtl != rtl)
12358 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12359 initialized);
12360 if (mem_loc_result != NULL)
12361 return mem_loc_result;
12364 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12365 get_address_mode (rtl), mode,
12366 VAR_INIT_STATUS_INITIALIZED);
12367 if (mem_loc_result == NULL)
12368 mem_loc_result = tls_mem_loc_descriptor (rtl);
12369 if (mem_loc_result != NULL)
12371 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12372 || GET_MODE_CLASS (mode) != MODE_INT)
12374 dw_die_ref type_die;
12375 dw_loc_descr_ref deref;
12377 if (dwarf_strict)
12378 return NULL;
12379 type_die
12380 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12381 if (type_die == NULL)
12382 return NULL;
12383 deref = new_loc_descr (DW_OP_GNU_deref_type,
12384 GET_MODE_SIZE (mode), 0);
12385 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12386 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12387 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12388 add_loc_descr (&mem_loc_result, deref);
12390 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12391 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12392 else
12393 add_loc_descr (&mem_loc_result,
12394 new_loc_descr (DW_OP_deref_size,
12395 GET_MODE_SIZE (mode), 0));
12397 break;
12399 case LO_SUM:
12400 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12402 case LABEL_REF:
12403 /* Some ports can transform a symbol ref into a label ref, because
12404 the symbol ref is too far away and has to be dumped into a constant
12405 pool. */
12406 case CONST:
12407 case SYMBOL_REF:
12408 if (GET_MODE_CLASS (mode) != MODE_INT
12409 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12410 #ifdef POINTERS_EXTEND_UNSIGNED
12411 && (mode != Pmode || mem_mode == VOIDmode)
12412 #endif
12414 break;
12415 if (GET_CODE (rtl) == SYMBOL_REF
12416 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12418 dw_loc_descr_ref temp;
12420 /* If this is not defined, we have no way to emit the data. */
12421 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12422 break;
12424 temp = new_addr_loc_descr (rtl, dtprel_true);
12426 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12427 add_loc_descr (&mem_loc_result, temp);
12429 break;
12432 if (!const_ok_for_output (rtl))
12433 break;
12435 symref:
12436 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12437 vec_safe_push (used_rtx_array, rtl);
12438 break;
12440 case CONCAT:
12441 case CONCATN:
12442 case VAR_LOCATION:
12443 case DEBUG_IMPLICIT_PTR:
12444 expansion_failed (NULL_TREE, rtl,
12445 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12446 return 0;
12448 case ENTRY_VALUE:
12449 if (dwarf_strict)
12450 return NULL;
12451 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12453 if (GET_MODE_CLASS (mode) != MODE_INT
12454 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12455 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12456 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12457 else
12459 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12460 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12461 return NULL;
12462 op0 = one_reg_loc_descriptor (dbx_regnum,
12463 VAR_INIT_STATUS_INITIALIZED);
12466 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12467 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12469 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12470 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12471 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12472 return NULL;
12474 else
12475 gcc_unreachable ();
12476 if (op0 == NULL)
12477 return NULL;
12478 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12479 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12480 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12481 break;
12483 case DEBUG_PARAMETER_REF:
12484 mem_loc_result = parameter_ref_descriptor (rtl);
12485 break;
12487 case PRE_MODIFY:
12488 /* Extract the PLUS expression nested inside and fall into
12489 PLUS code below. */
12490 rtl = XEXP (rtl, 1);
12491 goto plus;
12493 case PRE_INC:
12494 case PRE_DEC:
12495 /* Turn these into a PLUS expression and fall into the PLUS code
12496 below. */
12497 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12498 gen_int_mode (GET_CODE (rtl) == PRE_INC
12499 ? GET_MODE_UNIT_SIZE (mem_mode)
12500 : -GET_MODE_UNIT_SIZE (mem_mode),
12501 mode));
12503 /* ... fall through ... */
12505 case PLUS:
12506 plus:
12507 if (is_based_loc (rtl)
12508 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12509 || XEXP (rtl, 0) == arg_pointer_rtx
12510 || XEXP (rtl, 0) == frame_pointer_rtx)
12511 && GET_MODE_CLASS (mode) == MODE_INT)
12512 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12513 INTVAL (XEXP (rtl, 1)),
12514 VAR_INIT_STATUS_INITIALIZED);
12515 else
12517 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12518 VAR_INIT_STATUS_INITIALIZED);
12519 if (mem_loc_result == 0)
12520 break;
12522 if (CONST_INT_P (XEXP (rtl, 1))
12523 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12524 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12525 else
12527 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12528 VAR_INIT_STATUS_INITIALIZED);
12529 if (op1 == 0)
12530 break;
12531 add_loc_descr (&mem_loc_result, op1);
12532 add_loc_descr (&mem_loc_result,
12533 new_loc_descr (DW_OP_plus, 0, 0));
12536 break;
12538 /* If a pseudo-reg is optimized away, it is possible for it to
12539 be replaced with a MEM containing a multiply or shift. */
12540 case MINUS:
12541 op = DW_OP_minus;
12542 goto do_binop;
12544 case MULT:
12545 op = DW_OP_mul;
12546 goto do_binop;
12548 case DIV:
12549 if (!dwarf_strict
12550 && GET_MODE_CLASS (mode) == MODE_INT
12551 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12553 mem_loc_result = typed_binop (DW_OP_div, rtl,
12554 base_type_for_mode (mode, 0),
12555 mode, mem_mode);
12556 break;
12558 op = DW_OP_div;
12559 goto do_binop;
12561 case UMOD:
12562 op = DW_OP_mod;
12563 goto do_binop;
12565 case ASHIFT:
12566 op = DW_OP_shl;
12567 goto do_shift;
12569 case ASHIFTRT:
12570 op = DW_OP_shra;
12571 goto do_shift;
12573 case LSHIFTRT:
12574 op = DW_OP_shr;
12575 goto do_shift;
12577 do_shift:
12578 if (GET_MODE_CLASS (mode) != MODE_INT)
12579 break;
12580 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12581 VAR_INIT_STATUS_INITIALIZED);
12583 rtx rtlop1 = XEXP (rtl, 1);
12584 if (GET_MODE (rtlop1) != VOIDmode
12585 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12586 < GET_MODE_BITSIZE (mode))
12587 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12588 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12589 VAR_INIT_STATUS_INITIALIZED);
12592 if (op0 == 0 || op1 == 0)
12593 break;
12595 mem_loc_result = op0;
12596 add_loc_descr (&mem_loc_result, op1);
12597 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12598 break;
12600 case AND:
12601 op = DW_OP_and;
12602 goto do_binop;
12604 case IOR:
12605 op = DW_OP_or;
12606 goto do_binop;
12608 case XOR:
12609 op = DW_OP_xor;
12610 goto do_binop;
12612 do_binop:
12613 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12614 VAR_INIT_STATUS_INITIALIZED);
12615 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12616 VAR_INIT_STATUS_INITIALIZED);
12618 if (op0 == 0 || op1 == 0)
12619 break;
12621 mem_loc_result = op0;
12622 add_loc_descr (&mem_loc_result, op1);
12623 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12624 break;
12626 case MOD:
12627 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12629 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12630 base_type_for_mode (mode, 0),
12631 mode, mem_mode);
12632 break;
12635 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12636 VAR_INIT_STATUS_INITIALIZED);
12637 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12638 VAR_INIT_STATUS_INITIALIZED);
12640 if (op0 == 0 || op1 == 0)
12641 break;
12643 mem_loc_result = op0;
12644 add_loc_descr (&mem_loc_result, op1);
12645 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12646 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12647 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12648 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12649 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12650 break;
12652 case UDIV:
12653 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12655 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12657 op = DW_OP_div;
12658 goto do_binop;
12660 mem_loc_result = typed_binop (DW_OP_div, rtl,
12661 base_type_for_mode (mode, 1),
12662 mode, mem_mode);
12664 break;
12666 case NOT:
12667 op = DW_OP_not;
12668 goto do_unop;
12670 case ABS:
12671 op = DW_OP_abs;
12672 goto do_unop;
12674 case NEG:
12675 op = DW_OP_neg;
12676 goto do_unop;
12678 do_unop:
12679 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12680 VAR_INIT_STATUS_INITIALIZED);
12682 if (op0 == 0)
12683 break;
12685 mem_loc_result = op0;
12686 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12687 break;
12689 case CONST_INT:
12690 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12691 #ifdef POINTERS_EXTEND_UNSIGNED
12692 || (mode == Pmode
12693 && mem_mode != VOIDmode
12694 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12695 #endif
12698 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12699 break;
12701 if (!dwarf_strict
12702 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12703 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12705 dw_die_ref type_die = base_type_for_mode (mode, 1);
12706 enum machine_mode amode;
12707 if (type_die == NULL)
12708 return NULL;
12709 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12710 MODE_INT, 0);
12711 if (INTVAL (rtl) >= 0
12712 && amode != BLKmode
12713 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12714 /* const DW_OP_GNU_convert <XXX> vs.
12715 DW_OP_GNU_const_type <XXX, 1, const>. */
12716 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12717 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12719 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12720 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12721 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12722 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12723 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12724 add_loc_descr (&mem_loc_result, op0);
12725 return mem_loc_result;
12727 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12728 INTVAL (rtl));
12729 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12730 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12731 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12732 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12733 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12734 else
12736 mem_loc_result->dw_loc_oprnd2.val_class
12737 = dw_val_class_const_double;
12738 mem_loc_result->dw_loc_oprnd2.v.val_double
12739 = double_int::from_shwi (INTVAL (rtl));
12742 break;
12744 case CONST_DOUBLE:
12745 if (!dwarf_strict)
12747 dw_die_ref type_die;
12749 /* Note that a CONST_DOUBLE rtx could represent either an integer
12750 or a floating-point constant. A CONST_DOUBLE is used whenever
12751 the constant requires more than one word in order to be
12752 adequately represented. We output CONST_DOUBLEs as blocks. */
12753 if (mode == VOIDmode
12754 || (GET_MODE (rtl) == VOIDmode
12755 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12756 break;
12757 type_die = base_type_for_mode (mode,
12758 GET_MODE_CLASS (mode) == MODE_INT);
12759 if (type_die == NULL)
12760 return NULL;
12761 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12762 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12763 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12764 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12765 if (SCALAR_FLOAT_MODE_P (mode))
12767 unsigned int length = GET_MODE_SIZE (mode);
12768 unsigned char *array
12769 = (unsigned char*) ggc_alloc_atomic (length);
12771 insert_float (rtl, array);
12772 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12773 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12774 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12775 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12777 else
12779 mem_loc_result->dw_loc_oprnd2.val_class
12780 = dw_val_class_const_double;
12781 mem_loc_result->dw_loc_oprnd2.v.val_double
12782 = rtx_to_double_int (rtl);
12785 break;
12787 case EQ:
12788 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12789 break;
12791 case GE:
12792 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12793 break;
12795 case GT:
12796 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12797 break;
12799 case LE:
12800 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12801 break;
12803 case LT:
12804 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12805 break;
12807 case NE:
12808 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12809 break;
12811 case GEU:
12812 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12813 break;
12815 case GTU:
12816 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12817 break;
12819 case LEU:
12820 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12821 break;
12823 case LTU:
12824 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12825 break;
12827 case UMIN:
12828 case UMAX:
12829 if (GET_MODE_CLASS (mode) != MODE_INT)
12830 break;
12831 /* FALLTHRU */
12832 case SMIN:
12833 case SMAX:
12834 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12835 break;
12837 case ZERO_EXTRACT:
12838 case SIGN_EXTRACT:
12839 if (CONST_INT_P (XEXP (rtl, 1))
12840 && CONST_INT_P (XEXP (rtl, 2))
12841 && ((unsigned) INTVAL (XEXP (rtl, 1))
12842 + (unsigned) INTVAL (XEXP (rtl, 2))
12843 <= GET_MODE_BITSIZE (mode))
12844 && GET_MODE_CLASS (mode) == MODE_INT
12845 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12846 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12848 int shift, size;
12849 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12850 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12851 if (op0 == 0)
12852 break;
12853 if (GET_CODE (rtl) == SIGN_EXTRACT)
12854 op = DW_OP_shra;
12855 else
12856 op = DW_OP_shr;
12857 mem_loc_result = op0;
12858 size = INTVAL (XEXP (rtl, 1));
12859 shift = INTVAL (XEXP (rtl, 2));
12860 if (BITS_BIG_ENDIAN)
12861 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12862 - shift - size;
12863 if (shift + size != (int) DWARF2_ADDR_SIZE)
12865 add_loc_descr (&mem_loc_result,
12866 int_loc_descriptor (DWARF2_ADDR_SIZE
12867 - shift - size));
12868 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12870 if (size != (int) DWARF2_ADDR_SIZE)
12872 add_loc_descr (&mem_loc_result,
12873 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12874 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12877 break;
12879 case IF_THEN_ELSE:
12881 dw_loc_descr_ref op2, bra_node, drop_node;
12882 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12883 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12884 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12885 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12886 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12887 VAR_INIT_STATUS_INITIALIZED);
12888 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12889 VAR_INIT_STATUS_INITIALIZED);
12890 if (op0 == NULL || op1 == NULL || op2 == NULL)
12891 break;
12893 mem_loc_result = op1;
12894 add_loc_descr (&mem_loc_result, op2);
12895 add_loc_descr (&mem_loc_result, op0);
12896 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12897 add_loc_descr (&mem_loc_result, bra_node);
12898 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12899 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12900 add_loc_descr (&mem_loc_result, drop_node);
12901 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12902 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12904 break;
12906 case FLOAT_EXTEND:
12907 case FLOAT_TRUNCATE:
12908 case FLOAT:
12909 case UNSIGNED_FLOAT:
12910 case FIX:
12911 case UNSIGNED_FIX:
12912 if (!dwarf_strict)
12914 dw_die_ref type_die;
12915 dw_loc_descr_ref cvt;
12917 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12918 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12919 if (op0 == NULL)
12920 break;
12921 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12922 && (GET_CODE (rtl) == FLOAT
12923 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12924 <= DWARF2_ADDR_SIZE))
12926 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12927 GET_CODE (rtl) == UNSIGNED_FLOAT);
12928 if (type_die == NULL)
12929 break;
12930 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12931 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12932 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12933 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12934 add_loc_descr (&op0, cvt);
12936 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12937 if (type_die == NULL)
12938 break;
12939 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12940 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12941 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12942 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12943 add_loc_descr (&op0, cvt);
12944 if (GET_MODE_CLASS (mode) == MODE_INT
12945 && (GET_CODE (rtl) == FIX
12946 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12948 op0 = convert_descriptor_to_mode (mode, op0);
12949 if (op0 == NULL)
12950 break;
12952 mem_loc_result = op0;
12954 break;
12956 case CLZ:
12957 case CTZ:
12958 case FFS:
12959 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12960 break;
12962 case POPCOUNT:
12963 case PARITY:
12964 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12965 break;
12967 case BSWAP:
12968 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12969 break;
12971 case ROTATE:
12972 case ROTATERT:
12973 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12974 break;
12976 case COMPARE:
12977 /* In theory, we could implement the above. */
12978 /* DWARF cannot represent the unsigned compare operations
12979 natively. */
12980 case SS_MULT:
12981 case US_MULT:
12982 case SS_DIV:
12983 case US_DIV:
12984 case SS_PLUS:
12985 case US_PLUS:
12986 case SS_MINUS:
12987 case US_MINUS:
12988 case SS_NEG:
12989 case US_NEG:
12990 case SS_ABS:
12991 case SS_ASHIFT:
12992 case US_ASHIFT:
12993 case SS_TRUNCATE:
12994 case US_TRUNCATE:
12995 case UNORDERED:
12996 case ORDERED:
12997 case UNEQ:
12998 case UNGE:
12999 case UNGT:
13000 case UNLE:
13001 case UNLT:
13002 case LTGT:
13003 case FRACT_CONVERT:
13004 case UNSIGNED_FRACT_CONVERT:
13005 case SAT_FRACT:
13006 case UNSIGNED_SAT_FRACT:
13007 case SQRT:
13008 case ASM_OPERANDS:
13009 case VEC_MERGE:
13010 case VEC_SELECT:
13011 case VEC_CONCAT:
13012 case VEC_DUPLICATE:
13013 case UNSPEC:
13014 case HIGH:
13015 case FMA:
13016 case STRICT_LOW_PART:
13017 case CONST_VECTOR:
13018 case CONST_FIXED:
13019 case CLRSB:
13020 case CLOBBER:
13021 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13022 can't express it in the debug info. This can happen e.g. with some
13023 TLS UNSPECs. */
13024 break;
13026 case CONST_STRING:
13027 resolve_one_addr (&rtl, NULL);
13028 goto symref;
13030 default:
13031 #ifdef ENABLE_CHECKING
13032 print_rtl (stderr, rtl);
13033 gcc_unreachable ();
13034 #else
13035 break;
13036 #endif
13039 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13040 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13042 return mem_loc_result;
13045 /* Return a descriptor that describes the concatenation of two locations.
13046 This is typically a complex variable. */
13048 static dw_loc_descr_ref
13049 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13051 dw_loc_descr_ref cc_loc_result = NULL;
13052 dw_loc_descr_ref x0_ref
13053 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13054 dw_loc_descr_ref x1_ref
13055 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13057 if (x0_ref == 0 || x1_ref == 0)
13058 return 0;
13060 cc_loc_result = x0_ref;
13061 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13063 add_loc_descr (&cc_loc_result, x1_ref);
13064 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13066 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13067 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13069 return cc_loc_result;
13072 /* Return a descriptor that describes the concatenation of N
13073 locations. */
13075 static dw_loc_descr_ref
13076 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13078 unsigned int i;
13079 dw_loc_descr_ref cc_loc_result = NULL;
13080 unsigned int n = XVECLEN (concatn, 0);
13082 for (i = 0; i < n; ++i)
13084 dw_loc_descr_ref ref;
13085 rtx x = XVECEXP (concatn, 0, i);
13087 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13088 if (ref == NULL)
13089 return NULL;
13091 add_loc_descr (&cc_loc_result, ref);
13092 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13095 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13096 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13098 return cc_loc_result;
13101 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13102 for DEBUG_IMPLICIT_PTR RTL. */
13104 static dw_loc_descr_ref
13105 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13107 dw_loc_descr_ref ret;
13108 dw_die_ref ref;
13110 if (dwarf_strict)
13111 return NULL;
13112 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13113 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13114 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13115 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13116 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13117 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13118 if (ref)
13120 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13121 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13122 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13124 else
13126 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13127 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13129 return ret;
13132 /* Output a proper Dwarf location descriptor for a variable or parameter
13133 which is either allocated in a register or in a memory location. For a
13134 register, we just generate an OP_REG and the register number. For a
13135 memory location we provide a Dwarf postfix expression describing how to
13136 generate the (dynamic) address of the object onto the address stack.
13138 MODE is mode of the decl if this loc_descriptor is going to be used in
13139 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13140 allowed, VOIDmode otherwise.
13142 If we don't know how to describe it, return 0. */
13144 static dw_loc_descr_ref
13145 loc_descriptor (rtx rtl, enum machine_mode mode,
13146 enum var_init_status initialized)
13148 dw_loc_descr_ref loc_result = NULL;
13150 switch (GET_CODE (rtl))
13152 case SUBREG:
13153 /* The case of a subreg may arise when we have a local (register)
13154 variable or a formal (register) parameter which doesn't quite fill
13155 up an entire register. For now, just assume that it is
13156 legitimate to make the Dwarf info refer to the whole register which
13157 contains the given subreg. */
13158 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13159 loc_result = loc_descriptor (SUBREG_REG (rtl),
13160 GET_MODE (SUBREG_REG (rtl)), initialized);
13161 else
13162 goto do_default;
13163 break;
13165 case REG:
13166 loc_result = reg_loc_descriptor (rtl, initialized);
13167 break;
13169 case MEM:
13170 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13171 GET_MODE (rtl), initialized);
13172 if (loc_result == NULL)
13173 loc_result = tls_mem_loc_descriptor (rtl);
13174 if (loc_result == NULL)
13176 rtx new_rtl = avoid_constant_pool_reference (rtl);
13177 if (new_rtl != rtl)
13178 loc_result = loc_descriptor (new_rtl, mode, initialized);
13180 break;
13182 case CONCAT:
13183 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13184 initialized);
13185 break;
13187 case CONCATN:
13188 loc_result = concatn_loc_descriptor (rtl, initialized);
13189 break;
13191 case VAR_LOCATION:
13192 /* Single part. */
13193 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13195 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13196 if (GET_CODE (loc) == EXPR_LIST)
13197 loc = XEXP (loc, 0);
13198 loc_result = loc_descriptor (loc, mode, initialized);
13199 break;
13202 rtl = XEXP (rtl, 1);
13203 /* FALLTHRU */
13205 case PARALLEL:
13207 rtvec par_elems = XVEC (rtl, 0);
13208 int num_elem = GET_NUM_ELEM (par_elems);
13209 enum machine_mode mode;
13210 int i;
13212 /* Create the first one, so we have something to add to. */
13213 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13214 VOIDmode, initialized);
13215 if (loc_result == NULL)
13216 return NULL;
13217 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13218 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13219 for (i = 1; i < num_elem; i++)
13221 dw_loc_descr_ref temp;
13223 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13224 VOIDmode, initialized);
13225 if (temp == NULL)
13226 return NULL;
13227 add_loc_descr (&loc_result, temp);
13228 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13229 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13232 break;
13234 case CONST_INT:
13235 if (mode != VOIDmode && mode != BLKmode)
13236 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13237 INTVAL (rtl));
13238 break;
13240 case CONST_DOUBLE:
13241 if (mode == VOIDmode)
13242 mode = GET_MODE (rtl);
13244 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13246 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13248 /* Note that a CONST_DOUBLE rtx could represent either an integer
13249 or a floating-point constant. A CONST_DOUBLE is used whenever
13250 the constant requires more than one word in order to be
13251 adequately represented. We output CONST_DOUBLEs as blocks. */
13252 loc_result = new_loc_descr (DW_OP_implicit_value,
13253 GET_MODE_SIZE (mode), 0);
13254 if (SCALAR_FLOAT_MODE_P (mode))
13256 unsigned int length = GET_MODE_SIZE (mode);
13257 unsigned char *array
13258 = (unsigned char*) ggc_alloc_atomic (length);
13260 insert_float (rtl, array);
13261 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13262 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13263 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13264 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13266 else
13268 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13269 loc_result->dw_loc_oprnd2.v.val_double
13270 = rtx_to_double_int (rtl);
13273 break;
13275 case CONST_VECTOR:
13276 if (mode == VOIDmode)
13277 mode = GET_MODE (rtl);
13279 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13281 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13282 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13283 unsigned char *array = (unsigned char *)
13284 ggc_alloc_atomic (length * elt_size);
13285 unsigned int i;
13286 unsigned char *p;
13288 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13289 switch (GET_MODE_CLASS (mode))
13291 case MODE_VECTOR_INT:
13292 for (i = 0, p = array; i < length; i++, p += elt_size)
13294 rtx elt = CONST_VECTOR_ELT (rtl, i);
13295 double_int val = rtx_to_double_int (elt);
13297 if (elt_size <= sizeof (HOST_WIDE_INT))
13298 insert_int (val.to_shwi (), elt_size, p);
13299 else
13301 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13302 insert_double (val, p);
13305 break;
13307 case MODE_VECTOR_FLOAT:
13308 for (i = 0, p = array; i < length; i++, p += elt_size)
13310 rtx elt = CONST_VECTOR_ELT (rtl, i);
13311 insert_float (elt, p);
13313 break;
13315 default:
13316 gcc_unreachable ();
13319 loc_result = new_loc_descr (DW_OP_implicit_value,
13320 length * elt_size, 0);
13321 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13322 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13323 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13324 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13326 break;
13328 case CONST:
13329 if (mode == VOIDmode
13330 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13331 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13332 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13334 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13335 break;
13337 /* FALLTHROUGH */
13338 case SYMBOL_REF:
13339 if (!const_ok_for_output (rtl))
13340 break;
13341 case LABEL_REF:
13342 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13343 && (dwarf_version >= 4 || !dwarf_strict))
13345 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13346 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13347 vec_safe_push (used_rtx_array, rtl);
13349 break;
13351 case DEBUG_IMPLICIT_PTR:
13352 loc_result = implicit_ptr_descriptor (rtl, 0);
13353 break;
13355 case PLUS:
13356 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13357 && CONST_INT_P (XEXP (rtl, 1)))
13359 loc_result
13360 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13361 break;
13363 /* FALLTHRU */
13364 do_default:
13365 default:
13366 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13367 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13368 && dwarf_version >= 4)
13369 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13371 /* Value expression. */
13372 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13373 if (loc_result)
13374 add_loc_descr (&loc_result,
13375 new_loc_descr (DW_OP_stack_value, 0, 0));
13377 break;
13380 return loc_result;
13383 /* We need to figure out what section we should use as the base for the
13384 address ranges where a given location is valid.
13385 1. If this particular DECL has a section associated with it, use that.
13386 2. If this function has a section associated with it, use that.
13387 3. Otherwise, use the text section.
13388 XXX: If you split a variable across multiple sections, we won't notice. */
13390 static const char *
13391 secname_for_decl (const_tree decl)
13393 const char *secname;
13395 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13397 tree sectree = DECL_SECTION_NAME (decl);
13398 secname = TREE_STRING_POINTER (sectree);
13400 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13402 tree sectree = DECL_SECTION_NAME (current_function_decl);
13403 secname = TREE_STRING_POINTER (sectree);
13405 else if (cfun && in_cold_section_p)
13406 secname = crtl->subsections.cold_section_label;
13407 else
13408 secname = text_section_label;
13410 return secname;
13413 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13415 static bool
13416 decl_by_reference_p (tree decl)
13418 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13419 || TREE_CODE (decl) == VAR_DECL)
13420 && DECL_BY_REFERENCE (decl));
13423 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13424 for VARLOC. */
13426 static dw_loc_descr_ref
13427 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13428 enum var_init_status initialized)
13430 int have_address = 0;
13431 dw_loc_descr_ref descr;
13432 enum machine_mode mode;
13434 if (want_address != 2)
13436 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13437 /* Single part. */
13438 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13440 varloc = PAT_VAR_LOCATION_LOC (varloc);
13441 if (GET_CODE (varloc) == EXPR_LIST)
13442 varloc = XEXP (varloc, 0);
13443 mode = GET_MODE (varloc);
13444 if (MEM_P (varloc))
13446 rtx addr = XEXP (varloc, 0);
13447 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13448 mode, initialized);
13449 if (descr)
13450 have_address = 1;
13451 else
13453 rtx x = avoid_constant_pool_reference (varloc);
13454 if (x != varloc)
13455 descr = mem_loc_descriptor (x, mode, VOIDmode,
13456 initialized);
13459 else
13460 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13462 else
13463 return 0;
13465 else
13467 if (GET_CODE (varloc) == VAR_LOCATION)
13468 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13469 else
13470 mode = DECL_MODE (loc);
13471 descr = loc_descriptor (varloc, mode, initialized);
13472 have_address = 1;
13475 if (!descr)
13476 return 0;
13478 if (want_address == 2 && !have_address
13479 && (dwarf_version >= 4 || !dwarf_strict))
13481 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13483 expansion_failed (loc, NULL_RTX,
13484 "DWARF address size mismatch");
13485 return 0;
13487 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13488 have_address = 1;
13490 /* Show if we can't fill the request for an address. */
13491 if (want_address && !have_address)
13493 expansion_failed (loc, NULL_RTX,
13494 "Want address and only have value");
13495 return 0;
13498 /* If we've got an address and don't want one, dereference. */
13499 if (!want_address && have_address)
13501 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13502 enum dwarf_location_atom op;
13504 if (size > DWARF2_ADDR_SIZE || size == -1)
13506 expansion_failed (loc, NULL_RTX,
13507 "DWARF address size mismatch");
13508 return 0;
13510 else if (size == DWARF2_ADDR_SIZE)
13511 op = DW_OP_deref;
13512 else
13513 op = DW_OP_deref_size;
13515 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13518 return descr;
13521 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13522 if it is not possible. */
13524 static dw_loc_descr_ref
13525 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13527 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13528 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13529 else if (dwarf_version >= 3 || !dwarf_strict)
13530 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13531 else
13532 return NULL;
13535 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13536 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13538 static dw_loc_descr_ref
13539 dw_sra_loc_expr (tree decl, rtx loc)
13541 rtx p;
13542 unsigned int padsize = 0;
13543 dw_loc_descr_ref descr, *descr_tail;
13544 unsigned HOST_WIDE_INT decl_size;
13545 rtx varloc;
13546 enum var_init_status initialized;
13548 if (DECL_SIZE (decl) == NULL
13549 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13550 return NULL;
13552 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13553 descr = NULL;
13554 descr_tail = &descr;
13556 for (p = loc; p; p = XEXP (p, 1))
13558 unsigned int bitsize = decl_piece_bitsize (p);
13559 rtx loc_note = *decl_piece_varloc_ptr (p);
13560 dw_loc_descr_ref cur_descr;
13561 dw_loc_descr_ref *tail, last = NULL;
13562 unsigned int opsize = 0;
13564 if (loc_note == NULL_RTX
13565 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13567 padsize += bitsize;
13568 continue;
13570 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13571 varloc = NOTE_VAR_LOCATION (loc_note);
13572 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13573 if (cur_descr == NULL)
13575 padsize += bitsize;
13576 continue;
13579 /* Check that cur_descr either doesn't use
13580 DW_OP_*piece operations, or their sum is equal
13581 to bitsize. Otherwise we can't embed it. */
13582 for (tail = &cur_descr; *tail != NULL;
13583 tail = &(*tail)->dw_loc_next)
13584 if ((*tail)->dw_loc_opc == DW_OP_piece)
13586 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13587 * BITS_PER_UNIT;
13588 last = *tail;
13590 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13592 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13593 last = *tail;
13596 if (last != NULL && opsize != bitsize)
13598 padsize += bitsize;
13599 /* Discard the current piece of the descriptor and release any
13600 addr_table entries it uses. */
13601 remove_loc_list_addr_table_entries (cur_descr);
13602 continue;
13605 /* If there is a hole, add DW_OP_*piece after empty DWARF
13606 expression, which means that those bits are optimized out. */
13607 if (padsize)
13609 if (padsize > decl_size)
13611 remove_loc_list_addr_table_entries (cur_descr);
13612 goto discard_descr;
13614 decl_size -= padsize;
13615 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13616 if (*descr_tail == NULL)
13618 remove_loc_list_addr_table_entries (cur_descr);
13619 goto discard_descr;
13621 descr_tail = &(*descr_tail)->dw_loc_next;
13622 padsize = 0;
13624 *descr_tail = cur_descr;
13625 descr_tail = tail;
13626 if (bitsize > decl_size)
13627 goto discard_descr;
13628 decl_size -= bitsize;
13629 if (last == NULL)
13631 HOST_WIDE_INT offset = 0;
13632 if (GET_CODE (varloc) == VAR_LOCATION
13633 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13635 varloc = PAT_VAR_LOCATION_LOC (varloc);
13636 if (GET_CODE (varloc) == EXPR_LIST)
13637 varloc = XEXP (varloc, 0);
13641 if (GET_CODE (varloc) == CONST
13642 || GET_CODE (varloc) == SIGN_EXTEND
13643 || GET_CODE (varloc) == ZERO_EXTEND)
13644 varloc = XEXP (varloc, 0);
13645 else if (GET_CODE (varloc) == SUBREG)
13646 varloc = SUBREG_REG (varloc);
13647 else
13648 break;
13650 while (1);
13651 /* DW_OP_bit_size offset should be zero for register
13652 or implicit location descriptions and empty location
13653 descriptions, but for memory addresses needs big endian
13654 adjustment. */
13655 if (MEM_P (varloc))
13657 unsigned HOST_WIDE_INT memsize
13658 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13659 if (memsize != bitsize)
13661 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13662 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13663 goto discard_descr;
13664 if (memsize < bitsize)
13665 goto discard_descr;
13666 if (BITS_BIG_ENDIAN)
13667 offset = memsize - bitsize;
13671 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13672 if (*descr_tail == NULL)
13673 goto discard_descr;
13674 descr_tail = &(*descr_tail)->dw_loc_next;
13678 /* If there were any non-empty expressions, add padding till the end of
13679 the decl. */
13680 if (descr != NULL && decl_size != 0)
13682 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13683 if (*descr_tail == NULL)
13684 goto discard_descr;
13686 return descr;
13688 discard_descr:
13689 /* Discard the descriptor and release any addr_table entries it uses. */
13690 remove_loc_list_addr_table_entries (descr);
13691 return NULL;
13694 /* Return the dwarf representation of the location list LOC_LIST of
13695 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13696 function. */
13698 static dw_loc_list_ref
13699 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13701 const char *endname, *secname;
13702 rtx varloc;
13703 enum var_init_status initialized;
13704 struct var_loc_node *node;
13705 dw_loc_descr_ref descr;
13706 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13707 dw_loc_list_ref list = NULL;
13708 dw_loc_list_ref *listp = &list;
13710 /* Now that we know what section we are using for a base,
13711 actually construct the list of locations.
13712 The first location information is what is passed to the
13713 function that creates the location list, and the remaining
13714 locations just get added on to that list.
13715 Note that we only know the start address for a location
13716 (IE location changes), so to build the range, we use
13717 the range [current location start, next location start].
13718 This means we have to special case the last node, and generate
13719 a range of [last location start, end of function label]. */
13721 secname = secname_for_decl (decl);
13723 for (node = loc_list->first; node; node = node->next)
13724 if (GET_CODE (node->loc) == EXPR_LIST
13725 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13727 if (GET_CODE (node->loc) == EXPR_LIST)
13729 /* This requires DW_OP_{,bit_}piece, which is not usable
13730 inside DWARF expressions. */
13731 if (want_address != 2)
13732 continue;
13733 descr = dw_sra_loc_expr (decl, node->loc);
13734 if (descr == NULL)
13735 continue;
13737 else
13739 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13740 varloc = NOTE_VAR_LOCATION (node->loc);
13741 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13743 if (descr)
13745 bool range_across_switch = false;
13746 /* If section switch happens in between node->label
13747 and node->next->label (or end of function) and
13748 we can't emit it as a single entry list,
13749 emit two ranges, first one ending at the end
13750 of first partition and second one starting at the
13751 beginning of second partition. */
13752 if (node == loc_list->last_before_switch
13753 && (node != loc_list->first || loc_list->first->next)
13754 && current_function_decl)
13756 endname = cfun->fde->dw_fde_end;
13757 range_across_switch = true;
13759 /* The variable has a location between NODE->LABEL and
13760 NODE->NEXT->LABEL. */
13761 else if (node->next)
13762 endname = node->next->label;
13763 /* If the variable has a location at the last label
13764 it keeps its location until the end of function. */
13765 else if (!current_function_decl)
13766 endname = text_end_label;
13767 else
13769 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13770 current_function_funcdef_no);
13771 endname = ggc_strdup (label_id);
13774 *listp = new_loc_list (descr, node->label, endname, secname);
13775 if (TREE_CODE (decl) == PARM_DECL
13776 && node == loc_list->first
13777 && NOTE_P (node->loc)
13778 && strcmp (node->label, endname) == 0)
13779 (*listp)->force = true;
13780 listp = &(*listp)->dw_loc_next;
13782 if (range_across_switch)
13784 if (GET_CODE (node->loc) == EXPR_LIST)
13785 descr = dw_sra_loc_expr (decl, node->loc);
13786 else
13788 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13789 varloc = NOTE_VAR_LOCATION (node->loc);
13790 descr = dw_loc_list_1 (decl, varloc, want_address,
13791 initialized);
13793 gcc_assert (descr);
13794 /* The variable has a location between NODE->LABEL and
13795 NODE->NEXT->LABEL. */
13796 if (node->next)
13797 endname = node->next->label;
13798 else
13799 endname = cfun->fde->dw_fde_second_end;
13800 *listp = new_loc_list (descr,
13801 cfun->fde->dw_fde_second_begin,
13802 endname, secname);
13803 listp = &(*listp)->dw_loc_next;
13808 /* Try to avoid the overhead of a location list emitting a location
13809 expression instead, but only if we didn't have more than one
13810 location entry in the first place. If some entries were not
13811 representable, we don't want to pretend a single entry that was
13812 applies to the entire scope in which the variable is
13813 available. */
13814 if (list && loc_list->first->next)
13815 gen_llsym (list);
13817 return list;
13820 /* Return if the loc_list has only single element and thus can be represented
13821 as location description. */
13823 static bool
13824 single_element_loc_list_p (dw_loc_list_ref list)
13826 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13827 return !list->ll_symbol;
13830 /* To each location in list LIST add loc descr REF. */
13832 static void
13833 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13835 dw_loc_descr_ref copy;
13836 add_loc_descr (&list->expr, ref);
13837 list = list->dw_loc_next;
13838 while (list)
13840 copy = ggc_alloc_dw_loc_descr_node ();
13841 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13842 add_loc_descr (&list->expr, copy);
13843 while (copy->dw_loc_next)
13845 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13846 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13847 copy->dw_loc_next = new_copy;
13848 copy = new_copy;
13850 list = list->dw_loc_next;
13854 /* Given two lists RET and LIST
13855 produce location list that is result of adding expression in LIST
13856 to expression in RET on each position in program.
13857 Might be destructive on both RET and LIST.
13859 TODO: We handle only simple cases of RET or LIST having at most one
13860 element. General case would inolve sorting the lists in program order
13861 and merging them that will need some additional work.
13862 Adding that will improve quality of debug info especially for SRA-ed
13863 structures. */
13865 static void
13866 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13868 if (!list)
13869 return;
13870 if (!*ret)
13872 *ret = list;
13873 return;
13875 if (!list->dw_loc_next)
13877 add_loc_descr_to_each (*ret, list->expr);
13878 return;
13880 if (!(*ret)->dw_loc_next)
13882 add_loc_descr_to_each (list, (*ret)->expr);
13883 *ret = list;
13884 return;
13886 expansion_failed (NULL_TREE, NULL_RTX,
13887 "Don't know how to merge two non-trivial"
13888 " location lists.\n");
13889 *ret = NULL;
13890 return;
13893 /* LOC is constant expression. Try a luck, look it up in constant
13894 pool and return its loc_descr of its address. */
13896 static dw_loc_descr_ref
13897 cst_pool_loc_descr (tree loc)
13899 /* Get an RTL for this, if something has been emitted. */
13900 rtx rtl = lookup_constant_def (loc);
13902 if (!rtl || !MEM_P (rtl))
13904 gcc_assert (!rtl);
13905 return 0;
13907 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13909 /* TODO: We might get more coverage if we was actually delaying expansion
13910 of all expressions till end of compilation when constant pools are fully
13911 populated. */
13912 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13914 expansion_failed (loc, NULL_RTX,
13915 "CST value in contant pool but not marked.");
13916 return 0;
13918 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13919 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13922 /* Return dw_loc_list representing address of addr_expr LOC
13923 by looking for inner INDIRECT_REF expression and turning
13924 it into simple arithmetics. */
13926 static dw_loc_list_ref
13927 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13929 tree obj, offset;
13930 HOST_WIDE_INT bitsize, bitpos, bytepos;
13931 enum machine_mode mode;
13932 int unsignedp, volatilep = 0;
13933 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13935 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13936 &bitsize, &bitpos, &offset, &mode,
13937 &unsignedp, &volatilep, false);
13938 STRIP_NOPS (obj);
13939 if (bitpos % BITS_PER_UNIT)
13941 expansion_failed (loc, NULL_RTX, "bitfield access");
13942 return 0;
13944 if (!INDIRECT_REF_P (obj))
13946 expansion_failed (obj,
13947 NULL_RTX, "no indirect ref in inner refrence");
13948 return 0;
13950 if (!offset && !bitpos)
13951 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13952 else if (toplev
13953 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13954 && (dwarf_version >= 4 || !dwarf_strict))
13956 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13957 if (!list_ret)
13958 return 0;
13959 if (offset)
13961 /* Variable offset. */
13962 list_ret1 = loc_list_from_tree (offset, 0);
13963 if (list_ret1 == 0)
13964 return 0;
13965 add_loc_list (&list_ret, list_ret1);
13966 if (!list_ret)
13967 return 0;
13968 add_loc_descr_to_each (list_ret,
13969 new_loc_descr (DW_OP_plus, 0, 0));
13971 bytepos = bitpos / BITS_PER_UNIT;
13972 if (bytepos > 0)
13973 add_loc_descr_to_each (list_ret,
13974 new_loc_descr (DW_OP_plus_uconst,
13975 bytepos, 0));
13976 else if (bytepos < 0)
13977 loc_list_plus_const (list_ret, bytepos);
13978 add_loc_descr_to_each (list_ret,
13979 new_loc_descr (DW_OP_stack_value, 0, 0));
13981 return list_ret;
13985 /* Generate Dwarf location list representing LOC.
13986 If WANT_ADDRESS is false, expression computing LOC will be computed
13987 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13988 if WANT_ADDRESS is 2, expression computing address useable in location
13989 will be returned (i.e. DW_OP_reg can be used
13990 to refer to register values). */
13992 static dw_loc_list_ref
13993 loc_list_from_tree (tree loc, int want_address)
13995 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13996 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13997 int have_address = 0;
13998 enum dwarf_location_atom op;
14000 /* ??? Most of the time we do not take proper care for sign/zero
14001 extending the values properly. Hopefully this won't be a real
14002 problem... */
14004 switch (TREE_CODE (loc))
14006 case ERROR_MARK:
14007 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14008 return 0;
14010 case PLACEHOLDER_EXPR:
14011 /* This case involves extracting fields from an object to determine the
14012 position of other fields. We don't try to encode this here. The
14013 only user of this is Ada, which encodes the needed information using
14014 the names of types. */
14015 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14016 return 0;
14018 case CALL_EXPR:
14019 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14020 /* There are no opcodes for these operations. */
14021 return 0;
14023 case PREINCREMENT_EXPR:
14024 case PREDECREMENT_EXPR:
14025 case POSTINCREMENT_EXPR:
14026 case POSTDECREMENT_EXPR:
14027 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14028 /* There are no opcodes for these operations. */
14029 return 0;
14031 case ADDR_EXPR:
14032 /* If we already want an address, see if there is INDIRECT_REF inside
14033 e.g. for &this->field. */
14034 if (want_address)
14036 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14037 (loc, want_address == 2);
14038 if (list_ret)
14039 have_address = 1;
14040 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14041 && (ret = cst_pool_loc_descr (loc)))
14042 have_address = 1;
14044 /* Otherwise, process the argument and look for the address. */
14045 if (!list_ret && !ret)
14046 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14047 else
14049 if (want_address)
14050 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14051 return NULL;
14053 break;
14055 case VAR_DECL:
14056 if (DECL_THREAD_LOCAL_P (loc))
14058 rtx rtl;
14059 enum dwarf_location_atom tls_op;
14060 enum dtprel_bool dtprel = dtprel_false;
14062 if (targetm.have_tls)
14064 /* If this is not defined, we have no way to emit the
14065 data. */
14066 if (!targetm.asm_out.output_dwarf_dtprel)
14067 return 0;
14069 /* The way DW_OP_GNU_push_tls_address is specified, we
14070 can only look up addresses of objects in the current
14071 module. We used DW_OP_addr as first op, but that's
14072 wrong, because DW_OP_addr is relocated by the debug
14073 info consumer, while DW_OP_GNU_push_tls_address
14074 operand shouldn't be. */
14075 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14076 return 0;
14077 dtprel = dtprel_true;
14078 tls_op = DW_OP_GNU_push_tls_address;
14080 else
14082 if (!targetm.emutls.debug_form_tls_address
14083 || !(dwarf_version >= 3 || !dwarf_strict))
14084 return 0;
14085 /* We stuffed the control variable into the DECL_VALUE_EXPR
14086 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14087 no longer appear in gimple code. We used the control
14088 variable in specific so that we could pick it up here. */
14089 loc = DECL_VALUE_EXPR (loc);
14090 tls_op = DW_OP_form_tls_address;
14093 rtl = rtl_for_decl_location (loc);
14094 if (rtl == NULL_RTX)
14095 return 0;
14097 if (!MEM_P (rtl))
14098 return 0;
14099 rtl = XEXP (rtl, 0);
14100 if (! CONSTANT_P (rtl))
14101 return 0;
14103 ret = new_addr_loc_descr (rtl, dtprel);
14104 ret1 = new_loc_descr (tls_op, 0, 0);
14105 add_loc_descr (&ret, ret1);
14107 have_address = 1;
14108 break;
14110 /* FALLTHRU */
14112 case PARM_DECL:
14113 case RESULT_DECL:
14114 if (DECL_HAS_VALUE_EXPR_P (loc))
14115 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14116 want_address);
14117 /* FALLTHRU */
14119 case FUNCTION_DECL:
14121 rtx rtl;
14122 var_loc_list *loc_list = lookup_decl_loc (loc);
14124 if (loc_list && loc_list->first)
14126 list_ret = dw_loc_list (loc_list, loc, want_address);
14127 have_address = want_address != 0;
14128 break;
14130 rtl = rtl_for_decl_location (loc);
14131 if (rtl == NULL_RTX)
14133 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14134 return 0;
14136 else if (CONST_INT_P (rtl))
14138 HOST_WIDE_INT val = INTVAL (rtl);
14139 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14140 val &= GET_MODE_MASK (DECL_MODE (loc));
14141 ret = int_loc_descriptor (val);
14143 else if (GET_CODE (rtl) == CONST_STRING)
14145 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14146 return 0;
14148 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14149 ret = new_addr_loc_descr (rtl, dtprel_false);
14150 else
14152 enum machine_mode mode, mem_mode;
14154 /* Certain constructs can only be represented at top-level. */
14155 if (want_address == 2)
14157 ret = loc_descriptor (rtl, VOIDmode,
14158 VAR_INIT_STATUS_INITIALIZED);
14159 have_address = 1;
14161 else
14163 mode = GET_MODE (rtl);
14164 mem_mode = VOIDmode;
14165 if (MEM_P (rtl))
14167 mem_mode = mode;
14168 mode = get_address_mode (rtl);
14169 rtl = XEXP (rtl, 0);
14170 have_address = 1;
14172 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14173 VAR_INIT_STATUS_INITIALIZED);
14175 if (!ret)
14176 expansion_failed (loc, rtl,
14177 "failed to produce loc descriptor for rtl");
14180 break;
14182 case MEM_REF:
14183 /* ??? FIXME. */
14184 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14185 return 0;
14186 /* Fallthru. */
14187 case INDIRECT_REF:
14188 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14189 have_address = 1;
14190 break;
14192 case COMPOUND_EXPR:
14193 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14195 CASE_CONVERT:
14196 case VIEW_CONVERT_EXPR:
14197 case SAVE_EXPR:
14198 case MODIFY_EXPR:
14199 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14201 case COMPONENT_REF:
14202 case BIT_FIELD_REF:
14203 case ARRAY_REF:
14204 case ARRAY_RANGE_REF:
14205 case REALPART_EXPR:
14206 case IMAGPART_EXPR:
14208 tree obj, offset;
14209 HOST_WIDE_INT bitsize, bitpos, bytepos;
14210 enum machine_mode mode;
14211 int unsignedp, volatilep = 0;
14213 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14214 &unsignedp, &volatilep, false);
14216 gcc_assert (obj != loc);
14218 list_ret = loc_list_from_tree (obj,
14219 want_address == 2
14220 && !bitpos && !offset ? 2 : 1);
14221 /* TODO: We can extract value of the small expression via shifting even
14222 for nonzero bitpos. */
14223 if (list_ret == 0)
14224 return 0;
14225 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14227 expansion_failed (loc, NULL_RTX,
14228 "bitfield access");
14229 return 0;
14232 if (offset != NULL_TREE)
14234 /* Variable offset. */
14235 list_ret1 = loc_list_from_tree (offset, 0);
14236 if (list_ret1 == 0)
14237 return 0;
14238 add_loc_list (&list_ret, list_ret1);
14239 if (!list_ret)
14240 return 0;
14241 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14244 bytepos = bitpos / BITS_PER_UNIT;
14245 if (bytepos > 0)
14246 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14247 else if (bytepos < 0)
14248 loc_list_plus_const (list_ret, bytepos);
14250 have_address = 1;
14251 break;
14254 case INTEGER_CST:
14255 if ((want_address || !tree_fits_shwi_p (loc))
14256 && (ret = cst_pool_loc_descr (loc)))
14257 have_address = 1;
14258 else if (want_address == 2
14259 && tree_fits_shwi_p (loc)
14260 && (ret = address_of_int_loc_descriptor
14261 (int_size_in_bytes (TREE_TYPE (loc)),
14262 tree_to_shwi (loc))))
14263 have_address = 1;
14264 else if (tree_fits_shwi_p (loc))
14265 ret = int_loc_descriptor (tree_to_shwi (loc));
14266 else
14268 expansion_failed (loc, NULL_RTX,
14269 "Integer operand is not host integer");
14270 return 0;
14272 break;
14274 case CONSTRUCTOR:
14275 case REAL_CST:
14276 case STRING_CST:
14277 case COMPLEX_CST:
14278 if ((ret = cst_pool_loc_descr (loc)))
14279 have_address = 1;
14280 else
14281 /* We can construct small constants here using int_loc_descriptor. */
14282 expansion_failed (loc, NULL_RTX,
14283 "constructor or constant not in constant pool");
14284 break;
14286 case TRUTH_AND_EXPR:
14287 case TRUTH_ANDIF_EXPR:
14288 case BIT_AND_EXPR:
14289 op = DW_OP_and;
14290 goto do_binop;
14292 case TRUTH_XOR_EXPR:
14293 case BIT_XOR_EXPR:
14294 op = DW_OP_xor;
14295 goto do_binop;
14297 case TRUTH_OR_EXPR:
14298 case TRUTH_ORIF_EXPR:
14299 case BIT_IOR_EXPR:
14300 op = DW_OP_or;
14301 goto do_binop;
14303 case FLOOR_DIV_EXPR:
14304 case CEIL_DIV_EXPR:
14305 case ROUND_DIV_EXPR:
14306 case TRUNC_DIV_EXPR:
14307 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14308 return 0;
14309 op = DW_OP_div;
14310 goto do_binop;
14312 case MINUS_EXPR:
14313 op = DW_OP_minus;
14314 goto do_binop;
14316 case FLOOR_MOD_EXPR:
14317 case CEIL_MOD_EXPR:
14318 case ROUND_MOD_EXPR:
14319 case TRUNC_MOD_EXPR:
14320 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14322 op = DW_OP_mod;
14323 goto do_binop;
14325 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14326 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14327 if (list_ret == 0 || list_ret1 == 0)
14328 return 0;
14330 add_loc_list (&list_ret, list_ret1);
14331 if (list_ret == 0)
14332 return 0;
14333 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14334 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14335 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14336 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14337 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14338 break;
14340 case MULT_EXPR:
14341 op = DW_OP_mul;
14342 goto do_binop;
14344 case LSHIFT_EXPR:
14345 op = DW_OP_shl;
14346 goto do_binop;
14348 case RSHIFT_EXPR:
14349 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14350 goto do_binop;
14352 case POINTER_PLUS_EXPR:
14353 case PLUS_EXPR:
14354 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14356 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14357 if (list_ret == 0)
14358 return 0;
14360 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14361 break;
14364 op = DW_OP_plus;
14365 goto do_binop;
14367 case LE_EXPR:
14368 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14369 return 0;
14371 op = DW_OP_le;
14372 goto do_binop;
14374 case GE_EXPR:
14375 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14376 return 0;
14378 op = DW_OP_ge;
14379 goto do_binop;
14381 case LT_EXPR:
14382 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14383 return 0;
14385 op = DW_OP_lt;
14386 goto do_binop;
14388 case GT_EXPR:
14389 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14390 return 0;
14392 op = DW_OP_gt;
14393 goto do_binop;
14395 case EQ_EXPR:
14396 op = DW_OP_eq;
14397 goto do_binop;
14399 case NE_EXPR:
14400 op = DW_OP_ne;
14401 goto do_binop;
14403 do_binop:
14404 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14405 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14406 if (list_ret == 0 || list_ret1 == 0)
14407 return 0;
14409 add_loc_list (&list_ret, list_ret1);
14410 if (list_ret == 0)
14411 return 0;
14412 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14413 break;
14415 case TRUTH_NOT_EXPR:
14416 case BIT_NOT_EXPR:
14417 op = DW_OP_not;
14418 goto do_unop;
14420 case ABS_EXPR:
14421 op = DW_OP_abs;
14422 goto do_unop;
14424 case NEGATE_EXPR:
14425 op = DW_OP_neg;
14426 goto do_unop;
14428 do_unop:
14429 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14430 if (list_ret == 0)
14431 return 0;
14433 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14434 break;
14436 case MIN_EXPR:
14437 case MAX_EXPR:
14439 const enum tree_code code =
14440 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14442 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14443 build2 (code, integer_type_node,
14444 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14445 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14448 /* ... fall through ... */
14450 case COND_EXPR:
14452 dw_loc_descr_ref lhs
14453 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14454 dw_loc_list_ref rhs
14455 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14456 dw_loc_descr_ref bra_node, jump_node, tmp;
14458 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14459 if (list_ret == 0 || lhs == 0 || rhs == 0)
14460 return 0;
14462 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14463 add_loc_descr_to_each (list_ret, bra_node);
14465 add_loc_list (&list_ret, rhs);
14466 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14467 add_loc_descr_to_each (list_ret, jump_node);
14469 add_loc_descr_to_each (list_ret, lhs);
14470 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14471 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14473 /* ??? Need a node to point the skip at. Use a nop. */
14474 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14475 add_loc_descr_to_each (list_ret, tmp);
14476 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14477 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14479 break;
14481 case FIX_TRUNC_EXPR:
14482 return 0;
14484 default:
14485 /* Leave front-end specific codes as simply unknown. This comes
14486 up, for instance, with the C STMT_EXPR. */
14487 if ((unsigned int) TREE_CODE (loc)
14488 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14490 expansion_failed (loc, NULL_RTX,
14491 "language specific tree node");
14492 return 0;
14495 #ifdef ENABLE_CHECKING
14496 /* Otherwise this is a generic code; we should just lists all of
14497 these explicitly. We forgot one. */
14498 gcc_unreachable ();
14499 #else
14500 /* In a release build, we want to degrade gracefully: better to
14501 generate incomplete debugging information than to crash. */
14502 return NULL;
14503 #endif
14506 if (!ret && !list_ret)
14507 return 0;
14509 if (want_address == 2 && !have_address
14510 && (dwarf_version >= 4 || !dwarf_strict))
14512 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14514 expansion_failed (loc, NULL_RTX,
14515 "DWARF address size mismatch");
14516 return 0;
14518 if (ret)
14519 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14520 else
14521 add_loc_descr_to_each (list_ret,
14522 new_loc_descr (DW_OP_stack_value, 0, 0));
14523 have_address = 1;
14525 /* Show if we can't fill the request for an address. */
14526 if (want_address && !have_address)
14528 expansion_failed (loc, NULL_RTX,
14529 "Want address and only have value");
14530 return 0;
14533 gcc_assert (!ret || !list_ret);
14535 /* If we've got an address and don't want one, dereference. */
14536 if (!want_address && have_address)
14538 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14540 if (size > DWARF2_ADDR_SIZE || size == -1)
14542 expansion_failed (loc, NULL_RTX,
14543 "DWARF address size mismatch");
14544 return 0;
14546 else if (size == DWARF2_ADDR_SIZE)
14547 op = DW_OP_deref;
14548 else
14549 op = DW_OP_deref_size;
14551 if (ret)
14552 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14553 else
14554 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14556 if (ret)
14557 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14559 return list_ret;
14562 /* Same as above but return only single location expression. */
14563 static dw_loc_descr_ref
14564 loc_descriptor_from_tree (tree loc, int want_address)
14566 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14567 if (!ret)
14568 return NULL;
14569 if (ret->dw_loc_next)
14571 expansion_failed (loc, NULL_RTX,
14572 "Location list where only loc descriptor needed");
14573 return NULL;
14575 return ret->expr;
14578 /* Given a value, round it up to the lowest multiple of `boundary'
14579 which is not less than the value itself. */
14581 static inline HOST_WIDE_INT
14582 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14584 return (((value + boundary - 1) / boundary) * boundary);
14587 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14588 pointer to the declared type for the relevant field variable, or return
14589 `integer_type_node' if the given node turns out to be an
14590 ERROR_MARK node. */
14592 static inline tree
14593 field_type (const_tree decl)
14595 tree type;
14597 if (TREE_CODE (decl) == ERROR_MARK)
14598 return integer_type_node;
14600 type = DECL_BIT_FIELD_TYPE (decl);
14601 if (type == NULL_TREE)
14602 type = TREE_TYPE (decl);
14604 return type;
14607 /* Given a pointer to a tree node, return the alignment in bits for
14608 it, or else return BITS_PER_WORD if the node actually turns out to
14609 be an ERROR_MARK node. */
14611 static inline unsigned
14612 simple_type_align_in_bits (const_tree type)
14614 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14617 static inline unsigned
14618 simple_decl_align_in_bits (const_tree decl)
14620 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14623 /* Return the result of rounding T up to ALIGN. */
14625 static inline double_int
14626 round_up_to_align (double_int t, unsigned int align)
14628 double_int alignd = double_int::from_uhwi (align);
14629 t += alignd;
14630 t += double_int_minus_one;
14631 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14632 t *= alignd;
14633 return t;
14636 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14637 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14638 or return 0 if we are unable to determine what that offset is, either
14639 because the argument turns out to be a pointer to an ERROR_MARK node, or
14640 because the offset is actually variable. (We can't handle the latter case
14641 just yet). */
14643 static HOST_WIDE_INT
14644 field_byte_offset (const_tree decl)
14646 double_int object_offset_in_bits;
14647 double_int object_offset_in_bytes;
14648 double_int bitpos_int;
14650 if (TREE_CODE (decl) == ERROR_MARK)
14651 return 0;
14653 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14655 /* We cannot yet cope with fields whose positions are variable, so
14656 for now, when we see such things, we simply return 0. Someday, we may
14657 be able to handle such cases, but it will be damn difficult. */
14658 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14659 return 0;
14661 bitpos_int = tree_to_double_int (bit_position (decl));
14663 #ifdef PCC_BITFIELD_TYPE_MATTERS
14664 if (PCC_BITFIELD_TYPE_MATTERS)
14666 tree type;
14667 tree field_size_tree;
14668 double_int deepest_bitpos;
14669 double_int field_size_in_bits;
14670 unsigned int type_align_in_bits;
14671 unsigned int decl_align_in_bits;
14672 double_int type_size_in_bits;
14674 type = field_type (decl);
14675 type_size_in_bits = double_int_type_size_in_bits (type);
14676 type_align_in_bits = simple_type_align_in_bits (type);
14678 field_size_tree = DECL_SIZE (decl);
14680 /* The size could be unspecified if there was an error, or for
14681 a flexible array member. */
14682 if (!field_size_tree)
14683 field_size_tree = bitsize_zero_node;
14685 /* If the size of the field is not constant, use the type size. */
14686 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14687 field_size_in_bits = tree_to_double_int (field_size_tree);
14688 else
14689 field_size_in_bits = type_size_in_bits;
14691 decl_align_in_bits = simple_decl_align_in_bits (decl);
14693 /* The GCC front-end doesn't make any attempt to keep track of the
14694 starting bit offset (relative to the start of the containing
14695 structure type) of the hypothetical "containing object" for a
14696 bit-field. Thus, when computing the byte offset value for the
14697 start of the "containing object" of a bit-field, we must deduce
14698 this information on our own. This can be rather tricky to do in
14699 some cases. For example, handling the following structure type
14700 definition when compiling for an i386/i486 target (which only
14701 aligns long long's to 32-bit boundaries) can be very tricky:
14703 struct S { int field1; long long field2:31; };
14705 Fortunately, there is a simple rule-of-thumb which can be used
14706 in such cases. When compiling for an i386/i486, GCC will
14707 allocate 8 bytes for the structure shown above. It decides to
14708 do this based upon one simple rule for bit-field allocation.
14709 GCC allocates each "containing object" for each bit-field at
14710 the first (i.e. lowest addressed) legitimate alignment boundary
14711 (based upon the required minimum alignment for the declared
14712 type of the field) which it can possibly use, subject to the
14713 condition that there is still enough available space remaining
14714 in the containing object (when allocated at the selected point)
14715 to fully accommodate all of the bits of the bit-field itself.
14717 This simple rule makes it obvious why GCC allocates 8 bytes for
14718 each object of the structure type shown above. When looking
14719 for a place to allocate the "containing object" for `field2',
14720 the compiler simply tries to allocate a 64-bit "containing
14721 object" at each successive 32-bit boundary (starting at zero)
14722 until it finds a place to allocate that 64- bit field such that
14723 at least 31 contiguous (and previously unallocated) bits remain
14724 within that selected 64 bit field. (As it turns out, for the
14725 example above, the compiler finds it is OK to allocate the
14726 "containing object" 64-bit field at bit-offset zero within the
14727 structure type.)
14729 Here we attempt to work backwards from the limited set of facts
14730 we're given, and we try to deduce from those facts, where GCC
14731 must have believed that the containing object started (within
14732 the structure type). The value we deduce is then used (by the
14733 callers of this routine) to generate DW_AT_location and
14734 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14735 the case of DW_AT_location, regular fields as well). */
14737 /* Figure out the bit-distance from the start of the structure to
14738 the "deepest" bit of the bit-field. */
14739 deepest_bitpos = bitpos_int + field_size_in_bits;
14741 /* This is the tricky part. Use some fancy footwork to deduce
14742 where the lowest addressed bit of the containing object must
14743 be. */
14744 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14746 /* Round up to type_align by default. This works best for
14747 bitfields. */
14748 object_offset_in_bits
14749 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14751 if (object_offset_in_bits.ugt (bitpos_int))
14753 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14755 /* Round up to decl_align instead. */
14756 object_offset_in_bits
14757 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14760 else
14761 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14762 object_offset_in_bits = bitpos_int;
14764 object_offset_in_bytes
14765 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14766 true, TRUNC_DIV_EXPR);
14767 return object_offset_in_bytes.to_shwi ();
14770 /* The following routines define various Dwarf attributes and any data
14771 associated with them. */
14773 /* Add a location description attribute value to a DIE.
14775 This emits location attributes suitable for whole variables and
14776 whole parameters. Note that the location attributes for struct fields are
14777 generated by the routine `data_member_location_attribute' below. */
14779 static inline void
14780 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14781 dw_loc_list_ref descr)
14783 if (descr == 0)
14784 return;
14785 if (single_element_loc_list_p (descr))
14786 add_AT_loc (die, attr_kind, descr->expr);
14787 else
14788 add_AT_loc_list (die, attr_kind, descr);
14791 /* Add DW_AT_accessibility attribute to DIE if needed. */
14793 static void
14794 add_accessibility_attribute (dw_die_ref die, tree decl)
14796 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14797 children, otherwise the default is DW_ACCESS_public. In DWARF2
14798 the default has always been DW_ACCESS_public. */
14799 if (TREE_PROTECTED (decl))
14800 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14801 else if (TREE_PRIVATE (decl))
14803 if (dwarf_version == 2
14804 || die->die_parent == NULL
14805 || die->die_parent->die_tag != DW_TAG_class_type)
14806 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14808 else if (dwarf_version > 2
14809 && die->die_parent
14810 && die->die_parent->die_tag == DW_TAG_class_type)
14811 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14814 /* Attach the specialized form of location attribute used for data members of
14815 struct and union types. In the special case of a FIELD_DECL node which
14816 represents a bit-field, the "offset" part of this special location
14817 descriptor must indicate the distance in bytes from the lowest-addressed
14818 byte of the containing struct or union type to the lowest-addressed byte of
14819 the "containing object" for the bit-field. (See the `field_byte_offset'
14820 function above).
14822 For any given bit-field, the "containing object" is a hypothetical object
14823 (of some integral or enum type) within which the given bit-field lives. The
14824 type of this hypothetical "containing object" is always the same as the
14825 declared type of the individual bit-field itself (for GCC anyway... the
14826 DWARF spec doesn't actually mandate this). Note that it is the size (in
14827 bytes) of the hypothetical "containing object" which will be given in the
14828 DW_AT_byte_size attribute for this bit-field. (See the
14829 `byte_size_attribute' function below.) It is also used when calculating the
14830 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14831 function below.) */
14833 static void
14834 add_data_member_location_attribute (dw_die_ref die, tree decl)
14836 HOST_WIDE_INT offset;
14837 dw_loc_descr_ref loc_descr = 0;
14839 if (TREE_CODE (decl) == TREE_BINFO)
14841 /* We're working on the TAG_inheritance for a base class. */
14842 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14844 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14845 aren't at a fixed offset from all (sub)objects of the same
14846 type. We need to extract the appropriate offset from our
14847 vtable. The following dwarf expression means
14849 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14851 This is specific to the V3 ABI, of course. */
14853 dw_loc_descr_ref tmp;
14855 /* Make a copy of the object address. */
14856 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14857 add_loc_descr (&loc_descr, tmp);
14859 /* Extract the vtable address. */
14860 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14861 add_loc_descr (&loc_descr, tmp);
14863 /* Calculate the address of the offset. */
14864 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
14865 gcc_assert (offset < 0);
14867 tmp = int_loc_descriptor (-offset);
14868 add_loc_descr (&loc_descr, tmp);
14869 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14870 add_loc_descr (&loc_descr, tmp);
14872 /* Extract the offset. */
14873 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14874 add_loc_descr (&loc_descr, tmp);
14876 /* Add it to the object address. */
14877 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14878 add_loc_descr (&loc_descr, tmp);
14880 else
14881 offset = tree_to_shwi (BINFO_OFFSET (decl));
14883 else
14884 offset = field_byte_offset (decl);
14886 if (! loc_descr)
14888 if (dwarf_version > 2)
14890 /* Don't need to output a location expression, just the constant. */
14891 if (offset < 0)
14892 add_AT_int (die, DW_AT_data_member_location, offset);
14893 else
14894 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14895 return;
14897 else
14899 enum dwarf_location_atom op;
14901 /* The DWARF2 standard says that we should assume that the structure
14902 address is already on the stack, so we can specify a structure
14903 field address by using DW_OP_plus_uconst. */
14904 op = DW_OP_plus_uconst;
14905 loc_descr = new_loc_descr (op, offset, 0);
14909 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14912 /* Writes integer values to dw_vec_const array. */
14914 static void
14915 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14917 while (size != 0)
14919 *dest++ = val & 0xff;
14920 val >>= 8;
14921 --size;
14925 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14927 static HOST_WIDE_INT
14928 extract_int (const unsigned char *src, unsigned int size)
14930 HOST_WIDE_INT val = 0;
14932 src += size;
14933 while (size != 0)
14935 val <<= 8;
14936 val |= *--src & 0xff;
14937 --size;
14939 return val;
14942 /* Writes double_int values to dw_vec_const array. */
14944 static void
14945 insert_double (double_int val, unsigned char *dest)
14947 unsigned char *p0 = dest;
14948 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14950 if (WORDS_BIG_ENDIAN)
14952 p0 = p1;
14953 p1 = dest;
14956 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14957 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14960 /* Writes floating point values to dw_vec_const array. */
14962 static void
14963 insert_float (const_rtx rtl, unsigned char *array)
14965 REAL_VALUE_TYPE rv;
14966 long val[4];
14967 int i;
14969 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14970 real_to_target (val, &rv, GET_MODE (rtl));
14972 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14973 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14975 insert_int (val[i], 4, array);
14976 array += 4;
14980 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14981 does not have a "location" either in memory or in a register. These
14982 things can arise in GNU C when a constant is passed as an actual parameter
14983 to an inlined function. They can also arise in C++ where declared
14984 constants do not necessarily get memory "homes". */
14986 static bool
14987 add_const_value_attribute (dw_die_ref die, rtx rtl)
14989 switch (GET_CODE (rtl))
14991 case CONST_INT:
14993 HOST_WIDE_INT val = INTVAL (rtl);
14995 if (val < 0)
14996 add_AT_int (die, DW_AT_const_value, val);
14997 else
14998 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15000 return true;
15002 case CONST_DOUBLE:
15003 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15004 floating-point constant. A CONST_DOUBLE is used whenever the
15005 constant requires more than one word in order to be adequately
15006 represented. */
15008 enum machine_mode mode = GET_MODE (rtl);
15010 if (SCALAR_FLOAT_MODE_P (mode))
15012 unsigned int length = GET_MODE_SIZE (mode);
15013 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15015 insert_float (rtl, array);
15016 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15018 else
15019 add_AT_double (die, DW_AT_const_value,
15020 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15022 return true;
15024 case CONST_VECTOR:
15026 enum machine_mode mode = GET_MODE (rtl);
15027 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15028 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15029 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15030 (length * elt_size);
15031 unsigned int i;
15032 unsigned char *p;
15034 switch (GET_MODE_CLASS (mode))
15036 case MODE_VECTOR_INT:
15037 for (i = 0, p = array; i < length; i++, p += elt_size)
15039 rtx elt = CONST_VECTOR_ELT (rtl, i);
15040 double_int val = rtx_to_double_int (elt);
15042 if (elt_size <= sizeof (HOST_WIDE_INT))
15043 insert_int (val.to_shwi (), elt_size, p);
15044 else
15046 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15047 insert_double (val, p);
15050 break;
15052 case MODE_VECTOR_FLOAT:
15053 for (i = 0, p = array; i < length; i++, p += elt_size)
15055 rtx elt = CONST_VECTOR_ELT (rtl, i);
15056 insert_float (elt, p);
15058 break;
15060 default:
15061 gcc_unreachable ();
15064 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15066 return true;
15068 case CONST_STRING:
15069 if (dwarf_version >= 4 || !dwarf_strict)
15071 dw_loc_descr_ref loc_result;
15072 resolve_one_addr (&rtl, NULL);
15073 rtl_addr:
15074 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15075 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15076 add_AT_loc (die, DW_AT_location, loc_result);
15077 vec_safe_push (used_rtx_array, rtl);
15078 return true;
15080 return false;
15082 case CONST:
15083 if (CONSTANT_P (XEXP (rtl, 0)))
15084 return add_const_value_attribute (die, XEXP (rtl, 0));
15085 /* FALLTHROUGH */
15086 case SYMBOL_REF:
15087 if (!const_ok_for_output (rtl))
15088 return false;
15089 case LABEL_REF:
15090 if (dwarf_version >= 4 || !dwarf_strict)
15091 goto rtl_addr;
15092 return false;
15094 case PLUS:
15095 /* In cases where an inlined instance of an inline function is passed
15096 the address of an `auto' variable (which is local to the caller) we
15097 can get a situation where the DECL_RTL of the artificial local
15098 variable (for the inlining) which acts as a stand-in for the
15099 corresponding formal parameter (of the inline function) will look
15100 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15101 exactly a compile-time constant expression, but it isn't the address
15102 of the (artificial) local variable either. Rather, it represents the
15103 *value* which the artificial local variable always has during its
15104 lifetime. We currently have no way to represent such quasi-constant
15105 values in Dwarf, so for now we just punt and generate nothing. */
15106 return false;
15108 case HIGH:
15109 case CONST_FIXED:
15110 return false;
15112 case MEM:
15113 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15114 && MEM_READONLY_P (rtl)
15115 && GET_MODE (rtl) == BLKmode)
15117 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15118 return true;
15120 return false;
15122 default:
15123 /* No other kinds of rtx should be possible here. */
15124 gcc_unreachable ();
15126 return false;
15129 /* Determine whether the evaluation of EXPR references any variables
15130 or functions which aren't otherwise used (and therefore may not be
15131 output). */
15132 static tree
15133 reference_to_unused (tree * tp, int * walk_subtrees,
15134 void * data ATTRIBUTE_UNUSED)
15136 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15137 *walk_subtrees = 0;
15139 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15140 && ! TREE_ASM_WRITTEN (*tp))
15141 return *tp;
15142 /* ??? The C++ FE emits debug information for using decls, so
15143 putting gcc_unreachable here falls over. See PR31899. For now
15144 be conservative. */
15145 else if (!cgraph_global_info_ready
15146 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15147 return *tp;
15148 else if (TREE_CODE (*tp) == VAR_DECL)
15150 struct varpool_node *node = varpool_get_node (*tp);
15151 if (!node || !node->definition)
15152 return *tp;
15154 else if (TREE_CODE (*tp) == FUNCTION_DECL
15155 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15157 /* The call graph machinery must have finished analyzing,
15158 optimizing and gimplifying the CU by now.
15159 So if *TP has no call graph node associated
15160 to it, it means *TP will not be emitted. */
15161 if (!cgraph_get_node (*tp))
15162 return *tp;
15164 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15165 return *tp;
15167 return NULL_TREE;
15170 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15171 for use in a later add_const_value_attribute call. */
15173 static rtx
15174 rtl_for_decl_init (tree init, tree type)
15176 rtx rtl = NULL_RTX;
15178 STRIP_NOPS (init);
15180 /* If a variable is initialized with a string constant without embedded
15181 zeros, build CONST_STRING. */
15182 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15184 tree enttype = TREE_TYPE (type);
15185 tree domain = TYPE_DOMAIN (type);
15186 enum machine_mode mode = TYPE_MODE (enttype);
15188 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15189 && domain
15190 && integer_zerop (TYPE_MIN_VALUE (domain))
15191 && compare_tree_int (TYPE_MAX_VALUE (domain),
15192 TREE_STRING_LENGTH (init) - 1) == 0
15193 && ((size_t) TREE_STRING_LENGTH (init)
15194 == strlen (TREE_STRING_POINTER (init)) + 1))
15196 rtl = gen_rtx_CONST_STRING (VOIDmode,
15197 ggc_strdup (TREE_STRING_POINTER (init)));
15198 rtl = gen_rtx_MEM (BLKmode, rtl);
15199 MEM_READONLY_P (rtl) = 1;
15202 /* Other aggregates, and complex values, could be represented using
15203 CONCAT: FIXME! */
15204 else if (AGGREGATE_TYPE_P (type)
15205 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15206 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15207 || TREE_CODE (type) == COMPLEX_TYPE)
15209 /* Vectors only work if their mode is supported by the target.
15210 FIXME: generic vectors ought to work too. */
15211 else if (TREE_CODE (type) == VECTOR_TYPE
15212 && !VECTOR_MODE_P (TYPE_MODE (type)))
15214 /* If the initializer is something that we know will expand into an
15215 immediate RTL constant, expand it now. We must be careful not to
15216 reference variables which won't be output. */
15217 else if (initializer_constant_valid_p (init, type)
15218 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15220 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15221 possible. */
15222 if (TREE_CODE (type) == VECTOR_TYPE)
15223 switch (TREE_CODE (init))
15225 case VECTOR_CST:
15226 break;
15227 case CONSTRUCTOR:
15228 if (TREE_CONSTANT (init))
15230 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15231 bool constant_p = true;
15232 tree value;
15233 unsigned HOST_WIDE_INT ix;
15235 /* Even when ctor is constant, it might contain non-*_CST
15236 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15237 belong into VECTOR_CST nodes. */
15238 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15239 if (!CONSTANT_CLASS_P (value))
15241 constant_p = false;
15242 break;
15245 if (constant_p)
15247 init = build_vector_from_ctor (type, elts);
15248 break;
15251 /* FALLTHRU */
15253 default:
15254 return NULL;
15257 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15259 /* If expand_expr returns a MEM, it wasn't immediate. */
15260 gcc_assert (!rtl || !MEM_P (rtl));
15263 return rtl;
15266 /* Generate RTL for the variable DECL to represent its location. */
15268 static rtx
15269 rtl_for_decl_location (tree decl)
15271 rtx rtl;
15273 /* Here we have to decide where we are going to say the parameter "lives"
15274 (as far as the debugger is concerned). We only have a couple of
15275 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15277 DECL_RTL normally indicates where the parameter lives during most of the
15278 activation of the function. If optimization is enabled however, this
15279 could be either NULL or else a pseudo-reg. Both of those cases indicate
15280 that the parameter doesn't really live anywhere (as far as the code
15281 generation parts of GCC are concerned) during most of the function's
15282 activation. That will happen (for example) if the parameter is never
15283 referenced within the function.
15285 We could just generate a location descriptor here for all non-NULL
15286 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15287 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15288 where DECL_RTL is NULL or is a pseudo-reg.
15290 Note however that we can only get away with using DECL_INCOMING_RTL as
15291 a backup substitute for DECL_RTL in certain limited cases. In cases
15292 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15293 we can be sure that the parameter was passed using the same type as it is
15294 declared to have within the function, and that its DECL_INCOMING_RTL
15295 points us to a place where a value of that type is passed.
15297 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15298 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15299 because in these cases DECL_INCOMING_RTL points us to a value of some
15300 type which is *different* from the type of the parameter itself. Thus,
15301 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15302 such cases, the debugger would end up (for example) trying to fetch a
15303 `float' from a place which actually contains the first part of a
15304 `double'. That would lead to really incorrect and confusing
15305 output at debug-time.
15307 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15308 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15309 are a couple of exceptions however. On little-endian machines we can
15310 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15311 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15312 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15313 when (on a little-endian machine) a non-prototyped function has a
15314 parameter declared to be of type `short' or `char'. In such cases,
15315 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15316 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15317 passed `int' value. If the debugger then uses that address to fetch
15318 a `short' or a `char' (on a little-endian machine) the result will be
15319 the correct data, so we allow for such exceptional cases below.
15321 Note that our goal here is to describe the place where the given formal
15322 parameter lives during most of the function's activation (i.e. between the
15323 end of the prologue and the start of the epilogue). We'll do that as best
15324 as we can. Note however that if the given formal parameter is modified
15325 sometime during the execution of the function, then a stack backtrace (at
15326 debug-time) will show the function as having been called with the *new*
15327 value rather than the value which was originally passed in. This happens
15328 rarely enough that it is not a major problem, but it *is* a problem, and
15329 I'd like to fix it.
15331 A future version of dwarf2out.c may generate two additional attributes for
15332 any given DW_TAG_formal_parameter DIE which will describe the "passed
15333 type" and the "passed location" for the given formal parameter in addition
15334 to the attributes we now generate to indicate the "declared type" and the
15335 "active location" for each parameter. This additional set of attributes
15336 could be used by debuggers for stack backtraces. Separately, note that
15337 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15338 This happens (for example) for inlined-instances of inline function formal
15339 parameters which are never referenced. This really shouldn't be
15340 happening. All PARM_DECL nodes should get valid non-NULL
15341 DECL_INCOMING_RTL values. FIXME. */
15343 /* Use DECL_RTL as the "location" unless we find something better. */
15344 rtl = DECL_RTL_IF_SET (decl);
15346 /* When generating abstract instances, ignore everything except
15347 constants, symbols living in memory, and symbols living in
15348 fixed registers. */
15349 if (! reload_completed)
15351 if (rtl
15352 && (CONSTANT_P (rtl)
15353 || (MEM_P (rtl)
15354 && CONSTANT_P (XEXP (rtl, 0)))
15355 || (REG_P (rtl)
15356 && TREE_CODE (decl) == VAR_DECL
15357 && TREE_STATIC (decl))))
15359 rtl = targetm.delegitimize_address (rtl);
15360 return rtl;
15362 rtl = NULL_RTX;
15364 else if (TREE_CODE (decl) == PARM_DECL)
15366 if (rtl == NULL_RTX
15367 || is_pseudo_reg (rtl)
15368 || (MEM_P (rtl)
15369 && is_pseudo_reg (XEXP (rtl, 0))
15370 && DECL_INCOMING_RTL (decl)
15371 && MEM_P (DECL_INCOMING_RTL (decl))
15372 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15374 tree declared_type = TREE_TYPE (decl);
15375 tree passed_type = DECL_ARG_TYPE (decl);
15376 enum machine_mode dmode = TYPE_MODE (declared_type);
15377 enum machine_mode pmode = TYPE_MODE (passed_type);
15379 /* This decl represents a formal parameter which was optimized out.
15380 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15381 all cases where (rtl == NULL_RTX) just below. */
15382 if (dmode == pmode)
15383 rtl = DECL_INCOMING_RTL (decl);
15384 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15385 && SCALAR_INT_MODE_P (dmode)
15386 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15387 && DECL_INCOMING_RTL (decl))
15389 rtx inc = DECL_INCOMING_RTL (decl);
15390 if (REG_P (inc))
15391 rtl = inc;
15392 else if (MEM_P (inc))
15394 if (BYTES_BIG_ENDIAN)
15395 rtl = adjust_address_nv (inc, dmode,
15396 GET_MODE_SIZE (pmode)
15397 - GET_MODE_SIZE (dmode));
15398 else
15399 rtl = inc;
15404 /* If the parm was passed in registers, but lives on the stack, then
15405 make a big endian correction if the mode of the type of the
15406 parameter is not the same as the mode of the rtl. */
15407 /* ??? This is the same series of checks that are made in dbxout.c before
15408 we reach the big endian correction code there. It isn't clear if all
15409 of these checks are necessary here, but keeping them all is the safe
15410 thing to do. */
15411 else if (MEM_P (rtl)
15412 && XEXP (rtl, 0) != const0_rtx
15413 && ! CONSTANT_P (XEXP (rtl, 0))
15414 /* Not passed in memory. */
15415 && !MEM_P (DECL_INCOMING_RTL (decl))
15416 /* Not passed by invisible reference. */
15417 && (!REG_P (XEXP (rtl, 0))
15418 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15419 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15420 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15421 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15422 #endif
15424 /* Big endian correction check. */
15425 && BYTES_BIG_ENDIAN
15426 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15427 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15428 < UNITS_PER_WORD))
15430 enum machine_mode addr_mode = get_address_mode (rtl);
15431 int offset = (UNITS_PER_WORD
15432 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15434 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15435 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15438 else if (TREE_CODE (decl) == VAR_DECL
15439 && rtl
15440 && MEM_P (rtl)
15441 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15442 && BYTES_BIG_ENDIAN)
15444 enum machine_mode addr_mode = get_address_mode (rtl);
15445 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15446 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15448 /* If a variable is declared "register" yet is smaller than
15449 a register, then if we store the variable to memory, it
15450 looks like we're storing a register-sized value, when in
15451 fact we are not. We need to adjust the offset of the
15452 storage location to reflect the actual value's bytes,
15453 else gdb will not be able to display it. */
15454 if (rsize > dsize)
15455 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15456 plus_constant (addr_mode, XEXP (rtl, 0),
15457 rsize - dsize));
15460 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15461 and will have been substituted directly into all expressions that use it.
15462 C does not have such a concept, but C++ and other languages do. */
15463 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15464 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15466 if (rtl)
15467 rtl = targetm.delegitimize_address (rtl);
15469 /* If we don't look past the constant pool, we risk emitting a
15470 reference to a constant pool entry that isn't referenced from
15471 code, and thus is not emitted. */
15472 if (rtl)
15473 rtl = avoid_constant_pool_reference (rtl);
15475 /* Try harder to get a rtl. If this symbol ends up not being emitted
15476 in the current CU, resolve_addr will remove the expression referencing
15477 it. */
15478 if (rtl == NULL_RTX
15479 && TREE_CODE (decl) == VAR_DECL
15480 && !DECL_EXTERNAL (decl)
15481 && TREE_STATIC (decl)
15482 && DECL_NAME (decl)
15483 && !DECL_HARD_REGISTER (decl)
15484 && DECL_MODE (decl) != VOIDmode)
15486 rtl = make_decl_rtl_for_debug (decl);
15487 if (!MEM_P (rtl)
15488 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15489 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15490 rtl = NULL_RTX;
15493 return rtl;
15496 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15497 returned. If so, the decl for the COMMON block is returned, and the
15498 value is the offset into the common block for the symbol. */
15500 static tree
15501 fortran_common (tree decl, HOST_WIDE_INT *value)
15503 tree val_expr, cvar;
15504 enum machine_mode mode;
15505 HOST_WIDE_INT bitsize, bitpos;
15506 tree offset;
15507 int unsignedp, volatilep = 0;
15509 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15510 it does not have a value (the offset into the common area), or if it
15511 is thread local (as opposed to global) then it isn't common, and shouldn't
15512 be handled as such. */
15513 if (TREE_CODE (decl) != VAR_DECL
15514 || !TREE_STATIC (decl)
15515 || !DECL_HAS_VALUE_EXPR_P (decl)
15516 || !is_fortran ())
15517 return NULL_TREE;
15519 val_expr = DECL_VALUE_EXPR (decl);
15520 if (TREE_CODE (val_expr) != COMPONENT_REF)
15521 return NULL_TREE;
15523 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15524 &mode, &unsignedp, &volatilep, true);
15526 if (cvar == NULL_TREE
15527 || TREE_CODE (cvar) != VAR_DECL
15528 || DECL_ARTIFICIAL (cvar)
15529 || !TREE_PUBLIC (cvar))
15530 return NULL_TREE;
15532 *value = 0;
15533 if (offset != NULL)
15535 if (!tree_fits_shwi_p (offset))
15536 return NULL_TREE;
15537 *value = tree_to_shwi (offset);
15539 if (bitpos != 0)
15540 *value += bitpos / BITS_PER_UNIT;
15542 return cvar;
15545 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15546 data attribute for a variable or a parameter. We generate the
15547 DW_AT_const_value attribute only in those cases where the given variable
15548 or parameter does not have a true "location" either in memory or in a
15549 register. This can happen (for example) when a constant is passed as an
15550 actual argument in a call to an inline function. (It's possible that
15551 these things can crop up in other ways also.) Note that one type of
15552 constant value which can be passed into an inlined function is a constant
15553 pointer. This can happen for example if an actual argument in an inlined
15554 function call evaluates to a compile-time constant address.
15556 CACHE_P is true if it is worth caching the location list for DECL,
15557 so that future calls can reuse it rather than regenerate it from scratch.
15558 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15559 since we will need to refer to them each time the function is inlined. */
15561 static bool
15562 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15563 enum dwarf_attribute attr)
15565 rtx rtl;
15566 dw_loc_list_ref list;
15567 var_loc_list *loc_list;
15568 cached_dw_loc_list *cache;
15569 void **slot;
15571 if (TREE_CODE (decl) == ERROR_MARK)
15572 return false;
15574 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15575 || TREE_CODE (decl) == RESULT_DECL);
15577 /* Try to get some constant RTL for this decl, and use that as the value of
15578 the location. */
15580 rtl = rtl_for_decl_location (decl);
15581 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15582 && add_const_value_attribute (die, rtl))
15583 return true;
15585 /* See if we have single element location list that is equivalent to
15586 a constant value. That way we are better to use add_const_value_attribute
15587 rather than expanding constant value equivalent. */
15588 loc_list = lookup_decl_loc (decl);
15589 if (loc_list
15590 && loc_list->first
15591 && loc_list->first->next == NULL
15592 && NOTE_P (loc_list->first->loc)
15593 && NOTE_VAR_LOCATION (loc_list->first->loc)
15594 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15596 struct var_loc_node *node;
15598 node = loc_list->first;
15599 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15600 if (GET_CODE (rtl) == EXPR_LIST)
15601 rtl = XEXP (rtl, 0);
15602 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15603 && add_const_value_attribute (die, rtl))
15604 return true;
15606 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15607 list several times. See if we've already cached the contents. */
15608 list = NULL;
15609 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15610 cache_p = false;
15611 if (cache_p)
15613 cache = (cached_dw_loc_list *)
15614 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15615 if (cache)
15616 list = cache->loc_list;
15618 if (list == NULL)
15620 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15621 /* It is usually worth caching this result if the decl is from
15622 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15623 if (cache_p && list && list->dw_loc_next)
15625 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15626 DECL_UID (decl), INSERT);
15627 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15628 cache->decl_id = DECL_UID (decl);
15629 cache->loc_list = list;
15630 *slot = cache;
15633 if (list)
15635 add_AT_location_description (die, attr, list);
15636 return true;
15638 /* None of that worked, so it must not really have a location;
15639 try adding a constant value attribute from the DECL_INITIAL. */
15640 return tree_add_const_value_attribute_for_decl (die, decl);
15643 /* Add VARIABLE and DIE into deferred locations list. */
15645 static void
15646 defer_location (tree variable, dw_die_ref die)
15648 deferred_locations entry;
15649 entry.variable = variable;
15650 entry.die = die;
15651 vec_safe_push (deferred_locations_list, entry);
15654 /* Helper function for tree_add_const_value_attribute. Natively encode
15655 initializer INIT into an array. Return true if successful. */
15657 static bool
15658 native_encode_initializer (tree init, unsigned char *array, int size)
15660 tree type;
15662 if (init == NULL_TREE)
15663 return false;
15665 STRIP_NOPS (init);
15666 switch (TREE_CODE (init))
15668 case STRING_CST:
15669 type = TREE_TYPE (init);
15670 if (TREE_CODE (type) == ARRAY_TYPE)
15672 tree enttype = TREE_TYPE (type);
15673 enum machine_mode mode = TYPE_MODE (enttype);
15675 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15676 return false;
15677 if (int_size_in_bytes (type) != size)
15678 return false;
15679 if (size > TREE_STRING_LENGTH (init))
15681 memcpy (array, TREE_STRING_POINTER (init),
15682 TREE_STRING_LENGTH (init));
15683 memset (array + TREE_STRING_LENGTH (init),
15684 '\0', size - TREE_STRING_LENGTH (init));
15686 else
15687 memcpy (array, TREE_STRING_POINTER (init), size);
15688 return true;
15690 return false;
15691 case CONSTRUCTOR:
15692 type = TREE_TYPE (init);
15693 if (int_size_in_bytes (type) != size)
15694 return false;
15695 if (TREE_CODE (type) == ARRAY_TYPE)
15697 HOST_WIDE_INT min_index;
15698 unsigned HOST_WIDE_INT cnt;
15699 int curpos = 0, fieldsize;
15700 constructor_elt *ce;
15702 if (TYPE_DOMAIN (type) == NULL_TREE
15703 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15704 return false;
15706 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15707 if (fieldsize <= 0)
15708 return false;
15710 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15711 memset (array, '\0', size);
15712 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15714 tree val = ce->value;
15715 tree index = ce->index;
15716 int pos = curpos;
15717 if (index && TREE_CODE (index) == RANGE_EXPR)
15718 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15719 * fieldsize;
15720 else if (index)
15721 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15723 if (val)
15725 STRIP_NOPS (val);
15726 if (!native_encode_initializer (val, array + pos, fieldsize))
15727 return false;
15729 curpos = pos + fieldsize;
15730 if (index && TREE_CODE (index) == RANGE_EXPR)
15732 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15733 - tree_to_shwi (TREE_OPERAND (index, 0));
15734 while (count-- > 0)
15736 if (val)
15737 memcpy (array + curpos, array + pos, fieldsize);
15738 curpos += fieldsize;
15741 gcc_assert (curpos <= size);
15743 return true;
15745 else if (TREE_CODE (type) == RECORD_TYPE
15746 || TREE_CODE (type) == UNION_TYPE)
15748 tree field = NULL_TREE;
15749 unsigned HOST_WIDE_INT cnt;
15750 constructor_elt *ce;
15752 if (int_size_in_bytes (type) != size)
15753 return false;
15755 if (TREE_CODE (type) == RECORD_TYPE)
15756 field = TYPE_FIELDS (type);
15758 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15760 tree val = ce->value;
15761 int pos, fieldsize;
15763 if (ce->index != 0)
15764 field = ce->index;
15766 if (val)
15767 STRIP_NOPS (val);
15769 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15770 return false;
15772 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15773 && TYPE_DOMAIN (TREE_TYPE (field))
15774 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15775 return false;
15776 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15777 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15778 return false;
15779 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15780 pos = int_byte_position (field);
15781 gcc_assert (pos + fieldsize <= size);
15782 if (val
15783 && !native_encode_initializer (val, array + pos, fieldsize))
15784 return false;
15786 return true;
15788 return false;
15789 case VIEW_CONVERT_EXPR:
15790 case NON_LVALUE_EXPR:
15791 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15792 default:
15793 return native_encode_expr (init, array, size) == size;
15797 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15798 attribute is the const value T. */
15800 static bool
15801 tree_add_const_value_attribute (dw_die_ref die, tree t)
15803 tree init;
15804 tree type = TREE_TYPE (t);
15805 rtx rtl;
15807 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15808 return false;
15810 init = t;
15811 gcc_assert (!DECL_P (init));
15813 rtl = rtl_for_decl_init (init, type);
15814 if (rtl)
15815 return add_const_value_attribute (die, rtl);
15816 /* If the host and target are sane, try harder. */
15817 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15818 && initializer_constant_valid_p (init, type))
15820 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15821 if (size > 0 && (int) size == size)
15823 unsigned char *array = (unsigned char *)
15824 ggc_alloc_cleared_atomic (size);
15826 if (native_encode_initializer (init, array, size))
15828 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15829 return true;
15831 ggc_free (array);
15834 return false;
15837 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15838 attribute is the const value of T, where T is an integral constant
15839 variable with static storage duration
15840 (so it can't be a PARM_DECL or a RESULT_DECL). */
15842 static bool
15843 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15846 if (!decl
15847 || (TREE_CODE (decl) != VAR_DECL
15848 && TREE_CODE (decl) != CONST_DECL)
15849 || (TREE_CODE (decl) == VAR_DECL
15850 && !TREE_STATIC (decl)))
15851 return false;
15853 if (TREE_READONLY (decl)
15854 && ! TREE_THIS_VOLATILE (decl)
15855 && DECL_INITIAL (decl))
15856 /* OK */;
15857 else
15858 return false;
15860 /* Don't add DW_AT_const_value if abstract origin already has one. */
15861 if (get_AT (var_die, DW_AT_const_value))
15862 return false;
15864 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15867 /* Convert the CFI instructions for the current function into a
15868 location list. This is used for DW_AT_frame_base when we targeting
15869 a dwarf2 consumer that does not support the dwarf3
15870 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15871 expressions. */
15873 static dw_loc_list_ref
15874 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15876 int ix;
15877 dw_fde_ref fde;
15878 dw_loc_list_ref list, *list_tail;
15879 dw_cfi_ref cfi;
15880 dw_cfa_location last_cfa, next_cfa;
15881 const char *start_label, *last_label, *section;
15882 dw_cfa_location remember;
15884 fde = cfun->fde;
15885 gcc_assert (fde != NULL);
15887 section = secname_for_decl (current_function_decl);
15888 list_tail = &list;
15889 list = NULL;
15891 memset (&next_cfa, 0, sizeof (next_cfa));
15892 next_cfa.reg = INVALID_REGNUM;
15893 remember = next_cfa;
15895 start_label = fde->dw_fde_begin;
15897 /* ??? Bald assumption that the CIE opcode list does not contain
15898 advance opcodes. */
15899 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15900 lookup_cfa_1 (cfi, &next_cfa, &remember);
15902 last_cfa = next_cfa;
15903 last_label = start_label;
15905 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15907 /* If the first partition contained no CFI adjustments, the
15908 CIE opcodes apply to the whole first partition. */
15909 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15910 fde->dw_fde_begin, fde->dw_fde_end, section);
15911 list_tail =&(*list_tail)->dw_loc_next;
15912 start_label = last_label = fde->dw_fde_second_begin;
15915 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15917 switch (cfi->dw_cfi_opc)
15919 case DW_CFA_set_loc:
15920 case DW_CFA_advance_loc1:
15921 case DW_CFA_advance_loc2:
15922 case DW_CFA_advance_loc4:
15923 if (!cfa_equal_p (&last_cfa, &next_cfa))
15925 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15926 start_label, last_label, section);
15928 list_tail = &(*list_tail)->dw_loc_next;
15929 last_cfa = next_cfa;
15930 start_label = last_label;
15932 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15933 break;
15935 case DW_CFA_advance_loc:
15936 /* The encoding is complex enough that we should never emit this. */
15937 gcc_unreachable ();
15939 default:
15940 lookup_cfa_1 (cfi, &next_cfa, &remember);
15941 break;
15943 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15945 if (!cfa_equal_p (&last_cfa, &next_cfa))
15947 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15948 start_label, last_label, section);
15950 list_tail = &(*list_tail)->dw_loc_next;
15951 last_cfa = next_cfa;
15952 start_label = last_label;
15954 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15955 start_label, fde->dw_fde_end, section);
15956 list_tail = &(*list_tail)->dw_loc_next;
15957 start_label = last_label = fde->dw_fde_second_begin;
15961 if (!cfa_equal_p (&last_cfa, &next_cfa))
15963 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15964 start_label, last_label, section);
15965 list_tail = &(*list_tail)->dw_loc_next;
15966 start_label = last_label;
15969 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15970 start_label,
15971 fde->dw_fde_second_begin
15972 ? fde->dw_fde_second_end : fde->dw_fde_end,
15973 section);
15975 if (list && list->dw_loc_next)
15976 gen_llsym (list);
15978 return list;
15981 /* Compute a displacement from the "steady-state frame pointer" to the
15982 frame base (often the same as the CFA), and store it in
15983 frame_pointer_fb_offset. OFFSET is added to the displacement
15984 before the latter is negated. */
15986 static void
15987 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15989 rtx reg, elim;
15991 #ifdef FRAME_POINTER_CFA_OFFSET
15992 reg = frame_pointer_rtx;
15993 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15994 #else
15995 reg = arg_pointer_rtx;
15996 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15997 #endif
15999 elim = (ira_use_lra_p
16000 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16001 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16002 if (GET_CODE (elim) == PLUS)
16004 offset += INTVAL (XEXP (elim, 1));
16005 elim = XEXP (elim, 0);
16008 frame_pointer_fb_offset = -offset;
16010 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16011 in which to eliminate. This is because it's stack pointer isn't
16012 directly accessible as a register within the ISA. To work around
16013 this, assume that while we cannot provide a proper value for
16014 frame_pointer_fb_offset, we won't need one either. */
16015 frame_pointer_fb_offset_valid
16016 = ((SUPPORTS_STACK_ALIGNMENT
16017 && (elim == hard_frame_pointer_rtx
16018 || elim == stack_pointer_rtx))
16019 || elim == (frame_pointer_needed
16020 ? hard_frame_pointer_rtx
16021 : stack_pointer_rtx));
16024 /* Generate a DW_AT_name attribute given some string value to be included as
16025 the value of the attribute. */
16027 static void
16028 add_name_attribute (dw_die_ref die, const char *name_string)
16030 if (name_string != NULL && *name_string != 0)
16032 if (demangle_name_func)
16033 name_string = (*demangle_name_func) (name_string);
16035 add_AT_string (die, DW_AT_name, name_string);
16039 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16040 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16041 of TYPE accordingly.
16043 ??? This is a temporary measure until after we're able to generate
16044 regular DWARF for the complex Ada type system. */
16046 static void
16047 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16048 dw_die_ref context_die)
16050 tree dtype;
16051 dw_die_ref dtype_die;
16053 if (!lang_hooks.types.descriptive_type)
16054 return;
16056 dtype = lang_hooks.types.descriptive_type (type);
16057 if (!dtype)
16058 return;
16060 dtype_die = lookup_type_die (dtype);
16061 if (!dtype_die)
16063 gen_type_die (dtype, context_die);
16064 dtype_die = lookup_type_die (dtype);
16065 gcc_assert (dtype_die);
16068 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16071 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16073 static const char *
16074 comp_dir_string (void)
16076 const char *wd;
16077 char *wd1;
16078 static const char *cached_wd = NULL;
16080 if (cached_wd != NULL)
16081 return cached_wd;
16083 wd = get_src_pwd ();
16084 if (wd == NULL)
16085 return NULL;
16087 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16089 int wdlen;
16091 wdlen = strlen (wd);
16092 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16093 strcpy (wd1, wd);
16094 wd1 [wdlen] = DIR_SEPARATOR;
16095 wd1 [wdlen + 1] = 0;
16096 wd = wd1;
16099 cached_wd = remap_debug_filename (wd);
16100 return cached_wd;
16103 /* Generate a DW_AT_comp_dir attribute for DIE. */
16105 static void
16106 add_comp_dir_attribute (dw_die_ref die)
16108 const char * wd = comp_dir_string ();
16109 if (wd != NULL)
16110 add_AT_string (die, DW_AT_comp_dir, wd);
16113 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16114 default. */
16116 static int
16117 lower_bound_default (void)
16119 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16121 case DW_LANG_C:
16122 case DW_LANG_C89:
16123 case DW_LANG_C99:
16124 case DW_LANG_C_plus_plus:
16125 case DW_LANG_ObjC:
16126 case DW_LANG_ObjC_plus_plus:
16127 case DW_LANG_Java:
16128 return 0;
16129 case DW_LANG_Fortran77:
16130 case DW_LANG_Fortran90:
16131 case DW_LANG_Fortran95:
16132 return 1;
16133 case DW_LANG_UPC:
16134 case DW_LANG_D:
16135 case DW_LANG_Python:
16136 return dwarf_version >= 4 ? 0 : -1;
16137 case DW_LANG_Ada95:
16138 case DW_LANG_Ada83:
16139 case DW_LANG_Cobol74:
16140 case DW_LANG_Cobol85:
16141 case DW_LANG_Pascal83:
16142 case DW_LANG_Modula2:
16143 case DW_LANG_PLI:
16144 return dwarf_version >= 4 ? 1 : -1;
16145 default:
16146 return -1;
16150 /* Given a tree node describing an array bound (either lower or upper) output
16151 a representation for that bound. */
16153 static void
16154 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16156 switch (TREE_CODE (bound))
16158 case ERROR_MARK:
16159 return;
16161 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16162 case INTEGER_CST:
16164 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16165 int dflt;
16167 /* Use the default if possible. */
16168 if (bound_attr == DW_AT_lower_bound
16169 && tree_fits_shwi_p (bound)
16170 && (dflt = lower_bound_default ()) != -1
16171 && tree_to_shwi (bound) == dflt)
16174 /* Otherwise represent the bound as an unsigned value with the
16175 precision of its type. The precision and signedness of the
16176 type will be necessary to re-interpret it unambiguously. */
16177 else if (prec < HOST_BITS_PER_WIDE_INT)
16179 unsigned HOST_WIDE_INT mask
16180 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16181 add_AT_unsigned (subrange_die, bound_attr,
16182 TREE_INT_CST_LOW (bound) & mask);
16184 else if (prec == HOST_BITS_PER_WIDE_INT
16185 || TREE_INT_CST_HIGH (bound) == 0)
16186 add_AT_unsigned (subrange_die, bound_attr,
16187 TREE_INT_CST_LOW (bound));
16188 else
16189 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16190 TREE_INT_CST_LOW (bound));
16192 break;
16194 CASE_CONVERT:
16195 case VIEW_CONVERT_EXPR:
16196 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16197 break;
16199 case SAVE_EXPR:
16200 break;
16202 case VAR_DECL:
16203 case PARM_DECL:
16204 case RESULT_DECL:
16206 dw_die_ref decl_die = lookup_decl_die (bound);
16208 /* ??? Can this happen, or should the variable have been bound
16209 first? Probably it can, since I imagine that we try to create
16210 the types of parameters in the order in which they exist in
16211 the list, and won't have created a forward reference to a
16212 later parameter. */
16213 if (decl_die != NULL)
16215 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16216 break;
16219 /* FALLTHRU */
16221 default:
16223 /* Otherwise try to create a stack operation procedure to
16224 evaluate the value of the array bound. */
16226 dw_die_ref ctx, decl_die;
16227 dw_loc_list_ref list;
16229 list = loc_list_from_tree (bound, 2);
16230 if (list == NULL || single_element_loc_list_p (list))
16232 /* If DW_AT_*bound is not a reference nor constant, it is
16233 a DWARF expression rather than location description.
16234 For that loc_list_from_tree (bound, 0) is needed.
16235 If that fails to give a single element list,
16236 fall back to outputting this as a reference anyway. */
16237 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16238 if (list2 && single_element_loc_list_p (list2))
16240 add_AT_loc (subrange_die, bound_attr, list2->expr);
16241 break;
16244 if (list == NULL)
16245 break;
16247 if (current_function_decl == 0)
16248 ctx = comp_unit_die ();
16249 else
16250 ctx = lookup_decl_die (current_function_decl);
16252 decl_die = new_die (DW_TAG_variable, ctx, bound);
16253 add_AT_flag (decl_die, DW_AT_artificial, 1);
16254 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16255 add_AT_location_description (decl_die, DW_AT_location, list);
16256 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16257 break;
16262 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16263 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16264 Note that the block of subscript information for an array type also
16265 includes information about the element type of the given array type. */
16267 static void
16268 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16270 unsigned dimension_number;
16271 tree lower, upper;
16272 dw_die_ref subrange_die;
16274 for (dimension_number = 0;
16275 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16276 type = TREE_TYPE (type), dimension_number++)
16278 tree domain = TYPE_DOMAIN (type);
16280 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16281 break;
16283 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16284 and (in GNU C only) variable bounds. Handle all three forms
16285 here. */
16286 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16287 if (domain)
16289 /* We have an array type with specified bounds. */
16290 lower = TYPE_MIN_VALUE (domain);
16291 upper = TYPE_MAX_VALUE (domain);
16293 /* Define the index type. */
16294 if (TREE_TYPE (domain))
16296 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16297 TREE_TYPE field. We can't emit debug info for this
16298 because it is an unnamed integral type. */
16299 if (TREE_CODE (domain) == INTEGER_TYPE
16300 && TYPE_NAME (domain) == NULL_TREE
16301 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16302 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16304 else
16305 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16306 type_die);
16309 /* ??? If upper is NULL, the array has unspecified length,
16310 but it does have a lower bound. This happens with Fortran
16311 dimension arr(N:*)
16312 Since the debugger is definitely going to need to know N
16313 to produce useful results, go ahead and output the lower
16314 bound solo, and hope the debugger can cope. */
16316 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16317 if (upper)
16318 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16321 /* Otherwise we have an array type with an unspecified length. The
16322 DWARF-2 spec does not say how to handle this; let's just leave out the
16323 bounds. */
16327 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16329 static void
16330 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16332 dw_die_ref decl_die;
16333 HOST_WIDE_INT size;
16335 switch (TREE_CODE (tree_node))
16337 case ERROR_MARK:
16338 size = 0;
16339 break;
16340 case ENUMERAL_TYPE:
16341 case RECORD_TYPE:
16342 case UNION_TYPE:
16343 case QUAL_UNION_TYPE:
16344 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16345 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16347 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16348 return;
16350 size = int_size_in_bytes (tree_node);
16351 break;
16352 case FIELD_DECL:
16353 /* For a data member of a struct or union, the DW_AT_byte_size is
16354 generally given as the number of bytes normally allocated for an
16355 object of the *declared* type of the member itself. This is true
16356 even for bit-fields. */
16357 size = int_size_in_bytes (field_type (tree_node));
16358 break;
16359 default:
16360 gcc_unreachable ();
16363 /* Note that `size' might be -1 when we get to this point. If it is, that
16364 indicates that the byte size of the entity in question is variable. We
16365 have no good way of expressing this fact in Dwarf at the present time,
16366 when location description was not used by the caller code instead. */
16367 if (size >= 0)
16368 add_AT_unsigned (die, DW_AT_byte_size, size);
16371 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16372 which specifies the distance in bits from the highest order bit of the
16373 "containing object" for the bit-field to the highest order bit of the
16374 bit-field itself.
16376 For any given bit-field, the "containing object" is a hypothetical object
16377 (of some integral or enum type) within which the given bit-field lives. The
16378 type of this hypothetical "containing object" is always the same as the
16379 declared type of the individual bit-field itself. The determination of the
16380 exact location of the "containing object" for a bit-field is rather
16381 complicated. It's handled by the `field_byte_offset' function (above).
16383 Note that it is the size (in bytes) of the hypothetical "containing object"
16384 which will be given in the DW_AT_byte_size attribute for this bit-field.
16385 (See `byte_size_attribute' above). */
16387 static inline void
16388 add_bit_offset_attribute (dw_die_ref die, tree decl)
16390 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16391 tree type = DECL_BIT_FIELD_TYPE (decl);
16392 HOST_WIDE_INT bitpos_int;
16393 HOST_WIDE_INT highest_order_object_bit_offset;
16394 HOST_WIDE_INT highest_order_field_bit_offset;
16395 HOST_WIDE_INT bit_offset;
16397 /* Must be a field and a bit field. */
16398 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16400 /* We can't yet handle bit-fields whose offsets are variable, so if we
16401 encounter such things, just return without generating any attribute
16402 whatsoever. Likewise for variable or too large size. */
16403 if (! tree_fits_shwi_p (bit_position (decl))
16404 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16405 return;
16407 bitpos_int = int_bit_position (decl);
16409 /* Note that the bit offset is always the distance (in bits) from the
16410 highest-order bit of the "containing object" to the highest-order bit of
16411 the bit-field itself. Since the "high-order end" of any object or field
16412 is different on big-endian and little-endian machines, the computation
16413 below must take account of these differences. */
16414 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16415 highest_order_field_bit_offset = bitpos_int;
16417 if (! BYTES_BIG_ENDIAN)
16419 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16420 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16423 bit_offset
16424 = (! BYTES_BIG_ENDIAN
16425 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16426 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16428 if (bit_offset < 0)
16429 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16430 else
16431 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16434 /* For a FIELD_DECL node which represents a bit field, output an attribute
16435 which specifies the length in bits of the given field. */
16437 static inline void
16438 add_bit_size_attribute (dw_die_ref die, tree decl)
16440 /* Must be a field and a bit field. */
16441 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16442 && DECL_BIT_FIELD_TYPE (decl));
16444 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16445 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16448 /* If the compiled language is ANSI C, then add a 'prototyped'
16449 attribute, if arg types are given for the parameters of a function. */
16451 static inline void
16452 add_prototyped_attribute (dw_die_ref die, tree func_type)
16454 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16455 && prototype_p (func_type))
16456 add_AT_flag (die, DW_AT_prototyped, 1);
16459 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16460 by looking in either the type declaration or object declaration
16461 equate table. */
16463 static inline dw_die_ref
16464 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16466 dw_die_ref origin_die = NULL;
16468 if (TREE_CODE (origin) != FUNCTION_DECL)
16470 /* We may have gotten separated from the block for the inlined
16471 function, if we're in an exception handler or some such; make
16472 sure that the abstract function has been written out.
16474 Doing this for nested functions is wrong, however; functions are
16475 distinct units, and our context might not even be inline. */
16476 tree fn = origin;
16478 if (TYPE_P (fn))
16479 fn = TYPE_STUB_DECL (fn);
16481 fn = decl_function_context (fn);
16482 if (fn)
16483 dwarf2out_abstract_function (fn);
16486 if (DECL_P (origin))
16487 origin_die = lookup_decl_die (origin);
16488 else if (TYPE_P (origin))
16489 origin_die = lookup_type_die (origin);
16491 /* XXX: Functions that are never lowered don't always have correct block
16492 trees (in the case of java, they simply have no block tree, in some other
16493 languages). For these functions, there is nothing we can really do to
16494 output correct debug info for inlined functions in all cases. Rather
16495 than die, we'll just produce deficient debug info now, in that we will
16496 have variables without a proper abstract origin. In the future, when all
16497 functions are lowered, we should re-add a gcc_assert (origin_die)
16498 here. */
16500 if (origin_die)
16501 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16502 return origin_die;
16505 /* We do not currently support the pure_virtual attribute. */
16507 static inline void
16508 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16510 if (DECL_VINDEX (func_decl))
16512 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16514 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16515 add_AT_loc (die, DW_AT_vtable_elem_location,
16516 new_loc_descr (DW_OP_constu,
16517 tree_to_shwi (DECL_VINDEX (func_decl)),
16518 0));
16520 /* GNU extension: Record what type this method came from originally. */
16521 if (debug_info_level > DINFO_LEVEL_TERSE
16522 && DECL_CONTEXT (func_decl))
16523 add_AT_die_ref (die, DW_AT_containing_type,
16524 lookup_type_die (DECL_CONTEXT (func_decl)));
16528 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16529 given decl. This used to be a vendor extension until after DWARF 4
16530 standardized it. */
16532 static void
16533 add_linkage_attr (dw_die_ref die, tree decl)
16535 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16537 /* Mimic what assemble_name_raw does with a leading '*'. */
16538 if (name[0] == '*')
16539 name = &name[1];
16541 if (dwarf_version >= 4)
16542 add_AT_string (die, DW_AT_linkage_name, name);
16543 else
16544 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16547 /* Add source coordinate attributes for the given decl. */
16549 static void
16550 add_src_coords_attributes (dw_die_ref die, tree decl)
16552 expanded_location s;
16554 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16555 return;
16556 s = expand_location (DECL_SOURCE_LOCATION (decl));
16557 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16558 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16561 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16563 static void
16564 add_linkage_name (dw_die_ref die, tree decl)
16566 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16567 && TREE_PUBLIC (decl)
16568 && !DECL_ABSTRACT (decl)
16569 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16570 && die->die_tag != DW_TAG_member)
16572 /* Defer until we have an assembler name set. */
16573 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16575 limbo_die_node *asm_name;
16577 asm_name = ggc_alloc_cleared_limbo_die_node ();
16578 asm_name->die = die;
16579 asm_name->created_for = decl;
16580 asm_name->next = deferred_asm_name;
16581 deferred_asm_name = asm_name;
16583 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16584 add_linkage_attr (die, decl);
16588 /* Add a DW_AT_name attribute and source coordinate attribute for the
16589 given decl, but only if it actually has a name. */
16591 static void
16592 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16594 tree decl_name;
16596 decl_name = DECL_NAME (decl);
16597 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16599 const char *name = dwarf2_name (decl, 0);
16600 if (name)
16601 add_name_attribute (die, name);
16602 if (! DECL_ARTIFICIAL (decl))
16603 add_src_coords_attributes (die, decl);
16605 add_linkage_name (die, decl);
16608 #ifdef VMS_DEBUGGING_INFO
16609 /* Get the function's name, as described by its RTL. This may be different
16610 from the DECL_NAME name used in the source file. */
16611 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16613 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16614 XEXP (DECL_RTL (decl), 0), false);
16615 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16617 #endif /* VMS_DEBUGGING_INFO */
16620 #ifdef VMS_DEBUGGING_INFO
16621 /* Output the debug main pointer die for VMS */
16623 void
16624 dwarf2out_vms_debug_main_pointer (void)
16626 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16627 dw_die_ref die;
16629 /* Allocate the VMS debug main subprogram die. */
16630 die = ggc_alloc_cleared_die_node ();
16631 die->die_tag = DW_TAG_subprogram;
16632 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16633 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16634 current_function_funcdef_no);
16635 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16637 /* Make it the first child of comp_unit_die (). */
16638 die->die_parent = comp_unit_die ();
16639 if (comp_unit_die ()->die_child)
16641 die->die_sib = comp_unit_die ()->die_child->die_sib;
16642 comp_unit_die ()->die_child->die_sib = die;
16644 else
16646 die->die_sib = die;
16647 comp_unit_die ()->die_child = die;
16650 #endif /* VMS_DEBUGGING_INFO */
16652 /* Push a new declaration scope. */
16654 static void
16655 push_decl_scope (tree scope)
16657 vec_safe_push (decl_scope_table, scope);
16660 /* Pop a declaration scope. */
16662 static inline void
16663 pop_decl_scope (void)
16665 decl_scope_table->pop ();
16668 /* walk_tree helper function for uses_local_type, below. */
16670 static tree
16671 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16673 if (!TYPE_P (*tp))
16674 *walk_subtrees = 0;
16675 else
16677 tree name = TYPE_NAME (*tp);
16678 if (name && DECL_P (name) && decl_function_context (name))
16679 return *tp;
16681 return NULL_TREE;
16684 /* If TYPE involves a function-local type (including a local typedef to a
16685 non-local type), returns that type; otherwise returns NULL_TREE. */
16687 static tree
16688 uses_local_type (tree type)
16690 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16691 return used;
16694 /* Return the DIE for the scope that immediately contains this type.
16695 Non-named types that do not involve a function-local type get global
16696 scope. Named types nested in namespaces or other types get their
16697 containing scope. All other types (i.e. function-local named types) get
16698 the current active scope. */
16700 static dw_die_ref
16701 scope_die_for (tree t, dw_die_ref context_die)
16703 dw_die_ref scope_die = NULL;
16704 tree containing_scope;
16706 /* Non-types always go in the current scope. */
16707 gcc_assert (TYPE_P (t));
16709 /* Use the scope of the typedef, rather than the scope of the type
16710 it refers to. */
16711 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16712 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16713 else
16714 containing_scope = TYPE_CONTEXT (t);
16716 /* Use the containing namespace if there is one. */
16717 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16719 if (context_die == lookup_decl_die (containing_scope))
16720 /* OK */;
16721 else if (debug_info_level > DINFO_LEVEL_TERSE)
16722 context_die = get_context_die (containing_scope);
16723 else
16724 containing_scope = NULL_TREE;
16727 /* Ignore function type "scopes" from the C frontend. They mean that
16728 a tagged type is local to a parmlist of a function declarator, but
16729 that isn't useful to DWARF. */
16730 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16731 containing_scope = NULL_TREE;
16733 if (SCOPE_FILE_SCOPE_P (containing_scope))
16735 /* If T uses a local type keep it local as well, to avoid references
16736 to function-local DIEs from outside the function. */
16737 if (current_function_decl && uses_local_type (t))
16738 scope_die = context_die;
16739 else
16740 scope_die = comp_unit_die ();
16742 else if (TYPE_P (containing_scope))
16744 /* For types, we can just look up the appropriate DIE. */
16745 if (debug_info_level > DINFO_LEVEL_TERSE)
16746 scope_die = get_context_die (containing_scope);
16747 else
16749 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16750 if (scope_die == NULL)
16751 scope_die = comp_unit_die ();
16754 else
16755 scope_die = context_die;
16757 return scope_die;
16760 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16762 static inline int
16763 local_scope_p (dw_die_ref context_die)
16765 for (; context_die; context_die = context_die->die_parent)
16766 if (context_die->die_tag == DW_TAG_inlined_subroutine
16767 || context_die->die_tag == DW_TAG_subprogram)
16768 return 1;
16770 return 0;
16773 /* Returns nonzero if CONTEXT_DIE is a class. */
16775 static inline int
16776 class_scope_p (dw_die_ref context_die)
16778 return (context_die
16779 && (context_die->die_tag == DW_TAG_structure_type
16780 || context_die->die_tag == DW_TAG_class_type
16781 || context_die->die_tag == DW_TAG_interface_type
16782 || context_die->die_tag == DW_TAG_union_type));
16785 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16786 whether or not to treat a DIE in this context as a declaration. */
16788 static inline int
16789 class_or_namespace_scope_p (dw_die_ref context_die)
16791 return (class_scope_p (context_die)
16792 || (context_die && context_die->die_tag == DW_TAG_namespace));
16795 /* Many forms of DIEs require a "type description" attribute. This
16796 routine locates the proper "type descriptor" die for the type given
16797 by 'type', and adds a DW_AT_type attribute below the given die. */
16799 static void
16800 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16801 int decl_volatile, dw_die_ref context_die)
16803 enum tree_code code = TREE_CODE (type);
16804 dw_die_ref type_die = NULL;
16806 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16807 or fixed-point type, use the inner type. This is because we have no
16808 support for unnamed types in base_type_die. This can happen if this is
16809 an Ada subrange type. Correct solution is emit a subrange type die. */
16810 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16811 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16812 type = TREE_TYPE (type), code = TREE_CODE (type);
16814 if (code == ERROR_MARK
16815 /* Handle a special case. For functions whose return type is void, we
16816 generate *no* type attribute. (Note that no object may have type
16817 `void', so this only applies to function return types). */
16818 || code == VOID_TYPE)
16819 return;
16821 type_die = modified_type_die (type,
16822 decl_const || TYPE_READONLY (type),
16823 decl_volatile || TYPE_VOLATILE (type),
16824 context_die);
16826 if (type_die != NULL)
16827 add_AT_die_ref (object_die, DW_AT_type, type_die);
16830 /* Given an object die, add the calling convention attribute for the
16831 function call type. */
16832 static void
16833 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16835 enum dwarf_calling_convention value = DW_CC_normal;
16837 value = ((enum dwarf_calling_convention)
16838 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16840 if (is_fortran ()
16841 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16843 /* DWARF 2 doesn't provide a way to identify a program's source-level
16844 entry point. DW_AT_calling_convention attributes are only meant
16845 to describe functions' calling conventions. However, lacking a
16846 better way to signal the Fortran main program, we used this for
16847 a long time, following existing custom. Now, DWARF 4 has
16848 DW_AT_main_subprogram, which we add below, but some tools still
16849 rely on the old way, which we thus keep. */
16850 value = DW_CC_program;
16852 if (dwarf_version >= 4 || !dwarf_strict)
16853 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16856 /* Only add the attribute if the backend requests it, and
16857 is not DW_CC_normal. */
16858 if (value && (value != DW_CC_normal))
16859 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16862 /* Given a tree pointer to a struct, class, union, or enum type node, return
16863 a pointer to the (string) tag name for the given type, or zero if the type
16864 was declared without a tag. */
16866 static const char *
16867 type_tag (const_tree type)
16869 const char *name = 0;
16871 if (TYPE_NAME (type) != 0)
16873 tree t = 0;
16875 /* Find the IDENTIFIER_NODE for the type name. */
16876 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16877 && !TYPE_NAMELESS (type))
16878 t = TYPE_NAME (type);
16880 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16881 a TYPE_DECL node, regardless of whether or not a `typedef' was
16882 involved. */
16883 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16884 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16886 /* We want to be extra verbose. Don't call dwarf_name if
16887 DECL_NAME isn't set. The default hook for decl_printable_name
16888 doesn't like that, and in this context it's correct to return
16889 0, instead of "<anonymous>" or the like. */
16890 if (DECL_NAME (TYPE_NAME (type))
16891 && !DECL_NAMELESS (TYPE_NAME (type)))
16892 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16895 /* Now get the name as a string, or invent one. */
16896 if (!name && t != 0)
16897 name = IDENTIFIER_POINTER (t);
16900 return (name == 0 || *name == '\0') ? 0 : name;
16903 /* Return the type associated with a data member, make a special check
16904 for bit field types. */
16906 static inline tree
16907 member_declared_type (const_tree member)
16909 return (DECL_BIT_FIELD_TYPE (member)
16910 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16913 /* Get the decl's label, as described by its RTL. This may be different
16914 from the DECL_NAME name used in the source file. */
16916 #if 0
16917 static const char *
16918 decl_start_label (tree decl)
16920 rtx x;
16921 const char *fnname;
16923 x = DECL_RTL (decl);
16924 gcc_assert (MEM_P (x));
16926 x = XEXP (x, 0);
16927 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16929 fnname = XSTR (x, 0);
16930 return fnname;
16932 #endif
16934 /* These routines generate the internal representation of the DIE's for
16935 the compilation unit. Debugging information is collected by walking
16936 the declaration trees passed in from dwarf2out_decl(). */
16938 static void
16939 gen_array_type_die (tree type, dw_die_ref context_die)
16941 dw_die_ref scope_die = scope_die_for (type, context_die);
16942 dw_die_ref array_die;
16944 /* GNU compilers represent multidimensional array types as sequences of one
16945 dimensional array types whose element types are themselves array types.
16946 We sometimes squish that down to a single array_type DIE with multiple
16947 subscripts in the Dwarf debugging info. The draft Dwarf specification
16948 say that we are allowed to do this kind of compression in C, because
16949 there is no difference between an array of arrays and a multidimensional
16950 array. We don't do this for Ada to remain as close as possible to the
16951 actual representation, which is especially important against the language
16952 flexibilty wrt arrays of variable size. */
16954 bool collapse_nested_arrays = !is_ada ();
16955 tree element_type;
16957 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16958 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16959 if (TYPE_STRING_FLAG (type)
16960 && TREE_CODE (type) == ARRAY_TYPE
16961 && is_fortran ()
16962 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16964 HOST_WIDE_INT size;
16966 array_die = new_die (DW_TAG_string_type, scope_die, type);
16967 add_name_attribute (array_die, type_tag (type));
16968 equate_type_number_to_die (type, array_die);
16969 size = int_size_in_bytes (type);
16970 if (size >= 0)
16971 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16972 else if (TYPE_DOMAIN (type) != NULL_TREE
16973 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16974 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16976 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16977 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16979 size = int_size_in_bytes (TREE_TYPE (szdecl));
16980 if (loc && size > 0)
16982 add_AT_location_description (array_die, DW_AT_string_length, loc);
16983 if (size != DWARF2_ADDR_SIZE)
16984 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16987 return;
16990 array_die = new_die (DW_TAG_array_type, scope_die, type);
16991 add_name_attribute (array_die, type_tag (type));
16992 equate_type_number_to_die (type, array_die);
16994 if (TREE_CODE (type) == VECTOR_TYPE)
16995 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16997 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16998 if (is_fortran ()
16999 && TREE_CODE (type) == ARRAY_TYPE
17000 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17001 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17002 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17004 #if 0
17005 /* We default the array ordering. SDB will probably do
17006 the right things even if DW_AT_ordering is not present. It's not even
17007 an issue until we start to get into multidimensional arrays anyway. If
17008 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17009 then we'll have to put the DW_AT_ordering attribute back in. (But if
17010 and when we find out that we need to put these in, we will only do so
17011 for multidimensional arrays. */
17012 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17013 #endif
17015 if (TREE_CODE (type) == VECTOR_TYPE)
17017 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17018 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17019 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17020 add_bound_info (subrange_die, DW_AT_upper_bound,
17021 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17023 else
17024 add_subscript_info (array_die, type, collapse_nested_arrays);
17026 /* Add representation of the type of the elements of this array type and
17027 emit the corresponding DIE if we haven't done it already. */
17028 element_type = TREE_TYPE (type);
17029 if (collapse_nested_arrays)
17030 while (TREE_CODE (element_type) == ARRAY_TYPE)
17032 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17033 break;
17034 element_type = TREE_TYPE (element_type);
17037 add_type_attribute (array_die, element_type, 0, 0, context_die);
17039 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17040 if (TYPE_ARTIFICIAL (type))
17041 add_AT_flag (array_die, DW_AT_artificial, 1);
17043 if (get_AT (array_die, DW_AT_name))
17044 add_pubtype (type, array_die);
17047 static dw_loc_descr_ref
17048 descr_info_loc (tree val, tree base_decl)
17050 HOST_WIDE_INT size;
17051 dw_loc_descr_ref loc, loc2;
17052 enum dwarf_location_atom op;
17054 if (val == base_decl)
17055 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17057 switch (TREE_CODE (val))
17059 CASE_CONVERT:
17060 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17061 case VAR_DECL:
17062 return loc_descriptor_from_tree (val, 0);
17063 case INTEGER_CST:
17064 if (tree_fits_shwi_p (val))
17065 return int_loc_descriptor (tree_to_shwi (val));
17066 break;
17067 case INDIRECT_REF:
17068 size = int_size_in_bytes (TREE_TYPE (val));
17069 if (size < 0)
17070 break;
17071 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17072 if (!loc)
17073 break;
17074 if (size == DWARF2_ADDR_SIZE)
17075 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17076 else
17077 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17078 return loc;
17079 case POINTER_PLUS_EXPR:
17080 case PLUS_EXPR:
17081 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17082 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17084 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17085 if (!loc)
17086 break;
17087 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17089 else
17091 op = DW_OP_plus;
17092 do_binop:
17093 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17094 if (!loc)
17095 break;
17096 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17097 if (!loc2)
17098 break;
17099 add_loc_descr (&loc, loc2);
17100 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17102 return loc;
17103 case MINUS_EXPR:
17104 op = DW_OP_minus;
17105 goto do_binop;
17106 case MULT_EXPR:
17107 op = DW_OP_mul;
17108 goto do_binop;
17109 case EQ_EXPR:
17110 op = DW_OP_eq;
17111 goto do_binop;
17112 case NE_EXPR:
17113 op = DW_OP_ne;
17114 goto do_binop;
17115 default:
17116 break;
17118 return NULL;
17121 static void
17122 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17123 tree val, tree base_decl)
17125 dw_loc_descr_ref loc;
17127 if (tree_fits_shwi_p (val))
17129 add_AT_unsigned (die, attr, tree_to_shwi (val));
17130 return;
17133 loc = descr_info_loc (val, base_decl);
17134 if (!loc)
17135 return;
17137 add_AT_loc (die, attr, loc);
17140 /* This routine generates DIE for array with hidden descriptor, details
17141 are filled into *info by a langhook. */
17143 static void
17144 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17145 dw_die_ref context_die)
17147 dw_die_ref scope_die = scope_die_for (type, context_die);
17148 dw_die_ref array_die;
17149 int dim;
17151 array_die = new_die (DW_TAG_array_type, scope_die, type);
17152 add_name_attribute (array_die, type_tag (type));
17153 equate_type_number_to_die (type, array_die);
17155 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17156 if (is_fortran ()
17157 && info->ndimensions >= 2)
17158 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17160 if (info->data_location)
17161 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17162 info->base_decl);
17163 if (info->associated)
17164 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17165 info->base_decl);
17166 if (info->allocated)
17167 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17168 info->base_decl);
17170 for (dim = 0; dim < info->ndimensions; dim++)
17172 dw_die_ref subrange_die
17173 = new_die (DW_TAG_subrange_type, array_die, NULL);
17175 if (info->dimen[dim].lower_bound)
17177 /* If it is the default value, omit it. */
17178 int dflt;
17180 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17181 && (dflt = lower_bound_default ()) != -1
17182 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17184 else
17185 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17186 info->dimen[dim].lower_bound,
17187 info->base_decl);
17189 if (info->dimen[dim].upper_bound)
17190 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17191 info->dimen[dim].upper_bound,
17192 info->base_decl);
17193 if (info->dimen[dim].stride)
17194 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17195 info->dimen[dim].stride,
17196 info->base_decl);
17199 gen_type_die (info->element_type, context_die);
17200 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17202 if (get_AT (array_die, DW_AT_name))
17203 add_pubtype (type, array_die);
17206 #if 0
17207 static void
17208 gen_entry_point_die (tree decl, dw_die_ref context_die)
17210 tree origin = decl_ultimate_origin (decl);
17211 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17213 if (origin != NULL)
17214 add_abstract_origin_attribute (decl_die, origin);
17215 else
17217 add_name_and_src_coords_attributes (decl_die, decl);
17218 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17219 0, 0, context_die);
17222 if (DECL_ABSTRACT (decl))
17223 equate_decl_number_to_die (decl, decl_die);
17224 else
17225 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17227 #endif
17229 /* Walk through the list of incomplete types again, trying once more to
17230 emit full debugging info for them. */
17232 static void
17233 retry_incomplete_types (void)
17235 int i;
17237 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17238 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17239 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17242 /* Determine what tag to use for a record type. */
17244 static enum dwarf_tag
17245 record_type_tag (tree type)
17247 if (! lang_hooks.types.classify_record)
17248 return DW_TAG_structure_type;
17250 switch (lang_hooks.types.classify_record (type))
17252 case RECORD_IS_STRUCT:
17253 return DW_TAG_structure_type;
17255 case RECORD_IS_CLASS:
17256 return DW_TAG_class_type;
17258 case RECORD_IS_INTERFACE:
17259 if (dwarf_version >= 3 || !dwarf_strict)
17260 return DW_TAG_interface_type;
17261 return DW_TAG_structure_type;
17263 default:
17264 gcc_unreachable ();
17268 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17269 include all of the information about the enumeration values also. Each
17270 enumerated type name/value is listed as a child of the enumerated type
17271 DIE. */
17273 static dw_die_ref
17274 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17276 dw_die_ref type_die = lookup_type_die (type);
17278 if (type_die == NULL)
17280 type_die = new_die (DW_TAG_enumeration_type,
17281 scope_die_for (type, context_die), type);
17282 equate_type_number_to_die (type, type_die);
17283 add_name_attribute (type_die, type_tag (type));
17284 if (dwarf_version >= 4 || !dwarf_strict)
17286 if (ENUM_IS_SCOPED (type))
17287 add_AT_flag (type_die, DW_AT_enum_class, 1);
17288 if (ENUM_IS_OPAQUE (type))
17289 add_AT_flag (type_die, DW_AT_declaration, 1);
17292 else if (! TYPE_SIZE (type))
17293 return type_die;
17294 else
17295 remove_AT (type_die, DW_AT_declaration);
17297 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17298 given enum type is incomplete, do not generate the DW_AT_byte_size
17299 attribute or the DW_AT_element_list attribute. */
17300 if (TYPE_SIZE (type))
17302 tree link;
17304 TREE_ASM_WRITTEN (type) = 1;
17305 add_byte_size_attribute (type_die, type);
17306 if (TYPE_STUB_DECL (type) != NULL_TREE)
17308 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17309 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17312 /* If the first reference to this type was as the return type of an
17313 inline function, then it may not have a parent. Fix this now. */
17314 if (type_die->die_parent == NULL)
17315 add_child_die (scope_die_for (type, context_die), type_die);
17317 for (link = TYPE_VALUES (type);
17318 link != NULL; link = TREE_CHAIN (link))
17320 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17321 tree value = TREE_VALUE (link);
17323 add_name_attribute (enum_die,
17324 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17326 if (TREE_CODE (value) == CONST_DECL)
17327 value = DECL_INITIAL (value);
17329 if (simple_type_size_in_bits (TREE_TYPE (value))
17330 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17331 /* DWARF2 does not provide a way of indicating whether or
17332 not enumeration constants are signed or unsigned. GDB
17333 always assumes the values are signed, so we output all
17334 values as if they were signed. That means that
17335 enumeration constants with very large unsigned values
17336 will appear to have negative values in the debugger.
17338 TODO: the above comment is wrong, DWARF2 does provide
17339 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17340 This should be re-worked to use correct signed/unsigned
17341 int/double tags for all cases, instead of always treating as
17342 signed. */
17343 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17344 else
17345 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17346 that here. */
17347 add_AT_double (enum_die, DW_AT_const_value,
17348 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17351 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17352 if (TYPE_ARTIFICIAL (type))
17353 add_AT_flag (type_die, DW_AT_artificial, 1);
17355 else
17356 add_AT_flag (type_die, DW_AT_declaration, 1);
17358 add_pubtype (type, type_die);
17360 return type_die;
17363 /* Generate a DIE to represent either a real live formal parameter decl or to
17364 represent just the type of some formal parameter position in some function
17365 type.
17367 Note that this routine is a bit unusual because its argument may be a
17368 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17369 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17370 node. If it's the former then this function is being called to output a
17371 DIE to represent a formal parameter object (or some inlining thereof). If
17372 it's the latter, then this function is only being called to output a
17373 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17374 argument type of some subprogram type.
17375 If EMIT_NAME_P is true, name and source coordinate attributes
17376 are emitted. */
17378 static dw_die_ref
17379 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17380 dw_die_ref context_die)
17382 tree node_or_origin = node ? node : origin;
17383 tree ultimate_origin;
17384 dw_die_ref parm_die
17385 = new_die (DW_TAG_formal_parameter, context_die, node);
17387 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17389 case tcc_declaration:
17390 ultimate_origin = decl_ultimate_origin (node_or_origin);
17391 if (node || ultimate_origin)
17392 origin = ultimate_origin;
17393 if (origin != NULL)
17394 add_abstract_origin_attribute (parm_die, origin);
17395 else if (emit_name_p)
17396 add_name_and_src_coords_attributes (parm_die, node);
17397 if (origin == NULL
17398 || (! DECL_ABSTRACT (node_or_origin)
17399 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17400 decl_function_context
17401 (node_or_origin))))
17403 tree type = TREE_TYPE (node_or_origin);
17404 if (decl_by_reference_p (node_or_origin))
17405 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17406 context_die);
17407 else
17408 add_type_attribute (parm_die, type,
17409 TREE_READONLY (node_or_origin),
17410 TREE_THIS_VOLATILE (node_or_origin),
17411 context_die);
17413 if (origin == NULL && DECL_ARTIFICIAL (node))
17414 add_AT_flag (parm_die, DW_AT_artificial, 1);
17416 if (node && node != origin)
17417 equate_decl_number_to_die (node, parm_die);
17418 if (! DECL_ABSTRACT (node_or_origin))
17419 add_location_or_const_value_attribute (parm_die, node_or_origin,
17420 node == NULL, DW_AT_location);
17422 break;
17424 case tcc_type:
17425 /* We were called with some kind of a ..._TYPE node. */
17426 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17427 break;
17429 default:
17430 gcc_unreachable ();
17433 return parm_die;
17436 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17437 children DW_TAG_formal_parameter DIEs representing the arguments of the
17438 parameter pack.
17440 PARM_PACK must be a function parameter pack.
17441 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17442 must point to the subsequent arguments of the function PACK_ARG belongs to.
17443 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17444 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17445 following the last one for which a DIE was generated. */
17447 static dw_die_ref
17448 gen_formal_parameter_pack_die (tree parm_pack,
17449 tree pack_arg,
17450 dw_die_ref subr_die,
17451 tree *next_arg)
17453 tree arg;
17454 dw_die_ref parm_pack_die;
17456 gcc_assert (parm_pack
17457 && lang_hooks.function_parameter_pack_p (parm_pack)
17458 && subr_die);
17460 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17461 add_src_coords_attributes (parm_pack_die, parm_pack);
17463 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17465 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17466 parm_pack))
17467 break;
17468 gen_formal_parameter_die (arg, NULL,
17469 false /* Don't emit name attribute. */,
17470 parm_pack_die);
17472 if (next_arg)
17473 *next_arg = arg;
17474 return parm_pack_die;
17477 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17478 at the end of an (ANSI prototyped) formal parameters list. */
17480 static void
17481 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17483 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17486 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17487 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17488 parameters as specified in some function type specification (except for
17489 those which appear as part of a function *definition*). */
17491 static void
17492 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17494 tree link;
17495 tree formal_type = NULL;
17496 tree first_parm_type;
17497 tree arg;
17499 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17501 arg = DECL_ARGUMENTS (function_or_method_type);
17502 function_or_method_type = TREE_TYPE (function_or_method_type);
17504 else
17505 arg = NULL_TREE;
17507 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17509 /* Make our first pass over the list of formal parameter types and output a
17510 DW_TAG_formal_parameter DIE for each one. */
17511 for (link = first_parm_type; link; )
17513 dw_die_ref parm_die;
17515 formal_type = TREE_VALUE (link);
17516 if (formal_type == void_type_node)
17517 break;
17519 /* Output a (nameless) DIE to represent the formal parameter itself. */
17520 parm_die = gen_formal_parameter_die (formal_type, NULL,
17521 true /* Emit name attribute. */,
17522 context_die);
17523 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17524 && link == first_parm_type)
17526 add_AT_flag (parm_die, DW_AT_artificial, 1);
17527 if (dwarf_version >= 3 || !dwarf_strict)
17528 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17530 else if (arg && DECL_ARTIFICIAL (arg))
17531 add_AT_flag (parm_die, DW_AT_artificial, 1);
17533 link = TREE_CHAIN (link);
17534 if (arg)
17535 arg = DECL_CHAIN (arg);
17538 /* If this function type has an ellipsis, add a
17539 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17540 if (formal_type != void_type_node)
17541 gen_unspecified_parameters_die (function_or_method_type, context_die);
17543 /* Make our second (and final) pass over the list of formal parameter types
17544 and output DIEs to represent those types (as necessary). */
17545 for (link = TYPE_ARG_TYPES (function_or_method_type);
17546 link && TREE_VALUE (link);
17547 link = TREE_CHAIN (link))
17548 gen_type_die (TREE_VALUE (link), context_die);
17551 /* We want to generate the DIE for TYPE so that we can generate the
17552 die for MEMBER, which has been defined; we will need to refer back
17553 to the member declaration nested within TYPE. If we're trying to
17554 generate minimal debug info for TYPE, processing TYPE won't do the
17555 trick; we need to attach the member declaration by hand. */
17557 static void
17558 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17560 gen_type_die (type, context_die);
17562 /* If we're trying to avoid duplicate debug info, we may not have
17563 emitted the member decl for this function. Emit it now. */
17564 if (TYPE_STUB_DECL (type)
17565 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17566 && ! lookup_decl_die (member))
17568 dw_die_ref type_die;
17569 gcc_assert (!decl_ultimate_origin (member));
17571 push_decl_scope (type);
17572 type_die = lookup_type_die_strip_naming_typedef (type);
17573 if (TREE_CODE (member) == FUNCTION_DECL)
17574 gen_subprogram_die (member, type_die);
17575 else if (TREE_CODE (member) == FIELD_DECL)
17577 /* Ignore the nameless fields that are used to skip bits but handle
17578 C++ anonymous unions and structs. */
17579 if (DECL_NAME (member) != NULL_TREE
17580 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17581 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17583 gen_type_die (member_declared_type (member), type_die);
17584 gen_field_die (member, type_die);
17587 else
17588 gen_variable_die (member, NULL_TREE, type_die);
17590 pop_decl_scope ();
17594 /* Forward declare these functions, because they are mutually recursive
17595 with their set_block_* pairing functions. */
17596 static void set_decl_origin_self (tree);
17597 static void set_decl_abstract_flags (tree, int);
17599 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17600 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17601 that it points to the node itself, thus indicating that the node is its
17602 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17603 the given node is NULL, recursively descend the decl/block tree which
17604 it is the root of, and for each other ..._DECL or BLOCK node contained
17605 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17606 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17607 values to point to themselves. */
17609 static void
17610 set_block_origin_self (tree stmt)
17612 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17614 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17617 tree local_decl;
17619 for (local_decl = BLOCK_VARS (stmt);
17620 local_decl != NULL_TREE;
17621 local_decl = DECL_CHAIN (local_decl))
17622 if (! DECL_EXTERNAL (local_decl))
17623 set_decl_origin_self (local_decl); /* Potential recursion. */
17627 tree subblock;
17629 for (subblock = BLOCK_SUBBLOCKS (stmt);
17630 subblock != NULL_TREE;
17631 subblock = BLOCK_CHAIN (subblock))
17632 set_block_origin_self (subblock); /* Recurse. */
17637 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17638 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17639 node to so that it points to the node itself, thus indicating that the
17640 node represents its own (abstract) origin. Additionally, if the
17641 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17642 the decl/block tree of which the given node is the root of, and for
17643 each other ..._DECL or BLOCK node contained therein whose
17644 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17645 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17646 point to themselves. */
17648 static void
17649 set_decl_origin_self (tree decl)
17651 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17653 DECL_ABSTRACT_ORIGIN (decl) = decl;
17654 if (TREE_CODE (decl) == FUNCTION_DECL)
17656 tree arg;
17658 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17659 DECL_ABSTRACT_ORIGIN (arg) = arg;
17660 if (DECL_INITIAL (decl) != NULL_TREE
17661 && DECL_INITIAL (decl) != error_mark_node)
17662 set_block_origin_self (DECL_INITIAL (decl));
17667 /* Given a pointer to some BLOCK node, and a boolean value to set the
17668 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17669 the given block, and for all local decls and all local sub-blocks
17670 (recursively) which are contained therein. */
17672 static void
17673 set_block_abstract_flags (tree stmt, int setting)
17675 tree local_decl;
17676 tree subblock;
17677 unsigned int i;
17679 BLOCK_ABSTRACT (stmt) = setting;
17681 for (local_decl = BLOCK_VARS (stmt);
17682 local_decl != NULL_TREE;
17683 local_decl = DECL_CHAIN (local_decl))
17684 if (! DECL_EXTERNAL (local_decl))
17685 set_decl_abstract_flags (local_decl, setting);
17687 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17689 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17690 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17691 || TREE_CODE (local_decl) == PARM_DECL)
17692 set_decl_abstract_flags (local_decl, setting);
17695 for (subblock = BLOCK_SUBBLOCKS (stmt);
17696 subblock != NULL_TREE;
17697 subblock = BLOCK_CHAIN (subblock))
17698 set_block_abstract_flags (subblock, setting);
17701 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17702 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17703 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17704 set the abstract flags for all of the parameters, local vars, local
17705 blocks and sub-blocks (recursively) to the same setting. */
17707 static void
17708 set_decl_abstract_flags (tree decl, int setting)
17710 DECL_ABSTRACT (decl) = setting;
17711 if (TREE_CODE (decl) == FUNCTION_DECL)
17713 tree arg;
17715 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17716 DECL_ABSTRACT (arg) = setting;
17717 if (DECL_INITIAL (decl) != NULL_TREE
17718 && DECL_INITIAL (decl) != error_mark_node)
17719 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17723 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17724 may later generate inlined and/or out-of-line instances of. */
17726 static void
17727 dwarf2out_abstract_function (tree decl)
17729 dw_die_ref old_die;
17730 tree save_fn;
17731 tree context;
17732 int was_abstract;
17733 htab_t old_decl_loc_table;
17734 htab_t old_cached_dw_loc_list_table;
17735 int old_call_site_count, old_tail_call_site_count;
17736 struct call_arg_loc_node *old_call_arg_locations;
17738 /* Make sure we have the actual abstract inline, not a clone. */
17739 decl = DECL_ORIGIN (decl);
17741 old_die = lookup_decl_die (decl);
17742 if (old_die && get_AT (old_die, DW_AT_inline))
17743 /* We've already generated the abstract instance. */
17744 return;
17746 /* We can be called while recursively when seeing block defining inlined subroutine
17747 DIE. Be sure to not clobber the outer location table nor use it or we would
17748 get locations in abstract instantces. */
17749 old_decl_loc_table = decl_loc_table;
17750 decl_loc_table = NULL;
17751 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17752 cached_dw_loc_list_table = NULL;
17753 old_call_arg_locations = call_arg_locations;
17754 call_arg_locations = NULL;
17755 old_call_site_count = call_site_count;
17756 call_site_count = -1;
17757 old_tail_call_site_count = tail_call_site_count;
17758 tail_call_site_count = -1;
17760 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17761 we don't get confused by DECL_ABSTRACT. */
17762 if (debug_info_level > DINFO_LEVEL_TERSE)
17764 context = decl_class_context (decl);
17765 if (context)
17766 gen_type_die_for_member
17767 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17770 /* Pretend we've just finished compiling this function. */
17771 save_fn = current_function_decl;
17772 current_function_decl = decl;
17774 was_abstract = DECL_ABSTRACT (decl);
17775 set_decl_abstract_flags (decl, 1);
17776 dwarf2out_decl (decl);
17777 if (! was_abstract)
17778 set_decl_abstract_flags (decl, 0);
17780 current_function_decl = save_fn;
17781 decl_loc_table = old_decl_loc_table;
17782 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17783 call_arg_locations = old_call_arg_locations;
17784 call_site_count = old_call_site_count;
17785 tail_call_site_count = old_tail_call_site_count;
17788 /* Helper function of premark_used_types() which gets called through
17789 htab_traverse.
17791 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17792 marked as unused by prune_unused_types. */
17794 static int
17795 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17797 tree type;
17798 dw_die_ref die;
17800 type = (tree) *slot;
17801 die = lookup_type_die (type);
17802 if (die != NULL)
17803 die->die_perennial_p = 1;
17804 return 1;
17807 /* Helper function of premark_types_used_by_global_vars which gets called
17808 through htab_traverse.
17810 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17811 marked as unused by prune_unused_types. The DIE of the type is marked
17812 only if the global variable using the type will actually be emitted. */
17814 static int
17815 premark_types_used_by_global_vars_helper (void **slot,
17816 void *data ATTRIBUTE_UNUSED)
17818 struct types_used_by_vars_entry *entry;
17819 dw_die_ref die;
17821 entry = (struct types_used_by_vars_entry *) *slot;
17822 gcc_assert (entry->type != NULL
17823 && entry->var_decl != NULL);
17824 die = lookup_type_die (entry->type);
17825 if (die)
17827 /* Ask cgraph if the global variable really is to be emitted.
17828 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17829 struct varpool_node *node = varpool_get_node (entry->var_decl);
17830 if (node && node->definition)
17832 die->die_perennial_p = 1;
17833 /* Keep the parent DIEs as well. */
17834 while ((die = die->die_parent) && die->die_perennial_p == 0)
17835 die->die_perennial_p = 1;
17838 return 1;
17841 /* Mark all members of used_types_hash as perennial. */
17843 static void
17844 premark_used_types (struct function *fun)
17846 if (fun && fun->used_types_hash)
17847 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17850 /* Mark all members of types_used_by_vars_entry as perennial. */
17852 static void
17853 premark_types_used_by_global_vars (void)
17855 if (types_used_by_vars_hash)
17856 htab_traverse (types_used_by_vars_hash,
17857 premark_types_used_by_global_vars_helper, NULL);
17860 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17861 for CA_LOC call arg loc node. */
17863 static dw_die_ref
17864 gen_call_site_die (tree decl, dw_die_ref subr_die,
17865 struct call_arg_loc_node *ca_loc)
17867 dw_die_ref stmt_die = NULL, die;
17868 tree block = ca_loc->block;
17870 while (block
17871 && block != DECL_INITIAL (decl)
17872 && TREE_CODE (block) == BLOCK)
17874 if (block_map.length () > BLOCK_NUMBER (block))
17875 stmt_die = block_map[BLOCK_NUMBER (block)];
17876 if (stmt_die)
17877 break;
17878 block = BLOCK_SUPERCONTEXT (block);
17880 if (stmt_die == NULL)
17881 stmt_die = subr_die;
17882 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17883 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17884 if (ca_loc->tail_call_p)
17885 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17886 if (ca_loc->symbol_ref)
17888 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17889 if (tdie)
17890 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17891 else
17892 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17894 return die;
17897 /* Generate a DIE to represent a declared function (either file-scope or
17898 block-local). */
17900 static void
17901 gen_subprogram_die (tree decl, dw_die_ref context_die)
17903 tree origin = decl_ultimate_origin (decl);
17904 dw_die_ref subr_die;
17905 tree outer_scope;
17906 dw_die_ref old_die = lookup_decl_die (decl);
17907 int declaration = (current_function_decl != decl
17908 || class_or_namespace_scope_p (context_die));
17910 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17912 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17913 started to generate the abstract instance of an inline, decided to output
17914 its containing class, and proceeded to emit the declaration of the inline
17915 from the member list for the class. If so, DECLARATION takes priority;
17916 we'll get back to the abstract instance when done with the class. */
17918 /* The class-scope declaration DIE must be the primary DIE. */
17919 if (origin && declaration && class_or_namespace_scope_p (context_die))
17921 origin = NULL;
17922 gcc_assert (!old_die);
17925 /* Now that the C++ front end lazily declares artificial member fns, we
17926 might need to retrofit the declaration into its class. */
17927 if (!declaration && !origin && !old_die
17928 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17929 && !class_or_namespace_scope_p (context_die)
17930 && debug_info_level > DINFO_LEVEL_TERSE)
17931 old_die = force_decl_die (decl);
17933 if (origin != NULL)
17935 gcc_assert (!declaration || local_scope_p (context_die));
17937 /* Fixup die_parent for the abstract instance of a nested
17938 inline function. */
17939 if (old_die && old_die->die_parent == NULL)
17940 add_child_die (context_die, old_die);
17942 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17943 add_abstract_origin_attribute (subr_die, origin);
17944 /* This is where the actual code for a cloned function is.
17945 Let's emit linkage name attribute for it. This helps
17946 debuggers to e.g, set breakpoints into
17947 constructors/destructors when the user asks "break
17948 K::K". */
17949 add_linkage_name (subr_die, decl);
17951 else if (old_die)
17953 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17954 struct dwarf_file_data * file_index = lookup_filename (s.file);
17956 if (!get_AT_flag (old_die, DW_AT_declaration)
17957 /* We can have a normal definition following an inline one in the
17958 case of redefinition of GNU C extern inlines.
17959 It seems reasonable to use AT_specification in this case. */
17960 && !get_AT (old_die, DW_AT_inline))
17962 /* Detect and ignore this case, where we are trying to output
17963 something we have already output. */
17964 return;
17967 /* If the definition comes from the same place as the declaration,
17968 maybe use the old DIE. We always want the DIE for this function
17969 that has the *_pc attributes to be under comp_unit_die so the
17970 debugger can find it. We also need to do this for abstract
17971 instances of inlines, since the spec requires the out-of-line copy
17972 to have the same parent. For local class methods, this doesn't
17973 apply; we just use the old DIE. */
17974 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17975 && (DECL_ARTIFICIAL (decl)
17976 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17977 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17978 == (unsigned) s.line))))
17980 subr_die = old_die;
17982 /* Clear out the declaration attribute and the formal parameters.
17983 Do not remove all children, because it is possible that this
17984 declaration die was forced using force_decl_die(). In such
17985 cases die that forced declaration die (e.g. TAG_imported_module)
17986 is one of the children that we do not want to remove. */
17987 remove_AT (subr_die, DW_AT_declaration);
17988 remove_AT (subr_die, DW_AT_object_pointer);
17989 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17991 else
17993 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17994 add_AT_specification (subr_die, old_die);
17995 add_pubname (decl, subr_die);
17996 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17997 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17998 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17999 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18002 else
18004 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18006 if (TREE_PUBLIC (decl))
18007 add_AT_flag (subr_die, DW_AT_external, 1);
18009 add_name_and_src_coords_attributes (subr_die, decl);
18010 add_pubname (decl, subr_die);
18011 if (debug_info_level > DINFO_LEVEL_TERSE)
18013 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18014 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18015 0, 0, context_die);
18018 add_pure_or_virtual_attribute (subr_die, decl);
18019 if (DECL_ARTIFICIAL (decl))
18020 add_AT_flag (subr_die, DW_AT_artificial, 1);
18022 add_accessibility_attribute (subr_die, decl);
18025 if (declaration)
18027 if (!old_die || !get_AT (old_die, DW_AT_inline))
18029 add_AT_flag (subr_die, DW_AT_declaration, 1);
18031 /* If this is an explicit function declaration then generate
18032 a DW_AT_explicit attribute. */
18033 if (lang_hooks.decls.function_decl_explicit_p (decl)
18034 && (dwarf_version >= 3 || !dwarf_strict))
18035 add_AT_flag (subr_die, DW_AT_explicit, 1);
18037 /* The first time we see a member function, it is in the context of
18038 the class to which it belongs. We make sure of this by emitting
18039 the class first. The next time is the definition, which is
18040 handled above. The two may come from the same source text.
18042 Note that force_decl_die() forces function declaration die. It is
18043 later reused to represent definition. */
18044 equate_decl_number_to_die (decl, subr_die);
18047 else if (DECL_ABSTRACT (decl))
18049 if (DECL_DECLARED_INLINE_P (decl))
18051 if (cgraph_function_possibly_inlined_p (decl))
18052 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18053 else
18054 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18056 else
18058 if (cgraph_function_possibly_inlined_p (decl))
18059 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18060 else
18061 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18064 if (DECL_DECLARED_INLINE_P (decl)
18065 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18066 add_AT_flag (subr_die, DW_AT_artificial, 1);
18068 equate_decl_number_to_die (decl, subr_die);
18070 else if (!DECL_EXTERNAL (decl))
18072 HOST_WIDE_INT cfa_fb_offset;
18073 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18075 if (!old_die || !get_AT (old_die, DW_AT_inline))
18076 equate_decl_number_to_die (decl, subr_die);
18078 gcc_checking_assert (fun);
18079 if (!flag_reorder_blocks_and_partition)
18081 dw_fde_ref fde = fun->fde;
18082 if (fde->dw_fde_begin)
18084 /* We have already generated the labels. */
18085 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18086 fde->dw_fde_end, false);
18088 else
18090 /* Create start/end labels and add the range. */
18091 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18092 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18093 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18094 current_function_funcdef_no);
18095 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18096 current_function_funcdef_no);
18097 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18098 false);
18101 #if VMS_DEBUGGING_INFO
18102 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18103 Section 2.3 Prologue and Epilogue Attributes:
18104 When a breakpoint is set on entry to a function, it is generally
18105 desirable for execution to be suspended, not on the very first
18106 instruction of the function, but rather at a point after the
18107 function's frame has been set up, after any language defined local
18108 declaration processing has been completed, and before execution of
18109 the first statement of the function begins. Debuggers generally
18110 cannot properly determine where this point is. Similarly for a
18111 breakpoint set on exit from a function. The prologue and epilogue
18112 attributes allow a compiler to communicate the location(s) to use. */
18115 if (fde->dw_fde_vms_end_prologue)
18116 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18117 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18119 if (fde->dw_fde_vms_begin_epilogue)
18120 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18121 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18123 #endif
18126 else
18128 /* Generate pubnames entries for the split function code ranges. */
18129 dw_fde_ref fde = fun->fde;
18131 if (fde->dw_fde_second_begin)
18133 if (dwarf_version >= 3 || !dwarf_strict)
18135 /* We should use ranges for non-contiguous code section
18136 addresses. Use the actual code range for the initial
18137 section, since the HOT/COLD labels might precede an
18138 alignment offset. */
18139 bool range_list_added = false;
18140 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18141 fde->dw_fde_end, &range_list_added,
18142 false);
18143 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18144 fde->dw_fde_second_end,
18145 &range_list_added, false);
18146 if (range_list_added)
18147 add_ranges (NULL);
18149 else
18151 /* There is no real support in DW2 for this .. so we make
18152 a work-around. First, emit the pub name for the segment
18153 containing the function label. Then make and emit a
18154 simplified subprogram DIE for the second segment with the
18155 name pre-fixed by __hot/cold_sect_of_. We use the same
18156 linkage name for the second die so that gdb will find both
18157 sections when given "b foo". */
18158 const char *name = NULL;
18159 tree decl_name = DECL_NAME (decl);
18160 dw_die_ref seg_die;
18162 /* Do the 'primary' section. */
18163 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18164 fde->dw_fde_end, false);
18166 /* Build a minimal DIE for the secondary section. */
18167 seg_die = new_die (DW_TAG_subprogram,
18168 subr_die->die_parent, decl);
18170 if (TREE_PUBLIC (decl))
18171 add_AT_flag (seg_die, DW_AT_external, 1);
18173 if (decl_name != NULL
18174 && IDENTIFIER_POINTER (decl_name) != NULL)
18176 name = dwarf2_name (decl, 1);
18177 if (! DECL_ARTIFICIAL (decl))
18178 add_src_coords_attributes (seg_die, decl);
18180 add_linkage_name (seg_die, decl);
18182 gcc_assert (name != NULL);
18183 add_pure_or_virtual_attribute (seg_die, decl);
18184 if (DECL_ARTIFICIAL (decl))
18185 add_AT_flag (seg_die, DW_AT_artificial, 1);
18187 name = concat ("__second_sect_of_", name, NULL);
18188 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18189 fde->dw_fde_second_end, false);
18190 add_name_attribute (seg_die, name);
18191 if (want_pubnames ())
18192 add_pubname_string (name, seg_die);
18195 else
18196 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18197 false);
18200 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18202 /* We define the "frame base" as the function's CFA. This is more
18203 convenient for several reasons: (1) It's stable across the prologue
18204 and epilogue, which makes it better than just a frame pointer,
18205 (2) With dwarf3, there exists a one-byte encoding that allows us
18206 to reference the .debug_frame data by proxy, but failing that,
18207 (3) We can at least reuse the code inspection and interpretation
18208 code that determines the CFA position at various points in the
18209 function. */
18210 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18212 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18213 add_AT_loc (subr_die, DW_AT_frame_base, op);
18215 else
18217 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18218 if (list->dw_loc_next)
18219 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18220 else
18221 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18224 /* Compute a displacement from the "steady-state frame pointer" to
18225 the CFA. The former is what all stack slots and argument slots
18226 will reference in the rtl; the latter is what we've told the
18227 debugger about. We'll need to adjust all frame_base references
18228 by this displacement. */
18229 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18231 if (fun->static_chain_decl)
18232 add_AT_location_description (subr_die, DW_AT_static_link,
18233 loc_list_from_tree (fun->static_chain_decl, 2));
18236 /* Generate child dies for template paramaters. */
18237 if (debug_info_level > DINFO_LEVEL_TERSE)
18238 gen_generic_params_dies (decl);
18240 /* Now output descriptions of the arguments for this function. This gets
18241 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18242 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18243 `...' at the end of the formal parameter list. In order to find out if
18244 there was a trailing ellipsis or not, we must instead look at the type
18245 associated with the FUNCTION_DECL. This will be a node of type
18246 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18247 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18248 an ellipsis at the end. */
18250 /* In the case where we are describing a mere function declaration, all we
18251 need to do here (and all we *can* do here) is to describe the *types* of
18252 its formal parameters. */
18253 if (debug_info_level <= DINFO_LEVEL_TERSE)
18255 else if (declaration)
18256 gen_formal_types_die (decl, subr_die);
18257 else
18259 /* Generate DIEs to represent all known formal parameters. */
18260 tree parm = DECL_ARGUMENTS (decl);
18261 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18262 tree generic_decl_parm = generic_decl
18263 ? DECL_ARGUMENTS (generic_decl)
18264 : NULL;
18266 /* Now we want to walk the list of parameters of the function and
18267 emit their relevant DIEs.
18269 We consider the case of DECL being an instance of a generic function
18270 as well as it being a normal function.
18272 If DECL is an instance of a generic function we walk the
18273 parameters of the generic function declaration _and_ the parameters of
18274 DECL itself. This is useful because we want to emit specific DIEs for
18275 function parameter packs and those are declared as part of the
18276 generic function declaration. In that particular case,
18277 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18278 That DIE has children DIEs representing the set of arguments
18279 of the pack. Note that the set of pack arguments can be empty.
18280 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18281 children DIE.
18283 Otherwise, we just consider the parameters of DECL. */
18284 while (generic_decl_parm || parm)
18286 if (generic_decl_parm
18287 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18288 gen_formal_parameter_pack_die (generic_decl_parm,
18289 parm, subr_die,
18290 &parm);
18291 else if (parm && !POINTER_BOUNDS_P (parm))
18293 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18295 if (parm == DECL_ARGUMENTS (decl)
18296 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18297 && parm_die
18298 && (dwarf_version >= 3 || !dwarf_strict))
18299 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18301 parm = DECL_CHAIN (parm);
18303 else if (parm)
18304 parm = DECL_CHAIN (parm);
18306 if (generic_decl_parm)
18307 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18310 /* Decide whether we need an unspecified_parameters DIE at the end.
18311 There are 2 more cases to do this for: 1) the ansi ... declaration -
18312 this is detectable when the end of the arg list is not a
18313 void_type_node 2) an unprototyped function declaration (not a
18314 definition). This just means that we have no info about the
18315 parameters at all. */
18316 if (prototype_p (TREE_TYPE (decl)))
18318 /* This is the prototyped case, check for.... */
18319 if (stdarg_p (TREE_TYPE (decl)))
18320 gen_unspecified_parameters_die (decl, subr_die);
18322 else if (DECL_INITIAL (decl) == NULL_TREE)
18323 gen_unspecified_parameters_die (decl, subr_die);
18326 /* Output Dwarf info for all of the stuff within the body of the function
18327 (if it has one - it may be just a declaration). */
18328 outer_scope = DECL_INITIAL (decl);
18330 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18331 a function. This BLOCK actually represents the outermost binding contour
18332 for the function, i.e. the contour in which the function's formal
18333 parameters and labels get declared. Curiously, it appears that the front
18334 end doesn't actually put the PARM_DECL nodes for the current function onto
18335 the BLOCK_VARS list for this outer scope, but are strung off of the
18336 DECL_ARGUMENTS list for the function instead.
18338 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18339 the LABEL_DECL nodes for the function however, and we output DWARF info
18340 for those in decls_for_scope. Just within the `outer_scope' there will be
18341 a BLOCK node representing the function's outermost pair of curly braces,
18342 and any blocks used for the base and member initializers of a C++
18343 constructor function. */
18344 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18346 int call_site_note_count = 0;
18347 int tail_call_site_note_count = 0;
18349 /* Emit a DW_TAG_variable DIE for a named return value. */
18350 if (DECL_NAME (DECL_RESULT (decl)))
18351 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18353 current_function_has_inlines = 0;
18354 decls_for_scope (outer_scope, subr_die, 0);
18356 if (call_arg_locations && !dwarf_strict)
18358 struct call_arg_loc_node *ca_loc;
18359 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18361 dw_die_ref die = NULL;
18362 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18363 rtx arg, next_arg;
18365 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18366 arg; arg = next_arg)
18368 dw_loc_descr_ref reg, val;
18369 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18370 dw_die_ref cdie, tdie = NULL;
18372 next_arg = XEXP (arg, 1);
18373 if (REG_P (XEXP (XEXP (arg, 0), 0))
18374 && next_arg
18375 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18376 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18377 && REGNO (XEXP (XEXP (arg, 0), 0))
18378 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18379 next_arg = XEXP (next_arg, 1);
18380 if (mode == VOIDmode)
18382 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18383 if (mode == VOIDmode)
18384 mode = GET_MODE (XEXP (arg, 0));
18386 if (mode == VOIDmode || mode == BLKmode)
18387 continue;
18388 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18390 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18391 tloc = XEXP (XEXP (arg, 0), 1);
18392 continue;
18394 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18395 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18397 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18398 tlocc = XEXP (XEXP (arg, 0), 1);
18399 continue;
18401 reg = NULL;
18402 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18403 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18404 VAR_INIT_STATUS_INITIALIZED);
18405 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18407 rtx mem = XEXP (XEXP (arg, 0), 0);
18408 reg = mem_loc_descriptor (XEXP (mem, 0),
18409 get_address_mode (mem),
18410 GET_MODE (mem),
18411 VAR_INIT_STATUS_INITIALIZED);
18413 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18414 == DEBUG_PARAMETER_REF)
18416 tree tdecl
18417 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18418 tdie = lookup_decl_die (tdecl);
18419 if (tdie == NULL)
18420 continue;
18422 else
18423 continue;
18424 if (reg == NULL
18425 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18426 != DEBUG_PARAMETER_REF)
18427 continue;
18428 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18429 VOIDmode,
18430 VAR_INIT_STATUS_INITIALIZED);
18431 if (val == NULL)
18432 continue;
18433 if (die == NULL)
18434 die = gen_call_site_die (decl, subr_die, ca_loc);
18435 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18436 NULL_TREE);
18437 if (reg != NULL)
18438 add_AT_loc (cdie, DW_AT_location, reg);
18439 else if (tdie != NULL)
18440 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18441 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18442 if (next_arg != XEXP (arg, 1))
18444 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18445 if (mode == VOIDmode)
18446 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18447 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18448 0), 1),
18449 mode, VOIDmode,
18450 VAR_INIT_STATUS_INITIALIZED);
18451 if (val != NULL)
18452 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18455 if (die == NULL
18456 && (ca_loc->symbol_ref || tloc))
18457 die = gen_call_site_die (decl, subr_die, ca_loc);
18458 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18460 dw_loc_descr_ref tval = NULL;
18462 if (tloc != NULL_RTX)
18463 tval = mem_loc_descriptor (tloc,
18464 GET_MODE (tloc) == VOIDmode
18465 ? Pmode : GET_MODE (tloc),
18466 VOIDmode,
18467 VAR_INIT_STATUS_INITIALIZED);
18468 if (tval)
18469 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18470 else if (tlocc != NULL_RTX)
18472 tval = mem_loc_descriptor (tlocc,
18473 GET_MODE (tlocc) == VOIDmode
18474 ? Pmode : GET_MODE (tlocc),
18475 VOIDmode,
18476 VAR_INIT_STATUS_INITIALIZED);
18477 if (tval)
18478 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18479 tval);
18482 if (die != NULL)
18484 call_site_note_count++;
18485 if (ca_loc->tail_call_p)
18486 tail_call_site_note_count++;
18490 call_arg_locations = NULL;
18491 call_arg_loc_last = NULL;
18492 if (tail_call_site_count >= 0
18493 && tail_call_site_count == tail_call_site_note_count
18494 && !dwarf_strict)
18496 if (call_site_count >= 0
18497 && call_site_count == call_site_note_count)
18498 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18499 else
18500 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18502 call_site_count = -1;
18503 tail_call_site_count = -1;
18505 /* Add the calling convention attribute if requested. */
18506 add_calling_convention_attribute (subr_die, decl);
18510 /* Returns a hash value for X (which really is a die_struct). */
18512 static hashval_t
18513 common_block_die_table_hash (const void *x)
18515 const_dw_die_ref d = (const_dw_die_ref) x;
18516 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18519 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18520 as decl_id and die_parent of die_struct Y. */
18522 static int
18523 common_block_die_table_eq (const void *x, const void *y)
18525 const_dw_die_ref d = (const_dw_die_ref) x;
18526 const_dw_die_ref e = (const_dw_die_ref) y;
18527 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18530 /* Generate a DIE to represent a declared data object.
18531 Either DECL or ORIGIN must be non-null. */
18533 static void
18534 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18536 HOST_WIDE_INT off = 0;
18537 tree com_decl;
18538 tree decl_or_origin = decl ? decl : origin;
18539 tree ultimate_origin;
18540 dw_die_ref var_die;
18541 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18542 dw_die_ref origin_die;
18543 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18544 || class_or_namespace_scope_p (context_die));
18545 bool specialization_p = false;
18547 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18548 if (decl || ultimate_origin)
18549 origin = ultimate_origin;
18550 com_decl = fortran_common (decl_or_origin, &off);
18552 /* Symbol in common gets emitted as a child of the common block, in the form
18553 of a data member. */
18554 if (com_decl)
18556 dw_die_ref com_die;
18557 dw_loc_list_ref loc;
18558 die_node com_die_arg;
18560 var_die = lookup_decl_die (decl_or_origin);
18561 if (var_die)
18563 if (get_AT (var_die, DW_AT_location) == NULL)
18565 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18566 if (loc)
18568 if (off)
18570 /* Optimize the common case. */
18571 if (single_element_loc_list_p (loc)
18572 && loc->expr->dw_loc_opc == DW_OP_addr
18573 && loc->expr->dw_loc_next == NULL
18574 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18575 == SYMBOL_REF)
18577 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18578 loc->expr->dw_loc_oprnd1.v.val_addr
18579 = plus_constant (GET_MODE (x), x , off);
18581 else
18582 loc_list_plus_const (loc, off);
18584 add_AT_location_description (var_die, DW_AT_location, loc);
18585 remove_AT (var_die, DW_AT_declaration);
18588 return;
18591 if (common_block_die_table == NULL)
18592 common_block_die_table
18593 = htab_create_ggc (10, common_block_die_table_hash,
18594 common_block_die_table_eq, NULL);
18596 com_die_arg.decl_id = DECL_UID (com_decl);
18597 com_die_arg.die_parent = context_die;
18598 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18599 loc = loc_list_from_tree (com_decl, 2);
18600 if (com_die == NULL)
18602 const char *cnam
18603 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18604 void **slot;
18606 com_die = new_die (DW_TAG_common_block, context_die, decl);
18607 add_name_and_src_coords_attributes (com_die, com_decl);
18608 if (loc)
18610 add_AT_location_description (com_die, DW_AT_location, loc);
18611 /* Avoid sharing the same loc descriptor between
18612 DW_TAG_common_block and DW_TAG_variable. */
18613 loc = loc_list_from_tree (com_decl, 2);
18615 else if (DECL_EXTERNAL (decl))
18616 add_AT_flag (com_die, DW_AT_declaration, 1);
18617 if (want_pubnames ())
18618 add_pubname_string (cnam, com_die); /* ??? needed? */
18619 com_die->decl_id = DECL_UID (com_decl);
18620 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18621 *slot = (void *) com_die;
18623 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18625 add_AT_location_description (com_die, DW_AT_location, loc);
18626 loc = loc_list_from_tree (com_decl, 2);
18627 remove_AT (com_die, DW_AT_declaration);
18629 var_die = new_die (DW_TAG_variable, com_die, decl);
18630 add_name_and_src_coords_attributes (var_die, decl);
18631 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18632 TREE_THIS_VOLATILE (decl), context_die);
18633 add_AT_flag (var_die, DW_AT_external, 1);
18634 if (loc)
18636 if (off)
18638 /* Optimize the common case. */
18639 if (single_element_loc_list_p (loc)
18640 && loc->expr->dw_loc_opc == DW_OP_addr
18641 && loc->expr->dw_loc_next == NULL
18642 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18644 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18645 loc->expr->dw_loc_oprnd1.v.val_addr
18646 = plus_constant (GET_MODE (x), x, off);
18648 else
18649 loc_list_plus_const (loc, off);
18651 add_AT_location_description (var_die, DW_AT_location, loc);
18653 else if (DECL_EXTERNAL (decl))
18654 add_AT_flag (var_die, DW_AT_declaration, 1);
18655 equate_decl_number_to_die (decl, var_die);
18656 return;
18659 /* If the compiler emitted a definition for the DECL declaration
18660 and if we already emitted a DIE for it, don't emit a second
18661 DIE for it again. Allow re-declarations of DECLs that are
18662 inside functions, though. */
18663 if (old_die && declaration && !local_scope_p (context_die))
18664 return;
18666 /* For static data members, the declaration in the class is supposed
18667 to have DW_TAG_member tag; the specification should still be
18668 DW_TAG_variable referencing the DW_TAG_member DIE. */
18669 if (declaration && class_scope_p (context_die))
18670 var_die = new_die (DW_TAG_member, context_die, decl);
18671 else
18672 var_die = new_die (DW_TAG_variable, context_die, decl);
18674 origin_die = NULL;
18675 if (origin != NULL)
18676 origin_die = add_abstract_origin_attribute (var_die, origin);
18678 /* Loop unrolling can create multiple blocks that refer to the same
18679 static variable, so we must test for the DW_AT_declaration flag.
18681 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18682 copy decls and set the DECL_ABSTRACT flag on them instead of
18683 sharing them.
18685 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18687 ??? The declare_in_namespace support causes us to get two DIEs for one
18688 variable, both of which are declarations. We want to avoid considering
18689 one to be a specification, so we must test that this DIE is not a
18690 declaration. */
18691 else if (old_die && TREE_STATIC (decl) && ! declaration
18692 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18694 /* This is a definition of a C++ class level static. */
18695 add_AT_specification (var_die, old_die);
18696 specialization_p = true;
18697 if (DECL_NAME (decl))
18699 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18700 struct dwarf_file_data * file_index = lookup_filename (s.file);
18702 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18703 add_AT_file (var_die, DW_AT_decl_file, file_index);
18705 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18706 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18708 if (old_die->die_tag == DW_TAG_member)
18709 add_linkage_name (var_die, decl);
18712 else
18713 add_name_and_src_coords_attributes (var_die, decl);
18715 if ((origin == NULL && !specialization_p)
18716 || (origin != NULL
18717 && !DECL_ABSTRACT (decl_or_origin)
18718 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18719 decl_function_context
18720 (decl_or_origin))))
18722 tree type = TREE_TYPE (decl_or_origin);
18724 if (decl_by_reference_p (decl_or_origin))
18725 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18726 else
18727 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18728 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18731 if (origin == NULL && !specialization_p)
18733 if (TREE_PUBLIC (decl))
18734 add_AT_flag (var_die, DW_AT_external, 1);
18736 if (DECL_ARTIFICIAL (decl))
18737 add_AT_flag (var_die, DW_AT_artificial, 1);
18739 add_accessibility_attribute (var_die, decl);
18742 if (declaration)
18743 add_AT_flag (var_die, DW_AT_declaration, 1);
18745 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18746 equate_decl_number_to_die (decl, var_die);
18748 if (! declaration
18749 && (! DECL_ABSTRACT (decl_or_origin)
18750 /* Local static vars are shared between all clones/inlines,
18751 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18752 already set. */
18753 || (TREE_CODE (decl_or_origin) == VAR_DECL
18754 && TREE_STATIC (decl_or_origin)
18755 && DECL_RTL_SET_P (decl_or_origin)))
18756 /* When abstract origin already has DW_AT_location attribute, no need
18757 to add it again. */
18758 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18760 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18761 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18762 defer_location (decl_or_origin, var_die);
18763 else
18764 add_location_or_const_value_attribute (var_die, decl_or_origin,
18765 decl == NULL, DW_AT_location);
18766 add_pubname (decl_or_origin, var_die);
18768 else
18769 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18772 /* Generate a DIE to represent a named constant. */
18774 static void
18775 gen_const_die (tree decl, dw_die_ref context_die)
18777 dw_die_ref const_die;
18778 tree type = TREE_TYPE (decl);
18780 const_die = new_die (DW_TAG_constant, context_die, decl);
18781 add_name_and_src_coords_attributes (const_die, decl);
18782 add_type_attribute (const_die, type, 1, 0, context_die);
18783 if (TREE_PUBLIC (decl))
18784 add_AT_flag (const_die, DW_AT_external, 1);
18785 if (DECL_ARTIFICIAL (decl))
18786 add_AT_flag (const_die, DW_AT_artificial, 1);
18787 tree_add_const_value_attribute_for_decl (const_die, decl);
18790 /* Generate a DIE to represent a label identifier. */
18792 static void
18793 gen_label_die (tree decl, dw_die_ref context_die)
18795 tree origin = decl_ultimate_origin (decl);
18796 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18797 rtx insn;
18798 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18800 if (origin != NULL)
18801 add_abstract_origin_attribute (lbl_die, origin);
18802 else
18803 add_name_and_src_coords_attributes (lbl_die, decl);
18805 if (DECL_ABSTRACT (decl))
18806 equate_decl_number_to_die (decl, lbl_die);
18807 else
18809 insn = DECL_RTL_IF_SET (decl);
18811 /* Deleted labels are programmer specified labels which have been
18812 eliminated because of various optimizations. We still emit them
18813 here so that it is possible to put breakpoints on them. */
18814 if (insn
18815 && (LABEL_P (insn)
18816 || ((NOTE_P (insn)
18817 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18819 /* When optimization is enabled (via -O) some parts of the compiler
18820 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18821 represent source-level labels which were explicitly declared by
18822 the user. This really shouldn't be happening though, so catch
18823 it if it ever does happen. */
18824 gcc_assert (!INSN_DELETED_P (insn));
18826 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18827 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18829 else if (insn
18830 && NOTE_P (insn)
18831 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18832 && CODE_LABEL_NUMBER (insn) != -1)
18834 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18835 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18840 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18841 attributes to the DIE for a block STMT, to describe where the inlined
18842 function was called from. This is similar to add_src_coords_attributes. */
18844 static inline void
18845 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18847 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18849 if (dwarf_version >= 3 || !dwarf_strict)
18851 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18852 add_AT_unsigned (die, DW_AT_call_line, s.line);
18857 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18858 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18860 static inline void
18861 add_high_low_attributes (tree stmt, dw_die_ref die)
18863 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18865 if (BLOCK_FRAGMENT_CHAIN (stmt)
18866 && (dwarf_version >= 3 || !dwarf_strict))
18868 tree chain, superblock = NULL_TREE;
18869 dw_die_ref pdie;
18870 dw_attr_ref attr = NULL;
18872 if (inlined_function_outer_scope_p (stmt))
18874 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18875 BLOCK_NUMBER (stmt));
18876 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18879 /* Optimize duplicate .debug_ranges lists or even tails of
18880 lists. If this BLOCK has same ranges as its supercontext,
18881 lookup DW_AT_ranges attribute in the supercontext (and
18882 recursively so), verify that the ranges_table contains the
18883 right values and use it instead of adding a new .debug_range. */
18884 for (chain = stmt, pdie = die;
18885 BLOCK_SAME_RANGE (chain);
18886 chain = BLOCK_SUPERCONTEXT (chain))
18888 dw_attr_ref new_attr;
18890 pdie = pdie->die_parent;
18891 if (pdie == NULL)
18892 break;
18893 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18894 break;
18895 new_attr = get_AT (pdie, DW_AT_ranges);
18896 if (new_attr == NULL
18897 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18898 break;
18899 attr = new_attr;
18900 superblock = BLOCK_SUPERCONTEXT (chain);
18902 if (attr != NULL
18903 && (ranges_table[attr->dw_attr_val.v.val_offset
18904 / 2 / DWARF2_ADDR_SIZE].num
18905 == BLOCK_NUMBER (superblock))
18906 && BLOCK_FRAGMENT_CHAIN (superblock))
18908 unsigned long off = attr->dw_attr_val.v.val_offset
18909 / 2 / DWARF2_ADDR_SIZE;
18910 unsigned long supercnt = 0, thiscnt = 0;
18911 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18912 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18914 ++supercnt;
18915 gcc_checking_assert (ranges_table[off + supercnt].num
18916 == BLOCK_NUMBER (chain));
18918 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18919 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18920 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18921 ++thiscnt;
18922 gcc_assert (supercnt >= thiscnt);
18923 add_AT_range_list (die, DW_AT_ranges,
18924 ((off + supercnt - thiscnt)
18925 * 2 * DWARF2_ADDR_SIZE),
18926 false);
18927 return;
18930 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18932 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18935 add_ranges (chain);
18936 chain = BLOCK_FRAGMENT_CHAIN (chain);
18938 while (chain);
18939 add_ranges (NULL);
18941 else
18943 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18944 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18945 BLOCK_NUMBER (stmt));
18946 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18947 BLOCK_NUMBER (stmt));
18948 add_AT_low_high_pc (die, label, label_high, false);
18952 /* Generate a DIE for a lexical block. */
18954 static void
18955 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18957 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18959 if (call_arg_locations)
18961 if (block_map.length () <= BLOCK_NUMBER (stmt))
18962 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18963 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18966 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18967 add_high_low_attributes (stmt, stmt_die);
18969 decls_for_scope (stmt, stmt_die, depth);
18972 /* Generate a DIE for an inlined subprogram. */
18974 static void
18975 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18977 tree decl;
18979 /* The instance of function that is effectively being inlined shall not
18980 be abstract. */
18981 gcc_assert (! BLOCK_ABSTRACT (stmt));
18983 decl = block_ultimate_origin (stmt);
18985 /* Emit info for the abstract instance first, if we haven't yet. We
18986 must emit this even if the block is abstract, otherwise when we
18987 emit the block below (or elsewhere), we may end up trying to emit
18988 a die whose origin die hasn't been emitted, and crashing. */
18989 dwarf2out_abstract_function (decl);
18991 if (! BLOCK_ABSTRACT (stmt))
18993 dw_die_ref subr_die
18994 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18996 if (call_arg_locations)
18998 if (block_map.length () <= BLOCK_NUMBER (stmt))
18999 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19000 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19002 add_abstract_origin_attribute (subr_die, decl);
19003 if (TREE_ASM_WRITTEN (stmt))
19004 add_high_low_attributes (stmt, subr_die);
19005 add_call_src_coords_attributes (stmt, subr_die);
19007 decls_for_scope (stmt, subr_die, depth);
19008 current_function_has_inlines = 1;
19012 /* Generate a DIE for a field in a record, or structure. */
19014 static void
19015 gen_field_die (tree decl, dw_die_ref context_die)
19017 dw_die_ref decl_die;
19019 if (TREE_TYPE (decl) == error_mark_node)
19020 return;
19022 decl_die = new_die (DW_TAG_member, context_die, decl);
19023 add_name_and_src_coords_attributes (decl_die, decl);
19024 add_type_attribute (decl_die, member_declared_type (decl),
19025 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19026 context_die);
19028 if (DECL_BIT_FIELD_TYPE (decl))
19030 add_byte_size_attribute (decl_die, decl);
19031 add_bit_size_attribute (decl_die, decl);
19032 add_bit_offset_attribute (decl_die, decl);
19035 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19036 add_data_member_location_attribute (decl_die, decl);
19038 if (DECL_ARTIFICIAL (decl))
19039 add_AT_flag (decl_die, DW_AT_artificial, 1);
19041 add_accessibility_attribute (decl_die, decl);
19043 /* Equate decl number to die, so that we can look up this decl later on. */
19044 equate_decl_number_to_die (decl, decl_die);
19047 #if 0
19048 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19049 Use modified_type_die instead.
19050 We keep this code here just in case these types of DIEs may be needed to
19051 represent certain things in other languages (e.g. Pascal) someday. */
19053 static void
19054 gen_pointer_type_die (tree type, dw_die_ref context_die)
19056 dw_die_ref ptr_die
19057 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19059 equate_type_number_to_die (type, ptr_die);
19060 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19061 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19064 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19065 Use modified_type_die instead.
19066 We keep this code here just in case these types of DIEs may be needed to
19067 represent certain things in other languages (e.g. Pascal) someday. */
19069 static void
19070 gen_reference_type_die (tree type, dw_die_ref context_die)
19072 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19074 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19075 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19076 else
19077 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19079 equate_type_number_to_die (type, ref_die);
19080 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19081 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19083 #endif
19085 /* Generate a DIE for a pointer to a member type. */
19087 static void
19088 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19090 dw_die_ref ptr_die
19091 = new_die (DW_TAG_ptr_to_member_type,
19092 scope_die_for (type, context_die), type);
19094 equate_type_number_to_die (type, ptr_die);
19095 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19096 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19097 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19100 typedef const char *dchar_p; /* For DEF_VEC_P. */
19102 static char *producer_string;
19104 /* Return a heap allocated producer string including command line options
19105 if -grecord-gcc-switches. */
19107 static char *
19108 gen_producer_string (void)
19110 size_t j;
19111 vec<dchar_p> switches = vNULL;
19112 const char *language_string = lang_hooks.name;
19113 char *producer, *tail;
19114 const char *p;
19115 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19116 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19118 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19119 switch (save_decoded_options[j].opt_index)
19121 case OPT_o:
19122 case OPT_d:
19123 case OPT_dumpbase:
19124 case OPT_dumpdir:
19125 case OPT_auxbase:
19126 case OPT_auxbase_strip:
19127 case OPT_quiet:
19128 case OPT_version:
19129 case OPT_v:
19130 case OPT_w:
19131 case OPT_L:
19132 case OPT_D:
19133 case OPT_I:
19134 case OPT_U:
19135 case OPT_SPECIAL_unknown:
19136 case OPT_SPECIAL_ignore:
19137 case OPT_SPECIAL_program_name:
19138 case OPT_SPECIAL_input_file:
19139 case OPT_grecord_gcc_switches:
19140 case OPT_gno_record_gcc_switches:
19141 case OPT__output_pch_:
19142 case OPT_fdiagnostics_show_location_:
19143 case OPT_fdiagnostics_show_option:
19144 case OPT_fdiagnostics_show_caret:
19145 case OPT_fdiagnostics_color_:
19146 case OPT_fverbose_asm:
19147 case OPT____:
19148 case OPT__sysroot_:
19149 case OPT_nostdinc:
19150 case OPT_nostdinc__:
19151 /* Ignore these. */
19152 continue;
19153 default:
19154 if (cl_options[save_decoded_options[j].opt_index].flags
19155 & CL_NO_DWARF_RECORD)
19156 continue;
19157 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19158 == '-');
19159 switch (save_decoded_options[j].canonical_option[0][1])
19161 case 'M':
19162 case 'i':
19163 case 'W':
19164 continue;
19165 case 'f':
19166 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19167 "dump", 4) == 0)
19168 continue;
19169 break;
19170 default:
19171 break;
19173 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19174 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19175 break;
19178 producer = XNEWVEC (char, plen + 1 + len + 1);
19179 tail = producer;
19180 sprintf (tail, "%s %s", language_string, version_string);
19181 tail += plen;
19183 FOR_EACH_VEC_ELT (switches, j, p)
19185 len = strlen (p);
19186 *tail = ' ';
19187 memcpy (tail + 1, p, len);
19188 tail += len + 1;
19191 *tail = '\0';
19192 switches.release ();
19193 return producer;
19196 /* Generate the DIE for the compilation unit. */
19198 static dw_die_ref
19199 gen_compile_unit_die (const char *filename)
19201 dw_die_ref die;
19202 const char *language_string = lang_hooks.name;
19203 int language;
19205 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19207 if (filename)
19209 add_name_attribute (die, filename);
19210 /* Don't add cwd for <built-in>. */
19211 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19212 add_comp_dir_attribute (die);
19215 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19217 /* If our producer is LTO try to figure out a common language to use
19218 from the global list of translation units. */
19219 if (strcmp (language_string, "GNU GIMPLE") == 0)
19221 unsigned i;
19222 tree t;
19223 const char *common_lang = NULL;
19225 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19227 if (!TRANSLATION_UNIT_LANGUAGE (t))
19228 continue;
19229 if (!common_lang)
19230 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19231 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19233 else if (strncmp (common_lang, "GNU C", 5) == 0
19234 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19235 /* Mixing C and C++ is ok, use C++ in that case. */
19236 common_lang = "GNU C++";
19237 else
19239 /* Fall back to C. */
19240 common_lang = NULL;
19241 break;
19245 if (common_lang)
19246 language_string = common_lang;
19249 language = DW_LANG_C89;
19250 if (strcmp (language_string, "GNU C++") == 0)
19251 language = DW_LANG_C_plus_plus;
19252 else if (strcmp (language_string, "GNU F77") == 0)
19253 language = DW_LANG_Fortran77;
19254 else if (strcmp (language_string, "GNU Pascal") == 0)
19255 language = DW_LANG_Pascal83;
19256 else if (dwarf_version >= 3 || !dwarf_strict)
19258 if (strcmp (language_string, "GNU Ada") == 0)
19259 language = DW_LANG_Ada95;
19260 else if (strcmp (language_string, "GNU Fortran") == 0)
19261 language = DW_LANG_Fortran95;
19262 else if (strcmp (language_string, "GNU Java") == 0)
19263 language = DW_LANG_Java;
19264 else if (strcmp (language_string, "GNU Objective-C") == 0)
19265 language = DW_LANG_ObjC;
19266 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19267 language = DW_LANG_ObjC_plus_plus;
19268 else if (dwarf_version >= 5 || !dwarf_strict)
19270 if (strcmp (language_string, "GNU Go") == 0)
19271 language = DW_LANG_Go;
19274 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19275 else if (strcmp (language_string, "GNU Fortran") == 0)
19276 language = DW_LANG_Fortran90;
19278 add_AT_unsigned (die, DW_AT_language, language);
19280 switch (language)
19282 case DW_LANG_Fortran77:
19283 case DW_LANG_Fortran90:
19284 case DW_LANG_Fortran95:
19285 /* Fortran has case insensitive identifiers and the front-end
19286 lowercases everything. */
19287 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19288 break;
19289 default:
19290 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19291 break;
19293 return die;
19296 /* Generate the DIE for a base class. */
19298 static void
19299 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19301 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19303 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19304 add_data_member_location_attribute (die, binfo);
19306 if (BINFO_VIRTUAL_P (binfo))
19307 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19309 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19310 children, otherwise the default is DW_ACCESS_public. In DWARF2
19311 the default has always been DW_ACCESS_private. */
19312 if (access == access_public_node)
19314 if (dwarf_version == 2
19315 || context_die->die_tag == DW_TAG_class_type)
19316 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19318 else if (access == access_protected_node)
19319 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19320 else if (dwarf_version > 2
19321 && context_die->die_tag != DW_TAG_class_type)
19322 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19325 /* Generate a DIE for a class member. */
19327 static void
19328 gen_member_die (tree type, dw_die_ref context_die)
19330 tree member;
19331 tree binfo = TYPE_BINFO (type);
19332 dw_die_ref child;
19334 /* If this is not an incomplete type, output descriptions of each of its
19335 members. Note that as we output the DIEs necessary to represent the
19336 members of this record or union type, we will also be trying to output
19337 DIEs to represent the *types* of those members. However the `type'
19338 function (above) will specifically avoid generating type DIEs for member
19339 types *within* the list of member DIEs for this (containing) type except
19340 for those types (of members) which are explicitly marked as also being
19341 members of this (containing) type themselves. The g++ front- end can
19342 force any given type to be treated as a member of some other (containing)
19343 type by setting the TYPE_CONTEXT of the given (member) type to point to
19344 the TREE node representing the appropriate (containing) type. */
19346 /* First output info about the base classes. */
19347 if (binfo)
19349 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19350 int i;
19351 tree base;
19353 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19354 gen_inheritance_die (base,
19355 (accesses ? (*accesses)[i] : access_public_node),
19356 context_die);
19359 /* Now output info about the data members and type members. */
19360 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19362 /* If we thought we were generating minimal debug info for TYPE
19363 and then changed our minds, some of the member declarations
19364 may have already been defined. Don't define them again, but
19365 do put them in the right order. */
19367 child = lookup_decl_die (member);
19368 if (child)
19369 splice_child_die (context_die, child);
19370 else
19371 gen_decl_die (member, NULL, context_die);
19374 /* Now output info about the function members (if any). */
19375 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19377 /* Don't include clones in the member list. */
19378 if (DECL_ABSTRACT_ORIGIN (member))
19379 continue;
19381 child = lookup_decl_die (member);
19382 if (child)
19383 splice_child_die (context_die, child);
19384 else
19385 gen_decl_die (member, NULL, context_die);
19389 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19390 is set, we pretend that the type was never defined, so we only get the
19391 member DIEs needed by later specification DIEs. */
19393 static void
19394 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19395 enum debug_info_usage usage)
19397 dw_die_ref type_die = lookup_type_die (type);
19398 dw_die_ref scope_die = 0;
19399 int nested = 0;
19400 int complete = (TYPE_SIZE (type)
19401 && (! TYPE_STUB_DECL (type)
19402 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19403 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19404 complete = complete && should_emit_struct_debug (type, usage);
19406 if (type_die && ! complete)
19407 return;
19409 if (TYPE_CONTEXT (type) != NULL_TREE
19410 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19411 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19412 nested = 1;
19414 scope_die = scope_die_for (type, context_die);
19416 /* Generate child dies for template paramaters. */
19417 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19418 schedule_generic_params_dies_gen (type);
19420 if (! type_die || (nested && is_cu_die (scope_die)))
19421 /* First occurrence of type or toplevel definition of nested class. */
19423 dw_die_ref old_die = type_die;
19425 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19426 ? record_type_tag (type) : DW_TAG_union_type,
19427 scope_die, type);
19428 equate_type_number_to_die (type, type_die);
19429 if (old_die)
19430 add_AT_specification (type_die, old_die);
19431 else
19432 add_name_attribute (type_die, type_tag (type));
19434 else
19435 remove_AT (type_die, DW_AT_declaration);
19437 /* If this type has been completed, then give it a byte_size attribute and
19438 then give a list of members. */
19439 if (complete && !ns_decl)
19441 /* Prevent infinite recursion in cases where the type of some member of
19442 this type is expressed in terms of this type itself. */
19443 TREE_ASM_WRITTEN (type) = 1;
19444 add_byte_size_attribute (type_die, type);
19445 if (TYPE_STUB_DECL (type) != NULL_TREE)
19447 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19448 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19451 /* If the first reference to this type was as the return type of an
19452 inline function, then it may not have a parent. Fix this now. */
19453 if (type_die->die_parent == NULL)
19454 add_child_die (scope_die, type_die);
19456 push_decl_scope (type);
19457 gen_member_die (type, type_die);
19458 pop_decl_scope ();
19460 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19461 if (TYPE_ARTIFICIAL (type))
19462 add_AT_flag (type_die, DW_AT_artificial, 1);
19464 /* GNU extension: Record what type our vtable lives in. */
19465 if (TYPE_VFIELD (type))
19467 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19469 gen_type_die (vtype, context_die);
19470 add_AT_die_ref (type_die, DW_AT_containing_type,
19471 lookup_type_die (vtype));
19474 else
19476 add_AT_flag (type_die, DW_AT_declaration, 1);
19478 /* We don't need to do this for function-local types. */
19479 if (TYPE_STUB_DECL (type)
19480 && ! decl_function_context (TYPE_STUB_DECL (type)))
19481 vec_safe_push (incomplete_types, type);
19484 if (get_AT (type_die, DW_AT_name))
19485 add_pubtype (type, type_die);
19488 /* Generate a DIE for a subroutine _type_. */
19490 static void
19491 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19493 tree return_type = TREE_TYPE (type);
19494 dw_die_ref subr_die
19495 = new_die (DW_TAG_subroutine_type,
19496 scope_die_for (type, context_die), type);
19498 equate_type_number_to_die (type, subr_die);
19499 add_prototyped_attribute (subr_die, type);
19500 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19501 gen_formal_types_die (type, subr_die);
19503 if (get_AT (subr_die, DW_AT_name))
19504 add_pubtype (type, subr_die);
19507 /* Generate a DIE for a type definition. */
19509 static void
19510 gen_typedef_die (tree decl, dw_die_ref context_die)
19512 dw_die_ref type_die;
19513 tree origin;
19515 if (TREE_ASM_WRITTEN (decl))
19516 return;
19518 TREE_ASM_WRITTEN (decl) = 1;
19519 type_die = new_die (DW_TAG_typedef, context_die, decl);
19520 origin = decl_ultimate_origin (decl);
19521 if (origin != NULL)
19522 add_abstract_origin_attribute (type_die, origin);
19523 else
19525 tree type;
19527 add_name_and_src_coords_attributes (type_die, decl);
19528 if (DECL_ORIGINAL_TYPE (decl))
19530 type = DECL_ORIGINAL_TYPE (decl);
19532 gcc_assert (type != TREE_TYPE (decl));
19533 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19535 else
19537 type = TREE_TYPE (decl);
19539 if (is_naming_typedef_decl (TYPE_NAME (type)))
19541 /* Here, we are in the case of decl being a typedef naming
19542 an anonymous type, e.g:
19543 typedef struct {...} foo;
19544 In that case TREE_TYPE (decl) is not a typedef variant
19545 type and TYPE_NAME of the anonymous type is set to the
19546 TYPE_DECL of the typedef. This construct is emitted by
19547 the C++ FE.
19549 TYPE is the anonymous struct named by the typedef
19550 DECL. As we need the DW_AT_type attribute of the
19551 DW_TAG_typedef to point to the DIE of TYPE, let's
19552 generate that DIE right away. add_type_attribute
19553 called below will then pick (via lookup_type_die) that
19554 anonymous struct DIE. */
19555 if (!TREE_ASM_WRITTEN (type))
19556 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19558 /* This is a GNU Extension. We are adding a
19559 DW_AT_linkage_name attribute to the DIE of the
19560 anonymous struct TYPE. The value of that attribute
19561 is the name of the typedef decl naming the anonymous
19562 struct. This greatly eases the work of consumers of
19563 this debug info. */
19564 add_linkage_attr (lookup_type_die (type), decl);
19568 add_type_attribute (type_die, type, TREE_READONLY (decl),
19569 TREE_THIS_VOLATILE (decl), context_die);
19571 if (is_naming_typedef_decl (decl))
19572 /* We want that all subsequent calls to lookup_type_die with
19573 TYPE in argument yield the DW_TAG_typedef we have just
19574 created. */
19575 equate_type_number_to_die (type, type_die);
19577 add_accessibility_attribute (type_die, decl);
19580 if (DECL_ABSTRACT (decl))
19581 equate_decl_number_to_die (decl, type_die);
19583 if (get_AT (type_die, DW_AT_name))
19584 add_pubtype (decl, type_die);
19587 /* Generate a DIE for a struct, class, enum or union type. */
19589 static void
19590 gen_tagged_type_die (tree type,
19591 dw_die_ref context_die,
19592 enum debug_info_usage usage)
19594 int need_pop;
19596 if (type == NULL_TREE
19597 || !is_tagged_type (type))
19598 return;
19600 /* If this is a nested type whose containing class hasn't been written
19601 out yet, writing it out will cover this one, too. This does not apply
19602 to instantiations of member class templates; they need to be added to
19603 the containing class as they are generated. FIXME: This hurts the
19604 idea of combining type decls from multiple TUs, since we can't predict
19605 what set of template instantiations we'll get. */
19606 if (TYPE_CONTEXT (type)
19607 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19608 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19610 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19612 if (TREE_ASM_WRITTEN (type))
19613 return;
19615 /* If that failed, attach ourselves to the stub. */
19616 push_decl_scope (TYPE_CONTEXT (type));
19617 context_die = lookup_type_die (TYPE_CONTEXT (type));
19618 need_pop = 1;
19620 else if (TYPE_CONTEXT (type) != NULL_TREE
19621 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19623 /* If this type is local to a function that hasn't been written
19624 out yet, use a NULL context for now; it will be fixed up in
19625 decls_for_scope. */
19626 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19627 /* A declaration DIE doesn't count; nested types need to go in the
19628 specification. */
19629 if (context_die && is_declaration_die (context_die))
19630 context_die = NULL;
19631 need_pop = 0;
19633 else
19635 context_die = declare_in_namespace (type, context_die);
19636 need_pop = 0;
19639 if (TREE_CODE (type) == ENUMERAL_TYPE)
19641 /* This might have been written out by the call to
19642 declare_in_namespace. */
19643 if (!TREE_ASM_WRITTEN (type))
19644 gen_enumeration_type_die (type, context_die);
19646 else
19647 gen_struct_or_union_type_die (type, context_die, usage);
19649 if (need_pop)
19650 pop_decl_scope ();
19652 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19653 it up if it is ever completed. gen_*_type_die will set it for us
19654 when appropriate. */
19657 /* Generate a type description DIE. */
19659 static void
19660 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19661 enum debug_info_usage usage)
19663 struct array_descr_info info;
19665 if (type == NULL_TREE || type == error_mark_node)
19666 return;
19668 if (TYPE_NAME (type) != NULL_TREE
19669 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19670 && is_redundant_typedef (TYPE_NAME (type))
19671 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19672 /* The DECL of this type is a typedef we don't want to emit debug
19673 info for but we want debug info for its underlying typedef.
19674 This can happen for e.g, the injected-class-name of a C++
19675 type. */
19676 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19678 /* If TYPE is a typedef type variant, let's generate debug info
19679 for the parent typedef which TYPE is a type of. */
19680 if (typedef_variant_p (type))
19682 if (TREE_ASM_WRITTEN (type))
19683 return;
19685 /* Prevent broken recursion; we can't hand off to the same type. */
19686 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19688 /* Give typedefs the right scope. */
19689 context_die = scope_die_for (type, context_die);
19691 TREE_ASM_WRITTEN (type) = 1;
19693 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19694 return;
19697 /* If type is an anonymous tagged type named by a typedef, let's
19698 generate debug info for the typedef. */
19699 if (is_naming_typedef_decl (TYPE_NAME (type)))
19701 /* Use the DIE of the containing namespace as the parent DIE of
19702 the type description DIE we want to generate. */
19703 if (DECL_CONTEXT (TYPE_NAME (type))
19704 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19705 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19707 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19708 return;
19711 /* If this is an array type with hidden descriptor, handle it first. */
19712 if (!TREE_ASM_WRITTEN (type)
19713 && lang_hooks.types.get_array_descr_info
19714 && lang_hooks.types.get_array_descr_info (type, &info)
19715 && (dwarf_version >= 3 || !dwarf_strict))
19717 gen_descr_array_type_die (type, &info, context_die);
19718 TREE_ASM_WRITTEN (type) = 1;
19719 return;
19722 /* We are going to output a DIE to represent the unqualified version
19723 of this type (i.e. without any const or volatile qualifiers) so
19724 get the main variant (i.e. the unqualified version) of this type
19725 now. (Vectors are special because the debugging info is in the
19726 cloned type itself). */
19727 if (TREE_CODE (type) != VECTOR_TYPE)
19728 type = type_main_variant (type);
19730 if (TREE_ASM_WRITTEN (type))
19731 return;
19733 switch (TREE_CODE (type))
19735 case ERROR_MARK:
19736 break;
19738 case POINTER_TYPE:
19739 case REFERENCE_TYPE:
19740 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19741 ensures that the gen_type_die recursion will terminate even if the
19742 type is recursive. Recursive types are possible in Ada. */
19743 /* ??? We could perhaps do this for all types before the switch
19744 statement. */
19745 TREE_ASM_WRITTEN (type) = 1;
19747 /* For these types, all that is required is that we output a DIE (or a
19748 set of DIEs) to represent the "basis" type. */
19749 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19750 DINFO_USAGE_IND_USE);
19751 break;
19753 case OFFSET_TYPE:
19754 /* This code is used for C++ pointer-to-data-member types.
19755 Output a description of the relevant class type. */
19756 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19757 DINFO_USAGE_IND_USE);
19759 /* Output a description of the type of the object pointed to. */
19760 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19761 DINFO_USAGE_IND_USE);
19763 /* Now output a DIE to represent this pointer-to-data-member type
19764 itself. */
19765 gen_ptr_to_mbr_type_die (type, context_die);
19766 break;
19768 case FUNCTION_TYPE:
19769 /* Force out return type (in case it wasn't forced out already). */
19770 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19771 DINFO_USAGE_DIR_USE);
19772 gen_subroutine_type_die (type, context_die);
19773 break;
19775 case METHOD_TYPE:
19776 /* Force out return type (in case it wasn't forced out already). */
19777 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19778 DINFO_USAGE_DIR_USE);
19779 gen_subroutine_type_die (type, context_die);
19780 break;
19782 case ARRAY_TYPE:
19783 gen_array_type_die (type, context_die);
19784 break;
19786 case VECTOR_TYPE:
19787 gen_array_type_die (type, context_die);
19788 break;
19790 case ENUMERAL_TYPE:
19791 case RECORD_TYPE:
19792 case UNION_TYPE:
19793 case QUAL_UNION_TYPE:
19794 gen_tagged_type_die (type, context_die, usage);
19795 return;
19797 case VOID_TYPE:
19798 case INTEGER_TYPE:
19799 case REAL_TYPE:
19800 case FIXED_POINT_TYPE:
19801 case COMPLEX_TYPE:
19802 case BOOLEAN_TYPE:
19803 case POINTER_BOUNDS_TYPE:
19804 /* No DIEs needed for fundamental types. */
19805 break;
19807 case NULLPTR_TYPE:
19808 case LANG_TYPE:
19809 /* Just use DW_TAG_unspecified_type. */
19811 dw_die_ref type_die = lookup_type_die (type);
19812 if (type_die == NULL)
19814 tree name = TYPE_NAME (type);
19815 if (TREE_CODE (name) == TYPE_DECL)
19816 name = DECL_NAME (name);
19817 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19818 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19819 equate_type_number_to_die (type, type_die);
19822 break;
19824 default:
19825 gcc_unreachable ();
19828 TREE_ASM_WRITTEN (type) = 1;
19831 static void
19832 gen_type_die (tree type, dw_die_ref context_die)
19834 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19837 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19838 things which are local to the given block. */
19840 static void
19841 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19843 int must_output_die = 0;
19844 bool inlined_func;
19846 /* Ignore blocks that are NULL. */
19847 if (stmt == NULL_TREE)
19848 return;
19850 inlined_func = inlined_function_outer_scope_p (stmt);
19852 /* If the block is one fragment of a non-contiguous block, do not
19853 process the variables, since they will have been done by the
19854 origin block. Do process subblocks. */
19855 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19857 tree sub;
19859 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19860 gen_block_die (sub, context_die, depth + 1);
19862 return;
19865 /* Determine if we need to output any Dwarf DIEs at all to represent this
19866 block. */
19867 if (inlined_func)
19868 /* The outer scopes for inlinings *must* always be represented. We
19869 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19870 must_output_die = 1;
19871 else
19873 /* Determine if this block directly contains any "significant"
19874 local declarations which we will need to output DIEs for. */
19875 if (debug_info_level > DINFO_LEVEL_TERSE)
19876 /* We are not in terse mode so *any* local declaration counts
19877 as being a "significant" one. */
19878 must_output_die = ((BLOCK_VARS (stmt) != NULL
19879 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19880 && (TREE_USED (stmt)
19881 || TREE_ASM_WRITTEN (stmt)
19882 || BLOCK_ABSTRACT (stmt)));
19883 else if ((TREE_USED (stmt)
19884 || TREE_ASM_WRITTEN (stmt)
19885 || BLOCK_ABSTRACT (stmt))
19886 && !dwarf2out_ignore_block (stmt))
19887 must_output_die = 1;
19890 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19891 DIE for any block which contains no significant local declarations at
19892 all. Rather, in such cases we just call `decls_for_scope' so that any
19893 needed Dwarf info for any sub-blocks will get properly generated. Note
19894 that in terse mode, our definition of what constitutes a "significant"
19895 local declaration gets restricted to include only inlined function
19896 instances and local (nested) function definitions. */
19897 if (must_output_die)
19899 if (inlined_func)
19901 /* If STMT block is abstract, that means we have been called
19902 indirectly from dwarf2out_abstract_function.
19903 That function rightfully marks the descendent blocks (of
19904 the abstract function it is dealing with) as being abstract,
19905 precisely to prevent us from emitting any
19906 DW_TAG_inlined_subroutine DIE as a descendent
19907 of an abstract function instance. So in that case, we should
19908 not call gen_inlined_subroutine_die.
19910 Later though, when cgraph asks dwarf2out to emit info
19911 for the concrete instance of the function decl into which
19912 the concrete instance of STMT got inlined, the later will lead
19913 to the generation of a DW_TAG_inlined_subroutine DIE. */
19914 if (! BLOCK_ABSTRACT (stmt))
19915 gen_inlined_subroutine_die (stmt, context_die, depth);
19917 else
19918 gen_lexical_block_die (stmt, context_die, depth);
19920 else
19921 decls_for_scope (stmt, context_die, depth);
19924 /* Process variable DECL (or variable with origin ORIGIN) within
19925 block STMT and add it to CONTEXT_DIE. */
19926 static void
19927 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19929 dw_die_ref die;
19930 tree decl_or_origin = decl ? decl : origin;
19932 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19933 die = lookup_decl_die (decl_or_origin);
19934 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19935 && TYPE_DECL_IS_STUB (decl_or_origin))
19936 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19937 else
19938 die = NULL;
19940 if (die != NULL && die->die_parent == NULL)
19941 add_child_die (context_die, die);
19942 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19943 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19944 stmt, context_die);
19945 else
19946 gen_decl_die (decl, origin, context_die);
19949 /* Generate all of the decls declared within a given scope and (recursively)
19950 all of its sub-blocks. */
19952 static void
19953 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19955 tree decl;
19956 unsigned int i;
19957 tree subblocks;
19959 /* Ignore NULL blocks. */
19960 if (stmt == NULL_TREE)
19961 return;
19963 /* Output the DIEs to represent all of the data objects and typedefs
19964 declared directly within this block but not within any nested
19965 sub-blocks. Also, nested function and tag DIEs have been
19966 generated with a parent of NULL; fix that up now. */
19967 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19968 process_scope_var (stmt, decl, NULL_TREE, context_die);
19969 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19970 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19971 context_die);
19973 /* If we're at -g1, we're not interested in subblocks. */
19974 if (debug_info_level <= DINFO_LEVEL_TERSE)
19975 return;
19977 /* Output the DIEs to represent all sub-blocks (and the items declared
19978 therein) of this block. */
19979 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19980 subblocks != NULL;
19981 subblocks = BLOCK_CHAIN (subblocks))
19982 gen_block_die (subblocks, context_die, depth + 1);
19985 /* Is this a typedef we can avoid emitting? */
19987 static inline int
19988 is_redundant_typedef (const_tree decl)
19990 if (TYPE_DECL_IS_STUB (decl))
19991 return 1;
19993 if (DECL_ARTIFICIAL (decl)
19994 && DECL_CONTEXT (decl)
19995 && is_tagged_type (DECL_CONTEXT (decl))
19996 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19997 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19998 /* Also ignore the artificial member typedef for the class name. */
19999 return 1;
20001 return 0;
20004 /* Return TRUE if TYPE is a typedef that names a type for linkage
20005 purposes. This kind of typedefs is produced by the C++ FE for
20006 constructs like:
20008 typedef struct {...} foo;
20010 In that case, there is no typedef variant type produced for foo.
20011 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20012 struct type. */
20014 static bool
20015 is_naming_typedef_decl (const_tree decl)
20017 if (decl == NULL_TREE
20018 || TREE_CODE (decl) != TYPE_DECL
20019 || !is_tagged_type (TREE_TYPE (decl))
20020 || DECL_IS_BUILTIN (decl)
20021 || is_redundant_typedef (decl)
20022 /* It looks like Ada produces TYPE_DECLs that are very similar
20023 to C++ naming typedefs but that have different
20024 semantics. Let's be specific to c++ for now. */
20025 || !is_cxx ())
20026 return FALSE;
20028 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20029 && TYPE_NAME (TREE_TYPE (decl)) == decl
20030 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20031 != TYPE_NAME (TREE_TYPE (decl))));
20034 /* Returns the DIE for a context. */
20036 static inline dw_die_ref
20037 get_context_die (tree context)
20039 if (context)
20041 /* Find die that represents this context. */
20042 if (TYPE_P (context))
20044 context = TYPE_MAIN_VARIANT (context);
20045 return strip_naming_typedef (context, force_type_die (context));
20047 else
20048 return force_decl_die (context);
20050 return comp_unit_die ();
20053 /* Returns the DIE for decl. A DIE will always be returned. */
20055 static dw_die_ref
20056 force_decl_die (tree decl)
20058 dw_die_ref decl_die;
20059 unsigned saved_external_flag;
20060 tree save_fn = NULL_TREE;
20061 decl_die = lookup_decl_die (decl);
20062 if (!decl_die)
20064 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20066 decl_die = lookup_decl_die (decl);
20067 if (decl_die)
20068 return decl_die;
20070 switch (TREE_CODE (decl))
20072 case FUNCTION_DECL:
20073 /* Clear current_function_decl, so that gen_subprogram_die thinks
20074 that this is a declaration. At this point, we just want to force
20075 declaration die. */
20076 save_fn = current_function_decl;
20077 current_function_decl = NULL_TREE;
20078 gen_subprogram_die (decl, context_die);
20079 current_function_decl = save_fn;
20080 break;
20082 case VAR_DECL:
20083 /* Set external flag to force declaration die. Restore it after
20084 gen_decl_die() call. */
20085 saved_external_flag = DECL_EXTERNAL (decl);
20086 DECL_EXTERNAL (decl) = 1;
20087 gen_decl_die (decl, NULL, context_die);
20088 DECL_EXTERNAL (decl) = saved_external_flag;
20089 break;
20091 case NAMESPACE_DECL:
20092 if (dwarf_version >= 3 || !dwarf_strict)
20093 dwarf2out_decl (decl);
20094 else
20095 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20096 decl_die = comp_unit_die ();
20097 break;
20099 case TRANSLATION_UNIT_DECL:
20100 decl_die = comp_unit_die ();
20101 break;
20103 default:
20104 gcc_unreachable ();
20107 /* We should be able to find the DIE now. */
20108 if (!decl_die)
20109 decl_die = lookup_decl_die (decl);
20110 gcc_assert (decl_die);
20113 return decl_die;
20116 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20117 always returned. */
20119 static dw_die_ref
20120 force_type_die (tree type)
20122 dw_die_ref type_die;
20124 type_die = lookup_type_die (type);
20125 if (!type_die)
20127 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20129 type_die = modified_type_die (type, TYPE_READONLY (type),
20130 TYPE_VOLATILE (type), context_die);
20131 gcc_assert (type_die);
20133 return type_die;
20136 /* Force out any required namespaces to be able to output DECL,
20137 and return the new context_die for it, if it's changed. */
20139 static dw_die_ref
20140 setup_namespace_context (tree thing, dw_die_ref context_die)
20142 tree context = (DECL_P (thing)
20143 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20144 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20145 /* Force out the namespace. */
20146 context_die = force_decl_die (context);
20148 return context_die;
20151 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20152 type) within its namespace, if appropriate.
20154 For compatibility with older debuggers, namespace DIEs only contain
20155 declarations; all definitions are emitted at CU scope. */
20157 static dw_die_ref
20158 declare_in_namespace (tree thing, dw_die_ref context_die)
20160 dw_die_ref ns_context;
20162 if (debug_info_level <= DINFO_LEVEL_TERSE)
20163 return context_die;
20165 /* If this decl is from an inlined function, then don't try to emit it in its
20166 namespace, as we will get confused. It would have already been emitted
20167 when the abstract instance of the inline function was emitted anyways. */
20168 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20169 return context_die;
20171 ns_context = setup_namespace_context (thing, context_die);
20173 if (ns_context != context_die)
20175 if (is_fortran ())
20176 return ns_context;
20177 if (DECL_P (thing))
20178 gen_decl_die (thing, NULL, ns_context);
20179 else
20180 gen_type_die (thing, ns_context);
20182 return context_die;
20185 /* Generate a DIE for a namespace or namespace alias. */
20187 static void
20188 gen_namespace_die (tree decl, dw_die_ref context_die)
20190 dw_die_ref namespace_die;
20192 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20193 they are an alias of. */
20194 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20196 /* Output a real namespace or module. */
20197 context_die = setup_namespace_context (decl, comp_unit_die ());
20198 namespace_die = new_die (is_fortran ()
20199 ? DW_TAG_module : DW_TAG_namespace,
20200 context_die, decl);
20201 /* For Fortran modules defined in different CU don't add src coords. */
20202 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20204 const char *name = dwarf2_name (decl, 0);
20205 if (name)
20206 add_name_attribute (namespace_die, name);
20208 else
20209 add_name_and_src_coords_attributes (namespace_die, decl);
20210 if (DECL_EXTERNAL (decl))
20211 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20212 equate_decl_number_to_die (decl, namespace_die);
20214 else
20216 /* Output a namespace alias. */
20218 /* Force out the namespace we are an alias of, if necessary. */
20219 dw_die_ref origin_die
20220 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20222 if (DECL_FILE_SCOPE_P (decl)
20223 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20224 context_die = setup_namespace_context (decl, comp_unit_die ());
20225 /* Now create the namespace alias DIE. */
20226 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20227 add_name_and_src_coords_attributes (namespace_die, decl);
20228 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20229 equate_decl_number_to_die (decl, namespace_die);
20231 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20232 if (want_pubnames ())
20233 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20236 /* Generate Dwarf debug information for a decl described by DECL.
20237 The return value is currently only meaningful for PARM_DECLs,
20238 for all other decls it returns NULL. */
20240 static dw_die_ref
20241 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20243 tree decl_or_origin = decl ? decl : origin;
20244 tree class_origin = NULL, ultimate_origin;
20246 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20247 return NULL;
20249 switch (TREE_CODE (decl_or_origin))
20251 case ERROR_MARK:
20252 break;
20254 case CONST_DECL:
20255 if (!is_fortran () && !is_ada ())
20257 /* The individual enumerators of an enum type get output when we output
20258 the Dwarf representation of the relevant enum type itself. */
20259 break;
20262 /* Emit its type. */
20263 gen_type_die (TREE_TYPE (decl), context_die);
20265 /* And its containing namespace. */
20266 context_die = declare_in_namespace (decl, context_die);
20268 gen_const_die (decl, context_die);
20269 break;
20271 case FUNCTION_DECL:
20272 /* Don't output any DIEs to represent mere function declarations,
20273 unless they are class members or explicit block externs. */
20274 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20275 && DECL_FILE_SCOPE_P (decl_or_origin)
20276 && (current_function_decl == NULL_TREE
20277 || DECL_ARTIFICIAL (decl_or_origin)))
20278 break;
20280 #if 0
20281 /* FIXME */
20282 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20283 on local redeclarations of global functions. That seems broken. */
20284 if (current_function_decl != decl)
20285 /* This is only a declaration. */;
20286 #endif
20288 /* If we're emitting a clone, emit info for the abstract instance. */
20289 if (origin || DECL_ORIGIN (decl) != decl)
20290 dwarf2out_abstract_function (origin
20291 ? DECL_ORIGIN (origin)
20292 : DECL_ABSTRACT_ORIGIN (decl));
20294 /* If we're emitting an out-of-line copy of an inline function,
20295 emit info for the abstract instance and set up to refer to it. */
20296 else if (cgraph_function_possibly_inlined_p (decl)
20297 && ! DECL_ABSTRACT (decl)
20298 && ! class_or_namespace_scope_p (context_die)
20299 /* dwarf2out_abstract_function won't emit a die if this is just
20300 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20301 that case, because that works only if we have a die. */
20302 && DECL_INITIAL (decl) != NULL_TREE)
20304 dwarf2out_abstract_function (decl);
20305 set_decl_origin_self (decl);
20308 /* Otherwise we're emitting the primary DIE for this decl. */
20309 else if (debug_info_level > DINFO_LEVEL_TERSE)
20311 /* Before we describe the FUNCTION_DECL itself, make sure that we
20312 have its containing type. */
20313 if (!origin)
20314 origin = decl_class_context (decl);
20315 if (origin != NULL_TREE)
20316 gen_type_die (origin, context_die);
20318 /* And its return type. */
20319 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20321 /* And its virtual context. */
20322 if (DECL_VINDEX (decl) != NULL_TREE)
20323 gen_type_die (DECL_CONTEXT (decl), context_die);
20325 /* Make sure we have a member DIE for decl. */
20326 if (origin != NULL_TREE)
20327 gen_type_die_for_member (origin, decl, context_die);
20329 /* And its containing namespace. */
20330 context_die = declare_in_namespace (decl, context_die);
20333 /* Now output a DIE to represent the function itself. */
20334 if (decl)
20335 gen_subprogram_die (decl, context_die);
20336 break;
20338 case TYPE_DECL:
20339 /* If we are in terse mode, don't generate any DIEs to represent any
20340 actual typedefs. */
20341 if (debug_info_level <= DINFO_LEVEL_TERSE)
20342 break;
20344 /* In the special case of a TYPE_DECL node representing the declaration
20345 of some type tag, if the given TYPE_DECL is marked as having been
20346 instantiated from some other (original) TYPE_DECL node (e.g. one which
20347 was generated within the original definition of an inline function) we
20348 used to generate a special (abbreviated) DW_TAG_structure_type,
20349 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20350 should be actually referencing those DIEs, as variable DIEs with that
20351 type would be emitted already in the abstract origin, so it was always
20352 removed during unused type prunning. Don't add anything in this
20353 case. */
20354 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20355 break;
20357 if (is_redundant_typedef (decl))
20358 gen_type_die (TREE_TYPE (decl), context_die);
20359 else
20360 /* Output a DIE to represent the typedef itself. */
20361 gen_typedef_die (decl, context_die);
20362 break;
20364 case LABEL_DECL:
20365 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20366 gen_label_die (decl, context_die);
20367 break;
20369 case VAR_DECL:
20370 case RESULT_DECL:
20371 /* If we are in terse mode, don't generate any DIEs to represent any
20372 variable declarations or definitions. */
20373 if (debug_info_level <= DINFO_LEVEL_TERSE)
20374 break;
20376 /* Output any DIEs that are needed to specify the type of this data
20377 object. */
20378 if (decl_by_reference_p (decl_or_origin))
20379 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20380 else
20381 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20383 /* And its containing type. */
20384 class_origin = decl_class_context (decl_or_origin);
20385 if (class_origin != NULL_TREE)
20386 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20388 /* And its containing namespace. */
20389 context_die = declare_in_namespace (decl_or_origin, context_die);
20391 /* Now output the DIE to represent the data object itself. This gets
20392 complicated because of the possibility that the VAR_DECL really
20393 represents an inlined instance of a formal parameter for an inline
20394 function. */
20395 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20396 if (ultimate_origin != NULL_TREE
20397 && TREE_CODE (ultimate_origin) == PARM_DECL)
20398 gen_formal_parameter_die (decl, origin,
20399 true /* Emit name attribute. */,
20400 context_die);
20401 else
20402 gen_variable_die (decl, origin, context_die);
20403 break;
20405 case FIELD_DECL:
20406 /* Ignore the nameless fields that are used to skip bits but handle C++
20407 anonymous unions and structs. */
20408 if (DECL_NAME (decl) != NULL_TREE
20409 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20410 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20412 gen_type_die (member_declared_type (decl), context_die);
20413 gen_field_die (decl, context_die);
20415 break;
20417 case PARM_DECL:
20418 if (DECL_BY_REFERENCE (decl_or_origin))
20419 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20420 else
20421 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20422 return gen_formal_parameter_die (decl, origin,
20423 true /* Emit name attribute. */,
20424 context_die);
20426 case NAMESPACE_DECL:
20427 case IMPORTED_DECL:
20428 if (dwarf_version >= 3 || !dwarf_strict)
20429 gen_namespace_die (decl, context_die);
20430 break;
20432 default:
20433 /* Probably some frontend-internal decl. Assume we don't care. */
20434 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20435 break;
20438 return NULL;
20441 /* Output debug information for global decl DECL. Called from toplev.c after
20442 compilation proper has finished. */
20444 static void
20445 dwarf2out_global_decl (tree decl)
20447 /* Output DWARF2 information for file-scope tentative data object
20448 declarations, file-scope (extern) function declarations (which
20449 had no corresponding body) and file-scope tagged type declarations
20450 and definitions which have not yet been forced out. */
20451 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20452 && !POINTER_BOUNDS_P (decl))
20453 dwarf2out_decl (decl);
20456 /* Output debug information for type decl DECL. Called from toplev.c
20457 and from language front ends (to record built-in types). */
20458 static void
20459 dwarf2out_type_decl (tree decl, int local)
20461 if (!local)
20462 dwarf2out_decl (decl);
20465 /* Output debug information for imported module or decl DECL.
20466 NAME is non-NULL name in the lexical block if the decl has been renamed.
20467 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20468 that DECL belongs to.
20469 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20470 static void
20471 dwarf2out_imported_module_or_decl_1 (tree decl,
20472 tree name,
20473 tree lexical_block,
20474 dw_die_ref lexical_block_die)
20476 expanded_location xloc;
20477 dw_die_ref imported_die = NULL;
20478 dw_die_ref at_import_die;
20480 if (TREE_CODE (decl) == IMPORTED_DECL)
20482 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20483 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20484 gcc_assert (decl);
20486 else
20487 xloc = expand_location (input_location);
20489 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20491 at_import_die = force_type_die (TREE_TYPE (decl));
20492 /* For namespace N { typedef void T; } using N::T; base_type_die
20493 returns NULL, but DW_TAG_imported_declaration requires
20494 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20495 if (!at_import_die)
20497 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20498 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20499 at_import_die = lookup_type_die (TREE_TYPE (decl));
20500 gcc_assert (at_import_die);
20503 else
20505 at_import_die = lookup_decl_die (decl);
20506 if (!at_import_die)
20508 /* If we're trying to avoid duplicate debug info, we may not have
20509 emitted the member decl for this field. Emit it now. */
20510 if (TREE_CODE (decl) == FIELD_DECL)
20512 tree type = DECL_CONTEXT (decl);
20514 if (TYPE_CONTEXT (type)
20515 && TYPE_P (TYPE_CONTEXT (type))
20516 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20517 DINFO_USAGE_DIR_USE))
20518 return;
20519 gen_type_die_for_member (type, decl,
20520 get_context_die (TYPE_CONTEXT (type)));
20522 at_import_die = force_decl_die (decl);
20526 if (TREE_CODE (decl) == NAMESPACE_DECL)
20528 if (dwarf_version >= 3 || !dwarf_strict)
20529 imported_die = new_die (DW_TAG_imported_module,
20530 lexical_block_die,
20531 lexical_block);
20532 else
20533 return;
20535 else
20536 imported_die = new_die (DW_TAG_imported_declaration,
20537 lexical_block_die,
20538 lexical_block);
20540 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20541 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20542 if (name)
20543 add_AT_string (imported_die, DW_AT_name,
20544 IDENTIFIER_POINTER (name));
20545 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20548 /* Output debug information for imported module or decl DECL.
20549 NAME is non-NULL name in context if the decl has been renamed.
20550 CHILD is true if decl is one of the renamed decls as part of
20551 importing whole module. */
20553 static void
20554 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20555 bool child)
20557 /* dw_die_ref at_import_die; */
20558 dw_die_ref scope_die;
20560 if (debug_info_level <= DINFO_LEVEL_TERSE)
20561 return;
20563 gcc_assert (decl);
20565 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20566 We need decl DIE for reference and scope die. First, get DIE for the decl
20567 itself. */
20569 /* Get the scope die for decl context. Use comp_unit_die for global module
20570 or decl. If die is not found for non globals, force new die. */
20571 if (context
20572 && TYPE_P (context)
20573 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20574 return;
20576 if (!(dwarf_version >= 3 || !dwarf_strict))
20577 return;
20579 scope_die = get_context_die (context);
20581 if (child)
20583 gcc_assert (scope_die->die_child);
20584 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20585 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20586 scope_die = scope_die->die_child;
20589 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20590 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20594 /* Write the debugging output for DECL. */
20596 void
20597 dwarf2out_decl (tree decl)
20599 dw_die_ref context_die = comp_unit_die ();
20601 switch (TREE_CODE (decl))
20603 case ERROR_MARK:
20604 return;
20606 case FUNCTION_DECL:
20607 /* What we would really like to do here is to filter out all mere
20608 file-scope declarations of file-scope functions which are never
20609 referenced later within this translation unit (and keep all of ones
20610 that *are* referenced later on) but we aren't clairvoyant, so we have
20611 no idea which functions will be referenced in the future (i.e. later
20612 on within the current translation unit). So here we just ignore all
20613 file-scope function declarations which are not also definitions. If
20614 and when the debugger needs to know something about these functions,
20615 it will have to hunt around and find the DWARF information associated
20616 with the definition of the function.
20618 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20619 nodes represent definitions and which ones represent mere
20620 declarations. We have to check DECL_INITIAL instead. That's because
20621 the C front-end supports some weird semantics for "extern inline"
20622 function definitions. These can get inlined within the current
20623 translation unit (and thus, we need to generate Dwarf info for their
20624 abstract instances so that the Dwarf info for the concrete inlined
20625 instances can have something to refer to) but the compiler never
20626 generates any out-of-lines instances of such things (despite the fact
20627 that they *are* definitions).
20629 The important point is that the C front-end marks these "extern
20630 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20631 them anyway. Note that the C++ front-end also plays some similar games
20632 for inline function definitions appearing within include files which
20633 also contain `#pragma interface' pragmas.
20635 If we are called from dwarf2out_abstract_function output a DIE
20636 anyway. We can end up here this way with early inlining and LTO
20637 where the inlined function is output in a different LTRANS unit
20638 or not at all. */
20639 if (DECL_INITIAL (decl) == NULL_TREE
20640 && ! DECL_ABSTRACT (decl))
20641 return;
20643 /* If we're a nested function, initially use a parent of NULL; if we're
20644 a plain function, this will be fixed up in decls_for_scope. If
20645 we're a method, it will be ignored, since we already have a DIE. */
20646 if (decl_function_context (decl)
20647 /* But if we're in terse mode, we don't care about scope. */
20648 && debug_info_level > DINFO_LEVEL_TERSE)
20649 context_die = NULL;
20650 break;
20652 case VAR_DECL:
20653 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20654 declaration and if the declaration was never even referenced from
20655 within this entire compilation unit. We suppress these DIEs in
20656 order to save space in the .debug section (by eliminating entries
20657 which are probably useless). Note that we must not suppress
20658 block-local extern declarations (whether used or not) because that
20659 would screw-up the debugger's name lookup mechanism and cause it to
20660 miss things which really ought to be in scope at a given point. */
20661 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20662 return;
20664 /* For local statics lookup proper context die. */
20665 if (TREE_STATIC (decl)
20666 && DECL_CONTEXT (decl)
20667 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20668 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20670 /* If we are in terse mode, don't generate any DIEs to represent any
20671 variable declarations or definitions. */
20672 if (debug_info_level <= DINFO_LEVEL_TERSE)
20673 return;
20674 break;
20676 case CONST_DECL:
20677 if (debug_info_level <= DINFO_LEVEL_TERSE)
20678 return;
20679 if (!is_fortran () && !is_ada ())
20680 return;
20681 if (TREE_STATIC (decl) && decl_function_context (decl))
20682 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20683 break;
20685 case NAMESPACE_DECL:
20686 case IMPORTED_DECL:
20687 if (debug_info_level <= DINFO_LEVEL_TERSE)
20688 return;
20689 if (lookup_decl_die (decl) != NULL)
20690 return;
20691 break;
20693 case TYPE_DECL:
20694 /* Don't emit stubs for types unless they are needed by other DIEs. */
20695 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20696 return;
20698 /* Don't bother trying to generate any DIEs to represent any of the
20699 normal built-in types for the language we are compiling. */
20700 if (DECL_IS_BUILTIN (decl))
20701 return;
20703 /* If we are in terse mode, don't generate any DIEs for types. */
20704 if (debug_info_level <= DINFO_LEVEL_TERSE)
20705 return;
20707 /* If we're a function-scope tag, initially use a parent of NULL;
20708 this will be fixed up in decls_for_scope. */
20709 if (decl_function_context (decl))
20710 context_die = NULL;
20712 break;
20714 default:
20715 return;
20718 gen_decl_die (decl, NULL, context_die);
20721 /* Write the debugging output for DECL. */
20723 static void
20724 dwarf2out_function_decl (tree decl)
20726 dwarf2out_decl (decl);
20727 call_arg_locations = NULL;
20728 call_arg_loc_last = NULL;
20729 call_site_count = -1;
20730 tail_call_site_count = -1;
20731 block_map.release ();
20732 htab_empty (decl_loc_table);
20733 htab_empty (cached_dw_loc_list_table);
20736 /* Output a marker (i.e. a label) for the beginning of the generated code for
20737 a lexical block. */
20739 static void
20740 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20741 unsigned int blocknum)
20743 switch_to_section (current_function_section ());
20744 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20747 /* Output a marker (i.e. a label) for the end of the generated code for a
20748 lexical block. */
20750 static void
20751 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20753 switch_to_section (current_function_section ());
20754 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20757 /* Returns nonzero if it is appropriate not to emit any debugging
20758 information for BLOCK, because it doesn't contain any instructions.
20760 Don't allow this for blocks with nested functions or local classes
20761 as we would end up with orphans, and in the presence of scheduling
20762 we may end up calling them anyway. */
20764 static bool
20765 dwarf2out_ignore_block (const_tree block)
20767 tree decl;
20768 unsigned int i;
20770 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20771 if (TREE_CODE (decl) == FUNCTION_DECL
20772 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20773 return 0;
20774 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20776 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20777 if (TREE_CODE (decl) == FUNCTION_DECL
20778 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20779 return 0;
20782 return 1;
20785 /* Hash table routines for file_hash. */
20787 static int
20788 file_table_eq (const void *p1_p, const void *p2_p)
20790 const struct dwarf_file_data *const p1 =
20791 (const struct dwarf_file_data *) p1_p;
20792 const char *const p2 = (const char *) p2_p;
20793 return filename_cmp (p1->filename, p2) == 0;
20796 static hashval_t
20797 file_table_hash (const void *p_p)
20799 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20800 return htab_hash_string (p->filename);
20803 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20804 dwarf2out.c) and return its "index". The index of each (known) filename is
20805 just a unique number which is associated with only that one filename. We
20806 need such numbers for the sake of generating labels (in the .debug_sfnames
20807 section) and references to those files numbers (in the .debug_srcinfo
20808 and.debug_macinfo sections). If the filename given as an argument is not
20809 found in our current list, add it to the list and assign it the next
20810 available unique index number. In order to speed up searches, we remember
20811 the index of the filename was looked up last. This handles the majority of
20812 all searches. */
20814 static struct dwarf_file_data *
20815 lookup_filename (const char *file_name)
20817 void ** slot;
20818 struct dwarf_file_data * created;
20820 /* Check to see if the file name that was searched on the previous
20821 call matches this file name. If so, return the index. */
20822 if (file_table_last_lookup
20823 && (file_name == file_table_last_lookup->filename
20824 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20825 return file_table_last_lookup;
20827 /* Didn't match the previous lookup, search the table. */
20828 slot = htab_find_slot_with_hash (file_table, file_name,
20829 htab_hash_string (file_name), INSERT);
20830 if (*slot)
20831 return (struct dwarf_file_data *) *slot;
20833 created = ggc_alloc_dwarf_file_data ();
20834 created->filename = file_name;
20835 created->emitted_number = 0;
20836 *slot = created;
20837 return created;
20840 /* If the assembler will construct the file table, then translate the compiler
20841 internal file table number into the assembler file table number, and emit
20842 a .file directive if we haven't already emitted one yet. The file table
20843 numbers are different because we prune debug info for unused variables and
20844 types, which may include filenames. */
20846 static int
20847 maybe_emit_file (struct dwarf_file_data * fd)
20849 if (! fd->emitted_number)
20851 if (last_emitted_file)
20852 fd->emitted_number = last_emitted_file->emitted_number + 1;
20853 else
20854 fd->emitted_number = 1;
20855 last_emitted_file = fd;
20857 if (DWARF2_ASM_LINE_DEBUG_INFO)
20859 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20860 output_quoted_string (asm_out_file,
20861 remap_debug_filename (fd->filename));
20862 fputc ('\n', asm_out_file);
20866 return fd->emitted_number;
20869 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20870 That generation should happen after function debug info has been
20871 generated. The value of the attribute is the constant value of ARG. */
20873 static void
20874 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20876 die_arg_entry entry;
20878 if (!die || !arg)
20879 return;
20881 if (!tmpl_value_parm_die_table)
20882 vec_alloc (tmpl_value_parm_die_table, 32);
20884 entry.die = die;
20885 entry.arg = arg;
20886 vec_safe_push (tmpl_value_parm_die_table, entry);
20889 /* Return TRUE if T is an instance of generic type, FALSE
20890 otherwise. */
20892 static bool
20893 generic_type_p (tree t)
20895 if (t == NULL_TREE || !TYPE_P (t))
20896 return false;
20897 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20900 /* Schedule the generation of the generic parameter dies for the
20901 instance of generic type T. The proper generation itself is later
20902 done by gen_scheduled_generic_parms_dies. */
20904 static void
20905 schedule_generic_params_dies_gen (tree t)
20907 if (!generic_type_p (t))
20908 return;
20910 if (!generic_type_instances)
20911 vec_alloc (generic_type_instances, 256);
20913 vec_safe_push (generic_type_instances, t);
20916 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20917 by append_entry_to_tmpl_value_parm_die_table. This function must
20918 be called after function DIEs have been generated. */
20920 static void
20921 gen_remaining_tmpl_value_param_die_attribute (void)
20923 if (tmpl_value_parm_die_table)
20925 unsigned i;
20926 die_arg_entry *e;
20928 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20929 tree_add_const_value_attribute (e->die, e->arg);
20933 /* Generate generic parameters DIEs for instances of generic types
20934 that have been previously scheduled by
20935 schedule_generic_params_dies_gen. This function must be called
20936 after all the types of the CU have been laid out. */
20938 static void
20939 gen_scheduled_generic_parms_dies (void)
20941 unsigned i;
20942 tree t;
20944 if (!generic_type_instances)
20945 return;
20947 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20948 if (COMPLETE_TYPE_P (t))
20949 gen_generic_params_dies (t);
20953 /* Replace DW_AT_name for the decl with name. */
20955 static void
20956 dwarf2out_set_name (tree decl, tree name)
20958 dw_die_ref die;
20959 dw_attr_ref attr;
20960 const char *dname;
20962 die = TYPE_SYMTAB_DIE (decl);
20963 if (!die)
20964 return;
20966 dname = dwarf2_name (name, 0);
20967 if (!dname)
20968 return;
20970 attr = get_AT (die, DW_AT_name);
20971 if (attr)
20973 struct indirect_string_node *node;
20975 node = find_AT_string (dname);
20976 /* replace the string. */
20977 attr->dw_attr_val.v.val_str = node;
20980 else
20981 add_name_attribute (die, dname);
20984 /* True if before or during processing of the first function being emitted. */
20985 static bool in_first_function_p = true;
20986 /* True if loc_note during dwarf2out_var_location call might still be
20987 before first real instruction at address equal to .Ltext0. */
20988 static bool maybe_at_text_label_p = true;
20989 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20990 static unsigned int first_loclabel_num_not_at_text_label;
20992 /* Called by the final INSN scan whenever we see a var location. We
20993 use it to drop labels in the right places, and throw the location in
20994 our lookup table. */
20996 static void
20997 dwarf2out_var_location (rtx loc_note)
20999 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21000 struct var_loc_node *newloc;
21001 rtx next_real, next_note;
21002 static const char *last_label;
21003 static const char *last_postcall_label;
21004 static bool last_in_cold_section_p;
21005 static rtx expected_next_loc_note;
21006 tree decl;
21007 bool var_loc_p;
21009 if (!NOTE_P (loc_note))
21011 if (CALL_P (loc_note))
21013 call_site_count++;
21014 if (SIBLING_CALL_P (loc_note))
21015 tail_call_site_count++;
21017 return;
21020 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21021 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21022 return;
21024 /* Optimize processing a large consecutive sequence of location
21025 notes so we don't spend too much time in next_real_insn. If the
21026 next insn is another location note, remember the next_real_insn
21027 calculation for next time. */
21028 next_real = cached_next_real_insn;
21029 if (next_real)
21031 if (expected_next_loc_note != loc_note)
21032 next_real = NULL_RTX;
21035 next_note = NEXT_INSN (loc_note);
21036 if (! next_note
21037 || INSN_DELETED_P (next_note)
21038 || ! NOTE_P (next_note)
21039 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21040 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21041 next_note = NULL_RTX;
21043 if (! next_real)
21044 next_real = next_real_insn (loc_note);
21046 if (next_note)
21048 expected_next_loc_note = next_note;
21049 cached_next_real_insn = next_real;
21051 else
21052 cached_next_real_insn = NULL_RTX;
21054 /* If there are no instructions which would be affected by this note,
21055 don't do anything. */
21056 if (var_loc_p
21057 && next_real == NULL_RTX
21058 && !NOTE_DURING_CALL_P (loc_note))
21059 return;
21061 if (next_real == NULL_RTX)
21062 next_real = get_last_insn ();
21064 /* If there were any real insns between note we processed last time
21065 and this note (or if it is the first note), clear
21066 last_{,postcall_}label so that they are not reused this time. */
21067 if (last_var_location_insn == NULL_RTX
21068 || last_var_location_insn != next_real
21069 || last_in_cold_section_p != in_cold_section_p)
21071 last_label = NULL;
21072 last_postcall_label = NULL;
21075 if (var_loc_p)
21077 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21078 newloc = add_var_loc_to_decl (decl, loc_note,
21079 NOTE_DURING_CALL_P (loc_note)
21080 ? last_postcall_label : last_label);
21081 if (newloc == NULL)
21082 return;
21084 else
21086 decl = NULL_TREE;
21087 newloc = NULL;
21090 /* If there were no real insns between note we processed last time
21091 and this note, use the label we emitted last time. Otherwise
21092 create a new label and emit it. */
21093 if (last_label == NULL)
21095 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21096 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21097 loclabel_num++;
21098 last_label = ggc_strdup (loclabel);
21099 /* See if loclabel might be equal to .Ltext0. If yes,
21100 bump first_loclabel_num_not_at_text_label. */
21101 if (!have_multiple_function_sections
21102 && in_first_function_p
21103 && maybe_at_text_label_p)
21105 static rtx last_start;
21106 rtx insn;
21107 for (insn = loc_note; insn; insn = previous_insn (insn))
21108 if (insn == last_start)
21109 break;
21110 else if (!NONDEBUG_INSN_P (insn))
21111 continue;
21112 else
21114 rtx body = PATTERN (insn);
21115 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21116 continue;
21117 /* Inline asm could occupy zero bytes. */
21118 else if (GET_CODE (body) == ASM_INPUT
21119 || asm_noperands (body) >= 0)
21120 continue;
21121 #ifdef HAVE_attr_length
21122 else if (get_attr_min_length (insn) == 0)
21123 continue;
21124 #endif
21125 else
21127 /* Assume insn has non-zero length. */
21128 maybe_at_text_label_p = false;
21129 break;
21132 if (maybe_at_text_label_p)
21134 last_start = loc_note;
21135 first_loclabel_num_not_at_text_label = loclabel_num;
21140 if (!var_loc_p)
21142 struct call_arg_loc_node *ca_loc
21143 = ggc_alloc_cleared_call_arg_loc_node ();
21144 rtx prev = prev_real_insn (loc_note), x;
21145 ca_loc->call_arg_loc_note = loc_note;
21146 ca_loc->next = NULL;
21147 ca_loc->label = last_label;
21148 gcc_assert (prev
21149 && (CALL_P (prev)
21150 || (NONJUMP_INSN_P (prev)
21151 && GET_CODE (PATTERN (prev)) == SEQUENCE
21152 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21153 if (!CALL_P (prev))
21154 prev = XVECEXP (PATTERN (prev), 0, 0);
21155 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21156 x = get_call_rtx_from (PATTERN (prev));
21157 if (x)
21159 x = XEXP (XEXP (x, 0), 0);
21160 if (GET_CODE (x) == SYMBOL_REF
21161 && SYMBOL_REF_DECL (x)
21162 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21163 ca_loc->symbol_ref = x;
21165 ca_loc->block = insn_scope (prev);
21166 if (call_arg_locations)
21167 call_arg_loc_last->next = ca_loc;
21168 else
21169 call_arg_locations = ca_loc;
21170 call_arg_loc_last = ca_loc;
21172 else if (!NOTE_DURING_CALL_P (loc_note))
21173 newloc->label = last_label;
21174 else
21176 if (!last_postcall_label)
21178 sprintf (loclabel, "%s-1", last_label);
21179 last_postcall_label = ggc_strdup (loclabel);
21181 newloc->label = last_postcall_label;
21184 last_var_location_insn = next_real;
21185 last_in_cold_section_p = in_cold_section_p;
21188 /* Note in one location list that text section has changed. */
21190 static int
21191 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21193 var_loc_list *list = (var_loc_list *) *slot;
21194 if (list->first)
21195 list->last_before_switch
21196 = list->last->next ? list->last->next : list->last;
21197 return 1;
21200 /* Note in all location lists that text section has changed. */
21202 static void
21203 var_location_switch_text_section (void)
21205 if (decl_loc_table == NULL)
21206 return;
21208 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21211 /* Create a new line number table. */
21213 static dw_line_info_table *
21214 new_line_info_table (void)
21216 dw_line_info_table *table;
21218 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21219 table->file_num = 1;
21220 table->line_num = 1;
21221 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21223 return table;
21226 /* Lookup the "current" table into which we emit line info, so
21227 that we don't have to do it for every source line. */
21229 static void
21230 set_cur_line_info_table (section *sec)
21232 dw_line_info_table *table;
21234 if (sec == text_section)
21235 table = text_section_line_info;
21236 else if (sec == cold_text_section)
21238 table = cold_text_section_line_info;
21239 if (!table)
21241 cold_text_section_line_info = table = new_line_info_table ();
21242 table->end_label = cold_end_label;
21245 else
21247 const char *end_label;
21249 if (flag_reorder_blocks_and_partition)
21251 if (in_cold_section_p)
21252 end_label = crtl->subsections.cold_section_end_label;
21253 else
21254 end_label = crtl->subsections.hot_section_end_label;
21256 else
21258 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21259 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21260 current_function_funcdef_no);
21261 end_label = ggc_strdup (label);
21264 table = new_line_info_table ();
21265 table->end_label = end_label;
21267 vec_safe_push (separate_line_info, table);
21270 if (DWARF2_ASM_LINE_DEBUG_INFO)
21271 table->is_stmt = (cur_line_info_table
21272 ? cur_line_info_table->is_stmt
21273 : DWARF_LINE_DEFAULT_IS_STMT_START);
21274 cur_line_info_table = table;
21278 /* We need to reset the locations at the beginning of each
21279 function. We can't do this in the end_function hook, because the
21280 declarations that use the locations won't have been output when
21281 that hook is called. Also compute have_multiple_function_sections here. */
21283 static void
21284 dwarf2out_begin_function (tree fun)
21286 section *sec = function_section (fun);
21288 if (sec != text_section)
21289 have_multiple_function_sections = true;
21291 if (flag_reorder_blocks_and_partition && !cold_text_section)
21293 gcc_assert (current_function_decl == fun);
21294 cold_text_section = unlikely_text_section ();
21295 switch_to_section (cold_text_section);
21296 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21297 switch_to_section (sec);
21300 dwarf2out_note_section_used ();
21301 call_site_count = 0;
21302 tail_call_site_count = 0;
21304 set_cur_line_info_table (sec);
21307 /* Helper function of dwarf2out_end_function, called only after emitting
21308 the very first function into assembly. Check if some .debug_loc range
21309 might end with a .LVL* label that could be equal to .Ltext0.
21310 In that case we must force using absolute addresses in .debug_loc ranges,
21311 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21312 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21313 list terminator.
21314 Set have_multiple_function_sections to true in that case and
21315 terminate htab traversal. */
21317 static int
21318 find_empty_loc_ranges_at_text_label (void **slot, void *)
21320 var_loc_list *entry;
21321 struct var_loc_node *node;
21323 entry = (var_loc_list *) *slot;
21324 node = entry->first;
21325 if (node && node->next && node->next->label)
21327 unsigned int i;
21328 const char *label = node->next->label;
21329 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21331 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21333 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21334 if (strcmp (label, loclabel) == 0)
21336 have_multiple_function_sections = true;
21337 return 0;
21341 return 1;
21344 /* Hook called after emitting a function into assembly.
21345 This does something only for the very first function emitted. */
21347 static void
21348 dwarf2out_end_function (unsigned int)
21350 if (in_first_function_p
21351 && !have_multiple_function_sections
21352 && first_loclabel_num_not_at_text_label
21353 && decl_loc_table)
21354 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21355 NULL);
21356 in_first_function_p = false;
21357 maybe_at_text_label_p = false;
21360 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21362 static void
21363 push_dw_line_info_entry (dw_line_info_table *table,
21364 enum dw_line_info_opcode opcode, unsigned int val)
21366 dw_line_info_entry e;
21367 e.opcode = opcode;
21368 e.val = val;
21369 vec_safe_push (table->entries, e);
21372 /* Output a label to mark the beginning of a source code line entry
21373 and record information relating to this source line, in
21374 'line_info_table' for later output of the .debug_line section. */
21375 /* ??? The discriminator parameter ought to be unsigned. */
21377 static void
21378 dwarf2out_source_line (unsigned int line, const char *filename,
21379 int discriminator, bool is_stmt)
21381 unsigned int file_num;
21382 dw_line_info_table *table;
21384 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21385 return;
21387 /* The discriminator column was added in dwarf4. Simplify the below
21388 by simply removing it if we're not supposed to output it. */
21389 if (dwarf_version < 4 && dwarf_strict)
21390 discriminator = 0;
21392 table = cur_line_info_table;
21393 file_num = maybe_emit_file (lookup_filename (filename));
21395 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21396 the debugger has used the second (possibly duplicate) line number
21397 at the beginning of the function to mark the end of the prologue.
21398 We could eliminate any other duplicates within the function. For
21399 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21400 that second line number entry. */
21401 /* Recall that this end-of-prologue indication is *not* the same thing
21402 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21403 to which the hook corresponds, follows the last insn that was
21404 emitted by gen_prologue. What we need is to precede the first insn
21405 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21406 insn that corresponds to something the user wrote. These may be
21407 very different locations once scheduling is enabled. */
21409 if (0 && file_num == table->file_num
21410 && line == table->line_num
21411 && discriminator == table->discrim_num
21412 && is_stmt == table->is_stmt)
21413 return;
21415 switch_to_section (current_function_section ());
21417 /* If requested, emit something human-readable. */
21418 if (flag_debug_asm)
21419 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21421 if (DWARF2_ASM_LINE_DEBUG_INFO)
21423 /* Emit the .loc directive understood by GNU as. */
21424 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21425 file_num, line, is_stmt, discriminator */
21426 fputs ("\t.loc ", asm_out_file);
21427 fprint_ul (asm_out_file, file_num);
21428 putc (' ', asm_out_file);
21429 fprint_ul (asm_out_file, line);
21430 putc (' ', asm_out_file);
21431 putc ('0', asm_out_file);
21433 if (is_stmt != table->is_stmt)
21435 fputs (" is_stmt ", asm_out_file);
21436 putc (is_stmt ? '1' : '0', asm_out_file);
21438 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21440 gcc_assert (discriminator > 0);
21441 fputs (" discriminator ", asm_out_file);
21442 fprint_ul (asm_out_file, (unsigned long) discriminator);
21444 putc ('\n', asm_out_file);
21446 else
21448 unsigned int label_num = ++line_info_label_num;
21450 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21452 push_dw_line_info_entry (table, LI_set_address, label_num);
21453 if (file_num != table->file_num)
21454 push_dw_line_info_entry (table, LI_set_file, file_num);
21455 if (discriminator != table->discrim_num)
21456 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21457 if (is_stmt != table->is_stmt)
21458 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21459 push_dw_line_info_entry (table, LI_set_line, line);
21462 table->file_num = file_num;
21463 table->line_num = line;
21464 table->discrim_num = discriminator;
21465 table->is_stmt = is_stmt;
21466 table->in_use = true;
21469 /* Record the beginning of a new source file. */
21471 static void
21472 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21474 if (flag_eliminate_dwarf2_dups)
21476 /* Record the beginning of the file for break_out_includes. */
21477 dw_die_ref bincl_die;
21479 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21480 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21483 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21485 macinfo_entry e;
21486 e.code = DW_MACINFO_start_file;
21487 e.lineno = lineno;
21488 e.info = ggc_strdup (filename);
21489 vec_safe_push (macinfo_table, e);
21493 /* Record the end of a source file. */
21495 static void
21496 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21498 if (flag_eliminate_dwarf2_dups)
21499 /* Record the end of the file for break_out_includes. */
21500 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21502 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21504 macinfo_entry e;
21505 e.code = DW_MACINFO_end_file;
21506 e.lineno = lineno;
21507 e.info = NULL;
21508 vec_safe_push (macinfo_table, e);
21512 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21513 the tail part of the directive line, i.e. the part which is past the
21514 initial whitespace, #, whitespace, directive-name, whitespace part. */
21516 static void
21517 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21518 const char *buffer ATTRIBUTE_UNUSED)
21520 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21522 macinfo_entry e;
21523 /* Insert a dummy first entry to be able to optimize the whole
21524 predefined macro block using DW_MACRO_GNU_transparent_include. */
21525 if (macinfo_table->is_empty () && lineno <= 1)
21527 e.code = 0;
21528 e.lineno = 0;
21529 e.info = NULL;
21530 vec_safe_push (macinfo_table, e);
21532 e.code = DW_MACINFO_define;
21533 e.lineno = lineno;
21534 e.info = ggc_strdup (buffer);
21535 vec_safe_push (macinfo_table, e);
21539 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21540 the tail part of the directive line, i.e. the part which is past the
21541 initial whitespace, #, whitespace, directive-name, whitespace part. */
21543 static void
21544 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21545 const char *buffer ATTRIBUTE_UNUSED)
21547 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21549 macinfo_entry e;
21550 /* Insert a dummy first entry to be able to optimize the whole
21551 predefined macro block using DW_MACRO_GNU_transparent_include. */
21552 if (macinfo_table->is_empty () && lineno <= 1)
21554 e.code = 0;
21555 e.lineno = 0;
21556 e.info = NULL;
21557 vec_safe_push (macinfo_table, e);
21559 e.code = DW_MACINFO_undef;
21560 e.lineno = lineno;
21561 e.info = ggc_strdup (buffer);
21562 vec_safe_push (macinfo_table, e);
21566 /* Helpers to manipulate hash table of CUs. */
21568 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21570 typedef macinfo_entry value_type;
21571 typedef macinfo_entry compare_type;
21572 static inline hashval_t hash (const value_type *);
21573 static inline bool equal (const value_type *, const compare_type *);
21576 inline hashval_t
21577 macinfo_entry_hasher::hash (const value_type *entry)
21579 return htab_hash_string (entry->info);
21582 inline bool
21583 macinfo_entry_hasher::equal (const value_type *entry1,
21584 const compare_type *entry2)
21586 return !strcmp (entry1->info, entry2->info);
21589 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21591 /* Output a single .debug_macinfo entry. */
21593 static void
21594 output_macinfo_op (macinfo_entry *ref)
21596 int file_num;
21597 size_t len;
21598 struct indirect_string_node *node;
21599 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21600 struct dwarf_file_data *fd;
21602 switch (ref->code)
21604 case DW_MACINFO_start_file:
21605 fd = lookup_filename (ref->info);
21606 file_num = maybe_emit_file (fd);
21607 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21608 dw2_asm_output_data_uleb128 (ref->lineno,
21609 "Included from line number %lu",
21610 (unsigned long) ref->lineno);
21611 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21612 break;
21613 case DW_MACINFO_end_file:
21614 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21615 break;
21616 case DW_MACINFO_define:
21617 case DW_MACINFO_undef:
21618 len = strlen (ref->info) + 1;
21619 if (!dwarf_strict
21620 && len > DWARF_OFFSET_SIZE
21621 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21622 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21624 ref->code = ref->code == DW_MACINFO_define
21625 ? DW_MACRO_GNU_define_indirect
21626 : DW_MACRO_GNU_undef_indirect;
21627 output_macinfo_op (ref);
21628 return;
21630 dw2_asm_output_data (1, ref->code,
21631 ref->code == DW_MACINFO_define
21632 ? "Define macro" : "Undefine macro");
21633 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21634 (unsigned long) ref->lineno);
21635 dw2_asm_output_nstring (ref->info, -1, "The macro");
21636 break;
21637 case DW_MACRO_GNU_define_indirect:
21638 case DW_MACRO_GNU_undef_indirect:
21639 node = find_AT_string (ref->info);
21640 gcc_assert (node
21641 && ((node->form == DW_FORM_strp)
21642 || (node->form == DW_FORM_GNU_str_index)));
21643 dw2_asm_output_data (1, ref->code,
21644 ref->code == DW_MACRO_GNU_define_indirect
21645 ? "Define macro indirect"
21646 : "Undefine macro indirect");
21647 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21648 (unsigned long) ref->lineno);
21649 if (node->form == DW_FORM_strp)
21650 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21651 debug_str_section, "The macro: \"%s\"",
21652 ref->info);
21653 else
21654 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21655 ref->info);
21656 break;
21657 case DW_MACRO_GNU_transparent_include:
21658 dw2_asm_output_data (1, ref->code, "Transparent include");
21659 ASM_GENERATE_INTERNAL_LABEL (label,
21660 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21661 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21662 break;
21663 default:
21664 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21665 ASM_COMMENT_START, (unsigned long) ref->code);
21666 break;
21670 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21671 other compilation unit .debug_macinfo sections. IDX is the first
21672 index of a define/undef, return the number of ops that should be
21673 emitted in a comdat .debug_macinfo section and emit
21674 a DW_MACRO_GNU_transparent_include entry referencing it.
21675 If the define/undef entry should be emitted normally, return 0. */
21677 static unsigned
21678 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21679 macinfo_hash_type *macinfo_htab)
21681 macinfo_entry *first, *second, *cur, *inc;
21682 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21683 unsigned char checksum[16];
21684 struct md5_ctx ctx;
21685 char *grp_name, *tail;
21686 const char *base;
21687 unsigned int i, count, encoded_filename_len, linebuf_len;
21688 macinfo_entry **slot;
21690 first = &(*macinfo_table)[idx];
21691 second = &(*macinfo_table)[idx + 1];
21693 /* Optimize only if there are at least two consecutive define/undef ops,
21694 and either all of them are before first DW_MACINFO_start_file
21695 with lineno {0,1} (i.e. predefined macro block), or all of them are
21696 in some included header file. */
21697 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21698 return 0;
21699 if (vec_safe_is_empty (files))
21701 if (first->lineno > 1 || second->lineno > 1)
21702 return 0;
21704 else if (first->lineno == 0)
21705 return 0;
21707 /* Find the last define/undef entry that can be grouped together
21708 with first and at the same time compute md5 checksum of their
21709 codes, linenumbers and strings. */
21710 md5_init_ctx (&ctx);
21711 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21712 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21713 break;
21714 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21715 break;
21716 else
21718 unsigned char code = cur->code;
21719 md5_process_bytes (&code, 1, &ctx);
21720 checksum_uleb128 (cur->lineno, &ctx);
21721 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21723 md5_finish_ctx (&ctx, checksum);
21724 count = i - idx;
21726 /* From the containing include filename (if any) pick up just
21727 usable characters from its basename. */
21728 if (vec_safe_is_empty (files))
21729 base = "";
21730 else
21731 base = lbasename (files->last ().info);
21732 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21733 if (ISIDNUM (base[i]) || base[i] == '.')
21734 encoded_filename_len++;
21735 /* Count . at the end. */
21736 if (encoded_filename_len)
21737 encoded_filename_len++;
21739 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21740 linebuf_len = strlen (linebuf);
21742 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21743 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21744 + 16 * 2 + 1);
21745 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21746 tail = grp_name + 4;
21747 if (encoded_filename_len)
21749 for (i = 0; base[i]; i++)
21750 if (ISIDNUM (base[i]) || base[i] == '.')
21751 *tail++ = base[i];
21752 *tail++ = '.';
21754 memcpy (tail, linebuf, linebuf_len);
21755 tail += linebuf_len;
21756 *tail++ = '.';
21757 for (i = 0; i < 16; i++)
21758 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21760 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21761 in the empty vector entry before the first define/undef. */
21762 inc = &(*macinfo_table)[idx - 1];
21763 inc->code = DW_MACRO_GNU_transparent_include;
21764 inc->lineno = 0;
21765 inc->info = ggc_strdup (grp_name);
21766 if (!macinfo_htab->is_created ())
21767 macinfo_htab->create (10);
21768 /* Avoid emitting duplicates. */
21769 slot = macinfo_htab->find_slot (inc, INSERT);
21770 if (*slot != NULL)
21772 inc->code = 0;
21773 inc->info = NULL;
21774 /* If such an entry has been used before, just emit
21775 a DW_MACRO_GNU_transparent_include op. */
21776 inc = *slot;
21777 output_macinfo_op (inc);
21778 /* And clear all macinfo_entry in the range to avoid emitting them
21779 in the second pass. */
21780 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21782 cur->code = 0;
21783 cur->info = NULL;
21786 else
21788 *slot = inc;
21789 inc->lineno = macinfo_htab->elements ();
21790 output_macinfo_op (inc);
21792 return count;
21795 /* Save any strings needed by the macinfo table in the debug str
21796 table. All strings must be collected into the table by the time
21797 index_string is called. */
21799 static void
21800 save_macinfo_strings (void)
21802 unsigned len;
21803 unsigned i;
21804 macinfo_entry *ref;
21806 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21808 switch (ref->code)
21810 /* Match the logic in output_macinfo_op to decide on
21811 indirect strings. */
21812 case DW_MACINFO_define:
21813 case DW_MACINFO_undef:
21814 len = strlen (ref->info) + 1;
21815 if (!dwarf_strict
21816 && len > DWARF_OFFSET_SIZE
21817 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21818 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21819 set_indirect_string (find_AT_string (ref->info));
21820 break;
21821 case DW_MACRO_GNU_define_indirect:
21822 case DW_MACRO_GNU_undef_indirect:
21823 set_indirect_string (find_AT_string (ref->info));
21824 break;
21825 default:
21826 break;
21831 /* Output macinfo section(s). */
21833 static void
21834 output_macinfo (void)
21836 unsigned i;
21837 unsigned long length = vec_safe_length (macinfo_table);
21838 macinfo_entry *ref;
21839 vec<macinfo_entry, va_gc> *files = NULL;
21840 macinfo_hash_type macinfo_htab;
21842 if (! length)
21843 return;
21845 /* output_macinfo* uses these interchangeably. */
21846 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21847 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21848 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21849 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21851 /* For .debug_macro emit the section header. */
21852 if (!dwarf_strict)
21854 dw2_asm_output_data (2, 4, "DWARF macro version number");
21855 if (DWARF_OFFSET_SIZE == 8)
21856 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21857 else
21858 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21859 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21860 (!dwarf_split_debug_info ? debug_line_section_label
21861 : debug_skeleton_line_section_label),
21862 debug_line_section, NULL);
21865 /* In the first loop, it emits the primary .debug_macinfo section
21866 and after each emitted op the macinfo_entry is cleared.
21867 If a longer range of define/undef ops can be optimized using
21868 DW_MACRO_GNU_transparent_include, the
21869 DW_MACRO_GNU_transparent_include op is emitted and kept in
21870 the vector before the first define/undef in the range and the
21871 whole range of define/undef ops is not emitted and kept. */
21872 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21874 switch (ref->code)
21876 case DW_MACINFO_start_file:
21877 vec_safe_push (files, *ref);
21878 break;
21879 case DW_MACINFO_end_file:
21880 if (!vec_safe_is_empty (files))
21881 files->pop ();
21882 break;
21883 case DW_MACINFO_define:
21884 case DW_MACINFO_undef:
21885 if (!dwarf_strict
21886 && HAVE_COMDAT_GROUP
21887 && vec_safe_length (files) != 1
21888 && i > 0
21889 && i + 1 < length
21890 && (*macinfo_table)[i - 1].code == 0)
21892 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21893 if (count)
21895 i += count - 1;
21896 continue;
21899 break;
21900 case 0:
21901 /* A dummy entry may be inserted at the beginning to be able
21902 to optimize the whole block of predefined macros. */
21903 if (i == 0)
21904 continue;
21905 default:
21906 break;
21908 output_macinfo_op (ref);
21909 ref->info = NULL;
21910 ref->code = 0;
21913 if (!macinfo_htab.is_created ())
21914 return;
21916 macinfo_htab.dispose ();
21918 /* If any DW_MACRO_GNU_transparent_include were used, on those
21919 DW_MACRO_GNU_transparent_include entries terminate the
21920 current chain and switch to a new comdat .debug_macinfo
21921 section and emit the define/undef entries within it. */
21922 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21923 switch (ref->code)
21925 case 0:
21926 continue;
21927 case DW_MACRO_GNU_transparent_include:
21929 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21930 tree comdat_key = get_identifier (ref->info);
21931 /* Terminate the previous .debug_macinfo section. */
21932 dw2_asm_output_data (1, 0, "End compilation unit");
21933 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21934 SECTION_DEBUG
21935 | SECTION_LINKONCE,
21936 comdat_key);
21937 ASM_GENERATE_INTERNAL_LABEL (label,
21938 DEBUG_MACRO_SECTION_LABEL,
21939 ref->lineno);
21940 ASM_OUTPUT_LABEL (asm_out_file, label);
21941 ref->code = 0;
21942 ref->info = NULL;
21943 dw2_asm_output_data (2, 4, "DWARF macro version number");
21944 if (DWARF_OFFSET_SIZE == 8)
21945 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21946 else
21947 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21949 break;
21950 case DW_MACINFO_define:
21951 case DW_MACINFO_undef:
21952 output_macinfo_op (ref);
21953 ref->code = 0;
21954 ref->info = NULL;
21955 break;
21956 default:
21957 gcc_unreachable ();
21961 /* Set up for Dwarf output at the start of compilation. */
21963 static void
21964 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21966 /* Allocate the file_table. */
21967 file_table = htab_create_ggc (50, file_table_hash,
21968 file_table_eq, NULL);
21970 /* Allocate the decl_die_table. */
21971 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21972 decl_die_table_eq, NULL);
21974 /* Allocate the decl_loc_table. */
21975 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21976 decl_loc_table_eq, NULL);
21978 /* Allocate the cached_dw_loc_list_table. */
21979 cached_dw_loc_list_table
21980 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21981 cached_dw_loc_list_table_eq, NULL);
21983 /* Allocate the initial hunk of the decl_scope_table. */
21984 vec_alloc (decl_scope_table, 256);
21986 /* Allocate the initial hunk of the abbrev_die_table. */
21987 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21988 (ABBREV_DIE_TABLE_INCREMENT);
21989 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21990 /* Zero-th entry is allocated, but unused. */
21991 abbrev_die_table_in_use = 1;
21993 /* Allocate the pubtypes and pubnames vectors. */
21994 vec_alloc (pubname_table, 32);
21995 vec_alloc (pubtype_table, 32);
21997 vec_alloc (incomplete_types, 64);
21999 vec_alloc (used_rtx_array, 32);
22001 if (!dwarf_split_debug_info)
22003 debug_info_section = get_section (DEBUG_INFO_SECTION,
22004 SECTION_DEBUG, NULL);
22005 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22006 SECTION_DEBUG, NULL);
22007 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22008 SECTION_DEBUG, NULL);
22010 else
22012 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22013 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22014 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22015 SECTION_DEBUG | SECTION_EXCLUDE,
22016 NULL);
22017 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22018 SECTION_DEBUG, NULL);
22019 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22020 SECTION_DEBUG, NULL);
22021 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22022 SECTION_DEBUG, NULL);
22023 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22024 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22026 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22027 the main .o, but the skeleton_line goes into the split off dwo. */
22028 debug_skeleton_line_section
22029 = get_section (DEBUG_DWO_LINE_SECTION,
22030 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22031 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22032 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22033 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22034 SECTION_DEBUG | SECTION_EXCLUDE,
22035 NULL);
22036 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22037 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22038 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22039 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22040 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22041 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22043 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22044 SECTION_DEBUG, NULL);
22045 debug_macinfo_section = get_section (dwarf_strict
22046 ? DEBUG_MACINFO_SECTION
22047 : DEBUG_MACRO_SECTION,
22048 DEBUG_MACRO_SECTION_FLAGS, NULL);
22049 debug_line_section = get_section (DEBUG_LINE_SECTION,
22050 SECTION_DEBUG, NULL);
22051 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22052 SECTION_DEBUG, NULL);
22053 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22054 SECTION_DEBUG, NULL);
22055 debug_str_section = get_section (DEBUG_STR_SECTION,
22056 DEBUG_STR_SECTION_FLAGS, NULL);
22057 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22058 SECTION_DEBUG, NULL);
22059 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22060 SECTION_DEBUG, NULL);
22062 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22063 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22064 DEBUG_ABBREV_SECTION_LABEL, 0);
22065 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22066 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22067 COLD_TEXT_SECTION_LABEL, 0);
22068 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22070 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22071 DEBUG_INFO_SECTION_LABEL, 0);
22072 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22073 DEBUG_LINE_SECTION_LABEL, 0);
22074 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22075 DEBUG_RANGES_SECTION_LABEL, 0);
22076 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22077 DEBUG_ADDR_SECTION_LABEL, 0);
22078 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22079 dwarf_strict
22080 ? DEBUG_MACINFO_SECTION_LABEL
22081 : DEBUG_MACRO_SECTION_LABEL, 0);
22082 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22084 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22085 vec_alloc (macinfo_table, 64);
22087 switch_to_section (text_section);
22088 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22090 /* Make sure the line number table for .text always exists. */
22091 text_section_line_info = new_line_info_table ();
22092 text_section_line_info->end_label = text_end_label;
22095 /* Called before compile () starts outputtting functions, variables
22096 and toplevel asms into assembly. */
22098 static void
22099 dwarf2out_assembly_start (void)
22101 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22102 && dwarf2out_do_cfi_asm ()
22103 && (!(flag_unwind_tables || flag_exceptions)
22104 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22105 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22108 /* A helper function for dwarf2out_finish called through
22109 htab_traverse. Assign a string its index. All strings must be
22110 collected into the table by the time index_string is called,
22111 because the indexing code relies on htab_traverse to traverse nodes
22112 in the same order for each run. */
22114 static int
22115 index_string (void **h, void *v)
22117 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22118 unsigned int *index = (unsigned int *) v;
22120 find_string_form (node);
22121 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22123 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22124 node->index = *index;
22125 *index += 1;
22127 return 1;
22130 /* A helper function for output_indirect_strings called through
22131 htab_traverse. Output the offset to a string and update the
22132 current offset. */
22134 static int
22135 output_index_string_offset (void **h, void *v)
22137 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22138 unsigned int *offset = (unsigned int *) v;
22140 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22142 /* Assert that this node has been assigned an index. */
22143 gcc_assert (node->index != NO_INDEX_ASSIGNED
22144 && node->index != NOT_INDEXED);
22145 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22146 "indexed string 0x%x: %s", node->index, node->str);
22147 *offset += strlen (node->str) + 1;
22149 return 1;
22152 /* A helper function for dwarf2out_finish called through
22153 htab_traverse. Output the indexed string. */
22155 static int
22156 output_index_string (void **h, void *v)
22158 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22159 unsigned int *cur_idx = (unsigned int *) v;
22161 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22163 /* Assert that the strings are output in the same order as their
22164 indexes were assigned. */
22165 gcc_assert (*cur_idx == node->index);
22166 assemble_string (node->str, strlen (node->str) + 1);
22167 *cur_idx += 1;
22169 return 1;
22172 /* A helper function for dwarf2out_finish called through
22173 htab_traverse. Emit one queued .debug_str string. */
22175 static int
22176 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22178 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22180 node->form = find_string_form (node);
22181 if (node->form == DW_FORM_strp && node->refcount > 0)
22183 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22184 assemble_string (node->str, strlen (node->str) + 1);
22187 return 1;
22190 /* Output the indexed string table. */
22192 static void
22193 output_indirect_strings (void)
22195 switch_to_section (debug_str_section);
22196 if (!dwarf_split_debug_info)
22197 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22198 else
22200 unsigned int offset = 0;
22201 unsigned int cur_idx = 0;
22203 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22205 switch_to_section (debug_str_offsets_section);
22206 htab_traverse_noresize (debug_str_hash,
22207 output_index_string_offset,
22208 &offset);
22209 switch_to_section (debug_str_dwo_section);
22210 htab_traverse_noresize (debug_str_hash,
22211 output_index_string,
22212 &cur_idx);
22216 /* Callback for htab_traverse to assign an index to an entry in the
22217 table, and to write that entry to the .debug_addr section. */
22219 static int
22220 output_addr_table_entry (void **slot, void *data)
22222 addr_table_entry *entry = (addr_table_entry *) *slot;
22223 unsigned int *cur_index = (unsigned int *)data;
22225 if (entry->refcount == 0)
22227 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22228 || entry->index == NOT_INDEXED);
22229 return 1;
22232 gcc_assert (entry->index == *cur_index);
22233 (*cur_index)++;
22235 switch (entry->kind)
22237 case ate_kind_rtx:
22238 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22239 "0x%x", entry->index);
22240 break;
22241 case ate_kind_rtx_dtprel:
22242 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22243 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22244 DWARF2_ADDR_SIZE,
22245 entry->addr.rtl);
22246 fputc ('\n', asm_out_file);
22247 break;
22248 case ate_kind_label:
22249 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22250 "0x%x", entry->index);
22251 break;
22252 default:
22253 gcc_unreachable ();
22255 return 1;
22258 /* Produce the .debug_addr section. */
22260 static void
22261 output_addr_table (void)
22263 unsigned int index = 0;
22264 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22265 return;
22267 switch_to_section (debug_addr_section);
22268 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22271 #if ENABLE_ASSERT_CHECKING
22272 /* Verify that all marks are clear. */
22274 static void
22275 verify_marks_clear (dw_die_ref die)
22277 dw_die_ref c;
22279 gcc_assert (! die->die_mark);
22280 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22282 #endif /* ENABLE_ASSERT_CHECKING */
22284 /* Clear the marks for a die and its children.
22285 Be cool if the mark isn't set. */
22287 static void
22288 prune_unmark_dies (dw_die_ref die)
22290 dw_die_ref c;
22292 if (die->die_mark)
22293 die->die_mark = 0;
22294 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22297 /* Given DIE that we're marking as used, find any other dies
22298 it references as attributes and mark them as used. */
22300 static void
22301 prune_unused_types_walk_attribs (dw_die_ref die)
22303 dw_attr_ref a;
22304 unsigned ix;
22306 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22308 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22310 /* A reference to another DIE.
22311 Make sure that it will get emitted.
22312 If it was broken out into a comdat group, don't follow it. */
22313 if (! AT_ref (a)->comdat_type_p
22314 || a->dw_attr == DW_AT_specification)
22315 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22317 /* Set the string's refcount to 0 so that prune_unused_types_mark
22318 accounts properly for it. */
22319 if (AT_class (a) == dw_val_class_str)
22320 a->dw_attr_val.v.val_str->refcount = 0;
22324 /* Mark the generic parameters and arguments children DIEs of DIE. */
22326 static void
22327 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22329 dw_die_ref c;
22331 if (die == NULL || die->die_child == NULL)
22332 return;
22333 c = die->die_child;
22336 if (is_template_parameter (c))
22337 prune_unused_types_mark (c, 1);
22338 c = c->die_sib;
22339 } while (c && c != die->die_child);
22342 /* Mark DIE as being used. If DOKIDS is true, then walk down
22343 to DIE's children. */
22345 static void
22346 prune_unused_types_mark (dw_die_ref die, int dokids)
22348 dw_die_ref c;
22350 if (die->die_mark == 0)
22352 /* We haven't done this node yet. Mark it as used. */
22353 die->die_mark = 1;
22354 /* If this is the DIE of a generic type instantiation,
22355 mark the children DIEs that describe its generic parms and
22356 args. */
22357 prune_unused_types_mark_generic_parms_dies (die);
22359 /* We also have to mark its parents as used.
22360 (But we don't want to mark our parent's kids due to this,
22361 unless it is a class.) */
22362 if (die->die_parent)
22363 prune_unused_types_mark (die->die_parent,
22364 class_scope_p (die->die_parent));
22366 /* Mark any referenced nodes. */
22367 prune_unused_types_walk_attribs (die);
22369 /* If this node is a specification,
22370 also mark the definition, if it exists. */
22371 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22372 prune_unused_types_mark (die->die_definition, 1);
22375 if (dokids && die->die_mark != 2)
22377 /* We need to walk the children, but haven't done so yet.
22378 Remember that we've walked the kids. */
22379 die->die_mark = 2;
22381 /* If this is an array type, we need to make sure our
22382 kids get marked, even if they're types. If we're
22383 breaking out types into comdat sections, do this
22384 for all type definitions. */
22385 if (die->die_tag == DW_TAG_array_type
22386 || (use_debug_types
22387 && is_type_die (die) && ! is_declaration_die (die)))
22388 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22389 else
22390 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22394 /* For local classes, look if any static member functions were emitted
22395 and if so, mark them. */
22397 static void
22398 prune_unused_types_walk_local_classes (dw_die_ref die)
22400 dw_die_ref c;
22402 if (die->die_mark == 2)
22403 return;
22405 switch (die->die_tag)
22407 case DW_TAG_structure_type:
22408 case DW_TAG_union_type:
22409 case DW_TAG_class_type:
22410 break;
22412 case DW_TAG_subprogram:
22413 if (!get_AT_flag (die, DW_AT_declaration)
22414 || die->die_definition != NULL)
22415 prune_unused_types_mark (die, 1);
22416 return;
22418 default:
22419 return;
22422 /* Mark children. */
22423 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22426 /* Walk the tree DIE and mark types that we actually use. */
22428 static void
22429 prune_unused_types_walk (dw_die_ref die)
22431 dw_die_ref c;
22433 /* Don't do anything if this node is already marked and
22434 children have been marked as well. */
22435 if (die->die_mark == 2)
22436 return;
22438 switch (die->die_tag)
22440 case DW_TAG_structure_type:
22441 case DW_TAG_union_type:
22442 case DW_TAG_class_type:
22443 if (die->die_perennial_p)
22444 break;
22446 for (c = die->die_parent; c; c = c->die_parent)
22447 if (c->die_tag == DW_TAG_subprogram)
22448 break;
22450 /* Finding used static member functions inside of classes
22451 is needed just for local classes, because for other classes
22452 static member function DIEs with DW_AT_specification
22453 are emitted outside of the DW_TAG_*_type. If we ever change
22454 it, we'd need to call this even for non-local classes. */
22455 if (c)
22456 prune_unused_types_walk_local_classes (die);
22458 /* It's a type node --- don't mark it. */
22459 return;
22461 case DW_TAG_const_type:
22462 case DW_TAG_packed_type:
22463 case DW_TAG_pointer_type:
22464 case DW_TAG_reference_type:
22465 case DW_TAG_rvalue_reference_type:
22466 case DW_TAG_volatile_type:
22467 case DW_TAG_typedef:
22468 case DW_TAG_array_type:
22469 case DW_TAG_interface_type:
22470 case DW_TAG_friend:
22471 case DW_TAG_variant_part:
22472 case DW_TAG_enumeration_type:
22473 case DW_TAG_subroutine_type:
22474 case DW_TAG_string_type:
22475 case DW_TAG_set_type:
22476 case DW_TAG_subrange_type:
22477 case DW_TAG_ptr_to_member_type:
22478 case DW_TAG_file_type:
22479 if (die->die_perennial_p)
22480 break;
22482 /* It's a type node --- don't mark it. */
22483 return;
22485 default:
22486 /* Mark everything else. */
22487 break;
22490 if (die->die_mark == 0)
22492 die->die_mark = 1;
22494 /* Now, mark any dies referenced from here. */
22495 prune_unused_types_walk_attribs (die);
22498 die->die_mark = 2;
22500 /* Mark children. */
22501 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22504 /* Increment the string counts on strings referred to from DIE's
22505 attributes. */
22507 static void
22508 prune_unused_types_update_strings (dw_die_ref die)
22510 dw_attr_ref a;
22511 unsigned ix;
22513 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22514 if (AT_class (a) == dw_val_class_str)
22516 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22517 s->refcount++;
22518 /* Avoid unnecessarily putting strings that are used less than
22519 twice in the hash table. */
22520 if (s->refcount
22521 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22523 void ** slot;
22524 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22525 htab_hash_string (s->str),
22526 INSERT);
22527 gcc_assert (*slot == NULL);
22528 *slot = s;
22533 /* Remove from the tree DIE any dies that aren't marked. */
22535 static void
22536 prune_unused_types_prune (dw_die_ref die)
22538 dw_die_ref c;
22540 gcc_assert (die->die_mark);
22541 prune_unused_types_update_strings (die);
22543 if (! die->die_child)
22544 return;
22546 c = die->die_child;
22547 do {
22548 dw_die_ref prev = c;
22549 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22550 if (c == die->die_child)
22552 /* No marked children between 'prev' and the end of the list. */
22553 if (prev == c)
22554 /* No marked children at all. */
22555 die->die_child = NULL;
22556 else
22558 prev->die_sib = c->die_sib;
22559 die->die_child = prev;
22561 return;
22564 if (c != prev->die_sib)
22565 prev->die_sib = c;
22566 prune_unused_types_prune (c);
22567 } while (c != die->die_child);
22570 /* Remove dies representing declarations that we never use. */
22572 static void
22573 prune_unused_types (void)
22575 unsigned int i;
22576 limbo_die_node *node;
22577 comdat_type_node *ctnode;
22578 pubname_ref pub;
22579 dw_die_ref base_type;
22581 #if ENABLE_ASSERT_CHECKING
22582 /* All the marks should already be clear. */
22583 verify_marks_clear (comp_unit_die ());
22584 for (node = limbo_die_list; node; node = node->next)
22585 verify_marks_clear (node->die);
22586 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22587 verify_marks_clear (ctnode->root_die);
22588 #endif /* ENABLE_ASSERT_CHECKING */
22590 /* Mark types that are used in global variables. */
22591 premark_types_used_by_global_vars ();
22593 /* Set the mark on nodes that are actually used. */
22594 prune_unused_types_walk (comp_unit_die ());
22595 for (node = limbo_die_list; node; node = node->next)
22596 prune_unused_types_walk (node->die);
22597 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22599 prune_unused_types_walk (ctnode->root_die);
22600 prune_unused_types_mark (ctnode->type_die, 1);
22603 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22604 are unusual in that they are pubnames that are the children of pubtypes.
22605 They should only be marked via their parent DW_TAG_enumeration_type die,
22606 not as roots in themselves. */
22607 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22608 if (pub->die->die_tag != DW_TAG_enumerator)
22609 prune_unused_types_mark (pub->die, 1);
22610 for (i = 0; base_types.iterate (i, &base_type); i++)
22611 prune_unused_types_mark (base_type, 1);
22613 if (debug_str_hash)
22614 htab_empty (debug_str_hash);
22615 if (skeleton_debug_str_hash)
22616 htab_empty (skeleton_debug_str_hash);
22617 prune_unused_types_prune (comp_unit_die ());
22618 for (node = limbo_die_list; node; node = node->next)
22619 prune_unused_types_prune (node->die);
22620 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22621 prune_unused_types_prune (ctnode->root_die);
22623 /* Leave the marks clear. */
22624 prune_unmark_dies (comp_unit_die ());
22625 for (node = limbo_die_list; node; node = node->next)
22626 prune_unmark_dies (node->die);
22627 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22628 prune_unmark_dies (ctnode->root_die);
22631 /* Set the parameter to true if there are any relative pathnames in
22632 the file table. */
22633 static int
22634 file_table_relative_p (void ** slot, void *param)
22636 bool *p = (bool *) param;
22637 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22638 if (!IS_ABSOLUTE_PATH (d->filename))
22640 *p = true;
22641 return 0;
22643 return 1;
22646 /* Helpers to manipulate hash table of comdat type units. */
22648 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22650 typedef comdat_type_node value_type;
22651 typedef comdat_type_node compare_type;
22652 static inline hashval_t hash (const value_type *);
22653 static inline bool equal (const value_type *, const compare_type *);
22656 inline hashval_t
22657 comdat_type_hasher::hash (const value_type *type_node)
22659 hashval_t h;
22660 memcpy (&h, type_node->signature, sizeof (h));
22661 return h;
22664 inline bool
22665 comdat_type_hasher::equal (const value_type *type_node_1,
22666 const compare_type *type_node_2)
22668 return (! memcmp (type_node_1->signature, type_node_2->signature,
22669 DWARF_TYPE_SIGNATURE_SIZE));
22672 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22673 to the location it would have been added, should we know its
22674 DECL_ASSEMBLER_NAME when we added other attributes. This will
22675 probably improve compactness of debug info, removing equivalent
22676 abbrevs, and hide any differences caused by deferring the
22677 computation of the assembler name, triggered by e.g. PCH. */
22679 static inline void
22680 move_linkage_attr (dw_die_ref die)
22682 unsigned ix = vec_safe_length (die->die_attr);
22683 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22685 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22686 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22688 while (--ix > 0)
22690 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22692 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22693 break;
22696 if (ix != vec_safe_length (die->die_attr) - 1)
22698 die->die_attr->pop ();
22699 die->die_attr->quick_insert (ix, linkage);
22703 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22704 referenced from typed stack ops and count how often they are used. */
22706 static void
22707 mark_base_types (dw_loc_descr_ref loc)
22709 dw_die_ref base_type = NULL;
22711 for (; loc; loc = loc->dw_loc_next)
22713 switch (loc->dw_loc_opc)
22715 case DW_OP_GNU_regval_type:
22716 case DW_OP_GNU_deref_type:
22717 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22718 break;
22719 case DW_OP_GNU_convert:
22720 case DW_OP_GNU_reinterpret:
22721 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22722 continue;
22723 /* FALLTHRU */
22724 case DW_OP_GNU_const_type:
22725 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22726 break;
22727 case DW_OP_GNU_entry_value:
22728 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22729 continue;
22730 default:
22731 continue;
22733 gcc_assert (base_type->die_parent == comp_unit_die ());
22734 if (base_type->die_mark)
22735 base_type->die_mark++;
22736 else
22738 base_types.safe_push (base_type);
22739 base_type->die_mark = 1;
22744 /* Comparison function for sorting marked base types. */
22746 static int
22747 base_type_cmp (const void *x, const void *y)
22749 dw_die_ref dx = *(const dw_die_ref *) x;
22750 dw_die_ref dy = *(const dw_die_ref *) y;
22751 unsigned int byte_size1, byte_size2;
22752 unsigned int encoding1, encoding2;
22753 if (dx->die_mark > dy->die_mark)
22754 return -1;
22755 if (dx->die_mark < dy->die_mark)
22756 return 1;
22757 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22758 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22759 if (byte_size1 < byte_size2)
22760 return 1;
22761 if (byte_size1 > byte_size2)
22762 return -1;
22763 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22764 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22765 if (encoding1 < encoding2)
22766 return 1;
22767 if (encoding1 > encoding2)
22768 return -1;
22769 return 0;
22772 /* Move base types marked by mark_base_types as early as possible
22773 in the CU, sorted by decreasing usage count both to make the
22774 uleb128 references as small as possible and to make sure they
22775 will have die_offset already computed by calc_die_sizes when
22776 sizes of typed stack loc ops is computed. */
22778 static void
22779 move_marked_base_types (void)
22781 unsigned int i;
22782 dw_die_ref base_type, die, c;
22784 if (base_types.is_empty ())
22785 return;
22787 /* Sort by decreasing usage count, they will be added again in that
22788 order later on. */
22789 base_types.qsort (base_type_cmp);
22790 die = comp_unit_die ();
22791 c = die->die_child;
22794 dw_die_ref prev = c;
22795 c = c->die_sib;
22796 while (c->die_mark)
22798 remove_child_with_prev (c, prev);
22799 /* As base types got marked, there must be at least
22800 one node other than DW_TAG_base_type. */
22801 gcc_assert (c != c->die_sib);
22802 c = c->die_sib;
22805 while (c != die->die_child);
22806 gcc_assert (die->die_child);
22807 c = die->die_child;
22808 for (i = 0; base_types.iterate (i, &base_type); i++)
22810 base_type->die_mark = 0;
22811 base_type->die_sib = c->die_sib;
22812 c->die_sib = base_type;
22813 c = base_type;
22817 /* Helper function for resolve_addr, attempt to resolve
22818 one CONST_STRING, return non-zero if not successful. Similarly verify that
22819 SYMBOL_REFs refer to variables emitted in the current CU. */
22821 static int
22822 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22824 rtx rtl = *addr;
22826 if (GET_CODE (rtl) == CONST_STRING)
22828 size_t len = strlen (XSTR (rtl, 0)) + 1;
22829 tree t = build_string (len, XSTR (rtl, 0));
22830 tree tlen = size_int (len - 1);
22831 TREE_TYPE (t)
22832 = build_array_type (char_type_node, build_index_type (tlen));
22833 rtl = lookup_constant_def (t);
22834 if (!rtl || !MEM_P (rtl))
22835 return 1;
22836 rtl = XEXP (rtl, 0);
22837 if (GET_CODE (rtl) == SYMBOL_REF
22838 && SYMBOL_REF_DECL (rtl)
22839 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22840 return 1;
22841 vec_safe_push (used_rtx_array, rtl);
22842 *addr = rtl;
22843 return 0;
22846 if (GET_CODE (rtl) == SYMBOL_REF
22847 && SYMBOL_REF_DECL (rtl))
22849 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22851 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22852 return 1;
22854 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22855 return 1;
22858 if (GET_CODE (rtl) == CONST
22859 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22860 return 1;
22862 return 0;
22865 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22866 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22867 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22869 static rtx
22870 string_cst_pool_decl (tree t)
22872 rtx rtl = output_constant_def (t, 1);
22873 unsigned char *array;
22874 dw_loc_descr_ref l;
22875 tree decl;
22876 size_t len;
22877 dw_die_ref ref;
22879 if (!rtl || !MEM_P (rtl))
22880 return NULL_RTX;
22881 rtl = XEXP (rtl, 0);
22882 if (GET_CODE (rtl) != SYMBOL_REF
22883 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22884 return NULL_RTX;
22886 decl = SYMBOL_REF_DECL (rtl);
22887 if (!lookup_decl_die (decl))
22889 len = TREE_STRING_LENGTH (t);
22890 vec_safe_push (used_rtx_array, rtl);
22891 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22892 array = (unsigned char *) ggc_alloc_atomic (len);
22893 memcpy (array, TREE_STRING_POINTER (t), len);
22894 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22895 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22896 l->dw_loc_oprnd2.v.val_vec.length = len;
22897 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22898 l->dw_loc_oprnd2.v.val_vec.array = array;
22899 add_AT_loc (ref, DW_AT_location, l);
22900 equate_decl_number_to_die (decl, ref);
22902 return rtl;
22905 /* Helper function of resolve_addr_in_expr. LOC is
22906 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22907 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22908 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22909 with DW_OP_GNU_implicit_pointer if possible
22910 and return true, if unsuccessful, return false. */
22912 static bool
22913 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22915 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22916 HOST_WIDE_INT offset = 0;
22917 dw_die_ref ref = NULL;
22918 tree decl;
22920 if (GET_CODE (rtl) == CONST
22921 && GET_CODE (XEXP (rtl, 0)) == PLUS
22922 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22924 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22925 rtl = XEXP (XEXP (rtl, 0), 0);
22927 if (GET_CODE (rtl) == CONST_STRING)
22929 size_t len = strlen (XSTR (rtl, 0)) + 1;
22930 tree t = build_string (len, XSTR (rtl, 0));
22931 tree tlen = size_int (len - 1);
22933 TREE_TYPE (t)
22934 = build_array_type (char_type_node, build_index_type (tlen));
22935 rtl = string_cst_pool_decl (t);
22936 if (!rtl)
22937 return false;
22939 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22941 decl = SYMBOL_REF_DECL (rtl);
22942 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22944 ref = lookup_decl_die (decl);
22945 if (ref && (get_AT (ref, DW_AT_location)
22946 || get_AT (ref, DW_AT_const_value)))
22948 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22949 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22950 loc->dw_loc_oprnd1.val_entry = NULL;
22951 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22952 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22953 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22954 loc->dw_loc_oprnd2.v.val_int = offset;
22955 return true;
22959 return false;
22962 /* Helper function for resolve_addr, handle one location
22963 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22964 the location list couldn't be resolved. */
22966 static bool
22967 resolve_addr_in_expr (dw_loc_descr_ref loc)
22969 dw_loc_descr_ref keep = NULL;
22970 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22971 switch (loc->dw_loc_opc)
22973 case DW_OP_addr:
22974 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22976 if ((prev == NULL
22977 || prev->dw_loc_opc == DW_OP_piece
22978 || prev->dw_loc_opc == DW_OP_bit_piece)
22979 && loc->dw_loc_next
22980 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22981 && !dwarf_strict
22982 && optimize_one_addr_into_implicit_ptr (loc))
22983 break;
22984 return false;
22986 break;
22987 case DW_OP_GNU_addr_index:
22988 case DW_OP_GNU_const_index:
22989 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22990 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22991 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22992 NULL))
22993 return false;
22994 break;
22995 case DW_OP_const4u:
22996 case DW_OP_const8u:
22997 if (loc->dtprel
22998 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22999 return false;
23000 break;
23001 case DW_OP_plus_uconst:
23002 if (size_of_loc_descr (loc)
23003 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23005 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23007 dw_loc_descr_ref repl
23008 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23009 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23010 add_loc_descr (&repl, loc->dw_loc_next);
23011 *loc = *repl;
23013 break;
23014 case DW_OP_implicit_value:
23015 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23016 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23017 return false;
23018 break;
23019 case DW_OP_GNU_implicit_pointer:
23020 case DW_OP_GNU_parameter_ref:
23021 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23023 dw_die_ref ref
23024 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23025 if (ref == NULL)
23026 return false;
23027 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23028 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23029 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23031 break;
23032 case DW_OP_GNU_const_type:
23033 case DW_OP_GNU_regval_type:
23034 case DW_OP_GNU_deref_type:
23035 case DW_OP_GNU_convert:
23036 case DW_OP_GNU_reinterpret:
23037 while (loc->dw_loc_next
23038 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23040 dw_die_ref base1, base2;
23041 unsigned enc1, enc2, size1, size2;
23042 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23043 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23044 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23045 else if (loc->dw_loc_oprnd1.val_class
23046 == dw_val_class_unsigned_const)
23047 break;
23048 else
23049 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23050 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23051 == dw_val_class_unsigned_const)
23052 break;
23053 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23054 gcc_assert (base1->die_tag == DW_TAG_base_type
23055 && base2->die_tag == DW_TAG_base_type);
23056 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23057 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23058 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23059 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23060 if (size1 == size2
23061 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23062 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23063 && loc != keep)
23064 || enc1 == enc2))
23066 /* Optimize away next DW_OP_GNU_convert after
23067 adjusting LOC's base type die reference. */
23068 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23069 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23070 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23071 else
23072 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23073 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23074 continue;
23076 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23077 point typed stack entry. */
23078 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23079 keep = loc->dw_loc_next;
23080 break;
23082 break;
23083 default:
23084 break;
23086 return true;
23089 /* Helper function of resolve_addr. DIE had DW_AT_location of
23090 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23091 and DW_OP_addr couldn't be resolved. resolve_addr has already
23092 removed the DW_AT_location attribute. This function attempts to
23093 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23094 to it or DW_AT_const_value attribute, if possible. */
23096 static void
23097 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23099 if (TREE_CODE (decl) != VAR_DECL
23100 || lookup_decl_die (decl) != die
23101 || DECL_EXTERNAL (decl)
23102 || !TREE_STATIC (decl)
23103 || DECL_INITIAL (decl) == NULL_TREE
23104 || DECL_P (DECL_INITIAL (decl))
23105 || get_AT (die, DW_AT_const_value))
23106 return;
23108 tree init = DECL_INITIAL (decl);
23109 HOST_WIDE_INT offset = 0;
23110 /* For variables that have been optimized away and thus
23111 don't have a memory location, see if we can emit
23112 DW_AT_const_value instead. */
23113 if (tree_add_const_value_attribute (die, init))
23114 return;
23115 if (dwarf_strict)
23116 return;
23117 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23118 and ADDR_EXPR refers to a decl that has DW_AT_location or
23119 DW_AT_const_value (but isn't addressable, otherwise
23120 resolving the original DW_OP_addr wouldn't fail), see if
23121 we can add DW_OP_GNU_implicit_pointer. */
23122 STRIP_NOPS (init);
23123 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23124 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23126 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23127 init = TREE_OPERAND (init, 0);
23128 STRIP_NOPS (init);
23130 if (TREE_CODE (init) != ADDR_EXPR)
23131 return;
23132 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23133 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23134 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23135 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23136 && TREE_OPERAND (init, 0) != decl))
23138 dw_die_ref ref;
23139 dw_loc_descr_ref l;
23141 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23143 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23144 if (!rtl)
23145 return;
23146 decl = SYMBOL_REF_DECL (rtl);
23148 else
23149 decl = TREE_OPERAND (init, 0);
23150 ref = lookup_decl_die (decl);
23151 if (ref == NULL
23152 || (!get_AT (ref, DW_AT_location)
23153 && !get_AT (ref, DW_AT_const_value)))
23154 return;
23155 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23156 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23157 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23158 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23159 add_AT_loc (die, DW_AT_location, l);
23163 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23164 an address in .rodata section if the string literal is emitted there,
23165 or remove the containing location list or replace DW_AT_const_value
23166 with DW_AT_location and empty location expression, if it isn't found
23167 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23168 to something that has been emitted in the current CU. */
23170 static void
23171 resolve_addr (dw_die_ref die)
23173 dw_die_ref c;
23174 dw_attr_ref a;
23175 dw_loc_list_ref *curr, *start, loc;
23176 unsigned ix;
23178 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23179 switch (AT_class (a))
23181 case dw_val_class_loc_list:
23182 start = curr = AT_loc_list_ptr (a);
23183 loc = *curr;
23184 gcc_assert (loc);
23185 /* The same list can be referenced more than once. See if we have
23186 already recorded the result from a previous pass. */
23187 if (loc->replaced)
23188 *curr = loc->dw_loc_next;
23189 else if (!loc->resolved_addr)
23191 /* As things stand, we do not expect or allow one die to
23192 reference a suffix of another die's location list chain.
23193 References must be identical or completely separate.
23194 There is therefore no need to cache the result of this
23195 pass on any list other than the first; doing so
23196 would lead to unnecessary writes. */
23197 while (*curr)
23199 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23200 if (!resolve_addr_in_expr ((*curr)->expr))
23202 dw_loc_list_ref next = (*curr)->dw_loc_next;
23203 dw_loc_descr_ref l = (*curr)->expr;
23205 if (next && (*curr)->ll_symbol)
23207 gcc_assert (!next->ll_symbol);
23208 next->ll_symbol = (*curr)->ll_symbol;
23210 if (dwarf_split_debug_info)
23211 remove_loc_list_addr_table_entries (l);
23212 *curr = next;
23214 else
23216 mark_base_types ((*curr)->expr);
23217 curr = &(*curr)->dw_loc_next;
23220 if (loc == *start)
23221 loc->resolved_addr = 1;
23222 else
23224 loc->replaced = 1;
23225 loc->dw_loc_next = *start;
23228 if (!*start)
23230 remove_AT (die, a->dw_attr);
23231 ix--;
23233 break;
23234 case dw_val_class_loc:
23236 dw_loc_descr_ref l = AT_loc (a);
23237 /* For -gdwarf-2 don't attempt to optimize
23238 DW_AT_data_member_location containing
23239 DW_OP_plus_uconst - older consumers might
23240 rely on it being that op instead of a more complex,
23241 but shorter, location description. */
23242 if ((dwarf_version > 2
23243 || a->dw_attr != DW_AT_data_member_location
23244 || l == NULL
23245 || l->dw_loc_opc != DW_OP_plus_uconst
23246 || l->dw_loc_next != NULL)
23247 && !resolve_addr_in_expr (l))
23249 if (dwarf_split_debug_info)
23250 remove_loc_list_addr_table_entries (l);
23251 if (l != NULL
23252 && l->dw_loc_next == NULL
23253 && l->dw_loc_opc == DW_OP_addr
23254 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23255 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23256 && a->dw_attr == DW_AT_location)
23258 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23259 remove_AT (die, a->dw_attr);
23260 ix--;
23261 optimize_location_into_implicit_ptr (die, decl);
23262 break;
23264 remove_AT (die, a->dw_attr);
23265 ix--;
23267 else
23268 mark_base_types (l);
23270 break;
23271 case dw_val_class_addr:
23272 if (a->dw_attr == DW_AT_const_value
23273 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23275 if (AT_index (a) != NOT_INDEXED)
23276 remove_addr_table_entry (a->dw_attr_val.val_entry);
23277 remove_AT (die, a->dw_attr);
23278 ix--;
23280 if (die->die_tag == DW_TAG_GNU_call_site
23281 && a->dw_attr == DW_AT_abstract_origin)
23283 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23284 dw_die_ref tdie = lookup_decl_die (tdecl);
23285 if (tdie == NULL
23286 && DECL_EXTERNAL (tdecl)
23287 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23289 force_decl_die (tdecl);
23290 tdie = lookup_decl_die (tdecl);
23292 if (tdie)
23294 a->dw_attr_val.val_class = dw_val_class_die_ref;
23295 a->dw_attr_val.v.val_die_ref.die = tdie;
23296 a->dw_attr_val.v.val_die_ref.external = 0;
23298 else
23300 if (AT_index (a) != NOT_INDEXED)
23301 remove_addr_table_entry (a->dw_attr_val.val_entry);
23302 remove_AT (die, a->dw_attr);
23303 ix--;
23306 break;
23307 default:
23308 break;
23311 FOR_EACH_CHILD (die, c, resolve_addr (c));
23314 /* Helper routines for optimize_location_lists.
23315 This pass tries to share identical local lists in .debug_loc
23316 section. */
23318 /* Iteratively hash operands of LOC opcode. */
23320 static hashval_t
23321 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23323 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23324 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23326 switch (loc->dw_loc_opc)
23328 case DW_OP_const4u:
23329 case DW_OP_const8u:
23330 if (loc->dtprel)
23331 goto hash_addr;
23332 /* FALLTHRU */
23333 case DW_OP_const1u:
23334 case DW_OP_const1s:
23335 case DW_OP_const2u:
23336 case DW_OP_const2s:
23337 case DW_OP_const4s:
23338 case DW_OP_const8s:
23339 case DW_OP_constu:
23340 case DW_OP_consts:
23341 case DW_OP_pick:
23342 case DW_OP_plus_uconst:
23343 case DW_OP_breg0:
23344 case DW_OP_breg1:
23345 case DW_OP_breg2:
23346 case DW_OP_breg3:
23347 case DW_OP_breg4:
23348 case DW_OP_breg5:
23349 case DW_OP_breg6:
23350 case DW_OP_breg7:
23351 case DW_OP_breg8:
23352 case DW_OP_breg9:
23353 case DW_OP_breg10:
23354 case DW_OP_breg11:
23355 case DW_OP_breg12:
23356 case DW_OP_breg13:
23357 case DW_OP_breg14:
23358 case DW_OP_breg15:
23359 case DW_OP_breg16:
23360 case DW_OP_breg17:
23361 case DW_OP_breg18:
23362 case DW_OP_breg19:
23363 case DW_OP_breg20:
23364 case DW_OP_breg21:
23365 case DW_OP_breg22:
23366 case DW_OP_breg23:
23367 case DW_OP_breg24:
23368 case DW_OP_breg25:
23369 case DW_OP_breg26:
23370 case DW_OP_breg27:
23371 case DW_OP_breg28:
23372 case DW_OP_breg29:
23373 case DW_OP_breg30:
23374 case DW_OP_breg31:
23375 case DW_OP_regx:
23376 case DW_OP_fbreg:
23377 case DW_OP_piece:
23378 case DW_OP_deref_size:
23379 case DW_OP_xderef_size:
23380 hash = iterative_hash_object (val1->v.val_int, hash);
23381 break;
23382 case DW_OP_skip:
23383 case DW_OP_bra:
23385 int offset;
23387 gcc_assert (val1->val_class == dw_val_class_loc);
23388 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23389 hash = iterative_hash_object (offset, hash);
23391 break;
23392 case DW_OP_implicit_value:
23393 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23394 switch (val2->val_class)
23396 case dw_val_class_const:
23397 hash = iterative_hash_object (val2->v.val_int, hash);
23398 break;
23399 case dw_val_class_vec:
23401 unsigned int elt_size = val2->v.val_vec.elt_size;
23402 unsigned int len = val2->v.val_vec.length;
23404 hash = iterative_hash_object (elt_size, hash);
23405 hash = iterative_hash_object (len, hash);
23406 hash = iterative_hash (val2->v.val_vec.array,
23407 len * elt_size, hash);
23409 break;
23410 case dw_val_class_const_double:
23411 hash = iterative_hash_object (val2->v.val_double.low, hash);
23412 hash = iterative_hash_object (val2->v.val_double.high, hash);
23413 break;
23414 case dw_val_class_addr:
23415 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23416 break;
23417 default:
23418 gcc_unreachable ();
23420 break;
23421 case DW_OP_bregx:
23422 case DW_OP_bit_piece:
23423 hash = iterative_hash_object (val1->v.val_int, hash);
23424 hash = iterative_hash_object (val2->v.val_int, hash);
23425 break;
23426 case DW_OP_addr:
23427 hash_addr:
23428 if (loc->dtprel)
23430 unsigned char dtprel = 0xd1;
23431 hash = iterative_hash_object (dtprel, hash);
23433 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23434 break;
23435 case DW_OP_GNU_addr_index:
23436 case DW_OP_GNU_const_index:
23438 if (loc->dtprel)
23440 unsigned char dtprel = 0xd1;
23441 hash = iterative_hash_object (dtprel, hash);
23443 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23445 break;
23446 case DW_OP_GNU_implicit_pointer:
23447 hash = iterative_hash_object (val2->v.val_int, hash);
23448 break;
23449 case DW_OP_GNU_entry_value:
23450 hash = hash_loc_operands (val1->v.val_loc, hash);
23451 break;
23452 case DW_OP_GNU_regval_type:
23453 case DW_OP_GNU_deref_type:
23455 unsigned int byte_size
23456 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23457 unsigned int encoding
23458 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23459 hash = iterative_hash_object (val1->v.val_int, hash);
23460 hash = iterative_hash_object (byte_size, hash);
23461 hash = iterative_hash_object (encoding, hash);
23463 break;
23464 case DW_OP_GNU_convert:
23465 case DW_OP_GNU_reinterpret:
23466 if (val1->val_class == dw_val_class_unsigned_const)
23468 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23469 break;
23471 /* FALLTHRU */
23472 case DW_OP_GNU_const_type:
23474 unsigned int byte_size
23475 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23476 unsigned int encoding
23477 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23478 hash = iterative_hash_object (byte_size, hash);
23479 hash = iterative_hash_object (encoding, hash);
23480 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23481 break;
23482 hash = iterative_hash_object (val2->val_class, hash);
23483 switch (val2->val_class)
23485 case dw_val_class_const:
23486 hash = iterative_hash_object (val2->v.val_int, hash);
23487 break;
23488 case dw_val_class_vec:
23490 unsigned int elt_size = val2->v.val_vec.elt_size;
23491 unsigned int len = val2->v.val_vec.length;
23493 hash = iterative_hash_object (elt_size, hash);
23494 hash = iterative_hash_object (len, hash);
23495 hash = iterative_hash (val2->v.val_vec.array,
23496 len * elt_size, hash);
23498 break;
23499 case dw_val_class_const_double:
23500 hash = iterative_hash_object (val2->v.val_double.low, hash);
23501 hash = iterative_hash_object (val2->v.val_double.high, hash);
23502 break;
23503 default:
23504 gcc_unreachable ();
23507 break;
23509 default:
23510 /* Other codes have no operands. */
23511 break;
23513 return hash;
23516 /* Iteratively hash the whole DWARF location expression LOC. */
23518 static inline hashval_t
23519 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23521 dw_loc_descr_ref l;
23522 bool sizes_computed = false;
23523 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23524 size_of_locs (loc);
23526 for (l = loc; l != NULL; l = l->dw_loc_next)
23528 enum dwarf_location_atom opc = l->dw_loc_opc;
23529 hash = iterative_hash_object (opc, hash);
23530 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23532 size_of_locs (loc);
23533 sizes_computed = true;
23535 hash = hash_loc_operands (l, hash);
23537 return hash;
23540 /* Compute hash of the whole location list LIST_HEAD. */
23542 static inline void
23543 hash_loc_list (dw_loc_list_ref list_head)
23545 dw_loc_list_ref curr = list_head;
23546 hashval_t hash = 0;
23548 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23550 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23551 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23552 if (curr->section)
23553 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23554 hash);
23555 hash = hash_locs (curr->expr, hash);
23557 list_head->hash = hash;
23560 /* Return true if X and Y opcodes have the same operands. */
23562 static inline bool
23563 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23565 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23566 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23567 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23568 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23570 switch (x->dw_loc_opc)
23572 case DW_OP_const4u:
23573 case DW_OP_const8u:
23574 if (x->dtprel)
23575 goto hash_addr;
23576 /* FALLTHRU */
23577 case DW_OP_const1u:
23578 case DW_OP_const1s:
23579 case DW_OP_const2u:
23580 case DW_OP_const2s:
23581 case DW_OP_const4s:
23582 case DW_OP_const8s:
23583 case DW_OP_constu:
23584 case DW_OP_consts:
23585 case DW_OP_pick:
23586 case DW_OP_plus_uconst:
23587 case DW_OP_breg0:
23588 case DW_OP_breg1:
23589 case DW_OP_breg2:
23590 case DW_OP_breg3:
23591 case DW_OP_breg4:
23592 case DW_OP_breg5:
23593 case DW_OP_breg6:
23594 case DW_OP_breg7:
23595 case DW_OP_breg8:
23596 case DW_OP_breg9:
23597 case DW_OP_breg10:
23598 case DW_OP_breg11:
23599 case DW_OP_breg12:
23600 case DW_OP_breg13:
23601 case DW_OP_breg14:
23602 case DW_OP_breg15:
23603 case DW_OP_breg16:
23604 case DW_OP_breg17:
23605 case DW_OP_breg18:
23606 case DW_OP_breg19:
23607 case DW_OP_breg20:
23608 case DW_OP_breg21:
23609 case DW_OP_breg22:
23610 case DW_OP_breg23:
23611 case DW_OP_breg24:
23612 case DW_OP_breg25:
23613 case DW_OP_breg26:
23614 case DW_OP_breg27:
23615 case DW_OP_breg28:
23616 case DW_OP_breg29:
23617 case DW_OP_breg30:
23618 case DW_OP_breg31:
23619 case DW_OP_regx:
23620 case DW_OP_fbreg:
23621 case DW_OP_piece:
23622 case DW_OP_deref_size:
23623 case DW_OP_xderef_size:
23624 return valx1->v.val_int == valy1->v.val_int;
23625 case DW_OP_skip:
23626 case DW_OP_bra:
23627 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23628 can cause irrelevant differences in dw_loc_addr. */
23629 gcc_assert (valx1->val_class == dw_val_class_loc
23630 && valy1->val_class == dw_val_class_loc
23631 && (dwarf_split_debug_info
23632 || x->dw_loc_addr == y->dw_loc_addr));
23633 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23634 case DW_OP_implicit_value:
23635 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23636 || valx2->val_class != valy2->val_class)
23637 return false;
23638 switch (valx2->val_class)
23640 case dw_val_class_const:
23641 return valx2->v.val_int == valy2->v.val_int;
23642 case dw_val_class_vec:
23643 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23644 && valx2->v.val_vec.length == valy2->v.val_vec.length
23645 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23646 valx2->v.val_vec.elt_size
23647 * valx2->v.val_vec.length) == 0;
23648 case dw_val_class_const_double:
23649 return valx2->v.val_double.low == valy2->v.val_double.low
23650 && valx2->v.val_double.high == valy2->v.val_double.high;
23651 case dw_val_class_addr:
23652 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23653 default:
23654 gcc_unreachable ();
23656 case DW_OP_bregx:
23657 case DW_OP_bit_piece:
23658 return valx1->v.val_int == valy1->v.val_int
23659 && valx2->v.val_int == valy2->v.val_int;
23660 case DW_OP_addr:
23661 hash_addr:
23662 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23663 case DW_OP_GNU_addr_index:
23664 case DW_OP_GNU_const_index:
23666 rtx ax1 = valx1->val_entry->addr.rtl;
23667 rtx ay1 = valy1->val_entry->addr.rtl;
23668 return rtx_equal_p (ax1, ay1);
23670 case DW_OP_GNU_implicit_pointer:
23671 return valx1->val_class == dw_val_class_die_ref
23672 && valx1->val_class == valy1->val_class
23673 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23674 && valx2->v.val_int == valy2->v.val_int;
23675 case DW_OP_GNU_entry_value:
23676 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23677 case DW_OP_GNU_const_type:
23678 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23679 || valx2->val_class != valy2->val_class)
23680 return false;
23681 switch (valx2->val_class)
23683 case dw_val_class_const:
23684 return valx2->v.val_int == valy2->v.val_int;
23685 case dw_val_class_vec:
23686 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23687 && valx2->v.val_vec.length == valy2->v.val_vec.length
23688 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23689 valx2->v.val_vec.elt_size
23690 * valx2->v.val_vec.length) == 0;
23691 case dw_val_class_const_double:
23692 return valx2->v.val_double.low == valy2->v.val_double.low
23693 && valx2->v.val_double.high == valy2->v.val_double.high;
23694 default:
23695 gcc_unreachable ();
23697 case DW_OP_GNU_regval_type:
23698 case DW_OP_GNU_deref_type:
23699 return valx1->v.val_int == valy1->v.val_int
23700 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23701 case DW_OP_GNU_convert:
23702 case DW_OP_GNU_reinterpret:
23703 if (valx1->val_class != valy1->val_class)
23704 return false;
23705 if (valx1->val_class == dw_val_class_unsigned_const)
23706 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23707 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23708 case DW_OP_GNU_parameter_ref:
23709 return valx1->val_class == dw_val_class_die_ref
23710 && valx1->val_class == valy1->val_class
23711 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23712 default:
23713 /* Other codes have no operands. */
23714 return true;
23718 /* Return true if DWARF location expressions X and Y are the same. */
23720 static inline bool
23721 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23723 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23724 if (x->dw_loc_opc != y->dw_loc_opc
23725 || x->dtprel != y->dtprel
23726 || !compare_loc_operands (x, y))
23727 break;
23728 return x == NULL && y == NULL;
23731 /* Hashtable helpers. */
23733 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23735 typedef dw_loc_list_struct value_type;
23736 typedef dw_loc_list_struct compare_type;
23737 static inline hashval_t hash (const value_type *);
23738 static inline bool equal (const value_type *, const compare_type *);
23741 /* Return precomputed hash of location list X. */
23743 inline hashval_t
23744 loc_list_hasher::hash (const value_type *x)
23746 return x->hash;
23749 /* Return true if location lists A and B are the same. */
23751 inline bool
23752 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23754 if (a == b)
23755 return 1;
23756 if (a->hash != b->hash)
23757 return 0;
23758 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23759 if (strcmp (a->begin, b->begin) != 0
23760 || strcmp (a->end, b->end) != 0
23761 || (a->section == NULL) != (b->section == NULL)
23762 || (a->section && strcmp (a->section, b->section) != 0)
23763 || !compare_locs (a->expr, b->expr))
23764 break;
23765 return a == NULL && b == NULL;
23768 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23771 /* Recursively optimize location lists referenced from DIE
23772 children and share them whenever possible. */
23774 static void
23775 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23777 dw_die_ref c;
23778 dw_attr_ref a;
23779 unsigned ix;
23780 dw_loc_list_struct **slot;
23782 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23783 if (AT_class (a) == dw_val_class_loc_list)
23785 dw_loc_list_ref list = AT_loc_list (a);
23786 /* TODO: perform some optimizations here, before hashing
23787 it and storing into the hash table. */
23788 hash_loc_list (list);
23789 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23790 if (*slot == NULL)
23791 *slot = list;
23792 else
23793 a->dw_attr_val.v.val_loc_list = *slot;
23796 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23800 /* Recursively assign each location list a unique index into the debug_addr
23801 section. */
23803 static void
23804 index_location_lists (dw_die_ref die)
23806 dw_die_ref c;
23807 dw_attr_ref a;
23808 unsigned ix;
23810 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23811 if (AT_class (a) == dw_val_class_loc_list)
23813 dw_loc_list_ref list = AT_loc_list (a);
23814 dw_loc_list_ref curr;
23815 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23817 /* Don't index an entry that has already been indexed
23818 or won't be output. */
23819 if (curr->begin_entry != NULL
23820 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23821 continue;
23823 curr->begin_entry
23824 = add_addr_table_entry (xstrdup (curr->begin),
23825 ate_kind_label);
23829 FOR_EACH_CHILD (die, c, index_location_lists (c));
23832 /* Optimize location lists referenced from DIE
23833 children and share them whenever possible. */
23835 static void
23836 optimize_location_lists (dw_die_ref die)
23838 loc_list_hash_type htab;
23839 htab.create (500);
23840 optimize_location_lists_1 (die, htab);
23841 htab.dispose ();
23844 /* Output stuff that dwarf requires at the end of every file,
23845 and generate the DWARF-2 debugging info. */
23847 static void
23848 dwarf2out_finish (const char *filename)
23850 limbo_die_node *node, *next_node;
23851 comdat_type_node *ctnode;
23852 hash_table <comdat_type_hasher> comdat_type_table;
23853 unsigned int i;
23854 dw_die_ref main_comp_unit_die;
23856 /* PCH might result in DW_AT_producer string being restored from the
23857 header compilation, so always fill it with empty string initially
23858 and overwrite only here. */
23859 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23860 producer_string = gen_producer_string ();
23861 producer->dw_attr_val.v.val_str->refcount--;
23862 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23864 gen_scheduled_generic_parms_dies ();
23865 gen_remaining_tmpl_value_param_die_attribute ();
23867 /* Add the name for the main input file now. We delayed this from
23868 dwarf2out_init to avoid complications with PCH. */
23869 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23870 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23871 add_comp_dir_attribute (comp_unit_die ());
23872 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23874 bool p = false;
23875 htab_traverse (file_table, file_table_relative_p, &p);
23876 if (p)
23877 add_comp_dir_attribute (comp_unit_die ());
23880 if (deferred_locations_list)
23881 for (i = 0; i < deferred_locations_list->length (); i++)
23883 add_location_or_const_value_attribute (
23884 (*deferred_locations_list)[i].die,
23885 (*deferred_locations_list)[i].variable,
23886 false,
23887 DW_AT_location);
23890 /* Traverse the limbo die list, and add parent/child links. The only
23891 dies without parents that should be here are concrete instances of
23892 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23893 For concrete instances, we can get the parent die from the abstract
23894 instance. */
23895 for (node = limbo_die_list; node; node = next_node)
23897 dw_die_ref die = node->die;
23898 next_node = node->next;
23900 if (die->die_parent == NULL)
23902 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23904 if (origin && origin->die_parent)
23905 add_child_die (origin->die_parent, die);
23906 else if (is_cu_die (die))
23908 else if (seen_error ())
23909 /* It's OK to be confused by errors in the input. */
23910 add_child_die (comp_unit_die (), die);
23911 else
23913 /* In certain situations, the lexical block containing a
23914 nested function can be optimized away, which results
23915 in the nested function die being orphaned. Likewise
23916 with the return type of that nested function. Force
23917 this to be a child of the containing function.
23919 It may happen that even the containing function got fully
23920 inlined and optimized out. In that case we are lost and
23921 assign the empty child. This should not be big issue as
23922 the function is likely unreachable too. */
23923 gcc_assert (node->created_for);
23925 if (DECL_P (node->created_for))
23926 origin = get_context_die (DECL_CONTEXT (node->created_for));
23927 else if (TYPE_P (node->created_for))
23928 origin = scope_die_for (node->created_for, comp_unit_die ());
23929 else
23930 origin = comp_unit_die ();
23932 add_child_die (origin, die);
23937 limbo_die_list = NULL;
23939 #if ENABLE_ASSERT_CHECKING
23941 dw_die_ref die = comp_unit_die (), c;
23942 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23944 #endif
23945 resolve_addr (comp_unit_die ());
23946 move_marked_base_types ();
23948 for (node = deferred_asm_name; node; node = node->next)
23950 tree decl = node->created_for;
23951 /* When generating LTO bytecode we can not generate new assembler
23952 names at this point and all important decls got theirs via
23953 free-lang-data. */
23954 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23955 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23957 add_linkage_attr (node->die, decl);
23958 move_linkage_attr (node->die);
23962 deferred_asm_name = NULL;
23964 /* Walk through the list of incomplete types again, trying once more to
23965 emit full debugging info for them. */
23966 retry_incomplete_types ();
23968 if (flag_eliminate_unused_debug_types)
23969 prune_unused_types ();
23971 /* Generate separate COMDAT sections for type DIEs. */
23972 if (use_debug_types)
23974 break_out_comdat_types (comp_unit_die ());
23976 /* Each new type_unit DIE was added to the limbo die list when created.
23977 Since these have all been added to comdat_type_list, clear the
23978 limbo die list. */
23979 limbo_die_list = NULL;
23981 /* For each new comdat type unit, copy declarations for incomplete
23982 types to make the new unit self-contained (i.e., no direct
23983 references to the main compile unit). */
23984 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23985 copy_decls_for_unworthy_types (ctnode->root_die);
23986 copy_decls_for_unworthy_types (comp_unit_die ());
23988 /* In the process of copying declarations from one unit to another,
23989 we may have left some declarations behind that are no longer
23990 referenced. Prune them. */
23991 prune_unused_types ();
23994 /* Generate separate CUs for each of the include files we've seen.
23995 They will go into limbo_die_list. */
23996 if (flag_eliminate_dwarf2_dups)
23997 break_out_includes (comp_unit_die ());
23999 /* Traverse the DIE's and add add sibling attributes to those DIE's
24000 that have children. */
24001 add_sibling_attributes (comp_unit_die ());
24002 for (node = limbo_die_list; node; node = node->next)
24003 add_sibling_attributes (node->die);
24004 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24005 add_sibling_attributes (ctnode->root_die);
24007 /* When splitting DWARF info, we put some attributes in the
24008 skeleton compile_unit DIE that remains in the .o, while
24009 most attributes go in the DWO compile_unit_die. */
24010 if (dwarf_split_debug_info)
24011 main_comp_unit_die = gen_compile_unit_die (NULL);
24012 else
24013 main_comp_unit_die = comp_unit_die ();
24015 /* Output a terminator label for the .text section. */
24016 switch_to_section (text_section);
24017 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24018 if (cold_text_section)
24020 switch_to_section (cold_text_section);
24021 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24024 /* We can only use the low/high_pc attributes if all of the code was
24025 in .text. */
24026 if (!have_multiple_function_sections
24027 || (dwarf_version < 3 && dwarf_strict))
24029 /* Don't add if the CU has no associated code. */
24030 if (text_section_used)
24031 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24032 text_end_label, true);
24034 else
24036 unsigned fde_idx;
24037 dw_fde_ref fde;
24038 bool range_list_added = false;
24040 if (text_section_used)
24041 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24042 text_end_label, &range_list_added, true);
24043 if (cold_text_section_used)
24044 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24045 cold_end_label, &range_list_added, true);
24047 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24049 if (DECL_IGNORED_P (fde->decl))
24050 continue;
24051 if (!fde->in_std_section)
24052 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24053 fde->dw_fde_end, &range_list_added,
24054 true);
24055 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24056 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24057 fde->dw_fde_second_end, &range_list_added,
24058 true);
24061 if (range_list_added)
24063 /* We need to give .debug_loc and .debug_ranges an appropriate
24064 "base address". Use zero so that these addresses become
24065 absolute. Historically, we've emitted the unexpected
24066 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24067 Emit both to give time for other tools to adapt. */
24068 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24069 if (! dwarf_strict && dwarf_version < 4)
24070 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24072 add_ranges (NULL);
24076 if (debug_info_level >= DINFO_LEVEL_NORMAL)
24077 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24078 debug_line_section_label);
24080 if (have_macinfo)
24081 add_AT_macptr (comp_unit_die (),
24082 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24083 macinfo_section_label);
24085 if (dwarf_split_debug_info && addr_index_table != NULL)
24087 /* optimize_location_lists calculates the size of the lists,
24088 so index them first, and assign indices to the entries.
24089 Although optimize_location_lists will remove entries from
24090 the table, it only does so for duplicates, and therefore
24091 only reduces ref_counts to 1. */
24092 unsigned int index = 0;
24093 index_location_lists (comp_unit_die ());
24094 htab_traverse_noresize (addr_index_table,
24095 index_addr_table_entry, &index);
24097 if (have_location_lists)
24098 optimize_location_lists (comp_unit_die ());
24100 save_macinfo_strings ();
24102 if (dwarf_split_debug_info)
24104 unsigned int index = 0;
24106 /* Add attributes common to skeleton compile_units and
24107 type_units. Because these attributes include strings, it
24108 must be done before freezing the string table. Top-level
24109 skeleton die attrs are added when the skeleton type unit is
24110 created, so ensure it is created by this point. */
24111 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24112 (void) get_skeleton_type_unit ();
24113 htab_traverse_noresize (debug_str_hash, index_string, &index);
24116 /* Output all of the compilation units. We put the main one last so that
24117 the offsets are available to output_pubnames. */
24118 for (node = limbo_die_list; node; node = node->next)
24119 output_comp_unit (node->die, 0);
24121 comdat_type_table.create (100);
24122 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24124 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24126 /* Don't output duplicate types. */
24127 if (*slot != HTAB_EMPTY_ENTRY)
24128 continue;
24130 /* Add a pointer to the line table for the main compilation unit
24131 so that the debugger can make sense of DW_AT_decl_file
24132 attributes. */
24133 if (debug_info_level >= DINFO_LEVEL_NORMAL)
24134 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24135 (!dwarf_split_debug_info
24136 ? debug_line_section_label
24137 : debug_skeleton_line_section_label));
24139 output_comdat_type_unit (ctnode);
24140 *slot = ctnode;
24142 comdat_type_table.dispose ();
24144 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24145 both the main_cu and all skeleton TUs. Making this call unconditional
24146 would end up either adding a second copy of the AT_pubnames attribute, or
24147 requiring a special case in add_top_level_skeleton_die_attrs. */
24148 if (!dwarf_split_debug_info)
24149 add_AT_pubnames (comp_unit_die ());
24151 if (dwarf_split_debug_info)
24153 int mark;
24154 unsigned char checksum[16];
24155 struct md5_ctx ctx;
24157 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24158 md5_init_ctx (&ctx);
24159 mark = 0;
24160 die_checksum (comp_unit_die (), &ctx, &mark);
24161 unmark_all_dies (comp_unit_die ());
24162 md5_finish_ctx (&ctx, checksum);
24164 /* Use the first 8 bytes of the checksum as the dwo_id,
24165 and add it to both comp-unit DIEs. */
24166 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24167 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24169 /* Add the base offset of the ranges table to the skeleton
24170 comp-unit DIE. */
24171 if (ranges_table_in_use)
24172 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24173 ranges_section_label);
24175 switch_to_section (debug_addr_section);
24176 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24177 output_addr_table ();
24180 /* Output the main compilation unit if non-empty or if .debug_macinfo
24181 or .debug_macro will be emitted. */
24182 output_comp_unit (comp_unit_die (), have_macinfo);
24184 if (dwarf_split_debug_info && info_section_emitted)
24185 output_skeleton_debug_sections (main_comp_unit_die);
24187 /* Output the abbreviation table. */
24188 if (abbrev_die_table_in_use != 1)
24190 switch_to_section (debug_abbrev_section);
24191 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24192 output_abbrev_section ();
24195 /* Output location list section if necessary. */
24196 if (have_location_lists)
24198 /* Output the location lists info. */
24199 switch_to_section (debug_loc_section);
24200 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24201 output_location_lists (comp_unit_die ());
24204 output_pubtables ();
24206 /* Output the address range information if a CU (.debug_info section)
24207 was emitted. We output an empty table even if we had no functions
24208 to put in it. This because the consumer has no way to tell the
24209 difference between an empty table that we omitted and failure to
24210 generate a table that would have contained data. */
24211 if (info_section_emitted)
24213 unsigned long aranges_length = size_of_aranges ();
24215 switch_to_section (debug_aranges_section);
24216 output_aranges (aranges_length);
24219 /* Output ranges section if necessary. */
24220 if (ranges_table_in_use)
24222 switch_to_section (debug_ranges_section);
24223 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24224 output_ranges ();
24227 /* Have to end the macro section. */
24228 if (have_macinfo)
24230 switch_to_section (debug_macinfo_section);
24231 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24232 output_macinfo ();
24233 dw2_asm_output_data (1, 0, "End compilation unit");
24236 /* Output the source line correspondence table. We must do this
24237 even if there is no line information. Otherwise, on an empty
24238 translation unit, we will generate a present, but empty,
24239 .debug_info section. IRIX 6.5 `nm' will then complain when
24240 examining the file. This is done late so that any filenames
24241 used by the debug_info section are marked as 'used'. */
24242 switch_to_section (debug_line_section);
24243 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24244 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24245 output_line_info (false);
24247 if (dwarf_split_debug_info && info_section_emitted)
24249 switch_to_section (debug_skeleton_line_section);
24250 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24251 output_line_info (true);
24254 /* If we emitted any indirect strings, output the string table too. */
24255 if (debug_str_hash || skeleton_debug_str_hash)
24256 output_indirect_strings ();
24259 #include "gt-dwarf2out.h"