Reverting merge from trunk
[official-gcc.git] / gcc / dwarf2out.c
blob24f405ec26ab20080793faa918f715f777960c97
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "tree.h"
63 #include "version.h"
64 #include "flags.h"
65 #include "rtl.h"
66 #include "hard-reg-set.h"
67 #include "regs.h"
68 #include "insn-config.h"
69 #include "reload.h"
70 #include "function.h"
71 #include "output.h"
72 #include "expr.h"
73 #include "except.h"
74 #include "dwarf2.h"
75 #include "dwarf2out.h"
76 #include "dwarf2asm.h"
77 #include "toplev.h"
78 #include "ggc.h"
79 #include "md5.h"
80 #include "tm_p.h"
81 #include "diagnostic.h"
82 #include "tree-pretty-print.h"
83 #include "debug.h"
84 #include "target.h"
85 #include "common/common-target.h"
86 #include "langhooks.h"
87 #include "hash-table.h"
88 #include "cgraph.h"
89 #include "input.h"
90 #include "ira.h"
91 #include "lra.h"
92 #include "dumpfile.h"
93 #include "opts.h"
94 #include "tree-dfa.h"
95 #include "gdb/gdb-index.h"
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98 static rtx last_var_location_insn;
99 static rtx cached_next_real_insn;
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
104 /* Define this macro to be a nonzero value if the directory specifications
105 which are output in the debug info should end with a separator. */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108 from generating indirect strings in DWARF2 debug information, for instance
109 if your target is stuck with an old version of GDB that is unable to
110 process them properly or uses VMS Debug. */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
117 /* ??? Poison these here until it can be done generically. They've been
118 totally replaced in this file; make sure it stays that way. */
119 #undef DWARF2_UNWIND_INFO
120 #undef DWARF2_FRAME_INFO
121 #if (GCC_VERSION >= 3000)
122 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
123 #endif
125 /* The size of the target's pointer type. */
126 #ifndef PTR_SIZE
127 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
128 #endif
130 /* Array of RTXes referenced by the debugging information, which therefore
131 must be kept around forever. */
132 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
134 /* A pointer to the base of a list of incomplete types which might be
135 completed at some later time. incomplete_types_list needs to be a
136 vec<tree, va_gc> *because we want to tell the garbage collector about
137 it. */
138 static GTY(()) vec<tree, va_gc> *incomplete_types;
140 /* A pointer to the base of a table of references to declaration
141 scopes. This table is a display which tracks the nesting
142 of declaration scopes at the current scope and containing
143 scopes. This table is used to find the proper place to
144 define type declaration DIE's. */
145 static GTY(()) vec<tree, va_gc> *decl_scope_table;
147 /* Pointers to various DWARF2 sections. */
148 static GTY(()) section *debug_info_section;
149 static GTY(()) section *debug_skeleton_info_section;
150 static GTY(()) section *debug_abbrev_section;
151 static GTY(()) section *debug_skeleton_abbrev_section;
152 static GTY(()) section *debug_aranges_section;
153 static GTY(()) section *debug_addr_section;
154 static GTY(()) section *debug_macinfo_section;
155 static GTY(()) section *debug_line_section;
156 static GTY(()) section *debug_skeleton_line_section;
157 static GTY(()) section *debug_loc_section;
158 static GTY(()) section *debug_pubnames_section;
159 static GTY(()) section *debug_pubtypes_section;
160 static GTY(()) section *debug_str_section;
161 static GTY(()) section *debug_str_dwo_section;
162 static GTY(()) section *debug_str_offsets_section;
163 static GTY(()) section *debug_ranges_section;
164 static GTY(()) section *debug_frame_section;
166 /* Maximum size (in bytes) of an artificially generated label. */
167 #define MAX_ARTIFICIAL_LABEL_BYTES 30
169 /* According to the (draft) DWARF 3 specification, the initial length
170 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
171 bytes are 0xffffffff, followed by the length stored in the next 8
172 bytes.
174 However, the SGI/MIPS ABI uses an initial length which is equal to
175 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
177 #ifndef DWARF_INITIAL_LENGTH_SIZE
178 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
179 #endif
181 /* Round SIZE up to the nearest BOUNDARY. */
182 #define DWARF_ROUND(SIZE,BOUNDARY) \
183 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
185 /* CIE identifier. */
186 #if HOST_BITS_PER_WIDE_INT >= 64
187 #define DWARF_CIE_ID \
188 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
189 #else
190 #define DWARF_CIE_ID DW_CIE_ID
191 #endif
194 /* A vector for a table that contains frame description
195 information for each routine. */
196 #define NOT_INDEXED (-1U)
197 #define NO_INDEX_ASSIGNED (-2U)
199 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
201 struct GTY(()) indirect_string_node {
202 const char *str;
203 unsigned int refcount;
204 enum dwarf_form form;
205 char *label;
206 unsigned int index;
209 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
211 /* With split_debug_info, both the comp_dir and dwo_name go in the
212 main object file, rather than the dwo, similar to the force_direct
213 parameter elsewhere but with additional complications:
215 1) The string is needed in both the main object file and the dwo.
216 That is, the comp_dir and dwo_name will appear in both places.
218 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
219 DW_FORM_GNU_str_index.
221 3) GCC chooses the form to use late, depending on the size and
222 reference count.
224 Rather than forcing the all debug string handling functions and
225 callers to deal with these complications, simply use a separate,
226 special-cased string table for any attribute that should go in the
227 main object file. This limits the complexity to just the places
228 that need it. */
230 static GTY ((param_is (struct indirect_string_node)))
231 htab_t skeleton_debug_str_hash;
233 static GTY(()) int dw2_string_counter;
235 /* True if the compilation unit places functions in more than one section. */
236 static GTY(()) bool have_multiple_function_sections = false;
238 /* Whether the default text and cold text sections have been used at all. */
240 static GTY(()) bool text_section_used = false;
241 static GTY(()) bool cold_text_section_used = false;
243 /* The default cold text section. */
244 static GTY(()) section *cold_text_section;
246 /* Forward declarations for functions defined in this file. */
248 static char *stripattributes (const char *);
249 static void output_call_frame_info (int);
250 static void dwarf2out_note_section_used (void);
252 /* Personality decl of current unit. Used only when assembler does not support
253 personality CFI. */
254 static GTY(()) rtx current_unit_personality;
256 /* Data and reference forms for relocatable data. */
257 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
258 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
260 #ifndef DEBUG_FRAME_SECTION
261 #define DEBUG_FRAME_SECTION ".debug_frame"
262 #endif
264 #ifndef FUNC_BEGIN_LABEL
265 #define FUNC_BEGIN_LABEL "LFB"
266 #endif
268 #ifndef FUNC_END_LABEL
269 #define FUNC_END_LABEL "LFE"
270 #endif
272 #ifndef PROLOGUE_END_LABEL
273 #define PROLOGUE_END_LABEL "LPE"
274 #endif
276 #ifndef EPILOGUE_BEGIN_LABEL
277 #define EPILOGUE_BEGIN_LABEL "LEB"
278 #endif
280 #ifndef FRAME_BEGIN_LABEL
281 #define FRAME_BEGIN_LABEL "Lframe"
282 #endif
283 #define CIE_AFTER_SIZE_LABEL "LSCIE"
284 #define CIE_END_LABEL "LECIE"
285 #define FDE_LABEL "LSFDE"
286 #define FDE_AFTER_SIZE_LABEL "LASFDE"
287 #define FDE_END_LABEL "LEFDE"
288 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
289 #define LINE_NUMBER_END_LABEL "LELT"
290 #define LN_PROLOG_AS_LABEL "LASLTP"
291 #define LN_PROLOG_END_LABEL "LELTP"
292 #define DIE_LABEL_PREFIX "DW"
294 /* Match the base name of a file to the base name of a compilation unit. */
296 static int
297 matches_main_base (const char *path)
299 /* Cache the last query. */
300 static const char *last_path = NULL;
301 static int last_match = 0;
302 if (path != last_path)
304 const char *base;
305 int length = base_of_path (path, &base);
306 last_path = path;
307 last_match = (length == main_input_baselength
308 && memcmp (base, main_input_basename, length) == 0);
310 return last_match;
313 #ifdef DEBUG_DEBUG_STRUCT
315 static int
316 dump_struct_debug (tree type, enum debug_info_usage usage,
317 enum debug_struct_file criterion, int generic,
318 int matches, int result)
320 /* Find the type name. */
321 tree type_decl = TYPE_STUB_DECL (type);
322 tree t = type_decl;
323 const char *name = 0;
324 if (TREE_CODE (t) == TYPE_DECL)
325 t = DECL_NAME (t);
326 if (t)
327 name = IDENTIFIER_POINTER (t);
329 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
330 criterion,
331 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
332 matches ? "bas" : "hdr",
333 generic ? "gen" : "ord",
334 usage == DINFO_USAGE_DFN ? ";" :
335 usage == DINFO_USAGE_DIR_USE ? "." : "*",
336 result,
337 (void*) type_decl, name);
338 return result;
340 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
341 dump_struct_debug (type, usage, criterion, generic, matches, result)
343 #else
345 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
346 (result)
348 #endif
350 static bool
351 should_emit_struct_debug (tree type, enum debug_info_usage usage)
353 enum debug_struct_file criterion;
354 tree type_decl;
355 bool generic = lang_hooks.types.generic_p (type);
357 if (generic)
358 criterion = debug_struct_generic[usage];
359 else
360 criterion = debug_struct_ordinary[usage];
362 if (criterion == DINFO_STRUCT_FILE_NONE)
363 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
364 if (criterion == DINFO_STRUCT_FILE_ANY)
365 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
367 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
369 if (type_decl != NULL)
371 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
372 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
374 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
375 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
378 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
381 /* Return a pointer to a copy of the section string name S with all
382 attributes stripped off, and an asterisk prepended (for assemble_name). */
384 static inline char *
385 stripattributes (const char *s)
387 char *stripped = XNEWVEC (char, strlen (s) + 2);
388 char *p = stripped;
390 *p++ = '*';
392 while (*s && *s != ',')
393 *p++ = *s++;
395 *p = '\0';
396 return stripped;
399 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
400 switch to the data section instead, and write out a synthetic start label
401 for collect2 the first time around. */
403 static void
404 switch_to_eh_frame_section (bool back)
406 tree label;
408 #ifdef EH_FRAME_SECTION_NAME
409 if (eh_frame_section == 0)
411 int flags;
413 if (EH_TABLES_CAN_BE_READ_ONLY)
415 int fde_encoding;
416 int per_encoding;
417 int lsda_encoding;
419 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
420 /*global=*/0);
421 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
422 /*global=*/1);
423 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
424 /*global=*/0);
425 flags = ((! flag_pic
426 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
427 && (fde_encoding & 0x70) != DW_EH_PE_aligned
428 && (per_encoding & 0x70) != DW_EH_PE_absptr
429 && (per_encoding & 0x70) != DW_EH_PE_aligned
430 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
431 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
432 ? 0 : SECTION_WRITE);
434 else
435 flags = SECTION_WRITE;
436 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
438 #endif /* EH_FRAME_SECTION_NAME */
440 if (eh_frame_section)
441 switch_to_section (eh_frame_section);
442 else
444 /* We have no special eh_frame section. Put the information in
445 the data section and emit special labels to guide collect2. */
446 switch_to_section (data_section);
448 if (!back)
450 label = get_file_function_name ("F");
451 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
452 targetm.asm_out.globalize_label (asm_out_file,
453 IDENTIFIER_POINTER (label));
454 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
459 /* Switch [BACK] to the eh or debug frame table section, depending on
460 FOR_EH. */
462 static void
463 switch_to_frame_table_section (int for_eh, bool back)
465 if (for_eh)
466 switch_to_eh_frame_section (back);
467 else
469 if (!debug_frame_section)
470 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
471 SECTION_DEBUG, NULL);
472 switch_to_section (debug_frame_section);
476 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
478 enum dw_cfi_oprnd_type
479 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
481 switch (cfi)
483 case DW_CFA_nop:
484 case DW_CFA_GNU_window_save:
485 case DW_CFA_remember_state:
486 case DW_CFA_restore_state:
487 return dw_cfi_oprnd_unused;
489 case DW_CFA_set_loc:
490 case DW_CFA_advance_loc1:
491 case DW_CFA_advance_loc2:
492 case DW_CFA_advance_loc4:
493 case DW_CFA_MIPS_advance_loc8:
494 return dw_cfi_oprnd_addr;
496 case DW_CFA_offset:
497 case DW_CFA_offset_extended:
498 case DW_CFA_def_cfa:
499 case DW_CFA_offset_extended_sf:
500 case DW_CFA_def_cfa_sf:
501 case DW_CFA_restore:
502 case DW_CFA_restore_extended:
503 case DW_CFA_undefined:
504 case DW_CFA_same_value:
505 case DW_CFA_def_cfa_register:
506 case DW_CFA_register:
507 case DW_CFA_expression:
508 return dw_cfi_oprnd_reg_num;
510 case DW_CFA_def_cfa_offset:
511 case DW_CFA_GNU_args_size:
512 case DW_CFA_def_cfa_offset_sf:
513 return dw_cfi_oprnd_offset;
515 case DW_CFA_def_cfa_expression:
516 return dw_cfi_oprnd_loc;
518 default:
519 gcc_unreachable ();
523 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
525 enum dw_cfi_oprnd_type
526 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
528 switch (cfi)
530 case DW_CFA_def_cfa:
531 case DW_CFA_def_cfa_sf:
532 case DW_CFA_offset:
533 case DW_CFA_offset_extended_sf:
534 case DW_CFA_offset_extended:
535 return dw_cfi_oprnd_offset;
537 case DW_CFA_register:
538 return dw_cfi_oprnd_reg_num;
540 case DW_CFA_expression:
541 return dw_cfi_oprnd_loc;
543 default:
544 return dw_cfi_oprnd_unused;
548 /* Output one FDE. */
550 static void
551 output_fde (dw_fde_ref fde, bool for_eh, bool second,
552 char *section_start_label, int fde_encoding, char *augmentation,
553 bool any_lsda_needed, int lsda_encoding)
555 const char *begin, *end;
556 static unsigned int j;
557 char l1[20], l2[20];
559 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
560 /* empty */ 0);
561 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
562 for_eh + j);
563 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
564 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
565 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
566 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
567 " indicating 64-bit DWARF extension");
568 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
569 "FDE Length");
570 ASM_OUTPUT_LABEL (asm_out_file, l1);
572 if (for_eh)
573 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
574 else
575 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
576 debug_frame_section, "FDE CIE offset");
578 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
579 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
581 if (for_eh)
583 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
584 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
585 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
586 "FDE initial location");
587 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
588 end, begin, "FDE address range");
590 else
592 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
593 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
596 if (augmentation[0])
598 if (any_lsda_needed)
600 int size = size_of_encoded_value (lsda_encoding);
602 if (lsda_encoding == DW_EH_PE_aligned)
604 int offset = ( 4 /* Length */
605 + 4 /* CIE offset */
606 + 2 * size_of_encoded_value (fde_encoding)
607 + 1 /* Augmentation size */ );
608 int pad = -offset & (PTR_SIZE - 1);
610 size += pad;
611 gcc_assert (size_of_uleb128 (size) == 1);
614 dw2_asm_output_data_uleb128 (size, "Augmentation size");
616 if (fde->uses_eh_lsda)
618 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
619 fde->funcdef_number);
620 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
621 gen_rtx_SYMBOL_REF (Pmode, l1),
622 false,
623 "Language Specific Data Area");
625 else
627 if (lsda_encoding == DW_EH_PE_aligned)
628 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
629 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
630 "Language Specific Data Area (none)");
633 else
634 dw2_asm_output_data_uleb128 (0, "Augmentation size");
637 /* Loop through the Call Frame Instructions associated with this FDE. */
638 fde->dw_fde_current_label = begin;
640 size_t from, until, i;
642 from = 0;
643 until = vec_safe_length (fde->dw_fde_cfi);
645 if (fde->dw_fde_second_begin == NULL)
647 else if (!second)
648 until = fde->dw_fde_switch_cfi_index;
649 else
650 from = fde->dw_fde_switch_cfi_index;
652 for (i = from; i < until; i++)
653 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
656 /* If we are to emit a ref/link from function bodies to their frame tables,
657 do it now. This is typically performed to make sure that tables
658 associated with functions are dragged with them and not discarded in
659 garbage collecting links. We need to do this on a per function basis to
660 cope with -ffunction-sections. */
662 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
663 /* Switch to the function section, emit the ref to the tables, and
664 switch *back* into the table section. */
665 switch_to_section (function_section (fde->decl));
666 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
667 switch_to_frame_table_section (for_eh, true);
668 #endif
670 /* Pad the FDE out to an address sized boundary. */
671 ASM_OUTPUT_ALIGN (asm_out_file,
672 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
673 ASM_OUTPUT_LABEL (asm_out_file, l2);
675 j += 2;
678 /* Return true if frame description entry FDE is needed for EH. */
680 static bool
681 fde_needed_for_eh_p (dw_fde_ref fde)
683 if (flag_asynchronous_unwind_tables)
684 return true;
686 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
687 return true;
689 if (fde->uses_eh_lsda)
690 return true;
692 /* If exceptions are enabled, we have collected nothrow info. */
693 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
694 return false;
696 return true;
699 /* Output the call frame information used to record information
700 that relates to calculating the frame pointer, and records the
701 location of saved registers. */
703 static void
704 output_call_frame_info (int for_eh)
706 unsigned int i;
707 dw_fde_ref fde;
708 dw_cfi_ref cfi;
709 char l1[20], l2[20], section_start_label[20];
710 bool any_lsda_needed = false;
711 char augmentation[6];
712 int augmentation_size;
713 int fde_encoding = DW_EH_PE_absptr;
714 int per_encoding = DW_EH_PE_absptr;
715 int lsda_encoding = DW_EH_PE_absptr;
716 int return_reg;
717 rtx personality = NULL;
718 int dw_cie_version;
720 /* Don't emit a CIE if there won't be any FDEs. */
721 if (!fde_vec)
722 return;
724 /* Nothing to do if the assembler's doing it all. */
725 if (dwarf2out_do_cfi_asm ())
726 return;
728 /* If we don't have any functions we'll want to unwind out of, don't emit
729 any EH unwind information. If we make FDEs linkonce, we may have to
730 emit an empty label for an FDE that wouldn't otherwise be emitted. We
731 want to avoid having an FDE kept around when the function it refers to
732 is discarded. Example where this matters: a primary function template
733 in C++ requires EH information, an explicit specialization doesn't. */
734 if (for_eh)
736 bool any_eh_needed = false;
738 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
740 if (fde->uses_eh_lsda)
741 any_eh_needed = any_lsda_needed = true;
742 else if (fde_needed_for_eh_p (fde))
743 any_eh_needed = true;
744 else if (TARGET_USES_WEAK_UNWIND_INFO)
745 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
748 if (!any_eh_needed)
749 return;
752 /* We're going to be generating comments, so turn on app. */
753 if (flag_debug_asm)
754 app_enable ();
756 /* Switch to the proper frame section, first time. */
757 switch_to_frame_table_section (for_eh, false);
759 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
760 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
762 /* Output the CIE. */
763 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
764 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
765 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
766 dw2_asm_output_data (4, 0xffffffff,
767 "Initial length escape value indicating 64-bit DWARF extension");
768 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
769 "Length of Common Information Entry");
770 ASM_OUTPUT_LABEL (asm_out_file, l1);
772 /* Now that the CIE pointer is PC-relative for EH,
773 use 0 to identify the CIE. */
774 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
775 (for_eh ? 0 : DWARF_CIE_ID),
776 "CIE Identifier Tag");
778 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
779 use CIE version 1, unless that would produce incorrect results
780 due to overflowing the return register column. */
781 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
782 dw_cie_version = 1;
783 if (return_reg >= 256 || dwarf_version > 2)
784 dw_cie_version = 3;
785 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
787 augmentation[0] = 0;
788 augmentation_size = 0;
790 personality = current_unit_personality;
791 if (for_eh)
793 char *p;
795 /* Augmentation:
796 z Indicates that a uleb128 is present to size the
797 augmentation section.
798 L Indicates the encoding (and thus presence) of
799 an LSDA pointer in the FDE augmentation.
800 R Indicates a non-default pointer encoding for
801 FDE code pointers.
802 P Indicates the presence of an encoding + language
803 personality routine in the CIE augmentation. */
805 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
806 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
807 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
809 p = augmentation + 1;
810 if (personality)
812 *p++ = 'P';
813 augmentation_size += 1 + size_of_encoded_value (per_encoding);
814 assemble_external_libcall (personality);
816 if (any_lsda_needed)
818 *p++ = 'L';
819 augmentation_size += 1;
821 if (fde_encoding != DW_EH_PE_absptr)
823 *p++ = 'R';
824 augmentation_size += 1;
826 if (p > augmentation + 1)
828 augmentation[0] = 'z';
829 *p = '\0';
832 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
833 if (personality && per_encoding == DW_EH_PE_aligned)
835 int offset = ( 4 /* Length */
836 + 4 /* CIE Id */
837 + 1 /* CIE version */
838 + strlen (augmentation) + 1 /* Augmentation */
839 + size_of_uleb128 (1) /* Code alignment */
840 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
841 + 1 /* RA column */
842 + 1 /* Augmentation size */
843 + 1 /* Personality encoding */ );
844 int pad = -offset & (PTR_SIZE - 1);
846 augmentation_size += pad;
848 /* Augmentations should be small, so there's scarce need to
849 iterate for a solution. Die if we exceed one uleb128 byte. */
850 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
854 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
855 if (dw_cie_version >= 4)
857 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
858 dw2_asm_output_data (1, 0, "CIE Segment Size");
860 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
861 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
862 "CIE Data Alignment Factor");
864 if (dw_cie_version == 1)
865 dw2_asm_output_data (1, return_reg, "CIE RA Column");
866 else
867 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
869 if (augmentation[0])
871 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
872 if (personality)
874 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
875 eh_data_format_name (per_encoding));
876 dw2_asm_output_encoded_addr_rtx (per_encoding,
877 personality,
878 true, NULL);
881 if (any_lsda_needed)
882 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
883 eh_data_format_name (lsda_encoding));
885 if (fde_encoding != DW_EH_PE_absptr)
886 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
887 eh_data_format_name (fde_encoding));
890 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
891 output_cfi (cfi, NULL, for_eh);
893 /* Pad the CIE out to an address sized boundary. */
894 ASM_OUTPUT_ALIGN (asm_out_file,
895 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
896 ASM_OUTPUT_LABEL (asm_out_file, l2);
898 /* Loop through all of the FDE's. */
899 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
901 unsigned int k;
903 /* Don't emit EH unwind info for leaf functions that don't need it. */
904 if (for_eh && !fde_needed_for_eh_p (fde))
905 continue;
907 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
908 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
909 augmentation, any_lsda_needed, lsda_encoding);
912 if (for_eh && targetm.terminate_dw2_eh_frame_info)
913 dw2_asm_output_data (4, 0, "End of Table");
915 /* Turn off app to make assembly quicker. */
916 if (flag_debug_asm)
917 app_disable ();
920 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
922 static void
923 dwarf2out_do_cfi_startproc (bool second)
925 int enc;
926 rtx ref;
927 rtx personality = get_personality_function (current_function_decl);
929 fprintf (asm_out_file, "\t.cfi_startproc\n");
931 if (personality)
933 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
934 ref = personality;
936 /* ??? The GAS support isn't entirely consistent. We have to
937 handle indirect support ourselves, but PC-relative is done
938 in the assembler. Further, the assembler can't handle any
939 of the weirder relocation types. */
940 if (enc & DW_EH_PE_indirect)
941 ref = dw2_force_const_mem (ref, true);
943 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
944 output_addr_const (asm_out_file, ref);
945 fputc ('\n', asm_out_file);
948 if (crtl->uses_eh_lsda)
950 char lab[20];
952 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
953 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
954 current_function_funcdef_no);
955 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
956 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
958 if (enc & DW_EH_PE_indirect)
959 ref = dw2_force_const_mem (ref, true);
961 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
962 output_addr_const (asm_out_file, ref);
963 fputc ('\n', asm_out_file);
967 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
968 this allocation may be done before pass_final. */
970 dw_fde_ref
971 dwarf2out_alloc_current_fde (void)
973 dw_fde_ref fde;
975 fde = ggc_alloc_cleared_dw_fde_node ();
976 fde->decl = current_function_decl;
977 fde->funcdef_number = current_function_funcdef_no;
978 fde->fde_index = vec_safe_length (fde_vec);
979 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
980 fde->uses_eh_lsda = crtl->uses_eh_lsda;
981 fde->nothrow = crtl->nothrow;
982 fde->drap_reg = INVALID_REGNUM;
983 fde->vdrap_reg = INVALID_REGNUM;
985 /* Record the FDE associated with this function. */
986 cfun->fde = fde;
987 vec_safe_push (fde_vec, fde);
989 return fde;
992 /* Output a marker (i.e. a label) for the beginning of a function, before
993 the prologue. */
995 void
996 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
997 const char *file ATTRIBUTE_UNUSED)
999 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1000 char * dup_label;
1001 dw_fde_ref fde;
1002 section *fnsec;
1003 bool do_frame;
1005 current_function_func_begin_label = NULL;
1007 do_frame = dwarf2out_do_frame ();
1009 /* ??? current_function_func_begin_label is also used by except.c for
1010 call-site information. We must emit this label if it might be used. */
1011 if (!do_frame
1012 && (!flag_exceptions
1013 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1014 return;
1016 fnsec = function_section (current_function_decl);
1017 switch_to_section (fnsec);
1018 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1019 current_function_funcdef_no);
1020 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1021 current_function_funcdef_no);
1022 dup_label = xstrdup (label);
1023 current_function_func_begin_label = dup_label;
1025 /* We can elide the fde allocation if we're not emitting debug info. */
1026 if (!do_frame)
1027 return;
1029 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1030 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1031 would include pass_dwarf2_frame. If we've not created the FDE yet,
1032 do so now. */
1033 fde = cfun->fde;
1034 if (fde == NULL)
1035 fde = dwarf2out_alloc_current_fde ();
1037 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1038 fde->dw_fde_begin = dup_label;
1039 fde->dw_fde_current_label = dup_label;
1040 fde->in_std_section = (fnsec == text_section
1041 || (cold_text_section && fnsec == cold_text_section));
1043 /* We only want to output line number information for the genuine dwarf2
1044 prologue case, not the eh frame case. */
1045 #ifdef DWARF2_DEBUGGING_INFO
1046 if (file)
1047 dwarf2out_source_line (line, file, 0, true);
1048 #endif
1050 if (dwarf2out_do_cfi_asm ())
1051 dwarf2out_do_cfi_startproc (false);
1052 else
1054 rtx personality = get_personality_function (current_function_decl);
1055 if (!current_unit_personality)
1056 current_unit_personality = personality;
1058 /* We cannot keep a current personality per function as without CFI
1059 asm, at the point where we emit the CFI data, there is no current
1060 function anymore. */
1061 if (personality && current_unit_personality != personality)
1062 sorry ("multiple EH personalities are supported only with assemblers "
1063 "supporting .cfi_personality directive");
1067 /* Output a marker (i.e. a label) for the end of the generated code
1068 for a function prologue. This gets called *after* the prologue code has
1069 been generated. */
1071 void
1072 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1073 const char *file ATTRIBUTE_UNUSED)
1075 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1077 /* Output a label to mark the endpoint of the code generated for this
1078 function. */
1079 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1080 current_function_funcdef_no);
1081 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1082 current_function_funcdef_no);
1083 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1086 /* Output a marker (i.e. a label) for the beginning of the generated code
1087 for a function epilogue. This gets called *before* the prologue code has
1088 been generated. */
1090 void
1091 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1092 const char *file ATTRIBUTE_UNUSED)
1094 dw_fde_ref fde = cfun->fde;
1095 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1097 if (fde->dw_fde_vms_begin_epilogue)
1098 return;
1100 /* Output a label to mark the endpoint of the code generated for this
1101 function. */
1102 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1103 current_function_funcdef_no);
1104 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1105 current_function_funcdef_no);
1106 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1109 /* Output a marker (i.e. a label) for the absolute end of the generated code
1110 for a function definition. This gets called *after* the epilogue code has
1111 been generated. */
1113 void
1114 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1115 const char *file ATTRIBUTE_UNUSED)
1117 dw_fde_ref fde;
1118 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1120 last_var_location_insn = NULL_RTX;
1121 cached_next_real_insn = NULL_RTX;
1123 if (dwarf2out_do_cfi_asm ())
1124 fprintf (asm_out_file, "\t.cfi_endproc\n");
1126 /* Output a label to mark the endpoint of the code generated for this
1127 function. */
1128 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1129 current_function_funcdef_no);
1130 ASM_OUTPUT_LABEL (asm_out_file, label);
1131 fde = cfun->fde;
1132 gcc_assert (fde != NULL);
1133 if (fde->dw_fde_second_begin == NULL)
1134 fde->dw_fde_end = xstrdup (label);
1137 void
1138 dwarf2out_frame_finish (void)
1140 /* Output call frame information. */
1141 if (targetm.debug_unwind_info () == UI_DWARF2)
1142 output_call_frame_info (0);
1144 /* Output another copy for the unwinder. */
1145 if ((flag_unwind_tables || flag_exceptions)
1146 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1147 output_call_frame_info (1);
1150 /* Note that the current function section is being used for code. */
1152 static void
1153 dwarf2out_note_section_used (void)
1155 section *sec = current_function_section ();
1156 if (sec == text_section)
1157 text_section_used = true;
1158 else if (sec == cold_text_section)
1159 cold_text_section_used = true;
1162 static void var_location_switch_text_section (void);
1163 static void set_cur_line_info_table (section *);
1165 void
1166 dwarf2out_switch_text_section (void)
1168 section *sect;
1169 dw_fde_ref fde = cfun->fde;
1171 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1173 if (!in_cold_section_p)
1175 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1176 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1177 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1179 else
1181 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1182 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1183 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1185 have_multiple_function_sections = true;
1187 /* There is no need to mark used sections when not debugging. */
1188 if (cold_text_section != NULL)
1189 dwarf2out_note_section_used ();
1191 if (dwarf2out_do_cfi_asm ())
1192 fprintf (asm_out_file, "\t.cfi_endproc\n");
1194 /* Now do the real section switch. */
1195 sect = current_function_section ();
1196 switch_to_section (sect);
1198 fde->second_in_std_section
1199 = (sect == text_section
1200 || (cold_text_section && sect == cold_text_section));
1202 if (dwarf2out_do_cfi_asm ())
1203 dwarf2out_do_cfi_startproc (true);
1205 var_location_switch_text_section ();
1207 if (cold_text_section != NULL)
1208 set_cur_line_info_table (sect);
1211 /* And now, the subset of the debugging information support code necessary
1212 for emitting location expressions. */
1214 /* Data about a single source file. */
1215 struct GTY(()) dwarf_file_data {
1216 const char * filename;
1217 int emitted_number;
1220 typedef struct GTY(()) deferred_locations_struct
1222 tree variable;
1223 dw_die_ref die;
1224 } deferred_locations;
1227 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1230 /* Describe an entry into the .debug_addr section. */
1232 enum ate_kind {
1233 ate_kind_rtx,
1234 ate_kind_rtx_dtprel,
1235 ate_kind_label
1238 typedef struct GTY(()) addr_table_entry_struct {
1239 enum ate_kind kind;
1240 unsigned int refcount;
1241 unsigned int index;
1242 union addr_table_entry_struct_union
1244 rtx GTY ((tag ("0"))) rtl;
1245 char * GTY ((tag ("1"))) label;
1247 GTY ((desc ("%1.kind"))) addr;
1249 addr_table_entry;
1251 /* Location lists are ranges + location descriptions for that range,
1252 so you can track variables that are in different places over
1253 their entire life. */
1254 typedef struct GTY(()) dw_loc_list_struct {
1255 dw_loc_list_ref dw_loc_next;
1256 const char *begin; /* Label and addr_entry for start of range */
1257 addr_table_entry *begin_entry;
1258 const char *end; /* Label for end of range */
1259 char *ll_symbol; /* Label for beginning of location list.
1260 Only on head of list */
1261 const char *section; /* Section this loclist is relative to */
1262 dw_loc_descr_ref expr;
1263 hashval_t hash;
1264 /* True if all addresses in this and subsequent lists are known to be
1265 resolved. */
1266 bool resolved_addr;
1267 /* True if this list has been replaced by dw_loc_next. */
1268 bool replaced;
1269 bool emitted;
1270 /* True if the range should be emitted even if begin and end
1271 are the same. */
1272 bool force;
1273 } dw_loc_list_node;
1275 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1277 /* Convert a DWARF stack opcode into its string name. */
1279 static const char *
1280 dwarf_stack_op_name (unsigned int op)
1282 const char *name = get_DW_OP_name (op);
1284 if (name != NULL)
1285 return name;
1287 return "OP_<unknown>";
1290 /* Return a pointer to a newly allocated location description. Location
1291 descriptions are simple expression terms that can be strung
1292 together to form more complicated location (address) descriptions. */
1294 static inline dw_loc_descr_ref
1295 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1296 unsigned HOST_WIDE_INT oprnd2)
1298 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1300 descr->dw_loc_opc = op;
1301 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1302 descr->dw_loc_oprnd1.val_entry = NULL;
1303 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1304 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1305 descr->dw_loc_oprnd2.val_entry = NULL;
1306 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1308 return descr;
1311 /* Return a pointer to a newly allocated location description for
1312 REG and OFFSET. */
1314 static inline dw_loc_descr_ref
1315 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1317 if (reg <= 31)
1318 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1319 offset, 0);
1320 else
1321 return new_loc_descr (DW_OP_bregx, reg, offset);
1324 /* Add a location description term to a location description expression. */
1326 static inline void
1327 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1329 dw_loc_descr_ref *d;
1331 /* Find the end of the chain. */
1332 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1335 *d = descr;
1338 /* Compare two location operands for exact equality. */
1340 static bool
1341 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1343 if (a->val_class != b->val_class)
1344 return false;
1345 switch (a->val_class)
1347 case dw_val_class_none:
1348 return true;
1349 case dw_val_class_addr:
1350 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1352 case dw_val_class_offset:
1353 case dw_val_class_unsigned_const:
1354 case dw_val_class_const:
1355 case dw_val_class_range_list:
1356 case dw_val_class_lineptr:
1357 case dw_val_class_macptr:
1358 /* These are all HOST_WIDE_INT, signed or unsigned. */
1359 return a->v.val_unsigned == b->v.val_unsigned;
1361 case dw_val_class_loc:
1362 return a->v.val_loc == b->v.val_loc;
1363 case dw_val_class_loc_list:
1364 return a->v.val_loc_list == b->v.val_loc_list;
1365 case dw_val_class_die_ref:
1366 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1367 case dw_val_class_fde_ref:
1368 return a->v.val_fde_index == b->v.val_fde_index;
1369 case dw_val_class_lbl_id:
1370 case dw_val_class_high_pc:
1371 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1372 case dw_val_class_str:
1373 return a->v.val_str == b->v.val_str;
1374 case dw_val_class_flag:
1375 return a->v.val_flag == b->v.val_flag;
1376 case dw_val_class_file:
1377 return a->v.val_file == b->v.val_file;
1378 case dw_val_class_decl_ref:
1379 return a->v.val_decl_ref == b->v.val_decl_ref;
1381 case dw_val_class_const_double:
1382 return (a->v.val_double.high == b->v.val_double.high
1383 && a->v.val_double.low == b->v.val_double.low);
1385 case dw_val_class_vec:
1387 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1388 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1390 return (a_len == b_len
1391 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1394 case dw_val_class_data8:
1395 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1397 case dw_val_class_vms_delta:
1398 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1399 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1401 gcc_unreachable ();
1404 /* Compare two location atoms for exact equality. */
1406 static bool
1407 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1409 if (a->dw_loc_opc != b->dw_loc_opc)
1410 return false;
1412 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1413 address size, but since we always allocate cleared storage it
1414 should be zero for other types of locations. */
1415 if (a->dtprel != b->dtprel)
1416 return false;
1418 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1419 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1422 /* Compare two complete location expressions for exact equality. */
1424 bool
1425 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1427 while (1)
1429 if (a == b)
1430 return true;
1431 if (a == NULL || b == NULL)
1432 return false;
1433 if (!loc_descr_equal_p_1 (a, b))
1434 return false;
1436 a = a->dw_loc_next;
1437 b = b->dw_loc_next;
1442 /* Add a constant OFFSET to a location expression. */
1444 static void
1445 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1447 dw_loc_descr_ref loc;
1448 HOST_WIDE_INT *p;
1450 gcc_assert (*list_head != NULL);
1452 if (!offset)
1453 return;
1455 /* Find the end of the chain. */
1456 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1459 p = NULL;
1460 if (loc->dw_loc_opc == DW_OP_fbreg
1461 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1462 p = &loc->dw_loc_oprnd1.v.val_int;
1463 else if (loc->dw_loc_opc == DW_OP_bregx)
1464 p = &loc->dw_loc_oprnd2.v.val_int;
1466 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1467 offset. Don't optimize if an signed integer overflow would happen. */
1468 if (p != NULL
1469 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1470 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1471 *p += offset;
1473 else if (offset > 0)
1474 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1476 else
1478 loc->dw_loc_next = int_loc_descriptor (-offset);
1479 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1483 /* Add a constant OFFSET to a location list. */
1485 static void
1486 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1488 dw_loc_list_ref d;
1489 for (d = list_head; d != NULL; d = d->dw_loc_next)
1490 loc_descr_plus_const (&d->expr, offset);
1493 #define DWARF_REF_SIZE \
1494 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1496 static unsigned long int get_base_type_offset (dw_die_ref);
1498 /* Return the size of a location descriptor. */
1500 static unsigned long
1501 size_of_loc_descr (dw_loc_descr_ref loc)
1503 unsigned long size = 1;
1505 switch (loc->dw_loc_opc)
1507 case DW_OP_addr:
1508 size += DWARF2_ADDR_SIZE;
1509 break;
1510 case DW_OP_GNU_addr_index:
1511 case DW_OP_GNU_const_index:
1512 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1513 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1514 break;
1515 case DW_OP_const1u:
1516 case DW_OP_const1s:
1517 size += 1;
1518 break;
1519 case DW_OP_const2u:
1520 case DW_OP_const2s:
1521 size += 2;
1522 break;
1523 case DW_OP_const4u:
1524 case DW_OP_const4s:
1525 size += 4;
1526 break;
1527 case DW_OP_const8u:
1528 case DW_OP_const8s:
1529 size += 8;
1530 break;
1531 case DW_OP_constu:
1532 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1533 break;
1534 case DW_OP_consts:
1535 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1536 break;
1537 case DW_OP_pick:
1538 size += 1;
1539 break;
1540 case DW_OP_plus_uconst:
1541 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1542 break;
1543 case DW_OP_skip:
1544 case DW_OP_bra:
1545 size += 2;
1546 break;
1547 case DW_OP_breg0:
1548 case DW_OP_breg1:
1549 case DW_OP_breg2:
1550 case DW_OP_breg3:
1551 case DW_OP_breg4:
1552 case DW_OP_breg5:
1553 case DW_OP_breg6:
1554 case DW_OP_breg7:
1555 case DW_OP_breg8:
1556 case DW_OP_breg9:
1557 case DW_OP_breg10:
1558 case DW_OP_breg11:
1559 case DW_OP_breg12:
1560 case DW_OP_breg13:
1561 case DW_OP_breg14:
1562 case DW_OP_breg15:
1563 case DW_OP_breg16:
1564 case DW_OP_breg17:
1565 case DW_OP_breg18:
1566 case DW_OP_breg19:
1567 case DW_OP_breg20:
1568 case DW_OP_breg21:
1569 case DW_OP_breg22:
1570 case DW_OP_breg23:
1571 case DW_OP_breg24:
1572 case DW_OP_breg25:
1573 case DW_OP_breg26:
1574 case DW_OP_breg27:
1575 case DW_OP_breg28:
1576 case DW_OP_breg29:
1577 case DW_OP_breg30:
1578 case DW_OP_breg31:
1579 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1580 break;
1581 case DW_OP_regx:
1582 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1583 break;
1584 case DW_OP_fbreg:
1585 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1586 break;
1587 case DW_OP_bregx:
1588 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1589 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1590 break;
1591 case DW_OP_piece:
1592 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1593 break;
1594 case DW_OP_bit_piece:
1595 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1596 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1597 break;
1598 case DW_OP_deref_size:
1599 case DW_OP_xderef_size:
1600 size += 1;
1601 break;
1602 case DW_OP_call2:
1603 size += 2;
1604 break;
1605 case DW_OP_call4:
1606 size += 4;
1607 break;
1608 case DW_OP_call_ref:
1609 size += DWARF_REF_SIZE;
1610 break;
1611 case DW_OP_implicit_value:
1612 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1613 + loc->dw_loc_oprnd1.v.val_unsigned;
1614 break;
1615 case DW_OP_GNU_implicit_pointer:
1616 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1617 break;
1618 case DW_OP_GNU_entry_value:
1620 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1621 size += size_of_uleb128 (op_size) + op_size;
1622 break;
1624 case DW_OP_GNU_const_type:
1626 unsigned long o
1627 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1628 size += size_of_uleb128 (o) + 1;
1629 switch (loc->dw_loc_oprnd2.val_class)
1631 case dw_val_class_vec:
1632 size += loc->dw_loc_oprnd2.v.val_vec.length
1633 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1634 break;
1635 case dw_val_class_const:
1636 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1637 break;
1638 case dw_val_class_const_double:
1639 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1640 break;
1641 default:
1642 gcc_unreachable ();
1644 break;
1646 case DW_OP_GNU_regval_type:
1648 unsigned long o
1649 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1650 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1651 + size_of_uleb128 (o);
1653 break;
1654 case DW_OP_GNU_deref_type:
1656 unsigned long o
1657 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1658 size += 1 + size_of_uleb128 (o);
1660 break;
1661 case DW_OP_GNU_convert:
1662 case DW_OP_GNU_reinterpret:
1663 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1664 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1665 else
1667 unsigned long o
1668 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1669 size += size_of_uleb128 (o);
1671 break;
1672 case DW_OP_GNU_parameter_ref:
1673 size += 4;
1674 break;
1675 default:
1676 break;
1679 return size;
1682 /* Return the size of a series of location descriptors. */
1684 unsigned long
1685 size_of_locs (dw_loc_descr_ref loc)
1687 dw_loc_descr_ref l;
1688 unsigned long size;
1690 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1691 field, to avoid writing to a PCH file. */
1692 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1694 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1695 break;
1696 size += size_of_loc_descr (l);
1698 if (! l)
1699 return size;
1701 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1703 l->dw_loc_addr = size;
1704 size += size_of_loc_descr (l);
1707 return size;
1710 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1711 static void get_ref_die_offset_label (char *, dw_die_ref);
1712 static unsigned long int get_ref_die_offset (dw_die_ref);
1714 /* Output location description stack opcode's operands (if any).
1715 The for_eh_or_skip parameter controls whether register numbers are
1716 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1717 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1718 info). This should be suppressed for the cases that have not been converted
1719 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1721 static void
1722 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1724 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1725 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1727 switch (loc->dw_loc_opc)
1729 #ifdef DWARF2_DEBUGGING_INFO
1730 case DW_OP_const2u:
1731 case DW_OP_const2s:
1732 dw2_asm_output_data (2, val1->v.val_int, NULL);
1733 break;
1734 case DW_OP_const4u:
1735 if (loc->dtprel)
1737 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1738 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1739 val1->v.val_addr);
1740 fputc ('\n', asm_out_file);
1741 break;
1743 /* FALLTHRU */
1744 case DW_OP_const4s:
1745 dw2_asm_output_data (4, val1->v.val_int, NULL);
1746 break;
1747 case DW_OP_const8u:
1748 if (loc->dtprel)
1750 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1751 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1752 val1->v.val_addr);
1753 fputc ('\n', asm_out_file);
1754 break;
1756 /* FALLTHRU */
1757 case DW_OP_const8s:
1758 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1759 dw2_asm_output_data (8, val1->v.val_int, NULL);
1760 break;
1761 case DW_OP_skip:
1762 case DW_OP_bra:
1764 int offset;
1766 gcc_assert (val1->val_class == dw_val_class_loc);
1767 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1769 dw2_asm_output_data (2, offset, NULL);
1771 break;
1772 case DW_OP_implicit_value:
1773 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1774 switch (val2->val_class)
1776 case dw_val_class_const:
1777 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1778 break;
1779 case dw_val_class_vec:
1781 unsigned int elt_size = val2->v.val_vec.elt_size;
1782 unsigned int len = val2->v.val_vec.length;
1783 unsigned int i;
1784 unsigned char *p;
1786 if (elt_size > sizeof (HOST_WIDE_INT))
1788 elt_size /= 2;
1789 len *= 2;
1791 for (i = 0, p = val2->v.val_vec.array;
1792 i < len;
1793 i++, p += elt_size)
1794 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1795 "fp or vector constant word %u", i);
1797 break;
1798 case dw_val_class_const_double:
1800 unsigned HOST_WIDE_INT first, second;
1802 if (WORDS_BIG_ENDIAN)
1804 first = val2->v.val_double.high;
1805 second = val2->v.val_double.low;
1807 else
1809 first = val2->v.val_double.low;
1810 second = val2->v.val_double.high;
1812 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1813 first, NULL);
1814 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1815 second, NULL);
1817 break;
1818 case dw_val_class_addr:
1819 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1820 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1821 break;
1822 default:
1823 gcc_unreachable ();
1825 break;
1826 #else
1827 case DW_OP_const2u:
1828 case DW_OP_const2s:
1829 case DW_OP_const4u:
1830 case DW_OP_const4s:
1831 case DW_OP_const8u:
1832 case DW_OP_const8s:
1833 case DW_OP_skip:
1834 case DW_OP_bra:
1835 case DW_OP_implicit_value:
1836 /* We currently don't make any attempt to make sure these are
1837 aligned properly like we do for the main unwind info, so
1838 don't support emitting things larger than a byte if we're
1839 only doing unwinding. */
1840 gcc_unreachable ();
1841 #endif
1842 case DW_OP_const1u:
1843 case DW_OP_const1s:
1844 dw2_asm_output_data (1, val1->v.val_int, NULL);
1845 break;
1846 case DW_OP_constu:
1847 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1848 break;
1849 case DW_OP_consts:
1850 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1851 break;
1852 case DW_OP_pick:
1853 dw2_asm_output_data (1, val1->v.val_int, NULL);
1854 break;
1855 case DW_OP_plus_uconst:
1856 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1857 break;
1858 case DW_OP_breg0:
1859 case DW_OP_breg1:
1860 case DW_OP_breg2:
1861 case DW_OP_breg3:
1862 case DW_OP_breg4:
1863 case DW_OP_breg5:
1864 case DW_OP_breg6:
1865 case DW_OP_breg7:
1866 case DW_OP_breg8:
1867 case DW_OP_breg9:
1868 case DW_OP_breg10:
1869 case DW_OP_breg11:
1870 case DW_OP_breg12:
1871 case DW_OP_breg13:
1872 case DW_OP_breg14:
1873 case DW_OP_breg15:
1874 case DW_OP_breg16:
1875 case DW_OP_breg17:
1876 case DW_OP_breg18:
1877 case DW_OP_breg19:
1878 case DW_OP_breg20:
1879 case DW_OP_breg21:
1880 case DW_OP_breg22:
1881 case DW_OP_breg23:
1882 case DW_OP_breg24:
1883 case DW_OP_breg25:
1884 case DW_OP_breg26:
1885 case DW_OP_breg27:
1886 case DW_OP_breg28:
1887 case DW_OP_breg29:
1888 case DW_OP_breg30:
1889 case DW_OP_breg31:
1890 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1891 break;
1892 case DW_OP_regx:
1894 unsigned r = val1->v.val_unsigned;
1895 if (for_eh_or_skip >= 0)
1896 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1897 gcc_assert (size_of_uleb128 (r)
1898 == size_of_uleb128 (val1->v.val_unsigned));
1899 dw2_asm_output_data_uleb128 (r, NULL);
1901 break;
1902 case DW_OP_fbreg:
1903 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1904 break;
1905 case DW_OP_bregx:
1907 unsigned r = val1->v.val_unsigned;
1908 if (for_eh_or_skip >= 0)
1909 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1910 gcc_assert (size_of_uleb128 (r)
1911 == size_of_uleb128 (val1->v.val_unsigned));
1912 dw2_asm_output_data_uleb128 (r, NULL);
1913 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1915 break;
1916 case DW_OP_piece:
1917 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1918 break;
1919 case DW_OP_bit_piece:
1920 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1921 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1922 break;
1923 case DW_OP_deref_size:
1924 case DW_OP_xderef_size:
1925 dw2_asm_output_data (1, val1->v.val_int, NULL);
1926 break;
1928 case DW_OP_addr:
1929 if (loc->dtprel)
1931 if (targetm.asm_out.output_dwarf_dtprel)
1933 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1934 DWARF2_ADDR_SIZE,
1935 val1->v.val_addr);
1936 fputc ('\n', asm_out_file);
1938 else
1939 gcc_unreachable ();
1941 else
1943 #ifdef DWARF2_DEBUGGING_INFO
1944 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1945 #else
1946 gcc_unreachable ();
1947 #endif
1949 break;
1951 case DW_OP_GNU_addr_index:
1952 case DW_OP_GNU_const_index:
1953 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1954 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1955 "(index into .debug_addr)");
1956 break;
1958 case DW_OP_GNU_implicit_pointer:
1960 char label[MAX_ARTIFICIAL_LABEL_BYTES
1961 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1962 gcc_assert (val1->val_class == dw_val_class_die_ref);
1963 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1964 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1965 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1967 break;
1969 case DW_OP_GNU_entry_value:
1970 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1971 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1972 break;
1974 case DW_OP_GNU_const_type:
1976 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1977 gcc_assert (o);
1978 dw2_asm_output_data_uleb128 (o, NULL);
1979 switch (val2->val_class)
1981 case dw_val_class_const:
1982 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1983 dw2_asm_output_data (1, l, NULL);
1984 dw2_asm_output_data (l, val2->v.val_int, NULL);
1985 break;
1986 case dw_val_class_vec:
1988 unsigned int elt_size = val2->v.val_vec.elt_size;
1989 unsigned int len = val2->v.val_vec.length;
1990 unsigned int i;
1991 unsigned char *p;
1993 l = len * elt_size;
1994 dw2_asm_output_data (1, l, NULL);
1995 if (elt_size > sizeof (HOST_WIDE_INT))
1997 elt_size /= 2;
1998 len *= 2;
2000 for (i = 0, p = val2->v.val_vec.array;
2001 i < len;
2002 i++, p += elt_size)
2003 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2004 "fp or vector constant word %u", i);
2006 break;
2007 case dw_val_class_const_double:
2009 unsigned HOST_WIDE_INT first, second;
2010 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2012 dw2_asm_output_data (1, 2 * l, NULL);
2013 if (WORDS_BIG_ENDIAN)
2015 first = val2->v.val_double.high;
2016 second = val2->v.val_double.low;
2018 else
2020 first = val2->v.val_double.low;
2021 second = val2->v.val_double.high;
2023 dw2_asm_output_data (l, first, NULL);
2024 dw2_asm_output_data (l, second, NULL);
2026 break;
2027 default:
2028 gcc_unreachable ();
2031 break;
2032 case DW_OP_GNU_regval_type:
2034 unsigned r = val1->v.val_unsigned;
2035 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2036 gcc_assert (o);
2037 if (for_eh_or_skip >= 0)
2039 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2040 gcc_assert (size_of_uleb128 (r)
2041 == size_of_uleb128 (val1->v.val_unsigned));
2043 dw2_asm_output_data_uleb128 (r, NULL);
2044 dw2_asm_output_data_uleb128 (o, NULL);
2046 break;
2047 case DW_OP_GNU_deref_type:
2049 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2050 gcc_assert (o);
2051 dw2_asm_output_data (1, val1->v.val_int, NULL);
2052 dw2_asm_output_data_uleb128 (o, NULL);
2054 break;
2055 case DW_OP_GNU_convert:
2056 case DW_OP_GNU_reinterpret:
2057 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2058 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2059 else
2061 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2062 gcc_assert (o);
2063 dw2_asm_output_data_uleb128 (o, NULL);
2065 break;
2067 case DW_OP_GNU_parameter_ref:
2069 unsigned long o;
2070 gcc_assert (val1->val_class == dw_val_class_die_ref);
2071 o = get_ref_die_offset (val1->v.val_die_ref.die);
2072 dw2_asm_output_data (4, o, NULL);
2074 break;
2076 default:
2077 /* Other codes have no operands. */
2078 break;
2082 /* Output a sequence of location operations.
2083 The for_eh_or_skip parameter controls whether register numbers are
2084 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2085 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2086 info). This should be suppressed for the cases that have not been converted
2087 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2089 void
2090 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2092 for (; loc != NULL; loc = loc->dw_loc_next)
2094 enum dwarf_location_atom opc = loc->dw_loc_opc;
2095 /* Output the opcode. */
2096 if (for_eh_or_skip >= 0
2097 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2099 unsigned r = (opc - DW_OP_breg0);
2100 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2101 gcc_assert (r <= 31);
2102 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2104 else if (for_eh_or_skip >= 0
2105 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2107 unsigned r = (opc - DW_OP_reg0);
2108 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2109 gcc_assert (r <= 31);
2110 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2113 dw2_asm_output_data (1, opc,
2114 "%s", dwarf_stack_op_name (opc));
2116 /* Output the operand(s) (if any). */
2117 output_loc_operands (loc, for_eh_or_skip);
2121 /* Output location description stack opcode's operands (if any).
2122 The output is single bytes on a line, suitable for .cfi_escape. */
2124 static void
2125 output_loc_operands_raw (dw_loc_descr_ref loc)
2127 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2128 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2130 switch (loc->dw_loc_opc)
2132 case DW_OP_addr:
2133 case DW_OP_GNU_addr_index:
2134 case DW_OP_GNU_const_index:
2135 case DW_OP_implicit_value:
2136 /* We cannot output addresses in .cfi_escape, only bytes. */
2137 gcc_unreachable ();
2139 case DW_OP_const1u:
2140 case DW_OP_const1s:
2141 case DW_OP_pick:
2142 case DW_OP_deref_size:
2143 case DW_OP_xderef_size:
2144 fputc (',', asm_out_file);
2145 dw2_asm_output_data_raw (1, val1->v.val_int);
2146 break;
2148 case DW_OP_const2u:
2149 case DW_OP_const2s:
2150 fputc (',', asm_out_file);
2151 dw2_asm_output_data_raw (2, val1->v.val_int);
2152 break;
2154 case DW_OP_const4u:
2155 case DW_OP_const4s:
2156 fputc (',', asm_out_file);
2157 dw2_asm_output_data_raw (4, val1->v.val_int);
2158 break;
2160 case DW_OP_const8u:
2161 case DW_OP_const8s:
2162 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2163 fputc (',', asm_out_file);
2164 dw2_asm_output_data_raw (8, val1->v.val_int);
2165 break;
2167 case DW_OP_skip:
2168 case DW_OP_bra:
2170 int offset;
2172 gcc_assert (val1->val_class == dw_val_class_loc);
2173 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2175 fputc (',', asm_out_file);
2176 dw2_asm_output_data_raw (2, offset);
2178 break;
2180 case DW_OP_regx:
2182 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2183 gcc_assert (size_of_uleb128 (r)
2184 == size_of_uleb128 (val1->v.val_unsigned));
2185 fputc (',', asm_out_file);
2186 dw2_asm_output_data_uleb128_raw (r);
2188 break;
2190 case DW_OP_constu:
2191 case DW_OP_plus_uconst:
2192 case DW_OP_piece:
2193 fputc (',', asm_out_file);
2194 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2195 break;
2197 case DW_OP_bit_piece:
2198 fputc (',', asm_out_file);
2199 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2200 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2201 break;
2203 case DW_OP_consts:
2204 case DW_OP_breg0:
2205 case DW_OP_breg1:
2206 case DW_OP_breg2:
2207 case DW_OP_breg3:
2208 case DW_OP_breg4:
2209 case DW_OP_breg5:
2210 case DW_OP_breg6:
2211 case DW_OP_breg7:
2212 case DW_OP_breg8:
2213 case DW_OP_breg9:
2214 case DW_OP_breg10:
2215 case DW_OP_breg11:
2216 case DW_OP_breg12:
2217 case DW_OP_breg13:
2218 case DW_OP_breg14:
2219 case DW_OP_breg15:
2220 case DW_OP_breg16:
2221 case DW_OP_breg17:
2222 case DW_OP_breg18:
2223 case DW_OP_breg19:
2224 case DW_OP_breg20:
2225 case DW_OP_breg21:
2226 case DW_OP_breg22:
2227 case DW_OP_breg23:
2228 case DW_OP_breg24:
2229 case DW_OP_breg25:
2230 case DW_OP_breg26:
2231 case DW_OP_breg27:
2232 case DW_OP_breg28:
2233 case DW_OP_breg29:
2234 case DW_OP_breg30:
2235 case DW_OP_breg31:
2236 case DW_OP_fbreg:
2237 fputc (',', asm_out_file);
2238 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2239 break;
2241 case DW_OP_bregx:
2243 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2244 gcc_assert (size_of_uleb128 (r)
2245 == size_of_uleb128 (val1->v.val_unsigned));
2246 fputc (',', asm_out_file);
2247 dw2_asm_output_data_uleb128_raw (r);
2248 fputc (',', asm_out_file);
2249 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2251 break;
2253 case DW_OP_GNU_implicit_pointer:
2254 case DW_OP_GNU_entry_value:
2255 case DW_OP_GNU_const_type:
2256 case DW_OP_GNU_regval_type:
2257 case DW_OP_GNU_deref_type:
2258 case DW_OP_GNU_convert:
2259 case DW_OP_GNU_reinterpret:
2260 case DW_OP_GNU_parameter_ref:
2261 gcc_unreachable ();
2262 break;
2264 default:
2265 /* Other codes have no operands. */
2266 break;
2270 void
2271 output_loc_sequence_raw (dw_loc_descr_ref loc)
2273 while (1)
2275 enum dwarf_location_atom opc = loc->dw_loc_opc;
2276 /* Output the opcode. */
2277 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2279 unsigned r = (opc - DW_OP_breg0);
2280 r = DWARF2_FRAME_REG_OUT (r, 1);
2281 gcc_assert (r <= 31);
2282 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2284 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2286 unsigned r = (opc - DW_OP_reg0);
2287 r = DWARF2_FRAME_REG_OUT (r, 1);
2288 gcc_assert (r <= 31);
2289 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2291 /* Output the opcode. */
2292 fprintf (asm_out_file, "%#x", opc);
2293 output_loc_operands_raw (loc);
2295 if (!loc->dw_loc_next)
2296 break;
2297 loc = loc->dw_loc_next;
2299 fputc (',', asm_out_file);
2303 /* This function builds a dwarf location descriptor sequence from a
2304 dw_cfa_location, adding the given OFFSET to the result of the
2305 expression. */
2307 struct dw_loc_descr_struct *
2308 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2310 struct dw_loc_descr_struct *head, *tmp;
2312 offset += cfa->offset;
2314 if (cfa->indirect)
2316 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2317 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2318 head->dw_loc_oprnd1.val_entry = NULL;
2319 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2320 add_loc_descr (&head, tmp);
2321 if (offset != 0)
2323 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2324 add_loc_descr (&head, tmp);
2327 else
2328 head = new_reg_loc_descr (cfa->reg, offset);
2330 return head;
2333 /* This function builds a dwarf location descriptor sequence for
2334 the address at OFFSET from the CFA when stack is aligned to
2335 ALIGNMENT byte. */
2337 struct dw_loc_descr_struct *
2338 build_cfa_aligned_loc (dw_cfa_location *cfa,
2339 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2341 struct dw_loc_descr_struct *head;
2342 unsigned int dwarf_fp
2343 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2345 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2346 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2348 head = new_reg_loc_descr (dwarf_fp, 0);
2349 add_loc_descr (&head, int_loc_descriptor (alignment));
2350 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2351 loc_descr_plus_const (&head, offset);
2353 else
2354 head = new_reg_loc_descr (dwarf_fp, offset);
2355 return head;
2358 /* And now, the support for symbolic debugging information. */
2360 /* .debug_str support. */
2361 static int output_indirect_string (void **, void *);
2363 static void dwarf2out_init (const char *);
2364 static void dwarf2out_finish (const char *);
2365 static void dwarf2out_assembly_start (void);
2366 static void dwarf2out_define (unsigned int, const char *);
2367 static void dwarf2out_undef (unsigned int, const char *);
2368 static void dwarf2out_start_source_file (unsigned, const char *);
2369 static void dwarf2out_end_source_file (unsigned);
2370 static void dwarf2out_function_decl (tree);
2371 static void dwarf2out_begin_block (unsigned, unsigned);
2372 static void dwarf2out_end_block (unsigned, unsigned);
2373 static bool dwarf2out_ignore_block (const_tree);
2374 static void dwarf2out_global_decl (tree);
2375 static void dwarf2out_type_decl (tree, int);
2376 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2377 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2378 dw_die_ref);
2379 static void dwarf2out_abstract_function (tree);
2380 static void dwarf2out_var_location (rtx);
2381 static void dwarf2out_begin_function (tree);
2382 static void dwarf2out_end_function (unsigned int);
2383 static void dwarf2out_set_name (tree, tree);
2385 /* The debug hooks structure. */
2387 const struct gcc_debug_hooks dwarf2_debug_hooks =
2389 dwarf2out_init,
2390 dwarf2out_finish,
2391 dwarf2out_assembly_start,
2392 dwarf2out_define,
2393 dwarf2out_undef,
2394 dwarf2out_start_source_file,
2395 dwarf2out_end_source_file,
2396 dwarf2out_begin_block,
2397 dwarf2out_end_block,
2398 dwarf2out_ignore_block,
2399 dwarf2out_source_line,
2400 dwarf2out_begin_prologue,
2401 #if VMS_DEBUGGING_INFO
2402 dwarf2out_vms_end_prologue,
2403 dwarf2out_vms_begin_epilogue,
2404 #else
2405 debug_nothing_int_charstar,
2406 debug_nothing_int_charstar,
2407 #endif
2408 dwarf2out_end_epilogue,
2409 dwarf2out_begin_function,
2410 dwarf2out_end_function, /* end_function */
2411 dwarf2out_function_decl, /* function_decl */
2412 dwarf2out_global_decl,
2413 dwarf2out_type_decl, /* type_decl */
2414 dwarf2out_imported_module_or_decl,
2415 debug_nothing_tree, /* deferred_inline_function */
2416 /* The DWARF 2 backend tries to reduce debugging bloat by not
2417 emitting the abstract description of inline functions until
2418 something tries to reference them. */
2419 dwarf2out_abstract_function, /* outlining_inline_function */
2420 debug_nothing_rtx, /* label */
2421 debug_nothing_int, /* handle_pch */
2422 dwarf2out_var_location,
2423 dwarf2out_switch_text_section,
2424 dwarf2out_set_name,
2425 1, /* start_end_main_source_file */
2426 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2429 /* NOTE: In the comments in this file, many references are made to
2430 "Debugging Information Entries". This term is abbreviated as `DIE'
2431 throughout the remainder of this file. */
2433 /* An internal representation of the DWARF output is built, and then
2434 walked to generate the DWARF debugging info. The walk of the internal
2435 representation is done after the entire program has been compiled.
2436 The types below are used to describe the internal representation. */
2438 /* Whether to put type DIEs into their own section .debug_types instead
2439 of making them part of the .debug_info section. Only supported for
2440 Dwarf V4 or higher and the user didn't disable them through
2441 -fno-debug-types-section. It is more efficient to put them in a
2442 separate comdat sections since the linker will then be able to
2443 remove duplicates. But not all tools support .debug_types sections
2444 yet. */
2446 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2448 /* Various DIE's use offsets relative to the beginning of the
2449 .debug_info section to refer to each other. */
2451 typedef long int dw_offset;
2453 /* Define typedefs here to avoid circular dependencies. */
2455 typedef struct dw_attr_struct *dw_attr_ref;
2456 typedef struct dw_line_info_struct *dw_line_info_ref;
2457 typedef struct pubname_struct *pubname_ref;
2458 typedef struct dw_ranges_struct *dw_ranges_ref;
2459 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2460 typedef struct comdat_type_struct *comdat_type_node_ref;
2462 /* The entries in the line_info table more-or-less mirror the opcodes
2463 that are used in the real dwarf line table. Arrays of these entries
2464 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2465 supported. */
2467 enum dw_line_info_opcode {
2468 /* Emit DW_LNE_set_address; the operand is the label index. */
2469 LI_set_address,
2471 /* Emit a row to the matrix with the given line. This may be done
2472 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2473 special opcodes. */
2474 LI_set_line,
2476 /* Emit a DW_LNS_set_file. */
2477 LI_set_file,
2479 /* Emit a DW_LNS_set_column. */
2480 LI_set_column,
2482 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2483 LI_negate_stmt,
2485 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2486 LI_set_prologue_end,
2487 LI_set_epilogue_begin,
2489 /* Emit a DW_LNE_set_discriminator. */
2490 LI_set_discriminator
2493 typedef struct GTY(()) dw_line_info_struct {
2494 enum dw_line_info_opcode opcode;
2495 unsigned int val;
2496 } dw_line_info_entry;
2499 typedef struct GTY(()) dw_line_info_table_struct {
2500 /* The label that marks the end of this section. */
2501 const char *end_label;
2503 /* The values for the last row of the matrix, as collected in the table.
2504 These are used to minimize the changes to the next row. */
2505 unsigned int file_num;
2506 unsigned int line_num;
2507 unsigned int column_num;
2508 int discrim_num;
2509 bool is_stmt;
2510 bool in_use;
2512 vec<dw_line_info_entry, va_gc> *entries;
2513 } dw_line_info_table;
2515 typedef dw_line_info_table *dw_line_info_table_p;
2518 /* Each DIE attribute has a field specifying the attribute kind,
2519 a link to the next attribute in the chain, and an attribute value.
2520 Attributes are typically linked below the DIE they modify. */
2522 typedef struct GTY(()) dw_attr_struct {
2523 enum dwarf_attribute dw_attr;
2524 dw_val_node dw_attr_val;
2526 dw_attr_node;
2529 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2530 The children of each node form a circular list linked by
2531 die_sib. die_child points to the node *before* the "first" child node. */
2533 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2534 union die_symbol_or_type_node
2536 const char * GTY ((tag ("0"))) die_symbol;
2537 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2539 GTY ((desc ("%0.comdat_type_p"))) die_id;
2540 vec<dw_attr_node, va_gc> *die_attr;
2541 dw_die_ref die_parent;
2542 dw_die_ref die_child;
2543 dw_die_ref die_sib;
2544 dw_die_ref die_definition; /* ref from a specification to its definition */
2545 dw_offset die_offset;
2546 unsigned long die_abbrev;
2547 int die_mark;
2548 unsigned int decl_id;
2549 enum dwarf_tag die_tag;
2550 /* Die is used and must not be pruned as unused. */
2551 BOOL_BITFIELD die_perennial_p : 1;
2552 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2553 /* Lots of spare bits. */
2555 die_node;
2557 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2558 #define FOR_EACH_CHILD(die, c, expr) do { \
2559 c = die->die_child; \
2560 if (c) do { \
2561 c = c->die_sib; \
2562 expr; \
2563 } while (c != die->die_child); \
2564 } while (0)
2566 /* The pubname structure */
2568 typedef struct GTY(()) pubname_struct {
2569 dw_die_ref die;
2570 const char *name;
2572 pubname_entry;
2575 struct GTY(()) dw_ranges_struct {
2576 /* If this is positive, it's a block number, otherwise it's a
2577 bitwise-negated index into dw_ranges_by_label. */
2578 int num;
2581 /* A structure to hold a macinfo entry. */
2583 typedef struct GTY(()) macinfo_struct {
2584 unsigned char code;
2585 unsigned HOST_WIDE_INT lineno;
2586 const char *info;
2588 macinfo_entry;
2591 struct GTY(()) dw_ranges_by_label_struct {
2592 const char *begin;
2593 const char *end;
2596 /* The comdat type node structure. */
2597 typedef struct GTY(()) comdat_type_struct
2599 dw_die_ref root_die;
2600 dw_die_ref type_die;
2601 dw_die_ref skeleton_die;
2602 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2603 struct comdat_type_struct *next;
2605 comdat_type_node;
2607 /* The limbo die list structure. */
2608 typedef struct GTY(()) limbo_die_struct {
2609 dw_die_ref die;
2610 tree created_for;
2611 struct limbo_die_struct *next;
2613 limbo_die_node;
2615 typedef struct skeleton_chain_struct
2617 dw_die_ref old_die;
2618 dw_die_ref new_die;
2619 struct skeleton_chain_struct *parent;
2621 skeleton_chain_node;
2623 /* Define a macro which returns nonzero for a TYPE_DECL which was
2624 implicitly generated for a type.
2626 Note that, unlike the C front-end (which generates a NULL named
2627 TYPE_DECL node for each complete tagged type, each array type,
2628 and each function type node created) the C++ front-end generates
2629 a _named_ TYPE_DECL node for each tagged type node created.
2630 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2631 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2632 front-end, but for each type, tagged or not. */
2634 #define TYPE_DECL_IS_STUB(decl) \
2635 (DECL_NAME (decl) == NULL_TREE \
2636 || (DECL_ARTIFICIAL (decl) \
2637 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2638 /* This is necessary for stub decls that \
2639 appear in nested inline functions. */ \
2640 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2641 && (decl_ultimate_origin (decl) \
2642 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2644 /* Information concerning the compilation unit's programming
2645 language, and compiler version. */
2647 /* Fixed size portion of the DWARF compilation unit header. */
2648 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2649 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2651 /* Fixed size portion of the DWARF comdat type unit header. */
2652 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2653 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2654 + DWARF_OFFSET_SIZE)
2656 /* Fixed size portion of public names info. */
2657 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2659 /* Fixed size portion of the address range info. */
2660 #define DWARF_ARANGES_HEADER_SIZE \
2661 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2662 DWARF2_ADDR_SIZE * 2) \
2663 - DWARF_INITIAL_LENGTH_SIZE)
2665 /* Size of padding portion in the address range info. It must be
2666 aligned to twice the pointer size. */
2667 #define DWARF_ARANGES_PAD_SIZE \
2668 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2669 DWARF2_ADDR_SIZE * 2) \
2670 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2672 /* Use assembler line directives if available. */
2673 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2674 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2675 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2676 #else
2677 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2678 #endif
2679 #endif
2681 /* Minimum line offset in a special line info. opcode.
2682 This value was chosen to give a reasonable range of values. */
2683 #define DWARF_LINE_BASE -10
2685 /* First special line opcode - leave room for the standard opcodes. */
2686 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2688 /* Range of line offsets in a special line info. opcode. */
2689 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2691 /* Flag that indicates the initial value of the is_stmt_start flag.
2692 In the present implementation, we do not mark any lines as
2693 the beginning of a source statement, because that information
2694 is not made available by the GCC front-end. */
2695 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2697 /* Maximum number of operations per instruction bundle. */
2698 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2699 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2700 #endif
2702 /* This location is used by calc_die_sizes() to keep track
2703 the offset of each DIE within the .debug_info section. */
2704 static unsigned long next_die_offset;
2706 /* Record the root of the DIE's built for the current compilation unit. */
2707 static GTY(()) dw_die_ref single_comp_unit_die;
2709 /* A list of type DIEs that have been separated into comdat sections. */
2710 static GTY(()) comdat_type_node *comdat_type_list;
2712 /* A list of DIEs with a NULL parent waiting to be relocated. */
2713 static GTY(()) limbo_die_node *limbo_die_list;
2715 /* A list of DIEs for which we may have to generate
2716 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2717 static GTY(()) limbo_die_node *deferred_asm_name;
2719 /* Filenames referenced by this compilation unit. */
2720 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2722 /* A hash table of references to DIE's that describe declarations.
2723 The key is a DECL_UID() which is a unique number identifying each decl. */
2724 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2726 /* A hash table of references to DIE's that describe COMMON blocks.
2727 The key is DECL_UID() ^ die_parent. */
2728 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2730 typedef struct GTY(()) die_arg_entry_struct {
2731 dw_die_ref die;
2732 tree arg;
2733 } die_arg_entry;
2736 /* Node of the variable location list. */
2737 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2738 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2739 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2740 in mode of the EXPR_LIST node and first EXPR_LIST operand
2741 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2742 location or NULL for padding. For larger bitsizes,
2743 mode is 0 and first operand is a CONCAT with bitsize
2744 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2745 NULL as second operand. */
2746 rtx GTY (()) loc;
2747 const char * GTY (()) label;
2748 struct var_loc_node * GTY (()) next;
2751 /* Variable location list. */
2752 struct GTY (()) var_loc_list_def {
2753 struct var_loc_node * GTY (()) first;
2755 /* Pointer to the last but one or last element of the
2756 chained list. If the list is empty, both first and
2757 last are NULL, if the list contains just one node
2758 or the last node certainly is not redundant, it points
2759 to the last node, otherwise points to the last but one.
2760 Do not mark it for GC because it is marked through the chain. */
2761 struct var_loc_node * GTY ((skip ("%h"))) last;
2763 /* Pointer to the last element before section switch,
2764 if NULL, either sections weren't switched or first
2765 is after section switch. */
2766 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2768 /* DECL_UID of the variable decl. */
2769 unsigned int decl_id;
2771 typedef struct var_loc_list_def var_loc_list;
2773 /* Call argument location list. */
2774 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2775 rtx GTY (()) call_arg_loc_note;
2776 const char * GTY (()) label;
2777 tree GTY (()) block;
2778 bool tail_call_p;
2779 rtx GTY (()) symbol_ref;
2780 struct call_arg_loc_node * GTY (()) next;
2784 /* Table of decl location linked lists. */
2785 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2787 /* Head and tail of call_arg_loc chain. */
2788 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2789 static struct call_arg_loc_node *call_arg_loc_last;
2791 /* Number of call sites in the current function. */
2792 static int call_site_count = -1;
2793 /* Number of tail call sites in the current function. */
2794 static int tail_call_site_count = -1;
2796 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2797 DIEs. */
2798 static vec<dw_die_ref> block_map;
2800 /* A cached location list. */
2801 struct GTY (()) cached_dw_loc_list_def {
2802 /* The DECL_UID of the decl that this entry describes. */
2803 unsigned int decl_id;
2805 /* The cached location list. */
2806 dw_loc_list_ref loc_list;
2808 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2810 /* Table of cached location lists. */
2811 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2813 /* A pointer to the base of a list of references to DIE's that
2814 are uniquely identified by their tag, presence/absence of
2815 children DIE's, and list of attribute/value pairs. */
2816 static GTY((length ("abbrev_die_table_allocated")))
2817 dw_die_ref *abbrev_die_table;
2819 /* Number of elements currently allocated for abbrev_die_table. */
2820 static GTY(()) unsigned abbrev_die_table_allocated;
2822 /* Number of elements in type_die_table currently in use. */
2823 static GTY(()) unsigned abbrev_die_table_in_use;
2825 /* Size (in elements) of increments by which we may expand the
2826 abbrev_die_table. */
2827 #define ABBREV_DIE_TABLE_INCREMENT 256
2829 /* A global counter for generating labels for line number data. */
2830 static unsigned int line_info_label_num;
2832 /* The current table to which we should emit line number information
2833 for the current function. This will be set up at the beginning of
2834 assembly for the function. */
2835 static dw_line_info_table *cur_line_info_table;
2837 /* The two default tables of line number info. */
2838 static GTY(()) dw_line_info_table *text_section_line_info;
2839 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2841 /* The set of all non-default tables of line number info. */
2842 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2844 /* A flag to tell pubnames/types export if there is an info section to
2845 refer to. */
2846 static bool info_section_emitted;
2848 /* A pointer to the base of a table that contains a list of publicly
2849 accessible names. */
2850 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2852 /* A pointer to the base of a table that contains a list of publicly
2853 accessible types. */
2854 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2856 /* A pointer to the base of a table that contains a list of macro
2857 defines/undefines (and file start/end markers). */
2858 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2860 /* True if .debug_macinfo or .debug_macros section is going to be
2861 emitted. */
2862 #define have_macinfo \
2863 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2864 && !macinfo_table->is_empty ())
2866 /* Array of dies for which we should generate .debug_ranges info. */
2867 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2869 /* Number of elements currently allocated for ranges_table. */
2870 static GTY(()) unsigned ranges_table_allocated;
2872 /* Number of elements in ranges_table currently in use. */
2873 static GTY(()) unsigned ranges_table_in_use;
2875 /* Array of pairs of labels referenced in ranges_table. */
2876 static GTY ((length ("ranges_by_label_allocated")))
2877 dw_ranges_by_label_ref ranges_by_label;
2879 /* Number of elements currently allocated for ranges_by_label. */
2880 static GTY(()) unsigned ranges_by_label_allocated;
2882 /* Number of elements in ranges_by_label currently in use. */
2883 static GTY(()) unsigned ranges_by_label_in_use;
2885 /* Size (in elements) of increments by which we may expand the
2886 ranges_table. */
2887 #define RANGES_TABLE_INCREMENT 64
2889 /* Whether we have location lists that need outputting */
2890 static GTY(()) bool have_location_lists;
2892 /* Unique label counter. */
2893 static GTY(()) unsigned int loclabel_num;
2895 /* Unique label counter for point-of-call tables. */
2896 static GTY(()) unsigned int poc_label_num;
2898 /* Record whether the function being analyzed contains inlined functions. */
2899 static int current_function_has_inlines;
2901 /* The last file entry emitted by maybe_emit_file(). */
2902 static GTY(()) struct dwarf_file_data * last_emitted_file;
2904 /* Number of internal labels generated by gen_internal_sym(). */
2905 static GTY(()) int label_num;
2907 /* Cached result of previous call to lookup_filename. */
2908 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2910 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2912 /* Instances of generic types for which we need to generate debug
2913 info that describe their generic parameters and arguments. That
2914 generation needs to happen once all types are properly laid out so
2915 we do it at the end of compilation. */
2916 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2918 /* Offset from the "steady-state frame pointer" to the frame base,
2919 within the current function. */
2920 static HOST_WIDE_INT frame_pointer_fb_offset;
2921 static bool frame_pointer_fb_offset_valid;
2923 static vec<dw_die_ref> base_types;
2925 /* Forward declarations for functions defined in this file. */
2927 static int is_pseudo_reg (const_rtx);
2928 static tree type_main_variant (tree);
2929 static int is_tagged_type (const_tree);
2930 static const char *dwarf_tag_name (unsigned);
2931 static const char *dwarf_attr_name (unsigned);
2932 static const char *dwarf_form_name (unsigned);
2933 static tree decl_ultimate_origin (const_tree);
2934 static tree decl_class_context (tree);
2935 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2936 static inline enum dw_val_class AT_class (dw_attr_ref);
2937 static inline unsigned int AT_index (dw_attr_ref);
2938 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2939 static inline unsigned AT_flag (dw_attr_ref);
2940 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2941 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2942 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2943 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2944 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2945 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2946 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2947 unsigned int, unsigned char *);
2948 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2949 static hashval_t debug_str_do_hash (const void *);
2950 static int debug_str_eq (const void *, const void *);
2951 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2952 static inline const char *AT_string (dw_attr_ref);
2953 static enum dwarf_form AT_string_form (dw_attr_ref);
2954 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2955 static void add_AT_specification (dw_die_ref, dw_die_ref);
2956 static inline dw_die_ref AT_ref (dw_attr_ref);
2957 static inline int AT_ref_external (dw_attr_ref);
2958 static inline void set_AT_ref_external (dw_attr_ref, int);
2959 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2960 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2961 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2962 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2963 dw_loc_list_ref);
2964 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2965 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2966 static void remove_addr_table_entry (addr_table_entry *);
2967 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2968 static inline rtx AT_addr (dw_attr_ref);
2969 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2970 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2971 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2972 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2973 unsigned HOST_WIDE_INT);
2974 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2975 unsigned long, bool);
2976 static inline const char *AT_lbl (dw_attr_ref);
2977 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2978 static const char *get_AT_low_pc (dw_die_ref);
2979 static const char *get_AT_hi_pc (dw_die_ref);
2980 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2981 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2982 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2983 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2984 static bool is_cxx (void);
2985 static bool is_fortran (void);
2986 static bool is_ada (void);
2987 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2988 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2989 static void add_child_die (dw_die_ref, dw_die_ref);
2990 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2991 static dw_die_ref lookup_type_die (tree);
2992 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2993 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2994 static void equate_type_number_to_die (tree, dw_die_ref);
2995 static hashval_t decl_die_table_hash (const void *);
2996 static int decl_die_table_eq (const void *, const void *);
2997 static dw_die_ref lookup_decl_die (tree);
2998 static hashval_t common_block_die_table_hash (const void *);
2999 static int common_block_die_table_eq (const void *, const void *);
3000 static hashval_t decl_loc_table_hash (const void *);
3001 static int decl_loc_table_eq (const void *, const void *);
3002 static var_loc_list *lookup_decl_loc (const_tree);
3003 static void equate_decl_number_to_die (tree, dw_die_ref);
3004 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3005 static void print_spaces (FILE *);
3006 static void print_die (dw_die_ref, FILE *);
3007 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3008 static dw_die_ref pop_compile_unit (dw_die_ref);
3009 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3010 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3011 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3012 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3013 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3014 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3015 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3016 struct md5_ctx *, int *);
3017 struct checksum_attributes;
3018 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3019 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3020 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3021 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3022 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3023 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3024 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3025 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3026 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3027 static void compute_section_prefix (dw_die_ref);
3028 static int is_type_die (dw_die_ref);
3029 static int is_comdat_die (dw_die_ref);
3030 static int is_symbol_die (dw_die_ref);
3031 static inline bool is_template_instantiation (dw_die_ref);
3032 static void assign_symbol_names (dw_die_ref);
3033 static void break_out_includes (dw_die_ref);
3034 static int is_declaration_die (dw_die_ref);
3035 static int should_move_die_to_comdat (dw_die_ref);
3036 static dw_die_ref clone_as_declaration (dw_die_ref);
3037 static dw_die_ref clone_die (dw_die_ref);
3038 static dw_die_ref clone_tree (dw_die_ref);
3039 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3040 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3041 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3042 static dw_die_ref generate_skeleton (dw_die_ref);
3043 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3044 dw_die_ref,
3045 dw_die_ref);
3046 static void break_out_comdat_types (dw_die_ref);
3047 static void copy_decls_for_unworthy_types (dw_die_ref);
3049 static void add_sibling_attributes (dw_die_ref);
3050 static void output_location_lists (dw_die_ref);
3051 static int constant_size (unsigned HOST_WIDE_INT);
3052 static unsigned long size_of_die (dw_die_ref);
3053 static void calc_die_sizes (dw_die_ref);
3054 static void calc_base_type_die_sizes (void);
3055 static void mark_dies (dw_die_ref);
3056 static void unmark_dies (dw_die_ref);
3057 static void unmark_all_dies (dw_die_ref);
3058 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3059 static unsigned long size_of_aranges (void);
3060 static enum dwarf_form value_format (dw_attr_ref);
3061 static void output_value_format (dw_attr_ref);
3062 static void output_abbrev_section (void);
3063 static void output_die_abbrevs (unsigned long, dw_die_ref);
3064 static void output_die_symbol (dw_die_ref);
3065 static void output_die (dw_die_ref);
3066 static void output_compilation_unit_header (void);
3067 static void output_comp_unit (dw_die_ref, int);
3068 static void output_comdat_type_unit (comdat_type_node *);
3069 static const char *dwarf2_name (tree, int);
3070 static void add_pubname (tree, dw_die_ref);
3071 static void add_enumerator_pubname (const char *, dw_die_ref);
3072 static void add_pubname_string (const char *, dw_die_ref);
3073 static void add_pubtype (tree, dw_die_ref);
3074 static void output_pubnames (vec<pubname_entry, va_gc> *);
3075 static void output_aranges (unsigned long);
3076 static unsigned int add_ranges_num (int);
3077 static unsigned int add_ranges (const_tree);
3078 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3079 bool *, bool);
3080 static void output_ranges (void);
3081 static dw_line_info_table *new_line_info_table (void);
3082 static void output_line_info (bool);
3083 static void output_file_names (void);
3084 static dw_die_ref base_type_die (tree);
3085 static int is_base_type (tree);
3086 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3087 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3088 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3089 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3090 static int type_is_enum (const_tree);
3091 static unsigned int dbx_reg_number (const_rtx);
3092 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3093 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3094 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3095 enum var_init_status);
3096 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3097 enum var_init_status);
3098 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3099 enum var_init_status);
3100 static int is_based_loc (const_rtx);
3101 static int resolve_one_addr (rtx *, void *);
3102 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3103 enum var_init_status);
3104 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3105 enum var_init_status);
3106 static dw_loc_list_ref loc_list_from_tree (tree, int);
3107 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3108 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3109 static tree field_type (const_tree);
3110 static unsigned int simple_type_align_in_bits (const_tree);
3111 static unsigned int simple_decl_align_in_bits (const_tree);
3112 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3113 static HOST_WIDE_INT field_byte_offset (const_tree);
3114 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3115 dw_loc_list_ref);
3116 static void add_data_member_location_attribute (dw_die_ref, tree);
3117 static bool add_const_value_attribute (dw_die_ref, rtx);
3118 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3119 static void insert_double (double_int, unsigned char *);
3120 static void insert_float (const_rtx, unsigned char *);
3121 static rtx rtl_for_decl_location (tree);
3122 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3123 enum dwarf_attribute);
3124 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3125 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3126 static void add_name_attribute (dw_die_ref, const char *);
3127 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3128 static void add_comp_dir_attribute (dw_die_ref);
3129 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3130 static void add_subscript_info (dw_die_ref, tree, bool);
3131 static void add_byte_size_attribute (dw_die_ref, tree);
3132 static void add_bit_offset_attribute (dw_die_ref, tree);
3133 static void add_bit_size_attribute (dw_die_ref, tree);
3134 static void add_prototyped_attribute (dw_die_ref, tree);
3135 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3136 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3137 static void add_src_coords_attributes (dw_die_ref, tree);
3138 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3139 static void push_decl_scope (tree);
3140 static void pop_decl_scope (void);
3141 static dw_die_ref scope_die_for (tree, dw_die_ref);
3142 static inline int local_scope_p (dw_die_ref);
3143 static inline int class_scope_p (dw_die_ref);
3144 static inline int class_or_namespace_scope_p (dw_die_ref);
3145 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3146 static void add_calling_convention_attribute (dw_die_ref, tree);
3147 static const char *type_tag (const_tree);
3148 static tree member_declared_type (const_tree);
3149 #if 0
3150 static const char *decl_start_label (tree);
3151 #endif
3152 static void gen_array_type_die (tree, dw_die_ref);
3153 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3154 #if 0
3155 static void gen_entry_point_die (tree, dw_die_ref);
3156 #endif
3157 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3158 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3159 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3160 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3161 static void gen_formal_types_die (tree, dw_die_ref);
3162 static void gen_subprogram_die (tree, dw_die_ref);
3163 static void gen_variable_die (tree, tree, dw_die_ref);
3164 static void gen_const_die (tree, dw_die_ref);
3165 static void gen_label_die (tree, dw_die_ref);
3166 static void gen_lexical_block_die (tree, dw_die_ref, int);
3167 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3168 static void gen_field_die (tree, dw_die_ref);
3169 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3170 static dw_die_ref gen_compile_unit_die (const char *);
3171 static void gen_inheritance_die (tree, tree, dw_die_ref);
3172 static void gen_member_die (tree, dw_die_ref);
3173 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3174 enum debug_info_usage);
3175 static void gen_subroutine_type_die (tree, dw_die_ref);
3176 static void gen_typedef_die (tree, dw_die_ref);
3177 static void gen_type_die (tree, dw_die_ref);
3178 static void gen_block_die (tree, dw_die_ref, int);
3179 static void decls_for_scope (tree, dw_die_ref, int);
3180 static inline int is_redundant_typedef (const_tree);
3181 static bool is_naming_typedef_decl (const_tree);
3182 static inline dw_die_ref get_context_die (tree);
3183 static void gen_namespace_die (tree, dw_die_ref);
3184 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3185 static dw_die_ref force_decl_die (tree);
3186 static dw_die_ref force_type_die (tree);
3187 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3188 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3189 static struct dwarf_file_data * lookup_filename (const char *);
3190 static void retry_incomplete_types (void);
3191 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3192 static void gen_generic_params_dies (tree);
3193 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3194 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3195 static void splice_child_die (dw_die_ref, dw_die_ref);
3196 static int file_info_cmp (const void *, const void *);
3197 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3198 const char *, const char *);
3199 static void output_loc_list (dw_loc_list_ref);
3200 static char *gen_internal_sym (const char *);
3201 static bool want_pubnames (void);
3203 static void prune_unmark_dies (dw_die_ref);
3204 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3205 static void prune_unused_types_mark (dw_die_ref, int);
3206 static void prune_unused_types_walk (dw_die_ref);
3207 static void prune_unused_types_walk_attribs (dw_die_ref);
3208 static void prune_unused_types_prune (dw_die_ref);
3209 static void prune_unused_types (void);
3210 static int maybe_emit_file (struct dwarf_file_data *fd);
3211 static inline const char *AT_vms_delta1 (dw_attr_ref);
3212 static inline const char *AT_vms_delta2 (dw_attr_ref);
3213 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3214 const char *, const char *);
3215 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3216 static void gen_remaining_tmpl_value_param_die_attribute (void);
3217 static bool generic_type_p (tree);
3218 static void schedule_generic_params_dies_gen (tree t);
3219 static void gen_scheduled_generic_parms_dies (void);
3221 static const char *comp_dir_string (void);
3223 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3225 /* enum for tracking thread-local variables whose address is really an offset
3226 relative to the TLS pointer, which will need link-time relocation, but will
3227 not need relocation by the DWARF consumer. */
3229 enum dtprel_bool
3231 dtprel_false = 0,
3232 dtprel_true = 1
3235 /* Return the operator to use for an address of a variable. For dtprel_true, we
3236 use DW_OP_const*. For regular variables, which need both link-time
3237 relocation and consumer-level relocation (e.g., to account for shared objects
3238 loaded at a random address), we use DW_OP_addr*. */
3240 static inline enum dwarf_location_atom
3241 dw_addr_op (enum dtprel_bool dtprel)
3243 if (dtprel == dtprel_true)
3244 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3245 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3246 else
3247 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3250 /* Return a pointer to a newly allocated address location description. If
3251 dwarf_split_debug_info is true, then record the address with the appropriate
3252 relocation. */
3253 static inline dw_loc_descr_ref
3254 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3256 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3258 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3259 ref->dw_loc_oprnd1.v.val_addr = addr;
3260 ref->dtprel = dtprel;
3261 if (dwarf_split_debug_info)
3262 ref->dw_loc_oprnd1.val_entry
3263 = add_addr_table_entry (addr,
3264 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3265 else
3266 ref->dw_loc_oprnd1.val_entry = NULL;
3268 return ref;
3271 /* Section names used to hold DWARF debugging information. */
3273 #ifndef DEBUG_INFO_SECTION
3274 #define DEBUG_INFO_SECTION ".debug_info"
3275 #endif
3276 #ifndef DEBUG_DWO_INFO_SECTION
3277 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3278 #endif
3279 #ifndef DEBUG_ABBREV_SECTION
3280 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3281 #endif
3282 #ifndef DEBUG_DWO_ABBREV_SECTION
3283 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3284 #endif
3285 #ifndef DEBUG_ARANGES_SECTION
3286 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3287 #endif
3288 #ifndef DEBUG_ADDR_SECTION
3289 #define DEBUG_ADDR_SECTION ".debug_addr"
3290 #endif
3291 #ifndef DEBUG_NORM_MACINFO_SECTION
3292 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3293 #endif
3294 #ifndef DEBUG_DWO_MACINFO_SECTION
3295 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3296 #endif
3297 #ifndef DEBUG_MACINFO_SECTION
3298 #define DEBUG_MACINFO_SECTION \
3299 (!dwarf_split_debug_info \
3300 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3301 #endif
3302 #ifndef DEBUG_NORM_MACRO_SECTION
3303 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3304 #endif
3305 #ifndef DEBUG_DWO_MACRO_SECTION
3306 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3307 #endif
3308 #ifndef DEBUG_MACRO_SECTION
3309 #define DEBUG_MACRO_SECTION \
3310 (!dwarf_split_debug_info \
3311 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3312 #endif
3313 #ifndef DEBUG_LINE_SECTION
3314 #define DEBUG_LINE_SECTION ".debug_line"
3315 #endif
3316 #ifndef DEBUG_DWO_LINE_SECTION
3317 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3318 #endif
3319 #ifndef DEBUG_LOC_SECTION
3320 #define DEBUG_LOC_SECTION ".debug_loc"
3321 #endif
3322 #ifndef DEBUG_DWO_LOC_SECTION
3323 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3324 #endif
3325 #ifndef DEBUG_PUBNAMES_SECTION
3326 #define DEBUG_PUBNAMES_SECTION \
3327 ((debug_generate_pub_sections == 2) \
3328 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3329 #endif
3330 #ifndef DEBUG_PUBTYPES_SECTION
3331 #define DEBUG_PUBTYPES_SECTION \
3332 ((debug_generate_pub_sections == 2) \
3333 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3334 #endif
3335 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3336 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3337 #ifndef DEBUG_STR_OFFSETS_SECTION
3338 #define DEBUG_STR_OFFSETS_SECTION \
3339 (!dwarf_split_debug_info \
3340 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3341 #endif
3342 #ifndef DEBUG_STR_DWO_SECTION
3343 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3344 #endif
3345 #ifndef DEBUG_STR_SECTION
3346 #define DEBUG_STR_SECTION ".debug_str"
3347 #endif
3348 #ifndef DEBUG_RANGES_SECTION
3349 #define DEBUG_RANGES_SECTION ".debug_ranges"
3350 #endif
3352 /* Standard ELF section names for compiled code and data. */
3353 #ifndef TEXT_SECTION_NAME
3354 #define TEXT_SECTION_NAME ".text"
3355 #endif
3357 /* Section flags for .debug_macinfo/.debug_macro section. */
3358 #define DEBUG_MACRO_SECTION_FLAGS \
3359 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3361 /* Section flags for .debug_str section. */
3362 #define DEBUG_STR_SECTION_FLAGS \
3363 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3364 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3365 : SECTION_DEBUG)
3367 /* Section flags for .debug_str.dwo section. */
3368 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3370 /* Labels we insert at beginning sections we can reference instead of
3371 the section names themselves. */
3373 #ifndef TEXT_SECTION_LABEL
3374 #define TEXT_SECTION_LABEL "Ltext"
3375 #endif
3376 #ifndef COLD_TEXT_SECTION_LABEL
3377 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3378 #endif
3379 #ifndef DEBUG_LINE_SECTION_LABEL
3380 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3381 #endif
3382 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3383 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3384 #endif
3385 #ifndef DEBUG_INFO_SECTION_LABEL
3386 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3387 #endif
3388 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3389 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3390 #endif
3391 #ifndef DEBUG_ABBREV_SECTION_LABEL
3392 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3393 #endif
3394 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3395 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3396 #endif
3397 #ifndef DEBUG_ADDR_SECTION_LABEL
3398 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3399 #endif
3400 #ifndef DEBUG_LOC_SECTION_LABEL
3401 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3402 #endif
3403 #ifndef DEBUG_RANGES_SECTION_LABEL
3404 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3405 #endif
3406 #ifndef DEBUG_MACINFO_SECTION_LABEL
3407 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3408 #endif
3409 #ifndef DEBUG_MACRO_SECTION_LABEL
3410 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3411 #endif
3412 #define SKELETON_COMP_DIE_ABBREV 1
3413 #define SKELETON_TYPE_DIE_ABBREV 2
3415 /* Definitions of defaults for formats and names of various special
3416 (artificial) labels which may be generated within this file (when the -g
3417 options is used and DWARF2_DEBUGGING_INFO is in effect.
3418 If necessary, these may be overridden from within the tm.h file, but
3419 typically, overriding these defaults is unnecessary. */
3421 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3422 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3423 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3424 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3425 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3426 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3427 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3428 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3429 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3430 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3431 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3432 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3433 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3436 #ifndef TEXT_END_LABEL
3437 #define TEXT_END_LABEL "Letext"
3438 #endif
3439 #ifndef COLD_END_LABEL
3440 #define COLD_END_LABEL "Letext_cold"
3441 #endif
3442 #ifndef BLOCK_BEGIN_LABEL
3443 #define BLOCK_BEGIN_LABEL "LBB"
3444 #endif
3445 #ifndef BLOCK_END_LABEL
3446 #define BLOCK_END_LABEL "LBE"
3447 #endif
3448 #ifndef LINE_CODE_LABEL
3449 #define LINE_CODE_LABEL "LM"
3450 #endif
3453 /* Return the root of the DIE's built for the current compilation unit. */
3454 static dw_die_ref
3455 comp_unit_die (void)
3457 if (!single_comp_unit_die)
3458 single_comp_unit_die = gen_compile_unit_die (NULL);
3459 return single_comp_unit_die;
3462 /* We allow a language front-end to designate a function that is to be
3463 called to "demangle" any name before it is put into a DIE. */
3465 static const char *(*demangle_name_func) (const char *);
3467 void
3468 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3470 demangle_name_func = func;
3473 /* Test if rtl node points to a pseudo register. */
3475 static inline int
3476 is_pseudo_reg (const_rtx rtl)
3478 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3479 || (GET_CODE (rtl) == SUBREG
3480 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3483 /* Return a reference to a type, with its const and volatile qualifiers
3484 removed. */
3486 static inline tree
3487 type_main_variant (tree type)
3489 type = TYPE_MAIN_VARIANT (type);
3491 /* ??? There really should be only one main variant among any group of
3492 variants of a given type (and all of the MAIN_VARIANT values for all
3493 members of the group should point to that one type) but sometimes the C
3494 front-end messes this up for array types, so we work around that bug
3495 here. */
3496 if (TREE_CODE (type) == ARRAY_TYPE)
3497 while (type != TYPE_MAIN_VARIANT (type))
3498 type = TYPE_MAIN_VARIANT (type);
3500 return type;
3503 /* Return nonzero if the given type node represents a tagged type. */
3505 static inline int
3506 is_tagged_type (const_tree type)
3508 enum tree_code code = TREE_CODE (type);
3510 return (code == RECORD_TYPE || code == UNION_TYPE
3511 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3514 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3516 static void
3517 get_ref_die_offset_label (char *label, dw_die_ref ref)
3519 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3522 /* Return die_offset of a DIE reference to a base type. */
3524 static unsigned long int
3525 get_base_type_offset (dw_die_ref ref)
3527 if (ref->die_offset)
3528 return ref->die_offset;
3529 if (comp_unit_die ()->die_abbrev)
3531 calc_base_type_die_sizes ();
3532 gcc_assert (ref->die_offset);
3534 return ref->die_offset;
3537 /* Return die_offset of a DIE reference other than base type. */
3539 static unsigned long int
3540 get_ref_die_offset (dw_die_ref ref)
3542 gcc_assert (ref->die_offset);
3543 return ref->die_offset;
3546 /* Convert a DIE tag into its string name. */
3548 static const char *
3549 dwarf_tag_name (unsigned int tag)
3551 const char *name = get_DW_TAG_name (tag);
3553 if (name != NULL)
3554 return name;
3556 return "DW_TAG_<unknown>";
3559 /* Convert a DWARF attribute code into its string name. */
3561 static const char *
3562 dwarf_attr_name (unsigned int attr)
3564 const char *name;
3566 switch (attr)
3568 #if VMS_DEBUGGING_INFO
3569 case DW_AT_HP_prologue:
3570 return "DW_AT_HP_prologue";
3571 #else
3572 case DW_AT_MIPS_loop_unroll_factor:
3573 return "DW_AT_MIPS_loop_unroll_factor";
3574 #endif
3576 #if VMS_DEBUGGING_INFO
3577 case DW_AT_HP_epilogue:
3578 return "DW_AT_HP_epilogue";
3579 #else
3580 case DW_AT_MIPS_stride:
3581 return "DW_AT_MIPS_stride";
3582 #endif
3585 name = get_DW_AT_name (attr);
3587 if (name != NULL)
3588 return name;
3590 return "DW_AT_<unknown>";
3593 /* Convert a DWARF value form code into its string name. */
3595 static const char *
3596 dwarf_form_name (unsigned int form)
3598 const char *name = get_DW_FORM_name (form);
3600 if (name != NULL)
3601 return name;
3603 return "DW_FORM_<unknown>";
3606 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3607 instance of an inlined instance of a decl which is local to an inline
3608 function, so we have to trace all of the way back through the origin chain
3609 to find out what sort of node actually served as the original seed for the
3610 given block. */
3612 static tree
3613 decl_ultimate_origin (const_tree decl)
3615 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3616 return NULL_TREE;
3618 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3619 nodes in the function to point to themselves; ignore that if
3620 we're trying to output the abstract instance of this function. */
3621 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3622 return NULL_TREE;
3624 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3625 most distant ancestor, this should never happen. */
3626 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3628 return DECL_ABSTRACT_ORIGIN (decl);
3631 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3632 of a virtual function may refer to a base class, so we check the 'this'
3633 parameter. */
3635 static tree
3636 decl_class_context (tree decl)
3638 tree context = NULL_TREE;
3640 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3641 context = DECL_CONTEXT (decl);
3642 else
3643 context = TYPE_MAIN_VARIANT
3644 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3646 if (context && !TYPE_P (context))
3647 context = NULL_TREE;
3649 return context;
3652 /* Add an attribute/value pair to a DIE. */
3654 static inline void
3655 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3657 /* Maybe this should be an assert? */
3658 if (die == NULL)
3659 return;
3661 vec_safe_reserve (die->die_attr, 1);
3662 vec_safe_push (die->die_attr, *attr);
3665 static inline enum dw_val_class
3666 AT_class (dw_attr_ref a)
3668 return a->dw_attr_val.val_class;
3671 /* Return the index for any attribute that will be referenced with a
3672 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3673 are stored in dw_attr_val.v.val_str for reference counting
3674 pruning. */
3676 static inline unsigned int
3677 AT_index (dw_attr_ref a)
3679 if (AT_class (a) == dw_val_class_str)
3680 return a->dw_attr_val.v.val_str->index;
3681 else if (a->dw_attr_val.val_entry != NULL)
3682 return a->dw_attr_val.val_entry->index;
3683 return NOT_INDEXED;
3686 /* Add a flag value attribute to a DIE. */
3688 static inline void
3689 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3691 dw_attr_node attr;
3693 attr.dw_attr = attr_kind;
3694 attr.dw_attr_val.val_class = dw_val_class_flag;
3695 attr.dw_attr_val.val_entry = NULL;
3696 attr.dw_attr_val.v.val_flag = flag;
3697 add_dwarf_attr (die, &attr);
3700 static inline unsigned
3701 AT_flag (dw_attr_ref a)
3703 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3704 return a->dw_attr_val.v.val_flag;
3707 /* Add a signed integer attribute value to a DIE. */
3709 static inline void
3710 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3712 dw_attr_node attr;
3714 attr.dw_attr = attr_kind;
3715 attr.dw_attr_val.val_class = dw_val_class_const;
3716 attr.dw_attr_val.val_entry = NULL;
3717 attr.dw_attr_val.v.val_int = int_val;
3718 add_dwarf_attr (die, &attr);
3721 static inline HOST_WIDE_INT
3722 AT_int (dw_attr_ref a)
3724 gcc_assert (a && AT_class (a) == dw_val_class_const);
3725 return a->dw_attr_val.v.val_int;
3728 /* Add an unsigned integer attribute value to a DIE. */
3730 static inline void
3731 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3732 unsigned HOST_WIDE_INT unsigned_val)
3734 dw_attr_node attr;
3736 attr.dw_attr = attr_kind;
3737 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3738 attr.dw_attr_val.val_entry = NULL;
3739 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3740 add_dwarf_attr (die, &attr);
3743 static inline unsigned HOST_WIDE_INT
3744 AT_unsigned (dw_attr_ref a)
3746 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3747 return a->dw_attr_val.v.val_unsigned;
3750 /* Add an unsigned double integer attribute value to a DIE. */
3752 static inline void
3753 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3754 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3756 dw_attr_node attr;
3758 attr.dw_attr = attr_kind;
3759 attr.dw_attr_val.val_class = dw_val_class_const_double;
3760 attr.dw_attr_val.val_entry = NULL;
3761 attr.dw_attr_val.v.val_double.high = high;
3762 attr.dw_attr_val.v.val_double.low = low;
3763 add_dwarf_attr (die, &attr);
3766 /* Add a floating point attribute value to a DIE and return it. */
3768 static inline void
3769 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3770 unsigned int length, unsigned int elt_size, unsigned char *array)
3772 dw_attr_node attr;
3774 attr.dw_attr = attr_kind;
3775 attr.dw_attr_val.val_class = dw_val_class_vec;
3776 attr.dw_attr_val.val_entry = NULL;
3777 attr.dw_attr_val.v.val_vec.length = length;
3778 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3779 attr.dw_attr_val.v.val_vec.array = array;
3780 add_dwarf_attr (die, &attr);
3783 /* Add an 8-byte data attribute value to a DIE. */
3785 static inline void
3786 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3787 unsigned char data8[8])
3789 dw_attr_node attr;
3791 attr.dw_attr = attr_kind;
3792 attr.dw_attr_val.val_class = dw_val_class_data8;
3793 attr.dw_attr_val.val_entry = NULL;
3794 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3795 add_dwarf_attr (die, &attr);
3798 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3799 dwarf_split_debug_info, address attributes in dies destined for the
3800 final executable have force_direct set to avoid using indexed
3801 references. */
3803 static inline void
3804 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3805 bool force_direct)
3807 dw_attr_node attr;
3808 char * lbl_id;
3810 lbl_id = xstrdup (lbl_low);
3811 attr.dw_attr = DW_AT_low_pc;
3812 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3813 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3814 if (dwarf_split_debug_info && !force_direct)
3815 attr.dw_attr_val.val_entry
3816 = add_addr_table_entry (lbl_id, ate_kind_label);
3817 else
3818 attr.dw_attr_val.val_entry = NULL;
3819 add_dwarf_attr (die, &attr);
3821 attr.dw_attr = DW_AT_high_pc;
3822 if (dwarf_version < 4)
3823 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3824 else
3825 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3826 lbl_id = xstrdup (lbl_high);
3827 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3828 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3829 && dwarf_split_debug_info && !force_direct)
3830 attr.dw_attr_val.val_entry
3831 = add_addr_table_entry (lbl_id, ate_kind_label);
3832 else
3833 attr.dw_attr_val.val_entry = NULL;
3834 add_dwarf_attr (die, &attr);
3837 /* Hash and equality functions for debug_str_hash. */
3839 static hashval_t
3840 debug_str_do_hash (const void *x)
3842 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3845 static int
3846 debug_str_eq (const void *x1, const void *x2)
3848 return strcmp ((((const struct indirect_string_node *)x1)->str),
3849 (const char *)x2) == 0;
3852 /* Add STR to the given string hash table. */
3854 static struct indirect_string_node *
3855 find_AT_string_in_table (const char *str, htab_t table)
3857 struct indirect_string_node *node;
3858 void **slot;
3860 slot = htab_find_slot_with_hash (table, str,
3861 htab_hash_string (str), INSERT);
3862 if (*slot == NULL)
3864 node = ggc_alloc_cleared_indirect_string_node ();
3865 node->str = ggc_strdup (str);
3866 *slot = node;
3868 else
3869 node = (struct indirect_string_node *) *slot;
3871 node->refcount++;
3872 return node;
3875 /* Add STR to the indirect string hash table. */
3877 static struct indirect_string_node *
3878 find_AT_string (const char *str)
3880 if (! debug_str_hash)
3881 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3882 debug_str_eq, NULL);
3884 return find_AT_string_in_table (str, debug_str_hash);
3887 /* Add a string attribute value to a DIE. */
3889 static inline void
3890 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3892 dw_attr_node attr;
3893 struct indirect_string_node *node;
3895 node = find_AT_string (str);
3897 attr.dw_attr = attr_kind;
3898 attr.dw_attr_val.val_class = dw_val_class_str;
3899 attr.dw_attr_val.val_entry = NULL;
3900 attr.dw_attr_val.v.val_str = node;
3901 add_dwarf_attr (die, &attr);
3904 static inline const char *
3905 AT_string (dw_attr_ref a)
3907 gcc_assert (a && AT_class (a) == dw_val_class_str);
3908 return a->dw_attr_val.v.val_str->str;
3911 /* Call this function directly to bypass AT_string_form's logic to put
3912 the string inline in the die. */
3914 static void
3915 set_indirect_string (struct indirect_string_node *node)
3917 char label[32];
3918 /* Already indirect is a no op. */
3919 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3921 gcc_assert (node->label);
3922 return;
3924 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3925 ++dw2_string_counter;
3926 node->label = xstrdup (label);
3928 if (!dwarf_split_debug_info)
3930 node->form = DW_FORM_strp;
3931 node->index = NOT_INDEXED;
3933 else
3935 node->form = DW_FORM_GNU_str_index;
3936 node->index = NO_INDEX_ASSIGNED;
3940 /* Find out whether a string should be output inline in DIE
3941 or out-of-line in .debug_str section. */
3943 static enum dwarf_form
3944 find_string_form (struct indirect_string_node *node)
3946 unsigned int len;
3948 if (node->form)
3949 return node->form;
3951 len = strlen (node->str) + 1;
3953 /* If the string is shorter or equal to the size of the reference, it is
3954 always better to put it inline. */
3955 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3956 return node->form = DW_FORM_string;
3958 /* If we cannot expect the linker to merge strings in .debug_str
3959 section, only put it into .debug_str if it is worth even in this
3960 single module. */
3961 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3962 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3963 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3964 return node->form = DW_FORM_string;
3966 set_indirect_string (node);
3968 return node->form;
3971 /* Find out whether the string referenced from the attribute should be
3972 output inline in DIE or out-of-line in .debug_str section. */
3974 static enum dwarf_form
3975 AT_string_form (dw_attr_ref a)
3977 gcc_assert (a && AT_class (a) == dw_val_class_str);
3978 return find_string_form (a->dw_attr_val.v.val_str);
3981 /* Add a DIE reference attribute value to a DIE. */
3983 static inline void
3984 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3986 dw_attr_node attr;
3988 #ifdef ENABLE_CHECKING
3989 gcc_assert (targ_die != NULL);
3990 #else
3991 /* With LTO we can end up trying to reference something we didn't create
3992 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3993 if (targ_die == NULL)
3994 return;
3995 #endif
3997 attr.dw_attr = attr_kind;
3998 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3999 attr.dw_attr_val.val_entry = NULL;
4000 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4001 attr.dw_attr_val.v.val_die_ref.external = 0;
4002 add_dwarf_attr (die, &attr);
4005 /* Change DIE reference REF to point to NEW_DIE instead. */
4007 static inline void
4008 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4010 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4011 ref->dw_attr_val.v.val_die_ref.die = new_die;
4012 ref->dw_attr_val.v.val_die_ref.external = 0;
4015 /* Add an AT_specification attribute to a DIE, and also make the back
4016 pointer from the specification to the definition. */
4018 static inline void
4019 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4021 add_AT_die_ref (die, DW_AT_specification, targ_die);
4022 gcc_assert (!targ_die->die_definition);
4023 targ_die->die_definition = die;
4026 static inline dw_die_ref
4027 AT_ref (dw_attr_ref a)
4029 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4030 return a->dw_attr_val.v.val_die_ref.die;
4033 static inline int
4034 AT_ref_external (dw_attr_ref a)
4036 if (a && AT_class (a) == dw_val_class_die_ref)
4037 return a->dw_attr_val.v.val_die_ref.external;
4039 return 0;
4042 static inline void
4043 set_AT_ref_external (dw_attr_ref a, int i)
4045 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4046 a->dw_attr_val.v.val_die_ref.external = i;
4049 /* Add an FDE reference attribute value to a DIE. */
4051 static inline void
4052 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4054 dw_attr_node attr;
4056 attr.dw_attr = attr_kind;
4057 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4058 attr.dw_attr_val.val_entry = NULL;
4059 attr.dw_attr_val.v.val_fde_index = targ_fde;
4060 add_dwarf_attr (die, &attr);
4063 /* Add a location description attribute value to a DIE. */
4065 static inline void
4066 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4068 dw_attr_node attr;
4070 attr.dw_attr = attr_kind;
4071 attr.dw_attr_val.val_class = dw_val_class_loc;
4072 attr.dw_attr_val.val_entry = NULL;
4073 attr.dw_attr_val.v.val_loc = loc;
4074 add_dwarf_attr (die, &attr);
4077 static inline dw_loc_descr_ref
4078 AT_loc (dw_attr_ref a)
4080 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4081 return a->dw_attr_val.v.val_loc;
4084 static inline void
4085 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4087 dw_attr_node attr;
4089 attr.dw_attr = attr_kind;
4090 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4091 attr.dw_attr_val.val_entry = NULL;
4092 attr.dw_attr_val.v.val_loc_list = loc_list;
4093 add_dwarf_attr (die, &attr);
4094 have_location_lists = true;
4097 static inline dw_loc_list_ref
4098 AT_loc_list (dw_attr_ref a)
4100 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4101 return a->dw_attr_val.v.val_loc_list;
4104 static inline dw_loc_list_ref *
4105 AT_loc_list_ptr (dw_attr_ref a)
4107 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4108 return &a->dw_attr_val.v.val_loc_list;
4111 /* Table of entries into the .debug_addr section. */
4113 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4115 /* Hash an address_table_entry. */
4117 static hashval_t
4118 addr_table_entry_do_hash (const void *x)
4120 const addr_table_entry *a = (const addr_table_entry *) x;
4121 switch (a->kind)
4123 case ate_kind_rtx:
4124 return iterative_hash_rtx (a->addr.rtl, 0);
4125 case ate_kind_rtx_dtprel:
4126 return iterative_hash_rtx (a->addr.rtl, 1);
4127 case ate_kind_label:
4128 return htab_hash_string (a->addr.label);
4129 default:
4130 gcc_unreachable ();
4134 /* Determine equality for two address_table_entries. */
4136 static int
4137 addr_table_entry_eq (const void *x1, const void *x2)
4139 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4140 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4142 if (a1->kind != a2->kind)
4143 return 0;
4144 switch (a1->kind)
4146 case ate_kind_rtx:
4147 case ate_kind_rtx_dtprel:
4148 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4149 case ate_kind_label:
4150 return strcmp (a1->addr.label, a2->addr.label) == 0;
4151 default:
4152 gcc_unreachable ();
4156 /* Initialize an addr_table_entry. */
4158 void
4159 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4161 e->kind = kind;
4162 switch (kind)
4164 case ate_kind_rtx:
4165 case ate_kind_rtx_dtprel:
4166 e->addr.rtl = (rtx) addr;
4167 break;
4168 case ate_kind_label:
4169 e->addr.label = (char *) addr;
4170 break;
4172 e->refcount = 0;
4173 e->index = NO_INDEX_ASSIGNED;
4176 /* Add attr to the address table entry to the table. Defer setting an
4177 index until output time. */
4179 static addr_table_entry *
4180 add_addr_table_entry (void *addr, enum ate_kind kind)
4182 addr_table_entry *node;
4183 addr_table_entry finder;
4184 void **slot;
4186 gcc_assert (dwarf_split_debug_info);
4187 if (! addr_index_table)
4188 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4189 addr_table_entry_eq, NULL);
4190 init_addr_table_entry (&finder, kind, addr);
4191 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4193 if (*slot == HTAB_EMPTY_ENTRY)
4195 node = ggc_alloc_cleared_addr_table_entry ();
4196 init_addr_table_entry (node, kind, addr);
4197 *slot = node;
4199 else
4200 node = (addr_table_entry *) *slot;
4202 node->refcount++;
4203 return node;
4206 /* Remove an entry from the addr table by decrementing its refcount.
4207 Strictly, decrementing the refcount would be enough, but the
4208 assertion that the entry is actually in the table has found
4209 bugs. */
4211 static void
4212 remove_addr_table_entry (addr_table_entry *entry)
4214 addr_table_entry *node;
4216 gcc_assert (dwarf_split_debug_info && addr_index_table);
4217 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4218 /* After an index is assigned, the table is frozen. */
4219 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4220 node->refcount--;
4223 /* Given a location list, remove all addresses it refers to from the
4224 address_table. */
4226 static void
4227 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4229 for (; descr; descr = descr->dw_loc_next)
4230 if (descr->dw_loc_oprnd1.val_entry != NULL)
4232 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4233 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4237 /* A helper function for dwarf2out_finish called through
4238 htab_traverse. Assign an addr_table_entry its index. All entries
4239 must be collected into the table when this function is called,
4240 because the indexing code relies on htab_traverse to traverse nodes
4241 in the same order for each run. */
4243 static int
4244 index_addr_table_entry (void **h, void *v)
4246 addr_table_entry *node = (addr_table_entry *) *h;
4247 unsigned int *index = (unsigned int *) v;
4249 /* Don't index unreferenced nodes. */
4250 if (node->refcount == 0)
4251 return 1;
4253 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4254 node->index = *index;
4255 *index += 1;
4257 return 1;
4260 /* Add an address constant attribute value to a DIE. When using
4261 dwarf_split_debug_info, address attributes in dies destined for the
4262 final executable should be direct references--setting the parameter
4263 force_direct ensures this behavior. */
4265 static inline void
4266 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4267 bool force_direct)
4269 dw_attr_node attr;
4271 attr.dw_attr = attr_kind;
4272 attr.dw_attr_val.val_class = dw_val_class_addr;
4273 attr.dw_attr_val.v.val_addr = addr;
4274 if (dwarf_split_debug_info && !force_direct)
4275 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4276 else
4277 attr.dw_attr_val.val_entry = NULL;
4278 add_dwarf_attr (die, &attr);
4281 /* Get the RTX from to an address DIE attribute. */
4283 static inline rtx
4284 AT_addr (dw_attr_ref a)
4286 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4287 return a->dw_attr_val.v.val_addr;
4290 /* Add a file attribute value to a DIE. */
4292 static inline void
4293 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4294 struct dwarf_file_data *fd)
4296 dw_attr_node attr;
4298 attr.dw_attr = attr_kind;
4299 attr.dw_attr_val.val_class = dw_val_class_file;
4300 attr.dw_attr_val.val_entry = NULL;
4301 attr.dw_attr_val.v.val_file = fd;
4302 add_dwarf_attr (die, &attr);
4305 /* Get the dwarf_file_data from a file DIE attribute. */
4307 static inline struct dwarf_file_data *
4308 AT_file (dw_attr_ref a)
4310 gcc_assert (a && AT_class (a) == dw_val_class_file);
4311 return a->dw_attr_val.v.val_file;
4314 /* Add a vms delta attribute value to a DIE. */
4316 static inline void
4317 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4318 const char *lbl1, const char *lbl2)
4320 dw_attr_node attr;
4322 attr.dw_attr = attr_kind;
4323 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4324 attr.dw_attr_val.val_entry = NULL;
4325 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4326 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4327 add_dwarf_attr (die, &attr);
4330 /* Add a label identifier attribute value to a DIE. */
4332 static inline void
4333 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4334 const char *lbl_id)
4336 dw_attr_node attr;
4338 attr.dw_attr = attr_kind;
4339 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4340 attr.dw_attr_val.val_entry = NULL;
4341 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4342 if (dwarf_split_debug_info)
4343 attr.dw_attr_val.val_entry
4344 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4345 ate_kind_label);
4346 add_dwarf_attr (die, &attr);
4349 /* Add a section offset attribute value to a DIE, an offset into the
4350 debug_line section. */
4352 static inline void
4353 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4354 const char *label)
4356 dw_attr_node attr;
4358 attr.dw_attr = attr_kind;
4359 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4360 attr.dw_attr_val.val_entry = NULL;
4361 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4362 add_dwarf_attr (die, &attr);
4365 /* Add a section offset attribute value to a DIE, an offset into the
4366 debug_macinfo section. */
4368 static inline void
4369 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4370 const char *label)
4372 dw_attr_node attr;
4374 attr.dw_attr = attr_kind;
4375 attr.dw_attr_val.val_class = dw_val_class_macptr;
4376 attr.dw_attr_val.val_entry = NULL;
4377 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4378 add_dwarf_attr (die, &attr);
4381 /* Add an offset attribute value to a DIE. */
4383 static inline void
4384 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4385 unsigned HOST_WIDE_INT offset)
4387 dw_attr_node attr;
4389 attr.dw_attr = attr_kind;
4390 attr.dw_attr_val.val_class = dw_val_class_offset;
4391 attr.dw_attr_val.val_entry = NULL;
4392 attr.dw_attr_val.v.val_offset = offset;
4393 add_dwarf_attr (die, &attr);
4396 /* Add a range_list attribute value to a DIE. When using
4397 dwarf_split_debug_info, address attributes in dies destined for the
4398 final executable should be direct references--setting the parameter
4399 force_direct ensures this behavior. */
4401 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4402 #define RELOCATED_OFFSET (NULL)
4404 static void
4405 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4406 long unsigned int offset, bool force_direct)
4408 dw_attr_node attr;
4410 attr.dw_attr = attr_kind;
4411 attr.dw_attr_val.val_class = dw_val_class_range_list;
4412 /* For the range_list attribute, use val_entry to store whether the
4413 offset should follow split-debug-info or normal semantics. This
4414 value is read in output_range_list_offset. */
4415 if (dwarf_split_debug_info && !force_direct)
4416 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4417 else
4418 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4419 attr.dw_attr_val.v.val_offset = offset;
4420 add_dwarf_attr (die, &attr);
4423 /* Return the start label of a delta attribute. */
4425 static inline const char *
4426 AT_vms_delta1 (dw_attr_ref a)
4428 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4429 return a->dw_attr_val.v.val_vms_delta.lbl1;
4432 /* Return the end label of a delta attribute. */
4434 static inline const char *
4435 AT_vms_delta2 (dw_attr_ref a)
4437 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4438 return a->dw_attr_val.v.val_vms_delta.lbl2;
4441 static inline const char *
4442 AT_lbl (dw_attr_ref a)
4444 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4445 || AT_class (a) == dw_val_class_lineptr
4446 || AT_class (a) == dw_val_class_macptr
4447 || AT_class (a) == dw_val_class_high_pc));
4448 return a->dw_attr_val.v.val_lbl_id;
4451 /* Get the attribute of type attr_kind. */
4453 static dw_attr_ref
4454 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4456 dw_attr_ref a;
4457 unsigned ix;
4458 dw_die_ref spec = NULL;
4460 if (! die)
4461 return NULL;
4463 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4464 if (a->dw_attr == attr_kind)
4465 return a;
4466 else if (a->dw_attr == DW_AT_specification
4467 || a->dw_attr == DW_AT_abstract_origin)
4468 spec = AT_ref (a);
4470 if (spec)
4471 return get_AT (spec, attr_kind);
4473 return NULL;
4476 /* Returns the parent of the declaration of DIE. */
4478 static dw_die_ref
4479 get_die_parent (dw_die_ref die)
4481 dw_die_ref t;
4483 if (!die)
4484 return NULL;
4486 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4487 || (t = get_AT_ref (die, DW_AT_specification)))
4488 die = t;
4490 return die->die_parent;
4493 /* Return the "low pc" attribute value, typically associated with a subprogram
4494 DIE. Return null if the "low pc" attribute is either not present, or if it
4495 cannot be represented as an assembler label identifier. */
4497 static inline const char *
4498 get_AT_low_pc (dw_die_ref die)
4500 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4502 return a ? AT_lbl (a) : NULL;
4505 /* Return the "high pc" attribute value, typically associated with a subprogram
4506 DIE. Return null if the "high pc" attribute is either not present, or if it
4507 cannot be represented as an assembler label identifier. */
4509 static inline const char *
4510 get_AT_hi_pc (dw_die_ref die)
4512 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4514 return a ? AT_lbl (a) : NULL;
4517 /* Return the value of the string attribute designated by ATTR_KIND, or
4518 NULL if it is not present. */
4520 static inline const char *
4521 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4523 dw_attr_ref a = get_AT (die, attr_kind);
4525 return a ? AT_string (a) : NULL;
4528 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4529 if it is not present. */
4531 static inline int
4532 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4534 dw_attr_ref a = get_AT (die, attr_kind);
4536 return a ? AT_flag (a) : 0;
4539 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4540 if it is not present. */
4542 static inline unsigned
4543 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4545 dw_attr_ref a = get_AT (die, attr_kind);
4547 return a ? AT_unsigned (a) : 0;
4550 static inline dw_die_ref
4551 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4553 dw_attr_ref a = get_AT (die, attr_kind);
4555 return a ? AT_ref (a) : NULL;
4558 static inline struct dwarf_file_data *
4559 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4561 dw_attr_ref a = get_AT (die, attr_kind);
4563 return a ? AT_file (a) : NULL;
4566 /* Return TRUE if the language is C++. */
4568 static inline bool
4569 is_cxx (void)
4571 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4573 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4576 /* Return TRUE if the language is Java. */
4578 static inline bool
4579 is_java (void)
4581 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4583 return lang == DW_LANG_Java;
4586 /* Return TRUE if the language is Fortran. */
4588 static inline bool
4589 is_fortran (void)
4591 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4593 return (lang == DW_LANG_Fortran77
4594 || lang == DW_LANG_Fortran90
4595 || lang == DW_LANG_Fortran95);
4598 /* Return TRUE if the language is Ada. */
4600 static inline bool
4601 is_ada (void)
4603 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4605 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4608 /* Remove the specified attribute if present. */
4610 static void
4611 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4613 dw_attr_ref a;
4614 unsigned ix;
4616 if (! die)
4617 return;
4619 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4620 if (a->dw_attr == attr_kind)
4622 if (AT_class (a) == dw_val_class_str)
4623 if (a->dw_attr_val.v.val_str->refcount)
4624 a->dw_attr_val.v.val_str->refcount--;
4626 /* vec::ordered_remove should help reduce the number of abbrevs
4627 that are needed. */
4628 die->die_attr->ordered_remove (ix);
4629 return;
4633 /* Remove CHILD from its parent. PREV must have the property that
4634 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4636 static void
4637 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4639 gcc_assert (child->die_parent == prev->die_parent);
4640 gcc_assert (prev->die_sib == child);
4641 if (prev == child)
4643 gcc_assert (child->die_parent->die_child == child);
4644 prev = NULL;
4646 else
4647 prev->die_sib = child->die_sib;
4648 if (child->die_parent->die_child == child)
4649 child->die_parent->die_child = prev;
4652 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4653 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4655 static void
4656 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4658 dw_die_ref parent = old_child->die_parent;
4660 gcc_assert (parent == prev->die_parent);
4661 gcc_assert (prev->die_sib == old_child);
4663 new_child->die_parent = parent;
4664 if (prev == old_child)
4666 gcc_assert (parent->die_child == old_child);
4667 new_child->die_sib = new_child;
4669 else
4671 prev->die_sib = new_child;
4672 new_child->die_sib = old_child->die_sib;
4674 if (old_child->die_parent->die_child == old_child)
4675 old_child->die_parent->die_child = new_child;
4678 /* Move all children from OLD_PARENT to NEW_PARENT. */
4680 static void
4681 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4683 dw_die_ref c;
4684 new_parent->die_child = old_parent->die_child;
4685 old_parent->die_child = NULL;
4686 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4689 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4690 matches TAG. */
4692 static void
4693 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4695 dw_die_ref c;
4697 c = die->die_child;
4698 if (c) do {
4699 dw_die_ref prev = c;
4700 c = c->die_sib;
4701 while (c->die_tag == tag)
4703 remove_child_with_prev (c, prev);
4704 /* Might have removed every child. */
4705 if (c == c->die_sib)
4706 return;
4707 c = c->die_sib;
4709 } while (c != die->die_child);
4712 /* Add a CHILD_DIE as the last child of DIE. */
4714 static void
4715 add_child_die (dw_die_ref die, dw_die_ref child_die)
4717 /* FIXME this should probably be an assert. */
4718 if (! die || ! child_die)
4719 return;
4720 gcc_assert (die != child_die);
4722 child_die->die_parent = die;
4723 if (die->die_child)
4725 child_die->die_sib = die->die_child->die_sib;
4726 die->die_child->die_sib = child_die;
4728 else
4729 child_die->die_sib = child_die;
4730 die->die_child = child_die;
4733 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4734 is the specification, to the end of PARENT's list of children.
4735 This is done by removing and re-adding it. */
4737 static void
4738 splice_child_die (dw_die_ref parent, dw_die_ref child)
4740 dw_die_ref p;
4742 /* We want the declaration DIE from inside the class, not the
4743 specification DIE at toplevel. */
4744 if (child->die_parent != parent)
4746 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4748 if (tmp)
4749 child = tmp;
4752 gcc_assert (child->die_parent == parent
4753 || (child->die_parent
4754 == get_AT_ref (parent, DW_AT_specification)));
4756 for (p = child->die_parent->die_child; ; p = p->die_sib)
4757 if (p->die_sib == child)
4759 remove_child_with_prev (child, p);
4760 break;
4763 add_child_die (parent, child);
4766 /* Return a pointer to a newly created DIE node. */
4768 static inline dw_die_ref
4769 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4771 dw_die_ref die = ggc_alloc_cleared_die_node ();
4773 die->die_tag = tag_value;
4775 if (parent_die != NULL)
4776 add_child_die (parent_die, die);
4777 else
4779 limbo_die_node *limbo_node;
4781 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4782 limbo_node->die = die;
4783 limbo_node->created_for = t;
4784 limbo_node->next = limbo_die_list;
4785 limbo_die_list = limbo_node;
4788 return die;
4791 /* Return the DIE associated with the given type specifier. */
4793 static inline dw_die_ref
4794 lookup_type_die (tree type)
4796 return TYPE_SYMTAB_DIE (type);
4799 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4800 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4801 anonymous type instead the one of the naming typedef. */
4803 static inline dw_die_ref
4804 strip_naming_typedef (tree type, dw_die_ref type_die)
4806 if (type
4807 && TREE_CODE (type) == RECORD_TYPE
4808 && type_die
4809 && type_die->die_tag == DW_TAG_typedef
4810 && is_naming_typedef_decl (TYPE_NAME (type)))
4811 type_die = get_AT_ref (type_die, DW_AT_type);
4812 return type_die;
4815 /* Like lookup_type_die, but if type is an anonymous type named by a
4816 typedef[1], return the DIE of the anonymous type instead the one of
4817 the naming typedef. This is because in gen_typedef_die, we did
4818 equate the anonymous struct named by the typedef with the DIE of
4819 the naming typedef. So by default, lookup_type_die on an anonymous
4820 struct yields the DIE of the naming typedef.
4822 [1]: Read the comment of is_naming_typedef_decl to learn about what
4823 a naming typedef is. */
4825 static inline dw_die_ref
4826 lookup_type_die_strip_naming_typedef (tree type)
4828 dw_die_ref die = lookup_type_die (type);
4829 return strip_naming_typedef (type, die);
4832 /* Equate a DIE to a given type specifier. */
4834 static inline void
4835 equate_type_number_to_die (tree type, dw_die_ref type_die)
4837 TYPE_SYMTAB_DIE (type) = type_die;
4840 /* Returns a hash value for X (which really is a die_struct). */
4842 static hashval_t
4843 decl_die_table_hash (const void *x)
4845 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4848 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4850 static int
4851 decl_die_table_eq (const void *x, const void *y)
4853 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4856 /* Return the DIE associated with a given declaration. */
4858 static inline dw_die_ref
4859 lookup_decl_die (tree decl)
4861 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4864 /* Returns a hash value for X (which really is a var_loc_list). */
4866 static hashval_t
4867 decl_loc_table_hash (const void *x)
4869 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4872 /* Return nonzero if decl_id of var_loc_list X is the same as
4873 UID of decl *Y. */
4875 static int
4876 decl_loc_table_eq (const void *x, const void *y)
4878 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4881 /* Return the var_loc list associated with a given declaration. */
4883 static inline var_loc_list *
4884 lookup_decl_loc (const_tree decl)
4886 if (!decl_loc_table)
4887 return NULL;
4888 return (var_loc_list *)
4889 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4892 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4894 static hashval_t
4895 cached_dw_loc_list_table_hash (const void *x)
4897 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4900 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4901 UID of decl *Y. */
4903 static int
4904 cached_dw_loc_list_table_eq (const void *x, const void *y)
4906 return (((const cached_dw_loc_list *) x)->decl_id
4907 == DECL_UID ((const_tree) y));
4910 /* Equate a DIE to a particular declaration. */
4912 static void
4913 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4915 unsigned int decl_id = DECL_UID (decl);
4916 void **slot;
4918 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4919 *slot = decl_die;
4920 decl_die->decl_id = decl_id;
4923 /* Return how many bits covers PIECE EXPR_LIST. */
4925 static int
4926 decl_piece_bitsize (rtx piece)
4928 int ret = (int) GET_MODE (piece);
4929 if (ret)
4930 return ret;
4931 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4932 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4933 return INTVAL (XEXP (XEXP (piece, 0), 0));
4936 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4938 static rtx *
4939 decl_piece_varloc_ptr (rtx piece)
4941 if ((int) GET_MODE (piece))
4942 return &XEXP (piece, 0);
4943 else
4944 return &XEXP (XEXP (piece, 0), 1);
4947 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4948 Next is the chain of following piece nodes. */
4950 static rtx
4951 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4953 if (bitsize <= (int) MAX_MACHINE_MODE)
4954 return alloc_EXPR_LIST (bitsize, loc_note, next);
4955 else
4956 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4957 GEN_INT (bitsize),
4958 loc_note), next);
4961 /* Return rtx that should be stored into loc field for
4962 LOC_NOTE and BITPOS/BITSIZE. */
4964 static rtx
4965 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4966 HOST_WIDE_INT bitsize)
4968 if (bitsize != -1)
4970 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4971 if (bitpos != 0)
4972 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4974 return loc_note;
4977 /* This function either modifies location piece list *DEST in
4978 place (if SRC and INNER is NULL), or copies location piece list
4979 *SRC to *DEST while modifying it. Location BITPOS is modified
4980 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4981 not copied and if needed some padding around it is added.
4982 When modifying in place, DEST should point to EXPR_LIST where
4983 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4984 to the start of the whole list and INNER points to the EXPR_LIST
4985 where earlier pieces cover PIECE_BITPOS bits. */
4987 static void
4988 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4989 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4990 HOST_WIDE_INT bitsize, rtx loc_note)
4992 int diff;
4993 bool copy = inner != NULL;
4995 if (copy)
4997 /* First copy all nodes preceding the current bitpos. */
4998 while (src != inner)
5000 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5001 decl_piece_bitsize (*src), NULL_RTX);
5002 dest = &XEXP (*dest, 1);
5003 src = &XEXP (*src, 1);
5006 /* Add padding if needed. */
5007 if (bitpos != piece_bitpos)
5009 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5010 copy ? NULL_RTX : *dest);
5011 dest = &XEXP (*dest, 1);
5013 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5015 gcc_assert (!copy);
5016 /* A piece with correct bitpos and bitsize already exist,
5017 just update the location for it and return. */
5018 *decl_piece_varloc_ptr (*dest) = loc_note;
5019 return;
5021 /* Add the piece that changed. */
5022 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5023 dest = &XEXP (*dest, 1);
5024 /* Skip over pieces that overlap it. */
5025 diff = bitpos - piece_bitpos + bitsize;
5026 if (!copy)
5027 src = dest;
5028 while (diff > 0 && *src)
5030 rtx piece = *src;
5031 diff -= decl_piece_bitsize (piece);
5032 if (copy)
5033 src = &XEXP (piece, 1);
5034 else
5036 *src = XEXP (piece, 1);
5037 free_EXPR_LIST_node (piece);
5040 /* Add padding if needed. */
5041 if (diff < 0 && *src)
5043 if (!copy)
5044 dest = src;
5045 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5046 dest = &XEXP (*dest, 1);
5048 if (!copy)
5049 return;
5050 /* Finally copy all nodes following it. */
5051 while (*src)
5053 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5054 decl_piece_bitsize (*src), NULL_RTX);
5055 dest = &XEXP (*dest, 1);
5056 src = &XEXP (*src, 1);
5060 /* Add a variable location node to the linked list for DECL. */
5062 static struct var_loc_node *
5063 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5065 unsigned int decl_id;
5066 var_loc_list *temp;
5067 void **slot;
5068 struct var_loc_node *loc = NULL;
5069 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5071 if (TREE_CODE (decl) == VAR_DECL
5072 && DECL_HAS_DEBUG_EXPR_P (decl))
5074 tree realdecl = DECL_DEBUG_EXPR (decl);
5075 if (handled_component_p (realdecl)
5076 || (TREE_CODE (realdecl) == MEM_REF
5077 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5079 HOST_WIDE_INT maxsize;
5080 tree innerdecl;
5081 innerdecl
5082 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5083 if (!DECL_P (innerdecl)
5084 || DECL_IGNORED_P (innerdecl)
5085 || TREE_STATIC (innerdecl)
5086 || bitsize <= 0
5087 || bitpos + bitsize > 256
5088 || bitsize != maxsize)
5089 return NULL;
5090 decl = innerdecl;
5094 decl_id = DECL_UID (decl);
5095 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5096 if (*slot == NULL)
5098 temp = ggc_alloc_cleared_var_loc_list ();
5099 temp->decl_id = decl_id;
5100 *slot = temp;
5102 else
5103 temp = (var_loc_list *) *slot;
5105 /* For PARM_DECLs try to keep around the original incoming value,
5106 even if that means we'll emit a zero-range .debug_loc entry. */
5107 if (temp->last
5108 && temp->first == temp->last
5109 && TREE_CODE (decl) == PARM_DECL
5110 && NOTE_P (temp->first->loc)
5111 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5112 && DECL_INCOMING_RTL (decl)
5113 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5114 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5115 == GET_CODE (DECL_INCOMING_RTL (decl))
5116 && prev_real_insn (temp->first->loc) == NULL_RTX
5117 && (bitsize != -1
5118 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5119 NOTE_VAR_LOCATION_LOC (loc_note))
5120 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5121 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5123 loc = ggc_alloc_cleared_var_loc_node ();
5124 temp->first->next = loc;
5125 temp->last = loc;
5126 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5128 else if (temp->last)
5130 struct var_loc_node *last = temp->last, *unused = NULL;
5131 rtx *piece_loc = NULL, last_loc_note;
5132 int piece_bitpos = 0;
5133 if (last->next)
5135 last = last->next;
5136 gcc_assert (last->next == NULL);
5138 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5140 piece_loc = &last->loc;
5143 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5144 if (piece_bitpos + cur_bitsize > bitpos)
5145 break;
5146 piece_bitpos += cur_bitsize;
5147 piece_loc = &XEXP (*piece_loc, 1);
5149 while (*piece_loc);
5151 /* TEMP->LAST here is either pointer to the last but one or
5152 last element in the chained list, LAST is pointer to the
5153 last element. */
5154 if (label && strcmp (last->label, label) == 0)
5156 /* For SRA optimized variables if there weren't any real
5157 insns since last note, just modify the last node. */
5158 if (piece_loc != NULL)
5160 adjust_piece_list (piece_loc, NULL, NULL,
5161 bitpos, piece_bitpos, bitsize, loc_note);
5162 return NULL;
5164 /* If the last note doesn't cover any instructions, remove it. */
5165 if (temp->last != last)
5167 temp->last->next = NULL;
5168 unused = last;
5169 last = temp->last;
5170 gcc_assert (strcmp (last->label, label) != 0);
5172 else
5174 gcc_assert (temp->first == temp->last
5175 || (temp->first->next == temp->last
5176 && TREE_CODE (decl) == PARM_DECL));
5177 memset (temp->last, '\0', sizeof (*temp->last));
5178 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5179 return temp->last;
5182 if (bitsize == -1 && NOTE_P (last->loc))
5183 last_loc_note = last->loc;
5184 else if (piece_loc != NULL
5185 && *piece_loc != NULL_RTX
5186 && piece_bitpos == bitpos
5187 && decl_piece_bitsize (*piece_loc) == bitsize)
5188 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5189 else
5190 last_loc_note = NULL_RTX;
5191 /* If the current location is the same as the end of the list,
5192 and either both or neither of the locations is uninitialized,
5193 we have nothing to do. */
5194 if (last_loc_note == NULL_RTX
5195 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5196 NOTE_VAR_LOCATION_LOC (loc_note)))
5197 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5198 != NOTE_VAR_LOCATION_STATUS (loc_note))
5199 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5200 == VAR_INIT_STATUS_UNINITIALIZED)
5201 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5202 == VAR_INIT_STATUS_UNINITIALIZED))))
5204 /* Add LOC to the end of list and update LAST. If the last
5205 element of the list has been removed above, reuse its
5206 memory for the new node, otherwise allocate a new one. */
5207 if (unused)
5209 loc = unused;
5210 memset (loc, '\0', sizeof (*loc));
5212 else
5213 loc = ggc_alloc_cleared_var_loc_node ();
5214 if (bitsize == -1 || piece_loc == NULL)
5215 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5216 else
5217 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5218 bitpos, piece_bitpos, bitsize, loc_note);
5219 last->next = loc;
5220 /* Ensure TEMP->LAST will point either to the new last but one
5221 element of the chain, or to the last element in it. */
5222 if (last != temp->last)
5223 temp->last = last;
5225 else if (unused)
5226 ggc_free (unused);
5228 else
5230 loc = ggc_alloc_cleared_var_loc_node ();
5231 temp->first = loc;
5232 temp->last = loc;
5233 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5235 return loc;
5238 /* Keep track of the number of spaces used to indent the
5239 output of the debugging routines that print the structure of
5240 the DIE internal representation. */
5241 static int print_indent;
5243 /* Indent the line the number of spaces given by print_indent. */
5245 static inline void
5246 print_spaces (FILE *outfile)
5248 fprintf (outfile, "%*s", print_indent, "");
5251 /* Print a type signature in hex. */
5253 static inline void
5254 print_signature (FILE *outfile, char *sig)
5256 int i;
5258 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5259 fprintf (outfile, "%02x", sig[i] & 0xff);
5262 /* Print the information associated with a given DIE, and its children.
5263 This routine is a debugging aid only. */
5265 static void
5266 print_die (dw_die_ref die, FILE *outfile)
5268 dw_attr_ref a;
5269 dw_die_ref c;
5270 unsigned ix;
5272 print_spaces (outfile);
5273 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5274 die->die_offset, dwarf_tag_name (die->die_tag),
5275 (void*) die);
5276 print_spaces (outfile);
5277 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5278 fprintf (outfile, " offset: %ld", die->die_offset);
5279 fprintf (outfile, " mark: %d\n", die->die_mark);
5281 if (die->comdat_type_p)
5283 print_spaces (outfile);
5284 fprintf (outfile, " signature: ");
5285 print_signature (outfile, die->die_id.die_type_node->signature);
5286 fprintf (outfile, "\n");
5289 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5291 print_spaces (outfile);
5292 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5294 switch (AT_class (a))
5296 case dw_val_class_addr:
5297 fprintf (outfile, "address");
5298 break;
5299 case dw_val_class_offset:
5300 fprintf (outfile, "offset");
5301 break;
5302 case dw_val_class_loc:
5303 fprintf (outfile, "location descriptor");
5304 break;
5305 case dw_val_class_loc_list:
5306 fprintf (outfile, "location list -> label:%s",
5307 AT_loc_list (a)->ll_symbol);
5308 break;
5309 case dw_val_class_range_list:
5310 fprintf (outfile, "range list");
5311 break;
5312 case dw_val_class_const:
5313 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5314 break;
5315 case dw_val_class_unsigned_const:
5316 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5317 break;
5318 case dw_val_class_const_double:
5319 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5320 HOST_WIDE_INT_PRINT_UNSIGNED")",
5321 a->dw_attr_val.v.val_double.high,
5322 a->dw_attr_val.v.val_double.low);
5323 break;
5324 case dw_val_class_vec:
5325 fprintf (outfile, "floating-point or vector constant");
5326 break;
5327 case dw_val_class_flag:
5328 fprintf (outfile, "%u", AT_flag (a));
5329 break;
5330 case dw_val_class_die_ref:
5331 if (AT_ref (a) != NULL)
5333 if (AT_ref (a)->comdat_type_p)
5335 fprintf (outfile, "die -> signature: ");
5336 print_signature (outfile,
5337 AT_ref (a)->die_id.die_type_node->signature);
5339 else if (AT_ref (a)->die_id.die_symbol)
5340 fprintf (outfile, "die -> label: %s",
5341 AT_ref (a)->die_id.die_symbol);
5342 else
5343 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5344 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5346 else
5347 fprintf (outfile, "die -> <null>");
5348 break;
5349 case dw_val_class_vms_delta:
5350 fprintf (outfile, "delta: @slotcount(%s-%s)",
5351 AT_vms_delta2 (a), AT_vms_delta1 (a));
5352 break;
5353 case dw_val_class_lbl_id:
5354 case dw_val_class_lineptr:
5355 case dw_val_class_macptr:
5356 case dw_val_class_high_pc:
5357 fprintf (outfile, "label: %s", AT_lbl (a));
5358 break;
5359 case dw_val_class_str:
5360 if (AT_string (a) != NULL)
5361 fprintf (outfile, "\"%s\"", AT_string (a));
5362 else
5363 fprintf (outfile, "<null>");
5364 break;
5365 case dw_val_class_file:
5366 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5367 AT_file (a)->emitted_number);
5368 break;
5369 case dw_val_class_data8:
5371 int i;
5373 for (i = 0; i < 8; i++)
5374 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5375 break;
5377 default:
5378 break;
5381 fprintf (outfile, "\n");
5384 if (die->die_child != NULL)
5386 print_indent += 4;
5387 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5388 print_indent -= 4;
5390 if (print_indent == 0)
5391 fprintf (outfile, "\n");
5394 /* Print the information collected for a given DIE. */
5396 DEBUG_FUNCTION void
5397 debug_dwarf_die (dw_die_ref die)
5399 print_die (die, stderr);
5402 DEBUG_FUNCTION void
5403 debug (die_struct &ref)
5405 print_die (&ref, stderr);
5408 DEBUG_FUNCTION void
5409 debug (die_struct *ptr)
5411 if (ptr)
5412 debug (*ptr);
5413 else
5414 fprintf (stderr, "<nil>\n");
5418 /* Print all DWARF information collected for the compilation unit.
5419 This routine is a debugging aid only. */
5421 DEBUG_FUNCTION void
5422 debug_dwarf (void)
5424 print_indent = 0;
5425 print_die (comp_unit_die (), stderr);
5428 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5429 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5430 DIE that marks the start of the DIEs for this include file. */
5432 static dw_die_ref
5433 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5435 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5436 dw_die_ref new_unit = gen_compile_unit_die (filename);
5438 new_unit->die_sib = old_unit;
5439 return new_unit;
5442 /* Close an include-file CU and reopen the enclosing one. */
5444 static dw_die_ref
5445 pop_compile_unit (dw_die_ref old_unit)
5447 dw_die_ref new_unit = old_unit->die_sib;
5449 old_unit->die_sib = NULL;
5450 return new_unit;
5453 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5454 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5455 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5457 /* Calculate the checksum of a location expression. */
5459 static inline void
5460 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5462 int tem;
5463 hashval_t hash = 0;
5465 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5466 CHECKSUM (tem);
5467 hash = hash_loc_operands (loc, hash);
5468 CHECKSUM (hash);
5471 /* Calculate the checksum of an attribute. */
5473 static void
5474 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5476 dw_loc_descr_ref loc;
5477 rtx r;
5479 CHECKSUM (at->dw_attr);
5481 /* We don't care that this was compiled with a different compiler
5482 snapshot; if the output is the same, that's what matters. */
5483 if (at->dw_attr == DW_AT_producer)
5484 return;
5486 switch (AT_class (at))
5488 case dw_val_class_const:
5489 CHECKSUM (at->dw_attr_val.v.val_int);
5490 break;
5491 case dw_val_class_unsigned_const:
5492 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5493 break;
5494 case dw_val_class_const_double:
5495 CHECKSUM (at->dw_attr_val.v.val_double);
5496 break;
5497 case dw_val_class_vec:
5498 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5499 (at->dw_attr_val.v.val_vec.length
5500 * at->dw_attr_val.v.val_vec.elt_size));
5501 break;
5502 case dw_val_class_flag:
5503 CHECKSUM (at->dw_attr_val.v.val_flag);
5504 break;
5505 case dw_val_class_str:
5506 CHECKSUM_STRING (AT_string (at));
5507 break;
5509 case dw_val_class_addr:
5510 r = AT_addr (at);
5511 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5512 CHECKSUM_STRING (XSTR (r, 0));
5513 break;
5515 case dw_val_class_offset:
5516 CHECKSUM (at->dw_attr_val.v.val_offset);
5517 break;
5519 case dw_val_class_loc:
5520 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5521 loc_checksum (loc, ctx);
5522 break;
5524 case dw_val_class_die_ref:
5525 die_checksum (AT_ref (at), ctx, mark);
5526 break;
5528 case dw_val_class_fde_ref:
5529 case dw_val_class_vms_delta:
5530 case dw_val_class_lbl_id:
5531 case dw_val_class_lineptr:
5532 case dw_val_class_macptr:
5533 case dw_val_class_high_pc:
5534 break;
5536 case dw_val_class_file:
5537 CHECKSUM_STRING (AT_file (at)->filename);
5538 break;
5540 case dw_val_class_data8:
5541 CHECKSUM (at->dw_attr_val.v.val_data8);
5542 break;
5544 default:
5545 break;
5549 /* Calculate the checksum of a DIE. */
5551 static void
5552 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5554 dw_die_ref c;
5555 dw_attr_ref a;
5556 unsigned ix;
5558 /* To avoid infinite recursion. */
5559 if (die->die_mark)
5561 CHECKSUM (die->die_mark);
5562 return;
5564 die->die_mark = ++(*mark);
5566 CHECKSUM (die->die_tag);
5568 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5569 attr_checksum (a, ctx, mark);
5571 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5574 #undef CHECKSUM
5575 #undef CHECKSUM_BLOCK
5576 #undef CHECKSUM_STRING
5578 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5579 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5580 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5581 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5582 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5583 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5584 #define CHECKSUM_ATTR(FOO) \
5585 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5587 /* Calculate the checksum of a number in signed LEB128 format. */
5589 static void
5590 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5592 unsigned char byte;
5593 bool more;
5595 while (1)
5597 byte = (value & 0x7f);
5598 value >>= 7;
5599 more = !((value == 0 && (byte & 0x40) == 0)
5600 || (value == -1 && (byte & 0x40) != 0));
5601 if (more)
5602 byte |= 0x80;
5603 CHECKSUM (byte);
5604 if (!more)
5605 break;
5609 /* Calculate the checksum of a number in unsigned LEB128 format. */
5611 static void
5612 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5614 while (1)
5616 unsigned char byte = (value & 0x7f);
5617 value >>= 7;
5618 if (value != 0)
5619 /* More bytes to follow. */
5620 byte |= 0x80;
5621 CHECKSUM (byte);
5622 if (value == 0)
5623 break;
5627 /* Checksum the context of the DIE. This adds the names of any
5628 surrounding namespaces or structures to the checksum. */
5630 static void
5631 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5633 const char *name;
5634 dw_die_ref spec;
5635 int tag = die->die_tag;
5637 if (tag != DW_TAG_namespace
5638 && tag != DW_TAG_structure_type
5639 && tag != DW_TAG_class_type)
5640 return;
5642 name = get_AT_string (die, DW_AT_name);
5644 spec = get_AT_ref (die, DW_AT_specification);
5645 if (spec != NULL)
5646 die = spec;
5648 if (die->die_parent != NULL)
5649 checksum_die_context (die->die_parent, ctx);
5651 CHECKSUM_ULEB128 ('C');
5652 CHECKSUM_ULEB128 (tag);
5653 if (name != NULL)
5654 CHECKSUM_STRING (name);
5657 /* Calculate the checksum of a location expression. */
5659 static inline void
5660 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5662 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5663 were emitted as a DW_FORM_sdata instead of a location expression. */
5664 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5666 CHECKSUM_ULEB128 (DW_FORM_sdata);
5667 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5668 return;
5671 /* Otherwise, just checksum the raw location expression. */
5672 while (loc != NULL)
5674 hashval_t hash = 0;
5676 CHECKSUM_ULEB128 (loc->dtprel);
5677 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5678 hash = hash_loc_operands (loc, hash);
5679 CHECKSUM (hash);
5680 loc = loc->dw_loc_next;
5684 /* Calculate the checksum of an attribute. */
5686 static void
5687 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5688 struct md5_ctx *ctx, int *mark)
5690 dw_loc_descr_ref loc;
5691 rtx r;
5693 if (AT_class (at) == dw_val_class_die_ref)
5695 dw_die_ref target_die = AT_ref (at);
5697 /* For pointer and reference types, we checksum only the (qualified)
5698 name of the target type (if there is a name). For friend entries,
5699 we checksum only the (qualified) name of the target type or function.
5700 This allows the checksum to remain the same whether the target type
5701 is complete or not. */
5702 if ((at->dw_attr == DW_AT_type
5703 && (tag == DW_TAG_pointer_type
5704 || tag == DW_TAG_reference_type
5705 || tag == DW_TAG_rvalue_reference_type
5706 || tag == DW_TAG_ptr_to_member_type))
5707 || (at->dw_attr == DW_AT_friend
5708 && tag == DW_TAG_friend))
5710 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5712 if (name_attr != NULL)
5714 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5716 if (decl == NULL)
5717 decl = target_die;
5718 CHECKSUM_ULEB128 ('N');
5719 CHECKSUM_ULEB128 (at->dw_attr);
5720 if (decl->die_parent != NULL)
5721 checksum_die_context (decl->die_parent, ctx);
5722 CHECKSUM_ULEB128 ('E');
5723 CHECKSUM_STRING (AT_string (name_attr));
5724 return;
5728 /* For all other references to another DIE, we check to see if the
5729 target DIE has already been visited. If it has, we emit a
5730 backward reference; if not, we descend recursively. */
5731 if (target_die->die_mark > 0)
5733 CHECKSUM_ULEB128 ('R');
5734 CHECKSUM_ULEB128 (at->dw_attr);
5735 CHECKSUM_ULEB128 (target_die->die_mark);
5737 else
5739 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5741 if (decl == NULL)
5742 decl = target_die;
5743 target_die->die_mark = ++(*mark);
5744 CHECKSUM_ULEB128 ('T');
5745 CHECKSUM_ULEB128 (at->dw_attr);
5746 if (decl->die_parent != NULL)
5747 checksum_die_context (decl->die_parent, ctx);
5748 die_checksum_ordered (target_die, ctx, mark);
5750 return;
5753 CHECKSUM_ULEB128 ('A');
5754 CHECKSUM_ULEB128 (at->dw_attr);
5756 switch (AT_class (at))
5758 case dw_val_class_const:
5759 CHECKSUM_ULEB128 (DW_FORM_sdata);
5760 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5761 break;
5763 case dw_val_class_unsigned_const:
5764 CHECKSUM_ULEB128 (DW_FORM_sdata);
5765 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5766 break;
5768 case dw_val_class_const_double:
5769 CHECKSUM_ULEB128 (DW_FORM_block);
5770 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5771 CHECKSUM (at->dw_attr_val.v.val_double);
5772 break;
5774 case dw_val_class_vec:
5775 CHECKSUM_ULEB128 (DW_FORM_block);
5776 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5777 * at->dw_attr_val.v.val_vec.elt_size);
5778 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5779 (at->dw_attr_val.v.val_vec.length
5780 * at->dw_attr_val.v.val_vec.elt_size));
5781 break;
5783 case dw_val_class_flag:
5784 CHECKSUM_ULEB128 (DW_FORM_flag);
5785 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5786 break;
5788 case dw_val_class_str:
5789 CHECKSUM_ULEB128 (DW_FORM_string);
5790 CHECKSUM_STRING (AT_string (at));
5791 break;
5793 case dw_val_class_addr:
5794 r = AT_addr (at);
5795 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5796 CHECKSUM_ULEB128 (DW_FORM_string);
5797 CHECKSUM_STRING (XSTR (r, 0));
5798 break;
5800 case dw_val_class_offset:
5801 CHECKSUM_ULEB128 (DW_FORM_sdata);
5802 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5803 break;
5805 case dw_val_class_loc:
5806 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5807 loc_checksum_ordered (loc, ctx);
5808 break;
5810 case dw_val_class_fde_ref:
5811 case dw_val_class_lbl_id:
5812 case dw_val_class_lineptr:
5813 case dw_val_class_macptr:
5814 case dw_val_class_high_pc:
5815 break;
5817 case dw_val_class_file:
5818 CHECKSUM_ULEB128 (DW_FORM_string);
5819 CHECKSUM_STRING (AT_file (at)->filename);
5820 break;
5822 case dw_val_class_data8:
5823 CHECKSUM (at->dw_attr_val.v.val_data8);
5824 break;
5826 default:
5827 break;
5831 struct checksum_attributes
5833 dw_attr_ref at_name;
5834 dw_attr_ref at_type;
5835 dw_attr_ref at_friend;
5836 dw_attr_ref at_accessibility;
5837 dw_attr_ref at_address_class;
5838 dw_attr_ref at_allocated;
5839 dw_attr_ref at_artificial;
5840 dw_attr_ref at_associated;
5841 dw_attr_ref at_binary_scale;
5842 dw_attr_ref at_bit_offset;
5843 dw_attr_ref at_bit_size;
5844 dw_attr_ref at_bit_stride;
5845 dw_attr_ref at_byte_size;
5846 dw_attr_ref at_byte_stride;
5847 dw_attr_ref at_const_value;
5848 dw_attr_ref at_containing_type;
5849 dw_attr_ref at_count;
5850 dw_attr_ref at_data_location;
5851 dw_attr_ref at_data_member_location;
5852 dw_attr_ref at_decimal_scale;
5853 dw_attr_ref at_decimal_sign;
5854 dw_attr_ref at_default_value;
5855 dw_attr_ref at_digit_count;
5856 dw_attr_ref at_discr;
5857 dw_attr_ref at_discr_list;
5858 dw_attr_ref at_discr_value;
5859 dw_attr_ref at_encoding;
5860 dw_attr_ref at_endianity;
5861 dw_attr_ref at_explicit;
5862 dw_attr_ref at_is_optional;
5863 dw_attr_ref at_location;
5864 dw_attr_ref at_lower_bound;
5865 dw_attr_ref at_mutable;
5866 dw_attr_ref at_ordering;
5867 dw_attr_ref at_picture_string;
5868 dw_attr_ref at_prototyped;
5869 dw_attr_ref at_small;
5870 dw_attr_ref at_segment;
5871 dw_attr_ref at_string_length;
5872 dw_attr_ref at_threads_scaled;
5873 dw_attr_ref at_upper_bound;
5874 dw_attr_ref at_use_location;
5875 dw_attr_ref at_use_UTF8;
5876 dw_attr_ref at_variable_parameter;
5877 dw_attr_ref at_virtuality;
5878 dw_attr_ref at_visibility;
5879 dw_attr_ref at_vtable_elem_location;
5882 /* Collect the attributes that we will want to use for the checksum. */
5884 static void
5885 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5887 dw_attr_ref a;
5888 unsigned ix;
5890 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5892 switch (a->dw_attr)
5894 case DW_AT_name:
5895 attrs->at_name = a;
5896 break;
5897 case DW_AT_type:
5898 attrs->at_type = a;
5899 break;
5900 case DW_AT_friend:
5901 attrs->at_friend = a;
5902 break;
5903 case DW_AT_accessibility:
5904 attrs->at_accessibility = a;
5905 break;
5906 case DW_AT_address_class:
5907 attrs->at_address_class = a;
5908 break;
5909 case DW_AT_allocated:
5910 attrs->at_allocated = a;
5911 break;
5912 case DW_AT_artificial:
5913 attrs->at_artificial = a;
5914 break;
5915 case DW_AT_associated:
5916 attrs->at_associated = a;
5917 break;
5918 case DW_AT_binary_scale:
5919 attrs->at_binary_scale = a;
5920 break;
5921 case DW_AT_bit_offset:
5922 attrs->at_bit_offset = a;
5923 break;
5924 case DW_AT_bit_size:
5925 attrs->at_bit_size = a;
5926 break;
5927 case DW_AT_bit_stride:
5928 attrs->at_bit_stride = a;
5929 break;
5930 case DW_AT_byte_size:
5931 attrs->at_byte_size = a;
5932 break;
5933 case DW_AT_byte_stride:
5934 attrs->at_byte_stride = a;
5935 break;
5936 case DW_AT_const_value:
5937 attrs->at_const_value = a;
5938 break;
5939 case DW_AT_containing_type:
5940 attrs->at_containing_type = a;
5941 break;
5942 case DW_AT_count:
5943 attrs->at_count = a;
5944 break;
5945 case DW_AT_data_location:
5946 attrs->at_data_location = a;
5947 break;
5948 case DW_AT_data_member_location:
5949 attrs->at_data_member_location = a;
5950 break;
5951 case DW_AT_decimal_scale:
5952 attrs->at_decimal_scale = a;
5953 break;
5954 case DW_AT_decimal_sign:
5955 attrs->at_decimal_sign = a;
5956 break;
5957 case DW_AT_default_value:
5958 attrs->at_default_value = a;
5959 break;
5960 case DW_AT_digit_count:
5961 attrs->at_digit_count = a;
5962 break;
5963 case DW_AT_discr:
5964 attrs->at_discr = a;
5965 break;
5966 case DW_AT_discr_list:
5967 attrs->at_discr_list = a;
5968 break;
5969 case DW_AT_discr_value:
5970 attrs->at_discr_value = a;
5971 break;
5972 case DW_AT_encoding:
5973 attrs->at_encoding = a;
5974 break;
5975 case DW_AT_endianity:
5976 attrs->at_endianity = a;
5977 break;
5978 case DW_AT_explicit:
5979 attrs->at_explicit = a;
5980 break;
5981 case DW_AT_is_optional:
5982 attrs->at_is_optional = a;
5983 break;
5984 case DW_AT_location:
5985 attrs->at_location = a;
5986 break;
5987 case DW_AT_lower_bound:
5988 attrs->at_lower_bound = a;
5989 break;
5990 case DW_AT_mutable:
5991 attrs->at_mutable = a;
5992 break;
5993 case DW_AT_ordering:
5994 attrs->at_ordering = a;
5995 break;
5996 case DW_AT_picture_string:
5997 attrs->at_picture_string = a;
5998 break;
5999 case DW_AT_prototyped:
6000 attrs->at_prototyped = a;
6001 break;
6002 case DW_AT_small:
6003 attrs->at_small = a;
6004 break;
6005 case DW_AT_segment:
6006 attrs->at_segment = a;
6007 break;
6008 case DW_AT_string_length:
6009 attrs->at_string_length = a;
6010 break;
6011 case DW_AT_threads_scaled:
6012 attrs->at_threads_scaled = a;
6013 break;
6014 case DW_AT_upper_bound:
6015 attrs->at_upper_bound = a;
6016 break;
6017 case DW_AT_use_location:
6018 attrs->at_use_location = a;
6019 break;
6020 case DW_AT_use_UTF8:
6021 attrs->at_use_UTF8 = a;
6022 break;
6023 case DW_AT_variable_parameter:
6024 attrs->at_variable_parameter = a;
6025 break;
6026 case DW_AT_virtuality:
6027 attrs->at_virtuality = a;
6028 break;
6029 case DW_AT_visibility:
6030 attrs->at_visibility = a;
6031 break;
6032 case DW_AT_vtable_elem_location:
6033 attrs->at_vtable_elem_location = a;
6034 break;
6035 default:
6036 break;
6041 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6043 static void
6044 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6046 dw_die_ref c;
6047 dw_die_ref decl;
6048 struct checksum_attributes attrs;
6050 CHECKSUM_ULEB128 ('D');
6051 CHECKSUM_ULEB128 (die->die_tag);
6053 memset (&attrs, 0, sizeof (attrs));
6055 decl = get_AT_ref (die, DW_AT_specification);
6056 if (decl != NULL)
6057 collect_checksum_attributes (&attrs, decl);
6058 collect_checksum_attributes (&attrs, die);
6060 CHECKSUM_ATTR (attrs.at_name);
6061 CHECKSUM_ATTR (attrs.at_accessibility);
6062 CHECKSUM_ATTR (attrs.at_address_class);
6063 CHECKSUM_ATTR (attrs.at_allocated);
6064 CHECKSUM_ATTR (attrs.at_artificial);
6065 CHECKSUM_ATTR (attrs.at_associated);
6066 CHECKSUM_ATTR (attrs.at_binary_scale);
6067 CHECKSUM_ATTR (attrs.at_bit_offset);
6068 CHECKSUM_ATTR (attrs.at_bit_size);
6069 CHECKSUM_ATTR (attrs.at_bit_stride);
6070 CHECKSUM_ATTR (attrs.at_byte_size);
6071 CHECKSUM_ATTR (attrs.at_byte_stride);
6072 CHECKSUM_ATTR (attrs.at_const_value);
6073 CHECKSUM_ATTR (attrs.at_containing_type);
6074 CHECKSUM_ATTR (attrs.at_count);
6075 CHECKSUM_ATTR (attrs.at_data_location);
6076 CHECKSUM_ATTR (attrs.at_data_member_location);
6077 CHECKSUM_ATTR (attrs.at_decimal_scale);
6078 CHECKSUM_ATTR (attrs.at_decimal_sign);
6079 CHECKSUM_ATTR (attrs.at_default_value);
6080 CHECKSUM_ATTR (attrs.at_digit_count);
6081 CHECKSUM_ATTR (attrs.at_discr);
6082 CHECKSUM_ATTR (attrs.at_discr_list);
6083 CHECKSUM_ATTR (attrs.at_discr_value);
6084 CHECKSUM_ATTR (attrs.at_encoding);
6085 CHECKSUM_ATTR (attrs.at_endianity);
6086 CHECKSUM_ATTR (attrs.at_explicit);
6087 CHECKSUM_ATTR (attrs.at_is_optional);
6088 CHECKSUM_ATTR (attrs.at_location);
6089 CHECKSUM_ATTR (attrs.at_lower_bound);
6090 CHECKSUM_ATTR (attrs.at_mutable);
6091 CHECKSUM_ATTR (attrs.at_ordering);
6092 CHECKSUM_ATTR (attrs.at_picture_string);
6093 CHECKSUM_ATTR (attrs.at_prototyped);
6094 CHECKSUM_ATTR (attrs.at_small);
6095 CHECKSUM_ATTR (attrs.at_segment);
6096 CHECKSUM_ATTR (attrs.at_string_length);
6097 CHECKSUM_ATTR (attrs.at_threads_scaled);
6098 CHECKSUM_ATTR (attrs.at_upper_bound);
6099 CHECKSUM_ATTR (attrs.at_use_location);
6100 CHECKSUM_ATTR (attrs.at_use_UTF8);
6101 CHECKSUM_ATTR (attrs.at_variable_parameter);
6102 CHECKSUM_ATTR (attrs.at_virtuality);
6103 CHECKSUM_ATTR (attrs.at_visibility);
6104 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6105 CHECKSUM_ATTR (attrs.at_type);
6106 CHECKSUM_ATTR (attrs.at_friend);
6108 /* Checksum the child DIEs. */
6109 c = die->die_child;
6110 if (c) do {
6111 dw_attr_ref name_attr;
6113 c = c->die_sib;
6114 name_attr = get_AT (c, DW_AT_name);
6115 if (is_template_instantiation (c))
6117 /* Ignore instantiations of member type and function templates. */
6119 else if (name_attr != NULL
6120 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6122 /* Use a shallow checksum for named nested types and member
6123 functions. */
6124 CHECKSUM_ULEB128 ('S');
6125 CHECKSUM_ULEB128 (c->die_tag);
6126 CHECKSUM_STRING (AT_string (name_attr));
6128 else
6130 /* Use a deep checksum for other children. */
6131 /* Mark this DIE so it gets processed when unmarking. */
6132 if (c->die_mark == 0)
6133 c->die_mark = -1;
6134 die_checksum_ordered (c, ctx, mark);
6136 } while (c != die->die_child);
6138 CHECKSUM_ULEB128 (0);
6141 /* Add a type name and tag to a hash. */
6142 static void
6143 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6145 CHECKSUM_ULEB128 (tag);
6146 CHECKSUM_STRING (name);
6149 #undef CHECKSUM
6150 #undef CHECKSUM_STRING
6151 #undef CHECKSUM_ATTR
6152 #undef CHECKSUM_LEB128
6153 #undef CHECKSUM_ULEB128
6155 /* Generate the type signature for DIE. This is computed by generating an
6156 MD5 checksum over the DIE's tag, its relevant attributes, and its
6157 children. Attributes that are references to other DIEs are processed
6158 by recursion, using the MARK field to prevent infinite recursion.
6159 If the DIE is nested inside a namespace or another type, we also
6160 need to include that context in the signature. The lower 64 bits
6161 of the resulting MD5 checksum comprise the signature. */
6163 static void
6164 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6166 int mark;
6167 const char *name;
6168 unsigned char checksum[16];
6169 struct md5_ctx ctx;
6170 dw_die_ref decl;
6171 dw_die_ref parent;
6173 name = get_AT_string (die, DW_AT_name);
6174 decl = get_AT_ref (die, DW_AT_specification);
6175 parent = get_die_parent (die);
6177 /* First, compute a signature for just the type name (and its surrounding
6178 context, if any. This is stored in the type unit DIE for link-time
6179 ODR (one-definition rule) checking. */
6181 if (is_cxx () && name != NULL)
6183 md5_init_ctx (&ctx);
6185 /* Checksum the names of surrounding namespaces and structures. */
6186 if (parent != NULL)
6187 checksum_die_context (parent, &ctx);
6189 /* Checksum the current DIE. */
6190 die_odr_checksum (die->die_tag, name, &ctx);
6191 md5_finish_ctx (&ctx, checksum);
6193 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6196 /* Next, compute the complete type signature. */
6198 md5_init_ctx (&ctx);
6199 mark = 1;
6200 die->die_mark = mark;
6202 /* Checksum the names of surrounding namespaces and structures. */
6203 if (parent != NULL)
6204 checksum_die_context (parent, &ctx);
6206 /* Checksum the DIE and its children. */
6207 die_checksum_ordered (die, &ctx, &mark);
6208 unmark_all_dies (die);
6209 md5_finish_ctx (&ctx, checksum);
6211 /* Store the signature in the type node and link the type DIE and the
6212 type node together. */
6213 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6214 DWARF_TYPE_SIGNATURE_SIZE);
6215 die->comdat_type_p = true;
6216 die->die_id.die_type_node = type_node;
6217 type_node->type_die = die;
6219 /* If the DIE is a specification, link its declaration to the type node
6220 as well. */
6221 if (decl != NULL)
6223 decl->comdat_type_p = true;
6224 decl->die_id.die_type_node = type_node;
6228 /* Do the location expressions look same? */
6229 static inline int
6230 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6232 return loc1->dw_loc_opc == loc2->dw_loc_opc
6233 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6234 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6237 /* Do the values look the same? */
6238 static int
6239 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6241 dw_loc_descr_ref loc1, loc2;
6242 rtx r1, r2;
6244 if (v1->val_class != v2->val_class)
6245 return 0;
6247 switch (v1->val_class)
6249 case dw_val_class_const:
6250 return v1->v.val_int == v2->v.val_int;
6251 case dw_val_class_unsigned_const:
6252 return v1->v.val_unsigned == v2->v.val_unsigned;
6253 case dw_val_class_const_double:
6254 return v1->v.val_double.high == v2->v.val_double.high
6255 && v1->v.val_double.low == v2->v.val_double.low;
6256 case dw_val_class_vec:
6257 if (v1->v.val_vec.length != v2->v.val_vec.length
6258 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6259 return 0;
6260 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6261 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6262 return 0;
6263 return 1;
6264 case dw_val_class_flag:
6265 return v1->v.val_flag == v2->v.val_flag;
6266 case dw_val_class_str:
6267 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6269 case dw_val_class_addr:
6270 r1 = v1->v.val_addr;
6271 r2 = v2->v.val_addr;
6272 if (GET_CODE (r1) != GET_CODE (r2))
6273 return 0;
6274 return !rtx_equal_p (r1, r2);
6276 case dw_val_class_offset:
6277 return v1->v.val_offset == v2->v.val_offset;
6279 case dw_val_class_loc:
6280 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6281 loc1 && loc2;
6282 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6283 if (!same_loc_p (loc1, loc2, mark))
6284 return 0;
6285 return !loc1 && !loc2;
6287 case dw_val_class_die_ref:
6288 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6290 case dw_val_class_fde_ref:
6291 case dw_val_class_vms_delta:
6292 case dw_val_class_lbl_id:
6293 case dw_val_class_lineptr:
6294 case dw_val_class_macptr:
6295 case dw_val_class_high_pc:
6296 return 1;
6298 case dw_val_class_file:
6299 return v1->v.val_file == v2->v.val_file;
6301 case dw_val_class_data8:
6302 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6304 default:
6305 return 1;
6309 /* Do the attributes look the same? */
6311 static int
6312 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6314 if (at1->dw_attr != at2->dw_attr)
6315 return 0;
6317 /* We don't care that this was compiled with a different compiler
6318 snapshot; if the output is the same, that's what matters. */
6319 if (at1->dw_attr == DW_AT_producer)
6320 return 1;
6322 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6325 /* Do the dies look the same? */
6327 static int
6328 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6330 dw_die_ref c1, c2;
6331 dw_attr_ref a1;
6332 unsigned ix;
6334 /* To avoid infinite recursion. */
6335 if (die1->die_mark)
6336 return die1->die_mark == die2->die_mark;
6337 die1->die_mark = die2->die_mark = ++(*mark);
6339 if (die1->die_tag != die2->die_tag)
6340 return 0;
6342 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6343 return 0;
6345 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6346 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6347 return 0;
6349 c1 = die1->die_child;
6350 c2 = die2->die_child;
6351 if (! c1)
6353 if (c2)
6354 return 0;
6356 else
6357 for (;;)
6359 if (!same_die_p (c1, c2, mark))
6360 return 0;
6361 c1 = c1->die_sib;
6362 c2 = c2->die_sib;
6363 if (c1 == die1->die_child)
6365 if (c2 == die2->die_child)
6366 break;
6367 else
6368 return 0;
6372 return 1;
6375 /* Do the dies look the same? Wrapper around same_die_p. */
6377 static int
6378 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6380 int mark = 0;
6381 int ret = same_die_p (die1, die2, &mark);
6383 unmark_all_dies (die1);
6384 unmark_all_dies (die2);
6386 return ret;
6389 /* The prefix to attach to symbols on DIEs in the current comdat debug
6390 info section. */
6391 static const char *comdat_symbol_id;
6393 /* The index of the current symbol within the current comdat CU. */
6394 static unsigned int comdat_symbol_number;
6396 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6397 children, and set comdat_symbol_id accordingly. */
6399 static void
6400 compute_section_prefix (dw_die_ref unit_die)
6402 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6403 const char *base = die_name ? lbasename (die_name) : "anonymous";
6404 char *name = XALLOCAVEC (char, strlen (base) + 64);
6405 char *p;
6406 int i, mark;
6407 unsigned char checksum[16];
6408 struct md5_ctx ctx;
6410 /* Compute the checksum of the DIE, then append part of it as hex digits to
6411 the name filename of the unit. */
6413 md5_init_ctx (&ctx);
6414 mark = 0;
6415 die_checksum (unit_die, &ctx, &mark);
6416 unmark_all_dies (unit_die);
6417 md5_finish_ctx (&ctx, checksum);
6419 sprintf (name, "%s.", base);
6420 clean_symbol_name (name);
6422 p = name + strlen (name);
6423 for (i = 0; i < 4; i++)
6425 sprintf (p, "%.2x", checksum[i]);
6426 p += 2;
6429 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6430 comdat_symbol_number = 0;
6433 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6435 static int
6436 is_type_die (dw_die_ref die)
6438 switch (die->die_tag)
6440 case DW_TAG_array_type:
6441 case DW_TAG_class_type:
6442 case DW_TAG_interface_type:
6443 case DW_TAG_enumeration_type:
6444 case DW_TAG_pointer_type:
6445 case DW_TAG_reference_type:
6446 case DW_TAG_rvalue_reference_type:
6447 case DW_TAG_string_type:
6448 case DW_TAG_structure_type:
6449 case DW_TAG_subroutine_type:
6450 case DW_TAG_union_type:
6451 case DW_TAG_ptr_to_member_type:
6452 case DW_TAG_set_type:
6453 case DW_TAG_subrange_type:
6454 case DW_TAG_base_type:
6455 case DW_TAG_const_type:
6456 case DW_TAG_file_type:
6457 case DW_TAG_packed_type:
6458 case DW_TAG_volatile_type:
6459 case DW_TAG_typedef:
6460 return 1;
6461 default:
6462 return 0;
6466 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6467 Basically, we want to choose the bits that are likely to be shared between
6468 compilations (types) and leave out the bits that are specific to individual
6469 compilations (functions). */
6471 static int
6472 is_comdat_die (dw_die_ref c)
6474 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6475 we do for stabs. The advantage is a greater likelihood of sharing between
6476 objects that don't include headers in the same order (and therefore would
6477 put the base types in a different comdat). jason 8/28/00 */
6479 if (c->die_tag == DW_TAG_base_type)
6480 return 0;
6482 if (c->die_tag == DW_TAG_pointer_type
6483 || c->die_tag == DW_TAG_reference_type
6484 || c->die_tag == DW_TAG_rvalue_reference_type
6485 || c->die_tag == DW_TAG_const_type
6486 || c->die_tag == DW_TAG_volatile_type)
6488 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6490 return t ? is_comdat_die (t) : 0;
6493 return is_type_die (c);
6496 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6497 compilation unit. */
6499 static int
6500 is_symbol_die (dw_die_ref c)
6502 return (is_type_die (c)
6503 || is_declaration_die (c)
6504 || c->die_tag == DW_TAG_namespace
6505 || c->die_tag == DW_TAG_module);
6508 /* Returns true iff C is a compile-unit DIE. */
6510 static inline bool
6511 is_cu_die (dw_die_ref c)
6513 return c && c->die_tag == DW_TAG_compile_unit;
6516 /* Returns true iff C is a unit DIE of some sort. */
6518 static inline bool
6519 is_unit_die (dw_die_ref c)
6521 return c && (c->die_tag == DW_TAG_compile_unit
6522 || c->die_tag == DW_TAG_partial_unit
6523 || c->die_tag == DW_TAG_type_unit);
6526 /* Returns true iff C is a namespace DIE. */
6528 static inline bool
6529 is_namespace_die (dw_die_ref c)
6531 return c && c->die_tag == DW_TAG_namespace;
6534 /* Returns true iff C is a class or structure DIE. */
6536 static inline bool
6537 is_class_die (dw_die_ref c)
6539 return c && (c->die_tag == DW_TAG_class_type
6540 || c->die_tag == DW_TAG_structure_type);
6543 /* Return non-zero if this DIE is a template parameter. */
6545 static inline bool
6546 is_template_parameter (dw_die_ref die)
6548 switch (die->die_tag)
6550 case DW_TAG_template_type_param:
6551 case DW_TAG_template_value_param:
6552 case DW_TAG_GNU_template_template_param:
6553 case DW_TAG_GNU_template_parameter_pack:
6554 return true;
6555 default:
6556 return false;
6560 /* Return non-zero if this DIE represents a template instantiation. */
6562 static inline bool
6563 is_template_instantiation (dw_die_ref die)
6565 dw_die_ref c;
6567 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6568 return false;
6569 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6570 return false;
6573 static char *
6574 gen_internal_sym (const char *prefix)
6576 char buf[256];
6578 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6579 return xstrdup (buf);
6582 /* Assign symbols to all worthy DIEs under DIE. */
6584 static void
6585 assign_symbol_names (dw_die_ref die)
6587 dw_die_ref c;
6589 if (is_symbol_die (die) && !die->comdat_type_p)
6591 if (comdat_symbol_id)
6593 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6595 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6596 comdat_symbol_id, comdat_symbol_number++);
6597 die->die_id.die_symbol = xstrdup (p);
6599 else
6600 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6603 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6606 struct cu_hash_table_entry
6608 dw_die_ref cu;
6609 unsigned min_comdat_num, max_comdat_num;
6610 struct cu_hash_table_entry *next;
6613 /* Helpers to manipulate hash table of CUs. */
6615 struct cu_hash_table_entry_hasher
6617 typedef cu_hash_table_entry value_type;
6618 typedef die_struct compare_type;
6619 static inline hashval_t hash (const value_type *);
6620 static inline bool equal (const value_type *, const compare_type *);
6621 static inline void remove (value_type *);
6624 inline hashval_t
6625 cu_hash_table_entry_hasher::hash (const value_type *entry)
6627 return htab_hash_string (entry->cu->die_id.die_symbol);
6630 inline bool
6631 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6632 const compare_type *entry2)
6634 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6637 inline void
6638 cu_hash_table_entry_hasher::remove (value_type *entry)
6640 struct cu_hash_table_entry *next;
6642 while (entry)
6644 next = entry->next;
6645 free (entry);
6646 entry = next;
6650 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6652 /* Check whether we have already seen this CU and set up SYM_NUM
6653 accordingly. */
6654 static int
6655 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6657 struct cu_hash_table_entry dummy;
6658 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6660 dummy.max_comdat_num = 0;
6662 slot = htable.find_slot_with_hash (cu,
6663 htab_hash_string (cu->die_id.die_symbol),
6664 INSERT);
6665 entry = *slot;
6667 for (; entry; last = entry, entry = entry->next)
6669 if (same_die_p_wrap (cu, entry->cu))
6670 break;
6673 if (entry)
6675 *sym_num = entry->min_comdat_num;
6676 return 1;
6679 entry = XCNEW (struct cu_hash_table_entry);
6680 entry->cu = cu;
6681 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6682 entry->next = *slot;
6683 *slot = entry;
6685 return 0;
6688 /* Record SYM_NUM to record of CU in HTABLE. */
6689 static void
6690 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6691 unsigned int sym_num)
6693 struct cu_hash_table_entry **slot, *entry;
6695 slot = htable.find_slot_with_hash (cu,
6696 htab_hash_string (cu->die_id.die_symbol),
6697 NO_INSERT);
6698 entry = *slot;
6700 entry->max_comdat_num = sym_num;
6703 /* Traverse the DIE (which is always comp_unit_die), and set up
6704 additional compilation units for each of the include files we see
6705 bracketed by BINCL/EINCL. */
6707 static void
6708 break_out_includes (dw_die_ref die)
6710 dw_die_ref c;
6711 dw_die_ref unit = NULL;
6712 limbo_die_node *node, **pnode;
6713 cu_hash_type cu_hash_table;
6715 c = die->die_child;
6716 if (c) do {
6717 dw_die_ref prev = c;
6718 c = c->die_sib;
6719 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6720 || (unit && is_comdat_die (c)))
6722 dw_die_ref next = c->die_sib;
6724 /* This DIE is for a secondary CU; remove it from the main one. */
6725 remove_child_with_prev (c, prev);
6727 if (c->die_tag == DW_TAG_GNU_BINCL)
6728 unit = push_new_compile_unit (unit, c);
6729 else if (c->die_tag == DW_TAG_GNU_EINCL)
6730 unit = pop_compile_unit (unit);
6731 else
6732 add_child_die (unit, c);
6733 c = next;
6734 if (c == die->die_child)
6735 break;
6737 } while (c != die->die_child);
6739 #if 0
6740 /* We can only use this in debugging, since the frontend doesn't check
6741 to make sure that we leave every include file we enter. */
6742 gcc_assert (!unit);
6743 #endif
6745 assign_symbol_names (die);
6746 cu_hash_table.create (10);
6747 for (node = limbo_die_list, pnode = &limbo_die_list;
6748 node;
6749 node = node->next)
6751 int is_dupl;
6753 compute_section_prefix (node->die);
6754 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6755 &comdat_symbol_number);
6756 assign_symbol_names (node->die);
6757 if (is_dupl)
6758 *pnode = node->next;
6759 else
6761 pnode = &node->next;
6762 record_comdat_symbol_number (node->die, cu_hash_table,
6763 comdat_symbol_number);
6766 cu_hash_table.dispose ();
6769 /* Return non-zero if this DIE is a declaration. */
6771 static int
6772 is_declaration_die (dw_die_ref die)
6774 dw_attr_ref a;
6775 unsigned ix;
6777 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6778 if (a->dw_attr == DW_AT_declaration)
6779 return 1;
6781 return 0;
6784 /* Return non-zero if this DIE is nested inside a subprogram. */
6786 static int
6787 is_nested_in_subprogram (dw_die_ref die)
6789 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6791 if (decl == NULL)
6792 decl = die;
6793 return local_scope_p (decl);
6796 /* Return non-zero if this DIE contains a defining declaration of a
6797 subprogram. */
6799 static int
6800 contains_subprogram_definition (dw_die_ref die)
6802 dw_die_ref c;
6804 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6805 return 1;
6806 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6807 return 0;
6810 /* Return non-zero if this is a type DIE that should be moved to a
6811 COMDAT .debug_types section. */
6813 static int
6814 should_move_die_to_comdat (dw_die_ref die)
6816 switch (die->die_tag)
6818 case DW_TAG_class_type:
6819 case DW_TAG_structure_type:
6820 case DW_TAG_enumeration_type:
6821 case DW_TAG_union_type:
6822 /* Don't move declarations, inlined instances, or types nested in a
6823 subprogram. */
6824 if (is_declaration_die (die)
6825 || get_AT (die, DW_AT_abstract_origin)
6826 || is_nested_in_subprogram (die))
6827 return 0;
6828 /* A type definition should never contain a subprogram definition. */
6829 gcc_assert (!contains_subprogram_definition (die));
6830 return 1;
6831 case DW_TAG_array_type:
6832 case DW_TAG_interface_type:
6833 case DW_TAG_pointer_type:
6834 case DW_TAG_reference_type:
6835 case DW_TAG_rvalue_reference_type:
6836 case DW_TAG_string_type:
6837 case DW_TAG_subroutine_type:
6838 case DW_TAG_ptr_to_member_type:
6839 case DW_TAG_set_type:
6840 case DW_TAG_subrange_type:
6841 case DW_TAG_base_type:
6842 case DW_TAG_const_type:
6843 case DW_TAG_file_type:
6844 case DW_TAG_packed_type:
6845 case DW_TAG_volatile_type:
6846 case DW_TAG_typedef:
6847 default:
6848 return 0;
6852 /* Make a clone of DIE. */
6854 static dw_die_ref
6855 clone_die (dw_die_ref die)
6857 dw_die_ref clone;
6858 dw_attr_ref a;
6859 unsigned ix;
6861 clone = ggc_alloc_cleared_die_node ();
6862 clone->die_tag = die->die_tag;
6864 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6865 add_dwarf_attr (clone, a);
6867 return clone;
6870 /* Make a clone of the tree rooted at DIE. */
6872 static dw_die_ref
6873 clone_tree (dw_die_ref die)
6875 dw_die_ref c;
6876 dw_die_ref clone = clone_die (die);
6878 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6880 return clone;
6883 /* Make a clone of DIE as a declaration. */
6885 static dw_die_ref
6886 clone_as_declaration (dw_die_ref die)
6888 dw_die_ref clone;
6889 dw_die_ref decl;
6890 dw_attr_ref a;
6891 unsigned ix;
6893 /* If the DIE is already a declaration, just clone it. */
6894 if (is_declaration_die (die))
6895 return clone_die (die);
6897 /* If the DIE is a specification, just clone its declaration DIE. */
6898 decl = get_AT_ref (die, DW_AT_specification);
6899 if (decl != NULL)
6901 clone = clone_die (decl);
6902 if (die->comdat_type_p)
6903 add_AT_die_ref (clone, DW_AT_signature, die);
6904 return clone;
6907 clone = ggc_alloc_cleared_die_node ();
6908 clone->die_tag = die->die_tag;
6910 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6912 /* We don't want to copy over all attributes.
6913 For example we don't want DW_AT_byte_size because otherwise we will no
6914 longer have a declaration and GDB will treat it as a definition. */
6916 switch (a->dw_attr)
6918 case DW_AT_artificial:
6919 case DW_AT_containing_type:
6920 case DW_AT_external:
6921 case DW_AT_name:
6922 case DW_AT_type:
6923 case DW_AT_virtuality:
6924 case DW_AT_linkage_name:
6925 case DW_AT_MIPS_linkage_name:
6926 add_dwarf_attr (clone, a);
6927 break;
6928 case DW_AT_byte_size:
6929 default:
6930 break;
6934 if (die->comdat_type_p)
6935 add_AT_die_ref (clone, DW_AT_signature, die);
6937 add_AT_flag (clone, DW_AT_declaration, 1);
6938 return clone;
6942 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6944 struct decl_table_entry
6946 dw_die_ref orig;
6947 dw_die_ref copy;
6950 /* Helpers to manipulate hash table of copied declarations. */
6952 /* Hashtable helpers. */
6954 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6956 typedef decl_table_entry value_type;
6957 typedef die_struct compare_type;
6958 static inline hashval_t hash (const value_type *);
6959 static inline bool equal (const value_type *, const compare_type *);
6962 inline hashval_t
6963 decl_table_entry_hasher::hash (const value_type *entry)
6965 return htab_hash_pointer (entry->orig);
6968 inline bool
6969 decl_table_entry_hasher::equal (const value_type *entry1,
6970 const compare_type *entry2)
6972 return entry1->orig == entry2;
6975 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6977 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6978 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6979 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6980 to check if the ancestor has already been copied into UNIT. */
6982 static dw_die_ref
6983 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6985 dw_die_ref parent = die->die_parent;
6986 dw_die_ref new_parent = unit;
6987 dw_die_ref copy;
6988 decl_table_entry **slot = NULL;
6989 struct decl_table_entry *entry = NULL;
6991 if (decl_table.is_created ())
6993 /* Check if the entry has already been copied to UNIT. */
6994 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
6995 INSERT);
6996 if (*slot != HTAB_EMPTY_ENTRY)
6998 entry = *slot;
6999 return entry->copy;
7002 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7003 entry = XCNEW (struct decl_table_entry);
7004 entry->orig = die;
7005 entry->copy = NULL;
7006 *slot = entry;
7009 if (parent != NULL)
7011 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7012 if (spec != NULL)
7013 parent = spec;
7014 if (!is_unit_die (parent))
7015 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7018 copy = clone_as_declaration (die);
7019 add_child_die (new_parent, copy);
7021 if (decl_table.is_created ())
7023 /* Record the pointer to the copy. */
7024 entry->copy = copy;
7027 return copy;
7029 /* Copy the declaration context to the new type unit DIE. This includes
7030 any surrounding namespace or type declarations. If the DIE has an
7031 AT_specification attribute, it also includes attributes and children
7032 attached to the specification, and returns a pointer to the original
7033 parent of the declaration DIE. Returns NULL otherwise. */
7035 static dw_die_ref
7036 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7038 dw_die_ref decl;
7039 dw_die_ref new_decl;
7040 dw_die_ref orig_parent = NULL;
7042 decl = get_AT_ref (die, DW_AT_specification);
7043 if (decl == NULL)
7044 decl = die;
7045 else
7047 unsigned ix;
7048 dw_die_ref c;
7049 dw_attr_ref a;
7051 /* The original DIE will be changed to a declaration, and must
7052 be moved to be a child of the original declaration DIE. */
7053 orig_parent = decl->die_parent;
7055 /* Copy the type node pointer from the new DIE to the original
7056 declaration DIE so we can forward references later. */
7057 decl->comdat_type_p = true;
7058 decl->die_id.die_type_node = die->die_id.die_type_node;
7060 remove_AT (die, DW_AT_specification);
7062 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7064 if (a->dw_attr != DW_AT_name
7065 && a->dw_attr != DW_AT_declaration
7066 && a->dw_attr != DW_AT_external)
7067 add_dwarf_attr (die, a);
7070 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7073 if (decl->die_parent != NULL
7074 && !is_unit_die (decl->die_parent))
7076 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7077 if (new_decl != NULL)
7079 remove_AT (new_decl, DW_AT_signature);
7080 add_AT_specification (die, new_decl);
7084 return orig_parent;
7087 /* Generate the skeleton ancestor tree for the given NODE, then clone
7088 the DIE and add the clone into the tree. */
7090 static void
7091 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7093 if (node->new_die != NULL)
7094 return;
7096 node->new_die = clone_as_declaration (node->old_die);
7098 if (node->parent != NULL)
7100 generate_skeleton_ancestor_tree (node->parent);
7101 add_child_die (node->parent->new_die, node->new_die);
7105 /* Generate a skeleton tree of DIEs containing any declarations that are
7106 found in the original tree. We traverse the tree looking for declaration
7107 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7109 static void
7110 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7112 skeleton_chain_node node;
7113 dw_die_ref c;
7114 dw_die_ref first;
7115 dw_die_ref prev = NULL;
7116 dw_die_ref next = NULL;
7118 node.parent = parent;
7120 first = c = parent->old_die->die_child;
7121 if (c)
7122 next = c->die_sib;
7123 if (c) do {
7124 if (prev == NULL || prev->die_sib == c)
7125 prev = c;
7126 c = next;
7127 next = (c == first ? NULL : c->die_sib);
7128 node.old_die = c;
7129 node.new_die = NULL;
7130 if (is_declaration_die (c))
7132 if (is_template_instantiation (c))
7134 /* Instantiated templates do not need to be cloned into the
7135 type unit. Just move the DIE and its children back to
7136 the skeleton tree (in the main CU). */
7137 remove_child_with_prev (c, prev);
7138 add_child_die (parent->new_die, c);
7139 c = prev;
7141 else
7143 /* Clone the existing DIE, move the original to the skeleton
7144 tree (which is in the main CU), and put the clone, with
7145 all the original's children, where the original came from
7146 (which is about to be moved to the type unit). */
7147 dw_die_ref clone = clone_die (c);
7148 move_all_children (c, clone);
7150 replace_child (c, clone, prev);
7151 generate_skeleton_ancestor_tree (parent);
7152 add_child_die (parent->new_die, c);
7153 node.new_die = c;
7154 c = clone;
7157 generate_skeleton_bottom_up (&node);
7158 } while (next != NULL);
7161 /* Wrapper function for generate_skeleton_bottom_up. */
7163 static dw_die_ref
7164 generate_skeleton (dw_die_ref die)
7166 skeleton_chain_node node;
7168 node.old_die = die;
7169 node.new_die = NULL;
7170 node.parent = NULL;
7172 /* If this type definition is nested inside another type,
7173 and is not an instantiation of a template, always leave
7174 at least a declaration in its place. */
7175 if (die->die_parent != NULL
7176 && is_type_die (die->die_parent)
7177 && !is_template_instantiation (die))
7178 node.new_die = clone_as_declaration (die);
7180 generate_skeleton_bottom_up (&node);
7181 return node.new_die;
7184 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7185 declaration. The original DIE is moved to a new compile unit so that
7186 existing references to it follow it to the new location. If any of the
7187 original DIE's descendants is a declaration, we need to replace the
7188 original DIE with a skeleton tree and move the declarations back into the
7189 skeleton tree. */
7191 static dw_die_ref
7192 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7193 dw_die_ref prev)
7195 dw_die_ref skeleton, orig_parent;
7197 /* Copy the declaration context to the type unit DIE. If the returned
7198 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7199 that DIE. */
7200 orig_parent = copy_declaration_context (unit, child);
7202 skeleton = generate_skeleton (child);
7203 if (skeleton == NULL)
7204 remove_child_with_prev (child, prev);
7205 else
7207 skeleton->comdat_type_p = true;
7208 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7210 /* If the original DIE was a specification, we need to put
7211 the skeleton under the parent DIE of the declaration.
7212 This leaves the original declaration in the tree, but
7213 it will be pruned later since there are no longer any
7214 references to it. */
7215 if (orig_parent != NULL)
7217 remove_child_with_prev (child, prev);
7218 add_child_die (orig_parent, skeleton);
7220 else
7221 replace_child (child, skeleton, prev);
7224 return skeleton;
7227 /* Traverse the DIE and set up additional .debug_types sections for each
7228 type worthy of being placed in a COMDAT section. */
7230 static void
7231 break_out_comdat_types (dw_die_ref die)
7233 dw_die_ref c;
7234 dw_die_ref first;
7235 dw_die_ref prev = NULL;
7236 dw_die_ref next = NULL;
7237 dw_die_ref unit = NULL;
7239 first = c = die->die_child;
7240 if (c)
7241 next = c->die_sib;
7242 if (c) do {
7243 if (prev == NULL || prev->die_sib == c)
7244 prev = c;
7245 c = next;
7246 next = (c == first ? NULL : c->die_sib);
7247 if (should_move_die_to_comdat (c))
7249 dw_die_ref replacement;
7250 comdat_type_node_ref type_node;
7252 /* Break out nested types into their own type units. */
7253 break_out_comdat_types (c);
7255 /* Create a new type unit DIE as the root for the new tree, and
7256 add it to the list of comdat types. */
7257 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7258 add_AT_unsigned (unit, DW_AT_language,
7259 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7260 type_node = ggc_alloc_cleared_comdat_type_node ();
7261 type_node->root_die = unit;
7262 type_node->next = comdat_type_list;
7263 comdat_type_list = type_node;
7265 /* Generate the type signature. */
7266 generate_type_signature (c, type_node);
7268 /* Copy the declaration context, attributes, and children of the
7269 declaration into the new type unit DIE, then remove this DIE
7270 from the main CU (or replace it with a skeleton if necessary). */
7271 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7272 type_node->skeleton_die = replacement;
7274 /* Add the DIE to the new compunit. */
7275 add_child_die (unit, c);
7277 if (replacement != NULL)
7278 c = replacement;
7280 else if (c->die_tag == DW_TAG_namespace
7281 || c->die_tag == DW_TAG_class_type
7282 || c->die_tag == DW_TAG_structure_type
7283 || c->die_tag == DW_TAG_union_type)
7285 /* Look for nested types that can be broken out. */
7286 break_out_comdat_types (c);
7288 } while (next != NULL);
7291 /* Like clone_tree, but additionally enter all the children into
7292 the hash table decl_table. */
7294 static dw_die_ref
7295 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7297 dw_die_ref c;
7298 dw_die_ref clone = clone_die (die);
7299 struct decl_table_entry *entry;
7300 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7301 htab_hash_pointer (die), INSERT);
7302 /* Assert that DIE isn't in the hash table yet. If it would be there
7303 before, the ancestors would be necessarily there as well, therefore
7304 clone_tree_hash wouldn't be called. */
7305 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7306 entry = XCNEW (struct decl_table_entry);
7307 entry->orig = die;
7308 entry->copy = clone;
7309 *slot = entry;
7311 FOR_EACH_CHILD (die, c,
7312 add_child_die (clone, clone_tree_hash (c, decl_table)));
7314 return clone;
7317 /* Walk the DIE and its children, looking for references to incomplete
7318 or trivial types that are unmarked (i.e., that are not in the current
7319 type_unit). */
7321 static void
7322 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7324 dw_die_ref c;
7325 dw_attr_ref a;
7326 unsigned ix;
7328 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7330 if (AT_class (a) == dw_val_class_die_ref)
7332 dw_die_ref targ = AT_ref (a);
7333 decl_table_entry **slot;
7334 struct decl_table_entry *entry;
7336 if (targ->die_mark != 0 || targ->comdat_type_p)
7337 continue;
7339 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7340 INSERT);
7342 if (*slot != HTAB_EMPTY_ENTRY)
7344 /* TARG has already been copied, so we just need to
7345 modify the reference to point to the copy. */
7346 entry = *slot;
7347 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7349 else
7351 dw_die_ref parent = unit;
7352 dw_die_ref copy = clone_die (targ);
7354 /* Record in DECL_TABLE that TARG has been copied.
7355 Need to do this now, before the recursive call,
7356 because DECL_TABLE may be expanded and SLOT
7357 would no longer be a valid pointer. */
7358 entry = XCNEW (struct decl_table_entry);
7359 entry->orig = targ;
7360 entry->copy = copy;
7361 *slot = entry;
7363 FOR_EACH_CHILD (targ, c,
7364 add_child_die (copy,
7365 clone_tree_hash (c, decl_table)));
7367 /* Make sure the cloned tree is marked as part of the
7368 type unit. */
7369 mark_dies (copy);
7371 /* If TARG has surrounding context, copy its ancestor tree
7372 into the new type unit. */
7373 if (targ->die_parent != NULL
7374 && !is_unit_die (targ->die_parent))
7375 parent = copy_ancestor_tree (unit, targ->die_parent,
7376 decl_table);
7378 add_child_die (parent, copy);
7379 a->dw_attr_val.v.val_die_ref.die = copy;
7381 /* Make sure the newly-copied DIE is walked. If it was
7382 installed in a previously-added context, it won't
7383 get visited otherwise. */
7384 if (parent != unit)
7386 /* Find the highest point of the newly-added tree,
7387 mark each node along the way, and walk from there. */
7388 parent->die_mark = 1;
7389 while (parent->die_parent
7390 && parent->die_parent->die_mark == 0)
7392 parent = parent->die_parent;
7393 parent->die_mark = 1;
7395 copy_decls_walk (unit, parent, decl_table);
7401 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7404 /* Copy declarations for "unworthy" types into the new comdat section.
7405 Incomplete types, modified types, and certain other types aren't broken
7406 out into comdat sections of their own, so they don't have a signature,
7407 and we need to copy the declaration into the same section so that we
7408 don't have an external reference. */
7410 static void
7411 copy_decls_for_unworthy_types (dw_die_ref unit)
7413 decl_hash_type decl_table;
7415 mark_dies (unit);
7416 decl_table.create (10);
7417 copy_decls_walk (unit, unit, decl_table);
7418 decl_table.dispose ();
7419 unmark_dies (unit);
7422 /* Traverse the DIE and add a sibling attribute if it may have the
7423 effect of speeding up access to siblings. To save some space,
7424 avoid generating sibling attributes for DIE's without children. */
7426 static void
7427 add_sibling_attributes (dw_die_ref die)
7429 dw_die_ref c;
7431 if (! die->die_child)
7432 return;
7434 if (die->die_parent && die != die->die_parent->die_child)
7435 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7437 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7440 /* Output all location lists for the DIE and its children. */
7442 static void
7443 output_location_lists (dw_die_ref die)
7445 dw_die_ref c;
7446 dw_attr_ref a;
7447 unsigned ix;
7449 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7450 if (AT_class (a) == dw_val_class_loc_list)
7451 output_loc_list (AT_loc_list (a));
7453 FOR_EACH_CHILD (die, c, output_location_lists (c));
7456 /* We want to limit the number of external references, because they are
7457 larger than local references: a relocation takes multiple words, and
7458 even a sig8 reference is always eight bytes, whereas a local reference
7459 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7460 So if we encounter multiple external references to the same type DIE, we
7461 make a local typedef stub for it and redirect all references there.
7463 This is the element of the hash table for keeping track of these
7464 references. */
7466 struct external_ref
7468 dw_die_ref type;
7469 dw_die_ref stub;
7470 unsigned n_refs;
7473 /* Hashtable helpers. */
7475 struct external_ref_hasher : typed_free_remove <external_ref>
7477 typedef external_ref value_type;
7478 typedef external_ref compare_type;
7479 static inline hashval_t hash (const value_type *);
7480 static inline bool equal (const value_type *, const compare_type *);
7483 inline hashval_t
7484 external_ref_hasher::hash (const value_type *r)
7486 dw_die_ref die = r->type;
7487 hashval_t h = 0;
7489 /* We can't use the address of the DIE for hashing, because
7490 that will make the order of the stub DIEs non-deterministic. */
7491 if (! die->comdat_type_p)
7492 /* We have a symbol; use it to compute a hash. */
7493 h = htab_hash_string (die->die_id.die_symbol);
7494 else
7496 /* We have a type signature; use a subset of the bits as the hash.
7497 The 8-byte signature is at least as large as hashval_t. */
7498 comdat_type_node_ref type_node = die->die_id.die_type_node;
7499 memcpy (&h, type_node->signature, sizeof (h));
7501 return h;
7504 inline bool
7505 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7507 return r1->type == r2->type;
7510 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7512 /* Return a pointer to the external_ref for references to DIE. */
7514 static struct external_ref *
7515 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7517 struct external_ref ref, *ref_p;
7518 external_ref **slot;
7520 ref.type = die;
7521 slot = map.find_slot (&ref, INSERT);
7522 if (*slot != HTAB_EMPTY_ENTRY)
7523 return *slot;
7525 ref_p = XCNEW (struct external_ref);
7526 ref_p->type = die;
7527 *slot = ref_p;
7528 return ref_p;
7531 /* Subroutine of optimize_external_refs, below.
7533 If we see a type skeleton, record it as our stub. If we see external
7534 references, remember how many we've seen. */
7536 static void
7537 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7539 dw_die_ref c;
7540 dw_attr_ref a;
7541 unsigned ix;
7542 struct external_ref *ref_p;
7544 if (is_type_die (die)
7545 && (c = get_AT_ref (die, DW_AT_signature)))
7547 /* This is a local skeleton; use it for local references. */
7548 ref_p = lookup_external_ref (map, c);
7549 ref_p->stub = die;
7552 /* Scan the DIE references, and remember any that refer to DIEs from
7553 other CUs (i.e. those which are not marked). */
7554 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7555 if (AT_class (a) == dw_val_class_die_ref
7556 && (c = AT_ref (a))->die_mark == 0
7557 && is_type_die (c))
7559 ref_p = lookup_external_ref (map, c);
7560 ref_p->n_refs++;
7563 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7566 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7567 points to an external_ref, DATA is the CU we're processing. If we don't
7568 already have a local stub, and we have multiple refs, build a stub. */
7571 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7573 struct external_ref *ref_p = *slot;
7575 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7577 /* We have multiple references to this type, so build a small stub.
7578 Both of these forms are a bit dodgy from the perspective of the
7579 DWARF standard, since technically they should have names. */
7580 dw_die_ref cu = data;
7581 dw_die_ref type = ref_p->type;
7582 dw_die_ref stub = NULL;
7584 if (type->comdat_type_p)
7586 /* If we refer to this type via sig8, use AT_signature. */
7587 stub = new_die (type->die_tag, cu, NULL_TREE);
7588 add_AT_die_ref (stub, DW_AT_signature, type);
7590 else
7592 /* Otherwise, use a typedef with no name. */
7593 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7594 add_AT_die_ref (stub, DW_AT_type, type);
7597 stub->die_mark++;
7598 ref_p->stub = stub;
7600 return 1;
7603 /* DIE is a unit; look through all the DIE references to see if there are
7604 any external references to types, and if so, create local stubs for
7605 them which will be applied in build_abbrev_table. This is useful because
7606 references to local DIEs are smaller. */
7608 static external_ref_hash_type
7609 optimize_external_refs (dw_die_ref die)
7611 external_ref_hash_type map;
7612 map.create (10);
7613 optimize_external_refs_1 (die, map);
7614 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7615 return map;
7618 /* The format of each DIE (and its attribute value pairs) is encoded in an
7619 abbreviation table. This routine builds the abbreviation table and assigns
7620 a unique abbreviation id for each abbreviation entry. The children of each
7621 die are visited recursively. */
7623 static void
7624 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7626 unsigned long abbrev_id;
7627 unsigned int n_alloc;
7628 dw_die_ref c;
7629 dw_attr_ref a;
7630 unsigned ix;
7632 /* Scan the DIE references, and replace any that refer to
7633 DIEs from other CUs (i.e. those which are not marked) with
7634 the local stubs we built in optimize_external_refs. */
7635 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7636 if (AT_class (a) == dw_val_class_die_ref
7637 && (c = AT_ref (a))->die_mark == 0)
7639 struct external_ref *ref_p;
7640 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7642 ref_p = lookup_external_ref (extern_map, c);
7643 if (ref_p->stub && ref_p->stub != die)
7644 change_AT_die_ref (a, ref_p->stub);
7645 else
7646 /* We aren't changing this reference, so mark it external. */
7647 set_AT_ref_external (a, 1);
7650 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7652 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7653 dw_attr_ref die_a, abbrev_a;
7654 unsigned ix;
7655 bool ok = true;
7657 if (abbrev->die_tag != die->die_tag)
7658 continue;
7659 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7660 continue;
7662 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7663 continue;
7665 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7667 abbrev_a = &(*abbrev->die_attr)[ix];
7668 if ((abbrev_a->dw_attr != die_a->dw_attr)
7669 || (value_format (abbrev_a) != value_format (die_a)))
7671 ok = false;
7672 break;
7675 if (ok)
7676 break;
7679 if (abbrev_id >= abbrev_die_table_in_use)
7681 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7683 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7684 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7685 n_alloc);
7687 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7688 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7689 abbrev_die_table_allocated = n_alloc;
7692 ++abbrev_die_table_in_use;
7693 abbrev_die_table[abbrev_id] = die;
7696 die->die_abbrev = abbrev_id;
7697 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7700 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7702 static int
7703 constant_size (unsigned HOST_WIDE_INT value)
7705 int log;
7707 if (value == 0)
7708 log = 0;
7709 else
7710 log = floor_log2 (value);
7712 log = log / 8;
7713 log = 1 << (floor_log2 (log) + 1);
7715 return log;
7718 /* Return the size of a DIE as it is represented in the
7719 .debug_info section. */
7721 static unsigned long
7722 size_of_die (dw_die_ref die)
7724 unsigned long size = 0;
7725 dw_attr_ref a;
7726 unsigned ix;
7727 enum dwarf_form form;
7729 size += size_of_uleb128 (die->die_abbrev);
7730 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7732 switch (AT_class (a))
7734 case dw_val_class_addr:
7735 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7737 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7738 size += size_of_uleb128 (AT_index (a));
7740 else
7741 size += DWARF2_ADDR_SIZE;
7742 break;
7743 case dw_val_class_offset:
7744 size += DWARF_OFFSET_SIZE;
7745 break;
7746 case dw_val_class_loc:
7748 unsigned long lsize = size_of_locs (AT_loc (a));
7750 /* Block length. */
7751 if (dwarf_version >= 4)
7752 size += size_of_uleb128 (lsize);
7753 else
7754 size += constant_size (lsize);
7755 size += lsize;
7757 break;
7758 case dw_val_class_loc_list:
7759 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7761 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7762 size += size_of_uleb128 (AT_index (a));
7764 else
7765 size += DWARF_OFFSET_SIZE;
7766 break;
7767 case dw_val_class_range_list:
7768 size += DWARF_OFFSET_SIZE;
7769 break;
7770 case dw_val_class_const:
7771 size += size_of_sleb128 (AT_int (a));
7772 break;
7773 case dw_val_class_unsigned_const:
7775 int csize = constant_size (AT_unsigned (a));
7776 if (dwarf_version == 3
7777 && a->dw_attr == DW_AT_data_member_location
7778 && csize >= 4)
7779 size += size_of_uleb128 (AT_unsigned (a));
7780 else
7781 size += csize;
7783 break;
7784 case dw_val_class_const_double:
7785 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7786 if (HOST_BITS_PER_WIDE_INT >= 64)
7787 size++; /* block */
7788 break;
7789 case dw_val_class_vec:
7790 size += constant_size (a->dw_attr_val.v.val_vec.length
7791 * a->dw_attr_val.v.val_vec.elt_size)
7792 + a->dw_attr_val.v.val_vec.length
7793 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7794 break;
7795 case dw_val_class_flag:
7796 if (dwarf_version >= 4)
7797 /* Currently all add_AT_flag calls pass in 1 as last argument,
7798 so DW_FORM_flag_present can be used. If that ever changes,
7799 we'll need to use DW_FORM_flag and have some optimization
7800 in build_abbrev_table that will change those to
7801 DW_FORM_flag_present if it is set to 1 in all DIEs using
7802 the same abbrev entry. */
7803 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7804 else
7805 size += 1;
7806 break;
7807 case dw_val_class_die_ref:
7808 if (AT_ref_external (a))
7810 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7811 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7812 is sized by target address length, whereas in DWARF3
7813 it's always sized as an offset. */
7814 if (use_debug_types)
7815 size += DWARF_TYPE_SIGNATURE_SIZE;
7816 else if (dwarf_version == 2)
7817 size += DWARF2_ADDR_SIZE;
7818 else
7819 size += DWARF_OFFSET_SIZE;
7821 else
7822 size += DWARF_OFFSET_SIZE;
7823 break;
7824 case dw_val_class_fde_ref:
7825 size += DWARF_OFFSET_SIZE;
7826 break;
7827 case dw_val_class_lbl_id:
7828 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7830 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7831 size += size_of_uleb128 (AT_index (a));
7833 else
7834 size += DWARF2_ADDR_SIZE;
7835 break;
7836 case dw_val_class_lineptr:
7837 case dw_val_class_macptr:
7838 size += DWARF_OFFSET_SIZE;
7839 break;
7840 case dw_val_class_str:
7841 form = AT_string_form (a);
7842 if (form == DW_FORM_strp)
7843 size += DWARF_OFFSET_SIZE;
7844 else if (form == DW_FORM_GNU_str_index)
7845 size += size_of_uleb128 (AT_index (a));
7846 else
7847 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7848 break;
7849 case dw_val_class_file:
7850 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7851 break;
7852 case dw_val_class_data8:
7853 size += 8;
7854 break;
7855 case dw_val_class_vms_delta:
7856 size += DWARF_OFFSET_SIZE;
7857 break;
7858 case dw_val_class_high_pc:
7859 size += DWARF2_ADDR_SIZE;
7860 break;
7861 default:
7862 gcc_unreachable ();
7866 return size;
7869 /* Size the debugging information associated with a given DIE. Visits the
7870 DIE's children recursively. Updates the global variable next_die_offset, on
7871 each time through. Uses the current value of next_die_offset to update the
7872 die_offset field in each DIE. */
7874 static void
7875 calc_die_sizes (dw_die_ref die)
7877 dw_die_ref c;
7879 gcc_assert (die->die_offset == 0
7880 || (unsigned long int) die->die_offset == next_die_offset);
7881 die->die_offset = next_die_offset;
7882 next_die_offset += size_of_die (die);
7884 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7886 if (die->die_child != NULL)
7887 /* Count the null byte used to terminate sibling lists. */
7888 next_die_offset += 1;
7891 /* Size just the base type children at the start of the CU.
7892 This is needed because build_abbrev needs to size locs
7893 and sizing of type based stack ops needs to know die_offset
7894 values for the base types. */
7896 static void
7897 calc_base_type_die_sizes (void)
7899 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7900 unsigned int i;
7901 dw_die_ref base_type;
7902 #if ENABLE_ASSERT_CHECKING
7903 dw_die_ref prev = comp_unit_die ()->die_child;
7904 #endif
7906 die_offset += size_of_die (comp_unit_die ());
7907 for (i = 0; base_types.iterate (i, &base_type); i++)
7909 #if ENABLE_ASSERT_CHECKING
7910 gcc_assert (base_type->die_offset == 0
7911 && prev->die_sib == base_type
7912 && base_type->die_child == NULL
7913 && base_type->die_abbrev);
7914 prev = base_type;
7915 #endif
7916 base_type->die_offset = die_offset;
7917 die_offset += size_of_die (base_type);
7921 /* Set the marks for a die and its children. We do this so
7922 that we know whether or not a reference needs to use FORM_ref_addr; only
7923 DIEs in the same CU will be marked. We used to clear out the offset
7924 and use that as the flag, but ran into ordering problems. */
7926 static void
7927 mark_dies (dw_die_ref die)
7929 dw_die_ref c;
7931 gcc_assert (!die->die_mark);
7933 die->die_mark = 1;
7934 FOR_EACH_CHILD (die, c, mark_dies (c));
7937 /* Clear the marks for a die and its children. */
7939 static void
7940 unmark_dies (dw_die_ref die)
7942 dw_die_ref c;
7944 if (! use_debug_types)
7945 gcc_assert (die->die_mark);
7947 die->die_mark = 0;
7948 FOR_EACH_CHILD (die, c, unmark_dies (c));
7951 /* Clear the marks for a die, its children and referred dies. */
7953 static void
7954 unmark_all_dies (dw_die_ref die)
7956 dw_die_ref c;
7957 dw_attr_ref a;
7958 unsigned ix;
7960 if (!die->die_mark)
7961 return;
7962 die->die_mark = 0;
7964 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7966 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7967 if (AT_class (a) == dw_val_class_die_ref)
7968 unmark_all_dies (AT_ref (a));
7971 /* Calculate if the entry should appear in the final output file. It may be
7972 from a pruned a type. */
7974 static bool
7975 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7977 /* By limiting gnu pubnames to definitions only, gold can generate a
7978 gdb index without entries for declarations, which don't include
7979 enough information to be useful. */
7980 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7981 return false;
7983 if (table == pubname_table)
7985 /* Enumerator names are part of the pubname table, but the
7986 parent DW_TAG_enumeration_type die may have been pruned.
7987 Don't output them if that is the case. */
7988 if (p->die->die_tag == DW_TAG_enumerator &&
7989 (p->die->die_parent == NULL
7990 || !p->die->die_parent->die_perennial_p))
7991 return false;
7993 /* Everything else in the pubname table is included. */
7994 return true;
7997 /* The pubtypes table shouldn't include types that have been
7998 pruned. */
7999 return (p->die->die_offset != 0
8000 || !flag_eliminate_unused_debug_types);
8003 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8004 generated for the compilation unit. */
8006 static unsigned long
8007 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8009 unsigned long size;
8010 unsigned i;
8011 pubname_ref p;
8012 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8014 size = DWARF_PUBNAMES_HEADER_SIZE;
8015 FOR_EACH_VEC_ELT (*names, i, p)
8016 if (include_pubname_in_output (names, p))
8017 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8019 size += DWARF_OFFSET_SIZE;
8020 return size;
8023 /* Return the size of the information in the .debug_aranges section. */
8025 static unsigned long
8026 size_of_aranges (void)
8028 unsigned long size;
8030 size = DWARF_ARANGES_HEADER_SIZE;
8032 /* Count the address/length pair for this compilation unit. */
8033 if (text_section_used)
8034 size += 2 * DWARF2_ADDR_SIZE;
8035 if (cold_text_section_used)
8036 size += 2 * DWARF2_ADDR_SIZE;
8037 if (have_multiple_function_sections)
8039 unsigned fde_idx;
8040 dw_fde_ref fde;
8042 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8044 if (DECL_IGNORED_P (fde->decl))
8045 continue;
8046 if (!fde->in_std_section)
8047 size += 2 * DWARF2_ADDR_SIZE;
8048 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8049 size += 2 * DWARF2_ADDR_SIZE;
8053 /* Count the two zero words used to terminated the address range table. */
8054 size += 2 * DWARF2_ADDR_SIZE;
8055 return size;
8058 /* Select the encoding of an attribute value. */
8060 static enum dwarf_form
8061 value_format (dw_attr_ref a)
8063 switch (AT_class (a))
8065 case dw_val_class_addr:
8066 /* Only very few attributes allow DW_FORM_addr. */
8067 switch (a->dw_attr)
8069 case DW_AT_low_pc:
8070 case DW_AT_high_pc:
8071 case DW_AT_entry_pc:
8072 case DW_AT_trampoline:
8073 return (AT_index (a) == NOT_INDEXED
8074 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8075 default:
8076 break;
8078 switch (DWARF2_ADDR_SIZE)
8080 case 1:
8081 return DW_FORM_data1;
8082 case 2:
8083 return DW_FORM_data2;
8084 case 4:
8085 return DW_FORM_data4;
8086 case 8:
8087 return DW_FORM_data8;
8088 default:
8089 gcc_unreachable ();
8091 case dw_val_class_range_list:
8092 case dw_val_class_loc_list:
8093 if (dwarf_version >= 4)
8094 return DW_FORM_sec_offset;
8095 /* FALLTHRU */
8096 case dw_val_class_vms_delta:
8097 case dw_val_class_offset:
8098 switch (DWARF_OFFSET_SIZE)
8100 case 4:
8101 return DW_FORM_data4;
8102 case 8:
8103 return DW_FORM_data8;
8104 default:
8105 gcc_unreachable ();
8107 case dw_val_class_loc:
8108 if (dwarf_version >= 4)
8109 return DW_FORM_exprloc;
8110 switch (constant_size (size_of_locs (AT_loc (a))))
8112 case 1:
8113 return DW_FORM_block1;
8114 case 2:
8115 return DW_FORM_block2;
8116 case 4:
8117 return DW_FORM_block4;
8118 default:
8119 gcc_unreachable ();
8121 case dw_val_class_const:
8122 return DW_FORM_sdata;
8123 case dw_val_class_unsigned_const:
8124 switch (constant_size (AT_unsigned (a)))
8126 case 1:
8127 return DW_FORM_data1;
8128 case 2:
8129 return DW_FORM_data2;
8130 case 4:
8131 /* In DWARF3 DW_AT_data_member_location with
8132 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8133 constant, so we need to use DW_FORM_udata if we need
8134 a large constant. */
8135 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8136 return DW_FORM_udata;
8137 return DW_FORM_data4;
8138 case 8:
8139 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8140 return DW_FORM_udata;
8141 return DW_FORM_data8;
8142 default:
8143 gcc_unreachable ();
8145 case dw_val_class_const_double:
8146 switch (HOST_BITS_PER_WIDE_INT)
8148 case 8:
8149 return DW_FORM_data2;
8150 case 16:
8151 return DW_FORM_data4;
8152 case 32:
8153 return DW_FORM_data8;
8154 case 64:
8155 default:
8156 return DW_FORM_block1;
8158 case dw_val_class_vec:
8159 switch (constant_size (a->dw_attr_val.v.val_vec.length
8160 * a->dw_attr_val.v.val_vec.elt_size))
8162 case 1:
8163 return DW_FORM_block1;
8164 case 2:
8165 return DW_FORM_block2;
8166 case 4:
8167 return DW_FORM_block4;
8168 default:
8169 gcc_unreachable ();
8171 case dw_val_class_flag:
8172 if (dwarf_version >= 4)
8174 /* Currently all add_AT_flag calls pass in 1 as last argument,
8175 so DW_FORM_flag_present can be used. If that ever changes,
8176 we'll need to use DW_FORM_flag and have some optimization
8177 in build_abbrev_table that will change those to
8178 DW_FORM_flag_present if it is set to 1 in all DIEs using
8179 the same abbrev entry. */
8180 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8181 return DW_FORM_flag_present;
8183 return DW_FORM_flag;
8184 case dw_val_class_die_ref:
8185 if (AT_ref_external (a))
8186 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8187 else
8188 return DW_FORM_ref;
8189 case dw_val_class_fde_ref:
8190 return DW_FORM_data;
8191 case dw_val_class_lbl_id:
8192 return (AT_index (a) == NOT_INDEXED
8193 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8194 case dw_val_class_lineptr:
8195 case dw_val_class_macptr:
8196 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8197 case dw_val_class_str:
8198 return AT_string_form (a);
8199 case dw_val_class_file:
8200 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8202 case 1:
8203 return DW_FORM_data1;
8204 case 2:
8205 return DW_FORM_data2;
8206 case 4:
8207 return DW_FORM_data4;
8208 default:
8209 gcc_unreachable ();
8212 case dw_val_class_data8:
8213 return DW_FORM_data8;
8215 case dw_val_class_high_pc:
8216 switch (DWARF2_ADDR_SIZE)
8218 case 1:
8219 return DW_FORM_data1;
8220 case 2:
8221 return DW_FORM_data2;
8222 case 4:
8223 return DW_FORM_data4;
8224 case 8:
8225 return DW_FORM_data8;
8226 default:
8227 gcc_unreachable ();
8230 default:
8231 gcc_unreachable ();
8235 /* Output the encoding of an attribute value. */
8237 static void
8238 output_value_format (dw_attr_ref a)
8240 enum dwarf_form form = value_format (a);
8242 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8245 /* Given a die and id, produce the appropriate abbreviations. */
8247 static void
8248 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8250 unsigned ix;
8251 dw_attr_ref a_attr;
8253 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8254 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8255 dwarf_tag_name (abbrev->die_tag));
8257 if (abbrev->die_child != NULL)
8258 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8259 else
8260 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8262 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8264 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8265 dwarf_attr_name (a_attr->dw_attr));
8266 output_value_format (a_attr);
8269 dw2_asm_output_data (1, 0, NULL);
8270 dw2_asm_output_data (1, 0, NULL);
8274 /* Output the .debug_abbrev section which defines the DIE abbreviation
8275 table. */
8277 static void
8278 output_abbrev_section (void)
8280 unsigned long abbrev_id;
8282 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8283 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8285 /* Terminate the table. */
8286 dw2_asm_output_data (1, 0, NULL);
8289 /* Output a symbol we can use to refer to this DIE from another CU. */
8291 static inline void
8292 output_die_symbol (dw_die_ref die)
8294 const char *sym = die->die_id.die_symbol;
8296 gcc_assert (!die->comdat_type_p);
8298 if (sym == 0)
8299 return;
8301 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8302 /* We make these global, not weak; if the target doesn't support
8303 .linkonce, it doesn't support combining the sections, so debugging
8304 will break. */
8305 targetm.asm_out.globalize_label (asm_out_file, sym);
8307 ASM_OUTPUT_LABEL (asm_out_file, sym);
8310 /* Return a new location list, given the begin and end range, and the
8311 expression. */
8313 static inline dw_loc_list_ref
8314 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8315 const char *section)
8317 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8319 retlist->begin = begin;
8320 retlist->begin_entry = NULL;
8321 retlist->end = end;
8322 retlist->expr = expr;
8323 retlist->section = section;
8325 return retlist;
8328 /* Generate a new internal symbol for this location list node, if it
8329 hasn't got one yet. */
8331 static inline void
8332 gen_llsym (dw_loc_list_ref list)
8334 gcc_assert (!list->ll_symbol);
8335 list->ll_symbol = gen_internal_sym ("LLST");
8338 /* Output the location list given to us. */
8340 static void
8341 output_loc_list (dw_loc_list_ref list_head)
8343 dw_loc_list_ref curr = list_head;
8345 if (list_head->emitted)
8346 return;
8347 list_head->emitted = true;
8349 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8351 /* Walk the location list, and output each range + expression. */
8352 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8354 unsigned long size;
8355 /* Don't output an entry that starts and ends at the same address. */
8356 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8357 continue;
8358 size = size_of_locs (curr->expr);
8359 /* If the expression is too large, drop it on the floor. We could
8360 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8361 in the expression, but >= 64KB expressions for a single value
8362 in a single range are unlikely very useful. */
8363 if (size > 0xffff)
8364 continue;
8365 if (dwarf_split_debug_info)
8367 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8368 "Location list start/length entry (%s)",
8369 list_head->ll_symbol);
8370 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8371 "Location list range start index (%s)",
8372 curr->begin);
8373 /* The length field is 4 bytes. If we ever need to support
8374 an 8-byte length, we can add a new DW_LLE code or fall back
8375 to DW_LLE_GNU_start_end_entry. */
8376 dw2_asm_output_delta (4, curr->end, curr->begin,
8377 "Location list range length (%s)",
8378 list_head->ll_symbol);
8380 else if (!have_multiple_function_sections)
8382 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8383 "Location list begin address (%s)",
8384 list_head->ll_symbol);
8385 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8386 "Location list end address (%s)",
8387 list_head->ll_symbol);
8389 else
8391 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8392 "Location list begin address (%s)",
8393 list_head->ll_symbol);
8394 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8395 "Location list end address (%s)",
8396 list_head->ll_symbol);
8399 /* Output the block length for this list of location operations. */
8400 gcc_assert (size <= 0xffff);
8401 dw2_asm_output_data (2, size, "%s", "Location expression size");
8403 output_loc_sequence (curr->expr, -1);
8406 if (dwarf_split_debug_info)
8407 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8408 "Location list terminator (%s)",
8409 list_head->ll_symbol);
8410 else
8412 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8413 "Location list terminator begin (%s)",
8414 list_head->ll_symbol);
8415 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8416 "Location list terminator end (%s)",
8417 list_head->ll_symbol);
8421 /* Output a range_list offset into the debug_range section. Emit a
8422 relocated reference if val_entry is NULL, otherwise, emit an
8423 indirect reference. */
8425 static void
8426 output_range_list_offset (dw_attr_ref a)
8428 const char *name = dwarf_attr_name (a->dw_attr);
8430 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8432 char *p = strchr (ranges_section_label, '\0');
8433 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8434 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8435 debug_ranges_section, "%s", name);
8436 *p = '\0';
8438 else
8439 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8440 "%s (offset from %s)", name, ranges_section_label);
8443 /* Output the offset into the debug_loc section. */
8445 static void
8446 output_loc_list_offset (dw_attr_ref a)
8448 char *sym = AT_loc_list (a)->ll_symbol;
8450 gcc_assert (sym);
8451 if (dwarf_split_debug_info)
8452 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8453 "%s", dwarf_attr_name (a->dw_attr));
8454 else
8455 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8456 "%s", dwarf_attr_name (a->dw_attr));
8459 /* Output an attribute's index or value appropriately. */
8461 static void
8462 output_attr_index_or_value (dw_attr_ref a)
8464 const char *name = dwarf_attr_name (a->dw_attr);
8466 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8468 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8469 return;
8471 switch (AT_class (a))
8473 case dw_val_class_addr:
8474 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8475 break;
8476 case dw_val_class_high_pc:
8477 case dw_val_class_lbl_id:
8478 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8479 break;
8480 case dw_val_class_loc_list:
8481 output_loc_list_offset (a);
8482 break;
8483 default:
8484 gcc_unreachable ();
8488 /* Output a type signature. */
8490 static inline void
8491 output_signature (const char *sig, const char *name)
8493 int i;
8495 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8496 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8499 /* Output the DIE and its attributes. Called recursively to generate
8500 the definitions of each child DIE. */
8502 static void
8503 output_die (dw_die_ref die)
8505 dw_attr_ref a;
8506 dw_die_ref c;
8507 unsigned long size;
8508 unsigned ix;
8510 /* If someone in another CU might refer to us, set up a symbol for
8511 them to point to. */
8512 if (! die->comdat_type_p && die->die_id.die_symbol)
8513 output_die_symbol (die);
8515 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8516 (unsigned long)die->die_offset,
8517 dwarf_tag_name (die->die_tag));
8519 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8521 const char *name = dwarf_attr_name (a->dw_attr);
8523 switch (AT_class (a))
8525 case dw_val_class_addr:
8526 output_attr_index_or_value (a);
8527 break;
8529 case dw_val_class_offset:
8530 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8531 "%s", name);
8532 break;
8534 case dw_val_class_range_list:
8535 output_range_list_offset (a);
8536 break;
8538 case dw_val_class_loc:
8539 size = size_of_locs (AT_loc (a));
8541 /* Output the block length for this list of location operations. */
8542 if (dwarf_version >= 4)
8543 dw2_asm_output_data_uleb128 (size, "%s", name);
8544 else
8545 dw2_asm_output_data (constant_size (size), size, "%s", name);
8547 output_loc_sequence (AT_loc (a), -1);
8548 break;
8550 case dw_val_class_const:
8551 /* ??? It would be slightly more efficient to use a scheme like is
8552 used for unsigned constants below, but gdb 4.x does not sign
8553 extend. Gdb 5.x does sign extend. */
8554 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8555 break;
8557 case dw_val_class_unsigned_const:
8559 int csize = constant_size (AT_unsigned (a));
8560 if (dwarf_version == 3
8561 && a->dw_attr == DW_AT_data_member_location
8562 && csize >= 4)
8563 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8564 else
8565 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8567 break;
8569 case dw_val_class_const_double:
8571 unsigned HOST_WIDE_INT first, second;
8573 if (HOST_BITS_PER_WIDE_INT >= 64)
8574 dw2_asm_output_data (1,
8575 HOST_BITS_PER_DOUBLE_INT
8576 / HOST_BITS_PER_CHAR,
8577 NULL);
8579 if (WORDS_BIG_ENDIAN)
8581 first = a->dw_attr_val.v.val_double.high;
8582 second = a->dw_attr_val.v.val_double.low;
8584 else
8586 first = a->dw_attr_val.v.val_double.low;
8587 second = a->dw_attr_val.v.val_double.high;
8590 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8591 first, "%s", name);
8592 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8593 second, NULL);
8595 break;
8597 case dw_val_class_vec:
8599 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8600 unsigned int len = a->dw_attr_val.v.val_vec.length;
8601 unsigned int i;
8602 unsigned char *p;
8604 dw2_asm_output_data (constant_size (len * elt_size),
8605 len * elt_size, "%s", name);
8606 if (elt_size > sizeof (HOST_WIDE_INT))
8608 elt_size /= 2;
8609 len *= 2;
8611 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8612 i < len;
8613 i++, p += elt_size)
8614 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8615 "fp or vector constant word %u", i);
8616 break;
8619 case dw_val_class_flag:
8620 if (dwarf_version >= 4)
8622 /* Currently all add_AT_flag calls pass in 1 as last argument,
8623 so DW_FORM_flag_present can be used. If that ever changes,
8624 we'll need to use DW_FORM_flag and have some optimization
8625 in build_abbrev_table that will change those to
8626 DW_FORM_flag_present if it is set to 1 in all DIEs using
8627 the same abbrev entry. */
8628 gcc_assert (AT_flag (a) == 1);
8629 if (flag_debug_asm)
8630 fprintf (asm_out_file, "\t\t\t%s %s\n",
8631 ASM_COMMENT_START, name);
8632 break;
8634 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8635 break;
8637 case dw_val_class_loc_list:
8638 output_attr_index_or_value (a);
8639 break;
8641 case dw_val_class_die_ref:
8642 if (AT_ref_external (a))
8644 if (AT_ref (a)->comdat_type_p)
8646 comdat_type_node_ref type_node =
8647 AT_ref (a)->die_id.die_type_node;
8649 gcc_assert (type_node);
8650 output_signature (type_node->signature, name);
8652 else
8654 const char *sym = AT_ref (a)->die_id.die_symbol;
8655 int size;
8657 gcc_assert (sym);
8658 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8659 length, whereas in DWARF3 it's always sized as an
8660 offset. */
8661 if (dwarf_version == 2)
8662 size = DWARF2_ADDR_SIZE;
8663 else
8664 size = DWARF_OFFSET_SIZE;
8665 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8666 name);
8669 else
8671 gcc_assert (AT_ref (a)->die_offset);
8672 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8673 "%s", name);
8675 break;
8677 case dw_val_class_fde_ref:
8679 char l1[20];
8681 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8682 a->dw_attr_val.v.val_fde_index * 2);
8683 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8684 "%s", name);
8686 break;
8688 case dw_val_class_vms_delta:
8689 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8690 AT_vms_delta2 (a), AT_vms_delta1 (a),
8691 "%s", name);
8692 break;
8694 case dw_val_class_lbl_id:
8695 output_attr_index_or_value (a);
8696 break;
8698 case dw_val_class_lineptr:
8699 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8700 debug_line_section, "%s", name);
8701 break;
8703 case dw_val_class_macptr:
8704 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8705 debug_macinfo_section, "%s", name);
8706 break;
8708 case dw_val_class_str:
8709 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8710 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8711 a->dw_attr_val.v.val_str->label,
8712 debug_str_section,
8713 "%s: \"%s\"", name, AT_string (a));
8714 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8715 dw2_asm_output_data_uleb128 (AT_index (a),
8716 "%s: \"%s\"", name, AT_string (a));
8717 else
8718 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8719 break;
8721 case dw_val_class_file:
8723 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8725 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8726 a->dw_attr_val.v.val_file->filename);
8727 break;
8730 case dw_val_class_data8:
8732 int i;
8734 for (i = 0; i < 8; i++)
8735 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8736 i == 0 ? "%s" : NULL, name);
8737 break;
8740 case dw_val_class_high_pc:
8741 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8742 get_AT_low_pc (die), "DW_AT_high_pc");
8743 break;
8745 default:
8746 gcc_unreachable ();
8750 FOR_EACH_CHILD (die, c, output_die (c));
8752 /* Add null byte to terminate sibling list. */
8753 if (die->die_child != NULL)
8754 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8755 (unsigned long) die->die_offset);
8758 /* Output the compilation unit that appears at the beginning of the
8759 .debug_info section, and precedes the DIE descriptions. */
8761 static void
8762 output_compilation_unit_header (void)
8764 int ver = dwarf_version;
8766 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8767 dw2_asm_output_data (4, 0xffffffff,
8768 "Initial length escape value indicating 64-bit DWARF extension");
8769 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8770 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8771 "Length of Compilation Unit Info");
8772 dw2_asm_output_data (2, ver, "DWARF version number");
8773 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8774 debug_abbrev_section,
8775 "Offset Into Abbrev. Section");
8776 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8779 /* Output the compilation unit DIE and its children. */
8781 static void
8782 output_comp_unit (dw_die_ref die, int output_if_empty)
8784 const char *secname, *oldsym;
8785 char *tmp;
8786 external_ref_hash_type extern_map;
8788 /* Unless we are outputting main CU, we may throw away empty ones. */
8789 if (!output_if_empty && die->die_child == NULL)
8790 return;
8792 /* Even if there are no children of this DIE, we must output the information
8793 about the compilation unit. Otherwise, on an empty translation unit, we
8794 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8795 will then complain when examining the file. First mark all the DIEs in
8796 this CU so we know which get local refs. */
8797 mark_dies (die);
8799 extern_map = optimize_external_refs (die);
8801 build_abbrev_table (die, extern_map);
8803 extern_map.dispose ();
8805 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8806 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8807 calc_die_sizes (die);
8809 oldsym = die->die_id.die_symbol;
8810 if (oldsym)
8812 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8814 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8815 secname = tmp;
8816 die->die_id.die_symbol = NULL;
8817 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8819 else
8821 switch_to_section (debug_info_section);
8822 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8823 info_section_emitted = true;
8826 /* Output debugging information. */
8827 output_compilation_unit_header ();
8828 output_die (die);
8830 /* Leave the marks on the main CU, so we can check them in
8831 output_pubnames. */
8832 if (oldsym)
8834 unmark_dies (die);
8835 die->die_id.die_symbol = oldsym;
8839 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8840 and .debug_pubtypes. This is configured per-target, but can be
8841 overridden by the -gpubnames or -gno-pubnames options. */
8843 static inline bool
8844 want_pubnames (void)
8846 if (debug_generate_pub_sections != -1)
8847 return debug_generate_pub_sections;
8848 return targetm.want_debug_pub_sections;
8851 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8853 static void
8854 add_AT_pubnames (dw_die_ref die)
8856 if (want_pubnames ())
8857 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8860 /* Add a string attribute value to a skeleton DIE. */
8862 static inline void
8863 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8864 const char *str)
8866 dw_attr_node attr;
8867 struct indirect_string_node *node;
8869 if (! skeleton_debug_str_hash)
8870 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8871 debug_str_eq, NULL);
8873 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8874 find_string_form (node);
8875 if (node->form == DW_FORM_GNU_str_index)
8876 node->form = DW_FORM_strp;
8878 attr.dw_attr = attr_kind;
8879 attr.dw_attr_val.val_class = dw_val_class_str;
8880 attr.dw_attr_val.val_entry = NULL;
8881 attr.dw_attr_val.v.val_str = node;
8882 add_dwarf_attr (die, &attr);
8885 /* Helper function to generate top-level dies for skeleton debug_info and
8886 debug_types. */
8888 static void
8889 add_top_level_skeleton_die_attrs (dw_die_ref die)
8891 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8892 const char *comp_dir = comp_dir_string ();
8894 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8895 if (comp_dir != NULL)
8896 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8897 add_AT_pubnames (die);
8898 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8901 /* Return the single type-unit die for skeleton type units. */
8903 static dw_die_ref
8904 get_skeleton_type_unit (void)
8906 /* For dwarf_split_debug_sections with use_type info, all type units in the
8907 skeleton sections have identical dies (but different headers). This
8908 single die will be output many times. */
8910 static dw_die_ref skeleton_type_unit = NULL;
8912 if (skeleton_type_unit == NULL)
8914 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8915 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8916 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8918 return skeleton_type_unit;
8921 /* Output skeleton debug sections that point to the dwo file. */
8923 static void
8924 output_skeleton_debug_sections (dw_die_ref comp_unit)
8926 /* These attributes will be found in the full debug_info section. */
8927 remove_AT (comp_unit, DW_AT_producer);
8928 remove_AT (comp_unit, DW_AT_language);
8930 switch_to_section (debug_skeleton_info_section);
8931 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8933 /* Produce the skeleton compilation-unit header. This one differs enough from
8934 a normal CU header that it's better not to call output_compilation_unit
8935 header. */
8936 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8937 dw2_asm_output_data (4, 0xffffffff,
8938 "Initial length escape value indicating 64-bit DWARF extension");
8940 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8941 DWARF_COMPILE_UNIT_HEADER_SIZE
8942 - DWARF_INITIAL_LENGTH_SIZE
8943 + size_of_die (comp_unit),
8944 "Length of Compilation Unit Info");
8945 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8946 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8947 debug_abbrev_section,
8948 "Offset Into Abbrev. Section");
8949 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8951 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8952 output_die (comp_unit);
8954 /* Build the skeleton debug_abbrev section. */
8955 switch_to_section (debug_skeleton_abbrev_section);
8956 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8958 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8959 if (use_debug_types)
8960 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8962 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8965 /* Output a comdat type unit DIE and its children. */
8967 static void
8968 output_comdat_type_unit (comdat_type_node *node)
8970 const char *secname;
8971 char *tmp;
8972 int i;
8973 #if defined (OBJECT_FORMAT_ELF)
8974 tree comdat_key;
8975 #endif
8976 external_ref_hash_type extern_map;
8978 /* First mark all the DIEs in this CU so we know which get local refs. */
8979 mark_dies (node->root_die);
8981 extern_map = optimize_external_refs (node->root_die);
8983 build_abbrev_table (node->root_die, extern_map);
8985 extern_map.dispose ();
8987 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8988 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8989 calc_die_sizes (node->root_die);
8991 #if defined (OBJECT_FORMAT_ELF)
8992 if (!dwarf_split_debug_info)
8993 secname = ".debug_types";
8994 else
8995 secname = ".debug_types.dwo";
8997 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8998 sprintf (tmp, "wt.");
8999 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9000 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9001 comdat_key = get_identifier (tmp);
9002 targetm.asm_out.named_section (secname,
9003 SECTION_DEBUG | SECTION_LINKONCE,
9004 comdat_key);
9005 #else
9006 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9007 sprintf (tmp, ".gnu.linkonce.wt.");
9008 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9009 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9010 secname = tmp;
9011 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9012 #endif
9014 /* Output debugging information. */
9015 output_compilation_unit_header ();
9016 output_signature (node->signature, "Type Signature");
9017 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9018 "Offset to Type DIE");
9019 output_die (node->root_die);
9021 unmark_dies (node->root_die);
9023 #if defined (OBJECT_FORMAT_ELF)
9024 if (dwarf_split_debug_info)
9026 /* Produce the skeleton type-unit header. */
9027 const char *secname = ".debug_types";
9029 targetm.asm_out.named_section (secname,
9030 SECTION_DEBUG | SECTION_LINKONCE,
9031 comdat_key);
9032 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9033 dw2_asm_output_data (4, 0xffffffff,
9034 "Initial length escape value indicating 64-bit DWARF extension");
9036 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9037 DWARF_COMPILE_UNIT_HEADER_SIZE
9038 - DWARF_INITIAL_LENGTH_SIZE
9039 + size_of_die (get_skeleton_type_unit ())
9040 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9041 "Length of Type Unit Info");
9042 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9043 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9044 debug_skeleton_abbrev_section_label,
9045 debug_abbrev_section,
9046 "Offset Into Abbrev. Section");
9047 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9048 output_signature (node->signature, "Type Signature");
9049 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9051 output_die (get_skeleton_type_unit ());
9053 #endif
9056 /* Return the DWARF2/3 pubname associated with a decl. */
9058 static const char *
9059 dwarf2_name (tree decl, int scope)
9061 if (DECL_NAMELESS (decl))
9062 return NULL;
9063 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9066 /* Add a new entry to .debug_pubnames if appropriate. */
9068 static void
9069 add_pubname_string (const char *str, dw_die_ref die)
9071 pubname_entry e;
9073 e.die = die;
9074 e.name = xstrdup (str);
9075 vec_safe_push (pubname_table, e);
9078 static void
9079 add_pubname (tree decl, dw_die_ref die)
9081 if (!want_pubnames ())
9082 return;
9084 /* Don't add items to the table when we expect that the consumer will have
9085 just read the enclosing die. For example, if the consumer is looking at a
9086 class_member, it will either be inside the class already, or will have just
9087 looked up the class to find the member. Either way, searching the class is
9088 faster than searching the index. */
9089 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9090 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9092 const char *name = dwarf2_name (decl, 1);
9094 if (name)
9095 add_pubname_string (name, die);
9099 /* Add an enumerator to the pubnames section. */
9101 static void
9102 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9104 pubname_entry e;
9106 gcc_assert (scope_name);
9107 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9108 e.die = die;
9109 vec_safe_push (pubname_table, e);
9112 /* Add a new entry to .debug_pubtypes if appropriate. */
9114 static void
9115 add_pubtype (tree decl, dw_die_ref die)
9117 pubname_entry e;
9119 if (!want_pubnames ())
9120 return;
9122 if ((TREE_PUBLIC (decl)
9123 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9124 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9126 tree scope = NULL;
9127 const char *scope_name = "";
9128 const char *sep = is_cxx () ? "::" : ".";
9129 const char *name;
9131 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9132 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9134 scope_name = lang_hooks.dwarf_name (scope, 1);
9135 if (scope_name != NULL && scope_name[0] != '\0')
9136 scope_name = concat (scope_name, sep, NULL);
9137 else
9138 scope_name = "";
9141 if (TYPE_P (decl))
9142 name = type_tag (decl);
9143 else
9144 name = lang_hooks.dwarf_name (decl, 1);
9146 /* If we don't have a name for the type, there's no point in adding
9147 it to the table. */
9148 if (name != NULL && name[0] != '\0')
9150 e.die = die;
9151 e.name = concat (scope_name, name, NULL);
9152 vec_safe_push (pubtype_table, e);
9155 /* Although it might be more consistent to add the pubinfo for the
9156 enumerators as their dies are created, they should only be added if the
9157 enum type meets the criteria above. So rather than re-check the parent
9158 enum type whenever an enumerator die is created, just output them all
9159 here. This isn't protected by the name conditional because anonymous
9160 enums don't have names. */
9161 if (die->die_tag == DW_TAG_enumeration_type)
9163 dw_die_ref c;
9165 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9170 /* Output a single entry in the pubnames table. */
9172 static void
9173 output_pubname (dw_offset die_offset, pubname_entry *entry)
9175 dw_die_ref die = entry->die;
9176 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9178 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9180 if (debug_generate_pub_sections == 2)
9182 /* This logic follows gdb's method for determining the value of the flag
9183 byte. */
9184 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9185 switch (die->die_tag)
9187 case DW_TAG_typedef:
9188 case DW_TAG_base_type:
9189 case DW_TAG_subrange_type:
9190 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9191 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9192 break;
9193 case DW_TAG_enumerator:
9194 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9195 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9196 if (!is_cxx () && !is_java ())
9197 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9198 break;
9199 case DW_TAG_subprogram:
9200 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9201 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9202 if (!is_ada ())
9203 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9204 break;
9205 case DW_TAG_constant:
9206 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9207 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9208 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9209 break;
9210 case DW_TAG_variable:
9211 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9212 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9213 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9214 break;
9215 case DW_TAG_namespace:
9216 case DW_TAG_imported_declaration:
9217 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9218 break;
9219 case DW_TAG_class_type:
9220 case DW_TAG_interface_type:
9221 case DW_TAG_structure_type:
9222 case DW_TAG_union_type:
9223 case DW_TAG_enumeration_type:
9224 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9225 if (!is_cxx () && !is_java ())
9226 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9227 break;
9228 default:
9229 /* An unusual tag. Leave the flag-byte empty. */
9230 break;
9232 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9233 "GDB-index flags");
9236 dw2_asm_output_nstring (entry->name, -1, "external name");
9240 /* Output the public names table used to speed up access to externally
9241 visible names; or the public types table used to find type definitions. */
9243 static void
9244 output_pubnames (vec<pubname_entry, va_gc> *names)
9246 unsigned i;
9247 unsigned long pubnames_length = size_of_pubnames (names);
9248 pubname_ref pub;
9250 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9251 dw2_asm_output_data (4, 0xffffffff,
9252 "Initial length escape value indicating 64-bit DWARF extension");
9253 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9255 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9256 dw2_asm_output_data (2, 2, "DWARF Version");
9258 if (dwarf_split_debug_info)
9259 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9260 debug_skeleton_info_section,
9261 "Offset of Compilation Unit Info");
9262 else
9263 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9264 debug_info_section,
9265 "Offset of Compilation Unit Info");
9266 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9267 "Compilation Unit Length");
9269 FOR_EACH_VEC_ELT (*names, i, pub)
9271 if (include_pubname_in_output (names, pub))
9273 dw_offset die_offset = pub->die->die_offset;
9275 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9276 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9277 gcc_assert (pub->die->die_mark);
9279 /* If we're putting types in their own .debug_types sections,
9280 the .debug_pubtypes table will still point to the compile
9281 unit (not the type unit), so we want to use the offset of
9282 the skeleton DIE (if there is one). */
9283 if (pub->die->comdat_type_p && names == pubtype_table)
9285 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9287 if (type_node != NULL)
9288 die_offset = (type_node->skeleton_die != NULL
9289 ? type_node->skeleton_die->die_offset
9290 : 0);
9293 output_pubname (die_offset, pub);
9297 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9300 /* Output public names and types tables if necessary. */
9302 static void
9303 output_pubtables (void)
9305 if (!want_pubnames () || !info_section_emitted)
9306 return;
9308 switch_to_section (debug_pubnames_section);
9309 output_pubnames (pubname_table);
9310 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9311 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9312 simply won't look for the section. */
9313 switch_to_section (debug_pubtypes_section);
9314 output_pubnames (pubtype_table);
9318 /* Output the information that goes into the .debug_aranges table.
9319 Namely, define the beginning and ending address range of the
9320 text section generated for this compilation unit. */
9322 static void
9323 output_aranges (unsigned long aranges_length)
9325 unsigned i;
9327 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9328 dw2_asm_output_data (4, 0xffffffff,
9329 "Initial length escape value indicating 64-bit DWARF extension");
9330 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9331 "Length of Address Ranges Info");
9332 /* Version number for aranges is still 2, even in DWARF3. */
9333 dw2_asm_output_data (2, 2, "DWARF Version");
9334 if (dwarf_split_debug_info)
9335 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9336 debug_skeleton_info_section,
9337 "Offset of Compilation Unit Info");
9338 else
9339 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9340 debug_info_section,
9341 "Offset of Compilation Unit Info");
9342 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9343 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9345 /* We need to align to twice the pointer size here. */
9346 if (DWARF_ARANGES_PAD_SIZE)
9348 /* Pad using a 2 byte words so that padding is correct for any
9349 pointer size. */
9350 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9351 2 * DWARF2_ADDR_SIZE);
9352 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9353 dw2_asm_output_data (2, 0, NULL);
9356 /* It is necessary not to output these entries if the sections were
9357 not used; if the sections were not used, the length will be 0 and
9358 the address may end up as 0 if the section is discarded by ld
9359 --gc-sections, leaving an invalid (0, 0) entry that can be
9360 confused with the terminator. */
9361 if (text_section_used)
9363 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9364 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9365 text_section_label, "Length");
9367 if (cold_text_section_used)
9369 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9370 "Address");
9371 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9372 cold_text_section_label, "Length");
9375 if (have_multiple_function_sections)
9377 unsigned fde_idx;
9378 dw_fde_ref fde;
9380 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9382 if (DECL_IGNORED_P (fde->decl))
9383 continue;
9384 if (!fde->in_std_section)
9386 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9387 "Address");
9388 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9389 fde->dw_fde_begin, "Length");
9391 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9393 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9394 "Address");
9395 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9396 fde->dw_fde_second_begin, "Length");
9401 /* Output the terminator words. */
9402 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9403 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9406 /* Add a new entry to .debug_ranges. Return the offset at which it
9407 was placed. */
9409 static unsigned int
9410 add_ranges_num (int num)
9412 unsigned int in_use = ranges_table_in_use;
9414 if (in_use == ranges_table_allocated)
9416 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9417 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9418 ranges_table_allocated);
9419 memset (ranges_table + ranges_table_in_use, 0,
9420 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9423 ranges_table[in_use].num = num;
9424 ranges_table_in_use = in_use + 1;
9426 return in_use * 2 * DWARF2_ADDR_SIZE;
9429 /* Add a new entry to .debug_ranges corresponding to a block, or a
9430 range terminator if BLOCK is NULL. */
9432 static unsigned int
9433 add_ranges (const_tree block)
9435 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9438 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9439 When using dwarf_split_debug_info, address attributes in dies destined
9440 for the final executable should be direct references--setting the
9441 parameter force_direct ensures this behavior. */
9443 static void
9444 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9445 bool *added, bool force_direct)
9447 unsigned int in_use = ranges_by_label_in_use;
9448 unsigned int offset;
9450 if (in_use == ranges_by_label_allocated)
9452 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9453 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9454 ranges_by_label,
9455 ranges_by_label_allocated);
9456 memset (ranges_by_label + ranges_by_label_in_use, 0,
9457 RANGES_TABLE_INCREMENT
9458 * sizeof (struct dw_ranges_by_label_struct));
9461 ranges_by_label[in_use].begin = begin;
9462 ranges_by_label[in_use].end = end;
9463 ranges_by_label_in_use = in_use + 1;
9465 offset = add_ranges_num (-(int)in_use - 1);
9466 if (!*added)
9468 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9469 *added = true;
9473 static void
9474 output_ranges (void)
9476 unsigned i;
9477 static const char *const start_fmt = "Offset %#x";
9478 const char *fmt = start_fmt;
9480 for (i = 0; i < ranges_table_in_use; i++)
9482 int block_num = ranges_table[i].num;
9484 if (block_num > 0)
9486 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9487 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9489 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9490 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9492 /* If all code is in the text section, then the compilation
9493 unit base address defaults to DW_AT_low_pc, which is the
9494 base of the text section. */
9495 if (!have_multiple_function_sections)
9497 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9498 text_section_label,
9499 fmt, i * 2 * DWARF2_ADDR_SIZE);
9500 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9501 text_section_label, NULL);
9504 /* Otherwise, the compilation unit base address is zero,
9505 which allows us to use absolute addresses, and not worry
9506 about whether the target supports cross-section
9507 arithmetic. */
9508 else
9510 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9511 fmt, i * 2 * DWARF2_ADDR_SIZE);
9512 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9515 fmt = NULL;
9518 /* Negative block_num stands for an index into ranges_by_label. */
9519 else if (block_num < 0)
9521 int lab_idx = - block_num - 1;
9523 if (!have_multiple_function_sections)
9525 gcc_unreachable ();
9526 #if 0
9527 /* If we ever use add_ranges_by_labels () for a single
9528 function section, all we have to do is to take out
9529 the #if 0 above. */
9530 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9531 ranges_by_label[lab_idx].begin,
9532 text_section_label,
9533 fmt, i * 2 * DWARF2_ADDR_SIZE);
9534 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9535 ranges_by_label[lab_idx].end,
9536 text_section_label, NULL);
9537 #endif
9539 else
9541 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9542 ranges_by_label[lab_idx].begin,
9543 fmt, i * 2 * DWARF2_ADDR_SIZE);
9544 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9545 ranges_by_label[lab_idx].end,
9546 NULL);
9549 else
9551 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9552 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9553 fmt = start_fmt;
9558 /* Data structure containing information about input files. */
9559 struct file_info
9561 const char *path; /* Complete file name. */
9562 const char *fname; /* File name part. */
9563 int length; /* Length of entire string. */
9564 struct dwarf_file_data * file_idx; /* Index in input file table. */
9565 int dir_idx; /* Index in directory table. */
9568 /* Data structure containing information about directories with source
9569 files. */
9570 struct dir_info
9572 const char *path; /* Path including directory name. */
9573 int length; /* Path length. */
9574 int prefix; /* Index of directory entry which is a prefix. */
9575 int count; /* Number of files in this directory. */
9576 int dir_idx; /* Index of directory used as base. */
9579 /* Callback function for file_info comparison. We sort by looking at
9580 the directories in the path. */
9582 static int
9583 file_info_cmp (const void *p1, const void *p2)
9585 const struct file_info *const s1 = (const struct file_info *) p1;
9586 const struct file_info *const s2 = (const struct file_info *) p2;
9587 const unsigned char *cp1;
9588 const unsigned char *cp2;
9590 /* Take care of file names without directories. We need to make sure that
9591 we return consistent values to qsort since some will get confused if
9592 we return the same value when identical operands are passed in opposite
9593 orders. So if neither has a directory, return 0 and otherwise return
9594 1 or -1 depending on which one has the directory. */
9595 if ((s1->path == s1->fname || s2->path == s2->fname))
9596 return (s2->path == s2->fname) - (s1->path == s1->fname);
9598 cp1 = (const unsigned char *) s1->path;
9599 cp2 = (const unsigned char *) s2->path;
9601 while (1)
9603 ++cp1;
9604 ++cp2;
9605 /* Reached the end of the first path? If so, handle like above. */
9606 if ((cp1 == (const unsigned char *) s1->fname)
9607 || (cp2 == (const unsigned char *) s2->fname))
9608 return ((cp2 == (const unsigned char *) s2->fname)
9609 - (cp1 == (const unsigned char *) s1->fname));
9611 /* Character of current path component the same? */
9612 else if (*cp1 != *cp2)
9613 return *cp1 - *cp2;
9617 struct file_name_acquire_data
9619 struct file_info *files;
9620 int used_files;
9621 int max_files;
9624 /* Traversal function for the hash table. */
9626 static int
9627 file_name_acquire (void ** slot, void *data)
9629 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9630 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9631 struct file_info *fi;
9632 const char *f;
9634 gcc_assert (fnad->max_files >= d->emitted_number);
9636 if (! d->emitted_number)
9637 return 1;
9639 gcc_assert (fnad->max_files != fnad->used_files);
9641 fi = fnad->files + fnad->used_files++;
9643 /* Skip all leading "./". */
9644 f = d->filename;
9645 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9646 f += 2;
9648 /* Create a new array entry. */
9649 fi->path = f;
9650 fi->length = strlen (f);
9651 fi->file_idx = d;
9653 /* Search for the file name part. */
9654 f = strrchr (f, DIR_SEPARATOR);
9655 #if defined (DIR_SEPARATOR_2)
9657 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9659 if (g != NULL)
9661 if (f == NULL || f < g)
9662 f = g;
9665 #endif
9667 fi->fname = f == NULL ? fi->path : f + 1;
9668 return 1;
9671 /* Output the directory table and the file name table. We try to minimize
9672 the total amount of memory needed. A heuristic is used to avoid large
9673 slowdowns with many input files. */
9675 static void
9676 output_file_names (void)
9678 struct file_name_acquire_data fnad;
9679 int numfiles;
9680 struct file_info *files;
9681 struct dir_info *dirs;
9682 int *saved;
9683 int *savehere;
9684 int *backmap;
9685 int ndirs;
9686 int idx_offset;
9687 int i;
9689 if (!last_emitted_file)
9691 dw2_asm_output_data (1, 0, "End directory table");
9692 dw2_asm_output_data (1, 0, "End file name table");
9693 return;
9696 numfiles = last_emitted_file->emitted_number;
9698 /* Allocate the various arrays we need. */
9699 files = XALLOCAVEC (struct file_info, numfiles);
9700 dirs = XALLOCAVEC (struct dir_info, numfiles);
9702 fnad.files = files;
9703 fnad.used_files = 0;
9704 fnad.max_files = numfiles;
9705 htab_traverse (file_table, file_name_acquire, &fnad);
9706 gcc_assert (fnad.used_files == fnad.max_files);
9708 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9710 /* Find all the different directories used. */
9711 dirs[0].path = files[0].path;
9712 dirs[0].length = files[0].fname - files[0].path;
9713 dirs[0].prefix = -1;
9714 dirs[0].count = 1;
9715 dirs[0].dir_idx = 0;
9716 files[0].dir_idx = 0;
9717 ndirs = 1;
9719 for (i = 1; i < numfiles; i++)
9720 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9721 && memcmp (dirs[ndirs - 1].path, files[i].path,
9722 dirs[ndirs - 1].length) == 0)
9724 /* Same directory as last entry. */
9725 files[i].dir_idx = ndirs - 1;
9726 ++dirs[ndirs - 1].count;
9728 else
9730 int j;
9732 /* This is a new directory. */
9733 dirs[ndirs].path = files[i].path;
9734 dirs[ndirs].length = files[i].fname - files[i].path;
9735 dirs[ndirs].count = 1;
9736 dirs[ndirs].dir_idx = ndirs;
9737 files[i].dir_idx = ndirs;
9739 /* Search for a prefix. */
9740 dirs[ndirs].prefix = -1;
9741 for (j = 0; j < ndirs; j++)
9742 if (dirs[j].length < dirs[ndirs].length
9743 && dirs[j].length > 1
9744 && (dirs[ndirs].prefix == -1
9745 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9746 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9747 dirs[ndirs].prefix = j;
9749 ++ndirs;
9752 /* Now to the actual work. We have to find a subset of the directories which
9753 allow expressing the file name using references to the directory table
9754 with the least amount of characters. We do not do an exhaustive search
9755 where we would have to check out every combination of every single
9756 possible prefix. Instead we use a heuristic which provides nearly optimal
9757 results in most cases and never is much off. */
9758 saved = XALLOCAVEC (int, ndirs);
9759 savehere = XALLOCAVEC (int, ndirs);
9761 memset (saved, '\0', ndirs * sizeof (saved[0]));
9762 for (i = 0; i < ndirs; i++)
9764 int j;
9765 int total;
9767 /* We can always save some space for the current directory. But this
9768 does not mean it will be enough to justify adding the directory. */
9769 savehere[i] = dirs[i].length;
9770 total = (savehere[i] - saved[i]) * dirs[i].count;
9772 for (j = i + 1; j < ndirs; j++)
9774 savehere[j] = 0;
9775 if (saved[j] < dirs[i].length)
9777 /* Determine whether the dirs[i] path is a prefix of the
9778 dirs[j] path. */
9779 int k;
9781 k = dirs[j].prefix;
9782 while (k != -1 && k != (int) i)
9783 k = dirs[k].prefix;
9785 if (k == (int) i)
9787 /* Yes it is. We can possibly save some memory by
9788 writing the filenames in dirs[j] relative to
9789 dirs[i]. */
9790 savehere[j] = dirs[i].length;
9791 total += (savehere[j] - saved[j]) * dirs[j].count;
9796 /* Check whether we can save enough to justify adding the dirs[i]
9797 directory. */
9798 if (total > dirs[i].length + 1)
9800 /* It's worthwhile adding. */
9801 for (j = i; j < ndirs; j++)
9802 if (savehere[j] > 0)
9804 /* Remember how much we saved for this directory so far. */
9805 saved[j] = savehere[j];
9807 /* Remember the prefix directory. */
9808 dirs[j].dir_idx = i;
9813 /* Emit the directory name table. */
9814 idx_offset = dirs[0].length > 0 ? 1 : 0;
9815 for (i = 1 - idx_offset; i < ndirs; i++)
9816 dw2_asm_output_nstring (dirs[i].path,
9817 dirs[i].length
9818 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9819 "Directory Entry: %#x", i + idx_offset);
9821 dw2_asm_output_data (1, 0, "End directory table");
9823 /* We have to emit them in the order of emitted_number since that's
9824 used in the debug info generation. To do this efficiently we
9825 generate a back-mapping of the indices first. */
9826 backmap = XALLOCAVEC (int, numfiles);
9827 for (i = 0; i < numfiles; i++)
9828 backmap[files[i].file_idx->emitted_number - 1] = i;
9830 /* Now write all the file names. */
9831 for (i = 0; i < numfiles; i++)
9833 int file_idx = backmap[i];
9834 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9836 #ifdef VMS_DEBUGGING_INFO
9837 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9839 /* Setting these fields can lead to debugger miscomparisons,
9840 but VMS Debug requires them to be set correctly. */
9842 int ver;
9843 long long cdt;
9844 long siz;
9845 int maxfilelen = strlen (files[file_idx].path)
9846 + dirs[dir_idx].length
9847 + MAX_VMS_VERSION_LEN + 1;
9848 char *filebuf = XALLOCAVEC (char, maxfilelen);
9850 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9851 snprintf (filebuf, maxfilelen, "%s;%d",
9852 files[file_idx].path + dirs[dir_idx].length, ver);
9854 dw2_asm_output_nstring
9855 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9857 /* Include directory index. */
9858 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9860 /* Modification time. */
9861 dw2_asm_output_data_uleb128
9862 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9863 ? cdt : 0,
9864 NULL);
9866 /* File length in bytes. */
9867 dw2_asm_output_data_uleb128
9868 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9869 ? siz : 0,
9870 NULL);
9871 #else
9872 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9873 "File Entry: %#x", (unsigned) i + 1);
9875 /* Include directory index. */
9876 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9878 /* Modification time. */
9879 dw2_asm_output_data_uleb128 (0, NULL);
9881 /* File length in bytes. */
9882 dw2_asm_output_data_uleb128 (0, NULL);
9883 #endif /* VMS_DEBUGGING_INFO */
9886 dw2_asm_output_data (1, 0, "End file name table");
9890 /* Output one line number table into the .debug_line section. */
9892 static void
9893 output_one_line_info_table (dw_line_info_table *table)
9895 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9896 unsigned int current_line = 1;
9897 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9898 dw_line_info_entry *ent;
9899 size_t i;
9901 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9903 switch (ent->opcode)
9905 case LI_set_address:
9906 /* ??? Unfortunately, we have little choice here currently, and
9907 must always use the most general form. GCC does not know the
9908 address delta itself, so we can't use DW_LNS_advance_pc. Many
9909 ports do have length attributes which will give an upper bound
9910 on the address range. We could perhaps use length attributes
9911 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9912 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9914 /* This can handle any delta. This takes
9915 4+DWARF2_ADDR_SIZE bytes. */
9916 dw2_asm_output_data (1, 0, "set address %s", line_label);
9917 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9918 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9919 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9920 break;
9922 case LI_set_line:
9923 if (ent->val == current_line)
9925 /* We still need to start a new row, so output a copy insn. */
9926 dw2_asm_output_data (1, DW_LNS_copy,
9927 "copy line %u", current_line);
9929 else
9931 int line_offset = ent->val - current_line;
9932 int line_delta = line_offset - DWARF_LINE_BASE;
9934 current_line = ent->val;
9935 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9937 /* This can handle deltas from -10 to 234, using the current
9938 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9939 This takes 1 byte. */
9940 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9941 "line %u", current_line);
9943 else
9945 /* This can handle any delta. This takes at least 4 bytes,
9946 depending on the value being encoded. */
9947 dw2_asm_output_data (1, DW_LNS_advance_line,
9948 "advance to line %u", current_line);
9949 dw2_asm_output_data_sleb128 (line_offset, NULL);
9950 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9953 break;
9955 case LI_set_file:
9956 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9957 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9958 break;
9960 case LI_set_column:
9961 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9962 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9963 break;
9965 case LI_negate_stmt:
9966 current_is_stmt = !current_is_stmt;
9967 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9968 "is_stmt %d", current_is_stmt);
9969 break;
9971 case LI_set_prologue_end:
9972 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9973 "set prologue end");
9974 break;
9976 case LI_set_epilogue_begin:
9977 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9978 "set epilogue begin");
9979 break;
9981 case LI_set_discriminator:
9982 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9983 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9984 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9985 dw2_asm_output_data_uleb128 (ent->val, NULL);
9986 break;
9990 /* Emit debug info for the address of the end of the table. */
9991 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9992 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9993 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9994 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9996 dw2_asm_output_data (1, 0, "end sequence");
9997 dw2_asm_output_data_uleb128 (1, NULL);
9998 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10001 /* Output the source line number correspondence information. This
10002 information goes into the .debug_line section. */
10004 static void
10005 output_line_info (bool prologue_only)
10007 char l1[20], l2[20], p1[20], p2[20];
10008 int ver = dwarf_version;
10009 bool saw_one = false;
10010 int opc;
10012 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10013 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10014 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10015 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10017 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10018 dw2_asm_output_data (4, 0xffffffff,
10019 "Initial length escape value indicating 64-bit DWARF extension");
10020 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10021 "Length of Source Line Info");
10022 ASM_OUTPUT_LABEL (asm_out_file, l1);
10024 dw2_asm_output_data (2, ver, "DWARF Version");
10025 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10026 ASM_OUTPUT_LABEL (asm_out_file, p1);
10028 /* Define the architecture-dependent minimum instruction length (in bytes).
10029 In this implementation of DWARF, this field is used for information
10030 purposes only. Since GCC generates assembly language, we have no
10031 a priori knowledge of how many instruction bytes are generated for each
10032 source line, and therefore can use only the DW_LNE_set_address and
10033 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10034 this as '1', which is "correct enough" for all architectures,
10035 and don't let the target override. */
10036 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10038 if (ver >= 4)
10039 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10040 "Maximum Operations Per Instruction");
10041 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10042 "Default is_stmt_start flag");
10043 dw2_asm_output_data (1, DWARF_LINE_BASE,
10044 "Line Base Value (Special Opcodes)");
10045 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10046 "Line Range Value (Special Opcodes)");
10047 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10048 "Special Opcode Base");
10050 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10052 int n_op_args;
10053 switch (opc)
10055 case DW_LNS_advance_pc:
10056 case DW_LNS_advance_line:
10057 case DW_LNS_set_file:
10058 case DW_LNS_set_column:
10059 case DW_LNS_fixed_advance_pc:
10060 case DW_LNS_set_isa:
10061 n_op_args = 1;
10062 break;
10063 default:
10064 n_op_args = 0;
10065 break;
10068 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10069 opc, n_op_args);
10072 /* Write out the information about the files we use. */
10073 output_file_names ();
10074 ASM_OUTPUT_LABEL (asm_out_file, p2);
10075 if (prologue_only)
10077 /* Output the marker for the end of the line number info. */
10078 ASM_OUTPUT_LABEL (asm_out_file, l2);
10079 return;
10082 if (separate_line_info)
10084 dw_line_info_table *table;
10085 size_t i;
10087 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10088 if (table->in_use)
10090 output_one_line_info_table (table);
10091 saw_one = true;
10094 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10096 output_one_line_info_table (cold_text_section_line_info);
10097 saw_one = true;
10100 /* ??? Some Darwin linkers crash on a .debug_line section with no
10101 sequences. Further, merely a DW_LNE_end_sequence entry is not
10102 sufficient -- the address column must also be initialized.
10103 Make sure to output at least one set_address/end_sequence pair,
10104 choosing .text since that section is always present. */
10105 if (text_section_line_info->in_use || !saw_one)
10106 output_one_line_info_table (text_section_line_info);
10108 /* Output the marker for the end of the line number info. */
10109 ASM_OUTPUT_LABEL (asm_out_file, l2);
10112 /* Given a pointer to a tree node for some base type, return a pointer to
10113 a DIE that describes the given type.
10115 This routine must only be called for GCC type nodes that correspond to
10116 Dwarf base (fundamental) types. */
10118 static dw_die_ref
10119 base_type_die (tree type)
10121 dw_die_ref base_type_result;
10122 enum dwarf_type encoding;
10124 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10125 return 0;
10127 /* If this is a subtype that should not be emitted as a subrange type,
10128 use the base type. See subrange_type_for_debug_p. */
10129 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10130 type = TREE_TYPE (type);
10132 switch (TREE_CODE (type))
10134 case INTEGER_TYPE:
10135 if ((dwarf_version >= 4 || !dwarf_strict)
10136 && TYPE_NAME (type)
10137 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10138 && DECL_IS_BUILTIN (TYPE_NAME (type))
10139 && DECL_NAME (TYPE_NAME (type)))
10141 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10142 if (strcmp (name, "char16_t") == 0
10143 || strcmp (name, "char32_t") == 0)
10145 encoding = DW_ATE_UTF;
10146 break;
10149 if (TYPE_STRING_FLAG (type))
10151 if (TYPE_UNSIGNED (type))
10152 encoding = DW_ATE_unsigned_char;
10153 else
10154 encoding = DW_ATE_signed_char;
10156 else if (TYPE_UNSIGNED (type))
10157 encoding = DW_ATE_unsigned;
10158 else
10159 encoding = DW_ATE_signed;
10160 break;
10162 case REAL_TYPE:
10163 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10165 if (dwarf_version >= 3 || !dwarf_strict)
10166 encoding = DW_ATE_decimal_float;
10167 else
10168 encoding = DW_ATE_lo_user;
10170 else
10171 encoding = DW_ATE_float;
10172 break;
10174 case FIXED_POINT_TYPE:
10175 if (!(dwarf_version >= 3 || !dwarf_strict))
10176 encoding = DW_ATE_lo_user;
10177 else if (TYPE_UNSIGNED (type))
10178 encoding = DW_ATE_unsigned_fixed;
10179 else
10180 encoding = DW_ATE_signed_fixed;
10181 break;
10183 /* Dwarf2 doesn't know anything about complex ints, so use
10184 a user defined type for it. */
10185 case COMPLEX_TYPE:
10186 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10187 encoding = DW_ATE_complex_float;
10188 else
10189 encoding = DW_ATE_lo_user;
10190 break;
10192 case BOOLEAN_TYPE:
10193 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10194 encoding = DW_ATE_boolean;
10195 break;
10197 default:
10198 /* No other TREE_CODEs are Dwarf fundamental types. */
10199 gcc_unreachable ();
10202 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10204 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10205 int_size_in_bytes (type));
10206 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10207 add_pubtype (type, base_type_result);
10209 return base_type_result;
10212 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10213 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10215 static inline int
10216 is_base_type (tree type)
10218 switch (TREE_CODE (type))
10220 case ERROR_MARK:
10221 case VOID_TYPE:
10222 case INTEGER_TYPE:
10223 case REAL_TYPE:
10224 case FIXED_POINT_TYPE:
10225 case COMPLEX_TYPE:
10226 case BOOLEAN_TYPE:
10227 return 1;
10229 case ARRAY_TYPE:
10230 case RECORD_TYPE:
10231 case UNION_TYPE:
10232 case QUAL_UNION_TYPE:
10233 case ENUMERAL_TYPE:
10234 case FUNCTION_TYPE:
10235 case METHOD_TYPE:
10236 case POINTER_TYPE:
10237 case REFERENCE_TYPE:
10238 case NULLPTR_TYPE:
10239 case OFFSET_TYPE:
10240 case LANG_TYPE:
10241 case VECTOR_TYPE:
10242 return 0;
10244 default:
10245 gcc_unreachable ();
10248 return 0;
10251 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10252 node, return the size in bits for the type if it is a constant, or else
10253 return the alignment for the type if the type's size is not constant, or
10254 else return BITS_PER_WORD if the type actually turns out to be an
10255 ERROR_MARK node. */
10257 static inline unsigned HOST_WIDE_INT
10258 simple_type_size_in_bits (const_tree type)
10260 if (TREE_CODE (type) == ERROR_MARK)
10261 return BITS_PER_WORD;
10262 else if (TYPE_SIZE (type) == NULL_TREE)
10263 return 0;
10264 else if (host_integerp (TYPE_SIZE (type), 1))
10265 return tree_low_cst (TYPE_SIZE (type), 1);
10266 else
10267 return TYPE_ALIGN (type);
10270 /* Similarly, but return a double_int instead of UHWI. */
10272 static inline double_int
10273 double_int_type_size_in_bits (const_tree type)
10275 if (TREE_CODE (type) == ERROR_MARK)
10276 return double_int::from_uhwi (BITS_PER_WORD);
10277 else if (TYPE_SIZE (type) == NULL_TREE)
10278 return double_int_zero;
10279 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10280 return tree_to_double_int (TYPE_SIZE (type));
10281 else
10282 return double_int::from_uhwi (TYPE_ALIGN (type));
10285 /* Given a pointer to a tree node for a subrange type, return a pointer
10286 to a DIE that describes the given type. */
10288 static dw_die_ref
10289 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10291 dw_die_ref subrange_die;
10292 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10294 if (context_die == NULL)
10295 context_die = comp_unit_die ();
10297 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10299 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10301 /* The size of the subrange type and its base type do not match,
10302 so we need to generate a size attribute for the subrange type. */
10303 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10306 if (low)
10307 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10308 if (high)
10309 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10311 return subrange_die;
10314 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10315 entry that chains various modifiers in front of the given type. */
10317 static dw_die_ref
10318 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10319 dw_die_ref context_die)
10321 enum tree_code code = TREE_CODE (type);
10322 dw_die_ref mod_type_die;
10323 dw_die_ref sub_die = NULL;
10324 tree item_type = NULL;
10325 tree qualified_type;
10326 tree name, low, high;
10327 dw_die_ref mod_scope;
10329 if (code == ERROR_MARK)
10330 return NULL;
10332 /* See if we already have the appropriately qualified variant of
10333 this type. */
10334 qualified_type
10335 = get_qualified_type (type,
10336 ((is_const_type ? TYPE_QUAL_CONST : 0)
10337 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10339 if (qualified_type == sizetype
10340 && TYPE_NAME (qualified_type)
10341 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10343 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10345 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10346 && TYPE_PRECISION (t)
10347 == TYPE_PRECISION (qualified_type)
10348 && TYPE_UNSIGNED (t)
10349 == TYPE_UNSIGNED (qualified_type));
10350 qualified_type = t;
10353 /* If we do, then we can just use its DIE, if it exists. */
10354 if (qualified_type)
10356 mod_type_die = lookup_type_die (qualified_type);
10357 if (mod_type_die)
10358 return mod_type_die;
10361 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10363 /* Handle C typedef types. */
10364 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10365 && !DECL_ARTIFICIAL (name))
10367 tree dtype = TREE_TYPE (name);
10369 if (qualified_type == dtype)
10371 /* For a named type, use the typedef. */
10372 gen_type_die (qualified_type, context_die);
10373 return lookup_type_die (qualified_type);
10375 else if (is_const_type < TYPE_READONLY (dtype)
10376 || is_volatile_type < TYPE_VOLATILE (dtype)
10377 || (is_const_type <= TYPE_READONLY (dtype)
10378 && is_volatile_type <= TYPE_VOLATILE (dtype)
10379 && DECL_ORIGINAL_TYPE (name) != type))
10380 /* cv-unqualified version of named type. Just use the unnamed
10381 type to which it refers. */
10382 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10383 is_const_type, is_volatile_type,
10384 context_die);
10385 /* Else cv-qualified version of named type; fall through. */
10388 mod_scope = scope_die_for (type, context_die);
10390 if (is_const_type
10391 /* If both is_const_type and is_volatile_type, prefer the path
10392 which leads to a qualified type. */
10393 && (!is_volatile_type
10394 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10395 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10397 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10398 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10400 else if (is_volatile_type)
10402 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10403 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10405 else if (code == POINTER_TYPE)
10407 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10408 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10409 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10410 item_type = TREE_TYPE (type);
10411 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10412 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10413 TYPE_ADDR_SPACE (item_type));
10415 else if (code == REFERENCE_TYPE)
10417 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10418 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10419 type);
10420 else
10421 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10422 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10423 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10424 item_type = TREE_TYPE (type);
10425 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10426 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10427 TYPE_ADDR_SPACE (item_type));
10429 else if (code == INTEGER_TYPE
10430 && TREE_TYPE (type) != NULL_TREE
10431 && subrange_type_for_debug_p (type, &low, &high))
10433 mod_type_die = subrange_type_die (type, low, high, context_die);
10434 item_type = TREE_TYPE (type);
10436 else if (is_base_type (type))
10437 mod_type_die = base_type_die (type);
10438 else
10440 gen_type_die (type, context_die);
10442 /* We have to get the type_main_variant here (and pass that to the
10443 `lookup_type_die' routine) because the ..._TYPE node we have
10444 might simply be a *copy* of some original type node (where the
10445 copy was created to help us keep track of typedef names) and
10446 that copy might have a different TYPE_UID from the original
10447 ..._TYPE node. */
10448 if (TREE_CODE (type) != VECTOR_TYPE)
10449 return lookup_type_die (type_main_variant (type));
10450 else
10451 /* Vectors have the debugging information in the type,
10452 not the main variant. */
10453 return lookup_type_die (type);
10456 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10457 don't output a DW_TAG_typedef, since there isn't one in the
10458 user's program; just attach a DW_AT_name to the type.
10459 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10460 if the base type already has the same name. */
10461 if (name
10462 && ((TREE_CODE (name) != TYPE_DECL
10463 && (qualified_type == TYPE_MAIN_VARIANT (type)
10464 || (!is_const_type && !is_volatile_type)))
10465 || (TREE_CODE (name) == TYPE_DECL
10466 && TREE_TYPE (name) == qualified_type
10467 && DECL_NAME (name))))
10469 if (TREE_CODE (name) == TYPE_DECL)
10470 /* Could just call add_name_and_src_coords_attributes here,
10471 but since this is a builtin type it doesn't have any
10472 useful source coordinates anyway. */
10473 name = DECL_NAME (name);
10474 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10476 /* This probably indicates a bug. */
10477 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10479 name = TYPE_NAME (type);
10480 if (name
10481 && TREE_CODE (name) == TYPE_DECL)
10482 name = DECL_NAME (name);
10483 add_name_attribute (mod_type_die,
10484 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10487 if (qualified_type)
10488 equate_type_number_to_die (qualified_type, mod_type_die);
10490 if (item_type)
10491 /* We must do this after the equate_type_number_to_die call, in case
10492 this is a recursive type. This ensures that the modified_type_die
10493 recursion will terminate even if the type is recursive. Recursive
10494 types are possible in Ada. */
10495 sub_die = modified_type_die (item_type,
10496 TYPE_READONLY (item_type),
10497 TYPE_VOLATILE (item_type),
10498 context_die);
10500 if (sub_die != NULL)
10501 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10503 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10504 if (TYPE_ARTIFICIAL (type))
10505 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10507 return mod_type_die;
10510 /* Generate DIEs for the generic parameters of T.
10511 T must be either a generic type or a generic function.
10512 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10514 static void
10515 gen_generic_params_dies (tree t)
10517 tree parms, args;
10518 int parms_num, i;
10519 dw_die_ref die = NULL;
10520 int non_default;
10522 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10523 return;
10525 if (TYPE_P (t))
10526 die = lookup_type_die (t);
10527 else if (DECL_P (t))
10528 die = lookup_decl_die (t);
10530 gcc_assert (die);
10532 parms = lang_hooks.get_innermost_generic_parms (t);
10533 if (!parms)
10534 /* T has no generic parameter. It means T is neither a generic type
10535 or function. End of story. */
10536 return;
10538 parms_num = TREE_VEC_LENGTH (parms);
10539 args = lang_hooks.get_innermost_generic_args (t);
10540 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10541 non_default = int_cst_value (TREE_CHAIN (args));
10542 else
10543 non_default = TREE_VEC_LENGTH (args);
10544 for (i = 0; i < parms_num; i++)
10546 tree parm, arg, arg_pack_elems;
10547 dw_die_ref parm_die;
10549 parm = TREE_VEC_ELT (parms, i);
10550 arg = TREE_VEC_ELT (args, i);
10551 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10552 gcc_assert (parm && TREE_VALUE (parm) && arg);
10554 if (parm && TREE_VALUE (parm) && arg)
10556 /* If PARM represents a template parameter pack,
10557 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10558 by DW_TAG_template_*_parameter DIEs for the argument
10559 pack elements of ARG. Note that ARG would then be
10560 an argument pack. */
10561 if (arg_pack_elems)
10562 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10563 arg_pack_elems,
10564 die);
10565 else
10566 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10567 true /* emit name */, die);
10568 if (i >= non_default)
10569 add_AT_flag (parm_die, DW_AT_default_value, 1);
10574 /* Create and return a DIE for PARM which should be
10575 the representation of a generic type parameter.
10576 For instance, in the C++ front end, PARM would be a template parameter.
10577 ARG is the argument to PARM.
10578 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10579 name of the PARM.
10580 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10581 as a child node. */
10583 static dw_die_ref
10584 generic_parameter_die (tree parm, tree arg,
10585 bool emit_name_p,
10586 dw_die_ref parent_die)
10588 dw_die_ref tmpl_die = NULL;
10589 const char *name = NULL;
10591 if (!parm || !DECL_NAME (parm) || !arg)
10592 return NULL;
10594 /* We support non-type generic parameters and arguments,
10595 type generic parameters and arguments, as well as
10596 generic generic parameters (a.k.a. template template parameters in C++)
10597 and arguments. */
10598 if (TREE_CODE (parm) == PARM_DECL)
10599 /* PARM is a nontype generic parameter */
10600 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10601 else if (TREE_CODE (parm) == TYPE_DECL)
10602 /* PARM is a type generic parameter. */
10603 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10604 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10605 /* PARM is a generic generic parameter.
10606 Its DIE is a GNU extension. It shall have a
10607 DW_AT_name attribute to represent the name of the template template
10608 parameter, and a DW_AT_GNU_template_name attribute to represent the
10609 name of the template template argument. */
10610 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10611 parent_die, parm);
10612 else
10613 gcc_unreachable ();
10615 if (tmpl_die)
10617 tree tmpl_type;
10619 /* If PARM is a generic parameter pack, it means we are
10620 emitting debug info for a template argument pack element.
10621 In other terms, ARG is a template argument pack element.
10622 In that case, we don't emit any DW_AT_name attribute for
10623 the die. */
10624 if (emit_name_p)
10626 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10627 gcc_assert (name);
10628 add_AT_string (tmpl_die, DW_AT_name, name);
10631 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10633 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10634 TMPL_DIE should have a child DW_AT_type attribute that is set
10635 to the type of the argument to PARM, which is ARG.
10636 If PARM is a type generic parameter, TMPL_DIE should have a
10637 child DW_AT_type that is set to ARG. */
10638 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10639 add_type_attribute (tmpl_die, tmpl_type, 0,
10640 TREE_THIS_VOLATILE (tmpl_type),
10641 parent_die);
10643 else
10645 /* So TMPL_DIE is a DIE representing a
10646 a generic generic template parameter, a.k.a template template
10647 parameter in C++ and arg is a template. */
10649 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10650 to the name of the argument. */
10651 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10652 if (name)
10653 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10656 if (TREE_CODE (parm) == PARM_DECL)
10657 /* So PARM is a non-type generic parameter.
10658 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10659 attribute of TMPL_DIE which value represents the value
10660 of ARG.
10661 We must be careful here:
10662 The value of ARG might reference some function decls.
10663 We might currently be emitting debug info for a generic
10664 type and types are emitted before function decls, we don't
10665 know if the function decls referenced by ARG will actually be
10666 emitted after cgraph computations.
10667 So must defer the generation of the DW_AT_const_value to
10668 after cgraph is ready. */
10669 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10672 return tmpl_die;
10675 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10676 PARM_PACK must be a template parameter pack. The returned DIE
10677 will be child DIE of PARENT_DIE. */
10679 static dw_die_ref
10680 template_parameter_pack_die (tree parm_pack,
10681 tree parm_pack_args,
10682 dw_die_ref parent_die)
10684 dw_die_ref die;
10685 int j;
10687 gcc_assert (parent_die && parm_pack);
10689 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10690 add_name_and_src_coords_attributes (die, parm_pack);
10691 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10692 generic_parameter_die (parm_pack,
10693 TREE_VEC_ELT (parm_pack_args, j),
10694 false /* Don't emit DW_AT_name */,
10695 die);
10696 return die;
10699 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10700 an enumerated type. */
10702 static inline int
10703 type_is_enum (const_tree type)
10705 return TREE_CODE (type) == ENUMERAL_TYPE;
10708 /* Return the DBX register number described by a given RTL node. */
10710 static unsigned int
10711 dbx_reg_number (const_rtx rtl)
10713 unsigned regno = REGNO (rtl);
10715 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10717 #ifdef LEAF_REG_REMAP
10718 if (crtl->uses_only_leaf_regs)
10720 int leaf_reg = LEAF_REG_REMAP (regno);
10721 if (leaf_reg != -1)
10722 regno = (unsigned) leaf_reg;
10724 #endif
10726 regno = DBX_REGISTER_NUMBER (regno);
10727 gcc_assert (regno != INVALID_REGNUM);
10728 return regno;
10731 /* Optionally add a DW_OP_piece term to a location description expression.
10732 DW_OP_piece is only added if the location description expression already
10733 doesn't end with DW_OP_piece. */
10735 static void
10736 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10738 dw_loc_descr_ref loc;
10740 if (*list_head != NULL)
10742 /* Find the end of the chain. */
10743 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10746 if (loc->dw_loc_opc != DW_OP_piece)
10747 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10751 /* Return a location descriptor that designates a machine register or
10752 zero if there is none. */
10754 static dw_loc_descr_ref
10755 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10757 rtx regs;
10759 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10760 return 0;
10762 /* We only use "frame base" when we're sure we're talking about the
10763 post-prologue local stack frame. We do this by *not* running
10764 register elimination until this point, and recognizing the special
10765 argument pointer and soft frame pointer rtx's.
10766 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10767 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10768 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10770 dw_loc_descr_ref result = NULL;
10772 if (dwarf_version >= 4 || !dwarf_strict)
10774 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10775 initialized);
10776 if (result)
10777 add_loc_descr (&result,
10778 new_loc_descr (DW_OP_stack_value, 0, 0));
10780 return result;
10783 regs = targetm.dwarf_register_span (rtl);
10785 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10786 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10787 else
10789 unsigned int dbx_regnum = dbx_reg_number (rtl);
10790 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10791 return 0;
10792 return one_reg_loc_descriptor (dbx_regnum, initialized);
10796 /* Return a location descriptor that designates a machine register for
10797 a given hard register number. */
10799 static dw_loc_descr_ref
10800 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10802 dw_loc_descr_ref reg_loc_descr;
10804 if (regno <= 31)
10805 reg_loc_descr
10806 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10807 else
10808 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10810 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10811 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10813 return reg_loc_descr;
10816 /* Given an RTL of a register, return a location descriptor that
10817 designates a value that spans more than one register. */
10819 static dw_loc_descr_ref
10820 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10821 enum var_init_status initialized)
10823 int size, i;
10824 dw_loc_descr_ref loc_result = NULL;
10826 /* Simple, contiguous registers. */
10827 if (regs == NULL_RTX)
10829 unsigned reg = REGNO (rtl);
10830 int nregs;
10832 #ifdef LEAF_REG_REMAP
10833 if (crtl->uses_only_leaf_regs)
10835 int leaf_reg = LEAF_REG_REMAP (reg);
10836 if (leaf_reg != -1)
10837 reg = (unsigned) leaf_reg;
10839 #endif
10841 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10842 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10844 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10846 loc_result = NULL;
10847 while (nregs--)
10849 dw_loc_descr_ref t;
10851 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10852 VAR_INIT_STATUS_INITIALIZED);
10853 add_loc_descr (&loc_result, t);
10854 add_loc_descr_op_piece (&loc_result, size);
10855 ++reg;
10857 return loc_result;
10860 /* Now onto stupid register sets in non contiguous locations. */
10862 gcc_assert (GET_CODE (regs) == PARALLEL);
10864 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10865 loc_result = NULL;
10867 for (i = 0; i < XVECLEN (regs, 0); ++i)
10869 dw_loc_descr_ref t;
10871 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10872 VAR_INIT_STATUS_INITIALIZED);
10873 add_loc_descr (&loc_result, t);
10874 add_loc_descr_op_piece (&loc_result, size);
10877 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10878 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10879 return loc_result;
10882 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10884 /* Return a location descriptor that designates a constant i,
10885 as a compound operation from constant (i >> shift), constant shift
10886 and DW_OP_shl. */
10888 static dw_loc_descr_ref
10889 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10891 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10892 add_loc_descr (&ret, int_loc_descriptor (shift));
10893 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10894 return ret;
10897 /* Return a location descriptor that designates a constant. */
10899 static dw_loc_descr_ref
10900 int_loc_descriptor (HOST_WIDE_INT i)
10902 enum dwarf_location_atom op;
10904 /* Pick the smallest representation of a constant, rather than just
10905 defaulting to the LEB encoding. */
10906 if (i >= 0)
10908 int clz = clz_hwi (i);
10909 int ctz = ctz_hwi (i);
10910 if (i <= 31)
10911 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10912 else if (i <= 0xff)
10913 op = DW_OP_const1u;
10914 else if (i <= 0xffff)
10915 op = DW_OP_const2u;
10916 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10917 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10918 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10919 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10920 while DW_OP_const4u is 5 bytes. */
10921 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10922 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10923 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10924 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10925 while DW_OP_const4u is 5 bytes. */
10926 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10927 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10928 op = DW_OP_const4u;
10929 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10930 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10931 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10932 while DW_OP_constu of constant >= 0x100000000 takes at least
10933 6 bytes. */
10934 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10935 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10936 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10937 >= HOST_BITS_PER_WIDE_INT)
10938 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10939 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10940 while DW_OP_constu takes in this case at least 6 bytes. */
10941 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10942 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10943 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10944 && size_of_uleb128 (i) > 6)
10945 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10946 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10947 else
10948 op = DW_OP_constu;
10950 else
10952 if (i >= -0x80)
10953 op = DW_OP_const1s;
10954 else if (i >= -0x8000)
10955 op = DW_OP_const2s;
10956 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10958 if (size_of_int_loc_descriptor (i) < 5)
10960 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10961 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10962 return ret;
10964 op = DW_OP_const4s;
10966 else
10968 if (size_of_int_loc_descriptor (i)
10969 < (unsigned long) 1 + size_of_sleb128 (i))
10971 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10972 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10973 return ret;
10975 op = DW_OP_consts;
10979 return new_loc_descr (op, i, 0);
10982 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10983 without actually allocating it. */
10985 static unsigned long
10986 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10988 return size_of_int_loc_descriptor (i >> shift)
10989 + size_of_int_loc_descriptor (shift)
10990 + 1;
10993 /* Return size_of_locs (int_loc_descriptor (i)) without
10994 actually allocating it. */
10996 static unsigned long
10997 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10999 unsigned long s;
11001 if (i >= 0)
11003 int clz, ctz;
11004 if (i <= 31)
11005 return 1;
11006 else if (i <= 0xff)
11007 return 2;
11008 else if (i <= 0xffff)
11009 return 3;
11010 clz = clz_hwi (i);
11011 ctz = ctz_hwi (i);
11012 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11013 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11014 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11015 - clz - 5);
11016 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11017 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11018 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11019 - clz - 8);
11020 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11021 return 5;
11022 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11023 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11024 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11025 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11026 - clz - 8);
11027 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11028 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11029 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11030 - clz - 16);
11031 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11032 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11033 && s > 6)
11034 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11035 - clz - 32);
11036 else
11037 return 1 + s;
11039 else
11041 if (i >= -0x80)
11042 return 2;
11043 else if (i >= -0x8000)
11044 return 3;
11045 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11047 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11049 s = size_of_int_loc_descriptor (-i) + 1;
11050 if (s < 5)
11051 return s;
11053 return 5;
11055 else
11057 unsigned long r = 1 + size_of_sleb128 (i);
11058 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11060 s = size_of_int_loc_descriptor (-i) + 1;
11061 if (s < r)
11062 return s;
11064 return r;
11069 /* Return loc description representing "address" of integer value.
11070 This can appear only as toplevel expression. */
11072 static dw_loc_descr_ref
11073 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11075 int litsize;
11076 dw_loc_descr_ref loc_result = NULL;
11078 if (!(dwarf_version >= 4 || !dwarf_strict))
11079 return NULL;
11081 litsize = size_of_int_loc_descriptor (i);
11082 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11083 is more compact. For DW_OP_stack_value we need:
11084 litsize + 1 (DW_OP_stack_value)
11085 and for DW_OP_implicit_value:
11086 1 (DW_OP_implicit_value) + 1 (length) + size. */
11087 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11089 loc_result = int_loc_descriptor (i);
11090 add_loc_descr (&loc_result,
11091 new_loc_descr (DW_OP_stack_value, 0, 0));
11092 return loc_result;
11095 loc_result = new_loc_descr (DW_OP_implicit_value,
11096 size, 0);
11097 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11098 loc_result->dw_loc_oprnd2.v.val_int = i;
11099 return loc_result;
11102 /* Return a location descriptor that designates a base+offset location. */
11104 static dw_loc_descr_ref
11105 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11106 enum var_init_status initialized)
11108 unsigned int regno;
11109 dw_loc_descr_ref result;
11110 dw_fde_ref fde = cfun->fde;
11112 /* We only use "frame base" when we're sure we're talking about the
11113 post-prologue local stack frame. We do this by *not* running
11114 register elimination until this point, and recognizing the special
11115 argument pointer and soft frame pointer rtx's. */
11116 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11118 rtx elim = (ira_use_lra_p
11119 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11120 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11122 if (elim != reg)
11124 if (GET_CODE (elim) == PLUS)
11126 offset += INTVAL (XEXP (elim, 1));
11127 elim = XEXP (elim, 0);
11129 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11130 && (elim == hard_frame_pointer_rtx
11131 || elim == stack_pointer_rtx))
11132 || elim == (frame_pointer_needed
11133 ? hard_frame_pointer_rtx
11134 : stack_pointer_rtx));
11136 /* If drap register is used to align stack, use frame
11137 pointer + offset to access stack variables. If stack
11138 is aligned without drap, use stack pointer + offset to
11139 access stack variables. */
11140 if (crtl->stack_realign_tried
11141 && reg == frame_pointer_rtx)
11143 int base_reg
11144 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11145 ? HARD_FRAME_POINTER_REGNUM
11146 : REGNO (elim));
11147 return new_reg_loc_descr (base_reg, offset);
11150 gcc_assert (frame_pointer_fb_offset_valid);
11151 offset += frame_pointer_fb_offset;
11152 return new_loc_descr (DW_OP_fbreg, offset, 0);
11156 regno = REGNO (reg);
11157 #ifdef LEAF_REG_REMAP
11158 if (crtl->uses_only_leaf_regs)
11160 int leaf_reg = LEAF_REG_REMAP (regno);
11161 if (leaf_reg != -1)
11162 regno = (unsigned) leaf_reg;
11164 #endif
11165 regno = DWARF_FRAME_REGNUM (regno);
11167 if (!optimize && fde
11168 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11170 /* Use cfa+offset to represent the location of arguments passed
11171 on the stack when drap is used to align stack.
11172 Only do this when not optimizing, for optimized code var-tracking
11173 is supposed to track where the arguments live and the register
11174 used as vdrap or drap in some spot might be used for something
11175 else in other part of the routine. */
11176 return new_loc_descr (DW_OP_fbreg, offset, 0);
11179 if (regno <= 31)
11180 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11181 offset, 0);
11182 else
11183 result = new_loc_descr (DW_OP_bregx, regno, offset);
11185 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11186 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11188 return result;
11191 /* Return true if this RTL expression describes a base+offset calculation. */
11193 static inline int
11194 is_based_loc (const_rtx rtl)
11196 return (GET_CODE (rtl) == PLUS
11197 && ((REG_P (XEXP (rtl, 0))
11198 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11199 && CONST_INT_P (XEXP (rtl, 1)))));
11202 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11203 failed. */
11205 static dw_loc_descr_ref
11206 tls_mem_loc_descriptor (rtx mem)
11208 tree base;
11209 dw_loc_descr_ref loc_result;
11211 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11212 return NULL;
11214 base = get_base_address (MEM_EXPR (mem));
11215 if (base == NULL
11216 || TREE_CODE (base) != VAR_DECL
11217 || !DECL_THREAD_LOCAL_P (base))
11218 return NULL;
11220 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11221 if (loc_result == NULL)
11222 return NULL;
11224 if (MEM_OFFSET (mem))
11225 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11227 return loc_result;
11230 /* Output debug info about reason why we failed to expand expression as dwarf
11231 expression. */
11233 static void
11234 expansion_failed (tree expr, rtx rtl, char const *reason)
11236 if (dump_file && (dump_flags & TDF_DETAILS))
11238 fprintf (dump_file, "Failed to expand as dwarf: ");
11239 if (expr)
11240 print_generic_expr (dump_file, expr, dump_flags);
11241 if (rtl)
11243 fprintf (dump_file, "\n");
11244 print_rtl (dump_file, rtl);
11246 fprintf (dump_file, "\nReason: %s\n", reason);
11250 /* Helper function for const_ok_for_output, called either directly
11251 or via for_each_rtx. */
11253 static int
11254 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11256 rtx rtl = *rtlp;
11258 if (GET_CODE (rtl) == UNSPEC)
11260 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11261 we can't express it in the debug info. */
11262 #ifdef ENABLE_CHECKING
11263 /* Don't complain about TLS UNSPECs, those are just too hard to
11264 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11265 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11266 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11267 if (XVECLEN (rtl, 0) == 0
11268 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11269 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11270 inform (current_function_decl
11271 ? DECL_SOURCE_LOCATION (current_function_decl)
11272 : UNKNOWN_LOCATION,
11273 #if NUM_UNSPEC_VALUES > 0
11274 "non-delegitimized UNSPEC %s (%d) found in variable location",
11275 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11276 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11277 XINT (rtl, 1));
11278 #else
11279 "non-delegitimized UNSPEC %d found in variable location",
11280 XINT (rtl, 1));
11281 #endif
11282 #endif
11283 expansion_failed (NULL_TREE, rtl,
11284 "UNSPEC hasn't been delegitimized.\n");
11285 return 1;
11288 if (targetm.const_not_ok_for_debug_p (rtl))
11290 expansion_failed (NULL_TREE, rtl,
11291 "Expression rejected for debug by the backend.\n");
11292 return 1;
11295 if (GET_CODE (rtl) != SYMBOL_REF)
11296 return 0;
11298 if (CONSTANT_POOL_ADDRESS_P (rtl))
11300 bool marked;
11301 get_pool_constant_mark (rtl, &marked);
11302 /* If all references to this pool constant were optimized away,
11303 it was not output and thus we can't represent it. */
11304 if (!marked)
11306 expansion_failed (NULL_TREE, rtl,
11307 "Constant was removed from constant pool.\n");
11308 return 1;
11312 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11313 return 1;
11315 /* Avoid references to external symbols in debug info, on several targets
11316 the linker might even refuse to link when linking a shared library,
11317 and in many other cases the relocations for .debug_info/.debug_loc are
11318 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11319 to be defined within the same shared library or executable are fine. */
11320 if (SYMBOL_REF_EXTERNAL_P (rtl))
11322 tree decl = SYMBOL_REF_DECL (rtl);
11324 if (decl == NULL || !targetm.binds_local_p (decl))
11326 expansion_failed (NULL_TREE, rtl,
11327 "Symbol not defined in current TU.\n");
11328 return 1;
11332 return 0;
11335 /* Return true if constant RTL can be emitted in DW_OP_addr or
11336 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11337 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11339 static bool
11340 const_ok_for_output (rtx rtl)
11342 if (GET_CODE (rtl) == SYMBOL_REF)
11343 return const_ok_for_output_1 (&rtl, NULL) == 0;
11345 if (GET_CODE (rtl) == CONST)
11346 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11348 return true;
11351 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11352 if possible, NULL otherwise. */
11354 static dw_die_ref
11355 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11357 dw_die_ref type_die;
11358 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11360 if (type == NULL)
11361 return NULL;
11362 switch (TREE_CODE (type))
11364 case INTEGER_TYPE:
11365 case REAL_TYPE:
11366 break;
11367 default:
11368 return NULL;
11370 type_die = lookup_type_die (type);
11371 if (!type_die)
11372 type_die = modified_type_die (type, false, false, comp_unit_die ());
11373 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11374 return NULL;
11375 return type_die;
11378 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11379 type matching MODE, or, if MODE is narrower than or as wide as
11380 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11381 possible. */
11383 static dw_loc_descr_ref
11384 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11386 enum machine_mode outer_mode = mode;
11387 dw_die_ref type_die;
11388 dw_loc_descr_ref cvt;
11390 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11392 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11393 return op;
11395 type_die = base_type_for_mode (outer_mode, 1);
11396 if (type_die == NULL)
11397 return NULL;
11398 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11399 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11400 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11401 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11402 add_loc_descr (&op, cvt);
11403 return op;
11406 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11408 static dw_loc_descr_ref
11409 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11410 dw_loc_descr_ref op1)
11412 dw_loc_descr_ref ret = op0;
11413 add_loc_descr (&ret, op1);
11414 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11415 if (STORE_FLAG_VALUE != 1)
11417 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11418 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11420 return ret;
11423 /* Return location descriptor for signed comparison OP RTL. */
11425 static dw_loc_descr_ref
11426 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11427 enum machine_mode mem_mode)
11429 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11430 dw_loc_descr_ref op0, op1;
11431 int shift;
11433 if (op_mode == VOIDmode)
11434 op_mode = GET_MODE (XEXP (rtl, 1));
11435 if (op_mode == VOIDmode)
11436 return NULL;
11438 if (dwarf_strict
11439 && (GET_MODE_CLASS (op_mode) != MODE_INT
11440 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11441 return NULL;
11443 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11444 VAR_INIT_STATUS_INITIALIZED);
11445 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11446 VAR_INIT_STATUS_INITIALIZED);
11448 if (op0 == NULL || op1 == NULL)
11449 return NULL;
11451 if (GET_MODE_CLASS (op_mode) != MODE_INT
11452 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11453 return compare_loc_descriptor (op, op0, op1);
11455 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11457 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11458 dw_loc_descr_ref cvt;
11460 if (type_die == NULL)
11461 return NULL;
11462 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11463 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11464 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11465 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11466 add_loc_descr (&op0, cvt);
11467 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11468 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11469 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11470 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11471 add_loc_descr (&op1, cvt);
11472 return compare_loc_descriptor (op, op0, op1);
11475 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11476 /* For eq/ne, if the operands are known to be zero-extended,
11477 there is no need to do the fancy shifting up. */
11478 if (op == DW_OP_eq || op == DW_OP_ne)
11480 dw_loc_descr_ref last0, last1;
11481 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11483 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11485 /* deref_size zero extends, and for constants we can check
11486 whether they are zero extended or not. */
11487 if (((last0->dw_loc_opc == DW_OP_deref_size
11488 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11489 || (CONST_INT_P (XEXP (rtl, 0))
11490 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11491 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11492 && ((last1->dw_loc_opc == DW_OP_deref_size
11493 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11494 || (CONST_INT_P (XEXP (rtl, 1))
11495 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11496 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11497 return compare_loc_descriptor (op, op0, op1);
11499 /* EQ/NE comparison against constant in narrower type than
11500 DWARF2_ADDR_SIZE can be performed either as
11501 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11502 DW_OP_{eq,ne}
11504 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11505 DW_OP_{eq,ne}. Pick whatever is shorter. */
11506 if (CONST_INT_P (XEXP (rtl, 1))
11507 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11508 && (size_of_int_loc_descriptor (shift) + 1
11509 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11510 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11511 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11512 & GET_MODE_MASK (op_mode))))
11514 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11515 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11516 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11517 & GET_MODE_MASK (op_mode));
11518 return compare_loc_descriptor (op, op0, op1);
11521 add_loc_descr (&op0, int_loc_descriptor (shift));
11522 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11523 if (CONST_INT_P (XEXP (rtl, 1)))
11524 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11525 else
11527 add_loc_descr (&op1, int_loc_descriptor (shift));
11528 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11530 return compare_loc_descriptor (op, op0, op1);
11533 /* Return location descriptor for unsigned comparison OP RTL. */
11535 static dw_loc_descr_ref
11536 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11537 enum machine_mode mem_mode)
11539 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11540 dw_loc_descr_ref op0, op1;
11542 if (op_mode == VOIDmode)
11543 op_mode = GET_MODE (XEXP (rtl, 1));
11544 if (op_mode == VOIDmode)
11545 return NULL;
11546 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11547 return NULL;
11549 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11550 return NULL;
11552 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11553 VAR_INIT_STATUS_INITIALIZED);
11554 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11555 VAR_INIT_STATUS_INITIALIZED);
11557 if (op0 == NULL || op1 == NULL)
11558 return NULL;
11560 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11562 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11563 dw_loc_descr_ref last0, last1;
11564 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11566 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11568 if (CONST_INT_P (XEXP (rtl, 0)))
11569 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11570 /* deref_size zero extends, so no need to mask it again. */
11571 else if (last0->dw_loc_opc != DW_OP_deref_size
11572 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11574 add_loc_descr (&op0, int_loc_descriptor (mask));
11575 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11577 if (CONST_INT_P (XEXP (rtl, 1)))
11578 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11579 /* deref_size zero extends, so no need to mask it again. */
11580 else if (last1->dw_loc_opc != DW_OP_deref_size
11581 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11583 add_loc_descr (&op1, int_loc_descriptor (mask));
11584 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11587 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11589 HOST_WIDE_INT bias = 1;
11590 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11591 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11592 if (CONST_INT_P (XEXP (rtl, 1)))
11593 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11594 + INTVAL (XEXP (rtl, 1)));
11595 else
11596 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11597 bias, 0));
11599 return compare_loc_descriptor (op, op0, op1);
11602 /* Return location descriptor for {U,S}{MIN,MAX}. */
11604 static dw_loc_descr_ref
11605 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11606 enum machine_mode mem_mode)
11608 enum dwarf_location_atom op;
11609 dw_loc_descr_ref op0, op1, ret;
11610 dw_loc_descr_ref bra_node, drop_node;
11612 if (dwarf_strict
11613 && (GET_MODE_CLASS (mode) != MODE_INT
11614 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11615 return NULL;
11617 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11618 VAR_INIT_STATUS_INITIALIZED);
11619 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11620 VAR_INIT_STATUS_INITIALIZED);
11622 if (op0 == NULL || op1 == NULL)
11623 return NULL;
11625 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11626 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11627 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11628 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11630 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11632 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11633 add_loc_descr (&op0, int_loc_descriptor (mask));
11634 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11635 add_loc_descr (&op1, int_loc_descriptor (mask));
11636 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11638 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11640 HOST_WIDE_INT bias = 1;
11641 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11642 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11643 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11646 else if (GET_MODE_CLASS (mode) == MODE_INT
11647 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11649 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11650 add_loc_descr (&op0, int_loc_descriptor (shift));
11651 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11652 add_loc_descr (&op1, int_loc_descriptor (shift));
11653 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11655 else if (GET_MODE_CLASS (mode) == MODE_INT
11656 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11658 dw_die_ref type_die = base_type_for_mode (mode, 0);
11659 dw_loc_descr_ref cvt;
11660 if (type_die == NULL)
11661 return NULL;
11662 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11663 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11664 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11665 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11666 add_loc_descr (&op0, cvt);
11667 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11668 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11669 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11670 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11671 add_loc_descr (&op1, cvt);
11674 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11675 op = DW_OP_lt;
11676 else
11677 op = DW_OP_gt;
11678 ret = op0;
11679 add_loc_descr (&ret, op1);
11680 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11681 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11682 add_loc_descr (&ret, bra_node);
11683 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11684 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11685 add_loc_descr (&ret, drop_node);
11686 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11687 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11688 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11689 && GET_MODE_CLASS (mode) == MODE_INT
11690 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11691 ret = convert_descriptor_to_mode (mode, ret);
11692 return ret;
11695 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11696 but after converting arguments to type_die, afterwards
11697 convert back to unsigned. */
11699 static dw_loc_descr_ref
11700 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11701 enum machine_mode mode, enum machine_mode mem_mode)
11703 dw_loc_descr_ref cvt, op0, op1;
11705 if (type_die == NULL)
11706 return NULL;
11707 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11708 VAR_INIT_STATUS_INITIALIZED);
11709 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11710 VAR_INIT_STATUS_INITIALIZED);
11711 if (op0 == NULL || op1 == NULL)
11712 return NULL;
11713 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11714 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11715 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11716 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11717 add_loc_descr (&op0, cvt);
11718 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11719 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11720 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11721 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11722 add_loc_descr (&op1, cvt);
11723 add_loc_descr (&op0, op1);
11724 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11725 return convert_descriptor_to_mode (mode, op0);
11728 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11729 const0 is DW_OP_lit0 or corresponding typed constant,
11730 const1 is DW_OP_lit1 or corresponding typed constant
11731 and constMSB is constant with just the MSB bit set
11732 for the mode):
11733 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11734 L1: const0 DW_OP_swap
11735 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11736 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11737 L3: DW_OP_drop
11738 L4: DW_OP_nop
11740 CTZ is similar:
11741 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11742 L1: const0 DW_OP_swap
11743 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11744 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11745 L3: DW_OP_drop
11746 L4: DW_OP_nop
11748 FFS is similar:
11749 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11750 L1: const1 DW_OP_swap
11751 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11752 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11753 L3: DW_OP_drop
11754 L4: DW_OP_nop */
11756 static dw_loc_descr_ref
11757 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11758 enum machine_mode mem_mode)
11760 dw_loc_descr_ref op0, ret, tmp;
11761 HOST_WIDE_INT valv;
11762 dw_loc_descr_ref l1jump, l1label;
11763 dw_loc_descr_ref l2jump, l2label;
11764 dw_loc_descr_ref l3jump, l3label;
11765 dw_loc_descr_ref l4jump, l4label;
11766 rtx msb;
11768 if (GET_MODE_CLASS (mode) != MODE_INT
11769 || GET_MODE (XEXP (rtl, 0)) != mode
11770 || (GET_CODE (rtl) == CLZ
11771 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11772 return NULL;
11774 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11775 VAR_INIT_STATUS_INITIALIZED);
11776 if (op0 == NULL)
11777 return NULL;
11778 ret = op0;
11779 if (GET_CODE (rtl) == CLZ)
11781 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11782 valv = GET_MODE_BITSIZE (mode);
11784 else if (GET_CODE (rtl) == FFS)
11785 valv = 0;
11786 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11787 valv = GET_MODE_BITSIZE (mode);
11788 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11789 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11790 add_loc_descr (&ret, l1jump);
11791 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11792 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11793 VAR_INIT_STATUS_INITIALIZED);
11794 if (tmp == NULL)
11795 return NULL;
11796 add_loc_descr (&ret, tmp);
11797 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11798 add_loc_descr (&ret, l4jump);
11799 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11800 ? const1_rtx : const0_rtx,
11801 mode, mem_mode,
11802 VAR_INIT_STATUS_INITIALIZED);
11803 if (l1label == NULL)
11804 return NULL;
11805 add_loc_descr (&ret, l1label);
11806 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11807 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11808 add_loc_descr (&ret, l2label);
11809 if (GET_CODE (rtl) != CLZ)
11810 msb = const1_rtx;
11811 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11812 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11813 << (GET_MODE_BITSIZE (mode) - 1));
11814 else
11815 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11816 << (GET_MODE_BITSIZE (mode)
11817 - HOST_BITS_PER_WIDE_INT - 1), mode);
11818 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11819 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11820 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11821 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11822 else
11823 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11824 VAR_INIT_STATUS_INITIALIZED);
11825 if (tmp == NULL)
11826 return NULL;
11827 add_loc_descr (&ret, tmp);
11828 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11829 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11830 add_loc_descr (&ret, l3jump);
11831 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11832 VAR_INIT_STATUS_INITIALIZED);
11833 if (tmp == NULL)
11834 return NULL;
11835 add_loc_descr (&ret, tmp);
11836 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11837 ? DW_OP_shl : DW_OP_shr, 0, 0));
11838 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11839 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11840 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11841 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11842 add_loc_descr (&ret, l2jump);
11843 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11844 add_loc_descr (&ret, l3label);
11845 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11846 add_loc_descr (&ret, l4label);
11847 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11848 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11849 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11850 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11851 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11852 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11853 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11854 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11855 return ret;
11858 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11859 const1 is DW_OP_lit1 or corresponding typed constant):
11860 const0 DW_OP_swap
11861 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11862 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11863 L2: DW_OP_drop
11865 PARITY is similar:
11866 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11867 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11868 L2: DW_OP_drop */
11870 static dw_loc_descr_ref
11871 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11872 enum machine_mode mem_mode)
11874 dw_loc_descr_ref op0, ret, tmp;
11875 dw_loc_descr_ref l1jump, l1label;
11876 dw_loc_descr_ref l2jump, l2label;
11878 if (GET_MODE_CLASS (mode) != MODE_INT
11879 || GET_MODE (XEXP (rtl, 0)) != mode)
11880 return NULL;
11882 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11883 VAR_INIT_STATUS_INITIALIZED);
11884 if (op0 == NULL)
11885 return NULL;
11886 ret = op0;
11887 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11888 VAR_INIT_STATUS_INITIALIZED);
11889 if (tmp == NULL)
11890 return NULL;
11891 add_loc_descr (&ret, tmp);
11892 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11893 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11894 add_loc_descr (&ret, l1label);
11895 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11896 add_loc_descr (&ret, l2jump);
11897 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11898 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11899 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11900 VAR_INIT_STATUS_INITIALIZED);
11901 if (tmp == NULL)
11902 return NULL;
11903 add_loc_descr (&ret, tmp);
11904 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11905 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11906 ? DW_OP_plus : DW_OP_xor, 0, 0));
11907 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11908 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11909 VAR_INIT_STATUS_INITIALIZED);
11910 add_loc_descr (&ret, tmp);
11911 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11912 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11913 add_loc_descr (&ret, l1jump);
11914 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11915 add_loc_descr (&ret, l2label);
11916 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11917 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11918 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11919 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11920 return ret;
11923 /* BSWAP (constS is initial shift count, either 56 or 24):
11924 constS const0
11925 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11926 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11927 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11928 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11929 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11931 static dw_loc_descr_ref
11932 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11933 enum machine_mode mem_mode)
11935 dw_loc_descr_ref op0, ret, tmp;
11936 dw_loc_descr_ref l1jump, l1label;
11937 dw_loc_descr_ref l2jump, l2label;
11939 if (GET_MODE_CLASS (mode) != MODE_INT
11940 || BITS_PER_UNIT != 8
11941 || (GET_MODE_BITSIZE (mode) != 32
11942 && GET_MODE_BITSIZE (mode) != 64))
11943 return NULL;
11945 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11946 VAR_INIT_STATUS_INITIALIZED);
11947 if (op0 == NULL)
11948 return NULL;
11950 ret = op0;
11951 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11952 mode, mem_mode,
11953 VAR_INIT_STATUS_INITIALIZED);
11954 if (tmp == NULL)
11955 return NULL;
11956 add_loc_descr (&ret, tmp);
11957 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11958 VAR_INIT_STATUS_INITIALIZED);
11959 if (tmp == NULL)
11960 return NULL;
11961 add_loc_descr (&ret, tmp);
11962 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11963 add_loc_descr (&ret, l1label);
11964 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11965 mode, mem_mode,
11966 VAR_INIT_STATUS_INITIALIZED);
11967 add_loc_descr (&ret, tmp);
11968 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11969 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11970 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11971 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11972 VAR_INIT_STATUS_INITIALIZED);
11973 if (tmp == NULL)
11974 return NULL;
11975 add_loc_descr (&ret, tmp);
11976 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11977 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11978 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11979 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11980 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11981 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11982 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11983 VAR_INIT_STATUS_INITIALIZED);
11984 add_loc_descr (&ret, tmp);
11985 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11986 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11987 add_loc_descr (&ret, l2jump);
11988 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11989 VAR_INIT_STATUS_INITIALIZED);
11990 add_loc_descr (&ret, tmp);
11991 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11992 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11993 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11994 add_loc_descr (&ret, l1jump);
11995 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11996 add_loc_descr (&ret, l2label);
11997 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11998 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11999 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12000 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12001 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12002 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12003 return ret;
12006 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12007 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12008 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12009 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12011 ROTATERT is similar:
12012 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12013 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12014 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12016 static dw_loc_descr_ref
12017 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12018 enum machine_mode mem_mode)
12020 rtx rtlop1 = XEXP (rtl, 1);
12021 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12022 int i;
12024 if (GET_MODE_CLASS (mode) != MODE_INT)
12025 return NULL;
12027 if (GET_MODE (rtlop1) != VOIDmode
12028 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12029 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12030 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12031 VAR_INIT_STATUS_INITIALIZED);
12032 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12033 VAR_INIT_STATUS_INITIALIZED);
12034 if (op0 == NULL || op1 == NULL)
12035 return NULL;
12036 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12037 for (i = 0; i < 2; i++)
12039 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12040 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12041 mode, mem_mode,
12042 VAR_INIT_STATUS_INITIALIZED);
12043 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12044 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12045 ? DW_OP_const4u
12046 : HOST_BITS_PER_WIDE_INT == 64
12047 ? DW_OP_const8u : DW_OP_constu,
12048 GET_MODE_MASK (mode), 0);
12049 else
12050 mask[i] = NULL;
12051 if (mask[i] == NULL)
12052 return NULL;
12053 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12055 ret = op0;
12056 add_loc_descr (&ret, op1);
12057 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12058 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12059 if (GET_CODE (rtl) == ROTATERT)
12061 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12062 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12063 GET_MODE_BITSIZE (mode), 0));
12065 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12066 if (mask[0] != NULL)
12067 add_loc_descr (&ret, mask[0]);
12068 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12069 if (mask[1] != NULL)
12071 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12072 add_loc_descr (&ret, mask[1]);
12073 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12075 if (GET_CODE (rtl) == ROTATE)
12077 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12078 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12079 GET_MODE_BITSIZE (mode), 0));
12081 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12082 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12083 return ret;
12086 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12087 for DEBUG_PARAMETER_REF RTL. */
12089 static dw_loc_descr_ref
12090 parameter_ref_descriptor (rtx rtl)
12092 dw_loc_descr_ref ret;
12093 dw_die_ref ref;
12095 if (dwarf_strict)
12096 return NULL;
12097 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12098 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12099 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12100 if (ref)
12102 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12103 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12104 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12106 else
12108 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12109 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12111 return ret;
12114 /* The following routine converts the RTL for a variable or parameter
12115 (resident in memory) into an equivalent Dwarf representation of a
12116 mechanism for getting the address of that same variable onto the top of a
12117 hypothetical "address evaluation" stack.
12119 When creating memory location descriptors, we are effectively transforming
12120 the RTL for a memory-resident object into its Dwarf postfix expression
12121 equivalent. This routine recursively descends an RTL tree, turning
12122 it into Dwarf postfix code as it goes.
12124 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12126 MEM_MODE is the mode of the memory reference, needed to handle some
12127 autoincrement addressing modes.
12129 Return 0 if we can't represent the location. */
12131 dw_loc_descr_ref
12132 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12133 enum machine_mode mem_mode,
12134 enum var_init_status initialized)
12136 dw_loc_descr_ref mem_loc_result = NULL;
12137 enum dwarf_location_atom op;
12138 dw_loc_descr_ref op0, op1;
12139 rtx inner = NULL_RTX;
12141 if (mode == VOIDmode)
12142 mode = GET_MODE (rtl);
12144 /* Note that for a dynamically sized array, the location we will generate a
12145 description of here will be the lowest numbered location which is
12146 actually within the array. That's *not* necessarily the same as the
12147 zeroth element of the array. */
12149 rtl = targetm.delegitimize_address (rtl);
12151 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12152 return NULL;
12154 switch (GET_CODE (rtl))
12156 case POST_INC:
12157 case POST_DEC:
12158 case POST_MODIFY:
12159 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12161 case SUBREG:
12162 /* The case of a subreg may arise when we have a local (register)
12163 variable or a formal (register) parameter which doesn't quite fill
12164 up an entire register. For now, just assume that it is
12165 legitimate to make the Dwarf info refer to the whole register which
12166 contains the given subreg. */
12167 if (!subreg_lowpart_p (rtl))
12168 break;
12169 inner = SUBREG_REG (rtl);
12170 case TRUNCATE:
12171 if (inner == NULL_RTX)
12172 inner = XEXP (rtl, 0);
12173 if (GET_MODE_CLASS (mode) == MODE_INT
12174 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12175 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12176 #ifdef POINTERS_EXTEND_UNSIGNED
12177 || (mode == Pmode && mem_mode != VOIDmode)
12178 #endif
12180 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12182 mem_loc_result = mem_loc_descriptor (inner,
12183 GET_MODE (inner),
12184 mem_mode, initialized);
12185 break;
12187 if (dwarf_strict)
12188 break;
12189 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12190 break;
12191 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12192 && (GET_MODE_CLASS (mode) != MODE_INT
12193 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12194 break;
12195 else
12197 dw_die_ref type_die;
12198 dw_loc_descr_ref cvt;
12200 mem_loc_result = mem_loc_descriptor (inner,
12201 GET_MODE (inner),
12202 mem_mode, initialized);
12203 if (mem_loc_result == NULL)
12204 break;
12205 type_die = base_type_for_mode (mode,
12206 GET_MODE_CLASS (mode) == MODE_INT);
12207 if (type_die == NULL)
12209 mem_loc_result = NULL;
12210 break;
12212 if (GET_MODE_SIZE (mode)
12213 != GET_MODE_SIZE (GET_MODE (inner)))
12214 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12215 else
12216 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12217 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12218 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12219 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12220 add_loc_descr (&mem_loc_result, cvt);
12222 break;
12224 case REG:
12225 if (GET_MODE_CLASS (mode) != MODE_INT
12226 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12227 && rtl != arg_pointer_rtx
12228 && rtl != frame_pointer_rtx
12229 #ifdef POINTERS_EXTEND_UNSIGNED
12230 && (mode != Pmode || mem_mode == VOIDmode)
12231 #endif
12234 dw_die_ref type_die;
12235 unsigned int dbx_regnum;
12237 if (dwarf_strict)
12238 break;
12239 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12240 break;
12241 type_die = base_type_for_mode (mode,
12242 GET_MODE_CLASS (mode) == MODE_INT);
12243 if (type_die == NULL)
12244 break;
12246 dbx_regnum = dbx_reg_number (rtl);
12247 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12248 break;
12249 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12250 dbx_regnum, 0);
12251 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12252 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12253 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12254 break;
12256 /* Whenever a register number forms a part of the description of the
12257 method for calculating the (dynamic) address of a memory resident
12258 object, DWARF rules require the register number be referred to as
12259 a "base register". This distinction is not based in any way upon
12260 what category of register the hardware believes the given register
12261 belongs to. This is strictly DWARF terminology we're dealing with
12262 here. Note that in cases where the location of a memory-resident
12263 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12264 OP_CONST (0)) the actual DWARF location descriptor that we generate
12265 may just be OP_BASEREG (basereg). This may look deceptively like
12266 the object in question was allocated to a register (rather than in
12267 memory) so DWARF consumers need to be aware of the subtle
12268 distinction between OP_REG and OP_BASEREG. */
12269 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12270 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12271 else if (stack_realign_drap
12272 && crtl->drap_reg
12273 && crtl->args.internal_arg_pointer == rtl
12274 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12276 /* If RTL is internal_arg_pointer, which has been optimized
12277 out, use DRAP instead. */
12278 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12279 VAR_INIT_STATUS_INITIALIZED);
12281 break;
12283 case SIGN_EXTEND:
12284 case ZERO_EXTEND:
12285 if (GET_MODE_CLASS (mode) != MODE_INT)
12286 break;
12287 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12288 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12289 if (op0 == 0)
12290 break;
12291 else if (GET_CODE (rtl) == ZERO_EXTEND
12292 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12293 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12294 < HOST_BITS_PER_WIDE_INT
12295 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12296 to expand zero extend as two shifts instead of
12297 masking. */
12298 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12300 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12301 mem_loc_result = op0;
12302 add_loc_descr (&mem_loc_result,
12303 int_loc_descriptor (GET_MODE_MASK (imode)));
12304 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12306 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12308 int shift = DWARF2_ADDR_SIZE
12309 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12310 shift *= BITS_PER_UNIT;
12311 if (GET_CODE (rtl) == SIGN_EXTEND)
12312 op = DW_OP_shra;
12313 else
12314 op = DW_OP_shr;
12315 mem_loc_result = op0;
12316 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12317 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12318 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12319 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12321 else if (!dwarf_strict)
12323 dw_die_ref type_die1, type_die2;
12324 dw_loc_descr_ref cvt;
12326 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12327 GET_CODE (rtl) == ZERO_EXTEND);
12328 if (type_die1 == NULL)
12329 break;
12330 type_die2 = base_type_for_mode (mode, 1);
12331 if (type_die2 == NULL)
12332 break;
12333 mem_loc_result = op0;
12334 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12335 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12336 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12337 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12338 add_loc_descr (&mem_loc_result, cvt);
12339 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12340 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12341 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12342 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12343 add_loc_descr (&mem_loc_result, cvt);
12345 break;
12347 case MEM:
12349 rtx new_rtl = avoid_constant_pool_reference (rtl);
12350 if (new_rtl != rtl)
12352 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12353 initialized);
12354 if (mem_loc_result != NULL)
12355 return mem_loc_result;
12358 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12359 get_address_mode (rtl), mode,
12360 VAR_INIT_STATUS_INITIALIZED);
12361 if (mem_loc_result == NULL)
12362 mem_loc_result = tls_mem_loc_descriptor (rtl);
12363 if (mem_loc_result != NULL)
12365 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12366 || GET_MODE_CLASS (mode) != MODE_INT)
12368 dw_die_ref type_die;
12369 dw_loc_descr_ref deref;
12371 if (dwarf_strict)
12372 return NULL;
12373 type_die
12374 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12375 if (type_die == NULL)
12376 return NULL;
12377 deref = new_loc_descr (DW_OP_GNU_deref_type,
12378 GET_MODE_SIZE (mode), 0);
12379 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12380 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12381 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12382 add_loc_descr (&mem_loc_result, deref);
12384 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12385 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12386 else
12387 add_loc_descr (&mem_loc_result,
12388 new_loc_descr (DW_OP_deref_size,
12389 GET_MODE_SIZE (mode), 0));
12391 break;
12393 case LO_SUM:
12394 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12396 case LABEL_REF:
12397 /* Some ports can transform a symbol ref into a label ref, because
12398 the symbol ref is too far away and has to be dumped into a constant
12399 pool. */
12400 case CONST:
12401 case SYMBOL_REF:
12402 if (GET_MODE_CLASS (mode) != MODE_INT
12403 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12404 #ifdef POINTERS_EXTEND_UNSIGNED
12405 && (mode != Pmode || mem_mode == VOIDmode)
12406 #endif
12408 break;
12409 if (GET_CODE (rtl) == SYMBOL_REF
12410 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12412 dw_loc_descr_ref temp;
12414 /* If this is not defined, we have no way to emit the data. */
12415 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12416 break;
12418 temp = new_addr_loc_descr (rtl, dtprel_true);
12420 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12421 add_loc_descr (&mem_loc_result, temp);
12423 break;
12426 if (!const_ok_for_output (rtl))
12427 break;
12429 symref:
12430 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12431 vec_safe_push (used_rtx_array, rtl);
12432 break;
12434 case CONCAT:
12435 case CONCATN:
12436 case VAR_LOCATION:
12437 case DEBUG_IMPLICIT_PTR:
12438 expansion_failed (NULL_TREE, rtl,
12439 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12440 return 0;
12442 case ENTRY_VALUE:
12443 if (dwarf_strict)
12444 return NULL;
12445 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12447 if (GET_MODE_CLASS (mode) != MODE_INT
12448 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12449 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12450 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12451 else
12453 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12454 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12455 return NULL;
12456 op0 = one_reg_loc_descriptor (dbx_regnum,
12457 VAR_INIT_STATUS_INITIALIZED);
12460 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12461 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12463 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12464 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12465 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12466 return NULL;
12468 else
12469 gcc_unreachable ();
12470 if (op0 == NULL)
12471 return NULL;
12472 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12473 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12474 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12475 break;
12477 case DEBUG_PARAMETER_REF:
12478 mem_loc_result = parameter_ref_descriptor (rtl);
12479 break;
12481 case PRE_MODIFY:
12482 /* Extract the PLUS expression nested inside and fall into
12483 PLUS code below. */
12484 rtl = XEXP (rtl, 1);
12485 goto plus;
12487 case PRE_INC:
12488 case PRE_DEC:
12489 /* Turn these into a PLUS expression and fall into the PLUS code
12490 below. */
12491 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12492 gen_int_mode (GET_CODE (rtl) == PRE_INC
12493 ? GET_MODE_UNIT_SIZE (mem_mode)
12494 : -GET_MODE_UNIT_SIZE (mem_mode),
12495 mode));
12497 /* ... fall through ... */
12499 case PLUS:
12500 plus:
12501 if (is_based_loc (rtl)
12502 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12503 || XEXP (rtl, 0) == arg_pointer_rtx
12504 || XEXP (rtl, 0) == frame_pointer_rtx)
12505 && GET_MODE_CLASS (mode) == MODE_INT)
12506 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12507 INTVAL (XEXP (rtl, 1)),
12508 VAR_INIT_STATUS_INITIALIZED);
12509 else
12511 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12512 VAR_INIT_STATUS_INITIALIZED);
12513 if (mem_loc_result == 0)
12514 break;
12516 if (CONST_INT_P (XEXP (rtl, 1))
12517 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12518 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12519 else
12521 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12522 VAR_INIT_STATUS_INITIALIZED);
12523 if (op1 == 0)
12524 break;
12525 add_loc_descr (&mem_loc_result, op1);
12526 add_loc_descr (&mem_loc_result,
12527 new_loc_descr (DW_OP_plus, 0, 0));
12530 break;
12532 /* If a pseudo-reg is optimized away, it is possible for it to
12533 be replaced with a MEM containing a multiply or shift. */
12534 case MINUS:
12535 op = DW_OP_minus;
12536 goto do_binop;
12538 case MULT:
12539 op = DW_OP_mul;
12540 goto do_binop;
12542 case DIV:
12543 if (!dwarf_strict
12544 && GET_MODE_CLASS (mode) == MODE_INT
12545 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12547 mem_loc_result = typed_binop (DW_OP_div, rtl,
12548 base_type_for_mode (mode, 0),
12549 mode, mem_mode);
12550 break;
12552 op = DW_OP_div;
12553 goto do_binop;
12555 case UMOD:
12556 op = DW_OP_mod;
12557 goto do_binop;
12559 case ASHIFT:
12560 op = DW_OP_shl;
12561 goto do_shift;
12563 case ASHIFTRT:
12564 op = DW_OP_shra;
12565 goto do_shift;
12567 case LSHIFTRT:
12568 op = DW_OP_shr;
12569 goto do_shift;
12571 do_shift:
12572 if (GET_MODE_CLASS (mode) != MODE_INT)
12573 break;
12574 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12575 VAR_INIT_STATUS_INITIALIZED);
12577 rtx rtlop1 = XEXP (rtl, 1);
12578 if (GET_MODE (rtlop1) != VOIDmode
12579 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12580 < GET_MODE_BITSIZE (mode))
12581 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12582 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12583 VAR_INIT_STATUS_INITIALIZED);
12586 if (op0 == 0 || op1 == 0)
12587 break;
12589 mem_loc_result = op0;
12590 add_loc_descr (&mem_loc_result, op1);
12591 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12592 break;
12594 case AND:
12595 op = DW_OP_and;
12596 goto do_binop;
12598 case IOR:
12599 op = DW_OP_or;
12600 goto do_binop;
12602 case XOR:
12603 op = DW_OP_xor;
12604 goto do_binop;
12606 do_binop:
12607 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12608 VAR_INIT_STATUS_INITIALIZED);
12609 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12610 VAR_INIT_STATUS_INITIALIZED);
12612 if (op0 == 0 || op1 == 0)
12613 break;
12615 mem_loc_result = op0;
12616 add_loc_descr (&mem_loc_result, op1);
12617 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12618 break;
12620 case MOD:
12621 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12623 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12624 base_type_for_mode (mode, 0),
12625 mode, mem_mode);
12626 break;
12629 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12630 VAR_INIT_STATUS_INITIALIZED);
12631 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12632 VAR_INIT_STATUS_INITIALIZED);
12634 if (op0 == 0 || op1 == 0)
12635 break;
12637 mem_loc_result = op0;
12638 add_loc_descr (&mem_loc_result, op1);
12639 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12640 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12641 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12642 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12643 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12644 break;
12646 case UDIV:
12647 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12649 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12651 op = DW_OP_div;
12652 goto do_binop;
12654 mem_loc_result = typed_binop (DW_OP_div, rtl,
12655 base_type_for_mode (mode, 1),
12656 mode, mem_mode);
12658 break;
12660 case NOT:
12661 op = DW_OP_not;
12662 goto do_unop;
12664 case ABS:
12665 op = DW_OP_abs;
12666 goto do_unop;
12668 case NEG:
12669 op = DW_OP_neg;
12670 goto do_unop;
12672 do_unop:
12673 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12674 VAR_INIT_STATUS_INITIALIZED);
12676 if (op0 == 0)
12677 break;
12679 mem_loc_result = op0;
12680 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12681 break;
12683 case CONST_INT:
12684 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12685 #ifdef POINTERS_EXTEND_UNSIGNED
12686 || (mode == Pmode
12687 && mem_mode != VOIDmode
12688 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12689 #endif
12692 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12693 break;
12695 if (!dwarf_strict
12696 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12697 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12699 dw_die_ref type_die = base_type_for_mode (mode, 1);
12700 enum machine_mode amode;
12701 if (type_die == NULL)
12702 return NULL;
12703 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12704 MODE_INT, 0);
12705 if (INTVAL (rtl) >= 0
12706 && amode != BLKmode
12707 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12708 /* const DW_OP_GNU_convert <XXX> vs.
12709 DW_OP_GNU_const_type <XXX, 1, const>. */
12710 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12711 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12713 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12714 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12715 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12716 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12717 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12718 add_loc_descr (&mem_loc_result, op0);
12719 return mem_loc_result;
12721 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12722 INTVAL (rtl));
12723 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12724 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12725 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12726 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12727 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12728 else
12730 mem_loc_result->dw_loc_oprnd2.val_class
12731 = dw_val_class_const_double;
12732 mem_loc_result->dw_loc_oprnd2.v.val_double
12733 = double_int::from_shwi (INTVAL (rtl));
12736 break;
12738 case CONST_DOUBLE:
12739 if (!dwarf_strict)
12741 dw_die_ref type_die;
12743 /* Note that a CONST_DOUBLE rtx could represent either an integer
12744 or a floating-point constant. A CONST_DOUBLE is used whenever
12745 the constant requires more than one word in order to be
12746 adequately represented. We output CONST_DOUBLEs as blocks. */
12747 if (mode == VOIDmode
12748 || (GET_MODE (rtl) == VOIDmode
12749 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12750 break;
12751 type_die = base_type_for_mode (mode,
12752 GET_MODE_CLASS (mode) == MODE_INT);
12753 if (type_die == NULL)
12754 return NULL;
12755 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12756 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12757 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12758 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12759 if (SCALAR_FLOAT_MODE_P (mode))
12761 unsigned int length = GET_MODE_SIZE (mode);
12762 unsigned char *array
12763 = (unsigned char*) ggc_alloc_atomic (length);
12765 insert_float (rtl, array);
12766 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12767 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12768 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12769 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12771 else
12773 mem_loc_result->dw_loc_oprnd2.val_class
12774 = dw_val_class_const_double;
12775 mem_loc_result->dw_loc_oprnd2.v.val_double
12776 = rtx_to_double_int (rtl);
12779 break;
12781 case EQ:
12782 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12783 break;
12785 case GE:
12786 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12787 break;
12789 case GT:
12790 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12791 break;
12793 case LE:
12794 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12795 break;
12797 case LT:
12798 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12799 break;
12801 case NE:
12802 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12803 break;
12805 case GEU:
12806 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12807 break;
12809 case GTU:
12810 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12811 break;
12813 case LEU:
12814 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12815 break;
12817 case LTU:
12818 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12819 break;
12821 case UMIN:
12822 case UMAX:
12823 if (GET_MODE_CLASS (mode) != MODE_INT)
12824 break;
12825 /* FALLTHRU */
12826 case SMIN:
12827 case SMAX:
12828 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12829 break;
12831 case ZERO_EXTRACT:
12832 case SIGN_EXTRACT:
12833 if (CONST_INT_P (XEXP (rtl, 1))
12834 && CONST_INT_P (XEXP (rtl, 2))
12835 && ((unsigned) INTVAL (XEXP (rtl, 1))
12836 + (unsigned) INTVAL (XEXP (rtl, 2))
12837 <= GET_MODE_BITSIZE (mode))
12838 && GET_MODE_CLASS (mode) == MODE_INT
12839 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12840 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12842 int shift, size;
12843 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12844 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12845 if (op0 == 0)
12846 break;
12847 if (GET_CODE (rtl) == SIGN_EXTRACT)
12848 op = DW_OP_shra;
12849 else
12850 op = DW_OP_shr;
12851 mem_loc_result = op0;
12852 size = INTVAL (XEXP (rtl, 1));
12853 shift = INTVAL (XEXP (rtl, 2));
12854 if (BITS_BIG_ENDIAN)
12855 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12856 - shift - size;
12857 if (shift + size != (int) DWARF2_ADDR_SIZE)
12859 add_loc_descr (&mem_loc_result,
12860 int_loc_descriptor (DWARF2_ADDR_SIZE
12861 - shift - size));
12862 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12864 if (size != (int) DWARF2_ADDR_SIZE)
12866 add_loc_descr (&mem_loc_result,
12867 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12868 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12871 break;
12873 case IF_THEN_ELSE:
12875 dw_loc_descr_ref op2, bra_node, drop_node;
12876 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12877 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12878 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12879 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12880 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12881 VAR_INIT_STATUS_INITIALIZED);
12882 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12883 VAR_INIT_STATUS_INITIALIZED);
12884 if (op0 == NULL || op1 == NULL || op2 == NULL)
12885 break;
12887 mem_loc_result = op1;
12888 add_loc_descr (&mem_loc_result, op2);
12889 add_loc_descr (&mem_loc_result, op0);
12890 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12891 add_loc_descr (&mem_loc_result, bra_node);
12892 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12893 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12894 add_loc_descr (&mem_loc_result, drop_node);
12895 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12896 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12898 break;
12900 case FLOAT_EXTEND:
12901 case FLOAT_TRUNCATE:
12902 case FLOAT:
12903 case UNSIGNED_FLOAT:
12904 case FIX:
12905 case UNSIGNED_FIX:
12906 if (!dwarf_strict)
12908 dw_die_ref type_die;
12909 dw_loc_descr_ref cvt;
12911 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12912 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12913 if (op0 == NULL)
12914 break;
12915 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12916 && (GET_CODE (rtl) == FLOAT
12917 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12918 <= DWARF2_ADDR_SIZE))
12920 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12921 GET_CODE (rtl) == UNSIGNED_FLOAT);
12922 if (type_die == NULL)
12923 break;
12924 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12925 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12926 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12927 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12928 add_loc_descr (&op0, cvt);
12930 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12931 if (type_die == NULL)
12932 break;
12933 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12934 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12935 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12936 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12937 add_loc_descr (&op0, cvt);
12938 if (GET_MODE_CLASS (mode) == MODE_INT
12939 && (GET_CODE (rtl) == FIX
12940 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12942 op0 = convert_descriptor_to_mode (mode, op0);
12943 if (op0 == NULL)
12944 break;
12946 mem_loc_result = op0;
12948 break;
12950 case CLZ:
12951 case CTZ:
12952 case FFS:
12953 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12954 break;
12956 case POPCOUNT:
12957 case PARITY:
12958 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12959 break;
12961 case BSWAP:
12962 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12963 break;
12965 case ROTATE:
12966 case ROTATERT:
12967 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12968 break;
12970 case COMPARE:
12971 /* In theory, we could implement the above. */
12972 /* DWARF cannot represent the unsigned compare operations
12973 natively. */
12974 case SS_MULT:
12975 case US_MULT:
12976 case SS_DIV:
12977 case US_DIV:
12978 case SS_PLUS:
12979 case US_PLUS:
12980 case SS_MINUS:
12981 case US_MINUS:
12982 case SS_NEG:
12983 case US_NEG:
12984 case SS_ABS:
12985 case SS_ASHIFT:
12986 case US_ASHIFT:
12987 case SS_TRUNCATE:
12988 case US_TRUNCATE:
12989 case UNORDERED:
12990 case ORDERED:
12991 case UNEQ:
12992 case UNGE:
12993 case UNGT:
12994 case UNLE:
12995 case UNLT:
12996 case LTGT:
12997 case FRACT_CONVERT:
12998 case UNSIGNED_FRACT_CONVERT:
12999 case SAT_FRACT:
13000 case UNSIGNED_SAT_FRACT:
13001 case SQRT:
13002 case ASM_OPERANDS:
13003 case VEC_MERGE:
13004 case VEC_SELECT:
13005 case VEC_CONCAT:
13006 case VEC_DUPLICATE:
13007 case UNSPEC:
13008 case HIGH:
13009 case FMA:
13010 case STRICT_LOW_PART:
13011 case CONST_VECTOR:
13012 case CONST_FIXED:
13013 case CLRSB:
13014 case CLOBBER:
13015 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13016 can't express it in the debug info. This can happen e.g. with some
13017 TLS UNSPECs. */
13018 break;
13020 case CONST_STRING:
13021 resolve_one_addr (&rtl, NULL);
13022 goto symref;
13024 default:
13025 #ifdef ENABLE_CHECKING
13026 print_rtl (stderr, rtl);
13027 gcc_unreachable ();
13028 #else
13029 break;
13030 #endif
13033 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13034 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13036 return mem_loc_result;
13039 /* Return a descriptor that describes the concatenation of two locations.
13040 This is typically a complex variable. */
13042 static dw_loc_descr_ref
13043 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13045 dw_loc_descr_ref cc_loc_result = NULL;
13046 dw_loc_descr_ref x0_ref
13047 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13048 dw_loc_descr_ref x1_ref
13049 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13051 if (x0_ref == 0 || x1_ref == 0)
13052 return 0;
13054 cc_loc_result = x0_ref;
13055 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13057 add_loc_descr (&cc_loc_result, x1_ref);
13058 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13060 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13061 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13063 return cc_loc_result;
13066 /* Return a descriptor that describes the concatenation of N
13067 locations. */
13069 static dw_loc_descr_ref
13070 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13072 unsigned int i;
13073 dw_loc_descr_ref cc_loc_result = NULL;
13074 unsigned int n = XVECLEN (concatn, 0);
13076 for (i = 0; i < n; ++i)
13078 dw_loc_descr_ref ref;
13079 rtx x = XVECEXP (concatn, 0, i);
13081 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13082 if (ref == NULL)
13083 return NULL;
13085 add_loc_descr (&cc_loc_result, ref);
13086 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13089 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13090 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13092 return cc_loc_result;
13095 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13096 for DEBUG_IMPLICIT_PTR RTL. */
13098 static dw_loc_descr_ref
13099 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13101 dw_loc_descr_ref ret;
13102 dw_die_ref ref;
13104 if (dwarf_strict)
13105 return NULL;
13106 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13107 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13108 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13109 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13110 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13111 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13112 if (ref)
13114 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13115 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13116 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13118 else
13120 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13121 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13123 return ret;
13126 /* Output a proper Dwarf location descriptor for a variable or parameter
13127 which is either allocated in a register or in a memory location. For a
13128 register, we just generate an OP_REG and the register number. For a
13129 memory location we provide a Dwarf postfix expression describing how to
13130 generate the (dynamic) address of the object onto the address stack.
13132 MODE is mode of the decl if this loc_descriptor is going to be used in
13133 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13134 allowed, VOIDmode otherwise.
13136 If we don't know how to describe it, return 0. */
13138 static dw_loc_descr_ref
13139 loc_descriptor (rtx rtl, enum machine_mode mode,
13140 enum var_init_status initialized)
13142 dw_loc_descr_ref loc_result = NULL;
13144 switch (GET_CODE (rtl))
13146 case SUBREG:
13147 /* The case of a subreg may arise when we have a local (register)
13148 variable or a formal (register) parameter which doesn't quite fill
13149 up an entire register. For now, just assume that it is
13150 legitimate to make the Dwarf info refer to the whole register which
13151 contains the given subreg. */
13152 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13153 loc_result = loc_descriptor (SUBREG_REG (rtl),
13154 GET_MODE (SUBREG_REG (rtl)), initialized);
13155 else
13156 goto do_default;
13157 break;
13159 case REG:
13160 loc_result = reg_loc_descriptor (rtl, initialized);
13161 break;
13163 case MEM:
13164 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13165 GET_MODE (rtl), initialized);
13166 if (loc_result == NULL)
13167 loc_result = tls_mem_loc_descriptor (rtl);
13168 if (loc_result == NULL)
13170 rtx new_rtl = avoid_constant_pool_reference (rtl);
13171 if (new_rtl != rtl)
13172 loc_result = loc_descriptor (new_rtl, mode, initialized);
13174 break;
13176 case CONCAT:
13177 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13178 initialized);
13179 break;
13181 case CONCATN:
13182 loc_result = concatn_loc_descriptor (rtl, initialized);
13183 break;
13185 case VAR_LOCATION:
13186 /* Single part. */
13187 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13189 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13190 if (GET_CODE (loc) == EXPR_LIST)
13191 loc = XEXP (loc, 0);
13192 loc_result = loc_descriptor (loc, mode, initialized);
13193 break;
13196 rtl = XEXP (rtl, 1);
13197 /* FALLTHRU */
13199 case PARALLEL:
13201 rtvec par_elems = XVEC (rtl, 0);
13202 int num_elem = GET_NUM_ELEM (par_elems);
13203 enum machine_mode mode;
13204 int i;
13206 /* Create the first one, so we have something to add to. */
13207 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13208 VOIDmode, initialized);
13209 if (loc_result == NULL)
13210 return NULL;
13211 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13212 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13213 for (i = 1; i < num_elem; i++)
13215 dw_loc_descr_ref temp;
13217 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13218 VOIDmode, initialized);
13219 if (temp == NULL)
13220 return NULL;
13221 add_loc_descr (&loc_result, temp);
13222 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13223 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13226 break;
13228 case CONST_INT:
13229 if (mode != VOIDmode && mode != BLKmode)
13230 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13231 INTVAL (rtl));
13232 break;
13234 case CONST_DOUBLE:
13235 if (mode == VOIDmode)
13236 mode = GET_MODE (rtl);
13238 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13240 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13242 /* Note that a CONST_DOUBLE rtx could represent either an integer
13243 or a floating-point constant. A CONST_DOUBLE is used whenever
13244 the constant requires more than one word in order to be
13245 adequately represented. We output CONST_DOUBLEs as blocks. */
13246 loc_result = new_loc_descr (DW_OP_implicit_value,
13247 GET_MODE_SIZE (mode), 0);
13248 if (SCALAR_FLOAT_MODE_P (mode))
13250 unsigned int length = GET_MODE_SIZE (mode);
13251 unsigned char *array
13252 = (unsigned char*) ggc_alloc_atomic (length);
13254 insert_float (rtl, array);
13255 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13256 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13257 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13258 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13260 else
13262 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13263 loc_result->dw_loc_oprnd2.v.val_double
13264 = rtx_to_double_int (rtl);
13267 break;
13269 case CONST_VECTOR:
13270 if (mode == VOIDmode)
13271 mode = GET_MODE (rtl);
13273 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13275 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13276 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13277 unsigned char *array = (unsigned char *)
13278 ggc_alloc_atomic (length * elt_size);
13279 unsigned int i;
13280 unsigned char *p;
13282 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13283 switch (GET_MODE_CLASS (mode))
13285 case MODE_VECTOR_INT:
13286 for (i = 0, p = array; i < length; i++, p += elt_size)
13288 rtx elt = CONST_VECTOR_ELT (rtl, i);
13289 double_int val = rtx_to_double_int (elt);
13291 if (elt_size <= sizeof (HOST_WIDE_INT))
13292 insert_int (val.to_shwi (), elt_size, p);
13293 else
13295 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13296 insert_double (val, p);
13299 break;
13301 case MODE_VECTOR_FLOAT:
13302 for (i = 0, p = array; i < length; i++, p += elt_size)
13304 rtx elt = CONST_VECTOR_ELT (rtl, i);
13305 insert_float (elt, p);
13307 break;
13309 default:
13310 gcc_unreachable ();
13313 loc_result = new_loc_descr (DW_OP_implicit_value,
13314 length * elt_size, 0);
13315 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13316 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13317 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13318 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13320 break;
13322 case CONST:
13323 if (mode == VOIDmode
13324 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13325 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13326 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13328 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13329 break;
13331 /* FALLTHROUGH */
13332 case SYMBOL_REF:
13333 if (!const_ok_for_output (rtl))
13334 break;
13335 case LABEL_REF:
13336 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13337 && (dwarf_version >= 4 || !dwarf_strict))
13339 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13340 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13341 vec_safe_push (used_rtx_array, rtl);
13343 break;
13345 case DEBUG_IMPLICIT_PTR:
13346 loc_result = implicit_ptr_descriptor (rtl, 0);
13347 break;
13349 case PLUS:
13350 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13351 && CONST_INT_P (XEXP (rtl, 1)))
13353 loc_result
13354 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13355 break;
13357 /* FALLTHRU */
13358 do_default:
13359 default:
13360 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13361 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13362 && dwarf_version >= 4)
13363 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13365 /* Value expression. */
13366 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13367 if (loc_result)
13368 add_loc_descr (&loc_result,
13369 new_loc_descr (DW_OP_stack_value, 0, 0));
13371 break;
13374 return loc_result;
13377 /* We need to figure out what section we should use as the base for the
13378 address ranges where a given location is valid.
13379 1. If this particular DECL has a section associated with it, use that.
13380 2. If this function has a section associated with it, use that.
13381 3. Otherwise, use the text section.
13382 XXX: If you split a variable across multiple sections, we won't notice. */
13384 static const char *
13385 secname_for_decl (const_tree decl)
13387 const char *secname;
13389 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13391 tree sectree = DECL_SECTION_NAME (decl);
13392 secname = TREE_STRING_POINTER (sectree);
13394 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13396 tree sectree = DECL_SECTION_NAME (current_function_decl);
13397 secname = TREE_STRING_POINTER (sectree);
13399 else if (cfun && in_cold_section_p)
13400 secname = crtl->subsections.cold_section_label;
13401 else
13402 secname = text_section_label;
13404 return secname;
13407 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13409 static bool
13410 decl_by_reference_p (tree decl)
13412 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13413 || TREE_CODE (decl) == VAR_DECL)
13414 && DECL_BY_REFERENCE (decl));
13417 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13418 for VARLOC. */
13420 static dw_loc_descr_ref
13421 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13422 enum var_init_status initialized)
13424 int have_address = 0;
13425 dw_loc_descr_ref descr;
13426 enum machine_mode mode;
13428 if (want_address != 2)
13430 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13431 /* Single part. */
13432 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13434 varloc = PAT_VAR_LOCATION_LOC (varloc);
13435 if (GET_CODE (varloc) == EXPR_LIST)
13436 varloc = XEXP (varloc, 0);
13437 mode = GET_MODE (varloc);
13438 if (MEM_P (varloc))
13440 rtx addr = XEXP (varloc, 0);
13441 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13442 mode, initialized);
13443 if (descr)
13444 have_address = 1;
13445 else
13447 rtx x = avoid_constant_pool_reference (varloc);
13448 if (x != varloc)
13449 descr = mem_loc_descriptor (x, mode, VOIDmode,
13450 initialized);
13453 else
13454 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13456 else
13457 return 0;
13459 else
13461 if (GET_CODE (varloc) == VAR_LOCATION)
13462 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13463 else
13464 mode = DECL_MODE (loc);
13465 descr = loc_descriptor (varloc, mode, initialized);
13466 have_address = 1;
13469 if (!descr)
13470 return 0;
13472 if (want_address == 2 && !have_address
13473 && (dwarf_version >= 4 || !dwarf_strict))
13475 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13477 expansion_failed (loc, NULL_RTX,
13478 "DWARF address size mismatch");
13479 return 0;
13481 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13482 have_address = 1;
13484 /* Show if we can't fill the request for an address. */
13485 if (want_address && !have_address)
13487 expansion_failed (loc, NULL_RTX,
13488 "Want address and only have value");
13489 return 0;
13492 /* If we've got an address and don't want one, dereference. */
13493 if (!want_address && have_address)
13495 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13496 enum dwarf_location_atom op;
13498 if (size > DWARF2_ADDR_SIZE || size == -1)
13500 expansion_failed (loc, NULL_RTX,
13501 "DWARF address size mismatch");
13502 return 0;
13504 else if (size == DWARF2_ADDR_SIZE)
13505 op = DW_OP_deref;
13506 else
13507 op = DW_OP_deref_size;
13509 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13512 return descr;
13515 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13516 if it is not possible. */
13518 static dw_loc_descr_ref
13519 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13521 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13522 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13523 else if (dwarf_version >= 3 || !dwarf_strict)
13524 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13525 else
13526 return NULL;
13529 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13530 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13532 static dw_loc_descr_ref
13533 dw_sra_loc_expr (tree decl, rtx loc)
13535 rtx p;
13536 unsigned int padsize = 0;
13537 dw_loc_descr_ref descr, *descr_tail;
13538 unsigned HOST_WIDE_INT decl_size;
13539 rtx varloc;
13540 enum var_init_status initialized;
13542 if (DECL_SIZE (decl) == NULL
13543 || !host_integerp (DECL_SIZE (decl), 1))
13544 return NULL;
13546 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13547 descr = NULL;
13548 descr_tail = &descr;
13550 for (p = loc; p; p = XEXP (p, 1))
13552 unsigned int bitsize = decl_piece_bitsize (p);
13553 rtx loc_note = *decl_piece_varloc_ptr (p);
13554 dw_loc_descr_ref cur_descr;
13555 dw_loc_descr_ref *tail, last = NULL;
13556 unsigned int opsize = 0;
13558 if (loc_note == NULL_RTX
13559 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13561 padsize += bitsize;
13562 continue;
13564 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13565 varloc = NOTE_VAR_LOCATION (loc_note);
13566 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13567 if (cur_descr == NULL)
13569 padsize += bitsize;
13570 continue;
13573 /* Check that cur_descr either doesn't use
13574 DW_OP_*piece operations, or their sum is equal
13575 to bitsize. Otherwise we can't embed it. */
13576 for (tail = &cur_descr; *tail != NULL;
13577 tail = &(*tail)->dw_loc_next)
13578 if ((*tail)->dw_loc_opc == DW_OP_piece)
13580 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13581 * BITS_PER_UNIT;
13582 last = *tail;
13584 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13586 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13587 last = *tail;
13590 if (last != NULL && opsize != bitsize)
13592 padsize += bitsize;
13593 /* Discard the current piece of the descriptor and release any
13594 addr_table entries it uses. */
13595 remove_loc_list_addr_table_entries (cur_descr);
13596 continue;
13599 /* If there is a hole, add DW_OP_*piece after empty DWARF
13600 expression, which means that those bits are optimized out. */
13601 if (padsize)
13603 if (padsize > decl_size)
13605 remove_loc_list_addr_table_entries (cur_descr);
13606 goto discard_descr;
13608 decl_size -= padsize;
13609 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13610 if (*descr_tail == NULL)
13612 remove_loc_list_addr_table_entries (cur_descr);
13613 goto discard_descr;
13615 descr_tail = &(*descr_tail)->dw_loc_next;
13616 padsize = 0;
13618 *descr_tail = cur_descr;
13619 descr_tail = tail;
13620 if (bitsize > decl_size)
13621 goto discard_descr;
13622 decl_size -= bitsize;
13623 if (last == NULL)
13625 HOST_WIDE_INT offset = 0;
13626 if (GET_CODE (varloc) == VAR_LOCATION
13627 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13629 varloc = PAT_VAR_LOCATION_LOC (varloc);
13630 if (GET_CODE (varloc) == EXPR_LIST)
13631 varloc = XEXP (varloc, 0);
13635 if (GET_CODE (varloc) == CONST
13636 || GET_CODE (varloc) == SIGN_EXTEND
13637 || GET_CODE (varloc) == ZERO_EXTEND)
13638 varloc = XEXP (varloc, 0);
13639 else if (GET_CODE (varloc) == SUBREG)
13640 varloc = SUBREG_REG (varloc);
13641 else
13642 break;
13644 while (1);
13645 /* DW_OP_bit_size offset should be zero for register
13646 or implicit location descriptions and empty location
13647 descriptions, but for memory addresses needs big endian
13648 adjustment. */
13649 if (MEM_P (varloc))
13651 unsigned HOST_WIDE_INT memsize
13652 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13653 if (memsize != bitsize)
13655 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13656 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13657 goto discard_descr;
13658 if (memsize < bitsize)
13659 goto discard_descr;
13660 if (BITS_BIG_ENDIAN)
13661 offset = memsize - bitsize;
13665 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13666 if (*descr_tail == NULL)
13667 goto discard_descr;
13668 descr_tail = &(*descr_tail)->dw_loc_next;
13672 /* If there were any non-empty expressions, add padding till the end of
13673 the decl. */
13674 if (descr != NULL && decl_size != 0)
13676 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13677 if (*descr_tail == NULL)
13678 goto discard_descr;
13680 return descr;
13682 discard_descr:
13683 /* Discard the descriptor and release any addr_table entries it uses. */
13684 remove_loc_list_addr_table_entries (descr);
13685 return NULL;
13688 /* Return the dwarf representation of the location list LOC_LIST of
13689 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13690 function. */
13692 static dw_loc_list_ref
13693 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13695 const char *endname, *secname;
13696 rtx varloc;
13697 enum var_init_status initialized;
13698 struct var_loc_node *node;
13699 dw_loc_descr_ref descr;
13700 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13701 dw_loc_list_ref list = NULL;
13702 dw_loc_list_ref *listp = &list;
13704 /* Now that we know what section we are using for a base,
13705 actually construct the list of locations.
13706 The first location information is what is passed to the
13707 function that creates the location list, and the remaining
13708 locations just get added on to that list.
13709 Note that we only know the start address for a location
13710 (IE location changes), so to build the range, we use
13711 the range [current location start, next location start].
13712 This means we have to special case the last node, and generate
13713 a range of [last location start, end of function label]. */
13715 secname = secname_for_decl (decl);
13717 for (node = loc_list->first; node; node = node->next)
13718 if (GET_CODE (node->loc) == EXPR_LIST
13719 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13721 if (GET_CODE (node->loc) == EXPR_LIST)
13723 /* This requires DW_OP_{,bit_}piece, which is not usable
13724 inside DWARF expressions. */
13725 if (want_address != 2)
13726 continue;
13727 descr = dw_sra_loc_expr (decl, node->loc);
13728 if (descr == NULL)
13729 continue;
13731 else
13733 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13734 varloc = NOTE_VAR_LOCATION (node->loc);
13735 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13737 if (descr)
13739 bool range_across_switch = false;
13740 /* If section switch happens in between node->label
13741 and node->next->label (or end of function) and
13742 we can't emit it as a single entry list,
13743 emit two ranges, first one ending at the end
13744 of first partition and second one starting at the
13745 beginning of second partition. */
13746 if (node == loc_list->last_before_switch
13747 && (node != loc_list->first || loc_list->first->next)
13748 && current_function_decl)
13750 endname = cfun->fde->dw_fde_end;
13751 range_across_switch = true;
13753 /* The variable has a location between NODE->LABEL and
13754 NODE->NEXT->LABEL. */
13755 else if (node->next)
13756 endname = node->next->label;
13757 /* If the variable has a location at the last label
13758 it keeps its location until the end of function. */
13759 else if (!current_function_decl)
13760 endname = text_end_label;
13761 else
13763 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13764 current_function_funcdef_no);
13765 endname = ggc_strdup (label_id);
13768 *listp = new_loc_list (descr, node->label, endname, secname);
13769 if (TREE_CODE (decl) == PARM_DECL
13770 && node == loc_list->first
13771 && NOTE_P (node->loc)
13772 && strcmp (node->label, endname) == 0)
13773 (*listp)->force = true;
13774 listp = &(*listp)->dw_loc_next;
13776 if (range_across_switch)
13778 if (GET_CODE (node->loc) == EXPR_LIST)
13779 descr = dw_sra_loc_expr (decl, node->loc);
13780 else
13782 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13783 varloc = NOTE_VAR_LOCATION (node->loc);
13784 descr = dw_loc_list_1 (decl, varloc, want_address,
13785 initialized);
13787 gcc_assert (descr);
13788 /* The variable has a location between NODE->LABEL and
13789 NODE->NEXT->LABEL. */
13790 if (node->next)
13791 endname = node->next->label;
13792 else
13793 endname = cfun->fde->dw_fde_second_end;
13794 *listp = new_loc_list (descr,
13795 cfun->fde->dw_fde_second_begin,
13796 endname, secname);
13797 listp = &(*listp)->dw_loc_next;
13802 /* Try to avoid the overhead of a location list emitting a location
13803 expression instead, but only if we didn't have more than one
13804 location entry in the first place. If some entries were not
13805 representable, we don't want to pretend a single entry that was
13806 applies to the entire scope in which the variable is
13807 available. */
13808 if (list && loc_list->first->next)
13809 gen_llsym (list);
13811 return list;
13814 /* Return if the loc_list has only single element and thus can be represented
13815 as location description. */
13817 static bool
13818 single_element_loc_list_p (dw_loc_list_ref list)
13820 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13821 return !list->ll_symbol;
13824 /* To each location in list LIST add loc descr REF. */
13826 static void
13827 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13829 dw_loc_descr_ref copy;
13830 add_loc_descr (&list->expr, ref);
13831 list = list->dw_loc_next;
13832 while (list)
13834 copy = ggc_alloc_dw_loc_descr_node ();
13835 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13836 add_loc_descr (&list->expr, copy);
13837 while (copy->dw_loc_next)
13839 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13840 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13841 copy->dw_loc_next = new_copy;
13842 copy = new_copy;
13844 list = list->dw_loc_next;
13848 /* Given two lists RET and LIST
13849 produce location list that is result of adding expression in LIST
13850 to expression in RET on each position in program.
13851 Might be destructive on both RET and LIST.
13853 TODO: We handle only simple cases of RET or LIST having at most one
13854 element. General case would inolve sorting the lists in program order
13855 and merging them that will need some additional work.
13856 Adding that will improve quality of debug info especially for SRA-ed
13857 structures. */
13859 static void
13860 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13862 if (!list)
13863 return;
13864 if (!*ret)
13866 *ret = list;
13867 return;
13869 if (!list->dw_loc_next)
13871 add_loc_descr_to_each (*ret, list->expr);
13872 return;
13874 if (!(*ret)->dw_loc_next)
13876 add_loc_descr_to_each (list, (*ret)->expr);
13877 *ret = list;
13878 return;
13880 expansion_failed (NULL_TREE, NULL_RTX,
13881 "Don't know how to merge two non-trivial"
13882 " location lists.\n");
13883 *ret = NULL;
13884 return;
13887 /* LOC is constant expression. Try a luck, look it up in constant
13888 pool and return its loc_descr of its address. */
13890 static dw_loc_descr_ref
13891 cst_pool_loc_descr (tree loc)
13893 /* Get an RTL for this, if something has been emitted. */
13894 rtx rtl = lookup_constant_def (loc);
13896 if (!rtl || !MEM_P (rtl))
13898 gcc_assert (!rtl);
13899 return 0;
13901 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13903 /* TODO: We might get more coverage if we was actually delaying expansion
13904 of all expressions till end of compilation when constant pools are fully
13905 populated. */
13906 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13908 expansion_failed (loc, NULL_RTX,
13909 "CST value in contant pool but not marked.");
13910 return 0;
13912 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13913 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13916 /* Return dw_loc_list representing address of addr_expr LOC
13917 by looking for inner INDIRECT_REF expression and turning
13918 it into simple arithmetics. */
13920 static dw_loc_list_ref
13921 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13923 tree obj, offset;
13924 HOST_WIDE_INT bitsize, bitpos, bytepos;
13925 enum machine_mode mode;
13926 int unsignedp, volatilep = 0;
13927 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13929 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13930 &bitsize, &bitpos, &offset, &mode,
13931 &unsignedp, &volatilep, false);
13932 STRIP_NOPS (obj);
13933 if (bitpos % BITS_PER_UNIT)
13935 expansion_failed (loc, NULL_RTX, "bitfield access");
13936 return 0;
13938 if (!INDIRECT_REF_P (obj))
13940 expansion_failed (obj,
13941 NULL_RTX, "no indirect ref in inner refrence");
13942 return 0;
13944 if (!offset && !bitpos)
13945 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13946 else if (toplev
13947 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13948 && (dwarf_version >= 4 || !dwarf_strict))
13950 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13951 if (!list_ret)
13952 return 0;
13953 if (offset)
13955 /* Variable offset. */
13956 list_ret1 = loc_list_from_tree (offset, 0);
13957 if (list_ret1 == 0)
13958 return 0;
13959 add_loc_list (&list_ret, list_ret1);
13960 if (!list_ret)
13961 return 0;
13962 add_loc_descr_to_each (list_ret,
13963 new_loc_descr (DW_OP_plus, 0, 0));
13965 bytepos = bitpos / BITS_PER_UNIT;
13966 if (bytepos > 0)
13967 add_loc_descr_to_each (list_ret,
13968 new_loc_descr (DW_OP_plus_uconst,
13969 bytepos, 0));
13970 else if (bytepos < 0)
13971 loc_list_plus_const (list_ret, bytepos);
13972 add_loc_descr_to_each (list_ret,
13973 new_loc_descr (DW_OP_stack_value, 0, 0));
13975 return list_ret;
13979 /* Generate Dwarf location list representing LOC.
13980 If WANT_ADDRESS is false, expression computing LOC will be computed
13981 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13982 if WANT_ADDRESS is 2, expression computing address useable in location
13983 will be returned (i.e. DW_OP_reg can be used
13984 to refer to register values). */
13986 static dw_loc_list_ref
13987 loc_list_from_tree (tree loc, int want_address)
13989 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13990 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13991 int have_address = 0;
13992 enum dwarf_location_atom op;
13994 /* ??? Most of the time we do not take proper care for sign/zero
13995 extending the values properly. Hopefully this won't be a real
13996 problem... */
13998 switch (TREE_CODE (loc))
14000 case ERROR_MARK:
14001 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14002 return 0;
14004 case PLACEHOLDER_EXPR:
14005 /* This case involves extracting fields from an object to determine the
14006 position of other fields. We don't try to encode this here. The
14007 only user of this is Ada, which encodes the needed information using
14008 the names of types. */
14009 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14010 return 0;
14012 case CALL_EXPR:
14013 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14014 /* There are no opcodes for these operations. */
14015 return 0;
14017 case PREINCREMENT_EXPR:
14018 case PREDECREMENT_EXPR:
14019 case POSTINCREMENT_EXPR:
14020 case POSTDECREMENT_EXPR:
14021 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14022 /* There are no opcodes for these operations. */
14023 return 0;
14025 case ADDR_EXPR:
14026 /* If we already want an address, see if there is INDIRECT_REF inside
14027 e.g. for &this->field. */
14028 if (want_address)
14030 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14031 (loc, want_address == 2);
14032 if (list_ret)
14033 have_address = 1;
14034 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14035 && (ret = cst_pool_loc_descr (loc)))
14036 have_address = 1;
14038 /* Otherwise, process the argument and look for the address. */
14039 if (!list_ret && !ret)
14040 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14041 else
14043 if (want_address)
14044 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14045 return NULL;
14047 break;
14049 case VAR_DECL:
14050 if (DECL_THREAD_LOCAL_P (loc))
14052 rtx rtl;
14053 enum dwarf_location_atom tls_op;
14054 enum dtprel_bool dtprel = dtprel_false;
14056 if (targetm.have_tls)
14058 /* If this is not defined, we have no way to emit the
14059 data. */
14060 if (!targetm.asm_out.output_dwarf_dtprel)
14061 return 0;
14063 /* The way DW_OP_GNU_push_tls_address is specified, we
14064 can only look up addresses of objects in the current
14065 module. We used DW_OP_addr as first op, but that's
14066 wrong, because DW_OP_addr is relocated by the debug
14067 info consumer, while DW_OP_GNU_push_tls_address
14068 operand shouldn't be. */
14069 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14070 return 0;
14071 dtprel = dtprel_true;
14072 tls_op = DW_OP_GNU_push_tls_address;
14074 else
14076 if (!targetm.emutls.debug_form_tls_address
14077 || !(dwarf_version >= 3 || !dwarf_strict))
14078 return 0;
14079 /* We stuffed the control variable into the DECL_VALUE_EXPR
14080 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14081 no longer appear in gimple code. We used the control
14082 variable in specific so that we could pick it up here. */
14083 loc = DECL_VALUE_EXPR (loc);
14084 tls_op = DW_OP_form_tls_address;
14087 rtl = rtl_for_decl_location (loc);
14088 if (rtl == NULL_RTX)
14089 return 0;
14091 if (!MEM_P (rtl))
14092 return 0;
14093 rtl = XEXP (rtl, 0);
14094 if (! CONSTANT_P (rtl))
14095 return 0;
14097 ret = new_addr_loc_descr (rtl, dtprel);
14098 ret1 = new_loc_descr (tls_op, 0, 0);
14099 add_loc_descr (&ret, ret1);
14101 have_address = 1;
14102 break;
14104 /* FALLTHRU */
14106 case PARM_DECL:
14107 case RESULT_DECL:
14108 if (DECL_HAS_VALUE_EXPR_P (loc))
14109 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14110 want_address);
14111 /* FALLTHRU */
14113 case FUNCTION_DECL:
14115 rtx rtl;
14116 var_loc_list *loc_list = lookup_decl_loc (loc);
14118 if (loc_list && loc_list->first)
14120 list_ret = dw_loc_list (loc_list, loc, want_address);
14121 have_address = want_address != 0;
14122 break;
14124 rtl = rtl_for_decl_location (loc);
14125 if (rtl == NULL_RTX)
14127 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14128 return 0;
14130 else if (CONST_INT_P (rtl))
14132 HOST_WIDE_INT val = INTVAL (rtl);
14133 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14134 val &= GET_MODE_MASK (DECL_MODE (loc));
14135 ret = int_loc_descriptor (val);
14137 else if (GET_CODE (rtl) == CONST_STRING)
14139 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14140 return 0;
14142 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14143 ret = new_addr_loc_descr (rtl, dtprel_false);
14144 else
14146 enum machine_mode mode, mem_mode;
14148 /* Certain constructs can only be represented at top-level. */
14149 if (want_address == 2)
14151 ret = loc_descriptor (rtl, VOIDmode,
14152 VAR_INIT_STATUS_INITIALIZED);
14153 have_address = 1;
14155 else
14157 mode = GET_MODE (rtl);
14158 mem_mode = VOIDmode;
14159 if (MEM_P (rtl))
14161 mem_mode = mode;
14162 mode = get_address_mode (rtl);
14163 rtl = XEXP (rtl, 0);
14164 have_address = 1;
14166 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14167 VAR_INIT_STATUS_INITIALIZED);
14169 if (!ret)
14170 expansion_failed (loc, rtl,
14171 "failed to produce loc descriptor for rtl");
14174 break;
14176 case MEM_REF:
14177 /* ??? FIXME. */
14178 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14179 return 0;
14180 /* Fallthru. */
14181 case INDIRECT_REF:
14182 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14183 have_address = 1;
14184 break;
14186 case COMPOUND_EXPR:
14187 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14189 CASE_CONVERT:
14190 case VIEW_CONVERT_EXPR:
14191 case SAVE_EXPR:
14192 case MODIFY_EXPR:
14193 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14195 case COMPONENT_REF:
14196 case BIT_FIELD_REF:
14197 case ARRAY_REF:
14198 case ARRAY_RANGE_REF:
14199 case REALPART_EXPR:
14200 case IMAGPART_EXPR:
14202 tree obj, offset;
14203 HOST_WIDE_INT bitsize, bitpos, bytepos;
14204 enum machine_mode mode;
14205 int unsignedp, volatilep = 0;
14207 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14208 &unsignedp, &volatilep, false);
14210 gcc_assert (obj != loc);
14212 list_ret = loc_list_from_tree (obj,
14213 want_address == 2
14214 && !bitpos && !offset ? 2 : 1);
14215 /* TODO: We can extract value of the small expression via shifting even
14216 for nonzero bitpos. */
14217 if (list_ret == 0)
14218 return 0;
14219 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14221 expansion_failed (loc, NULL_RTX,
14222 "bitfield access");
14223 return 0;
14226 if (offset != NULL_TREE)
14228 /* Variable offset. */
14229 list_ret1 = loc_list_from_tree (offset, 0);
14230 if (list_ret1 == 0)
14231 return 0;
14232 add_loc_list (&list_ret, list_ret1);
14233 if (!list_ret)
14234 return 0;
14235 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14238 bytepos = bitpos / BITS_PER_UNIT;
14239 if (bytepos > 0)
14240 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14241 else if (bytepos < 0)
14242 loc_list_plus_const (list_ret, bytepos);
14244 have_address = 1;
14245 break;
14248 case INTEGER_CST:
14249 if ((want_address || !host_integerp (loc, 0))
14250 && (ret = cst_pool_loc_descr (loc)))
14251 have_address = 1;
14252 else if (want_address == 2
14253 && host_integerp (loc, 0)
14254 && (ret = address_of_int_loc_descriptor
14255 (int_size_in_bytes (TREE_TYPE (loc)),
14256 tree_low_cst (loc, 0))))
14257 have_address = 1;
14258 else if (host_integerp (loc, 0))
14259 ret = int_loc_descriptor (tree_low_cst (loc, 0));
14260 else
14262 expansion_failed (loc, NULL_RTX,
14263 "Integer operand is not host integer");
14264 return 0;
14266 break;
14268 case CONSTRUCTOR:
14269 case REAL_CST:
14270 case STRING_CST:
14271 case COMPLEX_CST:
14272 if ((ret = cst_pool_loc_descr (loc)))
14273 have_address = 1;
14274 else
14275 /* We can construct small constants here using int_loc_descriptor. */
14276 expansion_failed (loc, NULL_RTX,
14277 "constructor or constant not in constant pool");
14278 break;
14280 case TRUTH_AND_EXPR:
14281 case TRUTH_ANDIF_EXPR:
14282 case BIT_AND_EXPR:
14283 op = DW_OP_and;
14284 goto do_binop;
14286 case TRUTH_XOR_EXPR:
14287 case BIT_XOR_EXPR:
14288 op = DW_OP_xor;
14289 goto do_binop;
14291 case TRUTH_OR_EXPR:
14292 case TRUTH_ORIF_EXPR:
14293 case BIT_IOR_EXPR:
14294 op = DW_OP_or;
14295 goto do_binop;
14297 case FLOOR_DIV_EXPR:
14298 case CEIL_DIV_EXPR:
14299 case ROUND_DIV_EXPR:
14300 case TRUNC_DIV_EXPR:
14301 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14302 return 0;
14303 op = DW_OP_div;
14304 goto do_binop;
14306 case MINUS_EXPR:
14307 op = DW_OP_minus;
14308 goto do_binop;
14310 case FLOOR_MOD_EXPR:
14311 case CEIL_MOD_EXPR:
14312 case ROUND_MOD_EXPR:
14313 case TRUNC_MOD_EXPR:
14314 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14316 op = DW_OP_mod;
14317 goto do_binop;
14319 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14320 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14321 if (list_ret == 0 || list_ret1 == 0)
14322 return 0;
14324 add_loc_list (&list_ret, list_ret1);
14325 if (list_ret == 0)
14326 return 0;
14327 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14328 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14329 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14330 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14331 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14332 break;
14334 case MULT_EXPR:
14335 op = DW_OP_mul;
14336 goto do_binop;
14338 case LSHIFT_EXPR:
14339 op = DW_OP_shl;
14340 goto do_binop;
14342 case RSHIFT_EXPR:
14343 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14344 goto do_binop;
14346 case POINTER_PLUS_EXPR:
14347 case PLUS_EXPR:
14348 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14350 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14351 if (list_ret == 0)
14352 return 0;
14354 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14355 break;
14358 op = DW_OP_plus;
14359 goto do_binop;
14361 case LE_EXPR:
14362 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14363 return 0;
14365 op = DW_OP_le;
14366 goto do_binop;
14368 case GE_EXPR:
14369 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14370 return 0;
14372 op = DW_OP_ge;
14373 goto do_binop;
14375 case LT_EXPR:
14376 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14377 return 0;
14379 op = DW_OP_lt;
14380 goto do_binop;
14382 case GT_EXPR:
14383 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14384 return 0;
14386 op = DW_OP_gt;
14387 goto do_binop;
14389 case EQ_EXPR:
14390 op = DW_OP_eq;
14391 goto do_binop;
14393 case NE_EXPR:
14394 op = DW_OP_ne;
14395 goto do_binop;
14397 do_binop:
14398 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14399 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14400 if (list_ret == 0 || list_ret1 == 0)
14401 return 0;
14403 add_loc_list (&list_ret, list_ret1);
14404 if (list_ret == 0)
14405 return 0;
14406 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14407 break;
14409 case TRUTH_NOT_EXPR:
14410 case BIT_NOT_EXPR:
14411 op = DW_OP_not;
14412 goto do_unop;
14414 case ABS_EXPR:
14415 op = DW_OP_abs;
14416 goto do_unop;
14418 case NEGATE_EXPR:
14419 op = DW_OP_neg;
14420 goto do_unop;
14422 do_unop:
14423 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14424 if (list_ret == 0)
14425 return 0;
14427 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14428 break;
14430 case MIN_EXPR:
14431 case MAX_EXPR:
14433 const enum tree_code code =
14434 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14436 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14437 build2 (code, integer_type_node,
14438 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14439 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14442 /* ... fall through ... */
14444 case COND_EXPR:
14446 dw_loc_descr_ref lhs
14447 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14448 dw_loc_list_ref rhs
14449 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14450 dw_loc_descr_ref bra_node, jump_node, tmp;
14452 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14453 if (list_ret == 0 || lhs == 0 || rhs == 0)
14454 return 0;
14456 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14457 add_loc_descr_to_each (list_ret, bra_node);
14459 add_loc_list (&list_ret, rhs);
14460 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14461 add_loc_descr_to_each (list_ret, jump_node);
14463 add_loc_descr_to_each (list_ret, lhs);
14464 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14465 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14467 /* ??? Need a node to point the skip at. Use a nop. */
14468 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14469 add_loc_descr_to_each (list_ret, tmp);
14470 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14471 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14473 break;
14475 case FIX_TRUNC_EXPR:
14476 return 0;
14478 default:
14479 /* Leave front-end specific codes as simply unknown. This comes
14480 up, for instance, with the C STMT_EXPR. */
14481 if ((unsigned int) TREE_CODE (loc)
14482 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14484 expansion_failed (loc, NULL_RTX,
14485 "language specific tree node");
14486 return 0;
14489 #ifdef ENABLE_CHECKING
14490 /* Otherwise this is a generic code; we should just lists all of
14491 these explicitly. We forgot one. */
14492 gcc_unreachable ();
14493 #else
14494 /* In a release build, we want to degrade gracefully: better to
14495 generate incomplete debugging information than to crash. */
14496 return NULL;
14497 #endif
14500 if (!ret && !list_ret)
14501 return 0;
14503 if (want_address == 2 && !have_address
14504 && (dwarf_version >= 4 || !dwarf_strict))
14506 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14508 expansion_failed (loc, NULL_RTX,
14509 "DWARF address size mismatch");
14510 return 0;
14512 if (ret)
14513 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14514 else
14515 add_loc_descr_to_each (list_ret,
14516 new_loc_descr (DW_OP_stack_value, 0, 0));
14517 have_address = 1;
14519 /* Show if we can't fill the request for an address. */
14520 if (want_address && !have_address)
14522 expansion_failed (loc, NULL_RTX,
14523 "Want address and only have value");
14524 return 0;
14527 gcc_assert (!ret || !list_ret);
14529 /* If we've got an address and don't want one, dereference. */
14530 if (!want_address && have_address)
14532 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14534 if (size > DWARF2_ADDR_SIZE || size == -1)
14536 expansion_failed (loc, NULL_RTX,
14537 "DWARF address size mismatch");
14538 return 0;
14540 else if (size == DWARF2_ADDR_SIZE)
14541 op = DW_OP_deref;
14542 else
14543 op = DW_OP_deref_size;
14545 if (ret)
14546 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14547 else
14548 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14550 if (ret)
14551 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14553 return list_ret;
14556 /* Same as above but return only single location expression. */
14557 static dw_loc_descr_ref
14558 loc_descriptor_from_tree (tree loc, int want_address)
14560 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14561 if (!ret)
14562 return NULL;
14563 if (ret->dw_loc_next)
14565 expansion_failed (loc, NULL_RTX,
14566 "Location list where only loc descriptor needed");
14567 return NULL;
14569 return ret->expr;
14572 /* Given a value, round it up to the lowest multiple of `boundary'
14573 which is not less than the value itself. */
14575 static inline HOST_WIDE_INT
14576 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14578 return (((value + boundary - 1) / boundary) * boundary);
14581 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14582 pointer to the declared type for the relevant field variable, or return
14583 `integer_type_node' if the given node turns out to be an
14584 ERROR_MARK node. */
14586 static inline tree
14587 field_type (const_tree decl)
14589 tree type;
14591 if (TREE_CODE (decl) == ERROR_MARK)
14592 return integer_type_node;
14594 type = DECL_BIT_FIELD_TYPE (decl);
14595 if (type == NULL_TREE)
14596 type = TREE_TYPE (decl);
14598 return type;
14601 /* Given a pointer to a tree node, return the alignment in bits for
14602 it, or else return BITS_PER_WORD if the node actually turns out to
14603 be an ERROR_MARK node. */
14605 static inline unsigned
14606 simple_type_align_in_bits (const_tree type)
14608 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14611 static inline unsigned
14612 simple_decl_align_in_bits (const_tree decl)
14614 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14617 /* Return the result of rounding T up to ALIGN. */
14619 static inline double_int
14620 round_up_to_align (double_int t, unsigned int align)
14622 double_int alignd = double_int::from_uhwi (align);
14623 t += alignd;
14624 t += double_int_minus_one;
14625 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14626 t *= alignd;
14627 return t;
14630 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14631 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14632 or return 0 if we are unable to determine what that offset is, either
14633 because the argument turns out to be a pointer to an ERROR_MARK node, or
14634 because the offset is actually variable. (We can't handle the latter case
14635 just yet). */
14637 static HOST_WIDE_INT
14638 field_byte_offset (const_tree decl)
14640 double_int object_offset_in_bits;
14641 double_int object_offset_in_bytes;
14642 double_int bitpos_int;
14644 if (TREE_CODE (decl) == ERROR_MARK)
14645 return 0;
14647 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14649 /* We cannot yet cope with fields whose positions are variable, so
14650 for now, when we see such things, we simply return 0. Someday, we may
14651 be able to handle such cases, but it will be damn difficult. */
14652 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14653 return 0;
14655 bitpos_int = tree_to_double_int (bit_position (decl));
14657 #ifdef PCC_BITFIELD_TYPE_MATTERS
14658 if (PCC_BITFIELD_TYPE_MATTERS)
14660 tree type;
14661 tree field_size_tree;
14662 double_int deepest_bitpos;
14663 double_int field_size_in_bits;
14664 unsigned int type_align_in_bits;
14665 unsigned int decl_align_in_bits;
14666 double_int type_size_in_bits;
14668 type = field_type (decl);
14669 type_size_in_bits = double_int_type_size_in_bits (type);
14670 type_align_in_bits = simple_type_align_in_bits (type);
14672 field_size_tree = DECL_SIZE (decl);
14674 /* The size could be unspecified if there was an error, or for
14675 a flexible array member. */
14676 if (!field_size_tree)
14677 field_size_tree = bitsize_zero_node;
14679 /* If the size of the field is not constant, use the type size. */
14680 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14681 field_size_in_bits = tree_to_double_int (field_size_tree);
14682 else
14683 field_size_in_bits = type_size_in_bits;
14685 decl_align_in_bits = simple_decl_align_in_bits (decl);
14687 /* The GCC front-end doesn't make any attempt to keep track of the
14688 starting bit offset (relative to the start of the containing
14689 structure type) of the hypothetical "containing object" for a
14690 bit-field. Thus, when computing the byte offset value for the
14691 start of the "containing object" of a bit-field, we must deduce
14692 this information on our own. This can be rather tricky to do in
14693 some cases. For example, handling the following structure type
14694 definition when compiling for an i386/i486 target (which only
14695 aligns long long's to 32-bit boundaries) can be very tricky:
14697 struct S { int field1; long long field2:31; };
14699 Fortunately, there is a simple rule-of-thumb which can be used
14700 in such cases. When compiling for an i386/i486, GCC will
14701 allocate 8 bytes for the structure shown above. It decides to
14702 do this based upon one simple rule for bit-field allocation.
14703 GCC allocates each "containing object" for each bit-field at
14704 the first (i.e. lowest addressed) legitimate alignment boundary
14705 (based upon the required minimum alignment for the declared
14706 type of the field) which it can possibly use, subject to the
14707 condition that there is still enough available space remaining
14708 in the containing object (when allocated at the selected point)
14709 to fully accommodate all of the bits of the bit-field itself.
14711 This simple rule makes it obvious why GCC allocates 8 bytes for
14712 each object of the structure type shown above. When looking
14713 for a place to allocate the "containing object" for `field2',
14714 the compiler simply tries to allocate a 64-bit "containing
14715 object" at each successive 32-bit boundary (starting at zero)
14716 until it finds a place to allocate that 64- bit field such that
14717 at least 31 contiguous (and previously unallocated) bits remain
14718 within that selected 64 bit field. (As it turns out, for the
14719 example above, the compiler finds it is OK to allocate the
14720 "containing object" 64-bit field at bit-offset zero within the
14721 structure type.)
14723 Here we attempt to work backwards from the limited set of facts
14724 we're given, and we try to deduce from those facts, where GCC
14725 must have believed that the containing object started (within
14726 the structure type). The value we deduce is then used (by the
14727 callers of this routine) to generate DW_AT_location and
14728 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14729 the case of DW_AT_location, regular fields as well). */
14731 /* Figure out the bit-distance from the start of the structure to
14732 the "deepest" bit of the bit-field. */
14733 deepest_bitpos = bitpos_int + field_size_in_bits;
14735 /* This is the tricky part. Use some fancy footwork to deduce
14736 where the lowest addressed bit of the containing object must
14737 be. */
14738 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14740 /* Round up to type_align by default. This works best for
14741 bitfields. */
14742 object_offset_in_bits
14743 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14745 if (object_offset_in_bits.ugt (bitpos_int))
14747 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14749 /* Round up to decl_align instead. */
14750 object_offset_in_bits
14751 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14754 else
14755 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14756 object_offset_in_bits = bitpos_int;
14758 object_offset_in_bytes
14759 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14760 true, TRUNC_DIV_EXPR);
14761 return object_offset_in_bytes.to_shwi ();
14764 /* The following routines define various Dwarf attributes and any data
14765 associated with them. */
14767 /* Add a location description attribute value to a DIE.
14769 This emits location attributes suitable for whole variables and
14770 whole parameters. Note that the location attributes for struct fields are
14771 generated by the routine `data_member_location_attribute' below. */
14773 static inline void
14774 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14775 dw_loc_list_ref descr)
14777 if (descr == 0)
14778 return;
14779 if (single_element_loc_list_p (descr))
14780 add_AT_loc (die, attr_kind, descr->expr);
14781 else
14782 add_AT_loc_list (die, attr_kind, descr);
14785 /* Add DW_AT_accessibility attribute to DIE if needed. */
14787 static void
14788 add_accessibility_attribute (dw_die_ref die, tree decl)
14790 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14791 children, otherwise the default is DW_ACCESS_public. In DWARF2
14792 the default has always been DW_ACCESS_public. */
14793 if (TREE_PROTECTED (decl))
14794 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14795 else if (TREE_PRIVATE (decl))
14797 if (dwarf_version == 2
14798 || die->die_parent == NULL
14799 || die->die_parent->die_tag != DW_TAG_class_type)
14800 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14802 else if (dwarf_version > 2
14803 && die->die_parent
14804 && die->die_parent->die_tag == DW_TAG_class_type)
14805 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14808 /* Attach the specialized form of location attribute used for data members of
14809 struct and union types. In the special case of a FIELD_DECL node which
14810 represents a bit-field, the "offset" part of this special location
14811 descriptor must indicate the distance in bytes from the lowest-addressed
14812 byte of the containing struct or union type to the lowest-addressed byte of
14813 the "containing object" for the bit-field. (See the `field_byte_offset'
14814 function above).
14816 For any given bit-field, the "containing object" is a hypothetical object
14817 (of some integral or enum type) within which the given bit-field lives. The
14818 type of this hypothetical "containing object" is always the same as the
14819 declared type of the individual bit-field itself (for GCC anyway... the
14820 DWARF spec doesn't actually mandate this). Note that it is the size (in
14821 bytes) of the hypothetical "containing object" which will be given in the
14822 DW_AT_byte_size attribute for this bit-field. (See the
14823 `byte_size_attribute' function below.) It is also used when calculating the
14824 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14825 function below.) */
14827 static void
14828 add_data_member_location_attribute (dw_die_ref die, tree decl)
14830 HOST_WIDE_INT offset;
14831 dw_loc_descr_ref loc_descr = 0;
14833 if (TREE_CODE (decl) == TREE_BINFO)
14835 /* We're working on the TAG_inheritance for a base class. */
14836 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14838 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14839 aren't at a fixed offset from all (sub)objects of the same
14840 type. We need to extract the appropriate offset from our
14841 vtable. The following dwarf expression means
14843 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14845 This is specific to the V3 ABI, of course. */
14847 dw_loc_descr_ref tmp;
14849 /* Make a copy of the object address. */
14850 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14851 add_loc_descr (&loc_descr, tmp);
14853 /* Extract the vtable address. */
14854 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14855 add_loc_descr (&loc_descr, tmp);
14857 /* Calculate the address of the offset. */
14858 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14859 gcc_assert (offset < 0);
14861 tmp = int_loc_descriptor (-offset);
14862 add_loc_descr (&loc_descr, tmp);
14863 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14864 add_loc_descr (&loc_descr, tmp);
14866 /* Extract the offset. */
14867 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14868 add_loc_descr (&loc_descr, tmp);
14870 /* Add it to the object address. */
14871 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14872 add_loc_descr (&loc_descr, tmp);
14874 else
14875 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14877 else
14878 offset = field_byte_offset (decl);
14880 if (! loc_descr)
14882 if (dwarf_version > 2)
14884 /* Don't need to output a location expression, just the constant. */
14885 if (offset < 0)
14886 add_AT_int (die, DW_AT_data_member_location, offset);
14887 else
14888 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14889 return;
14891 else
14893 enum dwarf_location_atom op;
14895 /* The DWARF2 standard says that we should assume that the structure
14896 address is already on the stack, so we can specify a structure
14897 field address by using DW_OP_plus_uconst. */
14898 op = DW_OP_plus_uconst;
14899 loc_descr = new_loc_descr (op, offset, 0);
14903 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14906 /* Writes integer values to dw_vec_const array. */
14908 static void
14909 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14911 while (size != 0)
14913 *dest++ = val & 0xff;
14914 val >>= 8;
14915 --size;
14919 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14921 static HOST_WIDE_INT
14922 extract_int (const unsigned char *src, unsigned int size)
14924 HOST_WIDE_INT val = 0;
14926 src += size;
14927 while (size != 0)
14929 val <<= 8;
14930 val |= *--src & 0xff;
14931 --size;
14933 return val;
14936 /* Writes double_int values to dw_vec_const array. */
14938 static void
14939 insert_double (double_int val, unsigned char *dest)
14941 unsigned char *p0 = dest;
14942 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14944 if (WORDS_BIG_ENDIAN)
14946 p0 = p1;
14947 p1 = dest;
14950 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14951 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14954 /* Writes floating point values to dw_vec_const array. */
14956 static void
14957 insert_float (const_rtx rtl, unsigned char *array)
14959 REAL_VALUE_TYPE rv;
14960 long val[4];
14961 int i;
14963 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14964 real_to_target (val, &rv, GET_MODE (rtl));
14966 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14967 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14969 insert_int (val[i], 4, array);
14970 array += 4;
14974 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14975 does not have a "location" either in memory or in a register. These
14976 things can arise in GNU C when a constant is passed as an actual parameter
14977 to an inlined function. They can also arise in C++ where declared
14978 constants do not necessarily get memory "homes". */
14980 static bool
14981 add_const_value_attribute (dw_die_ref die, rtx rtl)
14983 switch (GET_CODE (rtl))
14985 case CONST_INT:
14987 HOST_WIDE_INT val = INTVAL (rtl);
14989 if (val < 0)
14990 add_AT_int (die, DW_AT_const_value, val);
14991 else
14992 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14994 return true;
14996 case CONST_DOUBLE:
14997 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14998 floating-point constant. A CONST_DOUBLE is used whenever the
14999 constant requires more than one word in order to be adequately
15000 represented. */
15002 enum machine_mode mode = GET_MODE (rtl);
15004 if (SCALAR_FLOAT_MODE_P (mode))
15006 unsigned int length = GET_MODE_SIZE (mode);
15007 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15009 insert_float (rtl, array);
15010 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15012 else
15013 add_AT_double (die, DW_AT_const_value,
15014 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15016 return true;
15018 case CONST_VECTOR:
15020 enum machine_mode mode = GET_MODE (rtl);
15021 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15022 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15023 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15024 (length * elt_size);
15025 unsigned int i;
15026 unsigned char *p;
15028 switch (GET_MODE_CLASS (mode))
15030 case MODE_VECTOR_INT:
15031 for (i = 0, p = array; i < length; i++, p += elt_size)
15033 rtx elt = CONST_VECTOR_ELT (rtl, i);
15034 double_int val = rtx_to_double_int (elt);
15036 if (elt_size <= sizeof (HOST_WIDE_INT))
15037 insert_int (val.to_shwi (), elt_size, p);
15038 else
15040 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15041 insert_double (val, p);
15044 break;
15046 case MODE_VECTOR_FLOAT:
15047 for (i = 0, p = array; i < length; i++, p += elt_size)
15049 rtx elt = CONST_VECTOR_ELT (rtl, i);
15050 insert_float (elt, p);
15052 break;
15054 default:
15055 gcc_unreachable ();
15058 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15060 return true;
15062 case CONST_STRING:
15063 if (dwarf_version >= 4 || !dwarf_strict)
15065 dw_loc_descr_ref loc_result;
15066 resolve_one_addr (&rtl, NULL);
15067 rtl_addr:
15068 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15069 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15070 add_AT_loc (die, DW_AT_location, loc_result);
15071 vec_safe_push (used_rtx_array, rtl);
15072 return true;
15074 return false;
15076 case CONST:
15077 if (CONSTANT_P (XEXP (rtl, 0)))
15078 return add_const_value_attribute (die, XEXP (rtl, 0));
15079 /* FALLTHROUGH */
15080 case SYMBOL_REF:
15081 if (!const_ok_for_output (rtl))
15082 return false;
15083 case LABEL_REF:
15084 if (dwarf_version >= 4 || !dwarf_strict)
15085 goto rtl_addr;
15086 return false;
15088 case PLUS:
15089 /* In cases where an inlined instance of an inline function is passed
15090 the address of an `auto' variable (which is local to the caller) we
15091 can get a situation where the DECL_RTL of the artificial local
15092 variable (for the inlining) which acts as a stand-in for the
15093 corresponding formal parameter (of the inline function) will look
15094 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15095 exactly a compile-time constant expression, but it isn't the address
15096 of the (artificial) local variable either. Rather, it represents the
15097 *value* which the artificial local variable always has during its
15098 lifetime. We currently have no way to represent such quasi-constant
15099 values in Dwarf, so for now we just punt and generate nothing. */
15100 return false;
15102 case HIGH:
15103 case CONST_FIXED:
15104 return false;
15106 case MEM:
15107 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15108 && MEM_READONLY_P (rtl)
15109 && GET_MODE (rtl) == BLKmode)
15111 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15112 return true;
15114 return false;
15116 default:
15117 /* No other kinds of rtx should be possible here. */
15118 gcc_unreachable ();
15120 return false;
15123 /* Determine whether the evaluation of EXPR references any variables
15124 or functions which aren't otherwise used (and therefore may not be
15125 output). */
15126 static tree
15127 reference_to_unused (tree * tp, int * walk_subtrees,
15128 void * data ATTRIBUTE_UNUSED)
15130 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15131 *walk_subtrees = 0;
15133 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15134 && ! TREE_ASM_WRITTEN (*tp))
15135 return *tp;
15136 /* ??? The C++ FE emits debug information for using decls, so
15137 putting gcc_unreachable here falls over. See PR31899. For now
15138 be conservative. */
15139 else if (!cgraph_global_info_ready
15140 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15141 return *tp;
15142 else if (TREE_CODE (*tp) == VAR_DECL)
15144 struct varpool_node *node = varpool_get_node (*tp);
15145 if (!node || !node->definition)
15146 return *tp;
15148 else if (TREE_CODE (*tp) == FUNCTION_DECL
15149 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15151 /* The call graph machinery must have finished analyzing,
15152 optimizing and gimplifying the CU by now.
15153 So if *TP has no call graph node associated
15154 to it, it means *TP will not be emitted. */
15155 if (!cgraph_get_node (*tp))
15156 return *tp;
15158 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15159 return *tp;
15161 return NULL_TREE;
15164 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15165 for use in a later add_const_value_attribute call. */
15167 static rtx
15168 rtl_for_decl_init (tree init, tree type)
15170 rtx rtl = NULL_RTX;
15172 STRIP_NOPS (init);
15174 /* If a variable is initialized with a string constant without embedded
15175 zeros, build CONST_STRING. */
15176 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15178 tree enttype = TREE_TYPE (type);
15179 tree domain = TYPE_DOMAIN (type);
15180 enum machine_mode mode = TYPE_MODE (enttype);
15182 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15183 && domain
15184 && integer_zerop (TYPE_MIN_VALUE (domain))
15185 && compare_tree_int (TYPE_MAX_VALUE (domain),
15186 TREE_STRING_LENGTH (init) - 1) == 0
15187 && ((size_t) TREE_STRING_LENGTH (init)
15188 == strlen (TREE_STRING_POINTER (init)) + 1))
15190 rtl = gen_rtx_CONST_STRING (VOIDmode,
15191 ggc_strdup (TREE_STRING_POINTER (init)));
15192 rtl = gen_rtx_MEM (BLKmode, rtl);
15193 MEM_READONLY_P (rtl) = 1;
15196 /* Other aggregates, and complex values, could be represented using
15197 CONCAT: FIXME! */
15198 else if (AGGREGATE_TYPE_P (type)
15199 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15200 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15201 || TREE_CODE (type) == COMPLEX_TYPE)
15203 /* Vectors only work if their mode is supported by the target.
15204 FIXME: generic vectors ought to work too. */
15205 else if (TREE_CODE (type) == VECTOR_TYPE
15206 && !VECTOR_MODE_P (TYPE_MODE (type)))
15208 /* If the initializer is something that we know will expand into an
15209 immediate RTL constant, expand it now. We must be careful not to
15210 reference variables which won't be output. */
15211 else if (initializer_constant_valid_p (init, type)
15212 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15214 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15215 possible. */
15216 if (TREE_CODE (type) == VECTOR_TYPE)
15217 switch (TREE_CODE (init))
15219 case VECTOR_CST:
15220 break;
15221 case CONSTRUCTOR:
15222 if (TREE_CONSTANT (init))
15224 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15225 bool constant_p = true;
15226 tree value;
15227 unsigned HOST_WIDE_INT ix;
15229 /* Even when ctor is constant, it might contain non-*_CST
15230 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15231 belong into VECTOR_CST nodes. */
15232 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15233 if (!CONSTANT_CLASS_P (value))
15235 constant_p = false;
15236 break;
15239 if (constant_p)
15241 init = build_vector_from_ctor (type, elts);
15242 break;
15245 /* FALLTHRU */
15247 default:
15248 return NULL;
15251 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15253 /* If expand_expr returns a MEM, it wasn't immediate. */
15254 gcc_assert (!rtl || !MEM_P (rtl));
15257 return rtl;
15260 /* Generate RTL for the variable DECL to represent its location. */
15262 static rtx
15263 rtl_for_decl_location (tree decl)
15265 rtx rtl;
15267 /* Here we have to decide where we are going to say the parameter "lives"
15268 (as far as the debugger is concerned). We only have a couple of
15269 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15271 DECL_RTL normally indicates where the parameter lives during most of the
15272 activation of the function. If optimization is enabled however, this
15273 could be either NULL or else a pseudo-reg. Both of those cases indicate
15274 that the parameter doesn't really live anywhere (as far as the code
15275 generation parts of GCC are concerned) during most of the function's
15276 activation. That will happen (for example) if the parameter is never
15277 referenced within the function.
15279 We could just generate a location descriptor here for all non-NULL
15280 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15281 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15282 where DECL_RTL is NULL or is a pseudo-reg.
15284 Note however that we can only get away with using DECL_INCOMING_RTL as
15285 a backup substitute for DECL_RTL in certain limited cases. In cases
15286 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15287 we can be sure that the parameter was passed using the same type as it is
15288 declared to have within the function, and that its DECL_INCOMING_RTL
15289 points us to a place where a value of that type is passed.
15291 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15292 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15293 because in these cases DECL_INCOMING_RTL points us to a value of some
15294 type which is *different* from the type of the parameter itself. Thus,
15295 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15296 such cases, the debugger would end up (for example) trying to fetch a
15297 `float' from a place which actually contains the first part of a
15298 `double'. That would lead to really incorrect and confusing
15299 output at debug-time.
15301 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15302 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15303 are a couple of exceptions however. On little-endian machines we can
15304 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15305 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15306 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15307 when (on a little-endian machine) a non-prototyped function has a
15308 parameter declared to be of type `short' or `char'. In such cases,
15309 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15310 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15311 passed `int' value. If the debugger then uses that address to fetch
15312 a `short' or a `char' (on a little-endian machine) the result will be
15313 the correct data, so we allow for such exceptional cases below.
15315 Note that our goal here is to describe the place where the given formal
15316 parameter lives during most of the function's activation (i.e. between the
15317 end of the prologue and the start of the epilogue). We'll do that as best
15318 as we can. Note however that if the given formal parameter is modified
15319 sometime during the execution of the function, then a stack backtrace (at
15320 debug-time) will show the function as having been called with the *new*
15321 value rather than the value which was originally passed in. This happens
15322 rarely enough that it is not a major problem, but it *is* a problem, and
15323 I'd like to fix it.
15325 A future version of dwarf2out.c may generate two additional attributes for
15326 any given DW_TAG_formal_parameter DIE which will describe the "passed
15327 type" and the "passed location" for the given formal parameter in addition
15328 to the attributes we now generate to indicate the "declared type" and the
15329 "active location" for each parameter. This additional set of attributes
15330 could be used by debuggers for stack backtraces. Separately, note that
15331 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15332 This happens (for example) for inlined-instances of inline function formal
15333 parameters which are never referenced. This really shouldn't be
15334 happening. All PARM_DECL nodes should get valid non-NULL
15335 DECL_INCOMING_RTL values. FIXME. */
15337 /* Use DECL_RTL as the "location" unless we find something better. */
15338 rtl = DECL_RTL_IF_SET (decl);
15340 /* When generating abstract instances, ignore everything except
15341 constants, symbols living in memory, and symbols living in
15342 fixed registers. */
15343 if (! reload_completed)
15345 if (rtl
15346 && (CONSTANT_P (rtl)
15347 || (MEM_P (rtl)
15348 && CONSTANT_P (XEXP (rtl, 0)))
15349 || (REG_P (rtl)
15350 && TREE_CODE (decl) == VAR_DECL
15351 && TREE_STATIC (decl))))
15353 rtl = targetm.delegitimize_address (rtl);
15354 return rtl;
15356 rtl = NULL_RTX;
15358 else if (TREE_CODE (decl) == PARM_DECL)
15360 if (rtl == NULL_RTX
15361 || is_pseudo_reg (rtl)
15362 || (MEM_P (rtl)
15363 && is_pseudo_reg (XEXP (rtl, 0))
15364 && DECL_INCOMING_RTL (decl)
15365 && MEM_P (DECL_INCOMING_RTL (decl))
15366 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15368 tree declared_type = TREE_TYPE (decl);
15369 tree passed_type = DECL_ARG_TYPE (decl);
15370 enum machine_mode dmode = TYPE_MODE (declared_type);
15371 enum machine_mode pmode = TYPE_MODE (passed_type);
15373 /* This decl represents a formal parameter which was optimized out.
15374 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15375 all cases where (rtl == NULL_RTX) just below. */
15376 if (dmode == pmode)
15377 rtl = DECL_INCOMING_RTL (decl);
15378 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15379 && SCALAR_INT_MODE_P (dmode)
15380 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15381 && DECL_INCOMING_RTL (decl))
15383 rtx inc = DECL_INCOMING_RTL (decl);
15384 if (REG_P (inc))
15385 rtl = inc;
15386 else if (MEM_P (inc))
15388 if (BYTES_BIG_ENDIAN)
15389 rtl = adjust_address_nv (inc, dmode,
15390 GET_MODE_SIZE (pmode)
15391 - GET_MODE_SIZE (dmode));
15392 else
15393 rtl = inc;
15398 /* If the parm was passed in registers, but lives on the stack, then
15399 make a big endian correction if the mode of the type of the
15400 parameter is not the same as the mode of the rtl. */
15401 /* ??? This is the same series of checks that are made in dbxout.c before
15402 we reach the big endian correction code there. It isn't clear if all
15403 of these checks are necessary here, but keeping them all is the safe
15404 thing to do. */
15405 else if (MEM_P (rtl)
15406 && XEXP (rtl, 0) != const0_rtx
15407 && ! CONSTANT_P (XEXP (rtl, 0))
15408 /* Not passed in memory. */
15409 && !MEM_P (DECL_INCOMING_RTL (decl))
15410 /* Not passed by invisible reference. */
15411 && (!REG_P (XEXP (rtl, 0))
15412 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15413 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15414 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15415 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15416 #endif
15418 /* Big endian correction check. */
15419 && BYTES_BIG_ENDIAN
15420 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15421 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15422 < UNITS_PER_WORD))
15424 enum machine_mode addr_mode = get_address_mode (rtl);
15425 int offset = (UNITS_PER_WORD
15426 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15428 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15429 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15432 else if (TREE_CODE (decl) == VAR_DECL
15433 && rtl
15434 && MEM_P (rtl)
15435 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15436 && BYTES_BIG_ENDIAN)
15438 enum machine_mode addr_mode = get_address_mode (rtl);
15439 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15440 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15442 /* If a variable is declared "register" yet is smaller than
15443 a register, then if we store the variable to memory, it
15444 looks like we're storing a register-sized value, when in
15445 fact we are not. We need to adjust the offset of the
15446 storage location to reflect the actual value's bytes,
15447 else gdb will not be able to display it. */
15448 if (rsize > dsize)
15449 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15450 plus_constant (addr_mode, XEXP (rtl, 0),
15451 rsize - dsize));
15454 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15455 and will have been substituted directly into all expressions that use it.
15456 C does not have such a concept, but C++ and other languages do. */
15457 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15458 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15460 if (rtl)
15461 rtl = targetm.delegitimize_address (rtl);
15463 /* If we don't look past the constant pool, we risk emitting a
15464 reference to a constant pool entry that isn't referenced from
15465 code, and thus is not emitted. */
15466 if (rtl)
15467 rtl = avoid_constant_pool_reference (rtl);
15469 /* Try harder to get a rtl. If this symbol ends up not being emitted
15470 in the current CU, resolve_addr will remove the expression referencing
15471 it. */
15472 if (rtl == NULL_RTX
15473 && TREE_CODE (decl) == VAR_DECL
15474 && !DECL_EXTERNAL (decl)
15475 && TREE_STATIC (decl)
15476 && DECL_NAME (decl)
15477 && !DECL_HARD_REGISTER (decl)
15478 && DECL_MODE (decl) != VOIDmode)
15480 rtl = make_decl_rtl_for_debug (decl);
15481 if (!MEM_P (rtl)
15482 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15483 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15484 rtl = NULL_RTX;
15487 return rtl;
15490 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15491 returned. If so, the decl for the COMMON block is returned, and the
15492 value is the offset into the common block for the symbol. */
15494 static tree
15495 fortran_common (tree decl, HOST_WIDE_INT *value)
15497 tree val_expr, cvar;
15498 enum machine_mode mode;
15499 HOST_WIDE_INT bitsize, bitpos;
15500 tree offset;
15501 int unsignedp, volatilep = 0;
15503 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15504 it does not have a value (the offset into the common area), or if it
15505 is thread local (as opposed to global) then it isn't common, and shouldn't
15506 be handled as such. */
15507 if (TREE_CODE (decl) != VAR_DECL
15508 || !TREE_STATIC (decl)
15509 || !DECL_HAS_VALUE_EXPR_P (decl)
15510 || !is_fortran ())
15511 return NULL_TREE;
15513 val_expr = DECL_VALUE_EXPR (decl);
15514 if (TREE_CODE (val_expr) != COMPONENT_REF)
15515 return NULL_TREE;
15517 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15518 &mode, &unsignedp, &volatilep, true);
15520 if (cvar == NULL_TREE
15521 || TREE_CODE (cvar) != VAR_DECL
15522 || DECL_ARTIFICIAL (cvar)
15523 || !TREE_PUBLIC (cvar))
15524 return NULL_TREE;
15526 *value = 0;
15527 if (offset != NULL)
15529 if (!host_integerp (offset, 0))
15530 return NULL_TREE;
15531 *value = tree_low_cst (offset, 0);
15533 if (bitpos != 0)
15534 *value += bitpos / BITS_PER_UNIT;
15536 return cvar;
15539 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15540 data attribute for a variable or a parameter. We generate the
15541 DW_AT_const_value attribute only in those cases where the given variable
15542 or parameter does not have a true "location" either in memory or in a
15543 register. This can happen (for example) when a constant is passed as an
15544 actual argument in a call to an inline function. (It's possible that
15545 these things can crop up in other ways also.) Note that one type of
15546 constant value which can be passed into an inlined function is a constant
15547 pointer. This can happen for example if an actual argument in an inlined
15548 function call evaluates to a compile-time constant address.
15550 CACHE_P is true if it is worth caching the location list for DECL,
15551 so that future calls can reuse it rather than regenerate it from scratch.
15552 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15553 since we will need to refer to them each time the function is inlined. */
15555 static bool
15556 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15557 enum dwarf_attribute attr)
15559 rtx rtl;
15560 dw_loc_list_ref list;
15561 var_loc_list *loc_list;
15562 cached_dw_loc_list *cache;
15563 void **slot;
15565 if (TREE_CODE (decl) == ERROR_MARK)
15566 return false;
15568 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15569 || TREE_CODE (decl) == RESULT_DECL);
15571 /* Try to get some constant RTL for this decl, and use that as the value of
15572 the location. */
15574 rtl = rtl_for_decl_location (decl);
15575 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15576 && add_const_value_attribute (die, rtl))
15577 return true;
15579 /* See if we have single element location list that is equivalent to
15580 a constant value. That way we are better to use add_const_value_attribute
15581 rather than expanding constant value equivalent. */
15582 loc_list = lookup_decl_loc (decl);
15583 if (loc_list
15584 && loc_list->first
15585 && loc_list->first->next == NULL
15586 && NOTE_P (loc_list->first->loc)
15587 && NOTE_VAR_LOCATION (loc_list->first->loc)
15588 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15590 struct var_loc_node *node;
15592 node = loc_list->first;
15593 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15594 if (GET_CODE (rtl) == EXPR_LIST)
15595 rtl = XEXP (rtl, 0);
15596 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15597 && add_const_value_attribute (die, rtl))
15598 return true;
15600 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15601 list several times. See if we've already cached the contents. */
15602 list = NULL;
15603 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15604 cache_p = false;
15605 if (cache_p)
15607 cache = (cached_dw_loc_list *)
15608 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15609 if (cache)
15610 list = cache->loc_list;
15612 if (list == NULL)
15614 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15615 /* It is usually worth caching this result if the decl is from
15616 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15617 if (cache_p && list && list->dw_loc_next)
15619 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15620 DECL_UID (decl), INSERT);
15621 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15622 cache->decl_id = DECL_UID (decl);
15623 cache->loc_list = list;
15624 *slot = cache;
15627 if (list)
15629 add_AT_location_description (die, attr, list);
15630 return true;
15632 /* None of that worked, so it must not really have a location;
15633 try adding a constant value attribute from the DECL_INITIAL. */
15634 return tree_add_const_value_attribute_for_decl (die, decl);
15637 /* Add VARIABLE and DIE into deferred locations list. */
15639 static void
15640 defer_location (tree variable, dw_die_ref die)
15642 deferred_locations entry;
15643 entry.variable = variable;
15644 entry.die = die;
15645 vec_safe_push (deferred_locations_list, entry);
15648 /* Helper function for tree_add_const_value_attribute. Natively encode
15649 initializer INIT into an array. Return true if successful. */
15651 static bool
15652 native_encode_initializer (tree init, unsigned char *array, int size)
15654 tree type;
15656 if (init == NULL_TREE)
15657 return false;
15659 STRIP_NOPS (init);
15660 switch (TREE_CODE (init))
15662 case STRING_CST:
15663 type = TREE_TYPE (init);
15664 if (TREE_CODE (type) == ARRAY_TYPE)
15666 tree enttype = TREE_TYPE (type);
15667 enum machine_mode mode = TYPE_MODE (enttype);
15669 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15670 return false;
15671 if (int_size_in_bytes (type) != size)
15672 return false;
15673 if (size > TREE_STRING_LENGTH (init))
15675 memcpy (array, TREE_STRING_POINTER (init),
15676 TREE_STRING_LENGTH (init));
15677 memset (array + TREE_STRING_LENGTH (init),
15678 '\0', size - TREE_STRING_LENGTH (init));
15680 else
15681 memcpy (array, TREE_STRING_POINTER (init), size);
15682 return true;
15684 return false;
15685 case CONSTRUCTOR:
15686 type = TREE_TYPE (init);
15687 if (int_size_in_bytes (type) != size)
15688 return false;
15689 if (TREE_CODE (type) == ARRAY_TYPE)
15691 HOST_WIDE_INT min_index;
15692 unsigned HOST_WIDE_INT cnt;
15693 int curpos = 0, fieldsize;
15694 constructor_elt *ce;
15696 if (TYPE_DOMAIN (type) == NULL_TREE
15697 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15698 return false;
15700 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15701 if (fieldsize <= 0)
15702 return false;
15704 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15705 memset (array, '\0', size);
15706 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15708 tree val = ce->value;
15709 tree index = ce->index;
15710 int pos = curpos;
15711 if (index && TREE_CODE (index) == RANGE_EXPR)
15712 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15713 * fieldsize;
15714 else if (index)
15715 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15717 if (val)
15719 STRIP_NOPS (val);
15720 if (!native_encode_initializer (val, array + pos, fieldsize))
15721 return false;
15723 curpos = pos + fieldsize;
15724 if (index && TREE_CODE (index) == RANGE_EXPR)
15726 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15727 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15728 while (count-- > 0)
15730 if (val)
15731 memcpy (array + curpos, array + pos, fieldsize);
15732 curpos += fieldsize;
15735 gcc_assert (curpos <= size);
15737 return true;
15739 else if (TREE_CODE (type) == RECORD_TYPE
15740 || TREE_CODE (type) == UNION_TYPE)
15742 tree field = NULL_TREE;
15743 unsigned HOST_WIDE_INT cnt;
15744 constructor_elt *ce;
15746 if (int_size_in_bytes (type) != size)
15747 return false;
15749 if (TREE_CODE (type) == RECORD_TYPE)
15750 field = TYPE_FIELDS (type);
15752 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15754 tree val = ce->value;
15755 int pos, fieldsize;
15757 if (ce->index != 0)
15758 field = ce->index;
15760 if (val)
15761 STRIP_NOPS (val);
15763 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15764 return false;
15766 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15767 && TYPE_DOMAIN (TREE_TYPE (field))
15768 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15769 return false;
15770 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15771 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15772 return false;
15773 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15774 pos = int_byte_position (field);
15775 gcc_assert (pos + fieldsize <= size);
15776 if (val
15777 && !native_encode_initializer (val, array + pos, fieldsize))
15778 return false;
15780 return true;
15782 return false;
15783 case VIEW_CONVERT_EXPR:
15784 case NON_LVALUE_EXPR:
15785 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15786 default:
15787 return native_encode_expr (init, array, size) == size;
15791 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15792 attribute is the const value T. */
15794 static bool
15795 tree_add_const_value_attribute (dw_die_ref die, tree t)
15797 tree init;
15798 tree type = TREE_TYPE (t);
15799 rtx rtl;
15801 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15802 return false;
15804 init = t;
15805 gcc_assert (!DECL_P (init));
15807 rtl = rtl_for_decl_init (init, type);
15808 if (rtl)
15809 return add_const_value_attribute (die, rtl);
15810 /* If the host and target are sane, try harder. */
15811 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15812 && initializer_constant_valid_p (init, type))
15814 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15815 if (size > 0 && (int) size == size)
15817 unsigned char *array = (unsigned char *)
15818 ggc_alloc_cleared_atomic (size);
15820 if (native_encode_initializer (init, array, size))
15822 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15823 return true;
15825 ggc_free (array);
15828 return false;
15831 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15832 attribute is the const value of T, where T is an integral constant
15833 variable with static storage duration
15834 (so it can't be a PARM_DECL or a RESULT_DECL). */
15836 static bool
15837 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15840 if (!decl
15841 || (TREE_CODE (decl) != VAR_DECL
15842 && TREE_CODE (decl) != CONST_DECL)
15843 || (TREE_CODE (decl) == VAR_DECL
15844 && !TREE_STATIC (decl)))
15845 return false;
15847 if (TREE_READONLY (decl)
15848 && ! TREE_THIS_VOLATILE (decl)
15849 && DECL_INITIAL (decl))
15850 /* OK */;
15851 else
15852 return false;
15854 /* Don't add DW_AT_const_value if abstract origin already has one. */
15855 if (get_AT (var_die, DW_AT_const_value))
15856 return false;
15858 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15861 /* Convert the CFI instructions for the current function into a
15862 location list. This is used for DW_AT_frame_base when we targeting
15863 a dwarf2 consumer that does not support the dwarf3
15864 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15865 expressions. */
15867 static dw_loc_list_ref
15868 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15870 int ix;
15871 dw_fde_ref fde;
15872 dw_loc_list_ref list, *list_tail;
15873 dw_cfi_ref cfi;
15874 dw_cfa_location last_cfa, next_cfa;
15875 const char *start_label, *last_label, *section;
15876 dw_cfa_location remember;
15878 fde = cfun->fde;
15879 gcc_assert (fde != NULL);
15881 section = secname_for_decl (current_function_decl);
15882 list_tail = &list;
15883 list = NULL;
15885 memset (&next_cfa, 0, sizeof (next_cfa));
15886 next_cfa.reg = INVALID_REGNUM;
15887 remember = next_cfa;
15889 start_label = fde->dw_fde_begin;
15891 /* ??? Bald assumption that the CIE opcode list does not contain
15892 advance opcodes. */
15893 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15894 lookup_cfa_1 (cfi, &next_cfa, &remember);
15896 last_cfa = next_cfa;
15897 last_label = start_label;
15899 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15901 /* If the first partition contained no CFI adjustments, the
15902 CIE opcodes apply to the whole first partition. */
15903 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15904 fde->dw_fde_begin, fde->dw_fde_end, section);
15905 list_tail =&(*list_tail)->dw_loc_next;
15906 start_label = last_label = fde->dw_fde_second_begin;
15909 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15911 switch (cfi->dw_cfi_opc)
15913 case DW_CFA_set_loc:
15914 case DW_CFA_advance_loc1:
15915 case DW_CFA_advance_loc2:
15916 case DW_CFA_advance_loc4:
15917 if (!cfa_equal_p (&last_cfa, &next_cfa))
15919 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15920 start_label, last_label, section);
15922 list_tail = &(*list_tail)->dw_loc_next;
15923 last_cfa = next_cfa;
15924 start_label = last_label;
15926 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15927 break;
15929 case DW_CFA_advance_loc:
15930 /* The encoding is complex enough that we should never emit this. */
15931 gcc_unreachable ();
15933 default:
15934 lookup_cfa_1 (cfi, &next_cfa, &remember);
15935 break;
15937 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15939 if (!cfa_equal_p (&last_cfa, &next_cfa))
15941 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15942 start_label, last_label, section);
15944 list_tail = &(*list_tail)->dw_loc_next;
15945 last_cfa = next_cfa;
15946 start_label = last_label;
15948 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15949 start_label, fde->dw_fde_end, section);
15950 list_tail = &(*list_tail)->dw_loc_next;
15951 start_label = last_label = fde->dw_fde_second_begin;
15955 if (!cfa_equal_p (&last_cfa, &next_cfa))
15957 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15958 start_label, last_label, section);
15959 list_tail = &(*list_tail)->dw_loc_next;
15960 start_label = last_label;
15963 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15964 start_label,
15965 fde->dw_fde_second_begin
15966 ? fde->dw_fde_second_end : fde->dw_fde_end,
15967 section);
15969 if (list && list->dw_loc_next)
15970 gen_llsym (list);
15972 return list;
15975 /* Compute a displacement from the "steady-state frame pointer" to the
15976 frame base (often the same as the CFA), and store it in
15977 frame_pointer_fb_offset. OFFSET is added to the displacement
15978 before the latter is negated. */
15980 static void
15981 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15983 rtx reg, elim;
15985 #ifdef FRAME_POINTER_CFA_OFFSET
15986 reg = frame_pointer_rtx;
15987 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15988 #else
15989 reg = arg_pointer_rtx;
15990 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15991 #endif
15993 elim = (ira_use_lra_p
15994 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15995 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15996 if (GET_CODE (elim) == PLUS)
15998 offset += INTVAL (XEXP (elim, 1));
15999 elim = XEXP (elim, 0);
16002 frame_pointer_fb_offset = -offset;
16004 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16005 in which to eliminate. This is because it's stack pointer isn't
16006 directly accessible as a register within the ISA. To work around
16007 this, assume that while we cannot provide a proper value for
16008 frame_pointer_fb_offset, we won't need one either. */
16009 frame_pointer_fb_offset_valid
16010 = ((SUPPORTS_STACK_ALIGNMENT
16011 && (elim == hard_frame_pointer_rtx
16012 || elim == stack_pointer_rtx))
16013 || elim == (frame_pointer_needed
16014 ? hard_frame_pointer_rtx
16015 : stack_pointer_rtx));
16018 /* Generate a DW_AT_name attribute given some string value to be included as
16019 the value of the attribute. */
16021 static void
16022 add_name_attribute (dw_die_ref die, const char *name_string)
16024 if (name_string != NULL && *name_string != 0)
16026 if (demangle_name_func)
16027 name_string = (*demangle_name_func) (name_string);
16029 add_AT_string (die, DW_AT_name, name_string);
16033 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16034 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16035 of TYPE accordingly.
16037 ??? This is a temporary measure until after we're able to generate
16038 regular DWARF for the complex Ada type system. */
16040 static void
16041 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16042 dw_die_ref context_die)
16044 tree dtype;
16045 dw_die_ref dtype_die;
16047 if (!lang_hooks.types.descriptive_type)
16048 return;
16050 dtype = lang_hooks.types.descriptive_type (type);
16051 if (!dtype)
16052 return;
16054 dtype_die = lookup_type_die (dtype);
16055 if (!dtype_die)
16057 gen_type_die (dtype, context_die);
16058 dtype_die = lookup_type_die (dtype);
16059 gcc_assert (dtype_die);
16062 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16065 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16067 static const char *
16068 comp_dir_string (void)
16070 const char *wd;
16071 char *wd1;
16072 static const char *cached_wd = NULL;
16074 if (cached_wd != NULL)
16075 return cached_wd;
16077 wd = get_src_pwd ();
16078 if (wd == NULL)
16079 return NULL;
16081 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16083 int wdlen;
16085 wdlen = strlen (wd);
16086 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16087 strcpy (wd1, wd);
16088 wd1 [wdlen] = DIR_SEPARATOR;
16089 wd1 [wdlen + 1] = 0;
16090 wd = wd1;
16093 cached_wd = remap_debug_filename (wd);
16094 return cached_wd;
16097 /* Generate a DW_AT_comp_dir attribute for DIE. */
16099 static void
16100 add_comp_dir_attribute (dw_die_ref die)
16102 const char * wd = comp_dir_string ();
16103 if (wd != NULL)
16104 add_AT_string (die, DW_AT_comp_dir, wd);
16107 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16108 default. */
16110 static int
16111 lower_bound_default (void)
16113 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16115 case DW_LANG_C:
16116 case DW_LANG_C89:
16117 case DW_LANG_C99:
16118 case DW_LANG_C_plus_plus:
16119 case DW_LANG_ObjC:
16120 case DW_LANG_ObjC_plus_plus:
16121 case DW_LANG_Java:
16122 return 0;
16123 case DW_LANG_Fortran77:
16124 case DW_LANG_Fortran90:
16125 case DW_LANG_Fortran95:
16126 return 1;
16127 case DW_LANG_UPC:
16128 case DW_LANG_D:
16129 case DW_LANG_Python:
16130 return dwarf_version >= 4 ? 0 : -1;
16131 case DW_LANG_Ada95:
16132 case DW_LANG_Ada83:
16133 case DW_LANG_Cobol74:
16134 case DW_LANG_Cobol85:
16135 case DW_LANG_Pascal83:
16136 case DW_LANG_Modula2:
16137 case DW_LANG_PLI:
16138 return dwarf_version >= 4 ? 1 : -1;
16139 default:
16140 return -1;
16144 /* Given a tree node describing an array bound (either lower or upper) output
16145 a representation for that bound. */
16147 static void
16148 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16150 switch (TREE_CODE (bound))
16152 case ERROR_MARK:
16153 return;
16155 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16156 case INTEGER_CST:
16158 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16159 int dflt;
16161 /* Use the default if possible. */
16162 if (bound_attr == DW_AT_lower_bound
16163 && host_integerp (bound, 0)
16164 && (dflt = lower_bound_default ()) != -1
16165 && tree_low_cst (bound, 0) == dflt)
16168 /* Otherwise represent the bound as an unsigned value with the
16169 precision of its type. The precision and signedness of the
16170 type will be necessary to re-interpret it unambiguously. */
16171 else if (prec < HOST_BITS_PER_WIDE_INT)
16173 unsigned HOST_WIDE_INT mask
16174 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16175 add_AT_unsigned (subrange_die, bound_attr,
16176 TREE_INT_CST_LOW (bound) & mask);
16178 else if (prec == HOST_BITS_PER_WIDE_INT
16179 || TREE_INT_CST_HIGH (bound) == 0)
16180 add_AT_unsigned (subrange_die, bound_attr,
16181 TREE_INT_CST_LOW (bound));
16182 else
16183 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16184 TREE_INT_CST_LOW (bound));
16186 break;
16188 CASE_CONVERT:
16189 case VIEW_CONVERT_EXPR:
16190 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16191 break;
16193 case SAVE_EXPR:
16194 break;
16196 case VAR_DECL:
16197 case PARM_DECL:
16198 case RESULT_DECL:
16200 dw_die_ref decl_die = lookup_decl_die (bound);
16202 /* ??? Can this happen, or should the variable have been bound
16203 first? Probably it can, since I imagine that we try to create
16204 the types of parameters in the order in which they exist in
16205 the list, and won't have created a forward reference to a
16206 later parameter. */
16207 if (decl_die != NULL)
16209 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16210 break;
16213 /* FALLTHRU */
16215 default:
16217 /* Otherwise try to create a stack operation procedure to
16218 evaluate the value of the array bound. */
16220 dw_die_ref ctx, decl_die;
16221 dw_loc_list_ref list;
16223 list = loc_list_from_tree (bound, 2);
16224 if (list == NULL || single_element_loc_list_p (list))
16226 /* If DW_AT_*bound is not a reference nor constant, it is
16227 a DWARF expression rather than location description.
16228 For that loc_list_from_tree (bound, 0) is needed.
16229 If that fails to give a single element list,
16230 fall back to outputting this as a reference anyway. */
16231 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16232 if (list2 && single_element_loc_list_p (list2))
16234 add_AT_loc (subrange_die, bound_attr, list2->expr);
16235 break;
16238 if (list == NULL)
16239 break;
16241 if (current_function_decl == 0)
16242 ctx = comp_unit_die ();
16243 else
16244 ctx = lookup_decl_die (current_function_decl);
16246 decl_die = new_die (DW_TAG_variable, ctx, bound);
16247 add_AT_flag (decl_die, DW_AT_artificial, 1);
16248 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16249 add_AT_location_description (decl_die, DW_AT_location, list);
16250 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16251 break;
16256 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16257 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16258 Note that the block of subscript information for an array type also
16259 includes information about the element type of the given array type. */
16261 static void
16262 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16264 unsigned dimension_number;
16265 tree lower, upper;
16266 dw_die_ref subrange_die;
16268 for (dimension_number = 0;
16269 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16270 type = TREE_TYPE (type), dimension_number++)
16272 tree domain = TYPE_DOMAIN (type);
16274 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16275 break;
16277 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16278 and (in GNU C only) variable bounds. Handle all three forms
16279 here. */
16280 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16281 if (domain)
16283 /* We have an array type with specified bounds. */
16284 lower = TYPE_MIN_VALUE (domain);
16285 upper = TYPE_MAX_VALUE (domain);
16287 /* Define the index type. */
16288 if (TREE_TYPE (domain))
16290 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16291 TREE_TYPE field. We can't emit debug info for this
16292 because it is an unnamed integral type. */
16293 if (TREE_CODE (domain) == INTEGER_TYPE
16294 && TYPE_NAME (domain) == NULL_TREE
16295 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16296 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16298 else
16299 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16300 type_die);
16303 /* ??? If upper is NULL, the array has unspecified length,
16304 but it does have a lower bound. This happens with Fortran
16305 dimension arr(N:*)
16306 Since the debugger is definitely going to need to know N
16307 to produce useful results, go ahead and output the lower
16308 bound solo, and hope the debugger can cope. */
16310 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16311 if (upper)
16312 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16315 /* Otherwise we have an array type with an unspecified length. The
16316 DWARF-2 spec does not say how to handle this; let's just leave out the
16317 bounds. */
16321 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16323 static void
16324 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16326 dw_die_ref decl_die;
16327 HOST_WIDE_INT size;
16329 switch (TREE_CODE (tree_node))
16331 case ERROR_MARK:
16332 size = 0;
16333 break;
16334 case ENUMERAL_TYPE:
16335 case RECORD_TYPE:
16336 case UNION_TYPE:
16337 case QUAL_UNION_TYPE:
16338 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16339 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16341 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16342 return;
16344 size = int_size_in_bytes (tree_node);
16345 break;
16346 case FIELD_DECL:
16347 /* For a data member of a struct or union, the DW_AT_byte_size is
16348 generally given as the number of bytes normally allocated for an
16349 object of the *declared* type of the member itself. This is true
16350 even for bit-fields. */
16351 size = int_size_in_bytes (field_type (tree_node));
16352 break;
16353 default:
16354 gcc_unreachable ();
16357 /* Note that `size' might be -1 when we get to this point. If it is, that
16358 indicates that the byte size of the entity in question is variable. We
16359 have no good way of expressing this fact in Dwarf at the present time,
16360 when location description was not used by the caller code instead. */
16361 if (size >= 0)
16362 add_AT_unsigned (die, DW_AT_byte_size, size);
16365 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16366 which specifies the distance in bits from the highest order bit of the
16367 "containing object" for the bit-field to the highest order bit of the
16368 bit-field itself.
16370 For any given bit-field, the "containing object" is a hypothetical object
16371 (of some integral or enum type) within which the given bit-field lives. The
16372 type of this hypothetical "containing object" is always the same as the
16373 declared type of the individual bit-field itself. The determination of the
16374 exact location of the "containing object" for a bit-field is rather
16375 complicated. It's handled by the `field_byte_offset' function (above).
16377 Note that it is the size (in bytes) of the hypothetical "containing object"
16378 which will be given in the DW_AT_byte_size attribute for this bit-field.
16379 (See `byte_size_attribute' above). */
16381 static inline void
16382 add_bit_offset_attribute (dw_die_ref die, tree decl)
16384 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16385 tree type = DECL_BIT_FIELD_TYPE (decl);
16386 HOST_WIDE_INT bitpos_int;
16387 HOST_WIDE_INT highest_order_object_bit_offset;
16388 HOST_WIDE_INT highest_order_field_bit_offset;
16389 HOST_WIDE_INT bit_offset;
16391 /* Must be a field and a bit field. */
16392 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16394 /* We can't yet handle bit-fields whose offsets are variable, so if we
16395 encounter such things, just return without generating any attribute
16396 whatsoever. Likewise for variable or too large size. */
16397 if (! host_integerp (bit_position (decl), 0)
16398 || ! host_integerp (DECL_SIZE (decl), 1))
16399 return;
16401 bitpos_int = int_bit_position (decl);
16403 /* Note that the bit offset is always the distance (in bits) from the
16404 highest-order bit of the "containing object" to the highest-order bit of
16405 the bit-field itself. Since the "high-order end" of any object or field
16406 is different on big-endian and little-endian machines, the computation
16407 below must take account of these differences. */
16408 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16409 highest_order_field_bit_offset = bitpos_int;
16411 if (! BYTES_BIG_ENDIAN)
16413 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16414 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16417 bit_offset
16418 = (! BYTES_BIG_ENDIAN
16419 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16420 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16422 if (bit_offset < 0)
16423 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16424 else
16425 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16428 /* For a FIELD_DECL node which represents a bit field, output an attribute
16429 which specifies the length in bits of the given field. */
16431 static inline void
16432 add_bit_size_attribute (dw_die_ref die, tree decl)
16434 /* Must be a field and a bit field. */
16435 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16436 && DECL_BIT_FIELD_TYPE (decl));
16438 if (host_integerp (DECL_SIZE (decl), 1))
16439 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16442 /* If the compiled language is ANSI C, then add a 'prototyped'
16443 attribute, if arg types are given for the parameters of a function. */
16445 static inline void
16446 add_prototyped_attribute (dw_die_ref die, tree func_type)
16448 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16449 && prototype_p (func_type))
16450 add_AT_flag (die, DW_AT_prototyped, 1);
16453 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16454 by looking in either the type declaration or object declaration
16455 equate table. */
16457 static inline dw_die_ref
16458 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16460 dw_die_ref origin_die = NULL;
16462 if (TREE_CODE (origin) != FUNCTION_DECL)
16464 /* We may have gotten separated from the block for the inlined
16465 function, if we're in an exception handler or some such; make
16466 sure that the abstract function has been written out.
16468 Doing this for nested functions is wrong, however; functions are
16469 distinct units, and our context might not even be inline. */
16470 tree fn = origin;
16472 if (TYPE_P (fn))
16473 fn = TYPE_STUB_DECL (fn);
16475 fn = decl_function_context (fn);
16476 if (fn)
16477 dwarf2out_abstract_function (fn);
16480 if (DECL_P (origin))
16481 origin_die = lookup_decl_die (origin);
16482 else if (TYPE_P (origin))
16483 origin_die = lookup_type_die (origin);
16485 /* XXX: Functions that are never lowered don't always have correct block
16486 trees (in the case of java, they simply have no block tree, in some other
16487 languages). For these functions, there is nothing we can really do to
16488 output correct debug info for inlined functions in all cases. Rather
16489 than die, we'll just produce deficient debug info now, in that we will
16490 have variables without a proper abstract origin. In the future, when all
16491 functions are lowered, we should re-add a gcc_assert (origin_die)
16492 here. */
16494 if (origin_die)
16495 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16496 return origin_die;
16499 /* We do not currently support the pure_virtual attribute. */
16501 static inline void
16502 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16504 if (DECL_VINDEX (func_decl))
16506 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16508 if (host_integerp (DECL_VINDEX (func_decl), 0))
16509 add_AT_loc (die, DW_AT_vtable_elem_location,
16510 new_loc_descr (DW_OP_constu,
16511 tree_low_cst (DECL_VINDEX (func_decl), 0),
16512 0));
16514 /* GNU extension: Record what type this method came from originally. */
16515 if (debug_info_level > DINFO_LEVEL_TERSE
16516 && DECL_CONTEXT (func_decl))
16517 add_AT_die_ref (die, DW_AT_containing_type,
16518 lookup_type_die (DECL_CONTEXT (func_decl)));
16522 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16523 given decl. This used to be a vendor extension until after DWARF 4
16524 standardized it. */
16526 static void
16527 add_linkage_attr (dw_die_ref die, tree decl)
16529 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16531 /* Mimic what assemble_name_raw does with a leading '*'. */
16532 if (name[0] == '*')
16533 name = &name[1];
16535 if (dwarf_version >= 4)
16536 add_AT_string (die, DW_AT_linkage_name, name);
16537 else
16538 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16541 /* Add source coordinate attributes for the given decl. */
16543 static void
16544 add_src_coords_attributes (dw_die_ref die, tree decl)
16546 expanded_location s;
16548 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16549 return;
16550 s = expand_location (DECL_SOURCE_LOCATION (decl));
16551 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16552 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16555 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16557 static void
16558 add_linkage_name (dw_die_ref die, tree decl)
16560 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16561 && TREE_PUBLIC (decl)
16562 && !DECL_ABSTRACT (decl)
16563 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16564 && die->die_tag != DW_TAG_member)
16566 /* Defer until we have an assembler name set. */
16567 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16569 limbo_die_node *asm_name;
16571 asm_name = ggc_alloc_cleared_limbo_die_node ();
16572 asm_name->die = die;
16573 asm_name->created_for = decl;
16574 asm_name->next = deferred_asm_name;
16575 deferred_asm_name = asm_name;
16577 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16578 add_linkage_attr (die, decl);
16582 /* Add a DW_AT_name attribute and source coordinate attribute for the
16583 given decl, but only if it actually has a name. */
16585 static void
16586 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16588 tree decl_name;
16590 decl_name = DECL_NAME (decl);
16591 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16593 const char *name = dwarf2_name (decl, 0);
16594 if (name)
16595 add_name_attribute (die, name);
16596 if (! DECL_ARTIFICIAL (decl))
16597 add_src_coords_attributes (die, decl);
16599 add_linkage_name (die, decl);
16602 #ifdef VMS_DEBUGGING_INFO
16603 /* Get the function's name, as described by its RTL. This may be different
16604 from the DECL_NAME name used in the source file. */
16605 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16607 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16608 XEXP (DECL_RTL (decl), 0), false);
16609 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16611 #endif /* VMS_DEBUGGING_INFO */
16614 #ifdef VMS_DEBUGGING_INFO
16615 /* Output the debug main pointer die for VMS */
16617 void
16618 dwarf2out_vms_debug_main_pointer (void)
16620 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16621 dw_die_ref die;
16623 /* Allocate the VMS debug main subprogram die. */
16624 die = ggc_alloc_cleared_die_node ();
16625 die->die_tag = DW_TAG_subprogram;
16626 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16627 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16628 current_function_funcdef_no);
16629 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16631 /* Make it the first child of comp_unit_die (). */
16632 die->die_parent = comp_unit_die ();
16633 if (comp_unit_die ()->die_child)
16635 die->die_sib = comp_unit_die ()->die_child->die_sib;
16636 comp_unit_die ()->die_child->die_sib = die;
16638 else
16640 die->die_sib = die;
16641 comp_unit_die ()->die_child = die;
16644 #endif /* VMS_DEBUGGING_INFO */
16646 /* Push a new declaration scope. */
16648 static void
16649 push_decl_scope (tree scope)
16651 vec_safe_push (decl_scope_table, scope);
16654 /* Pop a declaration scope. */
16656 static inline void
16657 pop_decl_scope (void)
16659 decl_scope_table->pop ();
16662 /* walk_tree helper function for uses_local_type, below. */
16664 static tree
16665 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16667 if (!TYPE_P (*tp))
16668 *walk_subtrees = 0;
16669 else
16671 tree name = TYPE_NAME (*tp);
16672 if (name && DECL_P (name) && decl_function_context (name))
16673 return *tp;
16675 return NULL_TREE;
16678 /* If TYPE involves a function-local type (including a local typedef to a
16679 non-local type), returns that type; otherwise returns NULL_TREE. */
16681 static tree
16682 uses_local_type (tree type)
16684 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16685 return used;
16688 /* Return the DIE for the scope that immediately contains this type.
16689 Non-named types that do not involve a function-local type get global
16690 scope. Named types nested in namespaces or other types get their
16691 containing scope. All other types (i.e. function-local named types) get
16692 the current active scope. */
16694 static dw_die_ref
16695 scope_die_for (tree t, dw_die_ref context_die)
16697 dw_die_ref scope_die = NULL;
16698 tree containing_scope;
16700 /* Non-types always go in the current scope. */
16701 gcc_assert (TYPE_P (t));
16703 /* Use the scope of the typedef, rather than the scope of the type
16704 it refers to. */
16705 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16706 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16707 else
16708 containing_scope = TYPE_CONTEXT (t);
16710 /* Use the containing namespace if there is one. */
16711 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16713 if (context_die == lookup_decl_die (containing_scope))
16714 /* OK */;
16715 else if (debug_info_level > DINFO_LEVEL_TERSE)
16716 context_die = get_context_die (containing_scope);
16717 else
16718 containing_scope = NULL_TREE;
16721 /* Ignore function type "scopes" from the C frontend. They mean that
16722 a tagged type is local to a parmlist of a function declarator, but
16723 that isn't useful to DWARF. */
16724 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16725 containing_scope = NULL_TREE;
16727 if (SCOPE_FILE_SCOPE_P (containing_scope))
16729 /* If T uses a local type keep it local as well, to avoid references
16730 to function-local DIEs from outside the function. */
16731 if (current_function_decl && uses_local_type (t))
16732 scope_die = context_die;
16733 else
16734 scope_die = comp_unit_die ();
16736 else if (TYPE_P (containing_scope))
16738 /* For types, we can just look up the appropriate DIE. */
16739 if (debug_info_level > DINFO_LEVEL_TERSE)
16740 scope_die = get_context_die (containing_scope);
16741 else
16743 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16744 if (scope_die == NULL)
16745 scope_die = comp_unit_die ();
16748 else
16749 scope_die = context_die;
16751 return scope_die;
16754 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16756 static inline int
16757 local_scope_p (dw_die_ref context_die)
16759 for (; context_die; context_die = context_die->die_parent)
16760 if (context_die->die_tag == DW_TAG_inlined_subroutine
16761 || context_die->die_tag == DW_TAG_subprogram)
16762 return 1;
16764 return 0;
16767 /* Returns nonzero if CONTEXT_DIE is a class. */
16769 static inline int
16770 class_scope_p (dw_die_ref context_die)
16772 return (context_die
16773 && (context_die->die_tag == DW_TAG_structure_type
16774 || context_die->die_tag == DW_TAG_class_type
16775 || context_die->die_tag == DW_TAG_interface_type
16776 || context_die->die_tag == DW_TAG_union_type));
16779 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16780 whether or not to treat a DIE in this context as a declaration. */
16782 static inline int
16783 class_or_namespace_scope_p (dw_die_ref context_die)
16785 return (class_scope_p (context_die)
16786 || (context_die && context_die->die_tag == DW_TAG_namespace));
16789 /* Many forms of DIEs require a "type description" attribute. This
16790 routine locates the proper "type descriptor" die for the type given
16791 by 'type', and adds a DW_AT_type attribute below the given die. */
16793 static void
16794 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16795 int decl_volatile, dw_die_ref context_die)
16797 enum tree_code code = TREE_CODE (type);
16798 dw_die_ref type_die = NULL;
16800 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16801 or fixed-point type, use the inner type. This is because we have no
16802 support for unnamed types in base_type_die. This can happen if this is
16803 an Ada subrange type. Correct solution is emit a subrange type die. */
16804 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16805 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16806 type = TREE_TYPE (type), code = TREE_CODE (type);
16808 if (code == ERROR_MARK
16809 /* Handle a special case. For functions whose return type is void, we
16810 generate *no* type attribute. (Note that no object may have type
16811 `void', so this only applies to function return types). */
16812 || code == VOID_TYPE)
16813 return;
16815 type_die = modified_type_die (type,
16816 decl_const || TYPE_READONLY (type),
16817 decl_volatile || TYPE_VOLATILE (type),
16818 context_die);
16820 if (type_die != NULL)
16821 add_AT_die_ref (object_die, DW_AT_type, type_die);
16824 /* Given an object die, add the calling convention attribute for the
16825 function call type. */
16826 static void
16827 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16829 enum dwarf_calling_convention value = DW_CC_normal;
16831 value = ((enum dwarf_calling_convention)
16832 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16834 if (is_fortran ()
16835 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16837 /* DWARF 2 doesn't provide a way to identify a program's source-level
16838 entry point. DW_AT_calling_convention attributes are only meant
16839 to describe functions' calling conventions. However, lacking a
16840 better way to signal the Fortran main program, we used this for
16841 a long time, following existing custom. Now, DWARF 4 has
16842 DW_AT_main_subprogram, which we add below, but some tools still
16843 rely on the old way, which we thus keep. */
16844 value = DW_CC_program;
16846 if (dwarf_version >= 4 || !dwarf_strict)
16847 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16850 /* Only add the attribute if the backend requests it, and
16851 is not DW_CC_normal. */
16852 if (value && (value != DW_CC_normal))
16853 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16856 /* Given a tree pointer to a struct, class, union, or enum type node, return
16857 a pointer to the (string) tag name for the given type, or zero if the type
16858 was declared without a tag. */
16860 static const char *
16861 type_tag (const_tree type)
16863 const char *name = 0;
16865 if (TYPE_NAME (type) != 0)
16867 tree t = 0;
16869 /* Find the IDENTIFIER_NODE for the type name. */
16870 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16871 && !TYPE_NAMELESS (type))
16872 t = TYPE_NAME (type);
16874 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16875 a TYPE_DECL node, regardless of whether or not a `typedef' was
16876 involved. */
16877 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16878 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16880 /* We want to be extra verbose. Don't call dwarf_name if
16881 DECL_NAME isn't set. The default hook for decl_printable_name
16882 doesn't like that, and in this context it's correct to return
16883 0, instead of "<anonymous>" or the like. */
16884 if (DECL_NAME (TYPE_NAME (type))
16885 && !DECL_NAMELESS (TYPE_NAME (type)))
16886 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16889 /* Now get the name as a string, or invent one. */
16890 if (!name && t != 0)
16891 name = IDENTIFIER_POINTER (t);
16894 return (name == 0 || *name == '\0') ? 0 : name;
16897 /* Return the type associated with a data member, make a special check
16898 for bit field types. */
16900 static inline tree
16901 member_declared_type (const_tree member)
16903 return (DECL_BIT_FIELD_TYPE (member)
16904 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16907 /* Get the decl's label, as described by its RTL. This may be different
16908 from the DECL_NAME name used in the source file. */
16910 #if 0
16911 static const char *
16912 decl_start_label (tree decl)
16914 rtx x;
16915 const char *fnname;
16917 x = DECL_RTL (decl);
16918 gcc_assert (MEM_P (x));
16920 x = XEXP (x, 0);
16921 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16923 fnname = XSTR (x, 0);
16924 return fnname;
16926 #endif
16928 /* These routines generate the internal representation of the DIE's for
16929 the compilation unit. Debugging information is collected by walking
16930 the declaration trees passed in from dwarf2out_decl(). */
16932 static void
16933 gen_array_type_die (tree type, dw_die_ref context_die)
16935 dw_die_ref scope_die = scope_die_for (type, context_die);
16936 dw_die_ref array_die;
16938 /* GNU compilers represent multidimensional array types as sequences of one
16939 dimensional array types whose element types are themselves array types.
16940 We sometimes squish that down to a single array_type DIE with multiple
16941 subscripts in the Dwarf debugging info. The draft Dwarf specification
16942 say that we are allowed to do this kind of compression in C, because
16943 there is no difference between an array of arrays and a multidimensional
16944 array. We don't do this for Ada to remain as close as possible to the
16945 actual representation, which is especially important against the language
16946 flexibilty wrt arrays of variable size. */
16948 bool collapse_nested_arrays = !is_ada ();
16949 tree element_type;
16951 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16952 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16953 if (TYPE_STRING_FLAG (type)
16954 && TREE_CODE (type) == ARRAY_TYPE
16955 && is_fortran ()
16956 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16958 HOST_WIDE_INT size;
16960 array_die = new_die (DW_TAG_string_type, scope_die, type);
16961 add_name_attribute (array_die, type_tag (type));
16962 equate_type_number_to_die (type, array_die);
16963 size = int_size_in_bytes (type);
16964 if (size >= 0)
16965 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16966 else if (TYPE_DOMAIN (type) != NULL_TREE
16967 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16968 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16970 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16971 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16973 size = int_size_in_bytes (TREE_TYPE (szdecl));
16974 if (loc && size > 0)
16976 add_AT_location_description (array_die, DW_AT_string_length, loc);
16977 if (size != DWARF2_ADDR_SIZE)
16978 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16981 return;
16984 array_die = new_die (DW_TAG_array_type, scope_die, type);
16985 add_name_attribute (array_die, type_tag (type));
16986 equate_type_number_to_die (type, array_die);
16988 if (TREE_CODE (type) == VECTOR_TYPE)
16989 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16991 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16992 if (is_fortran ()
16993 && TREE_CODE (type) == ARRAY_TYPE
16994 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16995 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16996 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16998 #if 0
16999 /* We default the array ordering. SDB will probably do
17000 the right things even if DW_AT_ordering is not present. It's not even
17001 an issue until we start to get into multidimensional arrays anyway. If
17002 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17003 then we'll have to put the DW_AT_ordering attribute back in. (But if
17004 and when we find out that we need to put these in, we will only do so
17005 for multidimensional arrays. */
17006 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17007 #endif
17009 if (TREE_CODE (type) == VECTOR_TYPE)
17011 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17012 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17013 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17014 add_bound_info (subrange_die, DW_AT_upper_bound,
17015 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17017 else
17018 add_subscript_info (array_die, type, collapse_nested_arrays);
17020 /* Add representation of the type of the elements of this array type and
17021 emit the corresponding DIE if we haven't done it already. */
17022 element_type = TREE_TYPE (type);
17023 if (collapse_nested_arrays)
17024 while (TREE_CODE (element_type) == ARRAY_TYPE)
17026 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17027 break;
17028 element_type = TREE_TYPE (element_type);
17031 add_type_attribute (array_die, element_type, 0, 0, context_die);
17033 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17034 if (TYPE_ARTIFICIAL (type))
17035 add_AT_flag (array_die, DW_AT_artificial, 1);
17037 if (get_AT (array_die, DW_AT_name))
17038 add_pubtype (type, array_die);
17041 static dw_loc_descr_ref
17042 descr_info_loc (tree val, tree base_decl)
17044 HOST_WIDE_INT size;
17045 dw_loc_descr_ref loc, loc2;
17046 enum dwarf_location_atom op;
17048 if (val == base_decl)
17049 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17051 switch (TREE_CODE (val))
17053 CASE_CONVERT:
17054 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17055 case VAR_DECL:
17056 return loc_descriptor_from_tree (val, 0);
17057 case INTEGER_CST:
17058 if (host_integerp (val, 0))
17059 return int_loc_descriptor (tree_low_cst (val, 0));
17060 break;
17061 case INDIRECT_REF:
17062 size = int_size_in_bytes (TREE_TYPE (val));
17063 if (size < 0)
17064 break;
17065 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17066 if (!loc)
17067 break;
17068 if (size == DWARF2_ADDR_SIZE)
17069 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17070 else
17071 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17072 return loc;
17073 case POINTER_PLUS_EXPR:
17074 case PLUS_EXPR:
17075 if (host_integerp (TREE_OPERAND (val, 1), 1)
17076 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
17077 < 16384)
17079 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17080 if (!loc)
17081 break;
17082 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
17084 else
17086 op = DW_OP_plus;
17087 do_binop:
17088 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17089 if (!loc)
17090 break;
17091 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17092 if (!loc2)
17093 break;
17094 add_loc_descr (&loc, loc2);
17095 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17097 return loc;
17098 case MINUS_EXPR:
17099 op = DW_OP_minus;
17100 goto do_binop;
17101 case MULT_EXPR:
17102 op = DW_OP_mul;
17103 goto do_binop;
17104 case EQ_EXPR:
17105 op = DW_OP_eq;
17106 goto do_binop;
17107 case NE_EXPR:
17108 op = DW_OP_ne;
17109 goto do_binop;
17110 default:
17111 break;
17113 return NULL;
17116 static void
17117 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17118 tree val, tree base_decl)
17120 dw_loc_descr_ref loc;
17122 if (host_integerp (val, 0))
17124 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
17125 return;
17128 loc = descr_info_loc (val, base_decl);
17129 if (!loc)
17130 return;
17132 add_AT_loc (die, attr, loc);
17135 /* This routine generates DIE for array with hidden descriptor, details
17136 are filled into *info by a langhook. */
17138 static void
17139 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17140 dw_die_ref context_die)
17142 dw_die_ref scope_die = scope_die_for (type, context_die);
17143 dw_die_ref array_die;
17144 int dim;
17146 array_die = new_die (DW_TAG_array_type, scope_die, type);
17147 add_name_attribute (array_die, type_tag (type));
17148 equate_type_number_to_die (type, array_die);
17150 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17151 if (is_fortran ()
17152 && info->ndimensions >= 2)
17153 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17155 if (info->data_location)
17156 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17157 info->base_decl);
17158 if (info->associated)
17159 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17160 info->base_decl);
17161 if (info->allocated)
17162 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17163 info->base_decl);
17165 for (dim = 0; dim < info->ndimensions; dim++)
17167 dw_die_ref subrange_die
17168 = new_die (DW_TAG_subrange_type, array_die, NULL);
17170 if (info->dimen[dim].lower_bound)
17172 /* If it is the default value, omit it. */
17173 int dflt;
17175 if (host_integerp (info->dimen[dim].lower_bound, 0)
17176 && (dflt = lower_bound_default ()) != -1
17177 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
17179 else
17180 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17181 info->dimen[dim].lower_bound,
17182 info->base_decl);
17184 if (info->dimen[dim].upper_bound)
17185 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17186 info->dimen[dim].upper_bound,
17187 info->base_decl);
17188 if (info->dimen[dim].stride)
17189 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17190 info->dimen[dim].stride,
17191 info->base_decl);
17194 gen_type_die (info->element_type, context_die);
17195 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17197 if (get_AT (array_die, DW_AT_name))
17198 add_pubtype (type, array_die);
17201 #if 0
17202 static void
17203 gen_entry_point_die (tree decl, dw_die_ref context_die)
17205 tree origin = decl_ultimate_origin (decl);
17206 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17208 if (origin != NULL)
17209 add_abstract_origin_attribute (decl_die, origin);
17210 else
17212 add_name_and_src_coords_attributes (decl_die, decl);
17213 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17214 0, 0, context_die);
17217 if (DECL_ABSTRACT (decl))
17218 equate_decl_number_to_die (decl, decl_die);
17219 else
17220 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17222 #endif
17224 /* Walk through the list of incomplete types again, trying once more to
17225 emit full debugging info for them. */
17227 static void
17228 retry_incomplete_types (void)
17230 int i;
17232 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17233 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17234 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17237 /* Determine what tag to use for a record type. */
17239 static enum dwarf_tag
17240 record_type_tag (tree type)
17242 if (! lang_hooks.types.classify_record)
17243 return DW_TAG_structure_type;
17245 switch (lang_hooks.types.classify_record (type))
17247 case RECORD_IS_STRUCT:
17248 return DW_TAG_structure_type;
17250 case RECORD_IS_CLASS:
17251 return DW_TAG_class_type;
17253 case RECORD_IS_INTERFACE:
17254 if (dwarf_version >= 3 || !dwarf_strict)
17255 return DW_TAG_interface_type;
17256 return DW_TAG_structure_type;
17258 default:
17259 gcc_unreachable ();
17263 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17264 include all of the information about the enumeration values also. Each
17265 enumerated type name/value is listed as a child of the enumerated type
17266 DIE. */
17268 static dw_die_ref
17269 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17271 dw_die_ref type_die = lookup_type_die (type);
17273 if (type_die == NULL)
17275 type_die = new_die (DW_TAG_enumeration_type,
17276 scope_die_for (type, context_die), type);
17277 equate_type_number_to_die (type, type_die);
17278 add_name_attribute (type_die, type_tag (type));
17279 if (dwarf_version >= 4 || !dwarf_strict)
17281 if (ENUM_IS_SCOPED (type))
17282 add_AT_flag (type_die, DW_AT_enum_class, 1);
17283 if (ENUM_IS_OPAQUE (type))
17284 add_AT_flag (type_die, DW_AT_declaration, 1);
17287 else if (! TYPE_SIZE (type))
17288 return type_die;
17289 else
17290 remove_AT (type_die, DW_AT_declaration);
17292 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17293 given enum type is incomplete, do not generate the DW_AT_byte_size
17294 attribute or the DW_AT_element_list attribute. */
17295 if (TYPE_SIZE (type))
17297 tree link;
17299 TREE_ASM_WRITTEN (type) = 1;
17300 add_byte_size_attribute (type_die, type);
17301 if (TYPE_STUB_DECL (type) != NULL_TREE)
17303 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17304 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17307 /* If the first reference to this type was as the return type of an
17308 inline function, then it may not have a parent. Fix this now. */
17309 if (type_die->die_parent == NULL)
17310 add_child_die (scope_die_for (type, context_die), type_die);
17312 for (link = TYPE_VALUES (type);
17313 link != NULL; link = TREE_CHAIN (link))
17315 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17316 tree value = TREE_VALUE (link);
17318 add_name_attribute (enum_die,
17319 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17321 if (TREE_CODE (value) == CONST_DECL)
17322 value = DECL_INITIAL (value);
17324 if (simple_type_size_in_bits (TREE_TYPE (value))
17325 <= HOST_BITS_PER_WIDE_INT || host_integerp (value, 0))
17326 /* DWARF2 does not provide a way of indicating whether or
17327 not enumeration constants are signed or unsigned. GDB
17328 always assumes the values are signed, so we output all
17329 values as if they were signed. That means that
17330 enumeration constants with very large unsigned values
17331 will appear to have negative values in the debugger.
17333 TODO: the above comment is wrong, DWARF2 does provide
17334 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17335 This should be re-worked to use correct signed/unsigned
17336 int/double tags for all cases, instead of always treating as
17337 signed. */
17338 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17339 else
17340 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17341 that here. */
17342 add_AT_double (enum_die, DW_AT_const_value,
17343 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17346 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17347 if (TYPE_ARTIFICIAL (type))
17348 add_AT_flag (type_die, DW_AT_artificial, 1);
17350 else
17351 add_AT_flag (type_die, DW_AT_declaration, 1);
17353 add_pubtype (type, type_die);
17355 return type_die;
17358 /* Generate a DIE to represent either a real live formal parameter decl or to
17359 represent just the type of some formal parameter position in some function
17360 type.
17362 Note that this routine is a bit unusual because its argument may be a
17363 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17364 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17365 node. If it's the former then this function is being called to output a
17366 DIE to represent a formal parameter object (or some inlining thereof). If
17367 it's the latter, then this function is only being called to output a
17368 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17369 argument type of some subprogram type.
17370 If EMIT_NAME_P is true, name and source coordinate attributes
17371 are emitted. */
17373 static dw_die_ref
17374 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17375 dw_die_ref context_die)
17377 tree node_or_origin = node ? node : origin;
17378 tree ultimate_origin;
17379 dw_die_ref parm_die
17380 = new_die (DW_TAG_formal_parameter, context_die, node);
17382 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17384 case tcc_declaration:
17385 ultimate_origin = decl_ultimate_origin (node_or_origin);
17386 if (node || ultimate_origin)
17387 origin = ultimate_origin;
17388 if (origin != NULL)
17389 add_abstract_origin_attribute (parm_die, origin);
17390 else if (emit_name_p)
17391 add_name_and_src_coords_attributes (parm_die, node);
17392 if (origin == NULL
17393 || (! DECL_ABSTRACT (node_or_origin)
17394 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17395 decl_function_context
17396 (node_or_origin))))
17398 tree type = TREE_TYPE (node_or_origin);
17399 if (decl_by_reference_p (node_or_origin))
17400 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17401 context_die);
17402 else
17403 add_type_attribute (parm_die, type,
17404 TREE_READONLY (node_or_origin),
17405 TREE_THIS_VOLATILE (node_or_origin),
17406 context_die);
17408 if (origin == NULL && DECL_ARTIFICIAL (node))
17409 add_AT_flag (parm_die, DW_AT_artificial, 1);
17411 if (node && node != origin)
17412 equate_decl_number_to_die (node, parm_die);
17413 if (! DECL_ABSTRACT (node_or_origin))
17414 add_location_or_const_value_attribute (parm_die, node_or_origin,
17415 node == NULL, DW_AT_location);
17417 break;
17419 case tcc_type:
17420 /* We were called with some kind of a ..._TYPE node. */
17421 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17422 break;
17424 default:
17425 gcc_unreachable ();
17428 return parm_die;
17431 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17432 children DW_TAG_formal_parameter DIEs representing the arguments of the
17433 parameter pack.
17435 PARM_PACK must be a function parameter pack.
17436 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17437 must point to the subsequent arguments of the function PACK_ARG belongs to.
17438 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17439 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17440 following the last one for which a DIE was generated. */
17442 static dw_die_ref
17443 gen_formal_parameter_pack_die (tree parm_pack,
17444 tree pack_arg,
17445 dw_die_ref subr_die,
17446 tree *next_arg)
17448 tree arg;
17449 dw_die_ref parm_pack_die;
17451 gcc_assert (parm_pack
17452 && lang_hooks.function_parameter_pack_p (parm_pack)
17453 && subr_die);
17455 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17456 add_src_coords_attributes (parm_pack_die, parm_pack);
17458 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17460 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17461 parm_pack))
17462 break;
17463 gen_formal_parameter_die (arg, NULL,
17464 false /* Don't emit name attribute. */,
17465 parm_pack_die);
17467 if (next_arg)
17468 *next_arg = arg;
17469 return parm_pack_die;
17472 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17473 at the end of an (ANSI prototyped) formal parameters list. */
17475 static void
17476 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17478 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17481 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17482 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17483 parameters as specified in some function type specification (except for
17484 those which appear as part of a function *definition*). */
17486 static void
17487 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17489 tree link;
17490 tree formal_type = NULL;
17491 tree first_parm_type;
17492 tree arg;
17494 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17496 arg = DECL_ARGUMENTS (function_or_method_type);
17497 function_or_method_type = TREE_TYPE (function_or_method_type);
17499 else
17500 arg = NULL_TREE;
17502 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17504 /* Make our first pass over the list of formal parameter types and output a
17505 DW_TAG_formal_parameter DIE for each one. */
17506 for (link = first_parm_type; link; )
17508 dw_die_ref parm_die;
17510 formal_type = TREE_VALUE (link);
17511 if (formal_type == void_type_node)
17512 break;
17514 /* Output a (nameless) DIE to represent the formal parameter itself. */
17515 parm_die = gen_formal_parameter_die (formal_type, NULL,
17516 true /* Emit name attribute. */,
17517 context_die);
17518 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17519 && link == first_parm_type)
17521 add_AT_flag (parm_die, DW_AT_artificial, 1);
17522 if (dwarf_version >= 3 || !dwarf_strict)
17523 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17525 else if (arg && DECL_ARTIFICIAL (arg))
17526 add_AT_flag (parm_die, DW_AT_artificial, 1);
17528 link = TREE_CHAIN (link);
17529 if (arg)
17530 arg = DECL_CHAIN (arg);
17533 /* If this function type has an ellipsis, add a
17534 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17535 if (formal_type != void_type_node)
17536 gen_unspecified_parameters_die (function_or_method_type, context_die);
17538 /* Make our second (and final) pass over the list of formal parameter types
17539 and output DIEs to represent those types (as necessary). */
17540 for (link = TYPE_ARG_TYPES (function_or_method_type);
17541 link && TREE_VALUE (link);
17542 link = TREE_CHAIN (link))
17543 gen_type_die (TREE_VALUE (link), context_die);
17546 /* We want to generate the DIE for TYPE so that we can generate the
17547 die for MEMBER, which has been defined; we will need to refer back
17548 to the member declaration nested within TYPE. If we're trying to
17549 generate minimal debug info for TYPE, processing TYPE won't do the
17550 trick; we need to attach the member declaration by hand. */
17552 static void
17553 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17555 gen_type_die (type, context_die);
17557 /* If we're trying to avoid duplicate debug info, we may not have
17558 emitted the member decl for this function. Emit it now. */
17559 if (TYPE_STUB_DECL (type)
17560 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17561 && ! lookup_decl_die (member))
17563 dw_die_ref type_die;
17564 gcc_assert (!decl_ultimate_origin (member));
17566 push_decl_scope (type);
17567 type_die = lookup_type_die_strip_naming_typedef (type);
17568 if (TREE_CODE (member) == FUNCTION_DECL)
17569 gen_subprogram_die (member, type_die);
17570 else if (TREE_CODE (member) == FIELD_DECL)
17572 /* Ignore the nameless fields that are used to skip bits but handle
17573 C++ anonymous unions and structs. */
17574 if (DECL_NAME (member) != NULL_TREE
17575 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17576 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17578 gen_type_die (member_declared_type (member), type_die);
17579 gen_field_die (member, type_die);
17582 else
17583 gen_variable_die (member, NULL_TREE, type_die);
17585 pop_decl_scope ();
17589 /* Forward declare these functions, because they are mutually recursive
17590 with their set_block_* pairing functions. */
17591 static void set_decl_origin_self (tree);
17592 static void set_decl_abstract_flags (tree, int);
17594 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17595 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17596 that it points to the node itself, thus indicating that the node is its
17597 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17598 the given node is NULL, recursively descend the decl/block tree which
17599 it is the root of, and for each other ..._DECL or BLOCK node contained
17600 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17601 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17602 values to point to themselves. */
17604 static void
17605 set_block_origin_self (tree stmt)
17607 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17609 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17612 tree local_decl;
17614 for (local_decl = BLOCK_VARS (stmt);
17615 local_decl != NULL_TREE;
17616 local_decl = DECL_CHAIN (local_decl))
17617 if (! DECL_EXTERNAL (local_decl))
17618 set_decl_origin_self (local_decl); /* Potential recursion. */
17622 tree subblock;
17624 for (subblock = BLOCK_SUBBLOCKS (stmt);
17625 subblock != NULL_TREE;
17626 subblock = BLOCK_CHAIN (subblock))
17627 set_block_origin_self (subblock); /* Recurse. */
17632 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17633 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17634 node to so that it points to the node itself, thus indicating that the
17635 node represents its own (abstract) origin. Additionally, if the
17636 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17637 the decl/block tree of which the given node is the root of, and for
17638 each other ..._DECL or BLOCK node contained therein whose
17639 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17640 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17641 point to themselves. */
17643 static void
17644 set_decl_origin_self (tree decl)
17646 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17648 DECL_ABSTRACT_ORIGIN (decl) = decl;
17649 if (TREE_CODE (decl) == FUNCTION_DECL)
17651 tree arg;
17653 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17654 DECL_ABSTRACT_ORIGIN (arg) = arg;
17655 if (DECL_INITIAL (decl) != NULL_TREE
17656 && DECL_INITIAL (decl) != error_mark_node)
17657 set_block_origin_self (DECL_INITIAL (decl));
17662 /* Given a pointer to some BLOCK node, and a boolean value to set the
17663 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17664 the given block, and for all local decls and all local sub-blocks
17665 (recursively) which are contained therein. */
17667 static void
17668 set_block_abstract_flags (tree stmt, int setting)
17670 tree local_decl;
17671 tree subblock;
17672 unsigned int i;
17674 BLOCK_ABSTRACT (stmt) = setting;
17676 for (local_decl = BLOCK_VARS (stmt);
17677 local_decl != NULL_TREE;
17678 local_decl = DECL_CHAIN (local_decl))
17679 if (! DECL_EXTERNAL (local_decl))
17680 set_decl_abstract_flags (local_decl, setting);
17682 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17684 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17685 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17686 || TREE_CODE (local_decl) == PARM_DECL)
17687 set_decl_abstract_flags (local_decl, setting);
17690 for (subblock = BLOCK_SUBBLOCKS (stmt);
17691 subblock != NULL_TREE;
17692 subblock = BLOCK_CHAIN (subblock))
17693 set_block_abstract_flags (subblock, setting);
17696 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17697 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17698 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17699 set the abstract flags for all of the parameters, local vars, local
17700 blocks and sub-blocks (recursively) to the same setting. */
17702 static void
17703 set_decl_abstract_flags (tree decl, int setting)
17705 DECL_ABSTRACT (decl) = setting;
17706 if (TREE_CODE (decl) == FUNCTION_DECL)
17708 tree arg;
17710 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17711 DECL_ABSTRACT (arg) = setting;
17712 if (DECL_INITIAL (decl) != NULL_TREE
17713 && DECL_INITIAL (decl) != error_mark_node)
17714 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17718 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17719 may later generate inlined and/or out-of-line instances of. */
17721 static void
17722 dwarf2out_abstract_function (tree decl)
17724 dw_die_ref old_die;
17725 tree save_fn;
17726 tree context;
17727 int was_abstract;
17728 htab_t old_decl_loc_table;
17729 htab_t old_cached_dw_loc_list_table;
17730 int old_call_site_count, old_tail_call_site_count;
17731 struct call_arg_loc_node *old_call_arg_locations;
17733 /* Make sure we have the actual abstract inline, not a clone. */
17734 decl = DECL_ORIGIN (decl);
17736 old_die = lookup_decl_die (decl);
17737 if (old_die && get_AT (old_die, DW_AT_inline))
17738 /* We've already generated the abstract instance. */
17739 return;
17741 /* We can be called while recursively when seeing block defining inlined subroutine
17742 DIE. Be sure to not clobber the outer location table nor use it or we would
17743 get locations in abstract instantces. */
17744 old_decl_loc_table = decl_loc_table;
17745 decl_loc_table = NULL;
17746 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17747 cached_dw_loc_list_table = NULL;
17748 old_call_arg_locations = call_arg_locations;
17749 call_arg_locations = NULL;
17750 old_call_site_count = call_site_count;
17751 call_site_count = -1;
17752 old_tail_call_site_count = tail_call_site_count;
17753 tail_call_site_count = -1;
17755 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17756 we don't get confused by DECL_ABSTRACT. */
17757 if (debug_info_level > DINFO_LEVEL_TERSE)
17759 context = decl_class_context (decl);
17760 if (context)
17761 gen_type_die_for_member
17762 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17765 /* Pretend we've just finished compiling this function. */
17766 save_fn = current_function_decl;
17767 current_function_decl = decl;
17769 was_abstract = DECL_ABSTRACT (decl);
17770 set_decl_abstract_flags (decl, 1);
17771 dwarf2out_decl (decl);
17772 if (! was_abstract)
17773 set_decl_abstract_flags (decl, 0);
17775 current_function_decl = save_fn;
17776 decl_loc_table = old_decl_loc_table;
17777 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17778 call_arg_locations = old_call_arg_locations;
17779 call_site_count = old_call_site_count;
17780 tail_call_site_count = old_tail_call_site_count;
17783 /* Helper function of premark_used_types() which gets called through
17784 htab_traverse.
17786 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17787 marked as unused by prune_unused_types. */
17789 static int
17790 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17792 tree type;
17793 dw_die_ref die;
17795 type = (tree) *slot;
17796 die = lookup_type_die (type);
17797 if (die != NULL)
17798 die->die_perennial_p = 1;
17799 return 1;
17802 /* Helper function of premark_types_used_by_global_vars which gets called
17803 through htab_traverse.
17805 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17806 marked as unused by prune_unused_types. The DIE of the type is marked
17807 only if the global variable using the type will actually be emitted. */
17809 static int
17810 premark_types_used_by_global_vars_helper (void **slot,
17811 void *data ATTRIBUTE_UNUSED)
17813 struct types_used_by_vars_entry *entry;
17814 dw_die_ref die;
17816 entry = (struct types_used_by_vars_entry *) *slot;
17817 gcc_assert (entry->type != NULL
17818 && entry->var_decl != NULL);
17819 die = lookup_type_die (entry->type);
17820 if (die)
17822 /* Ask cgraph if the global variable really is to be emitted.
17823 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17824 struct varpool_node *node = varpool_get_node (entry->var_decl);
17825 if (node && node->definition)
17827 die->die_perennial_p = 1;
17828 /* Keep the parent DIEs as well. */
17829 while ((die = die->die_parent) && die->die_perennial_p == 0)
17830 die->die_perennial_p = 1;
17833 return 1;
17836 /* Mark all members of used_types_hash as perennial. */
17838 static void
17839 premark_used_types (struct function *fun)
17841 if (fun && fun->used_types_hash)
17842 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17845 /* Mark all members of types_used_by_vars_entry as perennial. */
17847 static void
17848 premark_types_used_by_global_vars (void)
17850 if (types_used_by_vars_hash)
17851 htab_traverse (types_used_by_vars_hash,
17852 premark_types_used_by_global_vars_helper, NULL);
17855 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17856 for CA_LOC call arg loc node. */
17858 static dw_die_ref
17859 gen_call_site_die (tree decl, dw_die_ref subr_die,
17860 struct call_arg_loc_node *ca_loc)
17862 dw_die_ref stmt_die = NULL, die;
17863 tree block = ca_loc->block;
17865 while (block
17866 && block != DECL_INITIAL (decl)
17867 && TREE_CODE (block) == BLOCK)
17869 if (block_map.length () > BLOCK_NUMBER (block))
17870 stmt_die = block_map[BLOCK_NUMBER (block)];
17871 if (stmt_die)
17872 break;
17873 block = BLOCK_SUPERCONTEXT (block);
17875 if (stmt_die == NULL)
17876 stmt_die = subr_die;
17877 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17878 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17879 if (ca_loc->tail_call_p)
17880 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17881 if (ca_loc->symbol_ref)
17883 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17884 if (tdie)
17885 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17886 else
17887 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17889 return die;
17892 /* Generate a DIE to represent a declared function (either file-scope or
17893 block-local). */
17895 static void
17896 gen_subprogram_die (tree decl, dw_die_ref context_die)
17898 tree origin = decl_ultimate_origin (decl);
17899 dw_die_ref subr_die;
17900 tree outer_scope;
17901 dw_die_ref old_die = lookup_decl_die (decl);
17902 int declaration = (current_function_decl != decl
17903 || class_or_namespace_scope_p (context_die));
17905 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17907 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17908 started to generate the abstract instance of an inline, decided to output
17909 its containing class, and proceeded to emit the declaration of the inline
17910 from the member list for the class. If so, DECLARATION takes priority;
17911 we'll get back to the abstract instance when done with the class. */
17913 /* The class-scope declaration DIE must be the primary DIE. */
17914 if (origin && declaration && class_or_namespace_scope_p (context_die))
17916 origin = NULL;
17917 gcc_assert (!old_die);
17920 /* Now that the C++ front end lazily declares artificial member fns, we
17921 might need to retrofit the declaration into its class. */
17922 if (!declaration && !origin && !old_die
17923 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17924 && !class_or_namespace_scope_p (context_die)
17925 && debug_info_level > DINFO_LEVEL_TERSE)
17926 old_die = force_decl_die (decl);
17928 if (origin != NULL)
17930 gcc_assert (!declaration || local_scope_p (context_die));
17932 /* Fixup die_parent for the abstract instance of a nested
17933 inline function. */
17934 if (old_die && old_die->die_parent == NULL)
17935 add_child_die (context_die, old_die);
17937 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17938 add_abstract_origin_attribute (subr_die, origin);
17939 /* This is where the actual code for a cloned function is.
17940 Let's emit linkage name attribute for it. This helps
17941 debuggers to e.g, set breakpoints into
17942 constructors/destructors when the user asks "break
17943 K::K". */
17944 add_linkage_name (subr_die, decl);
17946 else if (old_die)
17948 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17949 struct dwarf_file_data * file_index = lookup_filename (s.file);
17951 if (!get_AT_flag (old_die, DW_AT_declaration)
17952 /* We can have a normal definition following an inline one in the
17953 case of redefinition of GNU C extern inlines.
17954 It seems reasonable to use AT_specification in this case. */
17955 && !get_AT (old_die, DW_AT_inline))
17957 /* Detect and ignore this case, where we are trying to output
17958 something we have already output. */
17959 return;
17962 /* If the definition comes from the same place as the declaration,
17963 maybe use the old DIE. We always want the DIE for this function
17964 that has the *_pc attributes to be under comp_unit_die so the
17965 debugger can find it. We also need to do this for abstract
17966 instances of inlines, since the spec requires the out-of-line copy
17967 to have the same parent. For local class methods, this doesn't
17968 apply; we just use the old DIE. */
17969 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17970 && (DECL_ARTIFICIAL (decl)
17971 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17972 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17973 == (unsigned) s.line))))
17975 subr_die = old_die;
17977 /* Clear out the declaration attribute and the formal parameters.
17978 Do not remove all children, because it is possible that this
17979 declaration die was forced using force_decl_die(). In such
17980 cases die that forced declaration die (e.g. TAG_imported_module)
17981 is one of the children that we do not want to remove. */
17982 remove_AT (subr_die, DW_AT_declaration);
17983 remove_AT (subr_die, DW_AT_object_pointer);
17984 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17986 else
17988 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17989 add_AT_specification (subr_die, old_die);
17990 add_pubname (decl, subr_die);
17991 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17992 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17993 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17994 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17997 else
17999 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18001 if (TREE_PUBLIC (decl))
18002 add_AT_flag (subr_die, DW_AT_external, 1);
18004 add_name_and_src_coords_attributes (subr_die, decl);
18005 add_pubname (decl, subr_die);
18006 if (debug_info_level > DINFO_LEVEL_TERSE)
18008 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18009 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18010 0, 0, context_die);
18013 add_pure_or_virtual_attribute (subr_die, decl);
18014 if (DECL_ARTIFICIAL (decl))
18015 add_AT_flag (subr_die, DW_AT_artificial, 1);
18017 add_accessibility_attribute (subr_die, decl);
18020 if (declaration)
18022 if (!old_die || !get_AT (old_die, DW_AT_inline))
18024 add_AT_flag (subr_die, DW_AT_declaration, 1);
18026 /* If this is an explicit function declaration then generate
18027 a DW_AT_explicit attribute. */
18028 if (lang_hooks.decls.function_decl_explicit_p (decl)
18029 && (dwarf_version >= 3 || !dwarf_strict))
18030 add_AT_flag (subr_die, DW_AT_explicit, 1);
18032 /* The first time we see a member function, it is in the context of
18033 the class to which it belongs. We make sure of this by emitting
18034 the class first. The next time is the definition, which is
18035 handled above. The two may come from the same source text.
18037 Note that force_decl_die() forces function declaration die. It is
18038 later reused to represent definition. */
18039 equate_decl_number_to_die (decl, subr_die);
18042 else if (DECL_ABSTRACT (decl))
18044 if (DECL_DECLARED_INLINE_P (decl))
18046 if (cgraph_function_possibly_inlined_p (decl))
18047 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18048 else
18049 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18051 else
18053 if (cgraph_function_possibly_inlined_p (decl))
18054 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18055 else
18056 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18059 if (DECL_DECLARED_INLINE_P (decl)
18060 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18061 add_AT_flag (subr_die, DW_AT_artificial, 1);
18063 equate_decl_number_to_die (decl, subr_die);
18065 else if (!DECL_EXTERNAL (decl))
18067 HOST_WIDE_INT cfa_fb_offset;
18068 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18070 if (!old_die || !get_AT (old_die, DW_AT_inline))
18071 equate_decl_number_to_die (decl, subr_die);
18073 gcc_checking_assert (fun);
18074 if (!flag_reorder_blocks_and_partition)
18076 dw_fde_ref fde = fun->fde;
18077 if (fde->dw_fde_begin)
18079 /* We have already generated the labels. */
18080 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18081 fde->dw_fde_end, false);
18083 else
18085 /* Create start/end labels and add the range. */
18086 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18087 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18088 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18089 current_function_funcdef_no);
18090 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18091 current_function_funcdef_no);
18092 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18093 false);
18096 #if VMS_DEBUGGING_INFO
18097 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18098 Section 2.3 Prologue and Epilogue Attributes:
18099 When a breakpoint is set on entry to a function, it is generally
18100 desirable for execution to be suspended, not on the very first
18101 instruction of the function, but rather at a point after the
18102 function's frame has been set up, after any language defined local
18103 declaration processing has been completed, and before execution of
18104 the first statement of the function begins. Debuggers generally
18105 cannot properly determine where this point is. Similarly for a
18106 breakpoint set on exit from a function. The prologue and epilogue
18107 attributes allow a compiler to communicate the location(s) to use. */
18110 if (fde->dw_fde_vms_end_prologue)
18111 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18112 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18114 if (fde->dw_fde_vms_begin_epilogue)
18115 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18116 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18118 #endif
18121 else
18123 /* Generate pubnames entries for the split function code ranges. */
18124 dw_fde_ref fde = fun->fde;
18126 if (fde->dw_fde_second_begin)
18128 if (dwarf_version >= 3 || !dwarf_strict)
18130 /* We should use ranges for non-contiguous code section
18131 addresses. Use the actual code range for the initial
18132 section, since the HOT/COLD labels might precede an
18133 alignment offset. */
18134 bool range_list_added = false;
18135 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18136 fde->dw_fde_end, &range_list_added,
18137 false);
18138 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18139 fde->dw_fde_second_end,
18140 &range_list_added, false);
18141 if (range_list_added)
18142 add_ranges (NULL);
18144 else
18146 /* There is no real support in DW2 for this .. so we make
18147 a work-around. First, emit the pub name for the segment
18148 containing the function label. Then make and emit a
18149 simplified subprogram DIE for the second segment with the
18150 name pre-fixed by __hot/cold_sect_of_. We use the same
18151 linkage name for the second die so that gdb will find both
18152 sections when given "b foo". */
18153 const char *name = NULL;
18154 tree decl_name = DECL_NAME (decl);
18155 dw_die_ref seg_die;
18157 /* Do the 'primary' section. */
18158 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18159 fde->dw_fde_end, false);
18161 /* Build a minimal DIE for the secondary section. */
18162 seg_die = new_die (DW_TAG_subprogram,
18163 subr_die->die_parent, decl);
18165 if (TREE_PUBLIC (decl))
18166 add_AT_flag (seg_die, DW_AT_external, 1);
18168 if (decl_name != NULL
18169 && IDENTIFIER_POINTER (decl_name) != NULL)
18171 name = dwarf2_name (decl, 1);
18172 if (! DECL_ARTIFICIAL (decl))
18173 add_src_coords_attributes (seg_die, decl);
18175 add_linkage_name (seg_die, decl);
18177 gcc_assert (name != NULL);
18178 add_pure_or_virtual_attribute (seg_die, decl);
18179 if (DECL_ARTIFICIAL (decl))
18180 add_AT_flag (seg_die, DW_AT_artificial, 1);
18182 name = concat ("__second_sect_of_", name, NULL);
18183 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18184 fde->dw_fde_second_end, false);
18185 add_name_attribute (seg_die, name);
18186 if (want_pubnames ())
18187 add_pubname_string (name, seg_die);
18190 else
18191 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18192 false);
18195 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18197 /* We define the "frame base" as the function's CFA. This is more
18198 convenient for several reasons: (1) It's stable across the prologue
18199 and epilogue, which makes it better than just a frame pointer,
18200 (2) With dwarf3, there exists a one-byte encoding that allows us
18201 to reference the .debug_frame data by proxy, but failing that,
18202 (3) We can at least reuse the code inspection and interpretation
18203 code that determines the CFA position at various points in the
18204 function. */
18205 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18207 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18208 add_AT_loc (subr_die, DW_AT_frame_base, op);
18210 else
18212 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18213 if (list->dw_loc_next)
18214 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18215 else
18216 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18219 /* Compute a displacement from the "steady-state frame pointer" to
18220 the CFA. The former is what all stack slots and argument slots
18221 will reference in the rtl; the latter is what we've told the
18222 debugger about. We'll need to adjust all frame_base references
18223 by this displacement. */
18224 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18226 if (fun->static_chain_decl)
18227 add_AT_location_description (subr_die, DW_AT_static_link,
18228 loc_list_from_tree (fun->static_chain_decl, 2));
18231 /* Generate child dies for template paramaters. */
18232 if (debug_info_level > DINFO_LEVEL_TERSE)
18233 gen_generic_params_dies (decl);
18235 /* Now output descriptions of the arguments for this function. This gets
18236 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18237 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18238 `...' at the end of the formal parameter list. In order to find out if
18239 there was a trailing ellipsis or not, we must instead look at the type
18240 associated with the FUNCTION_DECL. This will be a node of type
18241 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18242 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18243 an ellipsis at the end. */
18245 /* In the case where we are describing a mere function declaration, all we
18246 need to do here (and all we *can* do here) is to describe the *types* of
18247 its formal parameters. */
18248 if (debug_info_level <= DINFO_LEVEL_TERSE)
18250 else if (declaration)
18251 gen_formal_types_die (decl, subr_die);
18252 else
18254 /* Generate DIEs to represent all known formal parameters. */
18255 tree parm = DECL_ARGUMENTS (decl);
18256 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18257 tree generic_decl_parm = generic_decl
18258 ? DECL_ARGUMENTS (generic_decl)
18259 : NULL;
18261 /* Now we want to walk the list of parameters of the function and
18262 emit their relevant DIEs.
18264 We consider the case of DECL being an instance of a generic function
18265 as well as it being a normal function.
18267 If DECL is an instance of a generic function we walk the
18268 parameters of the generic function declaration _and_ the parameters of
18269 DECL itself. This is useful because we want to emit specific DIEs for
18270 function parameter packs and those are declared as part of the
18271 generic function declaration. In that particular case,
18272 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18273 That DIE has children DIEs representing the set of arguments
18274 of the pack. Note that the set of pack arguments can be empty.
18275 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18276 children DIE.
18278 Otherwise, we just consider the parameters of DECL. */
18279 while (generic_decl_parm || parm)
18281 if (generic_decl_parm
18282 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18283 gen_formal_parameter_pack_die (generic_decl_parm,
18284 parm, subr_die,
18285 &parm);
18286 else if (parm)
18288 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18290 if (parm == DECL_ARGUMENTS (decl)
18291 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18292 && parm_die
18293 && (dwarf_version >= 3 || !dwarf_strict))
18294 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18296 parm = DECL_CHAIN (parm);
18299 if (generic_decl_parm)
18300 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18303 /* Decide whether we need an unspecified_parameters DIE at the end.
18304 There are 2 more cases to do this for: 1) the ansi ... declaration -
18305 this is detectable when the end of the arg list is not a
18306 void_type_node 2) an unprototyped function declaration (not a
18307 definition). This just means that we have no info about the
18308 parameters at all. */
18309 if (prototype_p (TREE_TYPE (decl)))
18311 /* This is the prototyped case, check for.... */
18312 if (stdarg_p (TREE_TYPE (decl)))
18313 gen_unspecified_parameters_die (decl, subr_die);
18315 else if (DECL_INITIAL (decl) == NULL_TREE)
18316 gen_unspecified_parameters_die (decl, subr_die);
18319 /* Output Dwarf info for all of the stuff within the body of the function
18320 (if it has one - it may be just a declaration). */
18321 outer_scope = DECL_INITIAL (decl);
18323 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18324 a function. This BLOCK actually represents the outermost binding contour
18325 for the function, i.e. the contour in which the function's formal
18326 parameters and labels get declared. Curiously, it appears that the front
18327 end doesn't actually put the PARM_DECL nodes for the current function onto
18328 the BLOCK_VARS list for this outer scope, but are strung off of the
18329 DECL_ARGUMENTS list for the function instead.
18331 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18332 the LABEL_DECL nodes for the function however, and we output DWARF info
18333 for those in decls_for_scope. Just within the `outer_scope' there will be
18334 a BLOCK node representing the function's outermost pair of curly braces,
18335 and any blocks used for the base and member initializers of a C++
18336 constructor function. */
18337 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18339 int call_site_note_count = 0;
18340 int tail_call_site_note_count = 0;
18342 /* Emit a DW_TAG_variable DIE for a named return value. */
18343 if (DECL_NAME (DECL_RESULT (decl)))
18344 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18346 current_function_has_inlines = 0;
18347 decls_for_scope (outer_scope, subr_die, 0);
18349 if (call_arg_locations && !dwarf_strict)
18351 struct call_arg_loc_node *ca_loc;
18352 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18354 dw_die_ref die = NULL;
18355 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18356 rtx arg, next_arg;
18358 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18359 arg; arg = next_arg)
18361 dw_loc_descr_ref reg, val;
18362 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18363 dw_die_ref cdie, tdie = NULL;
18365 next_arg = XEXP (arg, 1);
18366 if (REG_P (XEXP (XEXP (arg, 0), 0))
18367 && next_arg
18368 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18369 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18370 && REGNO (XEXP (XEXP (arg, 0), 0))
18371 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18372 next_arg = XEXP (next_arg, 1);
18373 if (mode == VOIDmode)
18375 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18376 if (mode == VOIDmode)
18377 mode = GET_MODE (XEXP (arg, 0));
18379 if (mode == VOIDmode || mode == BLKmode)
18380 continue;
18381 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18383 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18384 tloc = XEXP (XEXP (arg, 0), 1);
18385 continue;
18387 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18388 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18390 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18391 tlocc = XEXP (XEXP (arg, 0), 1);
18392 continue;
18394 reg = NULL;
18395 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18396 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18397 VAR_INIT_STATUS_INITIALIZED);
18398 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18400 rtx mem = XEXP (XEXP (arg, 0), 0);
18401 reg = mem_loc_descriptor (XEXP (mem, 0),
18402 get_address_mode (mem),
18403 GET_MODE (mem),
18404 VAR_INIT_STATUS_INITIALIZED);
18406 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18407 == DEBUG_PARAMETER_REF)
18409 tree tdecl
18410 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18411 tdie = lookup_decl_die (tdecl);
18412 if (tdie == NULL)
18413 continue;
18415 else
18416 continue;
18417 if (reg == NULL
18418 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18419 != DEBUG_PARAMETER_REF)
18420 continue;
18421 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18422 VOIDmode,
18423 VAR_INIT_STATUS_INITIALIZED);
18424 if (val == NULL)
18425 continue;
18426 if (die == NULL)
18427 die = gen_call_site_die (decl, subr_die, ca_loc);
18428 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18429 NULL_TREE);
18430 if (reg != NULL)
18431 add_AT_loc (cdie, DW_AT_location, reg);
18432 else if (tdie != NULL)
18433 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18434 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18435 if (next_arg != XEXP (arg, 1))
18437 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18438 if (mode == VOIDmode)
18439 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18440 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18441 0), 1),
18442 mode, VOIDmode,
18443 VAR_INIT_STATUS_INITIALIZED);
18444 if (val != NULL)
18445 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18448 if (die == NULL
18449 && (ca_loc->symbol_ref || tloc))
18450 die = gen_call_site_die (decl, subr_die, ca_loc);
18451 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18453 dw_loc_descr_ref tval = NULL;
18455 if (tloc != NULL_RTX)
18456 tval = mem_loc_descriptor (tloc,
18457 GET_MODE (tloc) == VOIDmode
18458 ? Pmode : GET_MODE (tloc),
18459 VOIDmode,
18460 VAR_INIT_STATUS_INITIALIZED);
18461 if (tval)
18462 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18463 else if (tlocc != NULL_RTX)
18465 tval = mem_loc_descriptor (tlocc,
18466 GET_MODE (tlocc) == VOIDmode
18467 ? Pmode : GET_MODE (tlocc),
18468 VOIDmode,
18469 VAR_INIT_STATUS_INITIALIZED);
18470 if (tval)
18471 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18472 tval);
18475 if (die != NULL)
18477 call_site_note_count++;
18478 if (ca_loc->tail_call_p)
18479 tail_call_site_note_count++;
18483 call_arg_locations = NULL;
18484 call_arg_loc_last = NULL;
18485 if (tail_call_site_count >= 0
18486 && tail_call_site_count == tail_call_site_note_count
18487 && !dwarf_strict)
18489 if (call_site_count >= 0
18490 && call_site_count == call_site_note_count)
18491 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18492 else
18493 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18495 call_site_count = -1;
18496 tail_call_site_count = -1;
18498 /* Add the calling convention attribute if requested. */
18499 add_calling_convention_attribute (subr_die, decl);
18503 /* Returns a hash value for X (which really is a die_struct). */
18505 static hashval_t
18506 common_block_die_table_hash (const void *x)
18508 const_dw_die_ref d = (const_dw_die_ref) x;
18509 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18512 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18513 as decl_id and die_parent of die_struct Y. */
18515 static int
18516 common_block_die_table_eq (const void *x, const void *y)
18518 const_dw_die_ref d = (const_dw_die_ref) x;
18519 const_dw_die_ref e = (const_dw_die_ref) y;
18520 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18523 /* Generate a DIE to represent a declared data object.
18524 Either DECL or ORIGIN must be non-null. */
18526 static void
18527 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18529 HOST_WIDE_INT off = 0;
18530 tree com_decl;
18531 tree decl_or_origin = decl ? decl : origin;
18532 tree ultimate_origin;
18533 dw_die_ref var_die;
18534 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18535 dw_die_ref origin_die;
18536 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18537 || class_or_namespace_scope_p (context_die));
18538 bool specialization_p = false;
18540 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18541 if (decl || ultimate_origin)
18542 origin = ultimate_origin;
18543 com_decl = fortran_common (decl_or_origin, &off);
18545 /* Symbol in common gets emitted as a child of the common block, in the form
18546 of a data member. */
18547 if (com_decl)
18549 dw_die_ref com_die;
18550 dw_loc_list_ref loc;
18551 die_node com_die_arg;
18553 var_die = lookup_decl_die (decl_or_origin);
18554 if (var_die)
18556 if (get_AT (var_die, DW_AT_location) == NULL)
18558 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18559 if (loc)
18561 if (off)
18563 /* Optimize the common case. */
18564 if (single_element_loc_list_p (loc)
18565 && loc->expr->dw_loc_opc == DW_OP_addr
18566 && loc->expr->dw_loc_next == NULL
18567 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18568 == SYMBOL_REF)
18570 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18571 loc->expr->dw_loc_oprnd1.v.val_addr
18572 = plus_constant (GET_MODE (x), x , off);
18574 else
18575 loc_list_plus_const (loc, off);
18577 add_AT_location_description (var_die, DW_AT_location, loc);
18578 remove_AT (var_die, DW_AT_declaration);
18581 return;
18584 if (common_block_die_table == NULL)
18585 common_block_die_table
18586 = htab_create_ggc (10, common_block_die_table_hash,
18587 common_block_die_table_eq, NULL);
18589 com_die_arg.decl_id = DECL_UID (com_decl);
18590 com_die_arg.die_parent = context_die;
18591 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18592 loc = loc_list_from_tree (com_decl, 2);
18593 if (com_die == NULL)
18595 const char *cnam
18596 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18597 void **slot;
18599 com_die = new_die (DW_TAG_common_block, context_die, decl);
18600 add_name_and_src_coords_attributes (com_die, com_decl);
18601 if (loc)
18603 add_AT_location_description (com_die, DW_AT_location, loc);
18604 /* Avoid sharing the same loc descriptor between
18605 DW_TAG_common_block and DW_TAG_variable. */
18606 loc = loc_list_from_tree (com_decl, 2);
18608 else if (DECL_EXTERNAL (decl))
18609 add_AT_flag (com_die, DW_AT_declaration, 1);
18610 if (want_pubnames ())
18611 add_pubname_string (cnam, com_die); /* ??? needed? */
18612 com_die->decl_id = DECL_UID (com_decl);
18613 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18614 *slot = (void *) com_die;
18616 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18618 add_AT_location_description (com_die, DW_AT_location, loc);
18619 loc = loc_list_from_tree (com_decl, 2);
18620 remove_AT (com_die, DW_AT_declaration);
18622 var_die = new_die (DW_TAG_variable, com_die, decl);
18623 add_name_and_src_coords_attributes (var_die, decl);
18624 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18625 TREE_THIS_VOLATILE (decl), context_die);
18626 add_AT_flag (var_die, DW_AT_external, 1);
18627 if (loc)
18629 if (off)
18631 /* Optimize the common case. */
18632 if (single_element_loc_list_p (loc)
18633 && loc->expr->dw_loc_opc == DW_OP_addr
18634 && loc->expr->dw_loc_next == NULL
18635 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18637 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18638 loc->expr->dw_loc_oprnd1.v.val_addr
18639 = plus_constant (GET_MODE (x), x, off);
18641 else
18642 loc_list_plus_const (loc, off);
18644 add_AT_location_description (var_die, DW_AT_location, loc);
18646 else if (DECL_EXTERNAL (decl))
18647 add_AT_flag (var_die, DW_AT_declaration, 1);
18648 equate_decl_number_to_die (decl, var_die);
18649 return;
18652 /* If the compiler emitted a definition for the DECL declaration
18653 and if we already emitted a DIE for it, don't emit a second
18654 DIE for it again. Allow re-declarations of DECLs that are
18655 inside functions, though. */
18656 if (old_die && declaration && !local_scope_p (context_die))
18657 return;
18659 /* For static data members, the declaration in the class is supposed
18660 to have DW_TAG_member tag; the specification should still be
18661 DW_TAG_variable referencing the DW_TAG_member DIE. */
18662 if (declaration && class_scope_p (context_die))
18663 var_die = new_die (DW_TAG_member, context_die, decl);
18664 else
18665 var_die = new_die (DW_TAG_variable, context_die, decl);
18667 origin_die = NULL;
18668 if (origin != NULL)
18669 origin_die = add_abstract_origin_attribute (var_die, origin);
18671 /* Loop unrolling can create multiple blocks that refer to the same
18672 static variable, so we must test for the DW_AT_declaration flag.
18674 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18675 copy decls and set the DECL_ABSTRACT flag on them instead of
18676 sharing them.
18678 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18680 ??? The declare_in_namespace support causes us to get two DIEs for one
18681 variable, both of which are declarations. We want to avoid considering
18682 one to be a specification, so we must test that this DIE is not a
18683 declaration. */
18684 else if (old_die && TREE_STATIC (decl) && ! declaration
18685 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18687 /* This is a definition of a C++ class level static. */
18688 add_AT_specification (var_die, old_die);
18689 specialization_p = true;
18690 if (DECL_NAME (decl))
18692 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18693 struct dwarf_file_data * file_index = lookup_filename (s.file);
18695 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18696 add_AT_file (var_die, DW_AT_decl_file, file_index);
18698 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18699 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18701 if (old_die->die_tag == DW_TAG_member)
18702 add_linkage_name (var_die, decl);
18705 else
18706 add_name_and_src_coords_attributes (var_die, decl);
18708 if ((origin == NULL && !specialization_p)
18709 || (origin != NULL
18710 && !DECL_ABSTRACT (decl_or_origin)
18711 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18712 decl_function_context
18713 (decl_or_origin))))
18715 tree type = TREE_TYPE (decl_or_origin);
18717 if (decl_by_reference_p (decl_or_origin))
18718 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18719 else
18720 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18721 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18724 if (origin == NULL && !specialization_p)
18726 if (TREE_PUBLIC (decl))
18727 add_AT_flag (var_die, DW_AT_external, 1);
18729 if (DECL_ARTIFICIAL (decl))
18730 add_AT_flag (var_die, DW_AT_artificial, 1);
18732 add_accessibility_attribute (var_die, decl);
18735 if (declaration)
18736 add_AT_flag (var_die, DW_AT_declaration, 1);
18738 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18739 equate_decl_number_to_die (decl, var_die);
18741 if (! declaration
18742 && (! DECL_ABSTRACT (decl_or_origin)
18743 /* Local static vars are shared between all clones/inlines,
18744 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18745 already set. */
18746 || (TREE_CODE (decl_or_origin) == VAR_DECL
18747 && TREE_STATIC (decl_or_origin)
18748 && DECL_RTL_SET_P (decl_or_origin)))
18749 /* When abstract origin already has DW_AT_location attribute, no need
18750 to add it again. */
18751 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18753 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18754 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18755 defer_location (decl_or_origin, var_die);
18756 else
18757 add_location_or_const_value_attribute (var_die, decl_or_origin,
18758 decl == NULL, DW_AT_location);
18759 add_pubname (decl_or_origin, var_die);
18761 else
18762 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18765 /* Generate a DIE to represent a named constant. */
18767 static void
18768 gen_const_die (tree decl, dw_die_ref context_die)
18770 dw_die_ref const_die;
18771 tree type = TREE_TYPE (decl);
18773 const_die = new_die (DW_TAG_constant, context_die, decl);
18774 add_name_and_src_coords_attributes (const_die, decl);
18775 add_type_attribute (const_die, type, 1, 0, context_die);
18776 if (TREE_PUBLIC (decl))
18777 add_AT_flag (const_die, DW_AT_external, 1);
18778 if (DECL_ARTIFICIAL (decl))
18779 add_AT_flag (const_die, DW_AT_artificial, 1);
18780 tree_add_const_value_attribute_for_decl (const_die, decl);
18783 /* Generate a DIE to represent a label identifier. */
18785 static void
18786 gen_label_die (tree decl, dw_die_ref context_die)
18788 tree origin = decl_ultimate_origin (decl);
18789 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18790 rtx insn;
18791 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18793 if (origin != NULL)
18794 add_abstract_origin_attribute (lbl_die, origin);
18795 else
18796 add_name_and_src_coords_attributes (lbl_die, decl);
18798 if (DECL_ABSTRACT (decl))
18799 equate_decl_number_to_die (decl, lbl_die);
18800 else
18802 insn = DECL_RTL_IF_SET (decl);
18804 /* Deleted labels are programmer specified labels which have been
18805 eliminated because of various optimizations. We still emit them
18806 here so that it is possible to put breakpoints on them. */
18807 if (insn
18808 && (LABEL_P (insn)
18809 || ((NOTE_P (insn)
18810 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18812 /* When optimization is enabled (via -O) some parts of the compiler
18813 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18814 represent source-level labels which were explicitly declared by
18815 the user. This really shouldn't be happening though, so catch
18816 it if it ever does happen. */
18817 gcc_assert (!INSN_DELETED_P (insn));
18819 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18820 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18822 else if (insn
18823 && NOTE_P (insn)
18824 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18825 && CODE_LABEL_NUMBER (insn) != -1)
18827 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18828 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18833 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18834 attributes to the DIE for a block STMT, to describe where the inlined
18835 function was called from. This is similar to add_src_coords_attributes. */
18837 static inline void
18838 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18840 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18842 if (dwarf_version >= 3 || !dwarf_strict)
18844 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18845 add_AT_unsigned (die, DW_AT_call_line, s.line);
18850 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18851 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18853 static inline void
18854 add_high_low_attributes (tree stmt, dw_die_ref die)
18856 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18858 if (BLOCK_FRAGMENT_CHAIN (stmt)
18859 && (dwarf_version >= 3 || !dwarf_strict))
18861 tree chain, superblock = NULL_TREE;
18862 dw_die_ref pdie;
18863 dw_attr_ref attr = NULL;
18865 if (inlined_function_outer_scope_p (stmt))
18867 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18868 BLOCK_NUMBER (stmt));
18869 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18872 /* Optimize duplicate .debug_ranges lists or even tails of
18873 lists. If this BLOCK has same ranges as its supercontext,
18874 lookup DW_AT_ranges attribute in the supercontext (and
18875 recursively so), verify that the ranges_table contains the
18876 right values and use it instead of adding a new .debug_range. */
18877 for (chain = stmt, pdie = die;
18878 BLOCK_SAME_RANGE (chain);
18879 chain = BLOCK_SUPERCONTEXT (chain))
18881 dw_attr_ref new_attr;
18883 pdie = pdie->die_parent;
18884 if (pdie == NULL)
18885 break;
18886 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18887 break;
18888 new_attr = get_AT (pdie, DW_AT_ranges);
18889 if (new_attr == NULL
18890 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18891 break;
18892 attr = new_attr;
18893 superblock = BLOCK_SUPERCONTEXT (chain);
18895 if (attr != NULL
18896 && (ranges_table[attr->dw_attr_val.v.val_offset
18897 / 2 / DWARF2_ADDR_SIZE].num
18898 == BLOCK_NUMBER (superblock))
18899 && BLOCK_FRAGMENT_CHAIN (superblock))
18901 unsigned long off = attr->dw_attr_val.v.val_offset
18902 / 2 / DWARF2_ADDR_SIZE;
18903 unsigned long supercnt = 0, thiscnt = 0;
18904 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18905 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18907 ++supercnt;
18908 gcc_checking_assert (ranges_table[off + supercnt].num
18909 == BLOCK_NUMBER (chain));
18911 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18912 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18913 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18914 ++thiscnt;
18915 gcc_assert (supercnt >= thiscnt);
18916 add_AT_range_list (die, DW_AT_ranges,
18917 ((off + supercnt - thiscnt)
18918 * 2 * DWARF2_ADDR_SIZE),
18919 false);
18920 return;
18923 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18925 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18928 add_ranges (chain);
18929 chain = BLOCK_FRAGMENT_CHAIN (chain);
18931 while (chain);
18932 add_ranges (NULL);
18934 else
18936 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18937 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18938 BLOCK_NUMBER (stmt));
18939 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18940 BLOCK_NUMBER (stmt));
18941 add_AT_low_high_pc (die, label, label_high, false);
18945 /* Generate a DIE for a lexical block. */
18947 static void
18948 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18950 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18952 if (call_arg_locations)
18954 if (block_map.length () <= BLOCK_NUMBER (stmt))
18955 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18956 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18959 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18960 add_high_low_attributes (stmt, stmt_die);
18962 decls_for_scope (stmt, stmt_die, depth);
18965 /* Generate a DIE for an inlined subprogram. */
18967 static void
18968 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18970 tree decl;
18972 /* The instance of function that is effectively being inlined shall not
18973 be abstract. */
18974 gcc_assert (! BLOCK_ABSTRACT (stmt));
18976 decl = block_ultimate_origin (stmt);
18978 /* Emit info for the abstract instance first, if we haven't yet. We
18979 must emit this even if the block is abstract, otherwise when we
18980 emit the block below (or elsewhere), we may end up trying to emit
18981 a die whose origin die hasn't been emitted, and crashing. */
18982 dwarf2out_abstract_function (decl);
18984 if (! BLOCK_ABSTRACT (stmt))
18986 dw_die_ref subr_die
18987 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18989 if (call_arg_locations)
18991 if (block_map.length () <= BLOCK_NUMBER (stmt))
18992 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18993 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18995 add_abstract_origin_attribute (subr_die, decl);
18996 if (TREE_ASM_WRITTEN (stmt))
18997 add_high_low_attributes (stmt, subr_die);
18998 add_call_src_coords_attributes (stmt, subr_die);
19000 decls_for_scope (stmt, subr_die, depth);
19001 current_function_has_inlines = 1;
19005 /* Generate a DIE for a field in a record, or structure. */
19007 static void
19008 gen_field_die (tree decl, dw_die_ref context_die)
19010 dw_die_ref decl_die;
19012 if (TREE_TYPE (decl) == error_mark_node)
19013 return;
19015 decl_die = new_die (DW_TAG_member, context_die, decl);
19016 add_name_and_src_coords_attributes (decl_die, decl);
19017 add_type_attribute (decl_die, member_declared_type (decl),
19018 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19019 context_die);
19021 if (DECL_BIT_FIELD_TYPE (decl))
19023 add_byte_size_attribute (decl_die, decl);
19024 add_bit_size_attribute (decl_die, decl);
19025 add_bit_offset_attribute (decl_die, decl);
19028 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19029 add_data_member_location_attribute (decl_die, decl);
19031 if (DECL_ARTIFICIAL (decl))
19032 add_AT_flag (decl_die, DW_AT_artificial, 1);
19034 add_accessibility_attribute (decl_die, decl);
19036 /* Equate decl number to die, so that we can look up this decl later on. */
19037 equate_decl_number_to_die (decl, decl_die);
19040 #if 0
19041 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19042 Use modified_type_die instead.
19043 We keep this code here just in case these types of DIEs may be needed to
19044 represent certain things in other languages (e.g. Pascal) someday. */
19046 static void
19047 gen_pointer_type_die (tree type, dw_die_ref context_die)
19049 dw_die_ref ptr_die
19050 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19052 equate_type_number_to_die (type, ptr_die);
19053 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19054 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19057 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19058 Use modified_type_die instead.
19059 We keep this code here just in case these types of DIEs may be needed to
19060 represent certain things in other languages (e.g. Pascal) someday. */
19062 static void
19063 gen_reference_type_die (tree type, dw_die_ref context_die)
19065 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19067 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19068 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19069 else
19070 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19072 equate_type_number_to_die (type, ref_die);
19073 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19074 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19076 #endif
19078 /* Generate a DIE for a pointer to a member type. */
19080 static void
19081 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19083 dw_die_ref ptr_die
19084 = new_die (DW_TAG_ptr_to_member_type,
19085 scope_die_for (type, context_die), type);
19087 equate_type_number_to_die (type, ptr_die);
19088 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19089 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19090 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19093 typedef const char *dchar_p; /* For DEF_VEC_P. */
19095 static char *producer_string;
19097 /* Return a heap allocated producer string including command line options
19098 if -grecord-gcc-switches. */
19100 static char *
19101 gen_producer_string (void)
19103 size_t j;
19104 vec<dchar_p> switches = vNULL;
19105 const char *language_string = lang_hooks.name;
19106 char *producer, *tail;
19107 const char *p;
19108 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19109 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19111 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19112 switch (save_decoded_options[j].opt_index)
19114 case OPT_o:
19115 case OPT_d:
19116 case OPT_dumpbase:
19117 case OPT_dumpdir:
19118 case OPT_auxbase:
19119 case OPT_auxbase_strip:
19120 case OPT_quiet:
19121 case OPT_version:
19122 case OPT_v:
19123 case OPT_w:
19124 case OPT_L:
19125 case OPT_D:
19126 case OPT_I:
19127 case OPT_U:
19128 case OPT_SPECIAL_unknown:
19129 case OPT_SPECIAL_ignore:
19130 case OPT_SPECIAL_program_name:
19131 case OPT_SPECIAL_input_file:
19132 case OPT_grecord_gcc_switches:
19133 case OPT_gno_record_gcc_switches:
19134 case OPT__output_pch_:
19135 case OPT_fdiagnostics_show_location_:
19136 case OPT_fdiagnostics_show_option:
19137 case OPT_fdiagnostics_show_caret:
19138 case OPT_fdiagnostics_color_:
19139 case OPT_fverbose_asm:
19140 case OPT____:
19141 case OPT__sysroot_:
19142 case OPT_nostdinc:
19143 case OPT_nostdinc__:
19144 /* Ignore these. */
19145 continue;
19146 default:
19147 if (cl_options[save_decoded_options[j].opt_index].flags
19148 & CL_NO_DWARF_RECORD)
19149 continue;
19150 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19151 == '-');
19152 switch (save_decoded_options[j].canonical_option[0][1])
19154 case 'M':
19155 case 'i':
19156 case 'W':
19157 continue;
19158 case 'f':
19159 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19160 "dump", 4) == 0)
19161 continue;
19162 break;
19163 default:
19164 break;
19166 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19167 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19168 break;
19171 producer = XNEWVEC (char, plen + 1 + len + 1);
19172 tail = producer;
19173 sprintf (tail, "%s %s", language_string, version_string);
19174 tail += plen;
19176 FOR_EACH_VEC_ELT (switches, j, p)
19178 len = strlen (p);
19179 *tail = ' ';
19180 memcpy (tail + 1, p, len);
19181 tail += len + 1;
19184 *tail = '\0';
19185 switches.release ();
19186 return producer;
19189 /* Generate the DIE for the compilation unit. */
19191 static dw_die_ref
19192 gen_compile_unit_die (const char *filename)
19194 dw_die_ref die;
19195 const char *language_string = lang_hooks.name;
19196 int language;
19198 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19200 if (filename)
19202 add_name_attribute (die, filename);
19203 /* Don't add cwd for <built-in>. */
19204 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19205 add_comp_dir_attribute (die);
19208 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19210 /* If our producer is LTO try to figure out a common language to use
19211 from the global list of translation units. */
19212 if (strcmp (language_string, "GNU GIMPLE") == 0)
19214 unsigned i;
19215 tree t;
19216 const char *common_lang = NULL;
19218 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19220 if (!TRANSLATION_UNIT_LANGUAGE (t))
19221 continue;
19222 if (!common_lang)
19223 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19224 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19226 else if (strncmp (common_lang, "GNU C", 5) == 0
19227 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19228 /* Mixing C and C++ is ok, use C++ in that case. */
19229 common_lang = "GNU C++";
19230 else
19232 /* Fall back to C. */
19233 common_lang = NULL;
19234 break;
19238 if (common_lang)
19239 language_string = common_lang;
19242 language = DW_LANG_C89;
19243 if (strcmp (language_string, "GNU C++") == 0)
19244 language = DW_LANG_C_plus_plus;
19245 else if (strcmp (language_string, "GNU F77") == 0)
19246 language = DW_LANG_Fortran77;
19247 else if (strcmp (language_string, "GNU Pascal") == 0)
19248 language = DW_LANG_Pascal83;
19249 else if (dwarf_version >= 3 || !dwarf_strict)
19251 if (strcmp (language_string, "GNU Ada") == 0)
19252 language = DW_LANG_Ada95;
19253 else if (strcmp (language_string, "GNU Fortran") == 0)
19254 language = DW_LANG_Fortran95;
19255 else if (strcmp (language_string, "GNU Java") == 0)
19256 language = DW_LANG_Java;
19257 else if (strcmp (language_string, "GNU Objective-C") == 0)
19258 language = DW_LANG_ObjC;
19259 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19260 language = DW_LANG_ObjC_plus_plus;
19261 else if (dwarf_version >= 5 || !dwarf_strict)
19263 if (strcmp (language_string, "GNU Go") == 0)
19264 language = DW_LANG_Go;
19267 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19268 else if (strcmp (language_string, "GNU Fortran") == 0)
19269 language = DW_LANG_Fortran90;
19271 add_AT_unsigned (die, DW_AT_language, language);
19273 switch (language)
19275 case DW_LANG_Fortran77:
19276 case DW_LANG_Fortran90:
19277 case DW_LANG_Fortran95:
19278 /* Fortran has case insensitive identifiers and the front-end
19279 lowercases everything. */
19280 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19281 break;
19282 default:
19283 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19284 break;
19286 return die;
19289 /* Generate the DIE for a base class. */
19291 static void
19292 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19294 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19296 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19297 add_data_member_location_attribute (die, binfo);
19299 if (BINFO_VIRTUAL_P (binfo))
19300 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19302 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19303 children, otherwise the default is DW_ACCESS_public. In DWARF2
19304 the default has always been DW_ACCESS_private. */
19305 if (access == access_public_node)
19307 if (dwarf_version == 2
19308 || context_die->die_tag == DW_TAG_class_type)
19309 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19311 else if (access == access_protected_node)
19312 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19313 else if (dwarf_version > 2
19314 && context_die->die_tag != DW_TAG_class_type)
19315 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19318 /* Generate a DIE for a class member. */
19320 static void
19321 gen_member_die (tree type, dw_die_ref context_die)
19323 tree member;
19324 tree binfo = TYPE_BINFO (type);
19325 dw_die_ref child;
19327 /* If this is not an incomplete type, output descriptions of each of its
19328 members. Note that as we output the DIEs necessary to represent the
19329 members of this record or union type, we will also be trying to output
19330 DIEs to represent the *types* of those members. However the `type'
19331 function (above) will specifically avoid generating type DIEs for member
19332 types *within* the list of member DIEs for this (containing) type except
19333 for those types (of members) which are explicitly marked as also being
19334 members of this (containing) type themselves. The g++ front- end can
19335 force any given type to be treated as a member of some other (containing)
19336 type by setting the TYPE_CONTEXT of the given (member) type to point to
19337 the TREE node representing the appropriate (containing) type. */
19339 /* First output info about the base classes. */
19340 if (binfo)
19342 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19343 int i;
19344 tree base;
19346 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19347 gen_inheritance_die (base,
19348 (accesses ? (*accesses)[i] : access_public_node),
19349 context_die);
19352 /* Now output info about the data members and type members. */
19353 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19355 /* If we thought we were generating minimal debug info for TYPE
19356 and then changed our minds, some of the member declarations
19357 may have already been defined. Don't define them again, but
19358 do put them in the right order. */
19360 child = lookup_decl_die (member);
19361 if (child)
19362 splice_child_die (context_die, child);
19363 else
19364 gen_decl_die (member, NULL, context_die);
19367 /* Now output info about the function members (if any). */
19368 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19370 /* Don't include clones in the member list. */
19371 if (DECL_ABSTRACT_ORIGIN (member))
19372 continue;
19374 child = lookup_decl_die (member);
19375 if (child)
19376 splice_child_die (context_die, child);
19377 else
19378 gen_decl_die (member, NULL, context_die);
19382 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19383 is set, we pretend that the type was never defined, so we only get the
19384 member DIEs needed by later specification DIEs. */
19386 static void
19387 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19388 enum debug_info_usage usage)
19390 dw_die_ref type_die = lookup_type_die (type);
19391 dw_die_ref scope_die = 0;
19392 int nested = 0;
19393 int complete = (TYPE_SIZE (type)
19394 && (! TYPE_STUB_DECL (type)
19395 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19396 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19397 complete = complete && should_emit_struct_debug (type, usage);
19399 if (type_die && ! complete)
19400 return;
19402 if (TYPE_CONTEXT (type) != NULL_TREE
19403 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19404 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19405 nested = 1;
19407 scope_die = scope_die_for (type, context_die);
19409 /* Generate child dies for template paramaters. */
19410 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19411 schedule_generic_params_dies_gen (type);
19413 if (! type_die || (nested && is_cu_die (scope_die)))
19414 /* First occurrence of type or toplevel definition of nested class. */
19416 dw_die_ref old_die = type_die;
19418 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19419 ? record_type_tag (type) : DW_TAG_union_type,
19420 scope_die, type);
19421 equate_type_number_to_die (type, type_die);
19422 if (old_die)
19423 add_AT_specification (type_die, old_die);
19424 else
19425 add_name_attribute (type_die, type_tag (type));
19427 else
19428 remove_AT (type_die, DW_AT_declaration);
19430 /* If this type has been completed, then give it a byte_size attribute and
19431 then give a list of members. */
19432 if (complete && !ns_decl)
19434 /* Prevent infinite recursion in cases where the type of some member of
19435 this type is expressed in terms of this type itself. */
19436 TREE_ASM_WRITTEN (type) = 1;
19437 add_byte_size_attribute (type_die, type);
19438 if (TYPE_STUB_DECL (type) != NULL_TREE)
19440 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19441 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19444 /* If the first reference to this type was as the return type of an
19445 inline function, then it may not have a parent. Fix this now. */
19446 if (type_die->die_parent == NULL)
19447 add_child_die (scope_die, type_die);
19449 push_decl_scope (type);
19450 gen_member_die (type, type_die);
19451 pop_decl_scope ();
19453 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19454 if (TYPE_ARTIFICIAL (type))
19455 add_AT_flag (type_die, DW_AT_artificial, 1);
19457 /* GNU extension: Record what type our vtable lives in. */
19458 if (TYPE_VFIELD (type))
19460 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19462 gen_type_die (vtype, context_die);
19463 add_AT_die_ref (type_die, DW_AT_containing_type,
19464 lookup_type_die (vtype));
19467 else
19469 add_AT_flag (type_die, DW_AT_declaration, 1);
19471 /* We don't need to do this for function-local types. */
19472 if (TYPE_STUB_DECL (type)
19473 && ! decl_function_context (TYPE_STUB_DECL (type)))
19474 vec_safe_push (incomplete_types, type);
19477 if (get_AT (type_die, DW_AT_name))
19478 add_pubtype (type, type_die);
19481 /* Generate a DIE for a subroutine _type_. */
19483 static void
19484 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19486 tree return_type = TREE_TYPE (type);
19487 dw_die_ref subr_die
19488 = new_die (DW_TAG_subroutine_type,
19489 scope_die_for (type, context_die), type);
19491 equate_type_number_to_die (type, subr_die);
19492 add_prototyped_attribute (subr_die, type);
19493 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19494 gen_formal_types_die (type, subr_die);
19496 if (get_AT (subr_die, DW_AT_name))
19497 add_pubtype (type, subr_die);
19500 /* Generate a DIE for a type definition. */
19502 static void
19503 gen_typedef_die (tree decl, dw_die_ref context_die)
19505 dw_die_ref type_die;
19506 tree origin;
19508 if (TREE_ASM_WRITTEN (decl))
19509 return;
19511 TREE_ASM_WRITTEN (decl) = 1;
19512 type_die = new_die (DW_TAG_typedef, context_die, decl);
19513 origin = decl_ultimate_origin (decl);
19514 if (origin != NULL)
19515 add_abstract_origin_attribute (type_die, origin);
19516 else
19518 tree type;
19520 add_name_and_src_coords_attributes (type_die, decl);
19521 if (DECL_ORIGINAL_TYPE (decl))
19523 type = DECL_ORIGINAL_TYPE (decl);
19525 gcc_assert (type != TREE_TYPE (decl));
19526 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19528 else
19530 type = TREE_TYPE (decl);
19532 if (is_naming_typedef_decl (TYPE_NAME (type)))
19534 /* Here, we are in the case of decl being a typedef naming
19535 an anonymous type, e.g:
19536 typedef struct {...} foo;
19537 In that case TREE_TYPE (decl) is not a typedef variant
19538 type and TYPE_NAME of the anonymous type is set to the
19539 TYPE_DECL of the typedef. This construct is emitted by
19540 the C++ FE.
19542 TYPE is the anonymous struct named by the typedef
19543 DECL. As we need the DW_AT_type attribute of the
19544 DW_TAG_typedef to point to the DIE of TYPE, let's
19545 generate that DIE right away. add_type_attribute
19546 called below will then pick (via lookup_type_die) that
19547 anonymous struct DIE. */
19548 if (!TREE_ASM_WRITTEN (type))
19549 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19551 /* This is a GNU Extension. We are adding a
19552 DW_AT_linkage_name attribute to the DIE of the
19553 anonymous struct TYPE. The value of that attribute
19554 is the name of the typedef decl naming the anonymous
19555 struct. This greatly eases the work of consumers of
19556 this debug info. */
19557 add_linkage_attr (lookup_type_die (type), decl);
19561 add_type_attribute (type_die, type, TREE_READONLY (decl),
19562 TREE_THIS_VOLATILE (decl), context_die);
19564 if (is_naming_typedef_decl (decl))
19565 /* We want that all subsequent calls to lookup_type_die with
19566 TYPE in argument yield the DW_TAG_typedef we have just
19567 created. */
19568 equate_type_number_to_die (type, type_die);
19570 add_accessibility_attribute (type_die, decl);
19573 if (DECL_ABSTRACT (decl))
19574 equate_decl_number_to_die (decl, type_die);
19576 if (get_AT (type_die, DW_AT_name))
19577 add_pubtype (decl, type_die);
19580 /* Generate a DIE for a struct, class, enum or union type. */
19582 static void
19583 gen_tagged_type_die (tree type,
19584 dw_die_ref context_die,
19585 enum debug_info_usage usage)
19587 int need_pop;
19589 if (type == NULL_TREE
19590 || !is_tagged_type (type))
19591 return;
19593 /* If this is a nested type whose containing class hasn't been written
19594 out yet, writing it out will cover this one, too. This does not apply
19595 to instantiations of member class templates; they need to be added to
19596 the containing class as they are generated. FIXME: This hurts the
19597 idea of combining type decls from multiple TUs, since we can't predict
19598 what set of template instantiations we'll get. */
19599 if (TYPE_CONTEXT (type)
19600 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19601 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19603 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19605 if (TREE_ASM_WRITTEN (type))
19606 return;
19608 /* If that failed, attach ourselves to the stub. */
19609 push_decl_scope (TYPE_CONTEXT (type));
19610 context_die = lookup_type_die (TYPE_CONTEXT (type));
19611 need_pop = 1;
19613 else if (TYPE_CONTEXT (type) != NULL_TREE
19614 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19616 /* If this type is local to a function that hasn't been written
19617 out yet, use a NULL context for now; it will be fixed up in
19618 decls_for_scope. */
19619 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19620 /* A declaration DIE doesn't count; nested types need to go in the
19621 specification. */
19622 if (context_die && is_declaration_die (context_die))
19623 context_die = NULL;
19624 need_pop = 0;
19626 else
19628 context_die = declare_in_namespace (type, context_die);
19629 need_pop = 0;
19632 if (TREE_CODE (type) == ENUMERAL_TYPE)
19634 /* This might have been written out by the call to
19635 declare_in_namespace. */
19636 if (!TREE_ASM_WRITTEN (type))
19637 gen_enumeration_type_die (type, context_die);
19639 else
19640 gen_struct_or_union_type_die (type, context_die, usage);
19642 if (need_pop)
19643 pop_decl_scope ();
19645 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19646 it up if it is ever completed. gen_*_type_die will set it for us
19647 when appropriate. */
19650 /* Generate a type description DIE. */
19652 static void
19653 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19654 enum debug_info_usage usage)
19656 struct array_descr_info info;
19658 if (type == NULL_TREE || type == error_mark_node)
19659 return;
19661 if (TYPE_NAME (type) != NULL_TREE
19662 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19663 && is_redundant_typedef (TYPE_NAME (type))
19664 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19665 /* The DECL of this type is a typedef we don't want to emit debug
19666 info for but we want debug info for its underlying typedef.
19667 This can happen for e.g, the injected-class-name of a C++
19668 type. */
19669 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19671 /* If TYPE is a typedef type variant, let's generate debug info
19672 for the parent typedef which TYPE is a type of. */
19673 if (typedef_variant_p (type))
19675 if (TREE_ASM_WRITTEN (type))
19676 return;
19678 /* Prevent broken recursion; we can't hand off to the same type. */
19679 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19681 /* Give typedefs the right scope. */
19682 context_die = scope_die_for (type, context_die);
19684 TREE_ASM_WRITTEN (type) = 1;
19686 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19687 return;
19690 /* If type is an anonymous tagged type named by a typedef, let's
19691 generate debug info for the typedef. */
19692 if (is_naming_typedef_decl (TYPE_NAME (type)))
19694 /* Use the DIE of the containing namespace as the parent DIE of
19695 the type description DIE we want to generate. */
19696 if (DECL_CONTEXT (TYPE_NAME (type))
19697 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19698 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19700 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19701 return;
19704 /* If this is an array type with hidden descriptor, handle it first. */
19705 if (!TREE_ASM_WRITTEN (type)
19706 && lang_hooks.types.get_array_descr_info
19707 && lang_hooks.types.get_array_descr_info (type, &info)
19708 && (dwarf_version >= 3 || !dwarf_strict))
19710 gen_descr_array_type_die (type, &info, context_die);
19711 TREE_ASM_WRITTEN (type) = 1;
19712 return;
19715 /* We are going to output a DIE to represent the unqualified version
19716 of this type (i.e. without any const or volatile qualifiers) so
19717 get the main variant (i.e. the unqualified version) of this type
19718 now. (Vectors are special because the debugging info is in the
19719 cloned type itself). */
19720 if (TREE_CODE (type) != VECTOR_TYPE)
19721 type = type_main_variant (type);
19723 if (TREE_ASM_WRITTEN (type))
19724 return;
19726 switch (TREE_CODE (type))
19728 case ERROR_MARK:
19729 break;
19731 case POINTER_TYPE:
19732 case REFERENCE_TYPE:
19733 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19734 ensures that the gen_type_die recursion will terminate even if the
19735 type is recursive. Recursive types are possible in Ada. */
19736 /* ??? We could perhaps do this for all types before the switch
19737 statement. */
19738 TREE_ASM_WRITTEN (type) = 1;
19740 /* For these types, all that is required is that we output a DIE (or a
19741 set of DIEs) to represent the "basis" type. */
19742 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19743 DINFO_USAGE_IND_USE);
19744 break;
19746 case OFFSET_TYPE:
19747 /* This code is used for C++ pointer-to-data-member types.
19748 Output a description of the relevant class type. */
19749 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19750 DINFO_USAGE_IND_USE);
19752 /* Output a description of the type of the object pointed to. */
19753 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19754 DINFO_USAGE_IND_USE);
19756 /* Now output a DIE to represent this pointer-to-data-member type
19757 itself. */
19758 gen_ptr_to_mbr_type_die (type, context_die);
19759 break;
19761 case FUNCTION_TYPE:
19762 /* Force out return type (in case it wasn't forced out already). */
19763 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19764 DINFO_USAGE_DIR_USE);
19765 gen_subroutine_type_die (type, context_die);
19766 break;
19768 case METHOD_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 ARRAY_TYPE:
19776 gen_array_type_die (type, context_die);
19777 break;
19779 case VECTOR_TYPE:
19780 gen_array_type_die (type, context_die);
19781 break;
19783 case ENUMERAL_TYPE:
19784 case RECORD_TYPE:
19785 case UNION_TYPE:
19786 case QUAL_UNION_TYPE:
19787 gen_tagged_type_die (type, context_die, usage);
19788 return;
19790 case VOID_TYPE:
19791 case INTEGER_TYPE:
19792 case REAL_TYPE:
19793 case FIXED_POINT_TYPE:
19794 case COMPLEX_TYPE:
19795 case BOOLEAN_TYPE:
19796 /* No DIEs needed for fundamental types. */
19797 break;
19799 case NULLPTR_TYPE:
19800 case LANG_TYPE:
19801 /* Just use DW_TAG_unspecified_type. */
19803 dw_die_ref type_die = lookup_type_die (type);
19804 if (type_die == NULL)
19806 tree name = TYPE_NAME (type);
19807 if (TREE_CODE (name) == TYPE_DECL)
19808 name = DECL_NAME (name);
19809 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19810 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19811 equate_type_number_to_die (type, type_die);
19814 break;
19816 default:
19817 gcc_unreachable ();
19820 TREE_ASM_WRITTEN (type) = 1;
19823 static void
19824 gen_type_die (tree type, dw_die_ref context_die)
19826 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19829 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19830 things which are local to the given block. */
19832 static void
19833 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19835 int must_output_die = 0;
19836 bool inlined_func;
19838 /* Ignore blocks that are NULL. */
19839 if (stmt == NULL_TREE)
19840 return;
19842 inlined_func = inlined_function_outer_scope_p (stmt);
19844 /* If the block is one fragment of a non-contiguous block, do not
19845 process the variables, since they will have been done by the
19846 origin block. Do process subblocks. */
19847 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19849 tree sub;
19851 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19852 gen_block_die (sub, context_die, depth + 1);
19854 return;
19857 /* Determine if we need to output any Dwarf DIEs at all to represent this
19858 block. */
19859 if (inlined_func)
19860 /* The outer scopes for inlinings *must* always be represented. We
19861 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19862 must_output_die = 1;
19863 else
19865 /* Determine if this block directly contains any "significant"
19866 local declarations which we will need to output DIEs for. */
19867 if (debug_info_level > DINFO_LEVEL_TERSE)
19868 /* We are not in terse mode so *any* local declaration counts
19869 as being a "significant" one. */
19870 must_output_die = ((BLOCK_VARS (stmt) != NULL
19871 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19872 && (TREE_USED (stmt)
19873 || TREE_ASM_WRITTEN (stmt)
19874 || BLOCK_ABSTRACT (stmt)));
19875 else if ((TREE_USED (stmt)
19876 || TREE_ASM_WRITTEN (stmt)
19877 || BLOCK_ABSTRACT (stmt))
19878 && !dwarf2out_ignore_block (stmt))
19879 must_output_die = 1;
19882 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19883 DIE for any block which contains no significant local declarations at
19884 all. Rather, in such cases we just call `decls_for_scope' so that any
19885 needed Dwarf info for any sub-blocks will get properly generated. Note
19886 that in terse mode, our definition of what constitutes a "significant"
19887 local declaration gets restricted to include only inlined function
19888 instances and local (nested) function definitions. */
19889 if (must_output_die)
19891 if (inlined_func)
19893 /* If STMT block is abstract, that means we have been called
19894 indirectly from dwarf2out_abstract_function.
19895 That function rightfully marks the descendent blocks (of
19896 the abstract function it is dealing with) as being abstract,
19897 precisely to prevent us from emitting any
19898 DW_TAG_inlined_subroutine DIE as a descendent
19899 of an abstract function instance. So in that case, we should
19900 not call gen_inlined_subroutine_die.
19902 Later though, when cgraph asks dwarf2out to emit info
19903 for the concrete instance of the function decl into which
19904 the concrete instance of STMT got inlined, the later will lead
19905 to the generation of a DW_TAG_inlined_subroutine DIE. */
19906 if (! BLOCK_ABSTRACT (stmt))
19907 gen_inlined_subroutine_die (stmt, context_die, depth);
19909 else
19910 gen_lexical_block_die (stmt, context_die, depth);
19912 else
19913 decls_for_scope (stmt, context_die, depth);
19916 /* Process variable DECL (or variable with origin ORIGIN) within
19917 block STMT and add it to CONTEXT_DIE. */
19918 static void
19919 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19921 dw_die_ref die;
19922 tree decl_or_origin = decl ? decl : origin;
19924 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19925 die = lookup_decl_die (decl_or_origin);
19926 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19927 && TYPE_DECL_IS_STUB (decl_or_origin))
19928 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19929 else
19930 die = NULL;
19932 if (die != NULL && die->die_parent == NULL)
19933 add_child_die (context_die, die);
19934 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19935 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19936 stmt, context_die);
19937 else
19938 gen_decl_die (decl, origin, context_die);
19941 /* Generate all of the decls declared within a given scope and (recursively)
19942 all of its sub-blocks. */
19944 static void
19945 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19947 tree decl;
19948 unsigned int i;
19949 tree subblocks;
19951 /* Ignore NULL blocks. */
19952 if (stmt == NULL_TREE)
19953 return;
19955 /* Output the DIEs to represent all of the data objects and typedefs
19956 declared directly within this block but not within any nested
19957 sub-blocks. Also, nested function and tag DIEs have been
19958 generated with a parent of NULL; fix that up now. */
19959 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19960 process_scope_var (stmt, decl, NULL_TREE, context_die);
19961 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19962 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19963 context_die);
19965 /* If we're at -g1, we're not interested in subblocks. */
19966 if (debug_info_level <= DINFO_LEVEL_TERSE)
19967 return;
19969 /* Output the DIEs to represent all sub-blocks (and the items declared
19970 therein) of this block. */
19971 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19972 subblocks != NULL;
19973 subblocks = BLOCK_CHAIN (subblocks))
19974 gen_block_die (subblocks, context_die, depth + 1);
19977 /* Is this a typedef we can avoid emitting? */
19979 static inline int
19980 is_redundant_typedef (const_tree decl)
19982 if (TYPE_DECL_IS_STUB (decl))
19983 return 1;
19985 if (DECL_ARTIFICIAL (decl)
19986 && DECL_CONTEXT (decl)
19987 && is_tagged_type (DECL_CONTEXT (decl))
19988 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19989 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19990 /* Also ignore the artificial member typedef for the class name. */
19991 return 1;
19993 return 0;
19996 /* Return TRUE if TYPE is a typedef that names a type for linkage
19997 purposes. This kind of typedefs is produced by the C++ FE for
19998 constructs like:
20000 typedef struct {...} foo;
20002 In that case, there is no typedef variant type produced for foo.
20003 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20004 struct type. */
20006 static bool
20007 is_naming_typedef_decl (const_tree decl)
20009 if (decl == NULL_TREE
20010 || TREE_CODE (decl) != TYPE_DECL
20011 || !is_tagged_type (TREE_TYPE (decl))
20012 || DECL_IS_BUILTIN (decl)
20013 || is_redundant_typedef (decl)
20014 /* It looks like Ada produces TYPE_DECLs that are very similar
20015 to C++ naming typedefs but that have different
20016 semantics. Let's be specific to c++ for now. */
20017 || !is_cxx ())
20018 return FALSE;
20020 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20021 && TYPE_NAME (TREE_TYPE (decl)) == decl
20022 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20023 != TYPE_NAME (TREE_TYPE (decl))));
20026 /* Returns the DIE for a context. */
20028 static inline dw_die_ref
20029 get_context_die (tree context)
20031 if (context)
20033 /* Find die that represents this context. */
20034 if (TYPE_P (context))
20036 context = TYPE_MAIN_VARIANT (context);
20037 return strip_naming_typedef (context, force_type_die (context));
20039 else
20040 return force_decl_die (context);
20042 return comp_unit_die ();
20045 /* Returns the DIE for decl. A DIE will always be returned. */
20047 static dw_die_ref
20048 force_decl_die (tree decl)
20050 dw_die_ref decl_die;
20051 unsigned saved_external_flag;
20052 tree save_fn = NULL_TREE;
20053 decl_die = lookup_decl_die (decl);
20054 if (!decl_die)
20056 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20058 decl_die = lookup_decl_die (decl);
20059 if (decl_die)
20060 return decl_die;
20062 switch (TREE_CODE (decl))
20064 case FUNCTION_DECL:
20065 /* Clear current_function_decl, so that gen_subprogram_die thinks
20066 that this is a declaration. At this point, we just want to force
20067 declaration die. */
20068 save_fn = current_function_decl;
20069 current_function_decl = NULL_TREE;
20070 gen_subprogram_die (decl, context_die);
20071 current_function_decl = save_fn;
20072 break;
20074 case VAR_DECL:
20075 /* Set external flag to force declaration die. Restore it after
20076 gen_decl_die() call. */
20077 saved_external_flag = DECL_EXTERNAL (decl);
20078 DECL_EXTERNAL (decl) = 1;
20079 gen_decl_die (decl, NULL, context_die);
20080 DECL_EXTERNAL (decl) = saved_external_flag;
20081 break;
20083 case NAMESPACE_DECL:
20084 if (dwarf_version >= 3 || !dwarf_strict)
20085 dwarf2out_decl (decl);
20086 else
20087 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20088 decl_die = comp_unit_die ();
20089 break;
20091 case TRANSLATION_UNIT_DECL:
20092 decl_die = comp_unit_die ();
20093 break;
20095 default:
20096 gcc_unreachable ();
20099 /* We should be able to find the DIE now. */
20100 if (!decl_die)
20101 decl_die = lookup_decl_die (decl);
20102 gcc_assert (decl_die);
20105 return decl_die;
20108 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20109 always returned. */
20111 static dw_die_ref
20112 force_type_die (tree type)
20114 dw_die_ref type_die;
20116 type_die = lookup_type_die (type);
20117 if (!type_die)
20119 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20121 type_die = modified_type_die (type, TYPE_READONLY (type),
20122 TYPE_VOLATILE (type), context_die);
20123 gcc_assert (type_die);
20125 return type_die;
20128 /* Force out any required namespaces to be able to output DECL,
20129 and return the new context_die for it, if it's changed. */
20131 static dw_die_ref
20132 setup_namespace_context (tree thing, dw_die_ref context_die)
20134 tree context = (DECL_P (thing)
20135 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20136 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20137 /* Force out the namespace. */
20138 context_die = force_decl_die (context);
20140 return context_die;
20143 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20144 type) within its namespace, if appropriate.
20146 For compatibility with older debuggers, namespace DIEs only contain
20147 declarations; all definitions are emitted at CU scope. */
20149 static dw_die_ref
20150 declare_in_namespace (tree thing, dw_die_ref context_die)
20152 dw_die_ref ns_context;
20154 if (debug_info_level <= DINFO_LEVEL_TERSE)
20155 return context_die;
20157 /* If this decl is from an inlined function, then don't try to emit it in its
20158 namespace, as we will get confused. It would have already been emitted
20159 when the abstract instance of the inline function was emitted anyways. */
20160 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20161 return context_die;
20163 ns_context = setup_namespace_context (thing, context_die);
20165 if (ns_context != context_die)
20167 if (is_fortran ())
20168 return ns_context;
20169 if (DECL_P (thing))
20170 gen_decl_die (thing, NULL, ns_context);
20171 else
20172 gen_type_die (thing, ns_context);
20174 return context_die;
20177 /* Generate a DIE for a namespace or namespace alias. */
20179 static void
20180 gen_namespace_die (tree decl, dw_die_ref context_die)
20182 dw_die_ref namespace_die;
20184 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20185 they are an alias of. */
20186 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20188 /* Output a real namespace or module. */
20189 context_die = setup_namespace_context (decl, comp_unit_die ());
20190 namespace_die = new_die (is_fortran ()
20191 ? DW_TAG_module : DW_TAG_namespace,
20192 context_die, decl);
20193 /* For Fortran modules defined in different CU don't add src coords. */
20194 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20196 const char *name = dwarf2_name (decl, 0);
20197 if (name)
20198 add_name_attribute (namespace_die, name);
20200 else
20201 add_name_and_src_coords_attributes (namespace_die, decl);
20202 if (DECL_EXTERNAL (decl))
20203 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20204 equate_decl_number_to_die (decl, namespace_die);
20206 else
20208 /* Output a namespace alias. */
20210 /* Force out the namespace we are an alias of, if necessary. */
20211 dw_die_ref origin_die
20212 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20214 if (DECL_FILE_SCOPE_P (decl)
20215 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20216 context_die = setup_namespace_context (decl, comp_unit_die ());
20217 /* Now create the namespace alias DIE. */
20218 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20219 add_name_and_src_coords_attributes (namespace_die, decl);
20220 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20221 equate_decl_number_to_die (decl, namespace_die);
20223 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20224 if (want_pubnames ())
20225 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20228 /* Generate Dwarf debug information for a decl described by DECL.
20229 The return value is currently only meaningful for PARM_DECLs,
20230 for all other decls it returns NULL. */
20232 static dw_die_ref
20233 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20235 tree decl_or_origin = decl ? decl : origin;
20236 tree class_origin = NULL, ultimate_origin;
20238 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20239 return NULL;
20241 switch (TREE_CODE (decl_or_origin))
20243 case ERROR_MARK:
20244 break;
20246 case CONST_DECL:
20247 if (!is_fortran () && !is_ada ())
20249 /* The individual enumerators of an enum type get output when we output
20250 the Dwarf representation of the relevant enum type itself. */
20251 break;
20254 /* Emit its type. */
20255 gen_type_die (TREE_TYPE (decl), context_die);
20257 /* And its containing namespace. */
20258 context_die = declare_in_namespace (decl, context_die);
20260 gen_const_die (decl, context_die);
20261 break;
20263 case FUNCTION_DECL:
20264 /* Don't output any DIEs to represent mere function declarations,
20265 unless they are class members or explicit block externs. */
20266 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20267 && DECL_FILE_SCOPE_P (decl_or_origin)
20268 && (current_function_decl == NULL_TREE
20269 || DECL_ARTIFICIAL (decl_or_origin)))
20270 break;
20272 #if 0
20273 /* FIXME */
20274 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20275 on local redeclarations of global functions. That seems broken. */
20276 if (current_function_decl != decl)
20277 /* This is only a declaration. */;
20278 #endif
20280 /* If we're emitting a clone, emit info for the abstract instance. */
20281 if (origin || DECL_ORIGIN (decl) != decl)
20282 dwarf2out_abstract_function (origin
20283 ? DECL_ORIGIN (origin)
20284 : DECL_ABSTRACT_ORIGIN (decl));
20286 /* If we're emitting an out-of-line copy of an inline function,
20287 emit info for the abstract instance and set up to refer to it. */
20288 else if (cgraph_function_possibly_inlined_p (decl)
20289 && ! DECL_ABSTRACT (decl)
20290 && ! class_or_namespace_scope_p (context_die)
20291 /* dwarf2out_abstract_function won't emit a die if this is just
20292 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20293 that case, because that works only if we have a die. */
20294 && DECL_INITIAL (decl) != NULL_TREE)
20296 dwarf2out_abstract_function (decl);
20297 set_decl_origin_self (decl);
20300 /* Otherwise we're emitting the primary DIE for this decl. */
20301 else if (debug_info_level > DINFO_LEVEL_TERSE)
20303 /* Before we describe the FUNCTION_DECL itself, make sure that we
20304 have its containing type. */
20305 if (!origin)
20306 origin = decl_class_context (decl);
20307 if (origin != NULL_TREE)
20308 gen_type_die (origin, context_die);
20310 /* And its return type. */
20311 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20313 /* And its virtual context. */
20314 if (DECL_VINDEX (decl) != NULL_TREE)
20315 gen_type_die (DECL_CONTEXT (decl), context_die);
20317 /* Make sure we have a member DIE for decl. */
20318 if (origin != NULL_TREE)
20319 gen_type_die_for_member (origin, decl, context_die);
20321 /* And its containing namespace. */
20322 context_die = declare_in_namespace (decl, context_die);
20325 /* Now output a DIE to represent the function itself. */
20326 if (decl)
20327 gen_subprogram_die (decl, context_die);
20328 break;
20330 case TYPE_DECL:
20331 /* If we are in terse mode, don't generate any DIEs to represent any
20332 actual typedefs. */
20333 if (debug_info_level <= DINFO_LEVEL_TERSE)
20334 break;
20336 /* In the special case of a TYPE_DECL node representing the declaration
20337 of some type tag, if the given TYPE_DECL is marked as having been
20338 instantiated from some other (original) TYPE_DECL node (e.g. one which
20339 was generated within the original definition of an inline function) we
20340 used to generate a special (abbreviated) DW_TAG_structure_type,
20341 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20342 should be actually referencing those DIEs, as variable DIEs with that
20343 type would be emitted already in the abstract origin, so it was always
20344 removed during unused type prunning. Don't add anything in this
20345 case. */
20346 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20347 break;
20349 if (is_redundant_typedef (decl))
20350 gen_type_die (TREE_TYPE (decl), context_die);
20351 else
20352 /* Output a DIE to represent the typedef itself. */
20353 gen_typedef_die (decl, context_die);
20354 break;
20356 case LABEL_DECL:
20357 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20358 gen_label_die (decl, context_die);
20359 break;
20361 case VAR_DECL:
20362 case RESULT_DECL:
20363 /* If we are in terse mode, don't generate any DIEs to represent any
20364 variable declarations or definitions. */
20365 if (debug_info_level <= DINFO_LEVEL_TERSE)
20366 break;
20368 /* Output any DIEs that are needed to specify the type of this data
20369 object. */
20370 if (decl_by_reference_p (decl_or_origin))
20371 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20372 else
20373 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20375 /* And its containing type. */
20376 class_origin = decl_class_context (decl_or_origin);
20377 if (class_origin != NULL_TREE)
20378 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20380 /* And its containing namespace. */
20381 context_die = declare_in_namespace (decl_or_origin, context_die);
20383 /* Now output the DIE to represent the data object itself. This gets
20384 complicated because of the possibility that the VAR_DECL really
20385 represents an inlined instance of a formal parameter for an inline
20386 function. */
20387 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20388 if (ultimate_origin != NULL_TREE
20389 && TREE_CODE (ultimate_origin) == PARM_DECL)
20390 gen_formal_parameter_die (decl, origin,
20391 true /* Emit name attribute. */,
20392 context_die);
20393 else
20394 gen_variable_die (decl, origin, context_die);
20395 break;
20397 case FIELD_DECL:
20398 /* Ignore the nameless fields that are used to skip bits but handle C++
20399 anonymous unions and structs. */
20400 if (DECL_NAME (decl) != NULL_TREE
20401 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20402 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20404 gen_type_die (member_declared_type (decl), context_die);
20405 gen_field_die (decl, context_die);
20407 break;
20409 case PARM_DECL:
20410 if (DECL_BY_REFERENCE (decl_or_origin))
20411 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20412 else
20413 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20414 return gen_formal_parameter_die (decl, origin,
20415 true /* Emit name attribute. */,
20416 context_die);
20418 case NAMESPACE_DECL:
20419 case IMPORTED_DECL:
20420 if (dwarf_version >= 3 || !dwarf_strict)
20421 gen_namespace_die (decl, context_die);
20422 break;
20424 default:
20425 /* Probably some frontend-internal decl. Assume we don't care. */
20426 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20427 break;
20430 return NULL;
20433 /* Output debug information for global decl DECL. Called from toplev.c after
20434 compilation proper has finished. */
20436 static void
20437 dwarf2out_global_decl (tree decl)
20439 /* Output DWARF2 information for file-scope tentative data object
20440 declarations, file-scope (extern) function declarations (which
20441 had no corresponding body) and file-scope tagged type declarations
20442 and definitions which have not yet been forced out. */
20443 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20444 dwarf2out_decl (decl);
20447 /* Output debug information for type decl DECL. Called from toplev.c
20448 and from language front ends (to record built-in types). */
20449 static void
20450 dwarf2out_type_decl (tree decl, int local)
20452 if (!local)
20453 dwarf2out_decl (decl);
20456 /* Output debug information for imported module or decl DECL.
20457 NAME is non-NULL name in the lexical block if the decl has been renamed.
20458 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20459 that DECL belongs to.
20460 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20461 static void
20462 dwarf2out_imported_module_or_decl_1 (tree decl,
20463 tree name,
20464 tree lexical_block,
20465 dw_die_ref lexical_block_die)
20467 expanded_location xloc;
20468 dw_die_ref imported_die = NULL;
20469 dw_die_ref at_import_die;
20471 if (TREE_CODE (decl) == IMPORTED_DECL)
20473 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20474 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20475 gcc_assert (decl);
20477 else
20478 xloc = expand_location (input_location);
20480 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20482 at_import_die = force_type_die (TREE_TYPE (decl));
20483 /* For namespace N { typedef void T; } using N::T; base_type_die
20484 returns NULL, but DW_TAG_imported_declaration requires
20485 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20486 if (!at_import_die)
20488 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20489 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20490 at_import_die = lookup_type_die (TREE_TYPE (decl));
20491 gcc_assert (at_import_die);
20494 else
20496 at_import_die = lookup_decl_die (decl);
20497 if (!at_import_die)
20499 /* If we're trying to avoid duplicate debug info, we may not have
20500 emitted the member decl for this field. Emit it now. */
20501 if (TREE_CODE (decl) == FIELD_DECL)
20503 tree type = DECL_CONTEXT (decl);
20505 if (TYPE_CONTEXT (type)
20506 && TYPE_P (TYPE_CONTEXT (type))
20507 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20508 DINFO_USAGE_DIR_USE))
20509 return;
20510 gen_type_die_for_member (type, decl,
20511 get_context_die (TYPE_CONTEXT (type)));
20513 at_import_die = force_decl_die (decl);
20517 if (TREE_CODE (decl) == NAMESPACE_DECL)
20519 if (dwarf_version >= 3 || !dwarf_strict)
20520 imported_die = new_die (DW_TAG_imported_module,
20521 lexical_block_die,
20522 lexical_block);
20523 else
20524 return;
20526 else
20527 imported_die = new_die (DW_TAG_imported_declaration,
20528 lexical_block_die,
20529 lexical_block);
20531 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20532 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20533 if (name)
20534 add_AT_string (imported_die, DW_AT_name,
20535 IDENTIFIER_POINTER (name));
20536 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20539 /* Output debug information for imported module or decl DECL.
20540 NAME is non-NULL name in context if the decl has been renamed.
20541 CHILD is true if decl is one of the renamed decls as part of
20542 importing whole module. */
20544 static void
20545 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20546 bool child)
20548 /* dw_die_ref at_import_die; */
20549 dw_die_ref scope_die;
20551 if (debug_info_level <= DINFO_LEVEL_TERSE)
20552 return;
20554 gcc_assert (decl);
20556 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20557 We need decl DIE for reference and scope die. First, get DIE for the decl
20558 itself. */
20560 /* Get the scope die for decl context. Use comp_unit_die for global module
20561 or decl. If die is not found for non globals, force new die. */
20562 if (context
20563 && TYPE_P (context)
20564 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20565 return;
20567 if (!(dwarf_version >= 3 || !dwarf_strict))
20568 return;
20570 scope_die = get_context_die (context);
20572 if (child)
20574 gcc_assert (scope_die->die_child);
20575 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20576 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20577 scope_die = scope_die->die_child;
20580 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20581 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20585 /* Write the debugging output for DECL. */
20587 void
20588 dwarf2out_decl (tree decl)
20590 dw_die_ref context_die = comp_unit_die ();
20592 switch (TREE_CODE (decl))
20594 case ERROR_MARK:
20595 return;
20597 case FUNCTION_DECL:
20598 /* What we would really like to do here is to filter out all mere
20599 file-scope declarations of file-scope functions which are never
20600 referenced later within this translation unit (and keep all of ones
20601 that *are* referenced later on) but we aren't clairvoyant, so we have
20602 no idea which functions will be referenced in the future (i.e. later
20603 on within the current translation unit). So here we just ignore all
20604 file-scope function declarations which are not also definitions. If
20605 and when the debugger needs to know something about these functions,
20606 it will have to hunt around and find the DWARF information associated
20607 with the definition of the function.
20609 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20610 nodes represent definitions and which ones represent mere
20611 declarations. We have to check DECL_INITIAL instead. That's because
20612 the C front-end supports some weird semantics for "extern inline"
20613 function definitions. These can get inlined within the current
20614 translation unit (and thus, we need to generate Dwarf info for their
20615 abstract instances so that the Dwarf info for the concrete inlined
20616 instances can have something to refer to) but the compiler never
20617 generates any out-of-lines instances of such things (despite the fact
20618 that they *are* definitions).
20620 The important point is that the C front-end marks these "extern
20621 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20622 them anyway. Note that the C++ front-end also plays some similar games
20623 for inline function definitions appearing within include files which
20624 also contain `#pragma interface' pragmas.
20626 If we are called from dwarf2out_abstract_function output a DIE
20627 anyway. We can end up here this way with early inlining and LTO
20628 where the inlined function is output in a different LTRANS unit
20629 or not at all. */
20630 if (DECL_INITIAL (decl) == NULL_TREE
20631 && ! DECL_ABSTRACT (decl))
20632 return;
20634 /* If we're a nested function, initially use a parent of NULL; if we're
20635 a plain function, this will be fixed up in decls_for_scope. If
20636 we're a method, it will be ignored, since we already have a DIE. */
20637 if (decl_function_context (decl)
20638 /* But if we're in terse mode, we don't care about scope. */
20639 && debug_info_level > DINFO_LEVEL_TERSE)
20640 context_die = NULL;
20641 break;
20643 case VAR_DECL:
20644 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20645 declaration and if the declaration was never even referenced from
20646 within this entire compilation unit. We suppress these DIEs in
20647 order to save space in the .debug section (by eliminating entries
20648 which are probably useless). Note that we must not suppress
20649 block-local extern declarations (whether used or not) because that
20650 would screw-up the debugger's name lookup mechanism and cause it to
20651 miss things which really ought to be in scope at a given point. */
20652 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20653 return;
20655 /* For local statics lookup proper context die. */
20656 if (TREE_STATIC (decl)
20657 && DECL_CONTEXT (decl)
20658 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20659 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20661 /* If we are in terse mode, don't generate any DIEs to represent any
20662 variable declarations or definitions. */
20663 if (debug_info_level <= DINFO_LEVEL_TERSE)
20664 return;
20665 break;
20667 case CONST_DECL:
20668 if (debug_info_level <= DINFO_LEVEL_TERSE)
20669 return;
20670 if (!is_fortran () && !is_ada ())
20671 return;
20672 if (TREE_STATIC (decl) && decl_function_context (decl))
20673 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20674 break;
20676 case NAMESPACE_DECL:
20677 case IMPORTED_DECL:
20678 if (debug_info_level <= DINFO_LEVEL_TERSE)
20679 return;
20680 if (lookup_decl_die (decl) != NULL)
20681 return;
20682 break;
20684 case TYPE_DECL:
20685 /* Don't emit stubs for types unless they are needed by other DIEs. */
20686 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20687 return;
20689 /* Don't bother trying to generate any DIEs to represent any of the
20690 normal built-in types for the language we are compiling. */
20691 if (DECL_IS_BUILTIN (decl))
20692 return;
20694 /* If we are in terse mode, don't generate any DIEs for types. */
20695 if (debug_info_level <= DINFO_LEVEL_TERSE)
20696 return;
20698 /* If we're a function-scope tag, initially use a parent of NULL;
20699 this will be fixed up in decls_for_scope. */
20700 if (decl_function_context (decl))
20701 context_die = NULL;
20703 break;
20705 default:
20706 return;
20709 gen_decl_die (decl, NULL, context_die);
20712 /* Write the debugging output for DECL. */
20714 static void
20715 dwarf2out_function_decl (tree decl)
20717 dwarf2out_decl (decl);
20718 call_arg_locations = NULL;
20719 call_arg_loc_last = NULL;
20720 call_site_count = -1;
20721 tail_call_site_count = -1;
20722 block_map.release ();
20723 htab_empty (decl_loc_table);
20724 htab_empty (cached_dw_loc_list_table);
20727 /* Output a marker (i.e. a label) for the beginning of the generated code for
20728 a lexical block. */
20730 static void
20731 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20732 unsigned int blocknum)
20734 switch_to_section (current_function_section ());
20735 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20738 /* Output a marker (i.e. a label) for the end of the generated code for a
20739 lexical block. */
20741 static void
20742 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20744 switch_to_section (current_function_section ());
20745 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20748 /* Returns nonzero if it is appropriate not to emit any debugging
20749 information for BLOCK, because it doesn't contain any instructions.
20751 Don't allow this for blocks with nested functions or local classes
20752 as we would end up with orphans, and in the presence of scheduling
20753 we may end up calling them anyway. */
20755 static bool
20756 dwarf2out_ignore_block (const_tree block)
20758 tree decl;
20759 unsigned int i;
20761 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20762 if (TREE_CODE (decl) == FUNCTION_DECL
20763 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20764 return 0;
20765 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20767 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20768 if (TREE_CODE (decl) == FUNCTION_DECL
20769 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20770 return 0;
20773 return 1;
20776 /* Hash table routines for file_hash. */
20778 static int
20779 file_table_eq (const void *p1_p, const void *p2_p)
20781 const struct dwarf_file_data *const p1 =
20782 (const struct dwarf_file_data *) p1_p;
20783 const char *const p2 = (const char *) p2_p;
20784 return filename_cmp (p1->filename, p2) == 0;
20787 static hashval_t
20788 file_table_hash (const void *p_p)
20790 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20791 return htab_hash_string (p->filename);
20794 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20795 dwarf2out.c) and return its "index". The index of each (known) filename is
20796 just a unique number which is associated with only that one filename. We
20797 need such numbers for the sake of generating labels (in the .debug_sfnames
20798 section) and references to those files numbers (in the .debug_srcinfo
20799 and.debug_macinfo sections). If the filename given as an argument is not
20800 found in our current list, add it to the list and assign it the next
20801 available unique index number. In order to speed up searches, we remember
20802 the index of the filename was looked up last. This handles the majority of
20803 all searches. */
20805 static struct dwarf_file_data *
20806 lookup_filename (const char *file_name)
20808 void ** slot;
20809 struct dwarf_file_data * created;
20811 /* Check to see if the file name that was searched on the previous
20812 call matches this file name. If so, return the index. */
20813 if (file_table_last_lookup
20814 && (file_name == file_table_last_lookup->filename
20815 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20816 return file_table_last_lookup;
20818 /* Didn't match the previous lookup, search the table. */
20819 slot = htab_find_slot_with_hash (file_table, file_name,
20820 htab_hash_string (file_name), INSERT);
20821 if (*slot)
20822 return (struct dwarf_file_data *) *slot;
20824 created = ggc_alloc_dwarf_file_data ();
20825 created->filename = file_name;
20826 created->emitted_number = 0;
20827 *slot = created;
20828 return created;
20831 /* If the assembler will construct the file table, then translate the compiler
20832 internal file table number into the assembler file table number, and emit
20833 a .file directive if we haven't already emitted one yet. The file table
20834 numbers are different because we prune debug info for unused variables and
20835 types, which may include filenames. */
20837 static int
20838 maybe_emit_file (struct dwarf_file_data * fd)
20840 if (! fd->emitted_number)
20842 if (last_emitted_file)
20843 fd->emitted_number = last_emitted_file->emitted_number + 1;
20844 else
20845 fd->emitted_number = 1;
20846 last_emitted_file = fd;
20848 if (DWARF2_ASM_LINE_DEBUG_INFO)
20850 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20851 output_quoted_string (asm_out_file,
20852 remap_debug_filename (fd->filename));
20853 fputc ('\n', asm_out_file);
20857 return fd->emitted_number;
20860 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20861 That generation should happen after function debug info has been
20862 generated. The value of the attribute is the constant value of ARG. */
20864 static void
20865 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20867 die_arg_entry entry;
20869 if (!die || !arg)
20870 return;
20872 if (!tmpl_value_parm_die_table)
20873 vec_alloc (tmpl_value_parm_die_table, 32);
20875 entry.die = die;
20876 entry.arg = arg;
20877 vec_safe_push (tmpl_value_parm_die_table, entry);
20880 /* Return TRUE if T is an instance of generic type, FALSE
20881 otherwise. */
20883 static bool
20884 generic_type_p (tree t)
20886 if (t == NULL_TREE || !TYPE_P (t))
20887 return false;
20888 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20891 /* Schedule the generation of the generic parameter dies for the
20892 instance of generic type T. The proper generation itself is later
20893 done by gen_scheduled_generic_parms_dies. */
20895 static void
20896 schedule_generic_params_dies_gen (tree t)
20898 if (!generic_type_p (t))
20899 return;
20901 if (!generic_type_instances)
20902 vec_alloc (generic_type_instances, 256);
20904 vec_safe_push (generic_type_instances, t);
20907 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20908 by append_entry_to_tmpl_value_parm_die_table. This function must
20909 be called after function DIEs have been generated. */
20911 static void
20912 gen_remaining_tmpl_value_param_die_attribute (void)
20914 if (tmpl_value_parm_die_table)
20916 unsigned i;
20917 die_arg_entry *e;
20919 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20920 tree_add_const_value_attribute (e->die, e->arg);
20924 /* Generate generic parameters DIEs for instances of generic types
20925 that have been previously scheduled by
20926 schedule_generic_params_dies_gen. This function must be called
20927 after all the types of the CU have been laid out. */
20929 static void
20930 gen_scheduled_generic_parms_dies (void)
20932 unsigned i;
20933 tree t;
20935 if (!generic_type_instances)
20936 return;
20938 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20939 if (COMPLETE_TYPE_P (t))
20940 gen_generic_params_dies (t);
20944 /* Replace DW_AT_name for the decl with name. */
20946 static void
20947 dwarf2out_set_name (tree decl, tree name)
20949 dw_die_ref die;
20950 dw_attr_ref attr;
20951 const char *dname;
20953 die = TYPE_SYMTAB_DIE (decl);
20954 if (!die)
20955 return;
20957 dname = dwarf2_name (name, 0);
20958 if (!dname)
20959 return;
20961 attr = get_AT (die, DW_AT_name);
20962 if (attr)
20964 struct indirect_string_node *node;
20966 node = find_AT_string (dname);
20967 /* replace the string. */
20968 attr->dw_attr_val.v.val_str = node;
20971 else
20972 add_name_attribute (die, dname);
20975 /* True if before or during processing of the first function being emitted. */
20976 static bool in_first_function_p = true;
20977 /* True if loc_note during dwarf2out_var_location call might still be
20978 before first real instruction at address equal to .Ltext0. */
20979 static bool maybe_at_text_label_p = true;
20980 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20981 static unsigned int first_loclabel_num_not_at_text_label;
20983 /* Called by the final INSN scan whenever we see a var location. We
20984 use it to drop labels in the right places, and throw the location in
20985 our lookup table. */
20987 static void
20988 dwarf2out_var_location (rtx loc_note)
20990 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20991 struct var_loc_node *newloc;
20992 rtx next_real, next_note;
20993 static const char *last_label;
20994 static const char *last_postcall_label;
20995 static bool last_in_cold_section_p;
20996 static rtx expected_next_loc_note;
20997 tree decl;
20998 bool var_loc_p;
21000 if (!NOTE_P (loc_note))
21002 if (CALL_P (loc_note))
21004 call_site_count++;
21005 if (SIBLING_CALL_P (loc_note))
21006 tail_call_site_count++;
21008 return;
21011 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21012 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21013 return;
21015 /* Optimize processing a large consecutive sequence of location
21016 notes so we don't spend too much time in next_real_insn. If the
21017 next insn is another location note, remember the next_real_insn
21018 calculation for next time. */
21019 next_real = cached_next_real_insn;
21020 if (next_real)
21022 if (expected_next_loc_note != loc_note)
21023 next_real = NULL_RTX;
21026 next_note = NEXT_INSN (loc_note);
21027 if (! next_note
21028 || INSN_DELETED_P (next_note)
21029 || ! NOTE_P (next_note)
21030 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21031 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21032 next_note = NULL_RTX;
21034 if (! next_real)
21035 next_real = next_real_insn (loc_note);
21037 if (next_note)
21039 expected_next_loc_note = next_note;
21040 cached_next_real_insn = next_real;
21042 else
21043 cached_next_real_insn = NULL_RTX;
21045 /* If there are no instructions which would be affected by this note,
21046 don't do anything. */
21047 if (var_loc_p
21048 && next_real == NULL_RTX
21049 && !NOTE_DURING_CALL_P (loc_note))
21050 return;
21052 if (next_real == NULL_RTX)
21053 next_real = get_last_insn ();
21055 /* If there were any real insns between note we processed last time
21056 and this note (or if it is the first note), clear
21057 last_{,postcall_}label so that they are not reused this time. */
21058 if (last_var_location_insn == NULL_RTX
21059 || last_var_location_insn != next_real
21060 || last_in_cold_section_p != in_cold_section_p)
21062 last_label = NULL;
21063 last_postcall_label = NULL;
21066 if (var_loc_p)
21068 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21069 newloc = add_var_loc_to_decl (decl, loc_note,
21070 NOTE_DURING_CALL_P (loc_note)
21071 ? last_postcall_label : last_label);
21072 if (newloc == NULL)
21073 return;
21075 else
21077 decl = NULL_TREE;
21078 newloc = NULL;
21081 /* If there were no real insns between note we processed last time
21082 and this note, use the label we emitted last time. Otherwise
21083 create a new label and emit it. */
21084 if (last_label == NULL)
21086 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21087 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21088 loclabel_num++;
21089 last_label = ggc_strdup (loclabel);
21090 /* See if loclabel might be equal to .Ltext0. If yes,
21091 bump first_loclabel_num_not_at_text_label. */
21092 if (!have_multiple_function_sections
21093 && in_first_function_p
21094 && maybe_at_text_label_p)
21096 static rtx last_start;
21097 rtx insn;
21098 for (insn = loc_note; insn; insn = previous_insn (insn))
21099 if (insn == last_start)
21100 break;
21101 else if (!NONDEBUG_INSN_P (insn))
21102 continue;
21103 else
21105 rtx body = PATTERN (insn);
21106 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21107 continue;
21108 /* Inline asm could occupy zero bytes. */
21109 else if (GET_CODE (body) == ASM_INPUT
21110 || asm_noperands (body) >= 0)
21111 continue;
21112 #ifdef HAVE_attr_length
21113 else if (get_attr_min_length (insn) == 0)
21114 continue;
21115 #endif
21116 else
21118 /* Assume insn has non-zero length. */
21119 maybe_at_text_label_p = false;
21120 break;
21123 if (maybe_at_text_label_p)
21125 last_start = loc_note;
21126 first_loclabel_num_not_at_text_label = loclabel_num;
21131 if (!var_loc_p)
21133 struct call_arg_loc_node *ca_loc
21134 = ggc_alloc_cleared_call_arg_loc_node ();
21135 rtx prev = prev_real_insn (loc_note), x;
21136 ca_loc->call_arg_loc_note = loc_note;
21137 ca_loc->next = NULL;
21138 ca_loc->label = last_label;
21139 gcc_assert (prev
21140 && (CALL_P (prev)
21141 || (NONJUMP_INSN_P (prev)
21142 && GET_CODE (PATTERN (prev)) == SEQUENCE
21143 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21144 if (!CALL_P (prev))
21145 prev = XVECEXP (PATTERN (prev), 0, 0);
21146 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21147 x = get_call_rtx_from (PATTERN (prev));
21148 if (x)
21150 x = XEXP (XEXP (x, 0), 0);
21151 if (GET_CODE (x) == SYMBOL_REF
21152 && SYMBOL_REF_DECL (x)
21153 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21154 ca_loc->symbol_ref = x;
21156 ca_loc->block = insn_scope (prev);
21157 if (call_arg_locations)
21158 call_arg_loc_last->next = ca_loc;
21159 else
21160 call_arg_locations = ca_loc;
21161 call_arg_loc_last = ca_loc;
21163 else if (!NOTE_DURING_CALL_P (loc_note))
21164 newloc->label = last_label;
21165 else
21167 if (!last_postcall_label)
21169 sprintf (loclabel, "%s-1", last_label);
21170 last_postcall_label = ggc_strdup (loclabel);
21172 newloc->label = last_postcall_label;
21175 last_var_location_insn = next_real;
21176 last_in_cold_section_p = in_cold_section_p;
21179 /* Note in one location list that text section has changed. */
21181 static int
21182 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21184 var_loc_list *list = (var_loc_list *) *slot;
21185 if (list->first)
21186 list->last_before_switch
21187 = list->last->next ? list->last->next : list->last;
21188 return 1;
21191 /* Note in all location lists that text section has changed. */
21193 static void
21194 var_location_switch_text_section (void)
21196 if (decl_loc_table == NULL)
21197 return;
21199 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21202 /* Create a new line number table. */
21204 static dw_line_info_table *
21205 new_line_info_table (void)
21207 dw_line_info_table *table;
21209 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21210 table->file_num = 1;
21211 table->line_num = 1;
21212 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21214 return table;
21217 /* Lookup the "current" table into which we emit line info, so
21218 that we don't have to do it for every source line. */
21220 static void
21221 set_cur_line_info_table (section *sec)
21223 dw_line_info_table *table;
21225 if (sec == text_section)
21226 table = text_section_line_info;
21227 else if (sec == cold_text_section)
21229 table = cold_text_section_line_info;
21230 if (!table)
21232 cold_text_section_line_info = table = new_line_info_table ();
21233 table->end_label = cold_end_label;
21236 else
21238 const char *end_label;
21240 if (flag_reorder_blocks_and_partition)
21242 if (in_cold_section_p)
21243 end_label = crtl->subsections.cold_section_end_label;
21244 else
21245 end_label = crtl->subsections.hot_section_end_label;
21247 else
21249 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21250 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21251 current_function_funcdef_no);
21252 end_label = ggc_strdup (label);
21255 table = new_line_info_table ();
21256 table->end_label = end_label;
21258 vec_safe_push (separate_line_info, table);
21261 if (DWARF2_ASM_LINE_DEBUG_INFO)
21262 table->is_stmt = (cur_line_info_table
21263 ? cur_line_info_table->is_stmt
21264 : DWARF_LINE_DEFAULT_IS_STMT_START);
21265 cur_line_info_table = table;
21269 /* We need to reset the locations at the beginning of each
21270 function. We can't do this in the end_function hook, because the
21271 declarations that use the locations won't have been output when
21272 that hook is called. Also compute have_multiple_function_sections here. */
21274 static void
21275 dwarf2out_begin_function (tree fun)
21277 section *sec = function_section (fun);
21279 if (sec != text_section)
21280 have_multiple_function_sections = true;
21282 if (flag_reorder_blocks_and_partition && !cold_text_section)
21284 gcc_assert (current_function_decl == fun);
21285 cold_text_section = unlikely_text_section ();
21286 switch_to_section (cold_text_section);
21287 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21288 switch_to_section (sec);
21291 dwarf2out_note_section_used ();
21292 call_site_count = 0;
21293 tail_call_site_count = 0;
21295 set_cur_line_info_table (sec);
21298 /* Helper function of dwarf2out_end_function, called only after emitting
21299 the very first function into assembly. Check if some .debug_loc range
21300 might end with a .LVL* label that could be equal to .Ltext0.
21301 In that case we must force using absolute addresses in .debug_loc ranges,
21302 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21303 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21304 list terminator.
21305 Set have_multiple_function_sections to true in that case and
21306 terminate htab traversal. */
21308 static int
21309 find_empty_loc_ranges_at_text_label (void **slot, void *)
21311 var_loc_list *entry;
21312 struct var_loc_node *node;
21314 entry = (var_loc_list *) *slot;
21315 node = entry->first;
21316 if (node && node->next && node->next->label)
21318 unsigned int i;
21319 const char *label = node->next->label;
21320 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21322 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21324 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21325 if (strcmp (label, loclabel) == 0)
21327 have_multiple_function_sections = true;
21328 return 0;
21332 return 1;
21335 /* Hook called after emitting a function into assembly.
21336 This does something only for the very first function emitted. */
21338 static void
21339 dwarf2out_end_function (unsigned int)
21341 if (in_first_function_p
21342 && !have_multiple_function_sections
21343 && first_loclabel_num_not_at_text_label
21344 && decl_loc_table)
21345 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21346 NULL);
21347 in_first_function_p = false;
21348 maybe_at_text_label_p = false;
21351 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21353 static void
21354 push_dw_line_info_entry (dw_line_info_table *table,
21355 enum dw_line_info_opcode opcode, unsigned int val)
21357 dw_line_info_entry e;
21358 e.opcode = opcode;
21359 e.val = val;
21360 vec_safe_push (table->entries, e);
21363 /* Output a label to mark the beginning of a source code line entry
21364 and record information relating to this source line, in
21365 'line_info_table' for later output of the .debug_line section. */
21366 /* ??? The discriminator parameter ought to be unsigned. */
21368 static void
21369 dwarf2out_source_line (unsigned int line, const char *filename,
21370 int discriminator, bool is_stmt)
21372 unsigned int file_num;
21373 dw_line_info_table *table;
21375 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21376 return;
21378 /* The discriminator column was added in dwarf4. Simplify the below
21379 by simply removing it if we're not supposed to output it. */
21380 if (dwarf_version < 4 && dwarf_strict)
21381 discriminator = 0;
21383 table = cur_line_info_table;
21384 file_num = maybe_emit_file (lookup_filename (filename));
21386 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21387 the debugger has used the second (possibly duplicate) line number
21388 at the beginning of the function to mark the end of the prologue.
21389 We could eliminate any other duplicates within the function. For
21390 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21391 that second line number entry. */
21392 /* Recall that this end-of-prologue indication is *not* the same thing
21393 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21394 to which the hook corresponds, follows the last insn that was
21395 emitted by gen_prologue. What we need is to precede the first insn
21396 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21397 insn that corresponds to something the user wrote. These may be
21398 very different locations once scheduling is enabled. */
21400 if (0 && file_num == table->file_num
21401 && line == table->line_num
21402 && discriminator == table->discrim_num
21403 && is_stmt == table->is_stmt)
21404 return;
21406 switch_to_section (current_function_section ());
21408 /* If requested, emit something human-readable. */
21409 if (flag_debug_asm)
21410 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21412 if (DWARF2_ASM_LINE_DEBUG_INFO)
21414 /* Emit the .loc directive understood by GNU as. */
21415 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21416 file_num, line, is_stmt, discriminator */
21417 fputs ("\t.loc ", asm_out_file);
21418 fprint_ul (asm_out_file, file_num);
21419 putc (' ', asm_out_file);
21420 fprint_ul (asm_out_file, line);
21421 putc (' ', asm_out_file);
21422 putc ('0', asm_out_file);
21424 if (is_stmt != table->is_stmt)
21426 fputs (" is_stmt ", asm_out_file);
21427 putc (is_stmt ? '1' : '0', asm_out_file);
21429 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21431 gcc_assert (discriminator > 0);
21432 fputs (" discriminator ", asm_out_file);
21433 fprint_ul (asm_out_file, (unsigned long) discriminator);
21435 putc ('\n', asm_out_file);
21437 else
21439 unsigned int label_num = ++line_info_label_num;
21441 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21443 push_dw_line_info_entry (table, LI_set_address, label_num);
21444 if (file_num != table->file_num)
21445 push_dw_line_info_entry (table, LI_set_file, file_num);
21446 if (discriminator != table->discrim_num)
21447 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21448 if (is_stmt != table->is_stmt)
21449 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21450 push_dw_line_info_entry (table, LI_set_line, line);
21453 table->file_num = file_num;
21454 table->line_num = line;
21455 table->discrim_num = discriminator;
21456 table->is_stmt = is_stmt;
21457 table->in_use = true;
21460 /* Record the beginning of a new source file. */
21462 static void
21463 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21465 if (flag_eliminate_dwarf2_dups)
21467 /* Record the beginning of the file for break_out_includes. */
21468 dw_die_ref bincl_die;
21470 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21471 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21474 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21476 macinfo_entry e;
21477 e.code = DW_MACINFO_start_file;
21478 e.lineno = lineno;
21479 e.info = ggc_strdup (filename);
21480 vec_safe_push (macinfo_table, e);
21484 /* Record the end of a source file. */
21486 static void
21487 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21489 if (flag_eliminate_dwarf2_dups)
21490 /* Record the end of the file for break_out_includes. */
21491 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21493 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21495 macinfo_entry e;
21496 e.code = DW_MACINFO_end_file;
21497 e.lineno = lineno;
21498 e.info = NULL;
21499 vec_safe_push (macinfo_table, e);
21503 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21504 the tail part of the directive line, i.e. the part which is past the
21505 initial whitespace, #, whitespace, directive-name, whitespace part. */
21507 static void
21508 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21509 const char *buffer ATTRIBUTE_UNUSED)
21511 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21513 macinfo_entry e;
21514 /* Insert a dummy first entry to be able to optimize the whole
21515 predefined macro block using DW_MACRO_GNU_transparent_include. */
21516 if (macinfo_table->is_empty () && lineno <= 1)
21518 e.code = 0;
21519 e.lineno = 0;
21520 e.info = NULL;
21521 vec_safe_push (macinfo_table, e);
21523 e.code = DW_MACINFO_define;
21524 e.lineno = lineno;
21525 e.info = ggc_strdup (buffer);
21526 vec_safe_push (macinfo_table, e);
21530 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21531 the tail part of the directive line, i.e. the part which is past the
21532 initial whitespace, #, whitespace, directive-name, whitespace part. */
21534 static void
21535 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21536 const char *buffer ATTRIBUTE_UNUSED)
21538 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21540 macinfo_entry e;
21541 /* Insert a dummy first entry to be able to optimize the whole
21542 predefined macro block using DW_MACRO_GNU_transparent_include. */
21543 if (macinfo_table->is_empty () && lineno <= 1)
21545 e.code = 0;
21546 e.lineno = 0;
21547 e.info = NULL;
21548 vec_safe_push (macinfo_table, e);
21550 e.code = DW_MACINFO_undef;
21551 e.lineno = lineno;
21552 e.info = ggc_strdup (buffer);
21553 vec_safe_push (macinfo_table, e);
21557 /* Helpers to manipulate hash table of CUs. */
21559 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21561 typedef macinfo_entry value_type;
21562 typedef macinfo_entry compare_type;
21563 static inline hashval_t hash (const value_type *);
21564 static inline bool equal (const value_type *, const compare_type *);
21567 inline hashval_t
21568 macinfo_entry_hasher::hash (const value_type *entry)
21570 return htab_hash_string (entry->info);
21573 inline bool
21574 macinfo_entry_hasher::equal (const value_type *entry1,
21575 const compare_type *entry2)
21577 return !strcmp (entry1->info, entry2->info);
21580 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21582 /* Output a single .debug_macinfo entry. */
21584 static void
21585 output_macinfo_op (macinfo_entry *ref)
21587 int file_num;
21588 size_t len;
21589 struct indirect_string_node *node;
21590 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21591 struct dwarf_file_data *fd;
21593 switch (ref->code)
21595 case DW_MACINFO_start_file:
21596 fd = lookup_filename (ref->info);
21597 file_num = maybe_emit_file (fd);
21598 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21599 dw2_asm_output_data_uleb128 (ref->lineno,
21600 "Included from line number %lu",
21601 (unsigned long) ref->lineno);
21602 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21603 break;
21604 case DW_MACINFO_end_file:
21605 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21606 break;
21607 case DW_MACINFO_define:
21608 case DW_MACINFO_undef:
21609 len = strlen (ref->info) + 1;
21610 if (!dwarf_strict
21611 && len > DWARF_OFFSET_SIZE
21612 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21613 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21615 ref->code = ref->code == DW_MACINFO_define
21616 ? DW_MACRO_GNU_define_indirect
21617 : DW_MACRO_GNU_undef_indirect;
21618 output_macinfo_op (ref);
21619 return;
21621 dw2_asm_output_data (1, ref->code,
21622 ref->code == DW_MACINFO_define
21623 ? "Define macro" : "Undefine macro");
21624 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21625 (unsigned long) ref->lineno);
21626 dw2_asm_output_nstring (ref->info, -1, "The macro");
21627 break;
21628 case DW_MACRO_GNU_define_indirect:
21629 case DW_MACRO_GNU_undef_indirect:
21630 node = find_AT_string (ref->info);
21631 gcc_assert (node
21632 && ((node->form == DW_FORM_strp)
21633 || (node->form == DW_FORM_GNU_str_index)));
21634 dw2_asm_output_data (1, ref->code,
21635 ref->code == DW_MACRO_GNU_define_indirect
21636 ? "Define macro indirect"
21637 : "Undefine macro indirect");
21638 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21639 (unsigned long) ref->lineno);
21640 if (node->form == DW_FORM_strp)
21641 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21642 debug_str_section, "The macro: \"%s\"",
21643 ref->info);
21644 else
21645 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21646 ref->info);
21647 break;
21648 case DW_MACRO_GNU_transparent_include:
21649 dw2_asm_output_data (1, ref->code, "Transparent include");
21650 ASM_GENERATE_INTERNAL_LABEL (label,
21651 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21652 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21653 break;
21654 default:
21655 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21656 ASM_COMMENT_START, (unsigned long) ref->code);
21657 break;
21661 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21662 other compilation unit .debug_macinfo sections. IDX is the first
21663 index of a define/undef, return the number of ops that should be
21664 emitted in a comdat .debug_macinfo section and emit
21665 a DW_MACRO_GNU_transparent_include entry referencing it.
21666 If the define/undef entry should be emitted normally, return 0. */
21668 static unsigned
21669 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21670 macinfo_hash_type *macinfo_htab)
21672 macinfo_entry *first, *second, *cur, *inc;
21673 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21674 unsigned char checksum[16];
21675 struct md5_ctx ctx;
21676 char *grp_name, *tail;
21677 const char *base;
21678 unsigned int i, count, encoded_filename_len, linebuf_len;
21679 macinfo_entry **slot;
21681 first = &(*macinfo_table)[idx];
21682 second = &(*macinfo_table)[idx + 1];
21684 /* Optimize only if there are at least two consecutive define/undef ops,
21685 and either all of them are before first DW_MACINFO_start_file
21686 with lineno {0,1} (i.e. predefined macro block), or all of them are
21687 in some included header file. */
21688 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21689 return 0;
21690 if (vec_safe_is_empty (files))
21692 if (first->lineno > 1 || second->lineno > 1)
21693 return 0;
21695 else if (first->lineno == 0)
21696 return 0;
21698 /* Find the last define/undef entry that can be grouped together
21699 with first and at the same time compute md5 checksum of their
21700 codes, linenumbers and strings. */
21701 md5_init_ctx (&ctx);
21702 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21703 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21704 break;
21705 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21706 break;
21707 else
21709 unsigned char code = cur->code;
21710 md5_process_bytes (&code, 1, &ctx);
21711 checksum_uleb128 (cur->lineno, &ctx);
21712 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21714 md5_finish_ctx (&ctx, checksum);
21715 count = i - idx;
21717 /* From the containing include filename (if any) pick up just
21718 usable characters from its basename. */
21719 if (vec_safe_is_empty (files))
21720 base = "";
21721 else
21722 base = lbasename (files->last ().info);
21723 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21724 if (ISIDNUM (base[i]) || base[i] == '.')
21725 encoded_filename_len++;
21726 /* Count . at the end. */
21727 if (encoded_filename_len)
21728 encoded_filename_len++;
21730 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21731 linebuf_len = strlen (linebuf);
21733 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21734 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21735 + 16 * 2 + 1);
21736 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21737 tail = grp_name + 4;
21738 if (encoded_filename_len)
21740 for (i = 0; base[i]; i++)
21741 if (ISIDNUM (base[i]) || base[i] == '.')
21742 *tail++ = base[i];
21743 *tail++ = '.';
21745 memcpy (tail, linebuf, linebuf_len);
21746 tail += linebuf_len;
21747 *tail++ = '.';
21748 for (i = 0; i < 16; i++)
21749 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21751 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21752 in the empty vector entry before the first define/undef. */
21753 inc = &(*macinfo_table)[idx - 1];
21754 inc->code = DW_MACRO_GNU_transparent_include;
21755 inc->lineno = 0;
21756 inc->info = ggc_strdup (grp_name);
21757 if (!macinfo_htab->is_created ())
21758 macinfo_htab->create (10);
21759 /* Avoid emitting duplicates. */
21760 slot = macinfo_htab->find_slot (inc, INSERT);
21761 if (*slot != NULL)
21763 inc->code = 0;
21764 inc->info = NULL;
21765 /* If such an entry has been used before, just emit
21766 a DW_MACRO_GNU_transparent_include op. */
21767 inc = *slot;
21768 output_macinfo_op (inc);
21769 /* And clear all macinfo_entry in the range to avoid emitting them
21770 in the second pass. */
21771 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21773 cur->code = 0;
21774 cur->info = NULL;
21777 else
21779 *slot = inc;
21780 inc->lineno = macinfo_htab->elements ();
21781 output_macinfo_op (inc);
21783 return count;
21786 /* Save any strings needed by the macinfo table in the debug str
21787 table. All strings must be collected into the table by the time
21788 index_string is called. */
21790 static void
21791 save_macinfo_strings (void)
21793 unsigned len;
21794 unsigned i;
21795 macinfo_entry *ref;
21797 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21799 switch (ref->code)
21801 /* Match the logic in output_macinfo_op to decide on
21802 indirect strings. */
21803 case DW_MACINFO_define:
21804 case DW_MACINFO_undef:
21805 len = strlen (ref->info) + 1;
21806 if (!dwarf_strict
21807 && len > DWARF_OFFSET_SIZE
21808 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21809 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21810 set_indirect_string (find_AT_string (ref->info));
21811 break;
21812 case DW_MACRO_GNU_define_indirect:
21813 case DW_MACRO_GNU_undef_indirect:
21814 set_indirect_string (find_AT_string (ref->info));
21815 break;
21816 default:
21817 break;
21822 /* Output macinfo section(s). */
21824 static void
21825 output_macinfo (void)
21827 unsigned i;
21828 unsigned long length = vec_safe_length (macinfo_table);
21829 macinfo_entry *ref;
21830 vec<macinfo_entry, va_gc> *files = NULL;
21831 macinfo_hash_type macinfo_htab;
21833 if (! length)
21834 return;
21836 /* output_macinfo* uses these interchangeably. */
21837 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21838 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21839 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21840 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21842 /* For .debug_macro emit the section header. */
21843 if (!dwarf_strict)
21845 dw2_asm_output_data (2, 4, "DWARF macro version number");
21846 if (DWARF_OFFSET_SIZE == 8)
21847 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21848 else
21849 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21850 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21851 (!dwarf_split_debug_info ? debug_line_section_label
21852 : debug_skeleton_line_section_label),
21853 debug_line_section, NULL);
21856 /* In the first loop, it emits the primary .debug_macinfo section
21857 and after each emitted op the macinfo_entry is cleared.
21858 If a longer range of define/undef ops can be optimized using
21859 DW_MACRO_GNU_transparent_include, the
21860 DW_MACRO_GNU_transparent_include op is emitted and kept in
21861 the vector before the first define/undef in the range and the
21862 whole range of define/undef ops is not emitted and kept. */
21863 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21865 switch (ref->code)
21867 case DW_MACINFO_start_file:
21868 vec_safe_push (files, *ref);
21869 break;
21870 case DW_MACINFO_end_file:
21871 if (!vec_safe_is_empty (files))
21872 files->pop ();
21873 break;
21874 case DW_MACINFO_define:
21875 case DW_MACINFO_undef:
21876 if (!dwarf_strict
21877 && HAVE_COMDAT_GROUP
21878 && vec_safe_length (files) != 1
21879 && i > 0
21880 && i + 1 < length
21881 && (*macinfo_table)[i - 1].code == 0)
21883 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21884 if (count)
21886 i += count - 1;
21887 continue;
21890 break;
21891 case 0:
21892 /* A dummy entry may be inserted at the beginning to be able
21893 to optimize the whole block of predefined macros. */
21894 if (i == 0)
21895 continue;
21896 default:
21897 break;
21899 output_macinfo_op (ref);
21900 ref->info = NULL;
21901 ref->code = 0;
21904 if (!macinfo_htab.is_created ())
21905 return;
21907 macinfo_htab.dispose ();
21909 /* If any DW_MACRO_GNU_transparent_include were used, on those
21910 DW_MACRO_GNU_transparent_include entries terminate the
21911 current chain and switch to a new comdat .debug_macinfo
21912 section and emit the define/undef entries within it. */
21913 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21914 switch (ref->code)
21916 case 0:
21917 continue;
21918 case DW_MACRO_GNU_transparent_include:
21920 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21921 tree comdat_key = get_identifier (ref->info);
21922 /* Terminate the previous .debug_macinfo section. */
21923 dw2_asm_output_data (1, 0, "End compilation unit");
21924 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21925 SECTION_DEBUG
21926 | SECTION_LINKONCE,
21927 comdat_key);
21928 ASM_GENERATE_INTERNAL_LABEL (label,
21929 DEBUG_MACRO_SECTION_LABEL,
21930 ref->lineno);
21931 ASM_OUTPUT_LABEL (asm_out_file, label);
21932 ref->code = 0;
21933 ref->info = NULL;
21934 dw2_asm_output_data (2, 4, "DWARF macro version number");
21935 if (DWARF_OFFSET_SIZE == 8)
21936 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21937 else
21938 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21940 break;
21941 case DW_MACINFO_define:
21942 case DW_MACINFO_undef:
21943 output_macinfo_op (ref);
21944 ref->code = 0;
21945 ref->info = NULL;
21946 break;
21947 default:
21948 gcc_unreachable ();
21952 /* Set up for Dwarf output at the start of compilation. */
21954 static void
21955 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21957 /* Allocate the file_table. */
21958 file_table = htab_create_ggc (50, file_table_hash,
21959 file_table_eq, NULL);
21961 /* Allocate the decl_die_table. */
21962 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21963 decl_die_table_eq, NULL);
21965 /* Allocate the decl_loc_table. */
21966 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21967 decl_loc_table_eq, NULL);
21969 /* Allocate the cached_dw_loc_list_table. */
21970 cached_dw_loc_list_table
21971 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21972 cached_dw_loc_list_table_eq, NULL);
21974 /* Allocate the initial hunk of the decl_scope_table. */
21975 vec_alloc (decl_scope_table, 256);
21977 /* Allocate the initial hunk of the abbrev_die_table. */
21978 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21979 (ABBREV_DIE_TABLE_INCREMENT);
21980 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21981 /* Zero-th entry is allocated, but unused. */
21982 abbrev_die_table_in_use = 1;
21984 /* Allocate the pubtypes and pubnames vectors. */
21985 vec_alloc (pubname_table, 32);
21986 vec_alloc (pubtype_table, 32);
21988 vec_alloc (incomplete_types, 64);
21990 vec_alloc (used_rtx_array, 32);
21992 if (!dwarf_split_debug_info)
21994 debug_info_section = get_section (DEBUG_INFO_SECTION,
21995 SECTION_DEBUG, NULL);
21996 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21997 SECTION_DEBUG, NULL);
21998 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21999 SECTION_DEBUG, NULL);
22001 else
22003 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22004 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22005 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22006 SECTION_DEBUG | SECTION_EXCLUDE,
22007 NULL);
22008 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22009 SECTION_DEBUG, NULL);
22010 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22011 SECTION_DEBUG, NULL);
22012 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22013 SECTION_DEBUG, NULL);
22014 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22015 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22017 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22018 the main .o, but the skeleton_line goes into the split off dwo. */
22019 debug_skeleton_line_section
22020 = get_section (DEBUG_DWO_LINE_SECTION,
22021 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22022 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22023 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22024 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22025 SECTION_DEBUG | SECTION_EXCLUDE,
22026 NULL);
22027 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22028 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22029 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22030 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22031 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22032 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22034 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22035 SECTION_DEBUG, NULL);
22036 debug_macinfo_section = get_section (dwarf_strict
22037 ? DEBUG_MACINFO_SECTION
22038 : DEBUG_MACRO_SECTION,
22039 DEBUG_MACRO_SECTION_FLAGS, NULL);
22040 debug_line_section = get_section (DEBUG_LINE_SECTION,
22041 SECTION_DEBUG, NULL);
22042 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22043 SECTION_DEBUG, NULL);
22044 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22045 SECTION_DEBUG, NULL);
22046 debug_str_section = get_section (DEBUG_STR_SECTION,
22047 DEBUG_STR_SECTION_FLAGS, NULL);
22048 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22049 SECTION_DEBUG, NULL);
22050 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22051 SECTION_DEBUG, NULL);
22053 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22054 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22055 DEBUG_ABBREV_SECTION_LABEL, 0);
22056 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22057 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22058 COLD_TEXT_SECTION_LABEL, 0);
22059 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22061 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22062 DEBUG_INFO_SECTION_LABEL, 0);
22063 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22064 DEBUG_LINE_SECTION_LABEL, 0);
22065 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22066 DEBUG_RANGES_SECTION_LABEL, 0);
22067 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22068 DEBUG_ADDR_SECTION_LABEL, 0);
22069 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22070 dwarf_strict
22071 ? DEBUG_MACINFO_SECTION_LABEL
22072 : DEBUG_MACRO_SECTION_LABEL, 0);
22073 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22075 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22076 vec_alloc (macinfo_table, 64);
22078 switch_to_section (text_section);
22079 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22081 /* Make sure the line number table for .text always exists. */
22082 text_section_line_info = new_line_info_table ();
22083 text_section_line_info->end_label = text_end_label;
22086 /* Called before compile () starts outputtting functions, variables
22087 and toplevel asms into assembly. */
22089 static void
22090 dwarf2out_assembly_start (void)
22092 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22093 && dwarf2out_do_cfi_asm ()
22094 && (!(flag_unwind_tables || flag_exceptions)
22095 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22096 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22099 /* A helper function for dwarf2out_finish called through
22100 htab_traverse. Assign a string its index. All strings must be
22101 collected into the table by the time index_string is called,
22102 because the indexing code relies on htab_traverse to traverse nodes
22103 in the same order for each run. */
22105 static int
22106 index_string (void **h, void *v)
22108 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22109 unsigned int *index = (unsigned int *) v;
22111 find_string_form (node);
22112 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22114 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22115 node->index = *index;
22116 *index += 1;
22118 return 1;
22121 /* A helper function for output_indirect_strings called through
22122 htab_traverse. Output the offset to a string and update the
22123 current offset. */
22125 static int
22126 output_index_string_offset (void **h, void *v)
22128 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22129 unsigned int *offset = (unsigned int *) v;
22131 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22133 /* Assert that this node has been assigned an index. */
22134 gcc_assert (node->index != NO_INDEX_ASSIGNED
22135 && node->index != NOT_INDEXED);
22136 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22137 "indexed string 0x%x: %s", node->index, node->str);
22138 *offset += strlen (node->str) + 1;
22140 return 1;
22143 /* A helper function for dwarf2out_finish called through
22144 htab_traverse. Output the indexed string. */
22146 static int
22147 output_index_string (void **h, void *v)
22149 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22150 unsigned int *cur_idx = (unsigned int *) v;
22152 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22154 /* Assert that the strings are output in the same order as their
22155 indexes were assigned. */
22156 gcc_assert (*cur_idx == node->index);
22157 assemble_string (node->str, strlen (node->str) + 1);
22158 *cur_idx += 1;
22160 return 1;
22163 /* A helper function for dwarf2out_finish called through
22164 htab_traverse. Emit one queued .debug_str string. */
22166 static int
22167 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22169 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22171 node->form = find_string_form (node);
22172 if (node->form == DW_FORM_strp && node->refcount > 0)
22174 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22175 assemble_string (node->str, strlen (node->str) + 1);
22178 return 1;
22181 /* Output the indexed string table. */
22183 static void
22184 output_indirect_strings (void)
22186 switch_to_section (debug_str_section);
22187 if (!dwarf_split_debug_info)
22188 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22189 else
22191 unsigned int offset = 0;
22192 unsigned int cur_idx = 0;
22194 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22196 switch_to_section (debug_str_offsets_section);
22197 htab_traverse_noresize (debug_str_hash,
22198 output_index_string_offset,
22199 &offset);
22200 switch_to_section (debug_str_dwo_section);
22201 htab_traverse_noresize (debug_str_hash,
22202 output_index_string,
22203 &cur_idx);
22207 /* Callback for htab_traverse to assign an index to an entry in the
22208 table, and to write that entry to the .debug_addr section. */
22210 static int
22211 output_addr_table_entry (void **slot, void *data)
22213 addr_table_entry *entry = (addr_table_entry *) *slot;
22214 unsigned int *cur_index = (unsigned int *)data;
22216 if (entry->refcount == 0)
22218 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22219 || entry->index == NOT_INDEXED);
22220 return 1;
22223 gcc_assert (entry->index == *cur_index);
22224 (*cur_index)++;
22226 switch (entry->kind)
22228 case ate_kind_rtx:
22229 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22230 "0x%x", entry->index);
22231 break;
22232 case ate_kind_rtx_dtprel:
22233 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22234 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22235 DWARF2_ADDR_SIZE,
22236 entry->addr.rtl);
22237 fputc ('\n', asm_out_file);
22238 break;
22239 case ate_kind_label:
22240 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22241 "0x%x", entry->index);
22242 break;
22243 default:
22244 gcc_unreachable ();
22246 return 1;
22249 /* Produce the .debug_addr section. */
22251 static void
22252 output_addr_table (void)
22254 unsigned int index = 0;
22255 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22256 return;
22258 switch_to_section (debug_addr_section);
22259 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22262 #if ENABLE_ASSERT_CHECKING
22263 /* Verify that all marks are clear. */
22265 static void
22266 verify_marks_clear (dw_die_ref die)
22268 dw_die_ref c;
22270 gcc_assert (! die->die_mark);
22271 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22273 #endif /* ENABLE_ASSERT_CHECKING */
22275 /* Clear the marks for a die and its children.
22276 Be cool if the mark isn't set. */
22278 static void
22279 prune_unmark_dies (dw_die_ref die)
22281 dw_die_ref c;
22283 if (die->die_mark)
22284 die->die_mark = 0;
22285 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22288 /* Given DIE that we're marking as used, find any other dies
22289 it references as attributes and mark them as used. */
22291 static void
22292 prune_unused_types_walk_attribs (dw_die_ref die)
22294 dw_attr_ref a;
22295 unsigned ix;
22297 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22299 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22301 /* A reference to another DIE.
22302 Make sure that it will get emitted.
22303 If it was broken out into a comdat group, don't follow it. */
22304 if (! AT_ref (a)->comdat_type_p
22305 || a->dw_attr == DW_AT_specification)
22306 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22308 /* Set the string's refcount to 0 so that prune_unused_types_mark
22309 accounts properly for it. */
22310 if (AT_class (a) == dw_val_class_str)
22311 a->dw_attr_val.v.val_str->refcount = 0;
22315 /* Mark the generic parameters and arguments children DIEs of DIE. */
22317 static void
22318 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22320 dw_die_ref c;
22322 if (die == NULL || die->die_child == NULL)
22323 return;
22324 c = die->die_child;
22327 if (is_template_parameter (c))
22328 prune_unused_types_mark (c, 1);
22329 c = c->die_sib;
22330 } while (c && c != die->die_child);
22333 /* Mark DIE as being used. If DOKIDS is true, then walk down
22334 to DIE's children. */
22336 static void
22337 prune_unused_types_mark (dw_die_ref die, int dokids)
22339 dw_die_ref c;
22341 if (die->die_mark == 0)
22343 /* We haven't done this node yet. Mark it as used. */
22344 die->die_mark = 1;
22345 /* If this is the DIE of a generic type instantiation,
22346 mark the children DIEs that describe its generic parms and
22347 args. */
22348 prune_unused_types_mark_generic_parms_dies (die);
22350 /* We also have to mark its parents as used.
22351 (But we don't want to mark our parent's kids due to this,
22352 unless it is a class.) */
22353 if (die->die_parent)
22354 prune_unused_types_mark (die->die_parent,
22355 class_scope_p (die->die_parent));
22357 /* Mark any referenced nodes. */
22358 prune_unused_types_walk_attribs (die);
22360 /* If this node is a specification,
22361 also mark the definition, if it exists. */
22362 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22363 prune_unused_types_mark (die->die_definition, 1);
22366 if (dokids && die->die_mark != 2)
22368 /* We need to walk the children, but haven't done so yet.
22369 Remember that we've walked the kids. */
22370 die->die_mark = 2;
22372 /* If this is an array type, we need to make sure our
22373 kids get marked, even if they're types. If we're
22374 breaking out types into comdat sections, do this
22375 for all type definitions. */
22376 if (die->die_tag == DW_TAG_array_type
22377 || (use_debug_types
22378 && is_type_die (die) && ! is_declaration_die (die)))
22379 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22380 else
22381 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22385 /* For local classes, look if any static member functions were emitted
22386 and if so, mark them. */
22388 static void
22389 prune_unused_types_walk_local_classes (dw_die_ref die)
22391 dw_die_ref c;
22393 if (die->die_mark == 2)
22394 return;
22396 switch (die->die_tag)
22398 case DW_TAG_structure_type:
22399 case DW_TAG_union_type:
22400 case DW_TAG_class_type:
22401 break;
22403 case DW_TAG_subprogram:
22404 if (!get_AT_flag (die, DW_AT_declaration)
22405 || die->die_definition != NULL)
22406 prune_unused_types_mark (die, 1);
22407 return;
22409 default:
22410 return;
22413 /* Mark children. */
22414 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22417 /* Walk the tree DIE and mark types that we actually use. */
22419 static void
22420 prune_unused_types_walk (dw_die_ref die)
22422 dw_die_ref c;
22424 /* Don't do anything if this node is already marked and
22425 children have been marked as well. */
22426 if (die->die_mark == 2)
22427 return;
22429 switch (die->die_tag)
22431 case DW_TAG_structure_type:
22432 case DW_TAG_union_type:
22433 case DW_TAG_class_type:
22434 if (die->die_perennial_p)
22435 break;
22437 for (c = die->die_parent; c; c = c->die_parent)
22438 if (c->die_tag == DW_TAG_subprogram)
22439 break;
22441 /* Finding used static member functions inside of classes
22442 is needed just for local classes, because for other classes
22443 static member function DIEs with DW_AT_specification
22444 are emitted outside of the DW_TAG_*_type. If we ever change
22445 it, we'd need to call this even for non-local classes. */
22446 if (c)
22447 prune_unused_types_walk_local_classes (die);
22449 /* It's a type node --- don't mark it. */
22450 return;
22452 case DW_TAG_const_type:
22453 case DW_TAG_packed_type:
22454 case DW_TAG_pointer_type:
22455 case DW_TAG_reference_type:
22456 case DW_TAG_rvalue_reference_type:
22457 case DW_TAG_volatile_type:
22458 case DW_TAG_typedef:
22459 case DW_TAG_array_type:
22460 case DW_TAG_interface_type:
22461 case DW_TAG_friend:
22462 case DW_TAG_variant_part:
22463 case DW_TAG_enumeration_type:
22464 case DW_TAG_subroutine_type:
22465 case DW_TAG_string_type:
22466 case DW_TAG_set_type:
22467 case DW_TAG_subrange_type:
22468 case DW_TAG_ptr_to_member_type:
22469 case DW_TAG_file_type:
22470 if (die->die_perennial_p)
22471 break;
22473 /* It's a type node --- don't mark it. */
22474 return;
22476 default:
22477 /* Mark everything else. */
22478 break;
22481 if (die->die_mark == 0)
22483 die->die_mark = 1;
22485 /* Now, mark any dies referenced from here. */
22486 prune_unused_types_walk_attribs (die);
22489 die->die_mark = 2;
22491 /* Mark children. */
22492 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22495 /* Increment the string counts on strings referred to from DIE's
22496 attributes. */
22498 static void
22499 prune_unused_types_update_strings (dw_die_ref die)
22501 dw_attr_ref a;
22502 unsigned ix;
22504 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22505 if (AT_class (a) == dw_val_class_str)
22507 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22508 s->refcount++;
22509 /* Avoid unnecessarily putting strings that are used less than
22510 twice in the hash table. */
22511 if (s->refcount
22512 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22514 void ** slot;
22515 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22516 htab_hash_string (s->str),
22517 INSERT);
22518 gcc_assert (*slot == NULL);
22519 *slot = s;
22524 /* Remove from the tree DIE any dies that aren't marked. */
22526 static void
22527 prune_unused_types_prune (dw_die_ref die)
22529 dw_die_ref c;
22531 gcc_assert (die->die_mark);
22532 prune_unused_types_update_strings (die);
22534 if (! die->die_child)
22535 return;
22537 c = die->die_child;
22538 do {
22539 dw_die_ref prev = c;
22540 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22541 if (c == die->die_child)
22543 /* No marked children between 'prev' and the end of the list. */
22544 if (prev == c)
22545 /* No marked children at all. */
22546 die->die_child = NULL;
22547 else
22549 prev->die_sib = c->die_sib;
22550 die->die_child = prev;
22552 return;
22555 if (c != prev->die_sib)
22556 prev->die_sib = c;
22557 prune_unused_types_prune (c);
22558 } while (c != die->die_child);
22561 /* Remove dies representing declarations that we never use. */
22563 static void
22564 prune_unused_types (void)
22566 unsigned int i;
22567 limbo_die_node *node;
22568 comdat_type_node *ctnode;
22569 pubname_ref pub;
22570 dw_die_ref base_type;
22572 #if ENABLE_ASSERT_CHECKING
22573 /* All the marks should already be clear. */
22574 verify_marks_clear (comp_unit_die ());
22575 for (node = limbo_die_list; node; node = node->next)
22576 verify_marks_clear (node->die);
22577 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22578 verify_marks_clear (ctnode->root_die);
22579 #endif /* ENABLE_ASSERT_CHECKING */
22581 /* Mark types that are used in global variables. */
22582 premark_types_used_by_global_vars ();
22584 /* Set the mark on nodes that are actually used. */
22585 prune_unused_types_walk (comp_unit_die ());
22586 for (node = limbo_die_list; node; node = node->next)
22587 prune_unused_types_walk (node->die);
22588 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22590 prune_unused_types_walk (ctnode->root_die);
22591 prune_unused_types_mark (ctnode->type_die, 1);
22594 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22595 are unusual in that they are pubnames that are the children of pubtypes.
22596 They should only be marked via their parent DW_TAG_enumeration_type die,
22597 not as roots in themselves. */
22598 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22599 if (pub->die->die_tag != DW_TAG_enumerator)
22600 prune_unused_types_mark (pub->die, 1);
22601 for (i = 0; base_types.iterate (i, &base_type); i++)
22602 prune_unused_types_mark (base_type, 1);
22604 if (debug_str_hash)
22605 htab_empty (debug_str_hash);
22606 if (skeleton_debug_str_hash)
22607 htab_empty (skeleton_debug_str_hash);
22608 prune_unused_types_prune (comp_unit_die ());
22609 for (node = limbo_die_list; node; node = node->next)
22610 prune_unused_types_prune (node->die);
22611 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22612 prune_unused_types_prune (ctnode->root_die);
22614 /* Leave the marks clear. */
22615 prune_unmark_dies (comp_unit_die ());
22616 for (node = limbo_die_list; node; node = node->next)
22617 prune_unmark_dies (node->die);
22618 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22619 prune_unmark_dies (ctnode->root_die);
22622 /* Set the parameter to true if there are any relative pathnames in
22623 the file table. */
22624 static int
22625 file_table_relative_p (void ** slot, void *param)
22627 bool *p = (bool *) param;
22628 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22629 if (!IS_ABSOLUTE_PATH (d->filename))
22631 *p = true;
22632 return 0;
22634 return 1;
22637 /* Helpers to manipulate hash table of comdat type units. */
22639 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22641 typedef comdat_type_node value_type;
22642 typedef comdat_type_node compare_type;
22643 static inline hashval_t hash (const value_type *);
22644 static inline bool equal (const value_type *, const compare_type *);
22647 inline hashval_t
22648 comdat_type_hasher::hash (const value_type *type_node)
22650 hashval_t h;
22651 memcpy (&h, type_node->signature, sizeof (h));
22652 return h;
22655 inline bool
22656 comdat_type_hasher::equal (const value_type *type_node_1,
22657 const compare_type *type_node_2)
22659 return (! memcmp (type_node_1->signature, type_node_2->signature,
22660 DWARF_TYPE_SIGNATURE_SIZE));
22663 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22664 to the location it would have been added, should we know its
22665 DECL_ASSEMBLER_NAME when we added other attributes. This will
22666 probably improve compactness of debug info, removing equivalent
22667 abbrevs, and hide any differences caused by deferring the
22668 computation of the assembler name, triggered by e.g. PCH. */
22670 static inline void
22671 move_linkage_attr (dw_die_ref die)
22673 unsigned ix = vec_safe_length (die->die_attr);
22674 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22676 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22677 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22679 while (--ix > 0)
22681 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22683 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22684 break;
22687 if (ix != vec_safe_length (die->die_attr) - 1)
22689 die->die_attr->pop ();
22690 die->die_attr->quick_insert (ix, linkage);
22694 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22695 referenced from typed stack ops and count how often they are used. */
22697 static void
22698 mark_base_types (dw_loc_descr_ref loc)
22700 dw_die_ref base_type = NULL;
22702 for (; loc; loc = loc->dw_loc_next)
22704 switch (loc->dw_loc_opc)
22706 case DW_OP_GNU_regval_type:
22707 case DW_OP_GNU_deref_type:
22708 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22709 break;
22710 case DW_OP_GNU_convert:
22711 case DW_OP_GNU_reinterpret:
22712 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22713 continue;
22714 /* FALLTHRU */
22715 case DW_OP_GNU_const_type:
22716 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22717 break;
22718 case DW_OP_GNU_entry_value:
22719 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22720 continue;
22721 default:
22722 continue;
22724 gcc_assert (base_type->die_parent == comp_unit_die ());
22725 if (base_type->die_mark)
22726 base_type->die_mark++;
22727 else
22729 base_types.safe_push (base_type);
22730 base_type->die_mark = 1;
22735 /* Comparison function for sorting marked base types. */
22737 static int
22738 base_type_cmp (const void *x, const void *y)
22740 dw_die_ref dx = *(const dw_die_ref *) x;
22741 dw_die_ref dy = *(const dw_die_ref *) y;
22742 unsigned int byte_size1, byte_size2;
22743 unsigned int encoding1, encoding2;
22744 if (dx->die_mark > dy->die_mark)
22745 return -1;
22746 if (dx->die_mark < dy->die_mark)
22747 return 1;
22748 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22749 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22750 if (byte_size1 < byte_size2)
22751 return 1;
22752 if (byte_size1 > byte_size2)
22753 return -1;
22754 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22755 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22756 if (encoding1 < encoding2)
22757 return 1;
22758 if (encoding1 > encoding2)
22759 return -1;
22760 return 0;
22763 /* Move base types marked by mark_base_types as early as possible
22764 in the CU, sorted by decreasing usage count both to make the
22765 uleb128 references as small as possible and to make sure they
22766 will have die_offset already computed by calc_die_sizes when
22767 sizes of typed stack loc ops is computed. */
22769 static void
22770 move_marked_base_types (void)
22772 unsigned int i;
22773 dw_die_ref base_type, die, c;
22775 if (base_types.is_empty ())
22776 return;
22778 /* Sort by decreasing usage count, they will be added again in that
22779 order later on. */
22780 base_types.qsort (base_type_cmp);
22781 die = comp_unit_die ();
22782 c = die->die_child;
22785 dw_die_ref prev = c;
22786 c = c->die_sib;
22787 while (c->die_mark)
22789 remove_child_with_prev (c, prev);
22790 /* As base types got marked, there must be at least
22791 one node other than DW_TAG_base_type. */
22792 gcc_assert (c != c->die_sib);
22793 c = c->die_sib;
22796 while (c != die->die_child);
22797 gcc_assert (die->die_child);
22798 c = die->die_child;
22799 for (i = 0; base_types.iterate (i, &base_type); i++)
22801 base_type->die_mark = 0;
22802 base_type->die_sib = c->die_sib;
22803 c->die_sib = base_type;
22804 c = base_type;
22808 /* Helper function for resolve_addr, attempt to resolve
22809 one CONST_STRING, return non-zero if not successful. Similarly verify that
22810 SYMBOL_REFs refer to variables emitted in the current CU. */
22812 static int
22813 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22815 rtx rtl = *addr;
22817 if (GET_CODE (rtl) == CONST_STRING)
22819 size_t len = strlen (XSTR (rtl, 0)) + 1;
22820 tree t = build_string (len, XSTR (rtl, 0));
22821 tree tlen = size_int (len - 1);
22822 TREE_TYPE (t)
22823 = build_array_type (char_type_node, build_index_type (tlen));
22824 rtl = lookup_constant_def (t);
22825 if (!rtl || !MEM_P (rtl))
22826 return 1;
22827 rtl = XEXP (rtl, 0);
22828 if (GET_CODE (rtl) == SYMBOL_REF
22829 && SYMBOL_REF_DECL (rtl)
22830 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22831 return 1;
22832 vec_safe_push (used_rtx_array, rtl);
22833 *addr = rtl;
22834 return 0;
22837 if (GET_CODE (rtl) == SYMBOL_REF
22838 && SYMBOL_REF_DECL (rtl))
22840 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22842 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22843 return 1;
22845 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22846 return 1;
22849 if (GET_CODE (rtl) == CONST
22850 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22851 return 1;
22853 return 0;
22856 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22857 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22858 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22860 static rtx
22861 string_cst_pool_decl (tree t)
22863 rtx rtl = output_constant_def (t, 1);
22864 unsigned char *array;
22865 dw_loc_descr_ref l;
22866 tree decl;
22867 size_t len;
22868 dw_die_ref ref;
22870 if (!rtl || !MEM_P (rtl))
22871 return NULL_RTX;
22872 rtl = XEXP (rtl, 0);
22873 if (GET_CODE (rtl) != SYMBOL_REF
22874 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22875 return NULL_RTX;
22877 decl = SYMBOL_REF_DECL (rtl);
22878 if (!lookup_decl_die (decl))
22880 len = TREE_STRING_LENGTH (t);
22881 vec_safe_push (used_rtx_array, rtl);
22882 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22883 array = (unsigned char *) ggc_alloc_atomic (len);
22884 memcpy (array, TREE_STRING_POINTER (t), len);
22885 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22886 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22887 l->dw_loc_oprnd2.v.val_vec.length = len;
22888 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22889 l->dw_loc_oprnd2.v.val_vec.array = array;
22890 add_AT_loc (ref, DW_AT_location, l);
22891 equate_decl_number_to_die (decl, ref);
22893 return rtl;
22896 /* Helper function of resolve_addr_in_expr. LOC is
22897 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22898 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22899 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22900 with DW_OP_GNU_implicit_pointer if possible
22901 and return true, if unsuccessful, return false. */
22903 static bool
22904 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22906 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22907 HOST_WIDE_INT offset = 0;
22908 dw_die_ref ref = NULL;
22909 tree decl;
22911 if (GET_CODE (rtl) == CONST
22912 && GET_CODE (XEXP (rtl, 0)) == PLUS
22913 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22915 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22916 rtl = XEXP (XEXP (rtl, 0), 0);
22918 if (GET_CODE (rtl) == CONST_STRING)
22920 size_t len = strlen (XSTR (rtl, 0)) + 1;
22921 tree t = build_string (len, XSTR (rtl, 0));
22922 tree tlen = size_int (len - 1);
22924 TREE_TYPE (t)
22925 = build_array_type (char_type_node, build_index_type (tlen));
22926 rtl = string_cst_pool_decl (t);
22927 if (!rtl)
22928 return false;
22930 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22932 decl = SYMBOL_REF_DECL (rtl);
22933 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22935 ref = lookup_decl_die (decl);
22936 if (ref && (get_AT (ref, DW_AT_location)
22937 || get_AT (ref, DW_AT_const_value)))
22939 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22940 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22941 loc->dw_loc_oprnd1.val_entry = NULL;
22942 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22943 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22944 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22945 loc->dw_loc_oprnd2.v.val_int = offset;
22946 return true;
22950 return false;
22953 /* Helper function for resolve_addr, handle one location
22954 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22955 the location list couldn't be resolved. */
22957 static bool
22958 resolve_addr_in_expr (dw_loc_descr_ref loc)
22960 dw_loc_descr_ref keep = NULL;
22961 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22962 switch (loc->dw_loc_opc)
22964 case DW_OP_addr:
22965 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22967 if ((prev == NULL
22968 || prev->dw_loc_opc == DW_OP_piece
22969 || prev->dw_loc_opc == DW_OP_bit_piece)
22970 && loc->dw_loc_next
22971 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22972 && !dwarf_strict
22973 && optimize_one_addr_into_implicit_ptr (loc))
22974 break;
22975 return false;
22977 break;
22978 case DW_OP_GNU_addr_index:
22979 case DW_OP_GNU_const_index:
22980 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22981 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22982 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22983 NULL))
22984 return false;
22985 break;
22986 case DW_OP_const4u:
22987 case DW_OP_const8u:
22988 if (loc->dtprel
22989 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22990 return false;
22991 break;
22992 case DW_OP_plus_uconst:
22993 if (size_of_loc_descr (loc)
22994 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22996 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22998 dw_loc_descr_ref repl
22999 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23000 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23001 add_loc_descr (&repl, loc->dw_loc_next);
23002 *loc = *repl;
23004 break;
23005 case DW_OP_implicit_value:
23006 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23007 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23008 return false;
23009 break;
23010 case DW_OP_GNU_implicit_pointer:
23011 case DW_OP_GNU_parameter_ref:
23012 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23014 dw_die_ref ref
23015 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23016 if (ref == NULL)
23017 return false;
23018 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23019 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23020 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23022 break;
23023 case DW_OP_GNU_const_type:
23024 case DW_OP_GNU_regval_type:
23025 case DW_OP_GNU_deref_type:
23026 case DW_OP_GNU_convert:
23027 case DW_OP_GNU_reinterpret:
23028 while (loc->dw_loc_next
23029 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23031 dw_die_ref base1, base2;
23032 unsigned enc1, enc2, size1, size2;
23033 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23034 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23035 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23036 else if (loc->dw_loc_oprnd1.val_class
23037 == dw_val_class_unsigned_const)
23038 break;
23039 else
23040 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23041 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23042 == dw_val_class_unsigned_const)
23043 break;
23044 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23045 gcc_assert (base1->die_tag == DW_TAG_base_type
23046 && base2->die_tag == DW_TAG_base_type);
23047 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23048 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23049 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23050 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23051 if (size1 == size2
23052 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23053 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23054 && loc != keep)
23055 || enc1 == enc2))
23057 /* Optimize away next DW_OP_GNU_convert after
23058 adjusting LOC's base type die reference. */
23059 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23060 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23061 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23062 else
23063 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23064 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23065 continue;
23067 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23068 point typed stack entry. */
23069 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23070 keep = loc->dw_loc_next;
23071 break;
23073 break;
23074 default:
23075 break;
23077 return true;
23080 /* Helper function of resolve_addr. DIE had DW_AT_location of
23081 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23082 and DW_OP_addr couldn't be resolved. resolve_addr has already
23083 removed the DW_AT_location attribute. This function attempts to
23084 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23085 to it or DW_AT_const_value attribute, if possible. */
23087 static void
23088 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23090 if (TREE_CODE (decl) != VAR_DECL
23091 || lookup_decl_die (decl) != die
23092 || DECL_EXTERNAL (decl)
23093 || !TREE_STATIC (decl)
23094 || DECL_INITIAL (decl) == NULL_TREE
23095 || DECL_P (DECL_INITIAL (decl))
23096 || get_AT (die, DW_AT_const_value))
23097 return;
23099 tree init = DECL_INITIAL (decl);
23100 HOST_WIDE_INT offset = 0;
23101 /* For variables that have been optimized away and thus
23102 don't have a memory location, see if we can emit
23103 DW_AT_const_value instead. */
23104 if (tree_add_const_value_attribute (die, init))
23105 return;
23106 if (dwarf_strict)
23107 return;
23108 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23109 and ADDR_EXPR refers to a decl that has DW_AT_location or
23110 DW_AT_const_value (but isn't addressable, otherwise
23111 resolving the original DW_OP_addr wouldn't fail), see if
23112 we can add DW_OP_GNU_implicit_pointer. */
23113 STRIP_NOPS (init);
23114 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23115 && host_integerp (TREE_OPERAND (init, 1), 0))
23117 offset = tree_low_cst (TREE_OPERAND (init, 1), 0);
23118 init = TREE_OPERAND (init, 0);
23119 STRIP_NOPS (init);
23121 if (TREE_CODE (init) != ADDR_EXPR)
23122 return;
23123 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23124 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23125 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23126 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23127 && TREE_OPERAND (init, 0) != decl))
23129 dw_die_ref ref;
23130 dw_loc_descr_ref l;
23132 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23134 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23135 if (!rtl)
23136 return;
23137 decl = SYMBOL_REF_DECL (rtl);
23139 else
23140 decl = TREE_OPERAND (init, 0);
23141 ref = lookup_decl_die (decl);
23142 if (ref == NULL
23143 || (!get_AT (ref, DW_AT_location)
23144 && !get_AT (ref, DW_AT_const_value)))
23145 return;
23146 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23147 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23148 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23149 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23150 add_AT_loc (die, DW_AT_location, l);
23154 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23155 an address in .rodata section if the string literal is emitted there,
23156 or remove the containing location list or replace DW_AT_const_value
23157 with DW_AT_location and empty location expression, if it isn't found
23158 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23159 to something that has been emitted in the current CU. */
23161 static void
23162 resolve_addr (dw_die_ref die)
23164 dw_die_ref c;
23165 dw_attr_ref a;
23166 dw_loc_list_ref *curr, *start, loc;
23167 unsigned ix;
23169 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23170 switch (AT_class (a))
23172 case dw_val_class_loc_list:
23173 start = curr = AT_loc_list_ptr (a);
23174 loc = *curr;
23175 gcc_assert (loc);
23176 /* The same list can be referenced more than once. See if we have
23177 already recorded the result from a previous pass. */
23178 if (loc->replaced)
23179 *curr = loc->dw_loc_next;
23180 else if (!loc->resolved_addr)
23182 /* As things stand, we do not expect or allow one die to
23183 reference a suffix of another die's location list chain.
23184 References must be identical or completely separate.
23185 There is therefore no need to cache the result of this
23186 pass on any list other than the first; doing so
23187 would lead to unnecessary writes. */
23188 while (*curr)
23190 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23191 if (!resolve_addr_in_expr ((*curr)->expr))
23193 dw_loc_list_ref next = (*curr)->dw_loc_next;
23194 dw_loc_descr_ref l = (*curr)->expr;
23196 if (next && (*curr)->ll_symbol)
23198 gcc_assert (!next->ll_symbol);
23199 next->ll_symbol = (*curr)->ll_symbol;
23201 if (dwarf_split_debug_info)
23202 remove_loc_list_addr_table_entries (l);
23203 *curr = next;
23205 else
23207 mark_base_types ((*curr)->expr);
23208 curr = &(*curr)->dw_loc_next;
23211 if (loc == *start)
23212 loc->resolved_addr = 1;
23213 else
23215 loc->replaced = 1;
23216 loc->dw_loc_next = *start;
23219 if (!*start)
23221 remove_AT (die, a->dw_attr);
23222 ix--;
23224 break;
23225 case dw_val_class_loc:
23227 dw_loc_descr_ref l = AT_loc (a);
23228 /* For -gdwarf-2 don't attempt to optimize
23229 DW_AT_data_member_location containing
23230 DW_OP_plus_uconst - older consumers might
23231 rely on it being that op instead of a more complex,
23232 but shorter, location description. */
23233 if ((dwarf_version > 2
23234 || a->dw_attr != DW_AT_data_member_location
23235 || l == NULL
23236 || l->dw_loc_opc != DW_OP_plus_uconst
23237 || l->dw_loc_next != NULL)
23238 && !resolve_addr_in_expr (l))
23240 if (dwarf_split_debug_info)
23241 remove_loc_list_addr_table_entries (l);
23242 if (l != NULL
23243 && l->dw_loc_next == NULL
23244 && l->dw_loc_opc == DW_OP_addr
23245 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23246 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23247 && a->dw_attr == DW_AT_location)
23249 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23250 remove_AT (die, a->dw_attr);
23251 ix--;
23252 optimize_location_into_implicit_ptr (die, decl);
23253 break;
23255 remove_AT (die, a->dw_attr);
23256 ix--;
23258 else
23259 mark_base_types (l);
23261 break;
23262 case dw_val_class_addr:
23263 if (a->dw_attr == DW_AT_const_value
23264 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23266 if (AT_index (a) != NOT_INDEXED)
23267 remove_addr_table_entry (a->dw_attr_val.val_entry);
23268 remove_AT (die, a->dw_attr);
23269 ix--;
23271 if (die->die_tag == DW_TAG_GNU_call_site
23272 && a->dw_attr == DW_AT_abstract_origin)
23274 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23275 dw_die_ref tdie = lookup_decl_die (tdecl);
23276 if (tdie == NULL
23277 && DECL_EXTERNAL (tdecl)
23278 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23280 force_decl_die (tdecl);
23281 tdie = lookup_decl_die (tdecl);
23283 if (tdie)
23285 a->dw_attr_val.val_class = dw_val_class_die_ref;
23286 a->dw_attr_val.v.val_die_ref.die = tdie;
23287 a->dw_attr_val.v.val_die_ref.external = 0;
23289 else
23291 if (AT_index (a) != NOT_INDEXED)
23292 remove_addr_table_entry (a->dw_attr_val.val_entry);
23293 remove_AT (die, a->dw_attr);
23294 ix--;
23297 break;
23298 default:
23299 break;
23302 FOR_EACH_CHILD (die, c, resolve_addr (c));
23305 /* Helper routines for optimize_location_lists.
23306 This pass tries to share identical local lists in .debug_loc
23307 section. */
23309 /* Iteratively hash operands of LOC opcode. */
23311 static hashval_t
23312 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23314 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23315 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23317 switch (loc->dw_loc_opc)
23319 case DW_OP_const4u:
23320 case DW_OP_const8u:
23321 if (loc->dtprel)
23322 goto hash_addr;
23323 /* FALLTHRU */
23324 case DW_OP_const1u:
23325 case DW_OP_const1s:
23326 case DW_OP_const2u:
23327 case DW_OP_const2s:
23328 case DW_OP_const4s:
23329 case DW_OP_const8s:
23330 case DW_OP_constu:
23331 case DW_OP_consts:
23332 case DW_OP_pick:
23333 case DW_OP_plus_uconst:
23334 case DW_OP_breg0:
23335 case DW_OP_breg1:
23336 case DW_OP_breg2:
23337 case DW_OP_breg3:
23338 case DW_OP_breg4:
23339 case DW_OP_breg5:
23340 case DW_OP_breg6:
23341 case DW_OP_breg7:
23342 case DW_OP_breg8:
23343 case DW_OP_breg9:
23344 case DW_OP_breg10:
23345 case DW_OP_breg11:
23346 case DW_OP_breg12:
23347 case DW_OP_breg13:
23348 case DW_OP_breg14:
23349 case DW_OP_breg15:
23350 case DW_OP_breg16:
23351 case DW_OP_breg17:
23352 case DW_OP_breg18:
23353 case DW_OP_breg19:
23354 case DW_OP_breg20:
23355 case DW_OP_breg21:
23356 case DW_OP_breg22:
23357 case DW_OP_breg23:
23358 case DW_OP_breg24:
23359 case DW_OP_breg25:
23360 case DW_OP_breg26:
23361 case DW_OP_breg27:
23362 case DW_OP_breg28:
23363 case DW_OP_breg29:
23364 case DW_OP_breg30:
23365 case DW_OP_breg31:
23366 case DW_OP_regx:
23367 case DW_OP_fbreg:
23368 case DW_OP_piece:
23369 case DW_OP_deref_size:
23370 case DW_OP_xderef_size:
23371 hash = iterative_hash_object (val1->v.val_int, hash);
23372 break;
23373 case DW_OP_skip:
23374 case DW_OP_bra:
23376 int offset;
23378 gcc_assert (val1->val_class == dw_val_class_loc);
23379 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23380 hash = iterative_hash_object (offset, hash);
23382 break;
23383 case DW_OP_implicit_value:
23384 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23385 switch (val2->val_class)
23387 case dw_val_class_const:
23388 hash = iterative_hash_object (val2->v.val_int, hash);
23389 break;
23390 case dw_val_class_vec:
23392 unsigned int elt_size = val2->v.val_vec.elt_size;
23393 unsigned int len = val2->v.val_vec.length;
23395 hash = iterative_hash_object (elt_size, hash);
23396 hash = iterative_hash_object (len, hash);
23397 hash = iterative_hash (val2->v.val_vec.array,
23398 len * elt_size, hash);
23400 break;
23401 case dw_val_class_const_double:
23402 hash = iterative_hash_object (val2->v.val_double.low, hash);
23403 hash = iterative_hash_object (val2->v.val_double.high, hash);
23404 break;
23405 case dw_val_class_addr:
23406 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23407 break;
23408 default:
23409 gcc_unreachable ();
23411 break;
23412 case DW_OP_bregx:
23413 case DW_OP_bit_piece:
23414 hash = iterative_hash_object (val1->v.val_int, hash);
23415 hash = iterative_hash_object (val2->v.val_int, hash);
23416 break;
23417 case DW_OP_addr:
23418 hash_addr:
23419 if (loc->dtprel)
23421 unsigned char dtprel = 0xd1;
23422 hash = iterative_hash_object (dtprel, hash);
23424 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23425 break;
23426 case DW_OP_GNU_addr_index:
23427 case DW_OP_GNU_const_index:
23429 if (loc->dtprel)
23431 unsigned char dtprel = 0xd1;
23432 hash = iterative_hash_object (dtprel, hash);
23434 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23436 break;
23437 case DW_OP_GNU_implicit_pointer:
23438 hash = iterative_hash_object (val2->v.val_int, hash);
23439 break;
23440 case DW_OP_GNU_entry_value:
23441 hash = hash_loc_operands (val1->v.val_loc, hash);
23442 break;
23443 case DW_OP_GNU_regval_type:
23444 case DW_OP_GNU_deref_type:
23446 unsigned int byte_size
23447 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23448 unsigned int encoding
23449 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23450 hash = iterative_hash_object (val1->v.val_int, hash);
23451 hash = iterative_hash_object (byte_size, hash);
23452 hash = iterative_hash_object (encoding, hash);
23454 break;
23455 case DW_OP_GNU_convert:
23456 case DW_OP_GNU_reinterpret:
23457 if (val1->val_class == dw_val_class_unsigned_const)
23459 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23460 break;
23462 /* FALLTHRU */
23463 case DW_OP_GNU_const_type:
23465 unsigned int byte_size
23466 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23467 unsigned int encoding
23468 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23469 hash = iterative_hash_object (byte_size, hash);
23470 hash = iterative_hash_object (encoding, hash);
23471 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23472 break;
23473 hash = iterative_hash_object (val2->val_class, hash);
23474 switch (val2->val_class)
23476 case dw_val_class_const:
23477 hash = iterative_hash_object (val2->v.val_int, hash);
23478 break;
23479 case dw_val_class_vec:
23481 unsigned int elt_size = val2->v.val_vec.elt_size;
23482 unsigned int len = val2->v.val_vec.length;
23484 hash = iterative_hash_object (elt_size, hash);
23485 hash = iterative_hash_object (len, hash);
23486 hash = iterative_hash (val2->v.val_vec.array,
23487 len * elt_size, hash);
23489 break;
23490 case dw_val_class_const_double:
23491 hash = iterative_hash_object (val2->v.val_double.low, hash);
23492 hash = iterative_hash_object (val2->v.val_double.high, hash);
23493 break;
23494 default:
23495 gcc_unreachable ();
23498 break;
23500 default:
23501 /* Other codes have no operands. */
23502 break;
23504 return hash;
23507 /* Iteratively hash the whole DWARF location expression LOC. */
23509 static inline hashval_t
23510 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23512 dw_loc_descr_ref l;
23513 bool sizes_computed = false;
23514 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23515 size_of_locs (loc);
23517 for (l = loc; l != NULL; l = l->dw_loc_next)
23519 enum dwarf_location_atom opc = l->dw_loc_opc;
23520 hash = iterative_hash_object (opc, hash);
23521 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23523 size_of_locs (loc);
23524 sizes_computed = true;
23526 hash = hash_loc_operands (l, hash);
23528 return hash;
23531 /* Compute hash of the whole location list LIST_HEAD. */
23533 static inline void
23534 hash_loc_list (dw_loc_list_ref list_head)
23536 dw_loc_list_ref curr = list_head;
23537 hashval_t hash = 0;
23539 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23541 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23542 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23543 if (curr->section)
23544 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23545 hash);
23546 hash = hash_locs (curr->expr, hash);
23548 list_head->hash = hash;
23551 /* Return true if X and Y opcodes have the same operands. */
23553 static inline bool
23554 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23556 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23557 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23558 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23559 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23561 switch (x->dw_loc_opc)
23563 case DW_OP_const4u:
23564 case DW_OP_const8u:
23565 if (x->dtprel)
23566 goto hash_addr;
23567 /* FALLTHRU */
23568 case DW_OP_const1u:
23569 case DW_OP_const1s:
23570 case DW_OP_const2u:
23571 case DW_OP_const2s:
23572 case DW_OP_const4s:
23573 case DW_OP_const8s:
23574 case DW_OP_constu:
23575 case DW_OP_consts:
23576 case DW_OP_pick:
23577 case DW_OP_plus_uconst:
23578 case DW_OP_breg0:
23579 case DW_OP_breg1:
23580 case DW_OP_breg2:
23581 case DW_OP_breg3:
23582 case DW_OP_breg4:
23583 case DW_OP_breg5:
23584 case DW_OP_breg6:
23585 case DW_OP_breg7:
23586 case DW_OP_breg8:
23587 case DW_OP_breg9:
23588 case DW_OP_breg10:
23589 case DW_OP_breg11:
23590 case DW_OP_breg12:
23591 case DW_OP_breg13:
23592 case DW_OP_breg14:
23593 case DW_OP_breg15:
23594 case DW_OP_breg16:
23595 case DW_OP_breg17:
23596 case DW_OP_breg18:
23597 case DW_OP_breg19:
23598 case DW_OP_breg20:
23599 case DW_OP_breg21:
23600 case DW_OP_breg22:
23601 case DW_OP_breg23:
23602 case DW_OP_breg24:
23603 case DW_OP_breg25:
23604 case DW_OP_breg26:
23605 case DW_OP_breg27:
23606 case DW_OP_breg28:
23607 case DW_OP_breg29:
23608 case DW_OP_breg30:
23609 case DW_OP_breg31:
23610 case DW_OP_regx:
23611 case DW_OP_fbreg:
23612 case DW_OP_piece:
23613 case DW_OP_deref_size:
23614 case DW_OP_xderef_size:
23615 return valx1->v.val_int == valy1->v.val_int;
23616 case DW_OP_skip:
23617 case DW_OP_bra:
23618 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23619 can cause irrelevant differences in dw_loc_addr. */
23620 gcc_assert (valx1->val_class == dw_val_class_loc
23621 && valy1->val_class == dw_val_class_loc
23622 && (dwarf_split_debug_info
23623 || x->dw_loc_addr == y->dw_loc_addr));
23624 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23625 case DW_OP_implicit_value:
23626 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23627 || valx2->val_class != valy2->val_class)
23628 return false;
23629 switch (valx2->val_class)
23631 case dw_val_class_const:
23632 return valx2->v.val_int == valy2->v.val_int;
23633 case dw_val_class_vec:
23634 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23635 && valx2->v.val_vec.length == valy2->v.val_vec.length
23636 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23637 valx2->v.val_vec.elt_size
23638 * valx2->v.val_vec.length) == 0;
23639 case dw_val_class_const_double:
23640 return valx2->v.val_double.low == valy2->v.val_double.low
23641 && valx2->v.val_double.high == valy2->v.val_double.high;
23642 case dw_val_class_addr:
23643 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23644 default:
23645 gcc_unreachable ();
23647 case DW_OP_bregx:
23648 case DW_OP_bit_piece:
23649 return valx1->v.val_int == valy1->v.val_int
23650 && valx2->v.val_int == valy2->v.val_int;
23651 case DW_OP_addr:
23652 hash_addr:
23653 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23654 case DW_OP_GNU_addr_index:
23655 case DW_OP_GNU_const_index:
23657 rtx ax1 = valx1->val_entry->addr.rtl;
23658 rtx ay1 = valy1->val_entry->addr.rtl;
23659 return rtx_equal_p (ax1, ay1);
23661 case DW_OP_GNU_implicit_pointer:
23662 return valx1->val_class == dw_val_class_die_ref
23663 && valx1->val_class == valy1->val_class
23664 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23665 && valx2->v.val_int == valy2->v.val_int;
23666 case DW_OP_GNU_entry_value:
23667 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23668 case DW_OP_GNU_const_type:
23669 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23670 || valx2->val_class != valy2->val_class)
23671 return false;
23672 switch (valx2->val_class)
23674 case dw_val_class_const:
23675 return valx2->v.val_int == valy2->v.val_int;
23676 case dw_val_class_vec:
23677 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23678 && valx2->v.val_vec.length == valy2->v.val_vec.length
23679 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23680 valx2->v.val_vec.elt_size
23681 * valx2->v.val_vec.length) == 0;
23682 case dw_val_class_const_double:
23683 return valx2->v.val_double.low == valy2->v.val_double.low
23684 && valx2->v.val_double.high == valy2->v.val_double.high;
23685 default:
23686 gcc_unreachable ();
23688 case DW_OP_GNU_regval_type:
23689 case DW_OP_GNU_deref_type:
23690 return valx1->v.val_int == valy1->v.val_int
23691 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23692 case DW_OP_GNU_convert:
23693 case DW_OP_GNU_reinterpret:
23694 if (valx1->val_class != valy1->val_class)
23695 return false;
23696 if (valx1->val_class == dw_val_class_unsigned_const)
23697 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23698 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23699 case DW_OP_GNU_parameter_ref:
23700 return valx1->val_class == dw_val_class_die_ref
23701 && valx1->val_class == valy1->val_class
23702 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23703 default:
23704 /* Other codes have no operands. */
23705 return true;
23709 /* Return true if DWARF location expressions X and Y are the same. */
23711 static inline bool
23712 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23714 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23715 if (x->dw_loc_opc != y->dw_loc_opc
23716 || x->dtprel != y->dtprel
23717 || !compare_loc_operands (x, y))
23718 break;
23719 return x == NULL && y == NULL;
23722 /* Hashtable helpers. */
23724 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23726 typedef dw_loc_list_struct value_type;
23727 typedef dw_loc_list_struct compare_type;
23728 static inline hashval_t hash (const value_type *);
23729 static inline bool equal (const value_type *, const compare_type *);
23732 /* Return precomputed hash of location list X. */
23734 inline hashval_t
23735 loc_list_hasher::hash (const value_type *x)
23737 return x->hash;
23740 /* Return true if location lists A and B are the same. */
23742 inline bool
23743 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23745 if (a == b)
23746 return 1;
23747 if (a->hash != b->hash)
23748 return 0;
23749 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23750 if (strcmp (a->begin, b->begin) != 0
23751 || strcmp (a->end, b->end) != 0
23752 || (a->section == NULL) != (b->section == NULL)
23753 || (a->section && strcmp (a->section, b->section) != 0)
23754 || !compare_locs (a->expr, b->expr))
23755 break;
23756 return a == NULL && b == NULL;
23759 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23762 /* Recursively optimize location lists referenced from DIE
23763 children and share them whenever possible. */
23765 static void
23766 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23768 dw_die_ref c;
23769 dw_attr_ref a;
23770 unsigned ix;
23771 dw_loc_list_struct **slot;
23773 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23774 if (AT_class (a) == dw_val_class_loc_list)
23776 dw_loc_list_ref list = AT_loc_list (a);
23777 /* TODO: perform some optimizations here, before hashing
23778 it and storing into the hash table. */
23779 hash_loc_list (list);
23780 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23781 if (*slot == NULL)
23782 *slot = list;
23783 else
23784 a->dw_attr_val.v.val_loc_list = *slot;
23787 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23791 /* Recursively assign each location list a unique index into the debug_addr
23792 section. */
23794 static void
23795 index_location_lists (dw_die_ref die)
23797 dw_die_ref c;
23798 dw_attr_ref a;
23799 unsigned ix;
23801 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23802 if (AT_class (a) == dw_val_class_loc_list)
23804 dw_loc_list_ref list = AT_loc_list (a);
23805 dw_loc_list_ref curr;
23806 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23808 /* Don't index an entry that has already been indexed
23809 or won't be output. */
23810 if (curr->begin_entry != NULL
23811 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23812 continue;
23814 curr->begin_entry
23815 = add_addr_table_entry (xstrdup (curr->begin),
23816 ate_kind_label);
23820 FOR_EACH_CHILD (die, c, index_location_lists (c));
23823 /* Optimize location lists referenced from DIE
23824 children and share them whenever possible. */
23826 static void
23827 optimize_location_lists (dw_die_ref die)
23829 loc_list_hash_type htab;
23830 htab.create (500);
23831 optimize_location_lists_1 (die, htab);
23832 htab.dispose ();
23835 /* Output stuff that dwarf requires at the end of every file,
23836 and generate the DWARF-2 debugging info. */
23838 static void
23839 dwarf2out_finish (const char *filename)
23841 limbo_die_node *node, *next_node;
23842 comdat_type_node *ctnode;
23843 hash_table <comdat_type_hasher> comdat_type_table;
23844 unsigned int i;
23845 dw_die_ref main_comp_unit_die;
23847 /* PCH might result in DW_AT_producer string being restored from the
23848 header compilation, so always fill it with empty string initially
23849 and overwrite only here. */
23850 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23851 producer_string = gen_producer_string ();
23852 producer->dw_attr_val.v.val_str->refcount--;
23853 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23855 gen_scheduled_generic_parms_dies ();
23856 gen_remaining_tmpl_value_param_die_attribute ();
23858 /* Add the name for the main input file now. We delayed this from
23859 dwarf2out_init to avoid complications with PCH. */
23860 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23861 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23862 add_comp_dir_attribute (comp_unit_die ());
23863 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23865 bool p = false;
23866 htab_traverse (file_table, file_table_relative_p, &p);
23867 if (p)
23868 add_comp_dir_attribute (comp_unit_die ());
23871 if (deferred_locations_list)
23872 for (i = 0; i < deferred_locations_list->length (); i++)
23874 add_location_or_const_value_attribute (
23875 (*deferred_locations_list)[i].die,
23876 (*deferred_locations_list)[i].variable,
23877 false,
23878 DW_AT_location);
23881 /* Traverse the limbo die list, and add parent/child links. The only
23882 dies without parents that should be here are concrete instances of
23883 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23884 For concrete instances, we can get the parent die from the abstract
23885 instance. */
23886 for (node = limbo_die_list; node; node = next_node)
23888 dw_die_ref die = node->die;
23889 next_node = node->next;
23891 if (die->die_parent == NULL)
23893 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23895 if (origin && origin->die_parent)
23896 add_child_die (origin->die_parent, die);
23897 else if (is_cu_die (die))
23899 else if (seen_error ())
23900 /* It's OK to be confused by errors in the input. */
23901 add_child_die (comp_unit_die (), die);
23902 else
23904 /* In certain situations, the lexical block containing a
23905 nested function can be optimized away, which results
23906 in the nested function die being orphaned. Likewise
23907 with the return type of that nested function. Force
23908 this to be a child of the containing function.
23910 It may happen that even the containing function got fully
23911 inlined and optimized out. In that case we are lost and
23912 assign the empty child. This should not be big issue as
23913 the function is likely unreachable too. */
23914 gcc_assert (node->created_for);
23916 if (DECL_P (node->created_for))
23917 origin = get_context_die (DECL_CONTEXT (node->created_for));
23918 else if (TYPE_P (node->created_for))
23919 origin = scope_die_for (node->created_for, comp_unit_die ());
23920 else
23921 origin = comp_unit_die ();
23923 add_child_die (origin, die);
23928 limbo_die_list = NULL;
23930 #if ENABLE_ASSERT_CHECKING
23932 dw_die_ref die = comp_unit_die (), c;
23933 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23935 #endif
23936 resolve_addr (comp_unit_die ());
23937 move_marked_base_types ();
23939 for (node = deferred_asm_name; node; node = node->next)
23941 tree decl = node->created_for;
23942 /* When generating LTO bytecode we can not generate new assembler
23943 names at this point and all important decls got theirs via
23944 free-lang-data. */
23945 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23946 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23948 add_linkage_attr (node->die, decl);
23949 move_linkage_attr (node->die);
23953 deferred_asm_name = NULL;
23955 /* Walk through the list of incomplete types again, trying once more to
23956 emit full debugging info for them. */
23957 retry_incomplete_types ();
23959 if (flag_eliminate_unused_debug_types)
23960 prune_unused_types ();
23962 /* Generate separate COMDAT sections for type DIEs. */
23963 if (use_debug_types)
23965 break_out_comdat_types (comp_unit_die ());
23967 /* Each new type_unit DIE was added to the limbo die list when created.
23968 Since these have all been added to comdat_type_list, clear the
23969 limbo die list. */
23970 limbo_die_list = NULL;
23972 /* For each new comdat type unit, copy declarations for incomplete
23973 types to make the new unit self-contained (i.e., no direct
23974 references to the main compile unit). */
23975 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23976 copy_decls_for_unworthy_types (ctnode->root_die);
23977 copy_decls_for_unworthy_types (comp_unit_die ());
23979 /* In the process of copying declarations from one unit to another,
23980 we may have left some declarations behind that are no longer
23981 referenced. Prune them. */
23982 prune_unused_types ();
23985 /* Generate separate CUs for each of the include files we've seen.
23986 They will go into limbo_die_list. */
23987 if (flag_eliminate_dwarf2_dups)
23988 break_out_includes (comp_unit_die ());
23990 /* Traverse the DIE's and add add sibling attributes to those DIE's
23991 that have children. */
23992 add_sibling_attributes (comp_unit_die ());
23993 for (node = limbo_die_list; node; node = node->next)
23994 add_sibling_attributes (node->die);
23995 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23996 add_sibling_attributes (ctnode->root_die);
23998 /* When splitting DWARF info, we put some attributes in the
23999 skeleton compile_unit DIE that remains in the .o, while
24000 most attributes go in the DWO compile_unit_die. */
24001 if (dwarf_split_debug_info)
24002 main_comp_unit_die = gen_compile_unit_die (NULL);
24003 else
24004 main_comp_unit_die = comp_unit_die ();
24006 /* Output a terminator label for the .text section. */
24007 switch_to_section (text_section);
24008 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24009 if (cold_text_section)
24011 switch_to_section (cold_text_section);
24012 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24015 /* We can only use the low/high_pc attributes if all of the code was
24016 in .text. */
24017 if (!have_multiple_function_sections
24018 || (dwarf_version < 3 && dwarf_strict))
24020 /* Don't add if the CU has no associated code. */
24021 if (text_section_used)
24022 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24023 text_end_label, true);
24025 else
24027 unsigned fde_idx;
24028 dw_fde_ref fde;
24029 bool range_list_added = false;
24031 if (text_section_used)
24032 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24033 text_end_label, &range_list_added, true);
24034 if (cold_text_section_used)
24035 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24036 cold_end_label, &range_list_added, true);
24038 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24040 if (DECL_IGNORED_P (fde->decl))
24041 continue;
24042 if (!fde->in_std_section)
24043 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24044 fde->dw_fde_end, &range_list_added,
24045 true);
24046 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24047 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24048 fde->dw_fde_second_end, &range_list_added,
24049 true);
24052 if (range_list_added)
24054 /* We need to give .debug_loc and .debug_ranges an appropriate
24055 "base address". Use zero so that these addresses become
24056 absolute. Historically, we've emitted the unexpected
24057 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24058 Emit both to give time for other tools to adapt. */
24059 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24060 if (! dwarf_strict && dwarf_version < 4)
24061 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24063 add_ranges (NULL);
24067 if (debug_info_level >= DINFO_LEVEL_NORMAL)
24068 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24069 debug_line_section_label);
24071 if (have_macinfo)
24072 add_AT_macptr (comp_unit_die (),
24073 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24074 macinfo_section_label);
24076 if (dwarf_split_debug_info && addr_index_table != NULL)
24078 /* optimize_location_lists calculates the size of the lists,
24079 so index them first, and assign indices to the entries.
24080 Although optimize_location_lists will remove entries from
24081 the table, it only does so for duplicates, and therefore
24082 only reduces ref_counts to 1. */
24083 unsigned int index = 0;
24084 index_location_lists (comp_unit_die ());
24085 htab_traverse_noresize (addr_index_table,
24086 index_addr_table_entry, &index);
24088 if (have_location_lists)
24089 optimize_location_lists (comp_unit_die ());
24091 save_macinfo_strings ();
24093 if (dwarf_split_debug_info)
24095 unsigned int index = 0;
24097 /* Add attributes common to skeleton compile_units and
24098 type_units. Because these attributes include strings, it
24099 must be done before freezing the string table. Top-level
24100 skeleton die attrs are added when the skeleton type unit is
24101 created, so ensure it is created by this point. */
24102 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24103 (void) get_skeleton_type_unit ();
24104 htab_traverse_noresize (debug_str_hash, index_string, &index);
24107 /* Output all of the compilation units. We put the main one last so that
24108 the offsets are available to output_pubnames. */
24109 for (node = limbo_die_list; node; node = node->next)
24110 output_comp_unit (node->die, 0);
24112 comdat_type_table.create (100);
24113 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24115 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24117 /* Don't output duplicate types. */
24118 if (*slot != HTAB_EMPTY_ENTRY)
24119 continue;
24121 /* Add a pointer to the line table for the main compilation unit
24122 so that the debugger can make sense of DW_AT_decl_file
24123 attributes. */
24124 if (debug_info_level >= DINFO_LEVEL_NORMAL)
24125 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24126 (!dwarf_split_debug_info
24127 ? debug_line_section_label
24128 : debug_skeleton_line_section_label));
24130 output_comdat_type_unit (ctnode);
24131 *slot = ctnode;
24133 comdat_type_table.dispose ();
24135 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24136 both the main_cu and all skeleton TUs. Making this call unconditional
24137 would end up either adding a second copy of the AT_pubnames attribute, or
24138 requiring a special case in add_top_level_skeleton_die_attrs. */
24139 if (!dwarf_split_debug_info)
24140 add_AT_pubnames (comp_unit_die ());
24142 if (dwarf_split_debug_info)
24144 int mark;
24145 unsigned char checksum[16];
24146 struct md5_ctx ctx;
24148 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24149 md5_init_ctx (&ctx);
24150 mark = 0;
24151 die_checksum (comp_unit_die (), &ctx, &mark);
24152 unmark_all_dies (comp_unit_die ());
24153 md5_finish_ctx (&ctx, checksum);
24155 /* Use the first 8 bytes of the checksum as the dwo_id,
24156 and add it to both comp-unit DIEs. */
24157 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24158 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24160 /* Add the base offset of the ranges table to the skeleton
24161 comp-unit DIE. */
24162 if (ranges_table_in_use)
24163 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24164 ranges_section_label);
24166 switch_to_section (debug_addr_section);
24167 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24168 output_addr_table ();
24171 /* Output the main compilation unit if non-empty or if .debug_macinfo
24172 or .debug_macro will be emitted. */
24173 output_comp_unit (comp_unit_die (), have_macinfo);
24175 if (dwarf_split_debug_info && info_section_emitted)
24176 output_skeleton_debug_sections (main_comp_unit_die);
24178 /* Output the abbreviation table. */
24179 if (abbrev_die_table_in_use != 1)
24181 switch_to_section (debug_abbrev_section);
24182 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24183 output_abbrev_section ();
24186 /* Output location list section if necessary. */
24187 if (have_location_lists)
24189 /* Output the location lists info. */
24190 switch_to_section (debug_loc_section);
24191 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24192 output_location_lists (comp_unit_die ());
24195 output_pubtables ();
24197 /* Output the address range information if a CU (.debug_info section)
24198 was emitted. We output an empty table even if we had no functions
24199 to put in it. This because the consumer has no way to tell the
24200 difference between an empty table that we omitted and failure to
24201 generate a table that would have contained data. */
24202 if (info_section_emitted)
24204 unsigned long aranges_length = size_of_aranges ();
24206 switch_to_section (debug_aranges_section);
24207 output_aranges (aranges_length);
24210 /* Output ranges section if necessary. */
24211 if (ranges_table_in_use)
24213 switch_to_section (debug_ranges_section);
24214 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24215 output_ranges ();
24218 /* Have to end the macro section. */
24219 if (have_macinfo)
24221 switch_to_section (debug_macinfo_section);
24222 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24223 output_macinfo ();
24224 dw2_asm_output_data (1, 0, "End compilation unit");
24227 /* Output the source line correspondence table. We must do this
24228 even if there is no line information. Otherwise, on an empty
24229 translation unit, we will generate a present, but empty,
24230 .debug_info section. IRIX 6.5 `nm' will then complain when
24231 examining the file. This is done late so that any filenames
24232 used by the debug_info section are marked as 'used'. */
24233 switch_to_section (debug_line_section);
24234 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24235 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24236 output_line_info (false);
24238 if (dwarf_split_debug_info && info_section_emitted)
24240 switch_to_section (debug_skeleton_line_section);
24241 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24242 output_line_info (true);
24245 /* If we emitted any indirect strings, output the string table too. */
24246 if (debug_str_hash || skeleton_debug_str_hash)
24247 output_indirect_strings ();
24250 #include "gt-dwarf2out.h"