2013-07-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / dwarf2out.c
blob87f96447fa319caeec0f83439211163ed4c9249d
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 "gimple.h"
91 #include "ira.h"
92 #include "lra.h"
93 #include "dumpfile.h"
94 #include "opts.h"
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98 static rtx cached_next_real_insn;
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103 /* Define this macro to be a nonzero value if the directory specifications
104 which are output in the debug info should end with a separator. */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107 from generating indirect strings in DWARF2 debug information, for instance
108 if your target is stuck with an old version of GDB that is unable to
109 process them properly or uses VMS Debug. */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
116 /* ??? Poison these here until it can be done generically. They've been
117 totally replaced in this file; make sure it stays that way. */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
124 /* The size of the target's pointer type. */
125 #ifndef PTR_SIZE
126 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
127 #endif
129 /* Array of RTXes referenced by the debugging information, which therefore
130 must be kept around forever. */
131 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
133 /* A pointer to the base of a list of incomplete types which might be
134 completed at some later time. incomplete_types_list needs to be a
135 vec<tree, va_gc> *because we want to tell the garbage collector about
136 it. */
137 static GTY(()) vec<tree, va_gc> *incomplete_types;
139 /* A pointer to the base of a table of references to declaration
140 scopes. This table is a display which tracks the nesting
141 of declaration scopes at the current scope and containing
142 scopes. This table is used to find the proper place to
143 define type declaration DIE's. */
144 static GTY(()) vec<tree, va_gc> *decl_scope_table;
146 /* Pointers to various DWARF2 sections. */
147 static GTY(()) section *debug_info_section;
148 static GTY(()) section *debug_skeleton_info_section;
149 static GTY(()) section *debug_abbrev_section;
150 static GTY(()) section *debug_skeleton_abbrev_section;
151 static GTY(()) section *debug_aranges_section;
152 static GTY(()) section *debug_addr_section;
153 static GTY(()) section *debug_macinfo_section;
154 static GTY(()) section *debug_line_section;
155 static GTY(()) section *debug_skeleton_line_section;
156 static GTY(()) section *debug_loc_section;
157 static GTY(()) section *debug_pubnames_section;
158 static GTY(()) section *debug_pubtypes_section;
159 static GTY(()) section *debug_str_section;
160 static GTY(()) section *debug_str_dwo_section;
161 static GTY(()) section *debug_str_offsets_section;
162 static GTY(()) section *debug_ranges_section;
163 static GTY(()) section *debug_frame_section;
165 /* Maximum size (in bytes) of an artificially generated label. */
166 #define MAX_ARTIFICIAL_LABEL_BYTES 30
168 /* According to the (draft) DWARF 3 specification, the initial length
169 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
170 bytes are 0xffffffff, followed by the length stored in the next 8
171 bytes.
173 However, the SGI/MIPS ABI uses an initial length which is equal to
174 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
176 #ifndef DWARF_INITIAL_LENGTH_SIZE
177 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
178 #endif
180 /* Round SIZE up to the nearest BOUNDARY. */
181 #define DWARF_ROUND(SIZE,BOUNDARY) \
182 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
184 /* CIE identifier. */
185 #if HOST_BITS_PER_WIDE_INT >= 64
186 #define DWARF_CIE_ID \
187 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
188 #else
189 #define DWARF_CIE_ID DW_CIE_ID
190 #endif
193 /* A vector for a table that contains frame description
194 information for each routine. */
195 #define NOT_INDEXED (-1U)
196 #define NO_INDEX_ASSIGNED (-2U)
198 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
200 struct GTY(()) indirect_string_node {
201 const char *str;
202 unsigned int refcount;
203 enum dwarf_form form;
204 char *label;
205 unsigned int index;
208 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
210 /* With split_debug_info, both the comp_dir and dwo_name go in the
211 main object file, rather than the dwo, similar to the force_direct
212 parameter elsewhere but with additional complications:
214 1) The string is needed in both the main object file and the dwo.
215 That is, the comp_dir and dwo_name will appear in both places.
217 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
218 DW_FORM_GNU_str_index.
220 3) GCC chooses the form to use late, depending on the size and
221 reference count.
223 Rather than forcing the all debug string handling functions and
224 callers to deal with these complications, simply use a separate,
225 special-cased string table for any attribute that should go in the
226 main object file. This limits the complexity to just the places
227 that need it. */
229 static GTY ((param_is (struct indirect_string_node)))
230 htab_t skeleton_debug_str_hash;
232 static GTY(()) int dw2_string_counter;
234 /* True if the compilation unit places functions in more than one section. */
235 static GTY(()) bool have_multiple_function_sections = false;
237 /* Whether the default text and cold text sections have been used at all. */
239 static GTY(()) bool text_section_used = false;
240 static GTY(()) bool cold_text_section_used = false;
242 /* The default cold text section. */
243 static GTY(()) section *cold_text_section;
245 /* Forward declarations for functions defined in this file. */
247 static char *stripattributes (const char *);
248 static void output_call_frame_info (int);
249 static void dwarf2out_note_section_used (void);
251 /* Personality decl of current unit. Used only when assembler does not support
252 personality CFI. */
253 static GTY(()) rtx current_unit_personality;
255 /* Data and reference forms for relocatable data. */
256 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
257 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
259 #ifndef DEBUG_FRAME_SECTION
260 #define DEBUG_FRAME_SECTION ".debug_frame"
261 #endif
263 #ifndef FUNC_BEGIN_LABEL
264 #define FUNC_BEGIN_LABEL "LFB"
265 #endif
267 #ifndef FUNC_END_LABEL
268 #define FUNC_END_LABEL "LFE"
269 #endif
271 #ifndef PROLOGUE_END_LABEL
272 #define PROLOGUE_END_LABEL "LPE"
273 #endif
275 #ifndef EPILOGUE_BEGIN_LABEL
276 #define EPILOGUE_BEGIN_LABEL "LEB"
277 #endif
279 #ifndef FRAME_BEGIN_LABEL
280 #define FRAME_BEGIN_LABEL "Lframe"
281 #endif
282 #define CIE_AFTER_SIZE_LABEL "LSCIE"
283 #define CIE_END_LABEL "LECIE"
284 #define FDE_LABEL "LSFDE"
285 #define FDE_AFTER_SIZE_LABEL "LASFDE"
286 #define FDE_END_LABEL "LEFDE"
287 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
288 #define LINE_NUMBER_END_LABEL "LELT"
289 #define LN_PROLOG_AS_LABEL "LASLTP"
290 #define LN_PROLOG_END_LABEL "LELTP"
291 #define DIE_LABEL_PREFIX "DW"
293 /* Match the base name of a file to the base name of a compilation unit. */
295 static int
296 matches_main_base (const char *path)
298 /* Cache the last query. */
299 static const char *last_path = NULL;
300 static int last_match = 0;
301 if (path != last_path)
303 const char *base;
304 int length = base_of_path (path, &base);
305 last_path = path;
306 last_match = (length == main_input_baselength
307 && memcmp (base, main_input_basename, length) == 0);
309 return last_match;
312 #ifdef DEBUG_DEBUG_STRUCT
314 static int
315 dump_struct_debug (tree type, enum debug_info_usage usage,
316 enum debug_struct_file criterion, int generic,
317 int matches, int result)
319 /* Find the type name. */
320 tree type_decl = TYPE_STUB_DECL (type);
321 tree t = type_decl;
322 const char *name = 0;
323 if (TREE_CODE (t) == TYPE_DECL)
324 t = DECL_NAME (t);
325 if (t)
326 name = IDENTIFIER_POINTER (t);
328 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
329 criterion,
330 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
331 matches ? "bas" : "hdr",
332 generic ? "gen" : "ord",
333 usage == DINFO_USAGE_DFN ? ";" :
334 usage == DINFO_USAGE_DIR_USE ? "." : "*",
335 result,
336 (void*) type_decl, name);
337 return result;
339 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
340 dump_struct_debug (type, usage, criterion, generic, matches, result)
342 #else
344 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
345 (result)
347 #endif
349 static bool
350 should_emit_struct_debug (tree type, enum debug_info_usage usage)
352 enum debug_struct_file criterion;
353 tree type_decl;
354 bool generic = lang_hooks.types.generic_p (type);
356 if (generic)
357 criterion = debug_struct_generic[usage];
358 else
359 criterion = debug_struct_ordinary[usage];
361 if (criterion == DINFO_STRUCT_FILE_NONE)
362 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
363 if (criterion == DINFO_STRUCT_FILE_ANY)
364 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
366 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
368 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
369 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
371 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
372 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
373 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
376 /* Return a pointer to a copy of the section string name S with all
377 attributes stripped off, and an asterisk prepended (for assemble_name). */
379 static inline char *
380 stripattributes (const char *s)
382 char *stripped = XNEWVEC (char, strlen (s) + 2);
383 char *p = stripped;
385 *p++ = '*';
387 while (*s && *s != ',')
388 *p++ = *s++;
390 *p = '\0';
391 return stripped;
394 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
395 switch to the data section instead, and write out a synthetic start label
396 for collect2 the first time around. */
398 static void
399 switch_to_eh_frame_section (bool back)
401 tree label;
403 #ifdef EH_FRAME_SECTION_NAME
404 if (eh_frame_section == 0)
406 int flags;
408 if (EH_TABLES_CAN_BE_READ_ONLY)
410 int fde_encoding;
411 int per_encoding;
412 int lsda_encoding;
414 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
415 /*global=*/0);
416 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
417 /*global=*/1);
418 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
419 /*global=*/0);
420 flags = ((! flag_pic
421 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
422 && (fde_encoding & 0x70) != DW_EH_PE_aligned
423 && (per_encoding & 0x70) != DW_EH_PE_absptr
424 && (per_encoding & 0x70) != DW_EH_PE_aligned
425 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
426 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
427 ? 0 : SECTION_WRITE);
429 else
430 flags = SECTION_WRITE;
431 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
433 #endif /* EH_FRAME_SECTION_NAME */
435 if (eh_frame_section)
436 switch_to_section (eh_frame_section);
437 else
439 /* We have no special eh_frame section. Put the information in
440 the data section and emit special labels to guide collect2. */
441 switch_to_section (data_section);
443 if (!back)
445 label = get_file_function_name ("F");
446 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
447 targetm.asm_out.globalize_label (asm_out_file,
448 IDENTIFIER_POINTER (label));
449 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
454 /* Switch [BACK] to the eh or debug frame table section, depending on
455 FOR_EH. */
457 static void
458 switch_to_frame_table_section (int for_eh, bool back)
460 if (for_eh)
461 switch_to_eh_frame_section (back);
462 else
464 if (!debug_frame_section)
465 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
466 SECTION_DEBUG, NULL);
467 switch_to_section (debug_frame_section);
471 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
473 enum dw_cfi_oprnd_type
474 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
476 switch (cfi)
478 case DW_CFA_nop:
479 case DW_CFA_GNU_window_save:
480 case DW_CFA_remember_state:
481 case DW_CFA_restore_state:
482 return dw_cfi_oprnd_unused;
484 case DW_CFA_set_loc:
485 case DW_CFA_advance_loc1:
486 case DW_CFA_advance_loc2:
487 case DW_CFA_advance_loc4:
488 case DW_CFA_MIPS_advance_loc8:
489 return dw_cfi_oprnd_addr;
491 case DW_CFA_offset:
492 case DW_CFA_offset_extended:
493 case DW_CFA_def_cfa:
494 case DW_CFA_offset_extended_sf:
495 case DW_CFA_def_cfa_sf:
496 case DW_CFA_restore:
497 case DW_CFA_restore_extended:
498 case DW_CFA_undefined:
499 case DW_CFA_same_value:
500 case DW_CFA_def_cfa_register:
501 case DW_CFA_register:
502 case DW_CFA_expression:
503 return dw_cfi_oprnd_reg_num;
505 case DW_CFA_def_cfa_offset:
506 case DW_CFA_GNU_args_size:
507 case DW_CFA_def_cfa_offset_sf:
508 return dw_cfi_oprnd_offset;
510 case DW_CFA_def_cfa_expression:
511 return dw_cfi_oprnd_loc;
513 default:
514 gcc_unreachable ();
518 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
520 enum dw_cfi_oprnd_type
521 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
523 switch (cfi)
525 case DW_CFA_def_cfa:
526 case DW_CFA_def_cfa_sf:
527 case DW_CFA_offset:
528 case DW_CFA_offset_extended_sf:
529 case DW_CFA_offset_extended:
530 return dw_cfi_oprnd_offset;
532 case DW_CFA_register:
533 return dw_cfi_oprnd_reg_num;
535 case DW_CFA_expression:
536 return dw_cfi_oprnd_loc;
538 default:
539 return dw_cfi_oprnd_unused;
543 /* Output one FDE. */
545 static void
546 output_fde (dw_fde_ref fde, bool for_eh, bool second,
547 char *section_start_label, int fde_encoding, char *augmentation,
548 bool any_lsda_needed, int lsda_encoding)
550 const char *begin, *end;
551 static unsigned int j;
552 char l1[20], l2[20];
554 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
555 /* empty */ 0);
556 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
557 for_eh + j);
558 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
559 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
560 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
561 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
562 " indicating 64-bit DWARF extension");
563 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
564 "FDE Length");
565 ASM_OUTPUT_LABEL (asm_out_file, l1);
567 if (for_eh)
568 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
569 else
570 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
571 debug_frame_section, "FDE CIE offset");
573 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
574 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
576 if (for_eh)
578 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
579 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
580 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
581 "FDE initial location");
582 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
583 end, begin, "FDE address range");
585 else
587 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
588 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
591 if (augmentation[0])
593 if (any_lsda_needed)
595 int size = size_of_encoded_value (lsda_encoding);
597 if (lsda_encoding == DW_EH_PE_aligned)
599 int offset = ( 4 /* Length */
600 + 4 /* CIE offset */
601 + 2 * size_of_encoded_value (fde_encoding)
602 + 1 /* Augmentation size */ );
603 int pad = -offset & (PTR_SIZE - 1);
605 size += pad;
606 gcc_assert (size_of_uleb128 (size) == 1);
609 dw2_asm_output_data_uleb128 (size, "Augmentation size");
611 if (fde->uses_eh_lsda)
613 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
614 fde->funcdef_number);
615 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
616 gen_rtx_SYMBOL_REF (Pmode, l1),
617 false,
618 "Language Specific Data Area");
620 else
622 if (lsda_encoding == DW_EH_PE_aligned)
623 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
624 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
625 "Language Specific Data Area (none)");
628 else
629 dw2_asm_output_data_uleb128 (0, "Augmentation size");
632 /* Loop through the Call Frame Instructions associated with this FDE. */
633 fde->dw_fde_current_label = begin;
635 size_t from, until, i;
637 from = 0;
638 until = vec_safe_length (fde->dw_fde_cfi);
640 if (fde->dw_fde_second_begin == NULL)
642 else if (!second)
643 until = fde->dw_fde_switch_cfi_index;
644 else
645 from = fde->dw_fde_switch_cfi_index;
647 for (i = from; i < until; i++)
648 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
651 /* If we are to emit a ref/link from function bodies to their frame tables,
652 do it now. This is typically performed to make sure that tables
653 associated with functions are dragged with them and not discarded in
654 garbage collecting links. We need to do this on a per function basis to
655 cope with -ffunction-sections. */
657 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
658 /* Switch to the function section, emit the ref to the tables, and
659 switch *back* into the table section. */
660 switch_to_section (function_section (fde->decl));
661 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
662 switch_to_frame_table_section (for_eh, true);
663 #endif
665 /* Pad the FDE out to an address sized boundary. */
666 ASM_OUTPUT_ALIGN (asm_out_file,
667 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
668 ASM_OUTPUT_LABEL (asm_out_file, l2);
670 j += 2;
673 /* Return true if frame description entry FDE is needed for EH. */
675 static bool
676 fde_needed_for_eh_p (dw_fde_ref fde)
678 if (flag_asynchronous_unwind_tables)
679 return true;
681 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
682 return true;
684 if (fde->uses_eh_lsda)
685 return true;
687 /* If exceptions are enabled, we have collected nothrow info. */
688 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
689 return false;
691 return true;
694 /* Output the call frame information used to record information
695 that relates to calculating the frame pointer, and records the
696 location of saved registers. */
698 static void
699 output_call_frame_info (int for_eh)
701 unsigned int i;
702 dw_fde_ref fde;
703 dw_cfi_ref cfi;
704 char l1[20], l2[20], section_start_label[20];
705 bool any_lsda_needed = false;
706 char augmentation[6];
707 int augmentation_size;
708 int fde_encoding = DW_EH_PE_absptr;
709 int per_encoding = DW_EH_PE_absptr;
710 int lsda_encoding = DW_EH_PE_absptr;
711 int return_reg;
712 rtx personality = NULL;
713 int dw_cie_version;
715 /* Don't emit a CIE if there won't be any FDEs. */
716 if (!fde_vec)
717 return;
719 /* Nothing to do if the assembler's doing it all. */
720 if (dwarf2out_do_cfi_asm ())
721 return;
723 /* If we don't have any functions we'll want to unwind out of, don't emit
724 any EH unwind information. If we make FDEs linkonce, we may have to
725 emit an empty label for an FDE that wouldn't otherwise be emitted. We
726 want to avoid having an FDE kept around when the function it refers to
727 is discarded. Example where this matters: a primary function template
728 in C++ requires EH information, an explicit specialization doesn't. */
729 if (for_eh)
731 bool any_eh_needed = false;
733 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
735 if (fde->uses_eh_lsda)
736 any_eh_needed = any_lsda_needed = true;
737 else if (fde_needed_for_eh_p (fde))
738 any_eh_needed = true;
739 else if (TARGET_USES_WEAK_UNWIND_INFO)
740 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
743 if (!any_eh_needed)
744 return;
747 /* We're going to be generating comments, so turn on app. */
748 if (flag_debug_asm)
749 app_enable ();
751 /* Switch to the proper frame section, first time. */
752 switch_to_frame_table_section (for_eh, false);
754 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
755 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
757 /* Output the CIE. */
758 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
759 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
760 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
761 dw2_asm_output_data (4, 0xffffffff,
762 "Initial length escape value indicating 64-bit DWARF extension");
763 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
764 "Length of Common Information Entry");
765 ASM_OUTPUT_LABEL (asm_out_file, l1);
767 /* Now that the CIE pointer is PC-relative for EH,
768 use 0 to identify the CIE. */
769 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
770 (for_eh ? 0 : DWARF_CIE_ID),
771 "CIE Identifier Tag");
773 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
774 use CIE version 1, unless that would produce incorrect results
775 due to overflowing the return register column. */
776 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
777 dw_cie_version = 1;
778 if (return_reg >= 256 || dwarf_version > 2)
779 dw_cie_version = 3;
780 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
782 augmentation[0] = 0;
783 augmentation_size = 0;
785 personality = current_unit_personality;
786 if (for_eh)
788 char *p;
790 /* Augmentation:
791 z Indicates that a uleb128 is present to size the
792 augmentation section.
793 L Indicates the encoding (and thus presence) of
794 an LSDA pointer in the FDE augmentation.
795 R Indicates a non-default pointer encoding for
796 FDE code pointers.
797 P Indicates the presence of an encoding + language
798 personality routine in the CIE augmentation. */
800 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
801 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
802 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
804 p = augmentation + 1;
805 if (personality)
807 *p++ = 'P';
808 augmentation_size += 1 + size_of_encoded_value (per_encoding);
809 assemble_external_libcall (personality);
811 if (any_lsda_needed)
813 *p++ = 'L';
814 augmentation_size += 1;
816 if (fde_encoding != DW_EH_PE_absptr)
818 *p++ = 'R';
819 augmentation_size += 1;
821 if (p > augmentation + 1)
823 augmentation[0] = 'z';
824 *p = '\0';
827 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
828 if (personality && per_encoding == DW_EH_PE_aligned)
830 int offset = ( 4 /* Length */
831 + 4 /* CIE Id */
832 + 1 /* CIE version */
833 + strlen (augmentation) + 1 /* Augmentation */
834 + size_of_uleb128 (1) /* Code alignment */
835 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
836 + 1 /* RA column */
837 + 1 /* Augmentation size */
838 + 1 /* Personality encoding */ );
839 int pad = -offset & (PTR_SIZE - 1);
841 augmentation_size += pad;
843 /* Augmentations should be small, so there's scarce need to
844 iterate for a solution. Die if we exceed one uleb128 byte. */
845 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
849 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
850 if (dw_cie_version >= 4)
852 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
853 dw2_asm_output_data (1, 0, "CIE Segment Size");
855 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
856 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
857 "CIE Data Alignment Factor");
859 if (dw_cie_version == 1)
860 dw2_asm_output_data (1, return_reg, "CIE RA Column");
861 else
862 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
864 if (augmentation[0])
866 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
867 if (personality)
869 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
870 eh_data_format_name (per_encoding));
871 dw2_asm_output_encoded_addr_rtx (per_encoding,
872 personality,
873 true, NULL);
876 if (any_lsda_needed)
877 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
878 eh_data_format_name (lsda_encoding));
880 if (fde_encoding != DW_EH_PE_absptr)
881 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
882 eh_data_format_name (fde_encoding));
885 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
886 output_cfi (cfi, NULL, for_eh);
888 /* Pad the CIE out to an address sized boundary. */
889 ASM_OUTPUT_ALIGN (asm_out_file,
890 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
891 ASM_OUTPUT_LABEL (asm_out_file, l2);
893 /* Loop through all of the FDE's. */
894 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
896 unsigned int k;
898 /* Don't emit EH unwind info for leaf functions that don't need it. */
899 if (for_eh && !fde_needed_for_eh_p (fde))
900 continue;
902 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
903 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
904 augmentation, any_lsda_needed, lsda_encoding);
907 if (for_eh && targetm.terminate_dw2_eh_frame_info)
908 dw2_asm_output_data (4, 0, "End of Table");
910 /* Turn off app to make assembly quicker. */
911 if (flag_debug_asm)
912 app_disable ();
915 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
917 static void
918 dwarf2out_do_cfi_startproc (bool second)
920 int enc;
921 rtx ref;
922 rtx personality = get_personality_function (current_function_decl);
924 fprintf (asm_out_file, "\t.cfi_startproc\n");
926 if (personality)
928 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
929 ref = personality;
931 /* ??? The GAS support isn't entirely consistent. We have to
932 handle indirect support ourselves, but PC-relative is done
933 in the assembler. Further, the assembler can't handle any
934 of the weirder relocation types. */
935 if (enc & DW_EH_PE_indirect)
936 ref = dw2_force_const_mem (ref, true);
938 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
939 output_addr_const (asm_out_file, ref);
940 fputc ('\n', asm_out_file);
943 if (crtl->uses_eh_lsda)
945 char lab[20];
947 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
948 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
949 current_function_funcdef_no);
950 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
951 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
953 if (enc & DW_EH_PE_indirect)
954 ref = dw2_force_const_mem (ref, true);
956 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
957 output_addr_const (asm_out_file, ref);
958 fputc ('\n', asm_out_file);
962 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
963 this allocation may be done before pass_final. */
965 dw_fde_ref
966 dwarf2out_alloc_current_fde (void)
968 dw_fde_ref fde;
970 fde = ggc_alloc_cleared_dw_fde_node ();
971 fde->decl = current_function_decl;
972 fde->funcdef_number = current_function_funcdef_no;
973 fde->fde_index = vec_safe_length (fde_vec);
974 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
975 fde->uses_eh_lsda = crtl->uses_eh_lsda;
976 fde->nothrow = crtl->nothrow;
977 fde->drap_reg = INVALID_REGNUM;
978 fde->vdrap_reg = INVALID_REGNUM;
980 /* Record the FDE associated with this function. */
981 cfun->fde = fde;
982 vec_safe_push (fde_vec, fde);
984 return fde;
987 /* Output a marker (i.e. a label) for the beginning of a function, before
988 the prologue. */
990 void
991 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
992 const char *file ATTRIBUTE_UNUSED)
994 char label[MAX_ARTIFICIAL_LABEL_BYTES];
995 char * dup_label;
996 dw_fde_ref fde;
997 section *fnsec;
998 bool do_frame;
1000 current_function_func_begin_label = NULL;
1002 do_frame = dwarf2out_do_frame ();
1004 /* ??? current_function_func_begin_label is also used by except.c for
1005 call-site information. We must emit this label if it might be used. */
1006 if (!do_frame
1007 && (!flag_exceptions
1008 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1009 return;
1011 fnsec = function_section (current_function_decl);
1012 switch_to_section (fnsec);
1013 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1014 current_function_funcdef_no);
1015 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1016 current_function_funcdef_no);
1017 dup_label = xstrdup (label);
1018 current_function_func_begin_label = dup_label;
1020 /* We can elide the fde allocation if we're not emitting debug info. */
1021 if (!do_frame)
1022 return;
1024 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1025 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1026 would include pass_dwarf2_frame. If we've not created the FDE yet,
1027 do so now. */
1028 fde = cfun->fde;
1029 if (fde == NULL)
1030 fde = dwarf2out_alloc_current_fde ();
1032 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1033 fde->dw_fde_begin = dup_label;
1034 fde->dw_fde_current_label = dup_label;
1035 fde->in_std_section = (fnsec == text_section
1036 || (cold_text_section && fnsec == cold_text_section));
1038 /* We only want to output line number information for the genuine dwarf2
1039 prologue case, not the eh frame case. */
1040 #ifdef DWARF2_DEBUGGING_INFO
1041 if (file)
1042 dwarf2out_source_line (line, file, 0, true);
1043 #endif
1045 if (dwarf2out_do_cfi_asm ())
1046 dwarf2out_do_cfi_startproc (false);
1047 else
1049 rtx personality = get_personality_function (current_function_decl);
1050 if (!current_unit_personality)
1051 current_unit_personality = personality;
1053 /* We cannot keep a current personality per function as without CFI
1054 asm, at the point where we emit the CFI data, there is no current
1055 function anymore. */
1056 if (personality && current_unit_personality != personality)
1057 sorry ("multiple EH personalities are supported only with assemblers "
1058 "supporting .cfi_personality directive");
1062 /* Output a marker (i.e. a label) for the end of the generated code
1063 for a function prologue. This gets called *after* the prologue code has
1064 been generated. */
1066 void
1067 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1068 const char *file ATTRIBUTE_UNUSED)
1070 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1072 /* Output a label to mark the endpoint of the code generated for this
1073 function. */
1074 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1075 current_function_funcdef_no);
1076 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1077 current_function_funcdef_no);
1078 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1081 /* Output a marker (i.e. a label) for the beginning of the generated code
1082 for a function epilogue. This gets called *before* the prologue code has
1083 been generated. */
1085 void
1086 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1087 const char *file ATTRIBUTE_UNUSED)
1089 dw_fde_ref fde = cfun->fde;
1090 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1092 if (fde->dw_fde_vms_begin_epilogue)
1093 return;
1095 /* Output a label to mark the endpoint of the code generated for this
1096 function. */
1097 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1098 current_function_funcdef_no);
1099 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1100 current_function_funcdef_no);
1101 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1104 /* Output a marker (i.e. a label) for the absolute end of the generated code
1105 for a function definition. This gets called *after* the epilogue code has
1106 been generated. */
1108 void
1109 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 dw_fde_ref fde;
1113 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1115 last_var_location_insn = NULL_RTX;
1116 cached_next_real_insn = NULL_RTX;
1118 if (dwarf2out_do_cfi_asm ())
1119 fprintf (asm_out_file, "\t.cfi_endproc\n");
1121 /* Output a label to mark the endpoint of the code generated for this
1122 function. */
1123 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1124 current_function_funcdef_no);
1125 ASM_OUTPUT_LABEL (asm_out_file, label);
1126 fde = cfun->fde;
1127 gcc_assert (fde != NULL);
1128 if (fde->dw_fde_second_begin == NULL)
1129 fde->dw_fde_end = xstrdup (label);
1132 void
1133 dwarf2out_frame_finish (void)
1135 /* Output call frame information. */
1136 if (targetm.debug_unwind_info () == UI_DWARF2)
1137 output_call_frame_info (0);
1139 /* Output another copy for the unwinder. */
1140 if ((flag_unwind_tables || flag_exceptions)
1141 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1142 output_call_frame_info (1);
1145 /* Note that the current function section is being used for code. */
1147 static void
1148 dwarf2out_note_section_used (void)
1150 section *sec = current_function_section ();
1151 if (sec == text_section)
1152 text_section_used = true;
1153 else if (sec == cold_text_section)
1154 cold_text_section_used = true;
1157 static void var_location_switch_text_section (void);
1158 static void set_cur_line_info_table (section *);
1160 void
1161 dwarf2out_switch_text_section (void)
1163 section *sect;
1164 dw_fde_ref fde = cfun->fde;
1166 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1168 if (!in_cold_section_p)
1170 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1171 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1172 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1174 else
1176 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1177 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1178 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1180 have_multiple_function_sections = true;
1182 /* There is no need to mark used sections when not debugging. */
1183 if (cold_text_section != NULL)
1184 dwarf2out_note_section_used ();
1186 if (dwarf2out_do_cfi_asm ())
1187 fprintf (asm_out_file, "\t.cfi_endproc\n");
1189 /* Now do the real section switch. */
1190 sect = current_function_section ();
1191 switch_to_section (sect);
1193 fde->second_in_std_section
1194 = (sect == text_section
1195 || (cold_text_section && sect == cold_text_section));
1197 if (dwarf2out_do_cfi_asm ())
1198 dwarf2out_do_cfi_startproc (true);
1200 var_location_switch_text_section ();
1202 if (cold_text_section != NULL)
1203 set_cur_line_info_table (sect);
1206 /* And now, the subset of the debugging information support code necessary
1207 for emitting location expressions. */
1209 /* Data about a single source file. */
1210 struct GTY(()) dwarf_file_data {
1211 const char * filename;
1212 int emitted_number;
1215 typedef struct GTY(()) deferred_locations_struct
1217 tree variable;
1218 dw_die_ref die;
1219 } deferred_locations;
1222 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1225 /* Describe an entry into the .debug_addr section. */
1227 enum ate_kind {
1228 ate_kind_rtx,
1229 ate_kind_rtx_dtprel,
1230 ate_kind_label
1233 typedef struct GTY(()) addr_table_entry_struct {
1234 enum ate_kind kind;
1235 unsigned int refcount;
1236 unsigned int index;
1237 union addr_table_entry_struct_union
1239 rtx GTY ((tag ("0"))) rtl;
1240 char * GTY ((tag ("1"))) label;
1242 GTY ((desc ("%1.kind"))) addr;
1244 addr_table_entry;
1246 /* Location lists are ranges + location descriptions for that range,
1247 so you can track variables that are in different places over
1248 their entire life. */
1249 typedef struct GTY(()) dw_loc_list_struct {
1250 dw_loc_list_ref dw_loc_next;
1251 const char *begin; /* Label and addr_entry for start of range */
1252 addr_table_entry *begin_entry;
1253 const char *end; /* Label for end of range */
1254 char *ll_symbol; /* Label for beginning of location list.
1255 Only on head of list */
1256 const char *section; /* Section this loclist is relative to */
1257 dw_loc_descr_ref expr;
1258 hashval_t hash;
1259 /* True if all addresses in this and subsequent lists are known to be
1260 resolved. */
1261 bool resolved_addr;
1262 /* True if this list has been replaced by dw_loc_next. */
1263 bool replaced;
1264 bool emitted;
1265 /* True if the range should be emitted even if begin and end
1266 are the same. */
1267 bool force;
1268 } dw_loc_list_node;
1270 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1272 /* Convert a DWARF stack opcode into its string name. */
1274 static const char *
1275 dwarf_stack_op_name (unsigned int op)
1277 const char *name = get_DW_OP_name (op);
1279 if (name != NULL)
1280 return name;
1282 return "OP_<unknown>";
1285 /* Return a pointer to a newly allocated location description. Location
1286 descriptions are simple expression terms that can be strung
1287 together to form more complicated location (address) descriptions. */
1289 static inline dw_loc_descr_ref
1290 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1291 unsigned HOST_WIDE_INT oprnd2)
1293 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1295 descr->dw_loc_opc = op;
1296 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1297 descr->dw_loc_oprnd1.val_entry = NULL;
1298 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1299 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1300 descr->dw_loc_oprnd2.val_entry = NULL;
1301 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1303 return descr;
1306 /* Return a pointer to a newly allocated location description for
1307 REG and OFFSET. */
1309 static inline dw_loc_descr_ref
1310 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1312 if (reg <= 31)
1313 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1314 offset, 0);
1315 else
1316 return new_loc_descr (DW_OP_bregx, reg, offset);
1319 /* Add a location description term to a location description expression. */
1321 static inline void
1322 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1324 dw_loc_descr_ref *d;
1326 /* Find the end of the chain. */
1327 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1330 *d = descr;
1333 /* Compare two location operands for exact equality. */
1335 static bool
1336 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1338 if (a->val_class != b->val_class)
1339 return false;
1340 switch (a->val_class)
1342 case dw_val_class_none:
1343 return true;
1344 case dw_val_class_addr:
1345 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1347 case dw_val_class_offset:
1348 case dw_val_class_unsigned_const:
1349 case dw_val_class_const:
1350 case dw_val_class_range_list:
1351 case dw_val_class_lineptr:
1352 case dw_val_class_macptr:
1353 /* These are all HOST_WIDE_INT, signed or unsigned. */
1354 return a->v.val_unsigned == b->v.val_unsigned;
1356 case dw_val_class_loc:
1357 return a->v.val_loc == b->v.val_loc;
1358 case dw_val_class_loc_list:
1359 return a->v.val_loc_list == b->v.val_loc_list;
1360 case dw_val_class_die_ref:
1361 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1362 case dw_val_class_fde_ref:
1363 return a->v.val_fde_index == b->v.val_fde_index;
1364 case dw_val_class_lbl_id:
1365 case dw_val_class_high_pc:
1366 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1367 case dw_val_class_str:
1368 return a->v.val_str == b->v.val_str;
1369 case dw_val_class_flag:
1370 return a->v.val_flag == b->v.val_flag;
1371 case dw_val_class_file:
1372 return a->v.val_file == b->v.val_file;
1373 case dw_val_class_decl_ref:
1374 return a->v.val_decl_ref == b->v.val_decl_ref;
1376 case dw_val_class_const_double:
1377 return (a->v.val_double.high == b->v.val_double.high
1378 && a->v.val_double.low == b->v.val_double.low);
1380 case dw_val_class_vec:
1382 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1383 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1385 return (a_len == b_len
1386 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1389 case dw_val_class_data8:
1390 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1392 case dw_val_class_vms_delta:
1393 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1394 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1396 gcc_unreachable ();
1399 /* Compare two location atoms for exact equality. */
1401 static bool
1402 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1404 if (a->dw_loc_opc != b->dw_loc_opc)
1405 return false;
1407 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1408 address size, but since we always allocate cleared storage it
1409 should be zero for other types of locations. */
1410 if (a->dtprel != b->dtprel)
1411 return false;
1413 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1414 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1417 /* Compare two complete location expressions for exact equality. */
1419 bool
1420 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1422 while (1)
1424 if (a == b)
1425 return true;
1426 if (a == NULL || b == NULL)
1427 return false;
1428 if (!loc_descr_equal_p_1 (a, b))
1429 return false;
1431 a = a->dw_loc_next;
1432 b = b->dw_loc_next;
1437 /* Add a constant OFFSET to a location expression. */
1439 static void
1440 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1442 dw_loc_descr_ref loc;
1443 HOST_WIDE_INT *p;
1445 gcc_assert (*list_head != NULL);
1447 if (!offset)
1448 return;
1450 /* Find the end of the chain. */
1451 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1454 p = NULL;
1455 if (loc->dw_loc_opc == DW_OP_fbreg
1456 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1457 p = &loc->dw_loc_oprnd1.v.val_int;
1458 else if (loc->dw_loc_opc == DW_OP_bregx)
1459 p = &loc->dw_loc_oprnd2.v.val_int;
1461 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1462 offset. Don't optimize if an signed integer overflow would happen. */
1463 if (p != NULL
1464 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1465 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1466 *p += offset;
1468 else if (offset > 0)
1469 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1471 else
1473 loc->dw_loc_next = int_loc_descriptor (-offset);
1474 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1478 /* Add a constant OFFSET to a location list. */
1480 static void
1481 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1483 dw_loc_list_ref d;
1484 for (d = list_head; d != NULL; d = d->dw_loc_next)
1485 loc_descr_plus_const (&d->expr, offset);
1488 #define DWARF_REF_SIZE \
1489 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1491 static unsigned long int get_base_type_offset (dw_die_ref);
1493 /* Return the size of a location descriptor. */
1495 static unsigned long
1496 size_of_loc_descr (dw_loc_descr_ref loc)
1498 unsigned long size = 1;
1500 switch (loc->dw_loc_opc)
1502 case DW_OP_addr:
1503 size += DWARF2_ADDR_SIZE;
1504 break;
1505 case DW_OP_GNU_addr_index:
1506 case DW_OP_GNU_const_index:
1507 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1508 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1509 break;
1510 case DW_OP_const1u:
1511 case DW_OP_const1s:
1512 size += 1;
1513 break;
1514 case DW_OP_const2u:
1515 case DW_OP_const2s:
1516 size += 2;
1517 break;
1518 case DW_OP_const4u:
1519 case DW_OP_const4s:
1520 size += 4;
1521 break;
1522 case DW_OP_const8u:
1523 case DW_OP_const8s:
1524 size += 8;
1525 break;
1526 case DW_OP_constu:
1527 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1528 break;
1529 case DW_OP_consts:
1530 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1531 break;
1532 case DW_OP_pick:
1533 size += 1;
1534 break;
1535 case DW_OP_plus_uconst:
1536 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1537 break;
1538 case DW_OP_skip:
1539 case DW_OP_bra:
1540 size += 2;
1541 break;
1542 case DW_OP_breg0:
1543 case DW_OP_breg1:
1544 case DW_OP_breg2:
1545 case DW_OP_breg3:
1546 case DW_OP_breg4:
1547 case DW_OP_breg5:
1548 case DW_OP_breg6:
1549 case DW_OP_breg7:
1550 case DW_OP_breg8:
1551 case DW_OP_breg9:
1552 case DW_OP_breg10:
1553 case DW_OP_breg11:
1554 case DW_OP_breg12:
1555 case DW_OP_breg13:
1556 case DW_OP_breg14:
1557 case DW_OP_breg15:
1558 case DW_OP_breg16:
1559 case DW_OP_breg17:
1560 case DW_OP_breg18:
1561 case DW_OP_breg19:
1562 case DW_OP_breg20:
1563 case DW_OP_breg21:
1564 case DW_OP_breg22:
1565 case DW_OP_breg23:
1566 case DW_OP_breg24:
1567 case DW_OP_breg25:
1568 case DW_OP_breg26:
1569 case DW_OP_breg27:
1570 case DW_OP_breg28:
1571 case DW_OP_breg29:
1572 case DW_OP_breg30:
1573 case DW_OP_breg31:
1574 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1575 break;
1576 case DW_OP_regx:
1577 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1578 break;
1579 case DW_OP_fbreg:
1580 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1581 break;
1582 case DW_OP_bregx:
1583 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1584 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1585 break;
1586 case DW_OP_piece:
1587 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1588 break;
1589 case DW_OP_bit_piece:
1590 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1591 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1592 break;
1593 case DW_OP_deref_size:
1594 case DW_OP_xderef_size:
1595 size += 1;
1596 break;
1597 case DW_OP_call2:
1598 size += 2;
1599 break;
1600 case DW_OP_call4:
1601 size += 4;
1602 break;
1603 case DW_OP_call_ref:
1604 size += DWARF_REF_SIZE;
1605 break;
1606 case DW_OP_implicit_value:
1607 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1608 + loc->dw_loc_oprnd1.v.val_unsigned;
1609 break;
1610 case DW_OP_GNU_implicit_pointer:
1611 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1612 break;
1613 case DW_OP_GNU_entry_value:
1615 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1616 size += size_of_uleb128 (op_size) + op_size;
1617 break;
1619 case DW_OP_GNU_const_type:
1621 unsigned long o
1622 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1623 size += size_of_uleb128 (o) + 1;
1624 switch (loc->dw_loc_oprnd2.val_class)
1626 case dw_val_class_vec:
1627 size += loc->dw_loc_oprnd2.v.val_vec.length
1628 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1629 break;
1630 case dw_val_class_const:
1631 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1632 break;
1633 case dw_val_class_const_double:
1634 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1635 break;
1636 default:
1637 gcc_unreachable ();
1639 break;
1641 case DW_OP_GNU_regval_type:
1643 unsigned long o
1644 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1645 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1646 + size_of_uleb128 (o);
1648 break;
1649 case DW_OP_GNU_deref_type:
1651 unsigned long o
1652 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1653 size += 1 + size_of_uleb128 (o);
1655 break;
1656 case DW_OP_GNU_convert:
1657 case DW_OP_GNU_reinterpret:
1658 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1659 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1660 else
1662 unsigned long o
1663 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1664 size += size_of_uleb128 (o);
1666 break;
1667 case DW_OP_GNU_parameter_ref:
1668 size += 4;
1669 break;
1670 default:
1671 break;
1674 return size;
1677 /* Return the size of a series of location descriptors. */
1679 unsigned long
1680 size_of_locs (dw_loc_descr_ref loc)
1682 dw_loc_descr_ref l;
1683 unsigned long size;
1685 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1686 field, to avoid writing to a PCH file. */
1687 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1689 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1690 break;
1691 size += size_of_loc_descr (l);
1693 if (! l)
1694 return size;
1696 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1698 l->dw_loc_addr = size;
1699 size += size_of_loc_descr (l);
1702 return size;
1705 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1706 static void get_ref_die_offset_label (char *, dw_die_ref);
1707 static unsigned long int get_ref_die_offset (dw_die_ref);
1709 /* Output location description stack opcode's operands (if any).
1710 The for_eh_or_skip parameter controls whether register numbers are
1711 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1712 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1713 info). This should be suppressed for the cases that have not been converted
1714 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1716 static void
1717 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1719 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1720 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1722 switch (loc->dw_loc_opc)
1724 #ifdef DWARF2_DEBUGGING_INFO
1725 case DW_OP_const2u:
1726 case DW_OP_const2s:
1727 dw2_asm_output_data (2, val1->v.val_int, NULL);
1728 break;
1729 case DW_OP_const4u:
1730 if (loc->dtprel)
1732 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1733 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1734 val1->v.val_addr);
1735 fputc ('\n', asm_out_file);
1736 break;
1738 /* FALLTHRU */
1739 case DW_OP_const4s:
1740 dw2_asm_output_data (4, val1->v.val_int, NULL);
1741 break;
1742 case DW_OP_const8u:
1743 if (loc->dtprel)
1745 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1746 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1747 val1->v.val_addr);
1748 fputc ('\n', asm_out_file);
1749 break;
1751 /* FALLTHRU */
1752 case DW_OP_const8s:
1753 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1754 dw2_asm_output_data (8, val1->v.val_int, NULL);
1755 break;
1756 case DW_OP_skip:
1757 case DW_OP_bra:
1759 int offset;
1761 gcc_assert (val1->val_class == dw_val_class_loc);
1762 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1764 dw2_asm_output_data (2, offset, NULL);
1766 break;
1767 case DW_OP_implicit_value:
1768 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1769 switch (val2->val_class)
1771 case dw_val_class_const:
1772 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1773 break;
1774 case dw_val_class_vec:
1776 unsigned int elt_size = val2->v.val_vec.elt_size;
1777 unsigned int len = val2->v.val_vec.length;
1778 unsigned int i;
1779 unsigned char *p;
1781 if (elt_size > sizeof (HOST_WIDE_INT))
1783 elt_size /= 2;
1784 len *= 2;
1786 for (i = 0, p = val2->v.val_vec.array;
1787 i < len;
1788 i++, p += elt_size)
1789 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1790 "fp or vector constant word %u", i);
1792 break;
1793 case dw_val_class_const_double:
1795 unsigned HOST_WIDE_INT first, second;
1797 if (WORDS_BIG_ENDIAN)
1799 first = val2->v.val_double.high;
1800 second = val2->v.val_double.low;
1802 else
1804 first = val2->v.val_double.low;
1805 second = val2->v.val_double.high;
1807 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1808 first, NULL);
1809 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1810 second, NULL);
1812 break;
1813 case dw_val_class_addr:
1814 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1815 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1816 break;
1817 default:
1818 gcc_unreachable ();
1820 break;
1821 #else
1822 case DW_OP_const2u:
1823 case DW_OP_const2s:
1824 case DW_OP_const4u:
1825 case DW_OP_const4s:
1826 case DW_OP_const8u:
1827 case DW_OP_const8s:
1828 case DW_OP_skip:
1829 case DW_OP_bra:
1830 case DW_OP_implicit_value:
1831 /* We currently don't make any attempt to make sure these are
1832 aligned properly like we do for the main unwind info, so
1833 don't support emitting things larger than a byte if we're
1834 only doing unwinding. */
1835 gcc_unreachable ();
1836 #endif
1837 case DW_OP_const1u:
1838 case DW_OP_const1s:
1839 dw2_asm_output_data (1, val1->v.val_int, NULL);
1840 break;
1841 case DW_OP_constu:
1842 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1843 break;
1844 case DW_OP_consts:
1845 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1846 break;
1847 case DW_OP_pick:
1848 dw2_asm_output_data (1, val1->v.val_int, NULL);
1849 break;
1850 case DW_OP_plus_uconst:
1851 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1852 break;
1853 case DW_OP_breg0:
1854 case DW_OP_breg1:
1855 case DW_OP_breg2:
1856 case DW_OP_breg3:
1857 case DW_OP_breg4:
1858 case DW_OP_breg5:
1859 case DW_OP_breg6:
1860 case DW_OP_breg7:
1861 case DW_OP_breg8:
1862 case DW_OP_breg9:
1863 case DW_OP_breg10:
1864 case DW_OP_breg11:
1865 case DW_OP_breg12:
1866 case DW_OP_breg13:
1867 case DW_OP_breg14:
1868 case DW_OP_breg15:
1869 case DW_OP_breg16:
1870 case DW_OP_breg17:
1871 case DW_OP_breg18:
1872 case DW_OP_breg19:
1873 case DW_OP_breg20:
1874 case DW_OP_breg21:
1875 case DW_OP_breg22:
1876 case DW_OP_breg23:
1877 case DW_OP_breg24:
1878 case DW_OP_breg25:
1879 case DW_OP_breg26:
1880 case DW_OP_breg27:
1881 case DW_OP_breg28:
1882 case DW_OP_breg29:
1883 case DW_OP_breg30:
1884 case DW_OP_breg31:
1885 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1886 break;
1887 case DW_OP_regx:
1889 unsigned r = val1->v.val_unsigned;
1890 if (for_eh_or_skip >= 0)
1891 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1892 gcc_assert (size_of_uleb128 (r)
1893 == size_of_uleb128 (val1->v.val_unsigned));
1894 dw2_asm_output_data_uleb128 (r, NULL);
1896 break;
1897 case DW_OP_fbreg:
1898 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1899 break;
1900 case DW_OP_bregx:
1902 unsigned r = val1->v.val_unsigned;
1903 if (for_eh_or_skip >= 0)
1904 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1905 gcc_assert (size_of_uleb128 (r)
1906 == size_of_uleb128 (val1->v.val_unsigned));
1907 dw2_asm_output_data_uleb128 (r, NULL);
1908 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1910 break;
1911 case DW_OP_piece:
1912 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1913 break;
1914 case DW_OP_bit_piece:
1915 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1916 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1917 break;
1918 case DW_OP_deref_size:
1919 case DW_OP_xderef_size:
1920 dw2_asm_output_data (1, val1->v.val_int, NULL);
1921 break;
1923 case DW_OP_addr:
1924 if (loc->dtprel)
1926 if (targetm.asm_out.output_dwarf_dtprel)
1928 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1929 DWARF2_ADDR_SIZE,
1930 val1->v.val_addr);
1931 fputc ('\n', asm_out_file);
1933 else
1934 gcc_unreachable ();
1936 else
1938 #ifdef DWARF2_DEBUGGING_INFO
1939 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1940 #else
1941 gcc_unreachable ();
1942 #endif
1944 break;
1946 case DW_OP_GNU_addr_index:
1947 case DW_OP_GNU_const_index:
1948 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1949 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1950 "(index into .debug_addr)");
1951 break;
1953 case DW_OP_GNU_implicit_pointer:
1955 char label[MAX_ARTIFICIAL_LABEL_BYTES
1956 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1957 gcc_assert (val1->val_class == dw_val_class_die_ref);
1958 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1959 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1960 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1962 break;
1964 case DW_OP_GNU_entry_value:
1965 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1966 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1967 break;
1969 case DW_OP_GNU_const_type:
1971 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1972 gcc_assert (o);
1973 dw2_asm_output_data_uleb128 (o, NULL);
1974 switch (val2->val_class)
1976 case dw_val_class_const:
1977 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1978 dw2_asm_output_data (1, l, NULL);
1979 dw2_asm_output_data (l, val2->v.val_int, NULL);
1980 break;
1981 case dw_val_class_vec:
1983 unsigned int elt_size = val2->v.val_vec.elt_size;
1984 unsigned int len = val2->v.val_vec.length;
1985 unsigned int i;
1986 unsigned char *p;
1988 l = len * elt_size;
1989 dw2_asm_output_data (1, l, NULL);
1990 if (elt_size > sizeof (HOST_WIDE_INT))
1992 elt_size /= 2;
1993 len *= 2;
1995 for (i = 0, p = val2->v.val_vec.array;
1996 i < len;
1997 i++, p += elt_size)
1998 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1999 "fp or vector constant word %u", i);
2001 break;
2002 case dw_val_class_const_double:
2004 unsigned HOST_WIDE_INT first, second;
2005 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2007 dw2_asm_output_data (1, 2 * l, NULL);
2008 if (WORDS_BIG_ENDIAN)
2010 first = val2->v.val_double.high;
2011 second = val2->v.val_double.low;
2013 else
2015 first = val2->v.val_double.low;
2016 second = val2->v.val_double.high;
2018 dw2_asm_output_data (l, first, NULL);
2019 dw2_asm_output_data (l, second, NULL);
2021 break;
2022 default:
2023 gcc_unreachable ();
2026 break;
2027 case DW_OP_GNU_regval_type:
2029 unsigned r = val1->v.val_unsigned;
2030 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2031 gcc_assert (o);
2032 if (for_eh_or_skip >= 0)
2034 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2035 gcc_assert (size_of_uleb128 (r)
2036 == size_of_uleb128 (val1->v.val_unsigned));
2038 dw2_asm_output_data_uleb128 (r, NULL);
2039 dw2_asm_output_data_uleb128 (o, NULL);
2041 break;
2042 case DW_OP_GNU_deref_type:
2044 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2045 gcc_assert (o);
2046 dw2_asm_output_data (1, val1->v.val_int, NULL);
2047 dw2_asm_output_data_uleb128 (o, NULL);
2049 break;
2050 case DW_OP_GNU_convert:
2051 case DW_OP_GNU_reinterpret:
2052 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2053 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2054 else
2056 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2057 gcc_assert (o);
2058 dw2_asm_output_data_uleb128 (o, NULL);
2060 break;
2062 case DW_OP_GNU_parameter_ref:
2064 unsigned long o;
2065 gcc_assert (val1->val_class == dw_val_class_die_ref);
2066 o = get_ref_die_offset (val1->v.val_die_ref.die);
2067 dw2_asm_output_data (4, o, NULL);
2069 break;
2071 default:
2072 /* Other codes have no operands. */
2073 break;
2077 /* Output a sequence of location operations.
2078 The for_eh_or_skip parameter controls whether register numbers are
2079 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2080 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2081 info). This should be suppressed for the cases that have not been converted
2082 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2084 void
2085 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2087 for (; loc != NULL; loc = loc->dw_loc_next)
2089 enum dwarf_location_atom opc = loc->dw_loc_opc;
2090 /* Output the opcode. */
2091 if (for_eh_or_skip >= 0
2092 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2094 unsigned r = (opc - DW_OP_breg0);
2095 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2096 gcc_assert (r <= 31);
2097 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2099 else if (for_eh_or_skip >= 0
2100 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2102 unsigned r = (opc - DW_OP_reg0);
2103 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2104 gcc_assert (r <= 31);
2105 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2108 dw2_asm_output_data (1, opc,
2109 "%s", dwarf_stack_op_name (opc));
2111 /* Output the operand(s) (if any). */
2112 output_loc_operands (loc, for_eh_or_skip);
2116 /* Output location description stack opcode's operands (if any).
2117 The output is single bytes on a line, suitable for .cfi_escape. */
2119 static void
2120 output_loc_operands_raw (dw_loc_descr_ref loc)
2122 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2123 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2125 switch (loc->dw_loc_opc)
2127 case DW_OP_addr:
2128 case DW_OP_GNU_addr_index:
2129 case DW_OP_GNU_const_index:
2130 case DW_OP_implicit_value:
2131 /* We cannot output addresses in .cfi_escape, only bytes. */
2132 gcc_unreachable ();
2134 case DW_OP_const1u:
2135 case DW_OP_const1s:
2136 case DW_OP_pick:
2137 case DW_OP_deref_size:
2138 case DW_OP_xderef_size:
2139 fputc (',', asm_out_file);
2140 dw2_asm_output_data_raw (1, val1->v.val_int);
2141 break;
2143 case DW_OP_const2u:
2144 case DW_OP_const2s:
2145 fputc (',', asm_out_file);
2146 dw2_asm_output_data_raw (2, val1->v.val_int);
2147 break;
2149 case DW_OP_const4u:
2150 case DW_OP_const4s:
2151 fputc (',', asm_out_file);
2152 dw2_asm_output_data_raw (4, val1->v.val_int);
2153 break;
2155 case DW_OP_const8u:
2156 case DW_OP_const8s:
2157 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2158 fputc (',', asm_out_file);
2159 dw2_asm_output_data_raw (8, val1->v.val_int);
2160 break;
2162 case DW_OP_skip:
2163 case DW_OP_bra:
2165 int offset;
2167 gcc_assert (val1->val_class == dw_val_class_loc);
2168 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2170 fputc (',', asm_out_file);
2171 dw2_asm_output_data_raw (2, offset);
2173 break;
2175 case DW_OP_regx:
2177 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2178 gcc_assert (size_of_uleb128 (r)
2179 == size_of_uleb128 (val1->v.val_unsigned));
2180 fputc (',', asm_out_file);
2181 dw2_asm_output_data_uleb128_raw (r);
2183 break;
2185 case DW_OP_constu:
2186 case DW_OP_plus_uconst:
2187 case DW_OP_piece:
2188 fputc (',', asm_out_file);
2189 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2190 break;
2192 case DW_OP_bit_piece:
2193 fputc (',', asm_out_file);
2194 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2195 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2196 break;
2198 case DW_OP_consts:
2199 case DW_OP_breg0:
2200 case DW_OP_breg1:
2201 case DW_OP_breg2:
2202 case DW_OP_breg3:
2203 case DW_OP_breg4:
2204 case DW_OP_breg5:
2205 case DW_OP_breg6:
2206 case DW_OP_breg7:
2207 case DW_OP_breg8:
2208 case DW_OP_breg9:
2209 case DW_OP_breg10:
2210 case DW_OP_breg11:
2211 case DW_OP_breg12:
2212 case DW_OP_breg13:
2213 case DW_OP_breg14:
2214 case DW_OP_breg15:
2215 case DW_OP_breg16:
2216 case DW_OP_breg17:
2217 case DW_OP_breg18:
2218 case DW_OP_breg19:
2219 case DW_OP_breg20:
2220 case DW_OP_breg21:
2221 case DW_OP_breg22:
2222 case DW_OP_breg23:
2223 case DW_OP_breg24:
2224 case DW_OP_breg25:
2225 case DW_OP_breg26:
2226 case DW_OP_breg27:
2227 case DW_OP_breg28:
2228 case DW_OP_breg29:
2229 case DW_OP_breg30:
2230 case DW_OP_breg31:
2231 case DW_OP_fbreg:
2232 fputc (',', asm_out_file);
2233 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2234 break;
2236 case DW_OP_bregx:
2238 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2239 gcc_assert (size_of_uleb128 (r)
2240 == size_of_uleb128 (val1->v.val_unsigned));
2241 fputc (',', asm_out_file);
2242 dw2_asm_output_data_uleb128_raw (r);
2243 fputc (',', asm_out_file);
2244 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2246 break;
2248 case DW_OP_GNU_implicit_pointer:
2249 case DW_OP_GNU_entry_value:
2250 case DW_OP_GNU_const_type:
2251 case DW_OP_GNU_regval_type:
2252 case DW_OP_GNU_deref_type:
2253 case DW_OP_GNU_convert:
2254 case DW_OP_GNU_reinterpret:
2255 case DW_OP_GNU_parameter_ref:
2256 gcc_unreachable ();
2257 break;
2259 default:
2260 /* Other codes have no operands. */
2261 break;
2265 void
2266 output_loc_sequence_raw (dw_loc_descr_ref loc)
2268 while (1)
2270 enum dwarf_location_atom opc = loc->dw_loc_opc;
2271 /* Output the opcode. */
2272 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2274 unsigned r = (opc - DW_OP_breg0);
2275 r = DWARF2_FRAME_REG_OUT (r, 1);
2276 gcc_assert (r <= 31);
2277 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2279 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2281 unsigned r = (opc - DW_OP_reg0);
2282 r = DWARF2_FRAME_REG_OUT (r, 1);
2283 gcc_assert (r <= 31);
2284 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2286 /* Output the opcode. */
2287 fprintf (asm_out_file, "%#x", opc);
2288 output_loc_operands_raw (loc);
2290 if (!loc->dw_loc_next)
2291 break;
2292 loc = loc->dw_loc_next;
2294 fputc (',', asm_out_file);
2298 /* This function builds a dwarf location descriptor sequence from a
2299 dw_cfa_location, adding the given OFFSET to the result of the
2300 expression. */
2302 struct dw_loc_descr_struct *
2303 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2305 struct dw_loc_descr_struct *head, *tmp;
2307 offset += cfa->offset;
2309 if (cfa->indirect)
2311 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2312 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2313 head->dw_loc_oprnd1.val_entry = NULL;
2314 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2315 add_loc_descr (&head, tmp);
2316 if (offset != 0)
2318 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2319 add_loc_descr (&head, tmp);
2322 else
2323 head = new_reg_loc_descr (cfa->reg, offset);
2325 return head;
2328 /* This function builds a dwarf location descriptor sequence for
2329 the address at OFFSET from the CFA when stack is aligned to
2330 ALIGNMENT byte. */
2332 struct dw_loc_descr_struct *
2333 build_cfa_aligned_loc (dw_cfa_location *cfa,
2334 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2336 struct dw_loc_descr_struct *head;
2337 unsigned int dwarf_fp
2338 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2340 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2341 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2343 head = new_reg_loc_descr (dwarf_fp, 0);
2344 add_loc_descr (&head, int_loc_descriptor (alignment));
2345 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2346 loc_descr_plus_const (&head, offset);
2348 else
2349 head = new_reg_loc_descr (dwarf_fp, offset);
2350 return head;
2353 /* And now, the support for symbolic debugging information. */
2355 /* .debug_str support. */
2356 static int output_indirect_string (void **, void *);
2358 static void dwarf2out_init (const char *);
2359 static void dwarf2out_finish (const char *);
2360 static void dwarf2out_assembly_start (void);
2361 static void dwarf2out_define (unsigned int, const char *);
2362 static void dwarf2out_undef (unsigned int, const char *);
2363 static void dwarf2out_start_source_file (unsigned, const char *);
2364 static void dwarf2out_end_source_file (unsigned);
2365 static void dwarf2out_function_decl (tree);
2366 static void dwarf2out_begin_block (unsigned, unsigned);
2367 static void dwarf2out_end_block (unsigned, unsigned);
2368 static bool dwarf2out_ignore_block (const_tree);
2369 static void dwarf2out_global_decl (tree);
2370 static void dwarf2out_type_decl (tree, int);
2371 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2372 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2373 dw_die_ref);
2374 static void dwarf2out_abstract_function (tree);
2375 static void dwarf2out_var_location (rtx);
2376 static void dwarf2out_begin_function (tree);
2377 static void dwarf2out_end_function (unsigned int);
2378 static void dwarf2out_set_name (tree, tree);
2380 /* The debug hooks structure. */
2382 const struct gcc_debug_hooks dwarf2_debug_hooks =
2384 dwarf2out_init,
2385 dwarf2out_finish,
2386 dwarf2out_assembly_start,
2387 dwarf2out_define,
2388 dwarf2out_undef,
2389 dwarf2out_start_source_file,
2390 dwarf2out_end_source_file,
2391 dwarf2out_begin_block,
2392 dwarf2out_end_block,
2393 dwarf2out_ignore_block,
2394 dwarf2out_source_line,
2395 dwarf2out_begin_prologue,
2396 #if VMS_DEBUGGING_INFO
2397 dwarf2out_vms_end_prologue,
2398 dwarf2out_vms_begin_epilogue,
2399 #else
2400 debug_nothing_int_charstar,
2401 debug_nothing_int_charstar,
2402 #endif
2403 dwarf2out_end_epilogue,
2404 dwarf2out_begin_function,
2405 dwarf2out_end_function, /* end_function */
2406 dwarf2out_function_decl, /* function_decl */
2407 dwarf2out_global_decl,
2408 dwarf2out_type_decl, /* type_decl */
2409 dwarf2out_imported_module_or_decl,
2410 debug_nothing_tree, /* deferred_inline_function */
2411 /* The DWARF 2 backend tries to reduce debugging bloat by not
2412 emitting the abstract description of inline functions until
2413 something tries to reference them. */
2414 dwarf2out_abstract_function, /* outlining_inline_function */
2415 debug_nothing_rtx, /* label */
2416 debug_nothing_int, /* handle_pch */
2417 dwarf2out_var_location,
2418 dwarf2out_switch_text_section,
2419 dwarf2out_set_name,
2420 1, /* start_end_main_source_file */
2421 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2424 /* NOTE: In the comments in this file, many references are made to
2425 "Debugging Information Entries". This term is abbreviated as `DIE'
2426 throughout the remainder of this file. */
2428 /* An internal representation of the DWARF output is built, and then
2429 walked to generate the DWARF debugging info. The walk of the internal
2430 representation is done after the entire program has been compiled.
2431 The types below are used to describe the internal representation. */
2433 /* Whether to put type DIEs into their own section .debug_types instead
2434 of making them part of the .debug_info section. Only supported for
2435 Dwarf V4 or higher and the user didn't disable them through
2436 -fno-debug-types-section. It is more efficient to put them in a
2437 separate comdat sections since the linker will then be able to
2438 remove duplicates. But not all tools support .debug_types sections
2439 yet. */
2441 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2443 /* Various DIE's use offsets relative to the beginning of the
2444 .debug_info section to refer to each other. */
2446 typedef long int dw_offset;
2448 /* Define typedefs here to avoid circular dependencies. */
2450 typedef struct dw_attr_struct *dw_attr_ref;
2451 typedef struct dw_line_info_struct *dw_line_info_ref;
2452 typedef struct pubname_struct *pubname_ref;
2453 typedef struct dw_ranges_struct *dw_ranges_ref;
2454 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2455 typedef struct comdat_type_struct *comdat_type_node_ref;
2457 /* The entries in the line_info table more-or-less mirror the opcodes
2458 that are used in the real dwarf line table. Arrays of these entries
2459 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2460 supported. */
2462 enum dw_line_info_opcode {
2463 /* Emit DW_LNE_set_address; the operand is the label index. */
2464 LI_set_address,
2466 /* Emit a row to the matrix with the given line. This may be done
2467 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2468 special opcodes. */
2469 LI_set_line,
2471 /* Emit a DW_LNS_set_file. */
2472 LI_set_file,
2474 /* Emit a DW_LNS_set_column. */
2475 LI_set_column,
2477 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2478 LI_negate_stmt,
2480 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2481 LI_set_prologue_end,
2482 LI_set_epilogue_begin,
2484 /* Emit a DW_LNE_set_discriminator. */
2485 LI_set_discriminator
2488 typedef struct GTY(()) dw_line_info_struct {
2489 enum dw_line_info_opcode opcode;
2490 unsigned int val;
2491 } dw_line_info_entry;
2494 typedef struct GTY(()) dw_line_info_table_struct {
2495 /* The label that marks the end of this section. */
2496 const char *end_label;
2498 /* The values for the last row of the matrix, as collected in the table.
2499 These are used to minimize the changes to the next row. */
2500 unsigned int file_num;
2501 unsigned int line_num;
2502 unsigned int column_num;
2503 int discrim_num;
2504 bool is_stmt;
2505 bool in_use;
2507 vec<dw_line_info_entry, va_gc> *entries;
2508 } dw_line_info_table;
2510 typedef dw_line_info_table *dw_line_info_table_p;
2513 /* Each DIE attribute has a field specifying the attribute kind,
2514 a link to the next attribute in the chain, and an attribute value.
2515 Attributes are typically linked below the DIE they modify. */
2517 typedef struct GTY(()) dw_attr_struct {
2518 enum dwarf_attribute dw_attr;
2519 dw_val_node dw_attr_val;
2521 dw_attr_node;
2524 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2525 The children of each node form a circular list linked by
2526 die_sib. die_child points to the node *before* the "first" child node. */
2528 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2529 union die_symbol_or_type_node
2531 const char * GTY ((tag ("0"))) die_symbol;
2532 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2534 GTY ((desc ("%0.comdat_type_p"))) die_id;
2535 vec<dw_attr_node, va_gc> *die_attr;
2536 dw_die_ref die_parent;
2537 dw_die_ref die_child;
2538 dw_die_ref die_sib;
2539 dw_die_ref die_definition; /* ref from a specification to its definition */
2540 dw_offset die_offset;
2541 unsigned long die_abbrev;
2542 int die_mark;
2543 unsigned int decl_id;
2544 enum dwarf_tag die_tag;
2545 /* Die is used and must not be pruned as unused. */
2546 BOOL_BITFIELD die_perennial_p : 1;
2547 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2548 /* Lots of spare bits. */
2550 die_node;
2552 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2553 #define FOR_EACH_CHILD(die, c, expr) do { \
2554 c = die->die_child; \
2555 if (c) do { \
2556 c = c->die_sib; \
2557 expr; \
2558 } while (c != die->die_child); \
2559 } while (0)
2561 /* The pubname structure */
2563 typedef struct GTY(()) pubname_struct {
2564 dw_die_ref die;
2565 const char *name;
2567 pubname_entry;
2570 struct GTY(()) dw_ranges_struct {
2571 /* If this is positive, it's a block number, otherwise it's a
2572 bitwise-negated index into dw_ranges_by_label. */
2573 int num;
2576 /* A structure to hold a macinfo entry. */
2578 typedef struct GTY(()) macinfo_struct {
2579 unsigned char code;
2580 unsigned HOST_WIDE_INT lineno;
2581 const char *info;
2583 macinfo_entry;
2586 struct GTY(()) dw_ranges_by_label_struct {
2587 const char *begin;
2588 const char *end;
2591 /* The comdat type node structure. */
2592 typedef struct GTY(()) comdat_type_struct
2594 dw_die_ref root_die;
2595 dw_die_ref type_die;
2596 dw_die_ref skeleton_die;
2597 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2598 struct comdat_type_struct *next;
2600 comdat_type_node;
2602 /* The limbo die list structure. */
2603 typedef struct GTY(()) limbo_die_struct {
2604 dw_die_ref die;
2605 tree created_for;
2606 struct limbo_die_struct *next;
2608 limbo_die_node;
2610 typedef struct skeleton_chain_struct
2612 dw_die_ref old_die;
2613 dw_die_ref new_die;
2614 struct skeleton_chain_struct *parent;
2616 skeleton_chain_node;
2618 /* Define a macro which returns nonzero for a TYPE_DECL which was
2619 implicitly generated for a type.
2621 Note that, unlike the C front-end (which generates a NULL named
2622 TYPE_DECL node for each complete tagged type, each array type,
2623 and each function type node created) the C++ front-end generates
2624 a _named_ TYPE_DECL node for each tagged type node created.
2625 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2626 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2627 front-end, but for each type, tagged or not. */
2629 #define TYPE_DECL_IS_STUB(decl) \
2630 (DECL_NAME (decl) == NULL_TREE \
2631 || (DECL_ARTIFICIAL (decl) \
2632 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2633 /* This is necessary for stub decls that \
2634 appear in nested inline functions. */ \
2635 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2636 && (decl_ultimate_origin (decl) \
2637 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2639 /* Information concerning the compilation unit's programming
2640 language, and compiler version. */
2642 /* Fixed size portion of the DWARF compilation unit header. */
2643 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2644 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2646 /* Fixed size portion of the DWARF comdat type unit header. */
2647 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2648 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2649 + DWARF_OFFSET_SIZE)
2651 /* Fixed size portion of public names info. */
2652 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2654 /* Fixed size portion of the address range info. */
2655 #define DWARF_ARANGES_HEADER_SIZE \
2656 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2657 DWARF2_ADDR_SIZE * 2) \
2658 - DWARF_INITIAL_LENGTH_SIZE)
2660 /* Size of padding portion in the address range info. It must be
2661 aligned to twice the pointer size. */
2662 #define DWARF_ARANGES_PAD_SIZE \
2663 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2664 DWARF2_ADDR_SIZE * 2) \
2665 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2667 /* Use assembler line directives if available. */
2668 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2669 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2670 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2671 #else
2672 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2673 #endif
2674 #endif
2676 /* Minimum line offset in a special line info. opcode.
2677 This value was chosen to give a reasonable range of values. */
2678 #define DWARF_LINE_BASE -10
2680 /* First special line opcode - leave room for the standard opcodes. */
2681 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2683 /* Range of line offsets in a special line info. opcode. */
2684 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2686 /* Flag that indicates the initial value of the is_stmt_start flag.
2687 In the present implementation, we do not mark any lines as
2688 the beginning of a source statement, because that information
2689 is not made available by the GCC front-end. */
2690 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2692 /* Maximum number of operations per instruction bundle. */
2693 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2694 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2695 #endif
2697 /* This location is used by calc_die_sizes() to keep track
2698 the offset of each DIE within the .debug_info section. */
2699 static unsigned long next_die_offset;
2701 /* Record the root of the DIE's built for the current compilation unit. */
2702 static GTY(()) dw_die_ref single_comp_unit_die;
2704 /* A list of type DIEs that have been separated into comdat sections. */
2705 static GTY(()) comdat_type_node *comdat_type_list;
2707 /* A list of DIEs with a NULL parent waiting to be relocated. */
2708 static GTY(()) limbo_die_node *limbo_die_list;
2710 /* A list of DIEs for which we may have to generate
2711 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2712 static GTY(()) limbo_die_node *deferred_asm_name;
2714 /* Filenames referenced by this compilation unit. */
2715 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2717 /* A hash table of references to DIE's that describe declarations.
2718 The key is a DECL_UID() which is a unique number identifying each decl. */
2719 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2721 /* A hash table of references to DIE's that describe COMMON blocks.
2722 The key is DECL_UID() ^ die_parent. */
2723 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2725 typedef struct GTY(()) die_arg_entry_struct {
2726 dw_die_ref die;
2727 tree arg;
2728 } die_arg_entry;
2731 /* Node of the variable location list. */
2732 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2733 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2734 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2735 in mode of the EXPR_LIST node and first EXPR_LIST operand
2736 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2737 location or NULL for padding. For larger bitsizes,
2738 mode is 0 and first operand is a CONCAT with bitsize
2739 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2740 NULL as second operand. */
2741 rtx GTY (()) loc;
2742 const char * GTY (()) label;
2743 struct var_loc_node * GTY (()) next;
2746 /* Variable location list. */
2747 struct GTY (()) var_loc_list_def {
2748 struct var_loc_node * GTY (()) first;
2750 /* Pointer to the last but one or last element of the
2751 chained list. If the list is empty, both first and
2752 last are NULL, if the list contains just one node
2753 or the last node certainly is not redundant, it points
2754 to the last node, otherwise points to the last but one.
2755 Do not mark it for GC because it is marked through the chain. */
2756 struct var_loc_node * GTY ((skip ("%h"))) last;
2758 /* Pointer to the last element before section switch,
2759 if NULL, either sections weren't switched or first
2760 is after section switch. */
2761 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2763 /* DECL_UID of the variable decl. */
2764 unsigned int decl_id;
2766 typedef struct var_loc_list_def var_loc_list;
2768 /* Call argument location list. */
2769 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2770 rtx GTY (()) call_arg_loc_note;
2771 const char * GTY (()) label;
2772 tree GTY (()) block;
2773 bool tail_call_p;
2774 rtx GTY (()) symbol_ref;
2775 struct call_arg_loc_node * GTY (()) next;
2779 /* Table of decl location linked lists. */
2780 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2782 /* Head and tail of call_arg_loc chain. */
2783 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2784 static struct call_arg_loc_node *call_arg_loc_last;
2786 /* Number of call sites in the current function. */
2787 static int call_site_count = -1;
2788 /* Number of tail call sites in the current function. */
2789 static int tail_call_site_count = -1;
2791 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2792 DIEs. */
2793 static vec<dw_die_ref> block_map;
2795 /* A cached location list. */
2796 struct GTY (()) cached_dw_loc_list_def {
2797 /* The DECL_UID of the decl that this entry describes. */
2798 unsigned int decl_id;
2800 /* The cached location list. */
2801 dw_loc_list_ref loc_list;
2803 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2805 /* Table of cached location lists. */
2806 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2808 /* A pointer to the base of a list of references to DIE's that
2809 are uniquely identified by their tag, presence/absence of
2810 children DIE's, and list of attribute/value pairs. */
2811 static GTY((length ("abbrev_die_table_allocated")))
2812 dw_die_ref *abbrev_die_table;
2814 /* Number of elements currently allocated for abbrev_die_table. */
2815 static GTY(()) unsigned abbrev_die_table_allocated;
2817 /* Number of elements in type_die_table currently in use. */
2818 static GTY(()) unsigned abbrev_die_table_in_use;
2820 /* Size (in elements) of increments by which we may expand the
2821 abbrev_die_table. */
2822 #define ABBREV_DIE_TABLE_INCREMENT 256
2824 /* A global counter for generating labels for line number data. */
2825 static unsigned int line_info_label_num;
2827 /* The current table to which we should emit line number information
2828 for the current function. This will be set up at the beginning of
2829 assembly for the function. */
2830 static dw_line_info_table *cur_line_info_table;
2832 /* The two default tables of line number info. */
2833 static GTY(()) dw_line_info_table *text_section_line_info;
2834 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2836 /* The set of all non-default tables of line number info. */
2837 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2839 /* A flag to tell pubnames/types export if there is an info section to
2840 refer to. */
2841 static bool info_section_emitted;
2843 /* A pointer to the base of a table that contains a list of publicly
2844 accessible names. */
2845 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2847 /* A pointer to the base of a table that contains a list of publicly
2848 accessible types. */
2849 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2851 /* A pointer to the base of a table that contains a list of macro
2852 defines/undefines (and file start/end markers). */
2853 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2855 /* True if .debug_macinfo or .debug_macros section is going to be
2856 emitted. */
2857 #define have_macinfo \
2858 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2859 && !macinfo_table->is_empty ())
2861 /* Array of dies for which we should generate .debug_ranges info. */
2862 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2864 /* Number of elements currently allocated for ranges_table. */
2865 static GTY(()) unsigned ranges_table_allocated;
2867 /* Number of elements in ranges_table currently in use. */
2868 static GTY(()) unsigned ranges_table_in_use;
2870 /* Array of pairs of labels referenced in ranges_table. */
2871 static GTY ((length ("ranges_by_label_allocated")))
2872 dw_ranges_by_label_ref ranges_by_label;
2874 /* Number of elements currently allocated for ranges_by_label. */
2875 static GTY(()) unsigned ranges_by_label_allocated;
2877 /* Number of elements in ranges_by_label currently in use. */
2878 static GTY(()) unsigned ranges_by_label_in_use;
2880 /* Size (in elements) of increments by which we may expand the
2881 ranges_table. */
2882 #define RANGES_TABLE_INCREMENT 64
2884 /* Whether we have location lists that need outputting */
2885 static GTY(()) bool have_location_lists;
2887 /* Unique label counter. */
2888 static GTY(()) unsigned int loclabel_num;
2890 /* Unique label counter for point-of-call tables. */
2891 static GTY(()) unsigned int poc_label_num;
2893 /* Record whether the function being analyzed contains inlined functions. */
2894 static int current_function_has_inlines;
2896 /* The last file entry emitted by maybe_emit_file(). */
2897 static GTY(()) struct dwarf_file_data * last_emitted_file;
2899 /* Number of internal labels generated by gen_internal_sym(). */
2900 static GTY(()) int label_num;
2902 /* Cached result of previous call to lookup_filename. */
2903 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2905 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2907 /* Instances of generic types for which we need to generate debug
2908 info that describe their generic parameters and arguments. That
2909 generation needs to happen once all types are properly laid out so
2910 we do it at the end of compilation. */
2911 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2913 /* Offset from the "steady-state frame pointer" to the frame base,
2914 within the current function. */
2915 static HOST_WIDE_INT frame_pointer_fb_offset;
2916 static bool frame_pointer_fb_offset_valid;
2918 static vec<dw_die_ref> base_types;
2920 /* Forward declarations for functions defined in this file. */
2922 static int is_pseudo_reg (const_rtx);
2923 static tree type_main_variant (tree);
2924 static int is_tagged_type (const_tree);
2925 static const char *dwarf_tag_name (unsigned);
2926 static const char *dwarf_attr_name (unsigned);
2927 static const char *dwarf_form_name (unsigned);
2928 static tree decl_ultimate_origin (const_tree);
2929 static tree decl_class_context (tree);
2930 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2931 static inline enum dw_val_class AT_class (dw_attr_ref);
2932 static inline unsigned int AT_index (dw_attr_ref);
2933 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2934 static inline unsigned AT_flag (dw_attr_ref);
2935 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2936 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2937 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2938 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2939 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2940 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2941 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2942 unsigned int, unsigned char *);
2943 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2944 static hashval_t debug_str_do_hash (const void *);
2945 static int debug_str_eq (const void *, const void *);
2946 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2947 static inline const char *AT_string (dw_attr_ref);
2948 static enum dwarf_form AT_string_form (dw_attr_ref);
2949 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2950 static void add_AT_specification (dw_die_ref, dw_die_ref);
2951 static inline dw_die_ref AT_ref (dw_attr_ref);
2952 static inline int AT_ref_external (dw_attr_ref);
2953 static inline void set_AT_ref_external (dw_attr_ref, int);
2954 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2955 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2956 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2957 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2958 dw_loc_list_ref);
2959 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2960 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2961 static void remove_addr_table_entry (addr_table_entry *);
2962 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2963 static inline rtx AT_addr (dw_attr_ref);
2964 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2965 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2966 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2967 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2968 unsigned HOST_WIDE_INT);
2969 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2970 unsigned long, bool);
2971 static inline const char *AT_lbl (dw_attr_ref);
2972 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2973 static const char *get_AT_low_pc (dw_die_ref);
2974 static const char *get_AT_hi_pc (dw_die_ref);
2975 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2976 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2977 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2978 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2979 static bool is_cxx (void);
2980 static bool is_fortran (void);
2981 static bool is_ada (void);
2982 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2983 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2984 static void add_child_die (dw_die_ref, dw_die_ref);
2985 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2986 static dw_die_ref lookup_type_die (tree);
2987 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2988 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2989 static void equate_type_number_to_die (tree, dw_die_ref);
2990 static hashval_t decl_die_table_hash (const void *);
2991 static int decl_die_table_eq (const void *, const void *);
2992 static dw_die_ref lookup_decl_die (tree);
2993 static hashval_t common_block_die_table_hash (const void *);
2994 static int common_block_die_table_eq (const void *, const void *);
2995 static hashval_t decl_loc_table_hash (const void *);
2996 static int decl_loc_table_eq (const void *, const void *);
2997 static var_loc_list *lookup_decl_loc (const_tree);
2998 static void equate_decl_number_to_die (tree, dw_die_ref);
2999 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3000 static void print_spaces (FILE *);
3001 static void print_die (dw_die_ref, FILE *);
3002 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3003 static dw_die_ref pop_compile_unit (dw_die_ref);
3004 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3005 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3006 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3007 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3008 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3009 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3010 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3011 struct md5_ctx *, int *);
3012 struct checksum_attributes;
3013 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3014 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3015 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3016 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3017 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3018 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3019 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3020 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3021 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3022 static void compute_section_prefix (dw_die_ref);
3023 static int is_type_die (dw_die_ref);
3024 static int is_comdat_die (dw_die_ref);
3025 static int is_symbol_die (dw_die_ref);
3026 static void assign_symbol_names (dw_die_ref);
3027 static void break_out_includes (dw_die_ref);
3028 static int is_declaration_die (dw_die_ref);
3029 static int should_move_die_to_comdat (dw_die_ref);
3030 static dw_die_ref clone_as_declaration (dw_die_ref);
3031 static dw_die_ref clone_die (dw_die_ref);
3032 static dw_die_ref clone_tree (dw_die_ref);
3033 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3034 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3035 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3036 static dw_die_ref generate_skeleton (dw_die_ref);
3037 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3038 dw_die_ref,
3039 dw_die_ref);
3040 static void break_out_comdat_types (dw_die_ref);
3041 static void copy_decls_for_unworthy_types (dw_die_ref);
3043 static void add_sibling_attributes (dw_die_ref);
3044 static void output_location_lists (dw_die_ref);
3045 static int constant_size (unsigned HOST_WIDE_INT);
3046 static unsigned long size_of_die (dw_die_ref);
3047 static void calc_die_sizes (dw_die_ref);
3048 static void calc_base_type_die_sizes (void);
3049 static void mark_dies (dw_die_ref);
3050 static void unmark_dies (dw_die_ref);
3051 static void unmark_all_dies (dw_die_ref);
3052 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3053 static unsigned long size_of_aranges (void);
3054 static enum dwarf_form value_format (dw_attr_ref);
3055 static void output_value_format (dw_attr_ref);
3056 static void output_abbrev_section (void);
3057 static void output_die_abbrevs (unsigned long, dw_die_ref);
3058 static void output_die_symbol (dw_die_ref);
3059 static void output_die (dw_die_ref);
3060 static void output_compilation_unit_header (void);
3061 static void output_comp_unit (dw_die_ref, int);
3062 static void output_comdat_type_unit (comdat_type_node *);
3063 static const char *dwarf2_name (tree, int);
3064 static void add_pubname (tree, dw_die_ref);
3065 static void add_enumerator_pubname (const char *, dw_die_ref);
3066 static void add_pubname_string (const char *, dw_die_ref);
3067 static void add_pubtype (tree, dw_die_ref);
3068 static void output_pubnames (vec<pubname_entry, va_gc> *);
3069 static void output_aranges (unsigned long);
3070 static unsigned int add_ranges_num (int);
3071 static unsigned int add_ranges (const_tree);
3072 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3073 bool *, bool);
3074 static void output_ranges (void);
3075 static dw_line_info_table *new_line_info_table (void);
3076 static void output_line_info (bool);
3077 static void output_file_names (void);
3078 static dw_die_ref base_type_die (tree);
3079 static int is_base_type (tree);
3080 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3081 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3082 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3083 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3084 static int type_is_enum (const_tree);
3085 static unsigned int dbx_reg_number (const_rtx);
3086 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3087 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3088 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3089 enum var_init_status);
3090 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3091 enum var_init_status);
3092 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3093 enum var_init_status);
3094 static int is_based_loc (const_rtx);
3095 static int resolve_one_addr (rtx *, void *);
3096 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3097 enum var_init_status);
3098 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3099 enum var_init_status);
3100 static dw_loc_list_ref loc_list_from_tree (tree, int);
3101 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3102 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3103 static tree field_type (const_tree);
3104 static unsigned int simple_type_align_in_bits (const_tree);
3105 static unsigned int simple_decl_align_in_bits (const_tree);
3106 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3107 static HOST_WIDE_INT field_byte_offset (const_tree);
3108 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3109 dw_loc_list_ref);
3110 static void add_data_member_location_attribute (dw_die_ref, tree);
3111 static bool add_const_value_attribute (dw_die_ref, rtx);
3112 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3113 static void insert_double (double_int, unsigned char *);
3114 static void insert_float (const_rtx, unsigned char *);
3115 static rtx rtl_for_decl_location (tree);
3116 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3117 enum dwarf_attribute);
3118 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3119 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3120 static void add_name_attribute (dw_die_ref, const char *);
3121 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3122 static void add_comp_dir_attribute (dw_die_ref);
3123 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3124 static void add_subscript_info (dw_die_ref, tree, bool);
3125 static void add_byte_size_attribute (dw_die_ref, tree);
3126 static void add_bit_offset_attribute (dw_die_ref, tree);
3127 static void add_bit_size_attribute (dw_die_ref, tree);
3128 static void add_prototyped_attribute (dw_die_ref, tree);
3129 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3130 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3131 static void add_src_coords_attributes (dw_die_ref, tree);
3132 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3133 static void push_decl_scope (tree);
3134 static void pop_decl_scope (void);
3135 static dw_die_ref scope_die_for (tree, dw_die_ref);
3136 static inline int local_scope_p (dw_die_ref);
3137 static inline int class_scope_p (dw_die_ref);
3138 static inline int class_or_namespace_scope_p (dw_die_ref);
3139 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3140 static void add_calling_convention_attribute (dw_die_ref, tree);
3141 static const char *type_tag (const_tree);
3142 static tree member_declared_type (const_tree);
3143 #if 0
3144 static const char *decl_start_label (tree);
3145 #endif
3146 static void gen_array_type_die (tree, dw_die_ref);
3147 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3148 #if 0
3149 static void gen_entry_point_die (tree, dw_die_ref);
3150 #endif
3151 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3152 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3153 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3154 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3155 static void gen_formal_types_die (tree, dw_die_ref);
3156 static void gen_subprogram_die (tree, dw_die_ref);
3157 static void gen_variable_die (tree, tree, dw_die_ref);
3158 static void gen_const_die (tree, dw_die_ref);
3159 static void gen_label_die (tree, dw_die_ref);
3160 static void gen_lexical_block_die (tree, dw_die_ref, int);
3161 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3162 static void gen_field_die (tree, dw_die_ref);
3163 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3164 static dw_die_ref gen_compile_unit_die (const char *);
3165 static void gen_inheritance_die (tree, tree, dw_die_ref);
3166 static void gen_member_die (tree, dw_die_ref);
3167 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3168 enum debug_info_usage);
3169 static void gen_subroutine_type_die (tree, dw_die_ref);
3170 static void gen_typedef_die (tree, dw_die_ref);
3171 static void gen_type_die (tree, dw_die_ref);
3172 static void gen_block_die (tree, dw_die_ref, int);
3173 static void decls_for_scope (tree, dw_die_ref, int);
3174 static inline int is_redundant_typedef (const_tree);
3175 static bool is_naming_typedef_decl (const_tree);
3176 static inline dw_die_ref get_context_die (tree);
3177 static void gen_namespace_die (tree, dw_die_ref);
3178 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3179 static dw_die_ref force_decl_die (tree);
3180 static dw_die_ref force_type_die (tree);
3181 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3182 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3183 static struct dwarf_file_data * lookup_filename (const char *);
3184 static void retry_incomplete_types (void);
3185 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3186 static void gen_generic_params_dies (tree);
3187 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3188 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3189 static void splice_child_die (dw_die_ref, dw_die_ref);
3190 static int file_info_cmp (const void *, const void *);
3191 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3192 const char *, const char *);
3193 static void output_loc_list (dw_loc_list_ref);
3194 static char *gen_internal_sym (const char *);
3195 static bool want_pubnames (void);
3197 static void prune_unmark_dies (dw_die_ref);
3198 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3199 static void prune_unused_types_mark (dw_die_ref, int);
3200 static void prune_unused_types_walk (dw_die_ref);
3201 static void prune_unused_types_walk_attribs (dw_die_ref);
3202 static void prune_unused_types_prune (dw_die_ref);
3203 static void prune_unused_types (void);
3204 static int maybe_emit_file (struct dwarf_file_data *fd);
3205 static inline const char *AT_vms_delta1 (dw_attr_ref);
3206 static inline const char *AT_vms_delta2 (dw_attr_ref);
3207 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3208 const char *, const char *);
3209 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3210 static void gen_remaining_tmpl_value_param_die_attribute (void);
3211 static bool generic_type_p (tree);
3212 static void schedule_generic_params_dies_gen (tree t);
3213 static void gen_scheduled_generic_parms_dies (void);
3215 static const char *comp_dir_string (void);
3217 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3219 /* enum for tracking thread-local variables whose address is really an offset
3220 relative to the TLS pointer, which will need link-time relocation, but will
3221 not need relocation by the DWARF consumer. */
3223 enum dtprel_bool
3225 dtprel_false = 0,
3226 dtprel_true = 1
3229 /* Return the operator to use for an address of a variable. For dtprel_true, we
3230 use DW_OP_const*. For regular variables, which need both link-time
3231 relocation and consumer-level relocation (e.g., to account for shared objects
3232 loaded at a random address), we use DW_OP_addr*. */
3234 static inline enum dwarf_location_atom
3235 dw_addr_op (enum dtprel_bool dtprel)
3237 if (dtprel == dtprel_true)
3238 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3239 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3240 else
3241 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3244 /* Return a pointer to a newly allocated address location description. If
3245 dwarf_split_debug_info is true, then record the address with the appropriate
3246 relocation. */
3247 static inline dw_loc_descr_ref
3248 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3250 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3252 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3253 ref->dw_loc_oprnd1.v.val_addr = addr;
3254 ref->dtprel = dtprel;
3255 if (dwarf_split_debug_info)
3256 ref->dw_loc_oprnd1.val_entry
3257 = add_addr_table_entry (addr,
3258 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3259 else
3260 ref->dw_loc_oprnd1.val_entry = NULL;
3262 return ref;
3265 /* Section names used to hold DWARF debugging information. */
3267 #ifndef DEBUG_INFO_SECTION
3268 #define DEBUG_INFO_SECTION ".debug_info"
3269 #endif
3270 #ifndef DEBUG_DWO_INFO_SECTION
3271 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3272 #endif
3273 #ifndef DEBUG_ABBREV_SECTION
3274 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3275 #endif
3276 #ifndef DEBUG_DWO_ABBREV_SECTION
3277 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3278 #endif
3279 #ifndef DEBUG_ARANGES_SECTION
3280 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3281 #endif
3282 #ifndef DEBUG_ADDR_SECTION
3283 #define DEBUG_ADDR_SECTION ".debug_addr"
3284 #endif
3285 #ifndef DEBUG_NORM_MACINFO_SECTION
3286 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3287 #endif
3288 #ifndef DEBUG_DWO_MACINFO_SECTION
3289 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3290 #endif
3291 #ifndef DEBUG_MACINFO_SECTION
3292 #define DEBUG_MACINFO_SECTION \
3293 (!dwarf_split_debug_info \
3294 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3295 #endif
3296 #ifndef DEBUG_NORM_MACRO_SECTION
3297 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3298 #endif
3299 #ifndef DEBUG_DWO_MACRO_SECTION
3300 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3301 #endif
3302 #ifndef DEBUG_MACRO_SECTION
3303 #define DEBUG_MACRO_SECTION \
3304 (!dwarf_split_debug_info \
3305 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3306 #endif
3307 #ifndef DEBUG_LINE_SECTION
3308 #define DEBUG_LINE_SECTION ".debug_line"
3309 #endif
3310 #ifndef DEBUG_DWO_LINE_SECTION
3311 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3312 #endif
3313 #ifndef DEBUG_LOC_SECTION
3314 #define DEBUG_LOC_SECTION ".debug_loc"
3315 #endif
3316 #ifndef DEBUG_DWO_LOC_SECTION
3317 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3318 #endif
3319 #ifndef DEBUG_PUBNAMES_SECTION
3320 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3321 #endif
3322 #ifndef DEBUG_PUBTYPES_SECTION
3323 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3324 #endif
3325 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3326 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3327 #ifndef DEBUG_STR_OFFSETS_SECTION
3328 #define DEBUG_STR_OFFSETS_SECTION \
3329 (!dwarf_split_debug_info \
3330 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3331 #endif
3332 #ifndef DEBUG_STR_DWO_SECTION
3333 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3334 #endif
3335 #ifndef DEBUG_STR_SECTION
3336 #define DEBUG_STR_SECTION ".debug_str"
3337 #endif
3338 #ifndef DEBUG_RANGES_SECTION
3339 #define DEBUG_RANGES_SECTION ".debug_ranges"
3340 #endif
3342 /* Standard ELF section names for compiled code and data. */
3343 #ifndef TEXT_SECTION_NAME
3344 #define TEXT_SECTION_NAME ".text"
3345 #endif
3347 /* Section flags for .debug_macinfo/.debug_macro section. */
3348 #define DEBUG_MACRO_SECTION_FLAGS \
3349 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3351 /* Section flags for .debug_str section. */
3352 #define DEBUG_STR_SECTION_FLAGS \
3353 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3354 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3355 : SECTION_DEBUG)
3357 /* Section flags for .debug_str.dwo section. */
3358 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3360 /* Labels we insert at beginning sections we can reference instead of
3361 the section names themselves. */
3363 #ifndef TEXT_SECTION_LABEL
3364 #define TEXT_SECTION_LABEL "Ltext"
3365 #endif
3366 #ifndef COLD_TEXT_SECTION_LABEL
3367 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3368 #endif
3369 #ifndef DEBUG_LINE_SECTION_LABEL
3370 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3371 #endif
3372 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3373 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3374 #endif
3375 #ifndef DEBUG_INFO_SECTION_LABEL
3376 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3377 #endif
3378 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3379 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3380 #endif
3381 #ifndef DEBUG_ABBREV_SECTION_LABEL
3382 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3383 #endif
3384 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3385 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3386 #endif
3387 #ifndef DEBUG_ADDR_SECTION_LABEL
3388 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3389 #endif
3390 #ifndef DEBUG_LOC_SECTION_LABEL
3391 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3392 #endif
3393 #ifndef DEBUG_RANGES_SECTION_LABEL
3394 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3395 #endif
3396 #ifndef DEBUG_MACINFO_SECTION_LABEL
3397 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3398 #endif
3399 #ifndef DEBUG_MACRO_SECTION_LABEL
3400 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3401 #endif
3402 #define SKELETON_COMP_DIE_ABBREV 1
3403 #define SKELETON_TYPE_DIE_ABBREV 2
3405 /* Definitions of defaults for formats and names of various special
3406 (artificial) labels which may be generated within this file (when the -g
3407 options is used and DWARF2_DEBUGGING_INFO is in effect.
3408 If necessary, these may be overridden from within the tm.h file, but
3409 typically, overriding these defaults is unnecessary. */
3411 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3412 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3413 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3414 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3415 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3416 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3417 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3418 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3419 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3420 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3421 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3422 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3423 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3424 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3426 #ifndef TEXT_END_LABEL
3427 #define TEXT_END_LABEL "Letext"
3428 #endif
3429 #ifndef COLD_END_LABEL
3430 #define COLD_END_LABEL "Letext_cold"
3431 #endif
3432 #ifndef BLOCK_BEGIN_LABEL
3433 #define BLOCK_BEGIN_LABEL "LBB"
3434 #endif
3435 #ifndef BLOCK_END_LABEL
3436 #define BLOCK_END_LABEL "LBE"
3437 #endif
3438 #ifndef LINE_CODE_LABEL
3439 #define LINE_CODE_LABEL "LM"
3440 #endif
3443 /* Return the root of the DIE's built for the current compilation unit. */
3444 static dw_die_ref
3445 comp_unit_die (void)
3447 if (!single_comp_unit_die)
3448 single_comp_unit_die = gen_compile_unit_die (NULL);
3449 return single_comp_unit_die;
3452 /* We allow a language front-end to designate a function that is to be
3453 called to "demangle" any name before it is put into a DIE. */
3455 static const char *(*demangle_name_func) (const char *);
3457 void
3458 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3460 demangle_name_func = func;
3463 /* Test if rtl node points to a pseudo register. */
3465 static inline int
3466 is_pseudo_reg (const_rtx rtl)
3468 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3469 || (GET_CODE (rtl) == SUBREG
3470 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3473 /* Return a reference to a type, with its const and volatile qualifiers
3474 removed. */
3476 static inline tree
3477 type_main_variant (tree type)
3479 type = TYPE_MAIN_VARIANT (type);
3481 /* ??? There really should be only one main variant among any group of
3482 variants of a given type (and all of the MAIN_VARIANT values for all
3483 members of the group should point to that one type) but sometimes the C
3484 front-end messes this up for array types, so we work around that bug
3485 here. */
3486 if (TREE_CODE (type) == ARRAY_TYPE)
3487 while (type != TYPE_MAIN_VARIANT (type))
3488 type = TYPE_MAIN_VARIANT (type);
3490 return type;
3493 /* Return nonzero if the given type node represents a tagged type. */
3495 static inline int
3496 is_tagged_type (const_tree type)
3498 enum tree_code code = TREE_CODE (type);
3500 return (code == RECORD_TYPE || code == UNION_TYPE
3501 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3504 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3506 static void
3507 get_ref_die_offset_label (char *label, dw_die_ref ref)
3509 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3512 /* Return die_offset of a DIE reference to a base type. */
3514 static unsigned long int
3515 get_base_type_offset (dw_die_ref ref)
3517 if (ref->die_offset)
3518 return ref->die_offset;
3519 if (comp_unit_die ()->die_abbrev)
3521 calc_base_type_die_sizes ();
3522 gcc_assert (ref->die_offset);
3524 return ref->die_offset;
3527 /* Return die_offset of a DIE reference other than base type. */
3529 static unsigned long int
3530 get_ref_die_offset (dw_die_ref ref)
3532 gcc_assert (ref->die_offset);
3533 return ref->die_offset;
3536 /* Convert a DIE tag into its string name. */
3538 static const char *
3539 dwarf_tag_name (unsigned int tag)
3541 const char *name = get_DW_TAG_name (tag);
3543 if (name != NULL)
3544 return name;
3546 return "DW_TAG_<unknown>";
3549 /* Convert a DWARF attribute code into its string name. */
3551 static const char *
3552 dwarf_attr_name (unsigned int attr)
3554 const char *name;
3556 switch (attr)
3558 #if VMS_DEBUGGING_INFO
3559 case DW_AT_HP_prologue:
3560 return "DW_AT_HP_prologue";
3561 #else
3562 case DW_AT_MIPS_loop_unroll_factor:
3563 return "DW_AT_MIPS_loop_unroll_factor";
3564 #endif
3566 #if VMS_DEBUGGING_INFO
3567 case DW_AT_HP_epilogue:
3568 return "DW_AT_HP_epilogue";
3569 #else
3570 case DW_AT_MIPS_stride:
3571 return "DW_AT_MIPS_stride";
3572 #endif
3575 name = get_DW_AT_name (attr);
3577 if (name != NULL)
3578 return name;
3580 return "DW_AT_<unknown>";
3583 /* Convert a DWARF value form code into its string name. */
3585 static const char *
3586 dwarf_form_name (unsigned int form)
3588 const char *name = get_DW_FORM_name (form);
3590 if (name != NULL)
3591 return name;
3593 return "DW_FORM_<unknown>";
3596 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3597 instance of an inlined instance of a decl which is local to an inline
3598 function, so we have to trace all of the way back through the origin chain
3599 to find out what sort of node actually served as the original seed for the
3600 given block. */
3602 static tree
3603 decl_ultimate_origin (const_tree decl)
3605 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3606 return NULL_TREE;
3608 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3609 nodes in the function to point to themselves; ignore that if
3610 we're trying to output the abstract instance of this function. */
3611 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3612 return NULL_TREE;
3614 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3615 most distant ancestor, this should never happen. */
3616 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3618 return DECL_ABSTRACT_ORIGIN (decl);
3621 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3622 of a virtual function may refer to a base class, so we check the 'this'
3623 parameter. */
3625 static tree
3626 decl_class_context (tree decl)
3628 tree context = NULL_TREE;
3630 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3631 context = DECL_CONTEXT (decl);
3632 else
3633 context = TYPE_MAIN_VARIANT
3634 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3636 if (context && !TYPE_P (context))
3637 context = NULL_TREE;
3639 return context;
3642 /* Add an attribute/value pair to a DIE. */
3644 static inline void
3645 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3647 /* Maybe this should be an assert? */
3648 if (die == NULL)
3649 return;
3651 vec_safe_reserve (die->die_attr, 1);
3652 vec_safe_push (die->die_attr, *attr);
3655 static inline enum dw_val_class
3656 AT_class (dw_attr_ref a)
3658 return a->dw_attr_val.val_class;
3661 /* Return the index for any attribute that will be referenced with a
3662 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3663 are stored in dw_attr_val.v.val_str for reference counting
3664 pruning. */
3666 static inline unsigned int
3667 AT_index (dw_attr_ref a)
3669 if (AT_class (a) == dw_val_class_str)
3670 return a->dw_attr_val.v.val_str->index;
3671 else if (a->dw_attr_val.val_entry != NULL)
3672 return a->dw_attr_val.val_entry->index;
3673 return NOT_INDEXED;
3676 /* Add a flag value attribute to a DIE. */
3678 static inline void
3679 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3681 dw_attr_node attr;
3683 attr.dw_attr = attr_kind;
3684 attr.dw_attr_val.val_class = dw_val_class_flag;
3685 attr.dw_attr_val.val_entry = NULL;
3686 attr.dw_attr_val.v.val_flag = flag;
3687 add_dwarf_attr (die, &attr);
3690 static inline unsigned
3691 AT_flag (dw_attr_ref a)
3693 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3694 return a->dw_attr_val.v.val_flag;
3697 /* Add a signed integer attribute value to a DIE. */
3699 static inline void
3700 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3702 dw_attr_node attr;
3704 attr.dw_attr = attr_kind;
3705 attr.dw_attr_val.val_class = dw_val_class_const;
3706 attr.dw_attr_val.val_entry = NULL;
3707 attr.dw_attr_val.v.val_int = int_val;
3708 add_dwarf_attr (die, &attr);
3711 static inline HOST_WIDE_INT
3712 AT_int (dw_attr_ref a)
3714 gcc_assert (a && AT_class (a) == dw_val_class_const);
3715 return a->dw_attr_val.v.val_int;
3718 /* Add an unsigned integer attribute value to a DIE. */
3720 static inline void
3721 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3722 unsigned HOST_WIDE_INT unsigned_val)
3724 dw_attr_node attr;
3726 attr.dw_attr = attr_kind;
3727 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3728 attr.dw_attr_val.val_entry = NULL;
3729 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3730 add_dwarf_attr (die, &attr);
3733 static inline unsigned HOST_WIDE_INT
3734 AT_unsigned (dw_attr_ref a)
3736 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3737 return a->dw_attr_val.v.val_unsigned;
3740 /* Add an unsigned double integer attribute value to a DIE. */
3742 static inline void
3743 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3744 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3746 dw_attr_node attr;
3748 attr.dw_attr = attr_kind;
3749 attr.dw_attr_val.val_class = dw_val_class_const_double;
3750 attr.dw_attr_val.val_entry = NULL;
3751 attr.dw_attr_val.v.val_double.high = high;
3752 attr.dw_attr_val.v.val_double.low = low;
3753 add_dwarf_attr (die, &attr);
3756 /* Add a floating point attribute value to a DIE and return it. */
3758 static inline void
3759 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3760 unsigned int length, unsigned int elt_size, unsigned char *array)
3762 dw_attr_node attr;
3764 attr.dw_attr = attr_kind;
3765 attr.dw_attr_val.val_class = dw_val_class_vec;
3766 attr.dw_attr_val.val_entry = NULL;
3767 attr.dw_attr_val.v.val_vec.length = length;
3768 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3769 attr.dw_attr_val.v.val_vec.array = array;
3770 add_dwarf_attr (die, &attr);
3773 /* Add an 8-byte data attribute value to a DIE. */
3775 static inline void
3776 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3777 unsigned char data8[8])
3779 dw_attr_node attr;
3781 attr.dw_attr = attr_kind;
3782 attr.dw_attr_val.val_class = dw_val_class_data8;
3783 attr.dw_attr_val.val_entry = NULL;
3784 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3785 add_dwarf_attr (die, &attr);
3788 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3789 dwarf_split_debug_info, address attributes in dies destined for the
3790 final executable have force_direct set to avoid using indexed
3791 references. */
3793 static inline void
3794 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3795 bool force_direct)
3797 dw_attr_node attr;
3798 char * lbl_id;
3800 lbl_id = xstrdup (lbl_low);
3801 attr.dw_attr = DW_AT_low_pc;
3802 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3803 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3804 if (dwarf_split_debug_info && !force_direct)
3805 attr.dw_attr_val.val_entry
3806 = add_addr_table_entry (lbl_id, ate_kind_label);
3807 else
3808 attr.dw_attr_val.val_entry = NULL;
3809 add_dwarf_attr (die, &attr);
3811 attr.dw_attr = DW_AT_high_pc;
3812 if (dwarf_version < 4)
3813 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3814 else
3815 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3816 lbl_id = xstrdup (lbl_high);
3817 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3818 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3819 && dwarf_split_debug_info && !force_direct)
3820 attr.dw_attr_val.val_entry
3821 = add_addr_table_entry (lbl_id, ate_kind_label);
3822 else
3823 attr.dw_attr_val.val_entry = NULL;
3824 add_dwarf_attr (die, &attr);
3827 /* Hash and equality functions for debug_str_hash. */
3829 static hashval_t
3830 debug_str_do_hash (const void *x)
3832 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3835 static int
3836 debug_str_eq (const void *x1, const void *x2)
3838 return strcmp ((((const struct indirect_string_node *)x1)->str),
3839 (const char *)x2) == 0;
3842 /* Add STR to the given string hash table. */
3844 static struct indirect_string_node *
3845 find_AT_string_in_table (const char *str, htab_t table)
3847 struct indirect_string_node *node;
3848 void **slot;
3850 slot = htab_find_slot_with_hash (table, str,
3851 htab_hash_string (str), INSERT);
3852 if (*slot == NULL)
3854 node = ggc_alloc_cleared_indirect_string_node ();
3855 node->str = ggc_strdup (str);
3856 *slot = node;
3858 else
3859 node = (struct indirect_string_node *) *slot;
3861 node->refcount++;
3862 return node;
3865 /* Add STR to the indirect string hash table. */
3867 static struct indirect_string_node *
3868 find_AT_string (const char *str)
3870 if (! debug_str_hash)
3871 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3872 debug_str_eq, NULL);
3874 return find_AT_string_in_table (str, debug_str_hash);
3877 /* Add a string attribute value to a DIE. */
3879 static inline void
3880 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3882 dw_attr_node attr;
3883 struct indirect_string_node *node;
3885 node = find_AT_string (str);
3887 attr.dw_attr = attr_kind;
3888 attr.dw_attr_val.val_class = dw_val_class_str;
3889 attr.dw_attr_val.val_entry = NULL;
3890 attr.dw_attr_val.v.val_str = node;
3891 add_dwarf_attr (die, &attr);
3894 static inline const char *
3895 AT_string (dw_attr_ref a)
3897 gcc_assert (a && AT_class (a) == dw_val_class_str);
3898 return a->dw_attr_val.v.val_str->str;
3901 /* Call this function directly to bypass AT_string_form's logic to put
3902 the string inline in the die. */
3904 static void
3905 set_indirect_string (struct indirect_string_node *node)
3907 char label[32];
3908 /* Already indirect is a no op. */
3909 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3911 gcc_assert (node->label);
3912 return;
3914 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3915 ++dw2_string_counter;
3916 node->label = xstrdup (label);
3918 if (!dwarf_split_debug_info)
3920 node->form = DW_FORM_strp;
3921 node->index = NOT_INDEXED;
3923 else
3925 node->form = DW_FORM_GNU_str_index;
3926 node->index = NO_INDEX_ASSIGNED;
3930 /* Find out whether a string should be output inline in DIE
3931 or out-of-line in .debug_str section. */
3933 static enum dwarf_form
3934 find_string_form (struct indirect_string_node *node)
3936 unsigned int len;
3938 if (node->form)
3939 return node->form;
3941 len = strlen (node->str) + 1;
3943 /* If the string is shorter or equal to the size of the reference, it is
3944 always better to put it inline. */
3945 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3946 return node->form = DW_FORM_string;
3948 /* If we cannot expect the linker to merge strings in .debug_str
3949 section, only put it into .debug_str if it is worth even in this
3950 single module. */
3951 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3952 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3953 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3954 return node->form = DW_FORM_string;
3956 set_indirect_string (node);
3958 return node->form;
3961 /* Find out whether the string referenced from the attribute should be
3962 output inline in DIE or out-of-line in .debug_str section. */
3964 static enum dwarf_form
3965 AT_string_form (dw_attr_ref a)
3967 gcc_assert (a && AT_class (a) == dw_val_class_str);
3968 return find_string_form (a->dw_attr_val.v.val_str);
3971 /* Add a DIE reference attribute value to a DIE. */
3973 static inline void
3974 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3976 dw_attr_node attr;
3978 #ifdef ENABLE_CHECKING
3979 gcc_assert (targ_die != NULL);
3980 #else
3981 /* With LTO we can end up trying to reference something we didn't create
3982 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3983 if (targ_die == NULL)
3984 return;
3985 #endif
3987 attr.dw_attr = attr_kind;
3988 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3989 attr.dw_attr_val.val_entry = NULL;
3990 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3991 attr.dw_attr_val.v.val_die_ref.external = 0;
3992 add_dwarf_attr (die, &attr);
3995 /* Change DIE reference REF to point to NEW_DIE instead. */
3997 static inline void
3998 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4000 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4001 ref->dw_attr_val.v.val_die_ref.die = new_die;
4002 ref->dw_attr_val.v.val_die_ref.external = 0;
4005 /* Add an AT_specification attribute to a DIE, and also make the back
4006 pointer from the specification to the definition. */
4008 static inline void
4009 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4011 add_AT_die_ref (die, DW_AT_specification, targ_die);
4012 gcc_assert (!targ_die->die_definition);
4013 targ_die->die_definition = die;
4016 static inline dw_die_ref
4017 AT_ref (dw_attr_ref a)
4019 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4020 return a->dw_attr_val.v.val_die_ref.die;
4023 static inline int
4024 AT_ref_external (dw_attr_ref a)
4026 if (a && AT_class (a) == dw_val_class_die_ref)
4027 return a->dw_attr_val.v.val_die_ref.external;
4029 return 0;
4032 static inline void
4033 set_AT_ref_external (dw_attr_ref a, int i)
4035 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4036 a->dw_attr_val.v.val_die_ref.external = i;
4039 /* Add an FDE reference attribute value to a DIE. */
4041 static inline void
4042 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4044 dw_attr_node attr;
4046 attr.dw_attr = attr_kind;
4047 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4048 attr.dw_attr_val.val_entry = NULL;
4049 attr.dw_attr_val.v.val_fde_index = targ_fde;
4050 add_dwarf_attr (die, &attr);
4053 /* Add a location description attribute value to a DIE. */
4055 static inline void
4056 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4058 dw_attr_node attr;
4060 attr.dw_attr = attr_kind;
4061 attr.dw_attr_val.val_class = dw_val_class_loc;
4062 attr.dw_attr_val.val_entry = NULL;
4063 attr.dw_attr_val.v.val_loc = loc;
4064 add_dwarf_attr (die, &attr);
4067 static inline dw_loc_descr_ref
4068 AT_loc (dw_attr_ref a)
4070 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4071 return a->dw_attr_val.v.val_loc;
4074 static inline void
4075 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4077 dw_attr_node attr;
4079 attr.dw_attr = attr_kind;
4080 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4081 attr.dw_attr_val.val_entry = NULL;
4082 attr.dw_attr_val.v.val_loc_list = loc_list;
4083 add_dwarf_attr (die, &attr);
4084 have_location_lists = true;
4087 static inline dw_loc_list_ref
4088 AT_loc_list (dw_attr_ref a)
4090 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4091 return a->dw_attr_val.v.val_loc_list;
4094 static inline dw_loc_list_ref *
4095 AT_loc_list_ptr (dw_attr_ref a)
4097 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4098 return &a->dw_attr_val.v.val_loc_list;
4101 /* Table of entries into the .debug_addr section. */
4103 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4105 /* Hash an address_table_entry. */
4107 static hashval_t
4108 addr_table_entry_do_hash (const void *x)
4110 const addr_table_entry *a = (const addr_table_entry *) x;
4111 switch (a->kind)
4113 case ate_kind_rtx:
4114 return iterative_hash_rtx (a->addr.rtl, 0);
4115 case ate_kind_rtx_dtprel:
4116 return iterative_hash_rtx (a->addr.rtl, 1);
4117 case ate_kind_label:
4118 return htab_hash_string (a->addr.label);
4119 default:
4120 gcc_unreachable ();
4124 /* Determine equality for two address_table_entries. */
4126 static int
4127 addr_table_entry_eq (const void *x1, const void *x2)
4129 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4130 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4132 if (a1->kind != a2->kind)
4133 return 0;
4134 switch (a1->kind)
4136 case ate_kind_rtx:
4137 case ate_kind_rtx_dtprel:
4138 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4139 case ate_kind_label:
4140 return strcmp (a1->addr.label, a2->addr.label) == 0;
4141 default:
4142 gcc_unreachable ();
4146 /* Initialize an addr_table_entry. */
4148 void
4149 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4151 e->kind = kind;
4152 switch (kind)
4154 case ate_kind_rtx:
4155 case ate_kind_rtx_dtprel:
4156 e->addr.rtl = (rtx) addr;
4157 break;
4158 case ate_kind_label:
4159 e->addr.label = (char *) addr;
4160 break;
4162 e->refcount = 0;
4163 e->index = NO_INDEX_ASSIGNED;
4166 /* Add attr to the address table entry to the table. Defer setting an
4167 index until output time. */
4169 static addr_table_entry *
4170 add_addr_table_entry (void *addr, enum ate_kind kind)
4172 addr_table_entry *node;
4173 addr_table_entry finder;
4174 void **slot;
4176 gcc_assert (dwarf_split_debug_info);
4177 if (! addr_index_table)
4178 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4179 addr_table_entry_eq, NULL);
4180 init_addr_table_entry (&finder, kind, addr);
4181 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4183 if (*slot == HTAB_EMPTY_ENTRY)
4185 node = ggc_alloc_cleared_addr_table_entry ();
4186 init_addr_table_entry (node, kind, addr);
4187 *slot = node;
4189 else
4190 node = (addr_table_entry *) *slot;
4192 node->refcount++;
4193 return node;
4196 /* Remove an entry from the addr table by decrementing its refcount.
4197 Strictly, decrementing the refcount would be enough, but the
4198 assertion that the entry is actually in the table has found
4199 bugs. */
4201 static void
4202 remove_addr_table_entry (addr_table_entry *entry)
4204 addr_table_entry *node;
4206 gcc_assert (dwarf_split_debug_info && addr_index_table);
4207 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4208 /* After an index is assigned, the table is frozen. */
4209 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4210 node->refcount--;
4213 /* Given a location list, remove all addresses it refers to from the
4214 address_table. */
4216 static void
4217 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4219 for (; descr; descr = descr->dw_loc_next)
4220 if (descr->dw_loc_oprnd1.val_entry != NULL)
4222 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4223 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4227 /* A helper function for dwarf2out_finish called through
4228 htab_traverse. Assign an addr_table_entry its index. All entries
4229 must be collected into the table when this function is called,
4230 because the indexing code relies on htab_traverse to traverse nodes
4231 in the same order for each run. */
4233 static int
4234 index_addr_table_entry (void **h, void *v)
4236 addr_table_entry *node = (addr_table_entry *) *h;
4237 unsigned int *index = (unsigned int *) v;
4239 /* Don't index unreferenced nodes. */
4240 if (node->refcount == 0)
4241 return 1;
4243 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4244 node->index = *index;
4245 *index += 1;
4247 return 1;
4250 /* Add an address constant attribute value to a DIE. When using
4251 dwarf_split_debug_info, address attributes in dies destined for the
4252 final executable should be direct references--setting the parameter
4253 force_direct ensures this behavior. */
4255 static inline void
4256 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4257 bool force_direct)
4259 dw_attr_node attr;
4261 attr.dw_attr = attr_kind;
4262 attr.dw_attr_val.val_class = dw_val_class_addr;
4263 attr.dw_attr_val.v.val_addr = addr;
4264 if (dwarf_split_debug_info && !force_direct)
4265 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4266 else
4267 attr.dw_attr_val.val_entry = NULL;
4268 add_dwarf_attr (die, &attr);
4271 /* Get the RTX from to an address DIE attribute. */
4273 static inline rtx
4274 AT_addr (dw_attr_ref a)
4276 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4277 return a->dw_attr_val.v.val_addr;
4280 /* Add a file attribute value to a DIE. */
4282 static inline void
4283 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4284 struct dwarf_file_data *fd)
4286 dw_attr_node attr;
4288 attr.dw_attr = attr_kind;
4289 attr.dw_attr_val.val_class = dw_val_class_file;
4290 attr.dw_attr_val.val_entry = NULL;
4291 attr.dw_attr_val.v.val_file = fd;
4292 add_dwarf_attr (die, &attr);
4295 /* Get the dwarf_file_data from a file DIE attribute. */
4297 static inline struct dwarf_file_data *
4298 AT_file (dw_attr_ref a)
4300 gcc_assert (a && AT_class (a) == dw_val_class_file);
4301 return a->dw_attr_val.v.val_file;
4304 /* Add a vms delta attribute value to a DIE. */
4306 static inline void
4307 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4308 const char *lbl1, const char *lbl2)
4310 dw_attr_node attr;
4312 attr.dw_attr = attr_kind;
4313 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4314 attr.dw_attr_val.val_entry = NULL;
4315 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4316 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4317 add_dwarf_attr (die, &attr);
4320 /* Add a label identifier attribute value to a DIE. */
4322 static inline void
4323 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4324 const char *lbl_id)
4326 dw_attr_node attr;
4328 attr.dw_attr = attr_kind;
4329 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4330 attr.dw_attr_val.val_entry = NULL;
4331 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4332 if (dwarf_split_debug_info)
4333 attr.dw_attr_val.val_entry
4334 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4335 ate_kind_label);
4336 add_dwarf_attr (die, &attr);
4339 /* Add a section offset attribute value to a DIE, an offset into the
4340 debug_line section. */
4342 static inline void
4343 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4344 const char *label)
4346 dw_attr_node attr;
4348 attr.dw_attr = attr_kind;
4349 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4350 attr.dw_attr_val.val_entry = NULL;
4351 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4352 add_dwarf_attr (die, &attr);
4355 /* Add a section offset attribute value to a DIE, an offset into the
4356 debug_macinfo section. */
4358 static inline void
4359 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4360 const char *label)
4362 dw_attr_node attr;
4364 attr.dw_attr = attr_kind;
4365 attr.dw_attr_val.val_class = dw_val_class_macptr;
4366 attr.dw_attr_val.val_entry = NULL;
4367 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4368 add_dwarf_attr (die, &attr);
4371 /* Add an offset attribute value to a DIE. */
4373 static inline void
4374 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4375 unsigned HOST_WIDE_INT offset)
4377 dw_attr_node attr;
4379 attr.dw_attr = attr_kind;
4380 attr.dw_attr_val.val_class = dw_val_class_offset;
4381 attr.dw_attr_val.val_entry = NULL;
4382 attr.dw_attr_val.v.val_offset = offset;
4383 add_dwarf_attr (die, &attr);
4386 /* Add a range_list attribute value to a DIE. When using
4387 dwarf_split_debug_info, address attributes in dies destined for the
4388 final executable should be direct references--setting the parameter
4389 force_direct ensures this behavior. */
4391 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4392 #define RELOCATED_OFFSET (NULL)
4394 static void
4395 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4396 long unsigned int offset, bool force_direct)
4398 dw_attr_node attr;
4400 attr.dw_attr = attr_kind;
4401 attr.dw_attr_val.val_class = dw_val_class_range_list;
4402 /* For the range_list attribute, use val_entry to store whether the
4403 offset should follow split-debug-info or normal semantics. This
4404 value is read in output_range_list_offset. */
4405 if (dwarf_split_debug_info && !force_direct)
4406 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4407 else
4408 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4409 attr.dw_attr_val.v.val_offset = offset;
4410 add_dwarf_attr (die, &attr);
4413 /* Return the start label of a delta attribute. */
4415 static inline const char *
4416 AT_vms_delta1 (dw_attr_ref a)
4418 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4419 return a->dw_attr_val.v.val_vms_delta.lbl1;
4422 /* Return the end label of a delta attribute. */
4424 static inline const char *
4425 AT_vms_delta2 (dw_attr_ref a)
4427 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4428 return a->dw_attr_val.v.val_vms_delta.lbl2;
4431 static inline const char *
4432 AT_lbl (dw_attr_ref a)
4434 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4435 || AT_class (a) == dw_val_class_lineptr
4436 || AT_class (a) == dw_val_class_macptr
4437 || AT_class (a) == dw_val_class_high_pc));
4438 return a->dw_attr_val.v.val_lbl_id;
4441 /* Get the attribute of type attr_kind. */
4443 static dw_attr_ref
4444 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4446 dw_attr_ref a;
4447 unsigned ix;
4448 dw_die_ref spec = NULL;
4450 if (! die)
4451 return NULL;
4453 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4454 if (a->dw_attr == attr_kind)
4455 return a;
4456 else if (a->dw_attr == DW_AT_specification
4457 || a->dw_attr == DW_AT_abstract_origin)
4458 spec = AT_ref (a);
4460 if (spec)
4461 return get_AT (spec, attr_kind);
4463 return NULL;
4466 /* Returns the parent of the declaration of DIE. */
4468 static dw_die_ref
4469 get_die_parent (dw_die_ref die)
4471 dw_die_ref t;
4473 if (!die)
4474 return NULL;
4476 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4477 || (t = get_AT_ref (die, DW_AT_specification)))
4478 die = t;
4480 return die->die_parent;
4483 /* Return the "low pc" attribute value, typically associated with a subprogram
4484 DIE. Return null if the "low pc" attribute is either not present, or if it
4485 cannot be represented as an assembler label identifier. */
4487 static inline const char *
4488 get_AT_low_pc (dw_die_ref die)
4490 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4492 return a ? AT_lbl (a) : NULL;
4495 /* Return the "high pc" attribute value, typically associated with a subprogram
4496 DIE. Return null if the "high pc" attribute is either not present, or if it
4497 cannot be represented as an assembler label identifier. */
4499 static inline const char *
4500 get_AT_hi_pc (dw_die_ref die)
4502 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4504 return a ? AT_lbl (a) : NULL;
4507 /* Return the value of the string attribute designated by ATTR_KIND, or
4508 NULL if it is not present. */
4510 static inline const char *
4511 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4513 dw_attr_ref a = get_AT (die, attr_kind);
4515 return a ? AT_string (a) : NULL;
4518 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4519 if it is not present. */
4521 static inline int
4522 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4524 dw_attr_ref a = get_AT (die, attr_kind);
4526 return a ? AT_flag (a) : 0;
4529 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4530 if it is not present. */
4532 static inline unsigned
4533 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4535 dw_attr_ref a = get_AT (die, attr_kind);
4537 return a ? AT_unsigned (a) : 0;
4540 static inline dw_die_ref
4541 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4543 dw_attr_ref a = get_AT (die, attr_kind);
4545 return a ? AT_ref (a) : NULL;
4548 static inline struct dwarf_file_data *
4549 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4551 dw_attr_ref a = get_AT (die, attr_kind);
4553 return a ? AT_file (a) : NULL;
4556 /* Return TRUE if the language is C++. */
4558 static inline bool
4559 is_cxx (void)
4561 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4563 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4566 /* Return TRUE if the language is Fortran. */
4568 static inline bool
4569 is_fortran (void)
4571 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4573 return (lang == DW_LANG_Fortran77
4574 || lang == DW_LANG_Fortran90
4575 || lang == DW_LANG_Fortran95);
4578 /* Return TRUE if the language is Ada. */
4580 static inline bool
4581 is_ada (void)
4583 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4585 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4588 /* Remove the specified attribute if present. */
4590 static void
4591 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4593 dw_attr_ref a;
4594 unsigned ix;
4596 if (! die)
4597 return;
4599 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4600 if (a->dw_attr == attr_kind)
4602 if (AT_class (a) == dw_val_class_str)
4603 if (a->dw_attr_val.v.val_str->refcount)
4604 a->dw_attr_val.v.val_str->refcount--;
4606 /* vec::ordered_remove should help reduce the number of abbrevs
4607 that are needed. */
4608 die->die_attr->ordered_remove (ix);
4609 return;
4613 /* Remove CHILD from its parent. PREV must have the property that
4614 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4616 static void
4617 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4619 gcc_assert (child->die_parent == prev->die_parent);
4620 gcc_assert (prev->die_sib == child);
4621 if (prev == child)
4623 gcc_assert (child->die_parent->die_child == child);
4624 prev = NULL;
4626 else
4627 prev->die_sib = child->die_sib;
4628 if (child->die_parent->die_child == child)
4629 child->die_parent->die_child = prev;
4632 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4633 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4635 static void
4636 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4638 dw_die_ref parent = old_child->die_parent;
4640 gcc_assert (parent == prev->die_parent);
4641 gcc_assert (prev->die_sib == old_child);
4643 new_child->die_parent = parent;
4644 if (prev == old_child)
4646 gcc_assert (parent->die_child == old_child);
4647 new_child->die_sib = new_child;
4649 else
4651 prev->die_sib = new_child;
4652 new_child->die_sib = old_child->die_sib;
4654 if (old_child->die_parent->die_child == old_child)
4655 old_child->die_parent->die_child = new_child;
4658 /* Move all children from OLD_PARENT to NEW_PARENT. */
4660 static void
4661 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4663 dw_die_ref c;
4664 new_parent->die_child = old_parent->die_child;
4665 old_parent->die_child = NULL;
4666 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4669 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4670 matches TAG. */
4672 static void
4673 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4675 dw_die_ref c;
4677 c = die->die_child;
4678 if (c) do {
4679 dw_die_ref prev = c;
4680 c = c->die_sib;
4681 while (c->die_tag == tag)
4683 remove_child_with_prev (c, prev);
4684 /* Might have removed every child. */
4685 if (c == c->die_sib)
4686 return;
4687 c = c->die_sib;
4689 } while (c != die->die_child);
4692 /* Add a CHILD_DIE as the last child of DIE. */
4694 static void
4695 add_child_die (dw_die_ref die, dw_die_ref child_die)
4697 /* FIXME this should probably be an assert. */
4698 if (! die || ! child_die)
4699 return;
4700 gcc_assert (die != child_die);
4702 child_die->die_parent = die;
4703 if (die->die_child)
4705 child_die->die_sib = die->die_child->die_sib;
4706 die->die_child->die_sib = child_die;
4708 else
4709 child_die->die_sib = child_die;
4710 die->die_child = child_die;
4713 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4714 is the specification, to the end of PARENT's list of children.
4715 This is done by removing and re-adding it. */
4717 static void
4718 splice_child_die (dw_die_ref parent, dw_die_ref child)
4720 dw_die_ref p;
4722 /* We want the declaration DIE from inside the class, not the
4723 specification DIE at toplevel. */
4724 if (child->die_parent != parent)
4726 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4728 if (tmp)
4729 child = tmp;
4732 gcc_assert (child->die_parent == parent
4733 || (child->die_parent
4734 == get_AT_ref (parent, DW_AT_specification)));
4736 for (p = child->die_parent->die_child; ; p = p->die_sib)
4737 if (p->die_sib == child)
4739 remove_child_with_prev (child, p);
4740 break;
4743 add_child_die (parent, child);
4746 /* Return a pointer to a newly created DIE node. */
4748 static inline dw_die_ref
4749 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4751 dw_die_ref die = ggc_alloc_cleared_die_node ();
4753 die->die_tag = tag_value;
4755 if (parent_die != NULL)
4756 add_child_die (parent_die, die);
4757 else
4759 limbo_die_node *limbo_node;
4761 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4762 limbo_node->die = die;
4763 limbo_node->created_for = t;
4764 limbo_node->next = limbo_die_list;
4765 limbo_die_list = limbo_node;
4768 return die;
4771 /* Return the DIE associated with the given type specifier. */
4773 static inline dw_die_ref
4774 lookup_type_die (tree type)
4776 return TYPE_SYMTAB_DIE (type);
4779 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4780 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4781 anonymous type instead the one of the naming typedef. */
4783 static inline dw_die_ref
4784 strip_naming_typedef (tree type, dw_die_ref type_die)
4786 if (type
4787 && TREE_CODE (type) == RECORD_TYPE
4788 && type_die
4789 && type_die->die_tag == DW_TAG_typedef
4790 && is_naming_typedef_decl (TYPE_NAME (type)))
4791 type_die = get_AT_ref (type_die, DW_AT_type);
4792 return type_die;
4795 /* Like lookup_type_die, but if type is an anonymous type named by a
4796 typedef[1], return the DIE of the anonymous type instead the one of
4797 the naming typedef. This is because in gen_typedef_die, we did
4798 equate the anonymous struct named by the typedef with the DIE of
4799 the naming typedef. So by default, lookup_type_die on an anonymous
4800 struct yields the DIE of the naming typedef.
4802 [1]: Read the comment of is_naming_typedef_decl to learn about what
4803 a naming typedef is. */
4805 static inline dw_die_ref
4806 lookup_type_die_strip_naming_typedef (tree type)
4808 dw_die_ref die = lookup_type_die (type);
4809 return strip_naming_typedef (type, die);
4812 /* Equate a DIE to a given type specifier. */
4814 static inline void
4815 equate_type_number_to_die (tree type, dw_die_ref type_die)
4817 TYPE_SYMTAB_DIE (type) = type_die;
4820 /* Returns a hash value for X (which really is a die_struct). */
4822 static hashval_t
4823 decl_die_table_hash (const void *x)
4825 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4828 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4830 static int
4831 decl_die_table_eq (const void *x, const void *y)
4833 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4836 /* Return the DIE associated with a given declaration. */
4838 static inline dw_die_ref
4839 lookup_decl_die (tree decl)
4841 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4844 /* Returns a hash value for X (which really is a var_loc_list). */
4846 static hashval_t
4847 decl_loc_table_hash (const void *x)
4849 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4852 /* Return nonzero if decl_id of var_loc_list X is the same as
4853 UID of decl *Y. */
4855 static int
4856 decl_loc_table_eq (const void *x, const void *y)
4858 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4861 /* Return the var_loc list associated with a given declaration. */
4863 static inline var_loc_list *
4864 lookup_decl_loc (const_tree decl)
4866 if (!decl_loc_table)
4867 return NULL;
4868 return (var_loc_list *)
4869 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4872 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4874 static hashval_t
4875 cached_dw_loc_list_table_hash (const void *x)
4877 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4880 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4881 UID of decl *Y. */
4883 static int
4884 cached_dw_loc_list_table_eq (const void *x, const void *y)
4886 return (((const cached_dw_loc_list *) x)->decl_id
4887 == DECL_UID ((const_tree) y));
4890 /* Equate a DIE to a particular declaration. */
4892 static void
4893 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4895 unsigned int decl_id = DECL_UID (decl);
4896 void **slot;
4898 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4899 *slot = decl_die;
4900 decl_die->decl_id = decl_id;
4903 /* Return how many bits covers PIECE EXPR_LIST. */
4905 static int
4906 decl_piece_bitsize (rtx piece)
4908 int ret = (int) GET_MODE (piece);
4909 if (ret)
4910 return ret;
4911 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4912 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4913 return INTVAL (XEXP (XEXP (piece, 0), 0));
4916 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4918 static rtx *
4919 decl_piece_varloc_ptr (rtx piece)
4921 if ((int) GET_MODE (piece))
4922 return &XEXP (piece, 0);
4923 else
4924 return &XEXP (XEXP (piece, 0), 1);
4927 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4928 Next is the chain of following piece nodes. */
4930 static rtx
4931 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4933 if (bitsize <= (int) MAX_MACHINE_MODE)
4934 return alloc_EXPR_LIST (bitsize, loc_note, next);
4935 else
4936 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4937 GEN_INT (bitsize),
4938 loc_note), next);
4941 /* Return rtx that should be stored into loc field for
4942 LOC_NOTE and BITPOS/BITSIZE. */
4944 static rtx
4945 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4946 HOST_WIDE_INT bitsize)
4948 if (bitsize != -1)
4950 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4951 if (bitpos != 0)
4952 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4954 return loc_note;
4957 /* This function either modifies location piece list *DEST in
4958 place (if SRC and INNER is NULL), or copies location piece list
4959 *SRC to *DEST while modifying it. Location BITPOS is modified
4960 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4961 not copied and if needed some padding around it is added.
4962 When modifying in place, DEST should point to EXPR_LIST where
4963 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4964 to the start of the whole list and INNER points to the EXPR_LIST
4965 where earlier pieces cover PIECE_BITPOS bits. */
4967 static void
4968 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4969 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4970 HOST_WIDE_INT bitsize, rtx loc_note)
4972 int diff;
4973 bool copy = inner != NULL;
4975 if (copy)
4977 /* First copy all nodes preceding the current bitpos. */
4978 while (src != inner)
4980 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4981 decl_piece_bitsize (*src), NULL_RTX);
4982 dest = &XEXP (*dest, 1);
4983 src = &XEXP (*src, 1);
4986 /* Add padding if needed. */
4987 if (bitpos != piece_bitpos)
4989 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4990 copy ? NULL_RTX : *dest);
4991 dest = &XEXP (*dest, 1);
4993 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4995 gcc_assert (!copy);
4996 /* A piece with correct bitpos and bitsize already exist,
4997 just update the location for it and return. */
4998 *decl_piece_varloc_ptr (*dest) = loc_note;
4999 return;
5001 /* Add the piece that changed. */
5002 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5003 dest = &XEXP (*dest, 1);
5004 /* Skip over pieces that overlap it. */
5005 diff = bitpos - piece_bitpos + bitsize;
5006 if (!copy)
5007 src = dest;
5008 while (diff > 0 && *src)
5010 rtx piece = *src;
5011 diff -= decl_piece_bitsize (piece);
5012 if (copy)
5013 src = &XEXP (piece, 1);
5014 else
5016 *src = XEXP (piece, 1);
5017 free_EXPR_LIST_node (piece);
5020 /* Add padding if needed. */
5021 if (diff < 0 && *src)
5023 if (!copy)
5024 dest = src;
5025 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5026 dest = &XEXP (*dest, 1);
5028 if (!copy)
5029 return;
5030 /* Finally copy all nodes following it. */
5031 while (*src)
5033 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5034 decl_piece_bitsize (*src), NULL_RTX);
5035 dest = &XEXP (*dest, 1);
5036 src = &XEXP (*src, 1);
5040 /* Add a variable location node to the linked list for DECL. */
5042 static struct var_loc_node *
5043 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5045 unsigned int decl_id;
5046 var_loc_list *temp;
5047 void **slot;
5048 struct var_loc_node *loc = NULL;
5049 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5051 if (TREE_CODE (decl) == VAR_DECL
5052 && DECL_HAS_DEBUG_EXPR_P (decl))
5054 tree realdecl = DECL_DEBUG_EXPR (decl);
5055 if (handled_component_p (realdecl)
5056 || (TREE_CODE (realdecl) == MEM_REF
5057 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5059 HOST_WIDE_INT maxsize;
5060 tree innerdecl;
5061 innerdecl
5062 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5063 if (!DECL_P (innerdecl)
5064 || DECL_IGNORED_P (innerdecl)
5065 || TREE_STATIC (innerdecl)
5066 || bitsize <= 0
5067 || bitpos + bitsize > 256
5068 || bitsize != maxsize)
5069 return NULL;
5070 decl = innerdecl;
5074 decl_id = DECL_UID (decl);
5075 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5076 if (*slot == NULL)
5078 temp = ggc_alloc_cleared_var_loc_list ();
5079 temp->decl_id = decl_id;
5080 *slot = temp;
5082 else
5083 temp = (var_loc_list *) *slot;
5085 /* For PARM_DECLs try to keep around the original incoming value,
5086 even if that means we'll emit a zero-range .debug_loc entry. */
5087 if (temp->last
5088 && temp->first == temp->last
5089 && TREE_CODE (decl) == PARM_DECL
5090 && NOTE_P (temp->first->loc)
5091 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5092 && DECL_INCOMING_RTL (decl)
5093 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5094 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5095 == GET_CODE (DECL_INCOMING_RTL (decl))
5096 && prev_real_insn (temp->first->loc) == NULL_RTX
5097 && (bitsize != -1
5098 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5099 NOTE_VAR_LOCATION_LOC (loc_note))
5100 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5101 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5103 loc = ggc_alloc_cleared_var_loc_node ();
5104 temp->first->next = loc;
5105 temp->last = loc;
5106 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5108 else if (temp->last)
5110 struct var_loc_node *last = temp->last, *unused = NULL;
5111 rtx *piece_loc = NULL, last_loc_note;
5112 int piece_bitpos = 0;
5113 if (last->next)
5115 last = last->next;
5116 gcc_assert (last->next == NULL);
5118 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5120 piece_loc = &last->loc;
5123 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5124 if (piece_bitpos + cur_bitsize > bitpos)
5125 break;
5126 piece_bitpos += cur_bitsize;
5127 piece_loc = &XEXP (*piece_loc, 1);
5129 while (*piece_loc);
5131 /* TEMP->LAST here is either pointer to the last but one or
5132 last element in the chained list, LAST is pointer to the
5133 last element. */
5134 if (label && strcmp (last->label, label) == 0)
5136 /* For SRA optimized variables if there weren't any real
5137 insns since last note, just modify the last node. */
5138 if (piece_loc != NULL)
5140 adjust_piece_list (piece_loc, NULL, NULL,
5141 bitpos, piece_bitpos, bitsize, loc_note);
5142 return NULL;
5144 /* If the last note doesn't cover any instructions, remove it. */
5145 if (temp->last != last)
5147 temp->last->next = NULL;
5148 unused = last;
5149 last = temp->last;
5150 gcc_assert (strcmp (last->label, label) != 0);
5152 else
5154 gcc_assert (temp->first == temp->last
5155 || (temp->first->next == temp->last
5156 && TREE_CODE (decl) == PARM_DECL));
5157 memset (temp->last, '\0', sizeof (*temp->last));
5158 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5159 return temp->last;
5162 if (bitsize == -1 && NOTE_P (last->loc))
5163 last_loc_note = last->loc;
5164 else if (piece_loc != NULL
5165 && *piece_loc != NULL_RTX
5166 && piece_bitpos == bitpos
5167 && decl_piece_bitsize (*piece_loc) == bitsize)
5168 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5169 else
5170 last_loc_note = NULL_RTX;
5171 /* If the current location is the same as the end of the list,
5172 and either both or neither of the locations is uninitialized,
5173 we have nothing to do. */
5174 if (last_loc_note == NULL_RTX
5175 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5176 NOTE_VAR_LOCATION_LOC (loc_note)))
5177 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5178 != NOTE_VAR_LOCATION_STATUS (loc_note))
5179 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5180 == VAR_INIT_STATUS_UNINITIALIZED)
5181 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5182 == VAR_INIT_STATUS_UNINITIALIZED))))
5184 /* Add LOC to the end of list and update LAST. If the last
5185 element of the list has been removed above, reuse its
5186 memory for the new node, otherwise allocate a new one. */
5187 if (unused)
5189 loc = unused;
5190 memset (loc, '\0', sizeof (*loc));
5192 else
5193 loc = ggc_alloc_cleared_var_loc_node ();
5194 if (bitsize == -1 || piece_loc == NULL)
5195 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5196 else
5197 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5198 bitpos, piece_bitpos, bitsize, loc_note);
5199 last->next = loc;
5200 /* Ensure TEMP->LAST will point either to the new last but one
5201 element of the chain, or to the last element in it. */
5202 if (last != temp->last)
5203 temp->last = last;
5205 else if (unused)
5206 ggc_free (unused);
5208 else
5210 loc = ggc_alloc_cleared_var_loc_node ();
5211 temp->first = loc;
5212 temp->last = loc;
5213 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5215 return loc;
5218 /* Keep track of the number of spaces used to indent the
5219 output of the debugging routines that print the structure of
5220 the DIE internal representation. */
5221 static int print_indent;
5223 /* Indent the line the number of spaces given by print_indent. */
5225 static inline void
5226 print_spaces (FILE *outfile)
5228 fprintf (outfile, "%*s", print_indent, "");
5231 /* Print a type signature in hex. */
5233 static inline void
5234 print_signature (FILE *outfile, char *sig)
5236 int i;
5238 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5239 fprintf (outfile, "%02x", sig[i] & 0xff);
5242 /* Print the information associated with a given DIE, and its children.
5243 This routine is a debugging aid only. */
5245 static void
5246 print_die (dw_die_ref die, FILE *outfile)
5248 dw_attr_ref a;
5249 dw_die_ref c;
5250 unsigned ix;
5252 print_spaces (outfile);
5253 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5254 die->die_offset, dwarf_tag_name (die->die_tag),
5255 (void*) die);
5256 print_spaces (outfile);
5257 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5258 fprintf (outfile, " offset: %ld", die->die_offset);
5259 fprintf (outfile, " mark: %d\n", die->die_mark);
5261 if (die->comdat_type_p)
5263 print_spaces (outfile);
5264 fprintf (outfile, " signature: ");
5265 print_signature (outfile, die->die_id.die_type_node->signature);
5266 fprintf (outfile, "\n");
5269 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5271 print_spaces (outfile);
5272 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5274 switch (AT_class (a))
5276 case dw_val_class_addr:
5277 fprintf (outfile, "address");
5278 break;
5279 case dw_val_class_offset:
5280 fprintf (outfile, "offset");
5281 break;
5282 case dw_val_class_loc:
5283 fprintf (outfile, "location descriptor");
5284 break;
5285 case dw_val_class_loc_list:
5286 fprintf (outfile, "location list -> label:%s",
5287 AT_loc_list (a)->ll_symbol);
5288 break;
5289 case dw_val_class_range_list:
5290 fprintf (outfile, "range list");
5291 break;
5292 case dw_val_class_const:
5293 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5294 break;
5295 case dw_val_class_unsigned_const:
5296 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5297 break;
5298 case dw_val_class_const_double:
5299 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5300 HOST_WIDE_INT_PRINT_UNSIGNED")",
5301 a->dw_attr_val.v.val_double.high,
5302 a->dw_attr_val.v.val_double.low);
5303 break;
5304 case dw_val_class_vec:
5305 fprintf (outfile, "floating-point or vector constant");
5306 break;
5307 case dw_val_class_flag:
5308 fprintf (outfile, "%u", AT_flag (a));
5309 break;
5310 case dw_val_class_die_ref:
5311 if (AT_ref (a) != NULL)
5313 if (AT_ref (a)->comdat_type_p)
5315 fprintf (outfile, "die -> signature: ");
5316 print_signature (outfile,
5317 AT_ref (a)->die_id.die_type_node->signature);
5319 else if (AT_ref (a)->die_id.die_symbol)
5320 fprintf (outfile, "die -> label: %s",
5321 AT_ref (a)->die_id.die_symbol);
5322 else
5323 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5324 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5326 else
5327 fprintf (outfile, "die -> <null>");
5328 break;
5329 case dw_val_class_vms_delta:
5330 fprintf (outfile, "delta: @slotcount(%s-%s)",
5331 AT_vms_delta2 (a), AT_vms_delta1 (a));
5332 break;
5333 case dw_val_class_lbl_id:
5334 case dw_val_class_lineptr:
5335 case dw_val_class_macptr:
5336 case dw_val_class_high_pc:
5337 fprintf (outfile, "label: %s", AT_lbl (a));
5338 break;
5339 case dw_val_class_str:
5340 if (AT_string (a) != NULL)
5341 fprintf (outfile, "\"%s\"", AT_string (a));
5342 else
5343 fprintf (outfile, "<null>");
5344 break;
5345 case dw_val_class_file:
5346 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5347 AT_file (a)->emitted_number);
5348 break;
5349 case dw_val_class_data8:
5351 int i;
5353 for (i = 0; i < 8; i++)
5354 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5355 break;
5357 default:
5358 break;
5361 fprintf (outfile, "\n");
5364 if (die->die_child != NULL)
5366 print_indent += 4;
5367 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5368 print_indent -= 4;
5370 if (print_indent == 0)
5371 fprintf (outfile, "\n");
5374 /* Print the information collected for a given DIE. */
5376 DEBUG_FUNCTION void
5377 debug_dwarf_die (dw_die_ref die)
5379 print_die (die, stderr);
5382 DEBUG_FUNCTION void
5383 debug (die_struct &ref)
5385 print_die (&ref, stderr);
5388 DEBUG_FUNCTION void
5389 debug (die_struct *ptr)
5391 if (ptr)
5392 debug (*ptr);
5393 else
5394 fprintf (stderr, "<nil>\n");
5398 /* Print all DWARF information collected for the compilation unit.
5399 This routine is a debugging aid only. */
5401 DEBUG_FUNCTION void
5402 debug_dwarf (void)
5404 print_indent = 0;
5405 print_die (comp_unit_die (), stderr);
5408 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5409 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5410 DIE that marks the start of the DIEs for this include file. */
5412 static dw_die_ref
5413 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5415 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5416 dw_die_ref new_unit = gen_compile_unit_die (filename);
5418 new_unit->die_sib = old_unit;
5419 return new_unit;
5422 /* Close an include-file CU and reopen the enclosing one. */
5424 static dw_die_ref
5425 pop_compile_unit (dw_die_ref old_unit)
5427 dw_die_ref new_unit = old_unit->die_sib;
5429 old_unit->die_sib = NULL;
5430 return new_unit;
5433 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5434 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5436 /* Calculate the checksum of a location expression. */
5438 static inline void
5439 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5441 int tem;
5442 hashval_t hash = 0;
5444 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5445 CHECKSUM (tem);
5446 hash = hash_loc_operands (loc, hash);
5447 CHECKSUM (hash);
5450 /* Calculate the checksum of an attribute. */
5452 static void
5453 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5455 dw_loc_descr_ref loc;
5456 rtx r;
5458 CHECKSUM (at->dw_attr);
5460 /* We don't care that this was compiled with a different compiler
5461 snapshot; if the output is the same, that's what matters. */
5462 if (at->dw_attr == DW_AT_producer)
5463 return;
5465 switch (AT_class (at))
5467 case dw_val_class_const:
5468 CHECKSUM (at->dw_attr_val.v.val_int);
5469 break;
5470 case dw_val_class_unsigned_const:
5471 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5472 break;
5473 case dw_val_class_const_double:
5474 CHECKSUM (at->dw_attr_val.v.val_double);
5475 break;
5476 case dw_val_class_vec:
5477 CHECKSUM (at->dw_attr_val.v.val_vec);
5478 break;
5479 case dw_val_class_flag:
5480 CHECKSUM (at->dw_attr_val.v.val_flag);
5481 break;
5482 case dw_val_class_str:
5483 CHECKSUM_STRING (AT_string (at));
5484 break;
5486 case dw_val_class_addr:
5487 r = AT_addr (at);
5488 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5489 CHECKSUM_STRING (XSTR (r, 0));
5490 break;
5492 case dw_val_class_offset:
5493 CHECKSUM (at->dw_attr_val.v.val_offset);
5494 break;
5496 case dw_val_class_loc:
5497 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5498 loc_checksum (loc, ctx);
5499 break;
5501 case dw_val_class_die_ref:
5502 die_checksum (AT_ref (at), ctx, mark);
5503 break;
5505 case dw_val_class_fde_ref:
5506 case dw_val_class_vms_delta:
5507 case dw_val_class_lbl_id:
5508 case dw_val_class_lineptr:
5509 case dw_val_class_macptr:
5510 case dw_val_class_high_pc:
5511 break;
5513 case dw_val_class_file:
5514 CHECKSUM_STRING (AT_file (at)->filename);
5515 break;
5517 case dw_val_class_data8:
5518 CHECKSUM (at->dw_attr_val.v.val_data8);
5519 break;
5521 default:
5522 break;
5526 /* Calculate the checksum of a DIE. */
5528 static void
5529 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5531 dw_die_ref c;
5532 dw_attr_ref a;
5533 unsigned ix;
5535 /* To avoid infinite recursion. */
5536 if (die->die_mark)
5538 CHECKSUM (die->die_mark);
5539 return;
5541 die->die_mark = ++(*mark);
5543 CHECKSUM (die->die_tag);
5545 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5546 attr_checksum (a, ctx, mark);
5548 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5551 #undef CHECKSUM
5552 #undef CHECKSUM_STRING
5554 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5555 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5556 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5557 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5558 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5559 #define CHECKSUM_ATTR(FOO) \
5560 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5562 /* Calculate the checksum of a number in signed LEB128 format. */
5564 static void
5565 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5567 unsigned char byte;
5568 bool more;
5570 while (1)
5572 byte = (value & 0x7f);
5573 value >>= 7;
5574 more = !((value == 0 && (byte & 0x40) == 0)
5575 || (value == -1 && (byte & 0x40) != 0));
5576 if (more)
5577 byte |= 0x80;
5578 CHECKSUM (byte);
5579 if (!more)
5580 break;
5584 /* Calculate the checksum of a number in unsigned LEB128 format. */
5586 static void
5587 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5589 while (1)
5591 unsigned char byte = (value & 0x7f);
5592 value >>= 7;
5593 if (value != 0)
5594 /* More bytes to follow. */
5595 byte |= 0x80;
5596 CHECKSUM (byte);
5597 if (value == 0)
5598 break;
5602 /* Checksum the context of the DIE. This adds the names of any
5603 surrounding namespaces or structures to the checksum. */
5605 static void
5606 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5608 const char *name;
5609 dw_die_ref spec;
5610 int tag = die->die_tag;
5612 if (tag != DW_TAG_namespace
5613 && tag != DW_TAG_structure_type
5614 && tag != DW_TAG_class_type)
5615 return;
5617 name = get_AT_string (die, DW_AT_name);
5619 spec = get_AT_ref (die, DW_AT_specification);
5620 if (spec != NULL)
5621 die = spec;
5623 if (die->die_parent != NULL)
5624 checksum_die_context (die->die_parent, ctx);
5626 CHECKSUM_ULEB128 ('C');
5627 CHECKSUM_ULEB128 (tag);
5628 if (name != NULL)
5629 CHECKSUM_STRING (name);
5632 /* Calculate the checksum of a location expression. */
5634 static inline void
5635 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5637 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5638 were emitted as a DW_FORM_sdata instead of a location expression. */
5639 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5641 CHECKSUM_ULEB128 (DW_FORM_sdata);
5642 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5643 return;
5646 /* Otherwise, just checksum the raw location expression. */
5647 while (loc != NULL)
5649 hashval_t hash = 0;
5651 CHECKSUM_ULEB128 (loc->dtprel);
5652 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5653 hash = hash_loc_operands (loc, hash);
5654 CHECKSUM (hash);
5655 loc = loc->dw_loc_next;
5659 /* Calculate the checksum of an attribute. */
5661 static void
5662 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5663 struct md5_ctx *ctx, int *mark)
5665 dw_loc_descr_ref loc;
5666 rtx r;
5668 if (AT_class (at) == dw_val_class_die_ref)
5670 dw_die_ref target_die = AT_ref (at);
5672 /* For pointer and reference types, we checksum only the (qualified)
5673 name of the target type (if there is a name). For friend entries,
5674 we checksum only the (qualified) name of the target type or function.
5675 This allows the checksum to remain the same whether the target type
5676 is complete or not. */
5677 if ((at->dw_attr == DW_AT_type
5678 && (tag == DW_TAG_pointer_type
5679 || tag == DW_TAG_reference_type
5680 || tag == DW_TAG_rvalue_reference_type
5681 || tag == DW_TAG_ptr_to_member_type))
5682 || (at->dw_attr == DW_AT_friend
5683 && tag == DW_TAG_friend))
5685 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5687 if (name_attr != NULL)
5689 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5691 if (decl == NULL)
5692 decl = target_die;
5693 CHECKSUM_ULEB128 ('N');
5694 CHECKSUM_ULEB128 (at->dw_attr);
5695 if (decl->die_parent != NULL)
5696 checksum_die_context (decl->die_parent, ctx);
5697 CHECKSUM_ULEB128 ('E');
5698 CHECKSUM_STRING (AT_string (name_attr));
5699 return;
5703 /* For all other references to another DIE, we check to see if the
5704 target DIE has already been visited. If it has, we emit a
5705 backward reference; if not, we descend recursively. */
5706 if (target_die->die_mark > 0)
5708 CHECKSUM_ULEB128 ('R');
5709 CHECKSUM_ULEB128 (at->dw_attr);
5710 CHECKSUM_ULEB128 (target_die->die_mark);
5712 else
5714 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5716 if (decl == NULL)
5717 decl = target_die;
5718 target_die->die_mark = ++(*mark);
5719 CHECKSUM_ULEB128 ('T');
5720 CHECKSUM_ULEB128 (at->dw_attr);
5721 if (decl->die_parent != NULL)
5722 checksum_die_context (decl->die_parent, ctx);
5723 die_checksum_ordered (target_die, ctx, mark);
5725 return;
5728 CHECKSUM_ULEB128 ('A');
5729 CHECKSUM_ULEB128 (at->dw_attr);
5731 switch (AT_class (at))
5733 case dw_val_class_const:
5734 CHECKSUM_ULEB128 (DW_FORM_sdata);
5735 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5736 break;
5738 case dw_val_class_unsigned_const:
5739 CHECKSUM_ULEB128 (DW_FORM_sdata);
5740 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5741 break;
5743 case dw_val_class_const_double:
5744 CHECKSUM_ULEB128 (DW_FORM_block);
5745 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5746 CHECKSUM (at->dw_attr_val.v.val_double);
5747 break;
5749 case dw_val_class_vec:
5750 CHECKSUM_ULEB128 (DW_FORM_block);
5751 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5752 CHECKSUM (at->dw_attr_val.v.val_vec);
5753 break;
5755 case dw_val_class_flag:
5756 CHECKSUM_ULEB128 (DW_FORM_flag);
5757 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5758 break;
5760 case dw_val_class_str:
5761 CHECKSUM_ULEB128 (DW_FORM_string);
5762 CHECKSUM_STRING (AT_string (at));
5763 break;
5765 case dw_val_class_addr:
5766 r = AT_addr (at);
5767 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5768 CHECKSUM_ULEB128 (DW_FORM_string);
5769 CHECKSUM_STRING (XSTR (r, 0));
5770 break;
5772 case dw_val_class_offset:
5773 CHECKSUM_ULEB128 (DW_FORM_sdata);
5774 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5775 break;
5777 case dw_val_class_loc:
5778 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5779 loc_checksum_ordered (loc, ctx);
5780 break;
5782 case dw_val_class_fde_ref:
5783 case dw_val_class_lbl_id:
5784 case dw_val_class_lineptr:
5785 case dw_val_class_macptr:
5786 case dw_val_class_high_pc:
5787 break;
5789 case dw_val_class_file:
5790 CHECKSUM_ULEB128 (DW_FORM_string);
5791 CHECKSUM_STRING (AT_file (at)->filename);
5792 break;
5794 case dw_val_class_data8:
5795 CHECKSUM (at->dw_attr_val.v.val_data8);
5796 break;
5798 default:
5799 break;
5803 struct checksum_attributes
5805 dw_attr_ref at_name;
5806 dw_attr_ref at_type;
5807 dw_attr_ref at_friend;
5808 dw_attr_ref at_accessibility;
5809 dw_attr_ref at_address_class;
5810 dw_attr_ref at_allocated;
5811 dw_attr_ref at_artificial;
5812 dw_attr_ref at_associated;
5813 dw_attr_ref at_binary_scale;
5814 dw_attr_ref at_bit_offset;
5815 dw_attr_ref at_bit_size;
5816 dw_attr_ref at_bit_stride;
5817 dw_attr_ref at_byte_size;
5818 dw_attr_ref at_byte_stride;
5819 dw_attr_ref at_const_value;
5820 dw_attr_ref at_containing_type;
5821 dw_attr_ref at_count;
5822 dw_attr_ref at_data_location;
5823 dw_attr_ref at_data_member_location;
5824 dw_attr_ref at_decimal_scale;
5825 dw_attr_ref at_decimal_sign;
5826 dw_attr_ref at_default_value;
5827 dw_attr_ref at_digit_count;
5828 dw_attr_ref at_discr;
5829 dw_attr_ref at_discr_list;
5830 dw_attr_ref at_discr_value;
5831 dw_attr_ref at_encoding;
5832 dw_attr_ref at_endianity;
5833 dw_attr_ref at_explicit;
5834 dw_attr_ref at_is_optional;
5835 dw_attr_ref at_location;
5836 dw_attr_ref at_lower_bound;
5837 dw_attr_ref at_mutable;
5838 dw_attr_ref at_ordering;
5839 dw_attr_ref at_picture_string;
5840 dw_attr_ref at_prototyped;
5841 dw_attr_ref at_small;
5842 dw_attr_ref at_segment;
5843 dw_attr_ref at_string_length;
5844 dw_attr_ref at_threads_scaled;
5845 dw_attr_ref at_upper_bound;
5846 dw_attr_ref at_use_location;
5847 dw_attr_ref at_use_UTF8;
5848 dw_attr_ref at_variable_parameter;
5849 dw_attr_ref at_virtuality;
5850 dw_attr_ref at_visibility;
5851 dw_attr_ref at_vtable_elem_location;
5854 /* Collect the attributes that we will want to use for the checksum. */
5856 static void
5857 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5859 dw_attr_ref a;
5860 unsigned ix;
5862 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5864 switch (a->dw_attr)
5866 case DW_AT_name:
5867 attrs->at_name = a;
5868 break;
5869 case DW_AT_type:
5870 attrs->at_type = a;
5871 break;
5872 case DW_AT_friend:
5873 attrs->at_friend = a;
5874 break;
5875 case DW_AT_accessibility:
5876 attrs->at_accessibility = a;
5877 break;
5878 case DW_AT_address_class:
5879 attrs->at_address_class = a;
5880 break;
5881 case DW_AT_allocated:
5882 attrs->at_allocated = a;
5883 break;
5884 case DW_AT_artificial:
5885 attrs->at_artificial = a;
5886 break;
5887 case DW_AT_associated:
5888 attrs->at_associated = a;
5889 break;
5890 case DW_AT_binary_scale:
5891 attrs->at_binary_scale = a;
5892 break;
5893 case DW_AT_bit_offset:
5894 attrs->at_bit_offset = a;
5895 break;
5896 case DW_AT_bit_size:
5897 attrs->at_bit_size = a;
5898 break;
5899 case DW_AT_bit_stride:
5900 attrs->at_bit_stride = a;
5901 break;
5902 case DW_AT_byte_size:
5903 attrs->at_byte_size = a;
5904 break;
5905 case DW_AT_byte_stride:
5906 attrs->at_byte_stride = a;
5907 break;
5908 case DW_AT_const_value:
5909 attrs->at_const_value = a;
5910 break;
5911 case DW_AT_containing_type:
5912 attrs->at_containing_type = a;
5913 break;
5914 case DW_AT_count:
5915 attrs->at_count = a;
5916 break;
5917 case DW_AT_data_location:
5918 attrs->at_data_location = a;
5919 break;
5920 case DW_AT_data_member_location:
5921 attrs->at_data_member_location = a;
5922 break;
5923 case DW_AT_decimal_scale:
5924 attrs->at_decimal_scale = a;
5925 break;
5926 case DW_AT_decimal_sign:
5927 attrs->at_decimal_sign = a;
5928 break;
5929 case DW_AT_default_value:
5930 attrs->at_default_value = a;
5931 break;
5932 case DW_AT_digit_count:
5933 attrs->at_digit_count = a;
5934 break;
5935 case DW_AT_discr:
5936 attrs->at_discr = a;
5937 break;
5938 case DW_AT_discr_list:
5939 attrs->at_discr_list = a;
5940 break;
5941 case DW_AT_discr_value:
5942 attrs->at_discr_value = a;
5943 break;
5944 case DW_AT_encoding:
5945 attrs->at_encoding = a;
5946 break;
5947 case DW_AT_endianity:
5948 attrs->at_endianity = a;
5949 break;
5950 case DW_AT_explicit:
5951 attrs->at_explicit = a;
5952 break;
5953 case DW_AT_is_optional:
5954 attrs->at_is_optional = a;
5955 break;
5956 case DW_AT_location:
5957 attrs->at_location = a;
5958 break;
5959 case DW_AT_lower_bound:
5960 attrs->at_lower_bound = a;
5961 break;
5962 case DW_AT_mutable:
5963 attrs->at_mutable = a;
5964 break;
5965 case DW_AT_ordering:
5966 attrs->at_ordering = a;
5967 break;
5968 case DW_AT_picture_string:
5969 attrs->at_picture_string = a;
5970 break;
5971 case DW_AT_prototyped:
5972 attrs->at_prototyped = a;
5973 break;
5974 case DW_AT_small:
5975 attrs->at_small = a;
5976 break;
5977 case DW_AT_segment:
5978 attrs->at_segment = a;
5979 break;
5980 case DW_AT_string_length:
5981 attrs->at_string_length = a;
5982 break;
5983 case DW_AT_threads_scaled:
5984 attrs->at_threads_scaled = a;
5985 break;
5986 case DW_AT_upper_bound:
5987 attrs->at_upper_bound = a;
5988 break;
5989 case DW_AT_use_location:
5990 attrs->at_use_location = a;
5991 break;
5992 case DW_AT_use_UTF8:
5993 attrs->at_use_UTF8 = a;
5994 break;
5995 case DW_AT_variable_parameter:
5996 attrs->at_variable_parameter = a;
5997 break;
5998 case DW_AT_virtuality:
5999 attrs->at_virtuality = a;
6000 break;
6001 case DW_AT_visibility:
6002 attrs->at_visibility = a;
6003 break;
6004 case DW_AT_vtable_elem_location:
6005 attrs->at_vtable_elem_location = a;
6006 break;
6007 default:
6008 break;
6013 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6015 static void
6016 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6018 dw_die_ref c;
6019 dw_die_ref decl;
6020 struct checksum_attributes attrs;
6022 CHECKSUM_ULEB128 ('D');
6023 CHECKSUM_ULEB128 (die->die_tag);
6025 memset (&attrs, 0, sizeof (attrs));
6027 decl = get_AT_ref (die, DW_AT_specification);
6028 if (decl != NULL)
6029 collect_checksum_attributes (&attrs, decl);
6030 collect_checksum_attributes (&attrs, die);
6032 CHECKSUM_ATTR (attrs.at_name);
6033 CHECKSUM_ATTR (attrs.at_accessibility);
6034 CHECKSUM_ATTR (attrs.at_address_class);
6035 CHECKSUM_ATTR (attrs.at_allocated);
6036 CHECKSUM_ATTR (attrs.at_artificial);
6037 CHECKSUM_ATTR (attrs.at_associated);
6038 CHECKSUM_ATTR (attrs.at_binary_scale);
6039 CHECKSUM_ATTR (attrs.at_bit_offset);
6040 CHECKSUM_ATTR (attrs.at_bit_size);
6041 CHECKSUM_ATTR (attrs.at_bit_stride);
6042 CHECKSUM_ATTR (attrs.at_byte_size);
6043 CHECKSUM_ATTR (attrs.at_byte_stride);
6044 CHECKSUM_ATTR (attrs.at_const_value);
6045 CHECKSUM_ATTR (attrs.at_containing_type);
6046 CHECKSUM_ATTR (attrs.at_count);
6047 CHECKSUM_ATTR (attrs.at_data_location);
6048 CHECKSUM_ATTR (attrs.at_data_member_location);
6049 CHECKSUM_ATTR (attrs.at_decimal_scale);
6050 CHECKSUM_ATTR (attrs.at_decimal_sign);
6051 CHECKSUM_ATTR (attrs.at_default_value);
6052 CHECKSUM_ATTR (attrs.at_digit_count);
6053 CHECKSUM_ATTR (attrs.at_discr);
6054 CHECKSUM_ATTR (attrs.at_discr_list);
6055 CHECKSUM_ATTR (attrs.at_discr_value);
6056 CHECKSUM_ATTR (attrs.at_encoding);
6057 CHECKSUM_ATTR (attrs.at_endianity);
6058 CHECKSUM_ATTR (attrs.at_explicit);
6059 CHECKSUM_ATTR (attrs.at_is_optional);
6060 CHECKSUM_ATTR (attrs.at_location);
6061 CHECKSUM_ATTR (attrs.at_lower_bound);
6062 CHECKSUM_ATTR (attrs.at_mutable);
6063 CHECKSUM_ATTR (attrs.at_ordering);
6064 CHECKSUM_ATTR (attrs.at_picture_string);
6065 CHECKSUM_ATTR (attrs.at_prototyped);
6066 CHECKSUM_ATTR (attrs.at_small);
6067 CHECKSUM_ATTR (attrs.at_segment);
6068 CHECKSUM_ATTR (attrs.at_string_length);
6069 CHECKSUM_ATTR (attrs.at_threads_scaled);
6070 CHECKSUM_ATTR (attrs.at_upper_bound);
6071 CHECKSUM_ATTR (attrs.at_use_location);
6072 CHECKSUM_ATTR (attrs.at_use_UTF8);
6073 CHECKSUM_ATTR (attrs.at_variable_parameter);
6074 CHECKSUM_ATTR (attrs.at_virtuality);
6075 CHECKSUM_ATTR (attrs.at_visibility);
6076 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6077 CHECKSUM_ATTR (attrs.at_type);
6078 CHECKSUM_ATTR (attrs.at_friend);
6080 /* Checksum the child DIEs, except for nested types and member functions. */
6081 c = die->die_child;
6082 if (c) do {
6083 dw_attr_ref name_attr;
6085 c = c->die_sib;
6086 name_attr = get_AT (c, DW_AT_name);
6087 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6088 && name_attr != NULL)
6090 CHECKSUM_ULEB128 ('S');
6091 CHECKSUM_ULEB128 (c->die_tag);
6092 CHECKSUM_STRING (AT_string (name_attr));
6094 else
6096 /* Mark this DIE so it gets processed when unmarking. */
6097 if (c->die_mark == 0)
6098 c->die_mark = -1;
6099 die_checksum_ordered (c, ctx, mark);
6101 } while (c != die->die_child);
6103 CHECKSUM_ULEB128 (0);
6106 /* Add a type name and tag to a hash. */
6107 static void
6108 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6110 CHECKSUM_ULEB128 (tag);
6111 CHECKSUM_STRING (name);
6114 #undef CHECKSUM
6115 #undef CHECKSUM_STRING
6116 #undef CHECKSUM_ATTR
6117 #undef CHECKSUM_LEB128
6118 #undef CHECKSUM_ULEB128
6120 /* Generate the type signature for DIE. This is computed by generating an
6121 MD5 checksum over the DIE's tag, its relevant attributes, and its
6122 children. Attributes that are references to other DIEs are processed
6123 by recursion, using the MARK field to prevent infinite recursion.
6124 If the DIE is nested inside a namespace or another type, we also
6125 need to include that context in the signature. The lower 64 bits
6126 of the resulting MD5 checksum comprise the signature. */
6128 static void
6129 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6131 int mark;
6132 const char *name;
6133 unsigned char checksum[16];
6134 struct md5_ctx ctx;
6135 dw_die_ref decl;
6136 dw_die_ref parent;
6138 name = get_AT_string (die, DW_AT_name);
6139 decl = get_AT_ref (die, DW_AT_specification);
6140 parent = get_die_parent (die);
6142 /* First, compute a signature for just the type name (and its surrounding
6143 context, if any. This is stored in the type unit DIE for link-time
6144 ODR (one-definition rule) checking. */
6146 if (is_cxx() && name != NULL)
6148 md5_init_ctx (&ctx);
6150 /* Checksum the names of surrounding namespaces and structures. */
6151 if (parent != NULL)
6152 checksum_die_context (parent, &ctx);
6154 /* Checksum the current DIE. */
6155 die_odr_checksum (die->die_tag, name, &ctx);
6156 md5_finish_ctx (&ctx, checksum);
6158 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6161 /* Next, compute the complete type signature. */
6163 md5_init_ctx (&ctx);
6164 mark = 1;
6165 die->die_mark = mark;
6167 /* Checksum the names of surrounding namespaces and structures. */
6168 if (parent != NULL)
6169 checksum_die_context (parent, &ctx);
6171 /* Checksum the DIE and its children. */
6172 die_checksum_ordered (die, &ctx, &mark);
6173 unmark_all_dies (die);
6174 md5_finish_ctx (&ctx, checksum);
6176 /* Store the signature in the type node and link the type DIE and the
6177 type node together. */
6178 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6179 DWARF_TYPE_SIGNATURE_SIZE);
6180 die->comdat_type_p = true;
6181 die->die_id.die_type_node = type_node;
6182 type_node->type_die = die;
6184 /* If the DIE is a specification, link its declaration to the type node
6185 as well. */
6186 if (decl != NULL)
6188 decl->comdat_type_p = true;
6189 decl->die_id.die_type_node = type_node;
6193 /* Do the location expressions look same? */
6194 static inline int
6195 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6197 return loc1->dw_loc_opc == loc2->dw_loc_opc
6198 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6199 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6202 /* Do the values look the same? */
6203 static int
6204 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6206 dw_loc_descr_ref loc1, loc2;
6207 rtx r1, r2;
6209 if (v1->val_class != v2->val_class)
6210 return 0;
6212 switch (v1->val_class)
6214 case dw_val_class_const:
6215 return v1->v.val_int == v2->v.val_int;
6216 case dw_val_class_unsigned_const:
6217 return v1->v.val_unsigned == v2->v.val_unsigned;
6218 case dw_val_class_const_double:
6219 return v1->v.val_double.high == v2->v.val_double.high
6220 && v1->v.val_double.low == v2->v.val_double.low;
6221 case dw_val_class_vec:
6222 if (v1->v.val_vec.length != v2->v.val_vec.length
6223 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6224 return 0;
6225 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6226 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6227 return 0;
6228 return 1;
6229 case dw_val_class_flag:
6230 return v1->v.val_flag == v2->v.val_flag;
6231 case dw_val_class_str:
6232 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6234 case dw_val_class_addr:
6235 r1 = v1->v.val_addr;
6236 r2 = v2->v.val_addr;
6237 if (GET_CODE (r1) != GET_CODE (r2))
6238 return 0;
6239 return !rtx_equal_p (r1, r2);
6241 case dw_val_class_offset:
6242 return v1->v.val_offset == v2->v.val_offset;
6244 case dw_val_class_loc:
6245 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6246 loc1 && loc2;
6247 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6248 if (!same_loc_p (loc1, loc2, mark))
6249 return 0;
6250 return !loc1 && !loc2;
6252 case dw_val_class_die_ref:
6253 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6255 case dw_val_class_fde_ref:
6256 case dw_val_class_vms_delta:
6257 case dw_val_class_lbl_id:
6258 case dw_val_class_lineptr:
6259 case dw_val_class_macptr:
6260 case dw_val_class_high_pc:
6261 return 1;
6263 case dw_val_class_file:
6264 return v1->v.val_file == v2->v.val_file;
6266 case dw_val_class_data8:
6267 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6269 default:
6270 return 1;
6274 /* Do the attributes look the same? */
6276 static int
6277 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6279 if (at1->dw_attr != at2->dw_attr)
6280 return 0;
6282 /* We don't care that this was compiled with a different compiler
6283 snapshot; if the output is the same, that's what matters. */
6284 if (at1->dw_attr == DW_AT_producer)
6285 return 1;
6287 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6290 /* Do the dies look the same? */
6292 static int
6293 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6295 dw_die_ref c1, c2;
6296 dw_attr_ref a1;
6297 unsigned ix;
6299 /* To avoid infinite recursion. */
6300 if (die1->die_mark)
6301 return die1->die_mark == die2->die_mark;
6302 die1->die_mark = die2->die_mark = ++(*mark);
6304 if (die1->die_tag != die2->die_tag)
6305 return 0;
6307 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6308 return 0;
6310 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6311 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6312 return 0;
6314 c1 = die1->die_child;
6315 c2 = die2->die_child;
6316 if (! c1)
6318 if (c2)
6319 return 0;
6321 else
6322 for (;;)
6324 if (!same_die_p (c1, c2, mark))
6325 return 0;
6326 c1 = c1->die_sib;
6327 c2 = c2->die_sib;
6328 if (c1 == die1->die_child)
6330 if (c2 == die2->die_child)
6331 break;
6332 else
6333 return 0;
6337 return 1;
6340 /* Do the dies look the same? Wrapper around same_die_p. */
6342 static int
6343 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6345 int mark = 0;
6346 int ret = same_die_p (die1, die2, &mark);
6348 unmark_all_dies (die1);
6349 unmark_all_dies (die2);
6351 return ret;
6354 /* The prefix to attach to symbols on DIEs in the current comdat debug
6355 info section. */
6356 static const char *comdat_symbol_id;
6358 /* The index of the current symbol within the current comdat CU. */
6359 static unsigned int comdat_symbol_number;
6361 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6362 children, and set comdat_symbol_id accordingly. */
6364 static void
6365 compute_section_prefix (dw_die_ref unit_die)
6367 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6368 const char *base = die_name ? lbasename (die_name) : "anonymous";
6369 char *name = XALLOCAVEC (char, strlen (base) + 64);
6370 char *p;
6371 int i, mark;
6372 unsigned char checksum[16];
6373 struct md5_ctx ctx;
6375 /* Compute the checksum of the DIE, then append part of it as hex digits to
6376 the name filename of the unit. */
6378 md5_init_ctx (&ctx);
6379 mark = 0;
6380 die_checksum (unit_die, &ctx, &mark);
6381 unmark_all_dies (unit_die);
6382 md5_finish_ctx (&ctx, checksum);
6384 sprintf (name, "%s.", base);
6385 clean_symbol_name (name);
6387 p = name + strlen (name);
6388 for (i = 0; i < 4; i++)
6390 sprintf (p, "%.2x", checksum[i]);
6391 p += 2;
6394 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6395 comdat_symbol_number = 0;
6398 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6400 static int
6401 is_type_die (dw_die_ref die)
6403 switch (die->die_tag)
6405 case DW_TAG_array_type:
6406 case DW_TAG_class_type:
6407 case DW_TAG_interface_type:
6408 case DW_TAG_enumeration_type:
6409 case DW_TAG_pointer_type:
6410 case DW_TAG_reference_type:
6411 case DW_TAG_rvalue_reference_type:
6412 case DW_TAG_string_type:
6413 case DW_TAG_structure_type:
6414 case DW_TAG_subroutine_type:
6415 case DW_TAG_union_type:
6416 case DW_TAG_ptr_to_member_type:
6417 case DW_TAG_set_type:
6418 case DW_TAG_subrange_type:
6419 case DW_TAG_base_type:
6420 case DW_TAG_const_type:
6421 case DW_TAG_file_type:
6422 case DW_TAG_packed_type:
6423 case DW_TAG_volatile_type:
6424 case DW_TAG_typedef:
6425 return 1;
6426 default:
6427 return 0;
6431 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6432 Basically, we want to choose the bits that are likely to be shared between
6433 compilations (types) and leave out the bits that are specific to individual
6434 compilations (functions). */
6436 static int
6437 is_comdat_die (dw_die_ref c)
6439 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6440 we do for stabs. The advantage is a greater likelihood of sharing between
6441 objects that don't include headers in the same order (and therefore would
6442 put the base types in a different comdat). jason 8/28/00 */
6444 if (c->die_tag == DW_TAG_base_type)
6445 return 0;
6447 if (c->die_tag == DW_TAG_pointer_type
6448 || c->die_tag == DW_TAG_reference_type
6449 || c->die_tag == DW_TAG_rvalue_reference_type
6450 || c->die_tag == DW_TAG_const_type
6451 || c->die_tag == DW_TAG_volatile_type)
6453 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6455 return t ? is_comdat_die (t) : 0;
6458 return is_type_die (c);
6461 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6462 compilation unit. */
6464 static int
6465 is_symbol_die (dw_die_ref c)
6467 return (is_type_die (c)
6468 || is_declaration_die (c)
6469 || c->die_tag == DW_TAG_namespace
6470 || c->die_tag == DW_TAG_module);
6473 /* Returns true iff C is a compile-unit DIE. */
6475 static inline bool
6476 is_cu_die (dw_die_ref c)
6478 return c && c->die_tag == DW_TAG_compile_unit;
6481 /* Returns true iff C is a unit DIE of some sort. */
6483 static inline bool
6484 is_unit_die (dw_die_ref c)
6486 return c && (c->die_tag == DW_TAG_compile_unit
6487 || c->die_tag == DW_TAG_partial_unit
6488 || c->die_tag == DW_TAG_type_unit);
6491 /* Returns true iff C is a namespace DIE. */
6493 static inline bool
6494 is_namespace_die (dw_die_ref c)
6496 return c && c->die_tag == DW_TAG_namespace;
6499 /* Returns true iff C is a class or structure DIE. */
6501 static inline bool
6502 is_class_die (dw_die_ref c)
6504 return c && (c->die_tag == DW_TAG_class_type
6505 || c->die_tag == DW_TAG_structure_type);
6508 static char *
6509 gen_internal_sym (const char *prefix)
6511 char buf[256];
6513 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6514 return xstrdup (buf);
6517 /* Assign symbols to all worthy DIEs under DIE. */
6519 static void
6520 assign_symbol_names (dw_die_ref die)
6522 dw_die_ref c;
6524 if (is_symbol_die (die) && !die->comdat_type_p)
6526 if (comdat_symbol_id)
6528 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6530 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6531 comdat_symbol_id, comdat_symbol_number++);
6532 die->die_id.die_symbol = xstrdup (p);
6534 else
6535 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6538 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6541 struct cu_hash_table_entry
6543 dw_die_ref cu;
6544 unsigned min_comdat_num, max_comdat_num;
6545 struct cu_hash_table_entry *next;
6548 /* Helpers to manipulate hash table of CUs. */
6550 struct cu_hash_table_entry_hasher
6552 typedef cu_hash_table_entry value_type;
6553 typedef die_struct compare_type;
6554 static inline hashval_t hash (const value_type *);
6555 static inline bool equal (const value_type *, const compare_type *);
6556 static inline void remove (value_type *);
6559 inline hashval_t
6560 cu_hash_table_entry_hasher::hash (const value_type *entry)
6562 return htab_hash_string (entry->cu->die_id.die_symbol);
6565 inline bool
6566 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6567 const compare_type *entry2)
6569 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6572 inline void
6573 cu_hash_table_entry_hasher::remove (value_type *entry)
6575 struct cu_hash_table_entry *next;
6577 while (entry)
6579 next = entry->next;
6580 free (entry);
6581 entry = next;
6585 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6587 /* Check whether we have already seen this CU and set up SYM_NUM
6588 accordingly. */
6589 static int
6590 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6592 struct cu_hash_table_entry dummy;
6593 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6595 dummy.max_comdat_num = 0;
6597 slot = htable.find_slot_with_hash (cu,
6598 htab_hash_string (cu->die_id.die_symbol),
6599 INSERT);
6600 entry = *slot;
6602 for (; entry; last = entry, entry = entry->next)
6604 if (same_die_p_wrap (cu, entry->cu))
6605 break;
6608 if (entry)
6610 *sym_num = entry->min_comdat_num;
6611 return 1;
6614 entry = XCNEW (struct cu_hash_table_entry);
6615 entry->cu = cu;
6616 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6617 entry->next = *slot;
6618 *slot = entry;
6620 return 0;
6623 /* Record SYM_NUM to record of CU in HTABLE. */
6624 static void
6625 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6626 unsigned int sym_num)
6628 struct cu_hash_table_entry **slot, *entry;
6630 slot = htable.find_slot_with_hash (cu,
6631 htab_hash_string (cu->die_id.die_symbol),
6632 NO_INSERT);
6633 entry = *slot;
6635 entry->max_comdat_num = sym_num;
6638 /* Traverse the DIE (which is always comp_unit_die), and set up
6639 additional compilation units for each of the include files we see
6640 bracketed by BINCL/EINCL. */
6642 static void
6643 break_out_includes (dw_die_ref die)
6645 dw_die_ref c;
6646 dw_die_ref unit = NULL;
6647 limbo_die_node *node, **pnode;
6648 cu_hash_type cu_hash_table;
6650 c = die->die_child;
6651 if (c) do {
6652 dw_die_ref prev = c;
6653 c = c->die_sib;
6654 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6655 || (unit && is_comdat_die (c)))
6657 dw_die_ref next = c->die_sib;
6659 /* This DIE is for a secondary CU; remove it from the main one. */
6660 remove_child_with_prev (c, prev);
6662 if (c->die_tag == DW_TAG_GNU_BINCL)
6663 unit = push_new_compile_unit (unit, c);
6664 else if (c->die_tag == DW_TAG_GNU_EINCL)
6665 unit = pop_compile_unit (unit);
6666 else
6667 add_child_die (unit, c);
6668 c = next;
6669 if (c == die->die_child)
6670 break;
6672 } while (c != die->die_child);
6674 #if 0
6675 /* We can only use this in debugging, since the frontend doesn't check
6676 to make sure that we leave every include file we enter. */
6677 gcc_assert (!unit);
6678 #endif
6680 assign_symbol_names (die);
6681 cu_hash_table.create (10);
6682 for (node = limbo_die_list, pnode = &limbo_die_list;
6683 node;
6684 node = node->next)
6686 int is_dupl;
6688 compute_section_prefix (node->die);
6689 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6690 &comdat_symbol_number);
6691 assign_symbol_names (node->die);
6692 if (is_dupl)
6693 *pnode = node->next;
6694 else
6696 pnode = &node->next;
6697 record_comdat_symbol_number (node->die, cu_hash_table,
6698 comdat_symbol_number);
6701 cu_hash_table.dispose ();
6704 /* Return non-zero if this DIE is a declaration. */
6706 static int
6707 is_declaration_die (dw_die_ref die)
6709 dw_attr_ref a;
6710 unsigned ix;
6712 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6713 if (a->dw_attr == DW_AT_declaration)
6714 return 1;
6716 return 0;
6719 /* Return non-zero if this DIE is nested inside a subprogram. */
6721 static int
6722 is_nested_in_subprogram (dw_die_ref die)
6724 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6726 if (decl == NULL)
6727 decl = die;
6728 return local_scope_p (decl);
6731 /* Return non-zero if this DIE contains a defining declaration of a
6732 subprogram. */
6734 static int
6735 contains_subprogram_definition (dw_die_ref die)
6737 dw_die_ref c;
6739 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6740 return 1;
6741 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6742 return 0;
6745 /* Return non-zero if this is a type DIE that should be moved to a
6746 COMDAT .debug_types section. */
6748 static int
6749 should_move_die_to_comdat (dw_die_ref die)
6751 switch (die->die_tag)
6753 case DW_TAG_class_type:
6754 case DW_TAG_structure_type:
6755 case DW_TAG_enumeration_type:
6756 case DW_TAG_union_type:
6757 /* Don't move declarations, inlined instances, or types nested in a
6758 subprogram. */
6759 if (is_declaration_die (die)
6760 || get_AT (die, DW_AT_abstract_origin)
6761 || is_nested_in_subprogram (die))
6762 return 0;
6763 /* A type definition should never contain a subprogram definition. */
6764 gcc_assert (!contains_subprogram_definition (die));
6765 return 1;
6766 case DW_TAG_array_type:
6767 case DW_TAG_interface_type:
6768 case DW_TAG_pointer_type:
6769 case DW_TAG_reference_type:
6770 case DW_TAG_rvalue_reference_type:
6771 case DW_TAG_string_type:
6772 case DW_TAG_subroutine_type:
6773 case DW_TAG_ptr_to_member_type:
6774 case DW_TAG_set_type:
6775 case DW_TAG_subrange_type:
6776 case DW_TAG_base_type:
6777 case DW_TAG_const_type:
6778 case DW_TAG_file_type:
6779 case DW_TAG_packed_type:
6780 case DW_TAG_volatile_type:
6781 case DW_TAG_typedef:
6782 default:
6783 return 0;
6787 /* Make a clone of DIE. */
6789 static dw_die_ref
6790 clone_die (dw_die_ref die)
6792 dw_die_ref clone;
6793 dw_attr_ref a;
6794 unsigned ix;
6796 clone = ggc_alloc_cleared_die_node ();
6797 clone->die_tag = die->die_tag;
6799 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6800 add_dwarf_attr (clone, a);
6802 return clone;
6805 /* Make a clone of the tree rooted at DIE. */
6807 static dw_die_ref
6808 clone_tree (dw_die_ref die)
6810 dw_die_ref c;
6811 dw_die_ref clone = clone_die (die);
6813 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6815 return clone;
6818 /* Make a clone of DIE as a declaration. */
6820 static dw_die_ref
6821 clone_as_declaration (dw_die_ref die)
6823 dw_die_ref clone;
6824 dw_die_ref decl;
6825 dw_attr_ref a;
6826 unsigned ix;
6828 /* If the DIE is already a declaration, just clone it. */
6829 if (is_declaration_die (die))
6830 return clone_die (die);
6832 /* If the DIE is a specification, just clone its declaration DIE. */
6833 decl = get_AT_ref (die, DW_AT_specification);
6834 if (decl != NULL)
6836 clone = clone_die (decl);
6837 if (die->comdat_type_p)
6838 add_AT_die_ref (clone, DW_AT_signature, die);
6839 return clone;
6842 clone = ggc_alloc_cleared_die_node ();
6843 clone->die_tag = die->die_tag;
6845 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6847 /* We don't want to copy over all attributes.
6848 For example we don't want DW_AT_byte_size because otherwise we will no
6849 longer have a declaration and GDB will treat it as a definition. */
6851 switch (a->dw_attr)
6853 case DW_AT_artificial:
6854 case DW_AT_containing_type:
6855 case DW_AT_external:
6856 case DW_AT_name:
6857 case DW_AT_type:
6858 case DW_AT_virtuality:
6859 case DW_AT_linkage_name:
6860 case DW_AT_MIPS_linkage_name:
6861 add_dwarf_attr (clone, a);
6862 break;
6863 case DW_AT_byte_size:
6864 default:
6865 break;
6869 if (die->comdat_type_p)
6870 add_AT_die_ref (clone, DW_AT_signature, die);
6872 add_AT_flag (clone, DW_AT_declaration, 1);
6873 return clone;
6877 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6879 struct decl_table_entry
6881 dw_die_ref orig;
6882 dw_die_ref copy;
6885 /* Helpers to manipulate hash table of copied declarations. */
6887 /* Hashtable helpers. */
6889 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6891 typedef decl_table_entry value_type;
6892 typedef die_struct compare_type;
6893 static inline hashval_t hash (const value_type *);
6894 static inline bool equal (const value_type *, const compare_type *);
6897 inline hashval_t
6898 decl_table_entry_hasher::hash (const value_type *entry)
6900 return htab_hash_pointer (entry->orig);
6903 inline bool
6904 decl_table_entry_hasher::equal (const value_type *entry1,
6905 const compare_type *entry2)
6907 return entry1->orig == entry2;
6910 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6912 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6913 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6914 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6915 to check if the ancestor has already been copied into UNIT. */
6917 static dw_die_ref
6918 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6920 dw_die_ref parent = die->die_parent;
6921 dw_die_ref new_parent = unit;
6922 dw_die_ref copy;
6923 decl_table_entry **slot = NULL;
6924 struct decl_table_entry *entry = NULL;
6926 if (decl_table.is_created ())
6928 /* Check if the entry has already been copied to UNIT. */
6929 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
6930 INSERT);
6931 if (*slot != HTAB_EMPTY_ENTRY)
6933 entry = *slot;
6934 return entry->copy;
6937 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
6938 entry = XCNEW (struct decl_table_entry);
6939 entry->orig = die;
6940 entry->copy = NULL;
6941 *slot = entry;
6944 if (parent != NULL)
6946 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
6947 if (spec != NULL)
6948 parent = spec;
6949 if (!is_unit_die (parent))
6950 new_parent = copy_ancestor_tree (unit, parent, decl_table);
6953 copy = clone_as_declaration (die);
6954 add_child_die (new_parent, copy);
6956 if (decl_table.is_created ())
6958 /* Record the pointer to the copy. */
6959 entry->copy = copy;
6962 return copy;
6964 /* Copy the declaration context to the new type unit DIE. This includes
6965 any surrounding namespace or type declarations. If the DIE has an
6966 AT_specification attribute, it also includes attributes and children
6967 attached to the specification, and returns a pointer to the original
6968 parent of the declaration DIE. Returns NULL otherwise. */
6970 static dw_die_ref
6971 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6973 dw_die_ref decl;
6974 dw_die_ref new_decl;
6975 dw_die_ref orig_parent = NULL;
6977 decl = get_AT_ref (die, DW_AT_specification);
6978 if (decl == NULL)
6979 decl = die;
6980 else
6982 unsigned ix;
6983 dw_die_ref c;
6984 dw_attr_ref a;
6986 /* The original DIE will be changed to a declaration, and must
6987 be moved to be a child of the original declaration DIE. */
6988 orig_parent = decl->die_parent;
6990 /* Copy the type node pointer from the new DIE to the original
6991 declaration DIE so we can forward references later. */
6992 decl->comdat_type_p = true;
6993 decl->die_id.die_type_node = die->die_id.die_type_node;
6995 remove_AT (die, DW_AT_specification);
6997 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
6999 if (a->dw_attr != DW_AT_name
7000 && a->dw_attr != DW_AT_declaration
7001 && a->dw_attr != DW_AT_external)
7002 add_dwarf_attr (die, a);
7005 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7008 if (decl->die_parent != NULL
7009 && !is_unit_die (decl->die_parent))
7011 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7012 if (new_decl != NULL)
7014 remove_AT (new_decl, DW_AT_signature);
7015 add_AT_specification (die, new_decl);
7019 return orig_parent;
7022 /* Generate the skeleton ancestor tree for the given NODE, then clone
7023 the DIE and add the clone into the tree. */
7025 static void
7026 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7028 if (node->new_die != NULL)
7029 return;
7031 node->new_die = clone_as_declaration (node->old_die);
7033 if (node->parent != NULL)
7035 generate_skeleton_ancestor_tree (node->parent);
7036 add_child_die (node->parent->new_die, node->new_die);
7040 /* Generate a skeleton tree of DIEs containing any declarations that are
7041 found in the original tree. We traverse the tree looking for declaration
7042 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7044 static void
7045 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7047 skeleton_chain_node node;
7048 dw_die_ref c;
7049 dw_die_ref first;
7050 dw_die_ref prev = NULL;
7051 dw_die_ref next = NULL;
7053 node.parent = parent;
7055 first = c = parent->old_die->die_child;
7056 if (c)
7057 next = c->die_sib;
7058 if (c) do {
7059 if (prev == NULL || prev->die_sib == c)
7060 prev = c;
7061 c = next;
7062 next = (c == first ? NULL : c->die_sib);
7063 node.old_die = c;
7064 node.new_die = NULL;
7065 if (is_declaration_die (c))
7067 /* Clone the existing DIE, move the original to the skeleton
7068 tree (which is in the main CU), and put the clone, with
7069 all the original's children, where the original came from. */
7070 dw_die_ref clone = clone_die (c);
7071 move_all_children (c, clone);
7073 replace_child (c, clone, prev);
7074 generate_skeleton_ancestor_tree (parent);
7075 add_child_die (parent->new_die, c);
7076 node.new_die = c;
7077 c = clone;
7079 generate_skeleton_bottom_up (&node);
7080 } while (next != NULL);
7083 /* Wrapper function for generate_skeleton_bottom_up. */
7085 static dw_die_ref
7086 generate_skeleton (dw_die_ref die)
7088 skeleton_chain_node node;
7090 node.old_die = die;
7091 node.new_die = NULL;
7092 node.parent = NULL;
7094 /* If this type definition is nested inside another type,
7095 always leave at least a declaration in its place. */
7096 if (die->die_parent != NULL && is_type_die (die->die_parent))
7097 node.new_die = clone_as_declaration (die);
7099 generate_skeleton_bottom_up (&node);
7100 return node.new_die;
7103 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7104 declaration. The original DIE is moved to a new compile unit so that
7105 existing references to it follow it to the new location. If any of the
7106 original DIE's descendants is a declaration, we need to replace the
7107 original DIE with a skeleton tree and move the declarations back into the
7108 skeleton tree. */
7110 static dw_die_ref
7111 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7112 dw_die_ref prev)
7114 dw_die_ref skeleton, orig_parent;
7116 /* Copy the declaration context to the type unit DIE. If the returned
7117 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7118 that DIE. */
7119 orig_parent = copy_declaration_context (unit, child);
7121 skeleton = generate_skeleton (child);
7122 if (skeleton == NULL)
7123 remove_child_with_prev (child, prev);
7124 else
7126 skeleton->comdat_type_p = true;
7127 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7129 /* If the original DIE was a specification, we need to put
7130 the skeleton under the parent DIE of the declaration.
7131 This leaves the original declaration in the tree, but
7132 it will be pruned later since there are no longer any
7133 references to it. */
7134 if (orig_parent != NULL)
7136 remove_child_with_prev (child, prev);
7137 add_child_die (orig_parent, skeleton);
7139 else
7140 replace_child (child, skeleton, prev);
7143 return skeleton;
7146 /* Traverse the DIE and set up additional .debug_types sections for each
7147 type worthy of being placed in a COMDAT section. */
7149 static void
7150 break_out_comdat_types (dw_die_ref die)
7152 dw_die_ref c;
7153 dw_die_ref first;
7154 dw_die_ref prev = NULL;
7155 dw_die_ref next = NULL;
7156 dw_die_ref unit = NULL;
7158 first = c = die->die_child;
7159 if (c)
7160 next = c->die_sib;
7161 if (c) do {
7162 if (prev == NULL || prev->die_sib == c)
7163 prev = c;
7164 c = next;
7165 next = (c == first ? NULL : c->die_sib);
7166 if (should_move_die_to_comdat (c))
7168 dw_die_ref replacement;
7169 comdat_type_node_ref type_node;
7171 /* Create a new type unit DIE as the root for the new tree, and
7172 add it to the list of comdat types. */
7173 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7174 add_AT_unsigned (unit, DW_AT_language,
7175 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7176 type_node = ggc_alloc_cleared_comdat_type_node ();
7177 type_node->root_die = unit;
7178 type_node->next = comdat_type_list;
7179 comdat_type_list = type_node;
7181 /* Generate the type signature. */
7182 generate_type_signature (c, type_node);
7184 /* Copy the declaration context, attributes, and children of the
7185 declaration into the new type unit DIE, then remove this DIE
7186 from the main CU (or replace it with a skeleton if necessary). */
7187 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7188 type_node->skeleton_die = replacement;
7190 /* Break out nested types into their own type units. */
7191 break_out_comdat_types (c);
7193 /* Add the DIE to the new compunit. */
7194 add_child_die (unit, c);
7196 if (replacement != NULL)
7197 c = replacement;
7199 else if (c->die_tag == DW_TAG_namespace
7200 || c->die_tag == DW_TAG_class_type
7201 || c->die_tag == DW_TAG_structure_type
7202 || c->die_tag == DW_TAG_union_type)
7204 /* Look for nested types that can be broken out. */
7205 break_out_comdat_types (c);
7207 } while (next != NULL);
7210 /* Like clone_tree, but additionally enter all the children into
7211 the hash table decl_table. */
7213 static dw_die_ref
7214 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7216 dw_die_ref c;
7217 dw_die_ref clone = clone_die (die);
7218 struct decl_table_entry *entry;
7219 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7220 htab_hash_pointer (die), INSERT);
7221 /* Assert that DIE isn't in the hash table yet. If it would be there
7222 before, the ancestors would be necessarily there as well, therefore
7223 clone_tree_hash wouldn't be called. */
7224 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7225 entry = XCNEW (struct decl_table_entry);
7226 entry->orig = die;
7227 entry->copy = clone;
7228 *slot = entry;
7230 FOR_EACH_CHILD (die, c,
7231 add_child_die (clone, clone_tree_hash (c, decl_table)));
7233 return clone;
7236 /* Walk the DIE and its children, looking for references to incomplete
7237 or trivial types that are unmarked (i.e., that are not in the current
7238 type_unit). */
7240 static void
7241 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7243 dw_die_ref c;
7244 dw_attr_ref a;
7245 unsigned ix;
7247 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7249 if (AT_class (a) == dw_val_class_die_ref)
7251 dw_die_ref targ = AT_ref (a);
7252 decl_table_entry **slot;
7253 struct decl_table_entry *entry;
7255 if (targ->die_mark != 0 || targ->comdat_type_p)
7256 continue;
7258 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7259 INSERT);
7261 if (*slot != HTAB_EMPTY_ENTRY)
7263 /* TARG has already been copied, so we just need to
7264 modify the reference to point to the copy. */
7265 entry = *slot;
7266 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7268 else
7270 dw_die_ref parent = unit;
7271 dw_die_ref copy = clone_die (targ);
7273 /* Record in DECL_TABLE that TARG has been copied.
7274 Need to do this now, before the recursive call,
7275 because DECL_TABLE may be expanded and SLOT
7276 would no longer be a valid pointer. */
7277 entry = XCNEW (struct decl_table_entry);
7278 entry->orig = targ;
7279 entry->copy = copy;
7280 *slot = entry;
7282 FOR_EACH_CHILD (targ, c,
7283 add_child_die (copy,
7284 clone_tree_hash (c, decl_table)));
7286 /* Make sure the cloned tree is marked as part of the
7287 type unit. */
7288 mark_dies (copy);
7290 /* If TARG has surrounding context, copy its ancestor tree
7291 into the new type unit. */
7292 if (targ->die_parent != NULL
7293 && !is_unit_die (targ->die_parent))
7294 parent = copy_ancestor_tree (unit, targ->die_parent,
7295 decl_table);
7297 add_child_die (parent, copy);
7298 a->dw_attr_val.v.val_die_ref.die = copy;
7300 /* Make sure the newly-copied DIE is walked. If it was
7301 installed in a previously-added context, it won't
7302 get visited otherwise. */
7303 if (parent != unit)
7305 /* Find the highest point of the newly-added tree,
7306 mark each node along the way, and walk from there. */
7307 parent->die_mark = 1;
7308 while (parent->die_parent
7309 && parent->die_parent->die_mark == 0)
7311 parent = parent->die_parent;
7312 parent->die_mark = 1;
7314 copy_decls_walk (unit, parent, decl_table);
7320 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7323 /* Copy declarations for "unworthy" types into the new comdat section.
7324 Incomplete types, modified types, and certain other types aren't broken
7325 out into comdat sections of their own, so they don't have a signature,
7326 and we need to copy the declaration into the same section so that we
7327 don't have an external reference. */
7329 static void
7330 copy_decls_for_unworthy_types (dw_die_ref unit)
7332 decl_hash_type decl_table;
7334 mark_dies (unit);
7335 decl_table.create (10);
7336 copy_decls_walk (unit, unit, decl_table);
7337 decl_table.dispose ();
7338 unmark_dies (unit);
7341 /* Traverse the DIE and add a sibling attribute if it may have the
7342 effect of speeding up access to siblings. To save some space,
7343 avoid generating sibling attributes for DIE's without children. */
7345 static void
7346 add_sibling_attributes (dw_die_ref die)
7348 dw_die_ref c;
7350 if (! die->die_child)
7351 return;
7353 if (die->die_parent && die != die->die_parent->die_child)
7354 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7356 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7359 /* Output all location lists for the DIE and its children. */
7361 static void
7362 output_location_lists (dw_die_ref die)
7364 dw_die_ref c;
7365 dw_attr_ref a;
7366 unsigned ix;
7368 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7369 if (AT_class (a) == dw_val_class_loc_list)
7370 output_loc_list (AT_loc_list (a));
7372 FOR_EACH_CHILD (die, c, output_location_lists (c));
7375 /* We want to limit the number of external references, because they are
7376 larger than local references: a relocation takes multiple words, and
7377 even a sig8 reference is always eight bytes, whereas a local reference
7378 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7379 So if we encounter multiple external references to the same type DIE, we
7380 make a local typedef stub for it and redirect all references there.
7382 This is the element of the hash table for keeping track of these
7383 references. */
7385 struct external_ref
7387 dw_die_ref type;
7388 dw_die_ref stub;
7389 unsigned n_refs;
7392 /* Hashtable helpers. */
7394 struct external_ref_hasher : typed_free_remove <external_ref>
7396 typedef external_ref value_type;
7397 typedef external_ref compare_type;
7398 static inline hashval_t hash (const value_type *);
7399 static inline bool equal (const value_type *, const compare_type *);
7402 inline hashval_t
7403 external_ref_hasher::hash (const value_type *r)
7405 dw_die_ref die = r->type;
7406 hashval_t h = 0;
7408 /* We can't use the address of the DIE for hashing, because
7409 that will make the order of the stub DIEs non-deterministic. */
7410 if (! die->comdat_type_p)
7411 /* We have a symbol; use it to compute a hash. */
7412 h = htab_hash_string (die->die_id.die_symbol);
7413 else
7415 /* We have a type signature; use a subset of the bits as the hash.
7416 The 8-byte signature is at least as large as hashval_t. */
7417 comdat_type_node_ref type_node = die->die_id.die_type_node;
7418 memcpy (&h, type_node->signature, sizeof (h));
7420 return h;
7423 inline bool
7424 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7426 return r1->type == r2->type;
7429 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7431 /* Return a pointer to the external_ref for references to DIE. */
7433 static struct external_ref *
7434 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7436 struct external_ref ref, *ref_p;
7437 external_ref **slot;
7439 ref.type = die;
7440 slot = map.find_slot (&ref, INSERT);
7441 if (*slot != HTAB_EMPTY_ENTRY)
7442 return *slot;
7444 ref_p = XCNEW (struct external_ref);
7445 ref_p->type = die;
7446 *slot = ref_p;
7447 return ref_p;
7450 /* Subroutine of optimize_external_refs, below.
7452 If we see a type skeleton, record it as our stub. If we see external
7453 references, remember how many we've seen. */
7455 static void
7456 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7458 dw_die_ref c;
7459 dw_attr_ref a;
7460 unsigned ix;
7461 struct external_ref *ref_p;
7463 if (is_type_die (die)
7464 && (c = get_AT_ref (die, DW_AT_signature)))
7466 /* This is a local skeleton; use it for local references. */
7467 ref_p = lookup_external_ref (map, c);
7468 ref_p->stub = die;
7471 /* Scan the DIE references, and remember any that refer to DIEs from
7472 other CUs (i.e. those which are not marked). */
7473 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7474 if (AT_class (a) == dw_val_class_die_ref
7475 && (c = AT_ref (a))->die_mark == 0
7476 && is_type_die (c))
7478 ref_p = lookup_external_ref (map, c);
7479 ref_p->n_refs++;
7482 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7485 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7486 points to an external_ref, DATA is the CU we're processing. If we don't
7487 already have a local stub, and we have multiple refs, build a stub. */
7490 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7492 struct external_ref *ref_p = *slot;
7494 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7496 /* We have multiple references to this type, so build a small stub.
7497 Both of these forms are a bit dodgy from the perspective of the
7498 DWARF standard, since technically they should have names. */
7499 dw_die_ref cu = data;
7500 dw_die_ref type = ref_p->type;
7501 dw_die_ref stub = NULL;
7503 if (type->comdat_type_p)
7505 /* If we refer to this type via sig8, use AT_signature. */
7506 stub = new_die (type->die_tag, cu, NULL_TREE);
7507 add_AT_die_ref (stub, DW_AT_signature, type);
7509 else
7511 /* Otherwise, use a typedef with no name. */
7512 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7513 add_AT_die_ref (stub, DW_AT_type, type);
7516 stub->die_mark++;
7517 ref_p->stub = stub;
7519 return 1;
7522 /* DIE is a unit; look through all the DIE references to see if there are
7523 any external references to types, and if so, create local stubs for
7524 them which will be applied in build_abbrev_table. This is useful because
7525 references to local DIEs are smaller. */
7527 static external_ref_hash_type
7528 optimize_external_refs (dw_die_ref die)
7530 external_ref_hash_type map;
7531 map.create (10);
7532 optimize_external_refs_1 (die, map);
7533 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7534 return map;
7537 /* The format of each DIE (and its attribute value pairs) is encoded in an
7538 abbreviation table. This routine builds the abbreviation table and assigns
7539 a unique abbreviation id for each abbreviation entry. The children of each
7540 die are visited recursively. */
7542 static void
7543 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7545 unsigned long abbrev_id;
7546 unsigned int n_alloc;
7547 dw_die_ref c;
7548 dw_attr_ref a;
7549 unsigned ix;
7551 /* Scan the DIE references, and replace any that refer to
7552 DIEs from other CUs (i.e. those which are not marked) with
7553 the local stubs we built in optimize_external_refs. */
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)
7558 struct external_ref *ref_p;
7559 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7561 ref_p = lookup_external_ref (extern_map, c);
7562 if (ref_p->stub && ref_p->stub != die)
7563 change_AT_die_ref (a, ref_p->stub);
7564 else
7565 /* We aren't changing this reference, so mark it external. */
7566 set_AT_ref_external (a, 1);
7569 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7571 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7572 dw_attr_ref die_a, abbrev_a;
7573 unsigned ix;
7574 bool ok = true;
7576 if (abbrev->die_tag != die->die_tag)
7577 continue;
7578 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7579 continue;
7581 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7582 continue;
7584 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7586 abbrev_a = &(*abbrev->die_attr)[ix];
7587 if ((abbrev_a->dw_attr != die_a->dw_attr)
7588 || (value_format (abbrev_a) != value_format (die_a)))
7590 ok = false;
7591 break;
7594 if (ok)
7595 break;
7598 if (abbrev_id >= abbrev_die_table_in_use)
7600 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7602 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7603 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7604 n_alloc);
7606 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7607 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7608 abbrev_die_table_allocated = n_alloc;
7611 ++abbrev_die_table_in_use;
7612 abbrev_die_table[abbrev_id] = die;
7615 die->die_abbrev = abbrev_id;
7616 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7619 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7621 static int
7622 constant_size (unsigned HOST_WIDE_INT value)
7624 int log;
7626 if (value == 0)
7627 log = 0;
7628 else
7629 log = floor_log2 (value);
7631 log = log / 8;
7632 log = 1 << (floor_log2 (log) + 1);
7634 return log;
7637 /* Return the size of a DIE as it is represented in the
7638 .debug_info section. */
7640 static unsigned long
7641 size_of_die (dw_die_ref die)
7643 unsigned long size = 0;
7644 dw_attr_ref a;
7645 unsigned ix;
7646 enum dwarf_form form;
7648 size += size_of_uleb128 (die->die_abbrev);
7649 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7651 switch (AT_class (a))
7653 case dw_val_class_addr:
7654 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7656 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7657 size += size_of_uleb128 (AT_index (a));
7659 else
7660 size += DWARF2_ADDR_SIZE;
7661 break;
7662 case dw_val_class_offset:
7663 size += DWARF_OFFSET_SIZE;
7664 break;
7665 case dw_val_class_loc:
7667 unsigned long lsize = size_of_locs (AT_loc (a));
7669 /* Block length. */
7670 if (dwarf_version >= 4)
7671 size += size_of_uleb128 (lsize);
7672 else
7673 size += constant_size (lsize);
7674 size += lsize;
7676 break;
7677 case dw_val_class_loc_list:
7678 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7680 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7681 size += size_of_uleb128 (AT_index (a));
7683 else
7684 size += DWARF_OFFSET_SIZE;
7685 break;
7686 case dw_val_class_range_list:
7687 size += DWARF_OFFSET_SIZE;
7688 break;
7689 case dw_val_class_const:
7690 size += size_of_sleb128 (AT_int (a));
7691 break;
7692 case dw_val_class_unsigned_const:
7694 int csize = constant_size (AT_unsigned (a));
7695 if (dwarf_version == 3
7696 && a->dw_attr == DW_AT_data_member_location
7697 && csize >= 4)
7698 size += size_of_uleb128 (AT_unsigned (a));
7699 else
7700 size += csize;
7702 break;
7703 case dw_val_class_const_double:
7704 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7705 if (HOST_BITS_PER_WIDE_INT >= 64)
7706 size++; /* block */
7707 break;
7708 case dw_val_class_vec:
7709 size += constant_size (a->dw_attr_val.v.val_vec.length
7710 * a->dw_attr_val.v.val_vec.elt_size)
7711 + a->dw_attr_val.v.val_vec.length
7712 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7713 break;
7714 case dw_val_class_flag:
7715 if (dwarf_version >= 4)
7716 /* Currently all add_AT_flag calls pass in 1 as last argument,
7717 so DW_FORM_flag_present can be used. If that ever changes,
7718 we'll need to use DW_FORM_flag and have some optimization
7719 in build_abbrev_table that will change those to
7720 DW_FORM_flag_present if it is set to 1 in all DIEs using
7721 the same abbrev entry. */
7722 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7723 else
7724 size += 1;
7725 break;
7726 case dw_val_class_die_ref:
7727 if (AT_ref_external (a))
7729 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7730 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7731 is sized by target address length, whereas in DWARF3
7732 it's always sized as an offset. */
7733 if (use_debug_types)
7734 size += DWARF_TYPE_SIGNATURE_SIZE;
7735 else if (dwarf_version == 2)
7736 size += DWARF2_ADDR_SIZE;
7737 else
7738 size += DWARF_OFFSET_SIZE;
7740 else
7741 size += DWARF_OFFSET_SIZE;
7742 break;
7743 case dw_val_class_fde_ref:
7744 size += DWARF_OFFSET_SIZE;
7745 break;
7746 case dw_val_class_lbl_id:
7747 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7749 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7750 size += size_of_uleb128 (AT_index (a));
7752 else
7753 size += DWARF2_ADDR_SIZE;
7754 break;
7755 case dw_val_class_lineptr:
7756 case dw_val_class_macptr:
7757 size += DWARF_OFFSET_SIZE;
7758 break;
7759 case dw_val_class_str:
7760 form = AT_string_form (a);
7761 if (form == DW_FORM_strp)
7762 size += DWARF_OFFSET_SIZE;
7763 else if (form == DW_FORM_GNU_str_index)
7764 size += size_of_uleb128 (AT_index (a));
7765 else
7766 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7767 break;
7768 case dw_val_class_file:
7769 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7770 break;
7771 case dw_val_class_data8:
7772 size += 8;
7773 break;
7774 case dw_val_class_vms_delta:
7775 size += DWARF_OFFSET_SIZE;
7776 break;
7777 case dw_val_class_high_pc:
7778 size += DWARF2_ADDR_SIZE;
7779 break;
7780 default:
7781 gcc_unreachable ();
7785 return size;
7788 /* Size the debugging information associated with a given DIE. Visits the
7789 DIE's children recursively. Updates the global variable next_die_offset, on
7790 each time through. Uses the current value of next_die_offset to update the
7791 die_offset field in each DIE. */
7793 static void
7794 calc_die_sizes (dw_die_ref die)
7796 dw_die_ref c;
7798 gcc_assert (die->die_offset == 0
7799 || (unsigned long int) die->die_offset == next_die_offset);
7800 die->die_offset = next_die_offset;
7801 next_die_offset += size_of_die (die);
7803 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7805 if (die->die_child != NULL)
7806 /* Count the null byte used to terminate sibling lists. */
7807 next_die_offset += 1;
7810 /* Size just the base type children at the start of the CU.
7811 This is needed because build_abbrev needs to size locs
7812 and sizing of type based stack ops needs to know die_offset
7813 values for the base types. */
7815 static void
7816 calc_base_type_die_sizes (void)
7818 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7819 unsigned int i;
7820 dw_die_ref base_type;
7821 #if ENABLE_ASSERT_CHECKING
7822 dw_die_ref prev = comp_unit_die ()->die_child;
7823 #endif
7825 die_offset += size_of_die (comp_unit_die ());
7826 for (i = 0; base_types.iterate (i, &base_type); i++)
7828 #if ENABLE_ASSERT_CHECKING
7829 gcc_assert (base_type->die_offset == 0
7830 && prev->die_sib == base_type
7831 && base_type->die_child == NULL
7832 && base_type->die_abbrev);
7833 prev = base_type;
7834 #endif
7835 base_type->die_offset = die_offset;
7836 die_offset += size_of_die (base_type);
7840 /* Set the marks for a die and its children. We do this so
7841 that we know whether or not a reference needs to use FORM_ref_addr; only
7842 DIEs in the same CU will be marked. We used to clear out the offset
7843 and use that as the flag, but ran into ordering problems. */
7845 static void
7846 mark_dies (dw_die_ref die)
7848 dw_die_ref c;
7850 gcc_assert (!die->die_mark);
7852 die->die_mark = 1;
7853 FOR_EACH_CHILD (die, c, mark_dies (c));
7856 /* Clear the marks for a die and its children. */
7858 static void
7859 unmark_dies (dw_die_ref die)
7861 dw_die_ref c;
7863 if (! use_debug_types)
7864 gcc_assert (die->die_mark);
7866 die->die_mark = 0;
7867 FOR_EACH_CHILD (die, c, unmark_dies (c));
7870 /* Clear the marks for a die, its children and referred dies. */
7872 static void
7873 unmark_all_dies (dw_die_ref die)
7875 dw_die_ref c;
7876 dw_attr_ref a;
7877 unsigned ix;
7879 if (!die->die_mark)
7880 return;
7881 die->die_mark = 0;
7883 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7885 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7886 if (AT_class (a) == dw_val_class_die_ref)
7887 unmark_all_dies (AT_ref (a));
7890 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7891 generated for the compilation unit. */
7893 static unsigned long
7894 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7896 unsigned long size;
7897 unsigned i;
7898 pubname_ref p;
7900 size = DWARF_PUBNAMES_HEADER_SIZE;
7901 FOR_EACH_VEC_ELT (*names, i, p)
7902 if (names != pubtype_table
7903 || p->die->die_offset != 0
7904 || !flag_eliminate_unused_debug_types)
7905 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7907 size += DWARF_OFFSET_SIZE;
7908 return size;
7911 /* Return the size of the information in the .debug_aranges section. */
7913 static unsigned long
7914 size_of_aranges (void)
7916 unsigned long size;
7918 size = DWARF_ARANGES_HEADER_SIZE;
7920 /* Count the address/length pair for this compilation unit. */
7921 if (text_section_used)
7922 size += 2 * DWARF2_ADDR_SIZE;
7923 if (cold_text_section_used)
7924 size += 2 * DWARF2_ADDR_SIZE;
7925 if (have_multiple_function_sections)
7927 unsigned fde_idx;
7928 dw_fde_ref fde;
7930 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7932 if (DECL_IGNORED_P (fde->decl))
7933 continue;
7934 if (!fde->in_std_section)
7935 size += 2 * DWARF2_ADDR_SIZE;
7936 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7937 size += 2 * DWARF2_ADDR_SIZE;
7941 /* Count the two zero words used to terminated the address range table. */
7942 size += 2 * DWARF2_ADDR_SIZE;
7943 return size;
7946 /* Select the encoding of an attribute value. */
7948 static enum dwarf_form
7949 value_format (dw_attr_ref a)
7951 switch (AT_class (a))
7953 case dw_val_class_addr:
7954 /* Only very few attributes allow DW_FORM_addr. */
7955 switch (a->dw_attr)
7957 case DW_AT_low_pc:
7958 case DW_AT_high_pc:
7959 case DW_AT_entry_pc:
7960 case DW_AT_trampoline:
7961 return (AT_index (a) == NOT_INDEXED
7962 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7963 default:
7964 break;
7966 switch (DWARF2_ADDR_SIZE)
7968 case 1:
7969 return DW_FORM_data1;
7970 case 2:
7971 return DW_FORM_data2;
7972 case 4:
7973 return DW_FORM_data4;
7974 case 8:
7975 return DW_FORM_data8;
7976 default:
7977 gcc_unreachable ();
7979 case dw_val_class_range_list:
7980 case dw_val_class_loc_list:
7981 if (dwarf_version >= 4)
7982 return DW_FORM_sec_offset;
7983 /* FALLTHRU */
7984 case dw_val_class_vms_delta:
7985 case dw_val_class_offset:
7986 switch (DWARF_OFFSET_SIZE)
7988 case 4:
7989 return DW_FORM_data4;
7990 case 8:
7991 return DW_FORM_data8;
7992 default:
7993 gcc_unreachable ();
7995 case dw_val_class_loc:
7996 if (dwarf_version >= 4)
7997 return DW_FORM_exprloc;
7998 switch (constant_size (size_of_locs (AT_loc (a))))
8000 case 1:
8001 return DW_FORM_block1;
8002 case 2:
8003 return DW_FORM_block2;
8004 case 4:
8005 return DW_FORM_block4;
8006 default:
8007 gcc_unreachable ();
8009 case dw_val_class_const:
8010 return DW_FORM_sdata;
8011 case dw_val_class_unsigned_const:
8012 switch (constant_size (AT_unsigned (a)))
8014 case 1:
8015 return DW_FORM_data1;
8016 case 2:
8017 return DW_FORM_data2;
8018 case 4:
8019 /* In DWARF3 DW_AT_data_member_location with
8020 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8021 constant, so we need to use DW_FORM_udata if we need
8022 a large constant. */
8023 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8024 return DW_FORM_udata;
8025 return DW_FORM_data4;
8026 case 8:
8027 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8028 return DW_FORM_udata;
8029 return DW_FORM_data8;
8030 default:
8031 gcc_unreachable ();
8033 case dw_val_class_const_double:
8034 switch (HOST_BITS_PER_WIDE_INT)
8036 case 8:
8037 return DW_FORM_data2;
8038 case 16:
8039 return DW_FORM_data4;
8040 case 32:
8041 return DW_FORM_data8;
8042 case 64:
8043 default:
8044 return DW_FORM_block1;
8046 case dw_val_class_vec:
8047 switch (constant_size (a->dw_attr_val.v.val_vec.length
8048 * a->dw_attr_val.v.val_vec.elt_size))
8050 case 1:
8051 return DW_FORM_block1;
8052 case 2:
8053 return DW_FORM_block2;
8054 case 4:
8055 return DW_FORM_block4;
8056 default:
8057 gcc_unreachable ();
8059 case dw_val_class_flag:
8060 if (dwarf_version >= 4)
8062 /* Currently all add_AT_flag calls pass in 1 as last argument,
8063 so DW_FORM_flag_present can be used. If that ever changes,
8064 we'll need to use DW_FORM_flag and have some optimization
8065 in build_abbrev_table that will change those to
8066 DW_FORM_flag_present if it is set to 1 in all DIEs using
8067 the same abbrev entry. */
8068 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8069 return DW_FORM_flag_present;
8071 return DW_FORM_flag;
8072 case dw_val_class_die_ref:
8073 if (AT_ref_external (a))
8074 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8075 else
8076 return DW_FORM_ref;
8077 case dw_val_class_fde_ref:
8078 return DW_FORM_data;
8079 case dw_val_class_lbl_id:
8080 return (AT_index (a) == NOT_INDEXED
8081 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8082 case dw_val_class_lineptr:
8083 case dw_val_class_macptr:
8084 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8085 case dw_val_class_str:
8086 return AT_string_form (a);
8087 case dw_val_class_file:
8088 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8090 case 1:
8091 return DW_FORM_data1;
8092 case 2:
8093 return DW_FORM_data2;
8094 case 4:
8095 return DW_FORM_data4;
8096 default:
8097 gcc_unreachable ();
8100 case dw_val_class_data8:
8101 return DW_FORM_data8;
8103 case dw_val_class_high_pc:
8104 switch (DWARF2_ADDR_SIZE)
8106 case 1:
8107 return DW_FORM_data1;
8108 case 2:
8109 return DW_FORM_data2;
8110 case 4:
8111 return DW_FORM_data4;
8112 case 8:
8113 return DW_FORM_data8;
8114 default:
8115 gcc_unreachable ();
8118 default:
8119 gcc_unreachable ();
8123 /* Output the encoding of an attribute value. */
8125 static void
8126 output_value_format (dw_attr_ref a)
8128 enum dwarf_form form = value_format (a);
8130 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8133 /* Given a die and id, produce the appropriate abbreviations. */
8135 static void
8136 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8138 unsigned ix;
8139 dw_attr_ref a_attr;
8141 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8142 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8143 dwarf_tag_name (abbrev->die_tag));
8145 if (abbrev->die_child != NULL)
8146 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8147 else
8148 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8150 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8152 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8153 dwarf_attr_name (a_attr->dw_attr));
8154 output_value_format (a_attr);
8157 dw2_asm_output_data (1, 0, NULL);
8158 dw2_asm_output_data (1, 0, NULL);
8162 /* Output the .debug_abbrev section which defines the DIE abbreviation
8163 table. */
8165 static void
8166 output_abbrev_section (void)
8168 unsigned long abbrev_id;
8170 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8171 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8173 /* Terminate the table. */
8174 dw2_asm_output_data (1, 0, NULL);
8177 /* Output a symbol we can use to refer to this DIE from another CU. */
8179 static inline void
8180 output_die_symbol (dw_die_ref die)
8182 const char *sym = die->die_id.die_symbol;
8184 gcc_assert (!die->comdat_type_p);
8186 if (sym == 0)
8187 return;
8189 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8190 /* We make these global, not weak; if the target doesn't support
8191 .linkonce, it doesn't support combining the sections, so debugging
8192 will break. */
8193 targetm.asm_out.globalize_label (asm_out_file, sym);
8195 ASM_OUTPUT_LABEL (asm_out_file, sym);
8198 /* Return a new location list, given the begin and end range, and the
8199 expression. */
8201 static inline dw_loc_list_ref
8202 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8203 const char *section)
8205 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8207 retlist->begin = begin;
8208 retlist->begin_entry = NULL;
8209 retlist->end = end;
8210 retlist->expr = expr;
8211 retlist->section = section;
8213 return retlist;
8216 /* Generate a new internal symbol for this location list node, if it
8217 hasn't got one yet. */
8219 static inline void
8220 gen_llsym (dw_loc_list_ref list)
8222 gcc_assert (!list->ll_symbol);
8223 list->ll_symbol = gen_internal_sym ("LLST");
8226 /* Output the location list given to us. */
8228 static void
8229 output_loc_list (dw_loc_list_ref list_head)
8231 dw_loc_list_ref curr = list_head;
8233 if (list_head->emitted)
8234 return;
8235 list_head->emitted = true;
8237 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8239 /* Walk the location list, and output each range + expression. */
8240 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8242 unsigned long size;
8243 /* Don't output an entry that starts and ends at the same address. */
8244 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8245 continue;
8246 size = size_of_locs (curr->expr);
8247 /* If the expression is too large, drop it on the floor. We could
8248 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8249 in the expression, but >= 64KB expressions for a single value
8250 in a single range are unlikely very useful. */
8251 if (size > 0xffff)
8252 continue;
8253 if (dwarf_split_debug_info)
8255 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8256 "Location list start/length entry (%s)",
8257 list_head->ll_symbol);
8258 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8259 "Location list range start index (%s)",
8260 curr->begin);
8261 /* The length field is 4 bytes. If we ever need to support
8262 an 8-byte length, we can add a new DW_LLE code or fall back
8263 to DW_LLE_GNU_start_end_entry. */
8264 dw2_asm_output_delta (4, curr->end, curr->begin,
8265 "Location list range length (%s)",
8266 list_head->ll_symbol);
8268 else if (!have_multiple_function_sections)
8270 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8271 "Location list begin address (%s)",
8272 list_head->ll_symbol);
8273 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8274 "Location list end address (%s)",
8275 list_head->ll_symbol);
8277 else
8279 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8280 "Location list begin address (%s)",
8281 list_head->ll_symbol);
8282 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8283 "Location list end address (%s)",
8284 list_head->ll_symbol);
8287 /* Output the block length for this list of location operations. */
8288 gcc_assert (size <= 0xffff);
8289 dw2_asm_output_data (2, size, "%s", "Location expression size");
8291 output_loc_sequence (curr->expr, -1);
8294 if (dwarf_split_debug_info)
8295 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8296 "Location list terminator (%s)",
8297 list_head->ll_symbol);
8298 else
8300 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8301 "Location list terminator begin (%s)",
8302 list_head->ll_symbol);
8303 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8304 "Location list terminator end (%s)",
8305 list_head->ll_symbol);
8309 /* Output a range_list offset into the debug_range section. Emit a
8310 relocated reference if val_entry is NULL, otherwise, emit an
8311 indirect reference. */
8313 static void
8314 output_range_list_offset (dw_attr_ref a)
8316 const char *name = dwarf_attr_name (a->dw_attr);
8318 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8320 char *p = strchr (ranges_section_label, '\0');
8321 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8322 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8323 debug_ranges_section, "%s", name);
8324 *p = '\0';
8326 else
8327 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8328 "%s (offset from %s)", name, ranges_section_label);
8331 /* Output the offset into the debug_loc section. */
8333 static void
8334 output_loc_list_offset (dw_attr_ref a)
8336 char *sym = AT_loc_list (a)->ll_symbol;
8338 gcc_assert (sym);
8339 if (dwarf_split_debug_info)
8340 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8341 "%s", dwarf_attr_name (a->dw_attr));
8342 else
8343 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8344 "%s", dwarf_attr_name (a->dw_attr));
8347 /* Output an attribute's index or value appropriately. */
8349 static void
8350 output_attr_index_or_value (dw_attr_ref a)
8352 const char *name = dwarf_attr_name (a->dw_attr);
8354 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8356 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8357 return;
8359 switch (AT_class (a))
8361 case dw_val_class_addr:
8362 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8363 break;
8364 case dw_val_class_high_pc:
8365 case dw_val_class_lbl_id:
8366 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8367 break;
8368 case dw_val_class_loc_list:
8369 output_loc_list_offset (a);
8370 break;
8371 default:
8372 gcc_unreachable ();
8376 /* Output a type signature. */
8378 static inline void
8379 output_signature (const char *sig, const char *name)
8381 int i;
8383 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8384 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8387 /* Output the DIE and its attributes. Called recursively to generate
8388 the definitions of each child DIE. */
8390 static void
8391 output_die (dw_die_ref die)
8393 dw_attr_ref a;
8394 dw_die_ref c;
8395 unsigned long size;
8396 unsigned ix;
8398 /* If someone in another CU might refer to us, set up a symbol for
8399 them to point to. */
8400 if (! die->comdat_type_p && die->die_id.die_symbol)
8401 output_die_symbol (die);
8403 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8404 (unsigned long)die->die_offset,
8405 dwarf_tag_name (die->die_tag));
8407 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8409 const char *name = dwarf_attr_name (a->dw_attr);
8411 switch (AT_class (a))
8413 case dw_val_class_addr:
8414 output_attr_index_or_value (a);
8415 break;
8417 case dw_val_class_offset:
8418 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8419 "%s", name);
8420 break;
8422 case dw_val_class_range_list:
8423 output_range_list_offset (a);
8424 break;
8426 case dw_val_class_loc:
8427 size = size_of_locs (AT_loc (a));
8429 /* Output the block length for this list of location operations. */
8430 if (dwarf_version >= 4)
8431 dw2_asm_output_data_uleb128 (size, "%s", name);
8432 else
8433 dw2_asm_output_data (constant_size (size), size, "%s", name);
8435 output_loc_sequence (AT_loc (a), -1);
8436 break;
8438 case dw_val_class_const:
8439 /* ??? It would be slightly more efficient to use a scheme like is
8440 used for unsigned constants below, but gdb 4.x does not sign
8441 extend. Gdb 5.x does sign extend. */
8442 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8443 break;
8445 case dw_val_class_unsigned_const:
8447 int csize = constant_size (AT_unsigned (a));
8448 if (dwarf_version == 3
8449 && a->dw_attr == DW_AT_data_member_location
8450 && csize >= 4)
8451 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8452 else
8453 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8455 break;
8457 case dw_val_class_const_double:
8459 unsigned HOST_WIDE_INT first, second;
8461 if (HOST_BITS_PER_WIDE_INT >= 64)
8462 dw2_asm_output_data (1,
8463 HOST_BITS_PER_DOUBLE_INT
8464 / HOST_BITS_PER_CHAR,
8465 NULL);
8467 if (WORDS_BIG_ENDIAN)
8469 first = a->dw_attr_val.v.val_double.high;
8470 second = a->dw_attr_val.v.val_double.low;
8472 else
8474 first = a->dw_attr_val.v.val_double.low;
8475 second = a->dw_attr_val.v.val_double.high;
8478 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8479 first, "%s", name);
8480 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8481 second, NULL);
8483 break;
8485 case dw_val_class_vec:
8487 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8488 unsigned int len = a->dw_attr_val.v.val_vec.length;
8489 unsigned int i;
8490 unsigned char *p;
8492 dw2_asm_output_data (constant_size (len * elt_size),
8493 len * elt_size, "%s", name);
8494 if (elt_size > sizeof (HOST_WIDE_INT))
8496 elt_size /= 2;
8497 len *= 2;
8499 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8500 i < len;
8501 i++, p += elt_size)
8502 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8503 "fp or vector constant word %u", i);
8504 break;
8507 case dw_val_class_flag:
8508 if (dwarf_version >= 4)
8510 /* Currently all add_AT_flag calls pass in 1 as last argument,
8511 so DW_FORM_flag_present can be used. If that ever changes,
8512 we'll need to use DW_FORM_flag and have some optimization
8513 in build_abbrev_table that will change those to
8514 DW_FORM_flag_present if it is set to 1 in all DIEs using
8515 the same abbrev entry. */
8516 gcc_assert (AT_flag (a) == 1);
8517 if (flag_debug_asm)
8518 fprintf (asm_out_file, "\t\t\t%s %s\n",
8519 ASM_COMMENT_START, name);
8520 break;
8522 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8523 break;
8525 case dw_val_class_loc_list:
8526 output_attr_index_or_value (a);
8527 break;
8529 case dw_val_class_die_ref:
8530 if (AT_ref_external (a))
8532 if (AT_ref (a)->comdat_type_p)
8534 comdat_type_node_ref type_node =
8535 AT_ref (a)->die_id.die_type_node;
8537 gcc_assert (type_node);
8538 output_signature (type_node->signature, name);
8540 else
8542 const char *sym = AT_ref (a)->die_id.die_symbol;
8543 int size;
8545 gcc_assert (sym);
8546 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8547 length, whereas in DWARF3 it's always sized as an
8548 offset. */
8549 if (dwarf_version == 2)
8550 size = DWARF2_ADDR_SIZE;
8551 else
8552 size = DWARF_OFFSET_SIZE;
8553 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8554 name);
8557 else
8559 gcc_assert (AT_ref (a)->die_offset);
8560 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8561 "%s", name);
8563 break;
8565 case dw_val_class_fde_ref:
8567 char l1[20];
8569 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8570 a->dw_attr_val.v.val_fde_index * 2);
8571 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8572 "%s", name);
8574 break;
8576 case dw_val_class_vms_delta:
8577 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8578 AT_vms_delta2 (a), AT_vms_delta1 (a),
8579 "%s", name);
8580 break;
8582 case dw_val_class_lbl_id:
8583 output_attr_index_or_value (a);
8584 break;
8586 case dw_val_class_lineptr:
8587 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8588 debug_line_section, "%s", name);
8589 break;
8591 case dw_val_class_macptr:
8592 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8593 debug_macinfo_section, "%s", name);
8594 break;
8596 case dw_val_class_str:
8597 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8598 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8599 a->dw_attr_val.v.val_str->label,
8600 debug_str_section,
8601 "%s: \"%s\"", name, AT_string (a));
8602 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8603 dw2_asm_output_data_uleb128 (AT_index (a),
8604 "%s: \"%s\"", name, AT_string (a));
8605 else
8606 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8607 break;
8609 case dw_val_class_file:
8611 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8613 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8614 a->dw_attr_val.v.val_file->filename);
8615 break;
8618 case dw_val_class_data8:
8620 int i;
8622 for (i = 0; i < 8; i++)
8623 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8624 i == 0 ? "%s" : NULL, name);
8625 break;
8628 case dw_val_class_high_pc:
8629 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8630 get_AT_low_pc (die), "DW_AT_high_pc");
8631 break;
8633 default:
8634 gcc_unreachable ();
8638 FOR_EACH_CHILD (die, c, output_die (c));
8640 /* Add null byte to terminate sibling list. */
8641 if (die->die_child != NULL)
8642 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8643 (unsigned long) die->die_offset);
8646 /* Output the compilation unit that appears at the beginning of the
8647 .debug_info section, and precedes the DIE descriptions. */
8649 static void
8650 output_compilation_unit_header (void)
8652 int ver = dwarf_version;
8654 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8655 dw2_asm_output_data (4, 0xffffffff,
8656 "Initial length escape value indicating 64-bit DWARF extension");
8657 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8658 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8659 "Length of Compilation Unit Info");
8660 dw2_asm_output_data (2, ver, "DWARF version number");
8661 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8662 debug_abbrev_section,
8663 "Offset Into Abbrev. Section");
8664 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8667 /* Output the compilation unit DIE and its children. */
8669 static void
8670 output_comp_unit (dw_die_ref die, int output_if_empty)
8672 const char *secname, *oldsym;
8673 char *tmp;
8674 external_ref_hash_type extern_map;
8676 /* Unless we are outputting main CU, we may throw away empty ones. */
8677 if (!output_if_empty && die->die_child == NULL)
8678 return;
8680 /* Even if there are no children of this DIE, we must output the information
8681 about the compilation unit. Otherwise, on an empty translation unit, we
8682 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8683 will then complain when examining the file. First mark all the DIEs in
8684 this CU so we know which get local refs. */
8685 mark_dies (die);
8687 extern_map = optimize_external_refs (die);
8689 build_abbrev_table (die, extern_map);
8691 extern_map.dispose ();
8693 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8694 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8695 calc_die_sizes (die);
8697 oldsym = die->die_id.die_symbol;
8698 if (oldsym)
8700 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8702 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8703 secname = tmp;
8704 die->die_id.die_symbol = NULL;
8705 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8707 else
8709 switch_to_section (debug_info_section);
8710 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8711 info_section_emitted = true;
8714 /* Output debugging information. */
8715 output_compilation_unit_header ();
8716 output_die (die);
8718 /* Leave the marks on the main CU, so we can check them in
8719 output_pubnames. */
8720 if (oldsym)
8722 unmark_dies (die);
8723 die->die_id.die_symbol = oldsym;
8727 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8728 and .debug_pubtypes. This is configured per-target, but can be
8729 overridden by the -gpubnames or -gno-pubnames options. */
8731 static inline bool
8732 want_pubnames (void)
8734 return (debug_generate_pub_sections != -1
8735 ? debug_generate_pub_sections
8736 : targetm.want_debug_pub_sections);
8739 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8741 static void
8742 add_AT_pubnames (dw_die_ref die)
8744 if (want_pubnames ())
8745 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8748 /* Add a string attribute value to a skeleton DIE. */
8750 static inline void
8751 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8752 const char *str)
8754 dw_attr_node attr;
8755 struct indirect_string_node *node;
8757 if (! skeleton_debug_str_hash)
8758 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8759 debug_str_eq, NULL);
8761 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8762 find_string_form (node);
8763 if (node->form == DW_FORM_GNU_str_index)
8764 node->form = DW_FORM_strp;
8766 attr.dw_attr = attr_kind;
8767 attr.dw_attr_val.val_class = dw_val_class_str;
8768 attr.dw_attr_val.val_entry = NULL;
8769 attr.dw_attr_val.v.val_str = node;
8770 add_dwarf_attr (die, &attr);
8773 /* Helper function to generate top-level dies for skeleton debug_info and
8774 debug_types. */
8776 static void
8777 add_top_level_skeleton_die_attrs (dw_die_ref die)
8779 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8780 const char *comp_dir = comp_dir_string ();
8782 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8783 if (comp_dir != NULL)
8784 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8785 add_AT_pubnames (die);
8786 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8789 /* Return the single type-unit die for skeleton type units. */
8791 static dw_die_ref
8792 get_skeleton_type_unit (void)
8794 /* For dwarf_split_debug_sections with use_type info, all type units in the
8795 skeleton sections have identical dies (but different headers). This
8796 single die will be output many times. */
8798 static dw_die_ref skeleton_type_unit = NULL;
8800 if (skeleton_type_unit == NULL)
8802 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8803 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8804 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8806 return skeleton_type_unit;
8809 /* Output skeleton debug sections that point to the dwo file. */
8811 static void
8812 output_skeleton_debug_sections (dw_die_ref comp_unit)
8814 /* These attributes will be found in the full debug_info section. */
8815 remove_AT (comp_unit, DW_AT_producer);
8816 remove_AT (comp_unit, DW_AT_language);
8818 switch_to_section (debug_skeleton_info_section);
8819 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8821 /* Produce the skeleton compilation-unit header. This one differs enough from
8822 a normal CU header that it's better not to call output_compilation_unit
8823 header. */
8824 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8825 dw2_asm_output_data (4, 0xffffffff,
8826 "Initial length escape value indicating 64-bit DWARF extension");
8828 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8829 DWARF_COMPILE_UNIT_HEADER_SIZE
8830 - DWARF_INITIAL_LENGTH_SIZE
8831 + size_of_die (comp_unit),
8832 "Length of Compilation Unit Info");
8833 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8834 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8835 debug_abbrev_section,
8836 "Offset Into Abbrev. Section");
8837 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8839 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8840 output_die (comp_unit);
8842 /* Build the skeleton debug_abbrev section. */
8843 switch_to_section (debug_skeleton_abbrev_section);
8844 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8846 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8847 if (use_debug_types)
8848 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8850 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8853 /* Output a comdat type unit DIE and its children. */
8855 static void
8856 output_comdat_type_unit (comdat_type_node *node)
8858 const char *secname;
8859 char *tmp;
8860 int i;
8861 #if defined (OBJECT_FORMAT_ELF)
8862 tree comdat_key;
8863 #endif
8864 external_ref_hash_type extern_map;
8866 /* First mark all the DIEs in this CU so we know which get local refs. */
8867 mark_dies (node->root_die);
8869 extern_map = optimize_external_refs (node->root_die);
8871 build_abbrev_table (node->root_die, extern_map);
8873 extern_map.dispose ();
8875 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8876 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8877 calc_die_sizes (node->root_die);
8879 #if defined (OBJECT_FORMAT_ELF)
8880 if (!dwarf_split_debug_info)
8881 secname = ".debug_types";
8882 else
8883 secname = ".debug_types.dwo";
8885 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8886 sprintf (tmp, "wt.");
8887 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8888 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8889 comdat_key = get_identifier (tmp);
8890 targetm.asm_out.named_section (secname,
8891 SECTION_DEBUG | SECTION_LINKONCE,
8892 comdat_key);
8893 #else
8894 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8895 sprintf (tmp, ".gnu.linkonce.wt.");
8896 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8897 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8898 secname = tmp;
8899 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8900 #endif
8902 /* Output debugging information. */
8903 output_compilation_unit_header ();
8904 output_signature (node->signature, "Type Signature");
8905 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8906 "Offset to Type DIE");
8907 output_die (node->root_die);
8909 unmark_dies (node->root_die);
8911 #if defined (OBJECT_FORMAT_ELF)
8912 if (dwarf_split_debug_info)
8914 /* Produce the skeleton type-unit header. */
8915 const char *secname = ".debug_types";
8917 targetm.asm_out.named_section (secname,
8918 SECTION_DEBUG | SECTION_LINKONCE,
8919 comdat_key);
8920 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8921 dw2_asm_output_data (4, 0xffffffff,
8922 "Initial length escape value indicating 64-bit DWARF extension");
8924 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8925 DWARF_COMPILE_UNIT_HEADER_SIZE
8926 - DWARF_INITIAL_LENGTH_SIZE
8927 + size_of_die (get_skeleton_type_unit ())
8928 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8929 "Length of Type Unit Info");
8930 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8931 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8932 debug_skeleton_abbrev_section_label,
8933 debug_abbrev_section,
8934 "Offset Into Abbrev. Section");
8935 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8936 output_signature (node->signature, "Type Signature");
8937 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8939 output_die (get_skeleton_type_unit ());
8941 #endif
8944 /* Return the DWARF2/3 pubname associated with a decl. */
8946 static const char *
8947 dwarf2_name (tree decl, int scope)
8949 if (DECL_NAMELESS (decl))
8950 return NULL;
8951 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8954 /* Add a new entry to .debug_pubnames if appropriate. */
8956 static void
8957 add_pubname_string (const char *str, dw_die_ref die)
8959 pubname_entry e;
8961 e.die = die;
8962 e.name = xstrdup (str);
8963 vec_safe_push (pubname_table, e);
8966 static void
8967 add_pubname (tree decl, dw_die_ref die)
8969 if (!want_pubnames ())
8970 return;
8972 /* Don't add items to the table when we expect that the consumer will have
8973 just read the enclosing die. For example, if the consumer is looking at a
8974 class_member, it will either be inside the class already, or will have just
8975 looked up the class to find the member. Either way, searching the class is
8976 faster than searching the index. */
8977 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8978 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8980 const char *name = dwarf2_name (decl, 1);
8982 if (name)
8983 add_pubname_string (name, die);
8987 /* Add an enumerator to the pubnames section. */
8989 static void
8990 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8992 pubname_entry e;
8994 gcc_assert (scope_name);
8995 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8996 e.die = die;
8997 vec_safe_push (pubname_table, e);
9000 /* Add a new entry to .debug_pubtypes if appropriate. */
9002 static void
9003 add_pubtype (tree decl, dw_die_ref die)
9005 pubname_entry e;
9007 if (!want_pubnames ())
9008 return;
9010 if ((TREE_PUBLIC (decl)
9011 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9012 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9014 tree scope = NULL;
9015 const char *scope_name = "";
9016 const char *sep = is_cxx () ? "::" : ".";
9017 const char *name;
9019 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9020 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9022 scope_name = lang_hooks.dwarf_name (scope, 1);
9023 if (scope_name != NULL && scope_name[0] != '\0')
9024 scope_name = concat (scope_name, sep, NULL);
9025 else
9026 scope_name = "";
9029 if (TYPE_P (decl))
9030 name = type_tag (decl);
9031 else
9032 name = lang_hooks.dwarf_name (decl, 1);
9034 /* If we don't have a name for the type, there's no point in adding
9035 it to the table. */
9036 if (name != NULL && name[0] != '\0')
9038 e.die = die;
9039 e.name = concat (scope_name, name, NULL);
9040 vec_safe_push (pubtype_table, e);
9043 /* Although it might be more consistent to add the pubinfo for the
9044 enumerators as their dies are created, they should only be added if the
9045 enum type meets the criteria above. So rather than re-check the parent
9046 enum type whenever an enumerator die is created, just output them all
9047 here. This isn't protected by the name conditional because anonymous
9048 enums don't have names. */
9049 if (die->die_tag == DW_TAG_enumeration_type)
9051 dw_die_ref c;
9053 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9058 /* Output the public names table used to speed up access to externally
9059 visible names; or the public types table used to find type definitions. */
9061 static void
9062 output_pubnames (vec<pubname_entry, va_gc> *names)
9064 unsigned i;
9065 unsigned long pubnames_length = size_of_pubnames (names);
9066 pubname_ref pub;
9068 if (!want_pubnames () || !info_section_emitted)
9069 return;
9070 if (names == pubname_table)
9071 switch_to_section (debug_pubnames_section);
9072 else
9073 switch_to_section (debug_pubtypes_section);
9074 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9075 dw2_asm_output_data (4, 0xffffffff,
9076 "Initial length escape value indicating 64-bit DWARF extension");
9077 if (names == pubname_table)
9078 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9079 "Length of Public Names Info");
9080 else
9081 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9082 "Length of Public Type Names Info");
9083 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
9084 dw2_asm_output_data (2, 2, "DWARF Version");
9085 if (dwarf_split_debug_info)
9086 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9087 debug_skeleton_info_section,
9088 "Offset of Compilation Unit Info");
9089 else
9090 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9091 debug_info_section,
9092 "Offset of Compilation Unit Info");
9093 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9094 "Compilation Unit Length");
9096 FOR_EACH_VEC_ELT (*names, i, pub)
9098 /* Enumerator names are part of the pubname table, but the parent
9099 DW_TAG_enumeration_type die may have been pruned. Don't output
9100 them if that is the case. */
9101 if (pub->die->die_tag == DW_TAG_enumerator &&
9102 (pub->die->die_parent == NULL
9103 || !pub->die->die_parent->die_perennial_p))
9104 continue;
9106 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9107 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9108 gcc_assert (pub->die->die_mark);
9110 if (names != pubtype_table
9111 || pub->die->die_offset != 0
9112 || !flag_eliminate_unused_debug_types)
9114 dw_offset die_offset = pub->die->die_offset;
9116 /* If we're putting types in their own .debug_types sections,
9117 the .debug_pubtypes table will still point to the compile
9118 unit (not the type unit), so we want to use the offset of
9119 the skeleton DIE (if there is one). */
9120 if (pub->die->comdat_type_p && names == pubtype_table)
9122 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9124 if (type_node != NULL)
9125 die_offset = (type_node->skeleton_die != NULL
9126 ? type_node->skeleton_die->die_offset
9127 : 0);
9130 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9132 dw2_asm_output_nstring (pub->name, -1, "external name");
9136 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9139 /* Output the information that goes into the .debug_aranges table.
9140 Namely, define the beginning and ending address range of the
9141 text section generated for this compilation unit. */
9143 static void
9144 output_aranges (unsigned long aranges_length)
9146 unsigned i;
9148 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9149 dw2_asm_output_data (4, 0xffffffff,
9150 "Initial length escape value indicating 64-bit DWARF extension");
9151 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9152 "Length of Address Ranges Info");
9153 /* Version number for aranges is still 2, even in DWARF3. */
9154 dw2_asm_output_data (2, 2, "DWARF Version");
9155 if (dwarf_split_debug_info)
9156 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9157 debug_skeleton_info_section,
9158 "Offset of Compilation Unit Info");
9159 else
9160 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9161 debug_info_section,
9162 "Offset of Compilation Unit Info");
9163 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9164 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9166 /* We need to align to twice the pointer size here. */
9167 if (DWARF_ARANGES_PAD_SIZE)
9169 /* Pad using a 2 byte words so that padding is correct for any
9170 pointer size. */
9171 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9172 2 * DWARF2_ADDR_SIZE);
9173 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9174 dw2_asm_output_data (2, 0, NULL);
9177 /* It is necessary not to output these entries if the sections were
9178 not used; if the sections were not used, the length will be 0 and
9179 the address may end up as 0 if the section is discarded by ld
9180 --gc-sections, leaving an invalid (0, 0) entry that can be
9181 confused with the terminator. */
9182 if (text_section_used)
9184 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9185 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9186 text_section_label, "Length");
9188 if (cold_text_section_used)
9190 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9191 "Address");
9192 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9193 cold_text_section_label, "Length");
9196 if (have_multiple_function_sections)
9198 unsigned fde_idx;
9199 dw_fde_ref fde;
9201 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9203 if (DECL_IGNORED_P (fde->decl))
9204 continue;
9205 if (!fde->in_std_section)
9207 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9208 "Address");
9209 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9210 fde->dw_fde_begin, "Length");
9212 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9214 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9215 "Address");
9216 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9217 fde->dw_fde_second_begin, "Length");
9222 /* Output the terminator words. */
9223 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9224 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9227 /* Add a new entry to .debug_ranges. Return the offset at which it
9228 was placed. */
9230 static unsigned int
9231 add_ranges_num (int num)
9233 unsigned int in_use = ranges_table_in_use;
9235 if (in_use == ranges_table_allocated)
9237 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9238 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9239 ranges_table_allocated);
9240 memset (ranges_table + ranges_table_in_use, 0,
9241 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9244 ranges_table[in_use].num = num;
9245 ranges_table_in_use = in_use + 1;
9247 return in_use * 2 * DWARF2_ADDR_SIZE;
9250 /* Add a new entry to .debug_ranges corresponding to a block, or a
9251 range terminator if BLOCK is NULL. */
9253 static unsigned int
9254 add_ranges (const_tree block)
9256 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9259 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9260 When using dwarf_split_debug_info, address attributes in dies destined
9261 for the final executable should be direct references--setting the
9262 parameter force_direct ensures this behavior. */
9264 static void
9265 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9266 bool *added, bool force_direct)
9268 unsigned int in_use = ranges_by_label_in_use;
9269 unsigned int offset;
9271 if (in_use == ranges_by_label_allocated)
9273 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9274 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9275 ranges_by_label,
9276 ranges_by_label_allocated);
9277 memset (ranges_by_label + ranges_by_label_in_use, 0,
9278 RANGES_TABLE_INCREMENT
9279 * sizeof (struct dw_ranges_by_label_struct));
9282 ranges_by_label[in_use].begin = begin;
9283 ranges_by_label[in_use].end = end;
9284 ranges_by_label_in_use = in_use + 1;
9286 offset = add_ranges_num (-(int)in_use - 1);
9287 if (!*added)
9289 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9290 *added = true;
9294 static void
9295 output_ranges (void)
9297 unsigned i;
9298 static const char *const start_fmt = "Offset %#x";
9299 const char *fmt = start_fmt;
9301 for (i = 0; i < ranges_table_in_use; i++)
9303 int block_num = ranges_table[i].num;
9305 if (block_num > 0)
9307 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9308 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9310 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9311 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9313 /* If all code is in the text section, then the compilation
9314 unit base address defaults to DW_AT_low_pc, which is the
9315 base of the text section. */
9316 if (!have_multiple_function_sections)
9318 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9319 text_section_label,
9320 fmt, i * 2 * DWARF2_ADDR_SIZE);
9321 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9322 text_section_label, NULL);
9325 /* Otherwise, the compilation unit base address is zero,
9326 which allows us to use absolute addresses, and not worry
9327 about whether the target supports cross-section
9328 arithmetic. */
9329 else
9331 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9332 fmt, i * 2 * DWARF2_ADDR_SIZE);
9333 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9336 fmt = NULL;
9339 /* Negative block_num stands for an index into ranges_by_label. */
9340 else if (block_num < 0)
9342 int lab_idx = - block_num - 1;
9344 if (!have_multiple_function_sections)
9346 gcc_unreachable ();
9347 #if 0
9348 /* If we ever use add_ranges_by_labels () for a single
9349 function section, all we have to do is to take out
9350 the #if 0 above. */
9351 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9352 ranges_by_label[lab_idx].begin,
9353 text_section_label,
9354 fmt, i * 2 * DWARF2_ADDR_SIZE);
9355 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9356 ranges_by_label[lab_idx].end,
9357 text_section_label, NULL);
9358 #endif
9360 else
9362 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9363 ranges_by_label[lab_idx].begin,
9364 fmt, i * 2 * DWARF2_ADDR_SIZE);
9365 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9366 ranges_by_label[lab_idx].end,
9367 NULL);
9370 else
9372 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9373 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9374 fmt = start_fmt;
9379 /* Data structure containing information about input files. */
9380 struct file_info
9382 const char *path; /* Complete file name. */
9383 const char *fname; /* File name part. */
9384 int length; /* Length of entire string. */
9385 struct dwarf_file_data * file_idx; /* Index in input file table. */
9386 int dir_idx; /* Index in directory table. */
9389 /* Data structure containing information about directories with source
9390 files. */
9391 struct dir_info
9393 const char *path; /* Path including directory name. */
9394 int length; /* Path length. */
9395 int prefix; /* Index of directory entry which is a prefix. */
9396 int count; /* Number of files in this directory. */
9397 int dir_idx; /* Index of directory used as base. */
9400 /* Callback function for file_info comparison. We sort by looking at
9401 the directories in the path. */
9403 static int
9404 file_info_cmp (const void *p1, const void *p2)
9406 const struct file_info *const s1 = (const struct file_info *) p1;
9407 const struct file_info *const s2 = (const struct file_info *) p2;
9408 const unsigned char *cp1;
9409 const unsigned char *cp2;
9411 /* Take care of file names without directories. We need to make sure that
9412 we return consistent values to qsort since some will get confused if
9413 we return the same value when identical operands are passed in opposite
9414 orders. So if neither has a directory, return 0 and otherwise return
9415 1 or -1 depending on which one has the directory. */
9416 if ((s1->path == s1->fname || s2->path == s2->fname))
9417 return (s2->path == s2->fname) - (s1->path == s1->fname);
9419 cp1 = (const unsigned char *) s1->path;
9420 cp2 = (const unsigned char *) s2->path;
9422 while (1)
9424 ++cp1;
9425 ++cp2;
9426 /* Reached the end of the first path? If so, handle like above. */
9427 if ((cp1 == (const unsigned char *) s1->fname)
9428 || (cp2 == (const unsigned char *) s2->fname))
9429 return ((cp2 == (const unsigned char *) s2->fname)
9430 - (cp1 == (const unsigned char *) s1->fname));
9432 /* Character of current path component the same? */
9433 else if (*cp1 != *cp2)
9434 return *cp1 - *cp2;
9438 struct file_name_acquire_data
9440 struct file_info *files;
9441 int used_files;
9442 int max_files;
9445 /* Traversal function for the hash table. */
9447 static int
9448 file_name_acquire (void ** slot, void *data)
9450 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9451 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9452 struct file_info *fi;
9453 const char *f;
9455 gcc_assert (fnad->max_files >= d->emitted_number);
9457 if (! d->emitted_number)
9458 return 1;
9460 gcc_assert (fnad->max_files != fnad->used_files);
9462 fi = fnad->files + fnad->used_files++;
9464 /* Skip all leading "./". */
9465 f = d->filename;
9466 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9467 f += 2;
9469 /* Create a new array entry. */
9470 fi->path = f;
9471 fi->length = strlen (f);
9472 fi->file_idx = d;
9474 /* Search for the file name part. */
9475 f = strrchr (f, DIR_SEPARATOR);
9476 #if defined (DIR_SEPARATOR_2)
9478 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9480 if (g != NULL)
9482 if (f == NULL || f < g)
9483 f = g;
9486 #endif
9488 fi->fname = f == NULL ? fi->path : f + 1;
9489 return 1;
9492 /* Output the directory table and the file name table. We try to minimize
9493 the total amount of memory needed. A heuristic is used to avoid large
9494 slowdowns with many input files. */
9496 static void
9497 output_file_names (void)
9499 struct file_name_acquire_data fnad;
9500 int numfiles;
9501 struct file_info *files;
9502 struct dir_info *dirs;
9503 int *saved;
9504 int *savehere;
9505 int *backmap;
9506 int ndirs;
9507 int idx_offset;
9508 int i;
9510 if (!last_emitted_file)
9512 dw2_asm_output_data (1, 0, "End directory table");
9513 dw2_asm_output_data (1, 0, "End file name table");
9514 return;
9517 numfiles = last_emitted_file->emitted_number;
9519 /* Allocate the various arrays we need. */
9520 files = XALLOCAVEC (struct file_info, numfiles);
9521 dirs = XALLOCAVEC (struct dir_info, numfiles);
9523 fnad.files = files;
9524 fnad.used_files = 0;
9525 fnad.max_files = numfiles;
9526 htab_traverse (file_table, file_name_acquire, &fnad);
9527 gcc_assert (fnad.used_files == fnad.max_files);
9529 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9531 /* Find all the different directories used. */
9532 dirs[0].path = files[0].path;
9533 dirs[0].length = files[0].fname - files[0].path;
9534 dirs[0].prefix = -1;
9535 dirs[0].count = 1;
9536 dirs[0].dir_idx = 0;
9537 files[0].dir_idx = 0;
9538 ndirs = 1;
9540 for (i = 1; i < numfiles; i++)
9541 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9542 && memcmp (dirs[ndirs - 1].path, files[i].path,
9543 dirs[ndirs - 1].length) == 0)
9545 /* Same directory as last entry. */
9546 files[i].dir_idx = ndirs - 1;
9547 ++dirs[ndirs - 1].count;
9549 else
9551 int j;
9553 /* This is a new directory. */
9554 dirs[ndirs].path = files[i].path;
9555 dirs[ndirs].length = files[i].fname - files[i].path;
9556 dirs[ndirs].count = 1;
9557 dirs[ndirs].dir_idx = ndirs;
9558 files[i].dir_idx = ndirs;
9560 /* Search for a prefix. */
9561 dirs[ndirs].prefix = -1;
9562 for (j = 0; j < ndirs; j++)
9563 if (dirs[j].length < dirs[ndirs].length
9564 && dirs[j].length > 1
9565 && (dirs[ndirs].prefix == -1
9566 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9567 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9568 dirs[ndirs].prefix = j;
9570 ++ndirs;
9573 /* Now to the actual work. We have to find a subset of the directories which
9574 allow expressing the file name using references to the directory table
9575 with the least amount of characters. We do not do an exhaustive search
9576 where we would have to check out every combination of every single
9577 possible prefix. Instead we use a heuristic which provides nearly optimal
9578 results in most cases and never is much off. */
9579 saved = XALLOCAVEC (int, ndirs);
9580 savehere = XALLOCAVEC (int, ndirs);
9582 memset (saved, '\0', ndirs * sizeof (saved[0]));
9583 for (i = 0; i < ndirs; i++)
9585 int j;
9586 int total;
9588 /* We can always save some space for the current directory. But this
9589 does not mean it will be enough to justify adding the directory. */
9590 savehere[i] = dirs[i].length;
9591 total = (savehere[i] - saved[i]) * dirs[i].count;
9593 for (j = i + 1; j < ndirs; j++)
9595 savehere[j] = 0;
9596 if (saved[j] < dirs[i].length)
9598 /* Determine whether the dirs[i] path is a prefix of the
9599 dirs[j] path. */
9600 int k;
9602 k = dirs[j].prefix;
9603 while (k != -1 && k != (int) i)
9604 k = dirs[k].prefix;
9606 if (k == (int) i)
9608 /* Yes it is. We can possibly save some memory by
9609 writing the filenames in dirs[j] relative to
9610 dirs[i]. */
9611 savehere[j] = dirs[i].length;
9612 total += (savehere[j] - saved[j]) * dirs[j].count;
9617 /* Check whether we can save enough to justify adding the dirs[i]
9618 directory. */
9619 if (total > dirs[i].length + 1)
9621 /* It's worthwhile adding. */
9622 for (j = i; j < ndirs; j++)
9623 if (savehere[j] > 0)
9625 /* Remember how much we saved for this directory so far. */
9626 saved[j] = savehere[j];
9628 /* Remember the prefix directory. */
9629 dirs[j].dir_idx = i;
9634 /* Emit the directory name table. */
9635 idx_offset = dirs[0].length > 0 ? 1 : 0;
9636 for (i = 1 - idx_offset; i < ndirs; i++)
9637 dw2_asm_output_nstring (dirs[i].path,
9638 dirs[i].length
9639 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9640 "Directory Entry: %#x", i + idx_offset);
9642 dw2_asm_output_data (1, 0, "End directory table");
9644 /* We have to emit them in the order of emitted_number since that's
9645 used in the debug info generation. To do this efficiently we
9646 generate a back-mapping of the indices first. */
9647 backmap = XALLOCAVEC (int, numfiles);
9648 for (i = 0; i < numfiles; i++)
9649 backmap[files[i].file_idx->emitted_number - 1] = i;
9651 /* Now write all the file names. */
9652 for (i = 0; i < numfiles; i++)
9654 int file_idx = backmap[i];
9655 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9657 #ifdef VMS_DEBUGGING_INFO
9658 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9660 /* Setting these fields can lead to debugger miscomparisons,
9661 but VMS Debug requires them to be set correctly. */
9663 int ver;
9664 long long cdt;
9665 long siz;
9666 int maxfilelen = strlen (files[file_idx].path)
9667 + dirs[dir_idx].length
9668 + MAX_VMS_VERSION_LEN + 1;
9669 char *filebuf = XALLOCAVEC (char, maxfilelen);
9671 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9672 snprintf (filebuf, maxfilelen, "%s;%d",
9673 files[file_idx].path + dirs[dir_idx].length, ver);
9675 dw2_asm_output_nstring
9676 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9678 /* Include directory index. */
9679 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9681 /* Modification time. */
9682 dw2_asm_output_data_uleb128
9683 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9684 ? cdt : 0,
9685 NULL);
9687 /* File length in bytes. */
9688 dw2_asm_output_data_uleb128
9689 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9690 ? siz : 0,
9691 NULL);
9692 #else
9693 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9694 "File Entry: %#x", (unsigned) i + 1);
9696 /* Include directory index. */
9697 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9699 /* Modification time. */
9700 dw2_asm_output_data_uleb128 (0, NULL);
9702 /* File length in bytes. */
9703 dw2_asm_output_data_uleb128 (0, NULL);
9704 #endif /* VMS_DEBUGGING_INFO */
9707 dw2_asm_output_data (1, 0, "End file name table");
9711 /* Output one line number table into the .debug_line section. */
9713 static void
9714 output_one_line_info_table (dw_line_info_table *table)
9716 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9717 unsigned int current_line = 1;
9718 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9719 dw_line_info_entry *ent;
9720 size_t i;
9722 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9724 switch (ent->opcode)
9726 case LI_set_address:
9727 /* ??? Unfortunately, we have little choice here currently, and
9728 must always use the most general form. GCC does not know the
9729 address delta itself, so we can't use DW_LNS_advance_pc. Many
9730 ports do have length attributes which will give an upper bound
9731 on the address range. We could perhaps use length attributes
9732 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9733 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9735 /* This can handle any delta. This takes
9736 4+DWARF2_ADDR_SIZE bytes. */
9737 dw2_asm_output_data (1, 0, "set address %s", line_label);
9738 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9739 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9740 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9741 break;
9743 case LI_set_line:
9744 if (ent->val == current_line)
9746 /* We still need to start a new row, so output a copy insn. */
9747 dw2_asm_output_data (1, DW_LNS_copy,
9748 "copy line %u", current_line);
9750 else
9752 int line_offset = ent->val - current_line;
9753 int line_delta = line_offset - DWARF_LINE_BASE;
9755 current_line = ent->val;
9756 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9758 /* This can handle deltas from -10 to 234, using the current
9759 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9760 This takes 1 byte. */
9761 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9762 "line %u", current_line);
9764 else
9766 /* This can handle any delta. This takes at least 4 bytes,
9767 depending on the value being encoded. */
9768 dw2_asm_output_data (1, DW_LNS_advance_line,
9769 "advance to line %u", current_line);
9770 dw2_asm_output_data_sleb128 (line_offset, NULL);
9771 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9774 break;
9776 case LI_set_file:
9777 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9778 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9779 break;
9781 case LI_set_column:
9782 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9783 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9784 break;
9786 case LI_negate_stmt:
9787 current_is_stmt = !current_is_stmt;
9788 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9789 "is_stmt %d", current_is_stmt);
9790 break;
9792 case LI_set_prologue_end:
9793 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9794 "set prologue end");
9795 break;
9797 case LI_set_epilogue_begin:
9798 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9799 "set epilogue begin");
9800 break;
9802 case LI_set_discriminator:
9803 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9804 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9805 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9806 dw2_asm_output_data_uleb128 (ent->val, NULL);
9807 break;
9811 /* Emit debug info for the address of the end of the table. */
9812 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9813 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9814 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9815 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9817 dw2_asm_output_data (1, 0, "end sequence");
9818 dw2_asm_output_data_uleb128 (1, NULL);
9819 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9822 /* Output the source line number correspondence information. This
9823 information goes into the .debug_line section. */
9825 static void
9826 output_line_info (bool prologue_only)
9828 char l1[20], l2[20], p1[20], p2[20];
9829 int ver = dwarf_version;
9830 bool saw_one = false;
9831 int opc;
9833 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9834 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9835 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9836 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9838 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9839 dw2_asm_output_data (4, 0xffffffff,
9840 "Initial length escape value indicating 64-bit DWARF extension");
9841 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9842 "Length of Source Line Info");
9843 ASM_OUTPUT_LABEL (asm_out_file, l1);
9845 dw2_asm_output_data (2, ver, "DWARF Version");
9846 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9847 ASM_OUTPUT_LABEL (asm_out_file, p1);
9849 /* Define the architecture-dependent minimum instruction length (in bytes).
9850 In this implementation of DWARF, this field is used for information
9851 purposes only. Since GCC generates assembly language, we have no
9852 a priori knowledge of how many instruction bytes are generated for each
9853 source line, and therefore can use only the DW_LNE_set_address and
9854 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9855 this as '1', which is "correct enough" for all architectures,
9856 and don't let the target override. */
9857 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9859 if (ver >= 4)
9860 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9861 "Maximum Operations Per Instruction");
9862 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9863 "Default is_stmt_start flag");
9864 dw2_asm_output_data (1, DWARF_LINE_BASE,
9865 "Line Base Value (Special Opcodes)");
9866 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9867 "Line Range Value (Special Opcodes)");
9868 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9869 "Special Opcode Base");
9871 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9873 int n_op_args;
9874 switch (opc)
9876 case DW_LNS_advance_pc:
9877 case DW_LNS_advance_line:
9878 case DW_LNS_set_file:
9879 case DW_LNS_set_column:
9880 case DW_LNS_fixed_advance_pc:
9881 case DW_LNS_set_isa:
9882 n_op_args = 1;
9883 break;
9884 default:
9885 n_op_args = 0;
9886 break;
9889 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9890 opc, n_op_args);
9893 /* Write out the information about the files we use. */
9894 output_file_names ();
9895 ASM_OUTPUT_LABEL (asm_out_file, p2);
9896 if (prologue_only)
9898 /* Output the marker for the end of the line number info. */
9899 ASM_OUTPUT_LABEL (asm_out_file, l2);
9900 return;
9903 if (separate_line_info)
9905 dw_line_info_table *table;
9906 size_t i;
9908 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9909 if (table->in_use)
9911 output_one_line_info_table (table);
9912 saw_one = true;
9915 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9917 output_one_line_info_table (cold_text_section_line_info);
9918 saw_one = true;
9921 /* ??? Some Darwin linkers crash on a .debug_line section with no
9922 sequences. Further, merely a DW_LNE_end_sequence entry is not
9923 sufficient -- the address column must also be initialized.
9924 Make sure to output at least one set_address/end_sequence pair,
9925 choosing .text since that section is always present. */
9926 if (text_section_line_info->in_use || !saw_one)
9927 output_one_line_info_table (text_section_line_info);
9929 /* Output the marker for the end of the line number info. */
9930 ASM_OUTPUT_LABEL (asm_out_file, l2);
9933 /* Given a pointer to a tree node for some base type, return a pointer to
9934 a DIE that describes the given type.
9936 This routine must only be called for GCC type nodes that correspond to
9937 Dwarf base (fundamental) types. */
9939 static dw_die_ref
9940 base_type_die (tree type)
9942 dw_die_ref base_type_result;
9943 enum dwarf_type encoding;
9945 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9946 return 0;
9948 /* If this is a subtype that should not be emitted as a subrange type,
9949 use the base type. See subrange_type_for_debug_p. */
9950 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9951 type = TREE_TYPE (type);
9953 switch (TREE_CODE (type))
9955 case INTEGER_TYPE:
9956 if ((dwarf_version >= 4 || !dwarf_strict)
9957 && TYPE_NAME (type)
9958 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9959 && DECL_IS_BUILTIN (TYPE_NAME (type))
9960 && DECL_NAME (TYPE_NAME (type)))
9962 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9963 if (strcmp (name, "char16_t") == 0
9964 || strcmp (name, "char32_t") == 0)
9966 encoding = DW_ATE_UTF;
9967 break;
9970 if (TYPE_STRING_FLAG (type))
9972 if (TYPE_UNSIGNED (type))
9973 encoding = DW_ATE_unsigned_char;
9974 else
9975 encoding = DW_ATE_signed_char;
9977 else if (TYPE_UNSIGNED (type))
9978 encoding = DW_ATE_unsigned;
9979 else
9980 encoding = DW_ATE_signed;
9981 break;
9983 case REAL_TYPE:
9984 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9986 if (dwarf_version >= 3 || !dwarf_strict)
9987 encoding = DW_ATE_decimal_float;
9988 else
9989 encoding = DW_ATE_lo_user;
9991 else
9992 encoding = DW_ATE_float;
9993 break;
9995 case FIXED_POINT_TYPE:
9996 if (!(dwarf_version >= 3 || !dwarf_strict))
9997 encoding = DW_ATE_lo_user;
9998 else if (TYPE_UNSIGNED (type))
9999 encoding = DW_ATE_unsigned_fixed;
10000 else
10001 encoding = DW_ATE_signed_fixed;
10002 break;
10004 /* Dwarf2 doesn't know anything about complex ints, so use
10005 a user defined type for it. */
10006 case COMPLEX_TYPE:
10007 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10008 encoding = DW_ATE_complex_float;
10009 else
10010 encoding = DW_ATE_lo_user;
10011 break;
10013 case BOOLEAN_TYPE:
10014 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10015 encoding = DW_ATE_boolean;
10016 break;
10018 default:
10019 /* No other TREE_CODEs are Dwarf fundamental types. */
10020 gcc_unreachable ();
10023 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10025 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10026 int_size_in_bytes (type));
10027 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10028 add_pubtype (type, base_type_result);
10030 return base_type_result;
10033 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10034 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10036 static inline int
10037 is_base_type (tree type)
10039 switch (TREE_CODE (type))
10041 case ERROR_MARK:
10042 case VOID_TYPE:
10043 case INTEGER_TYPE:
10044 case REAL_TYPE:
10045 case FIXED_POINT_TYPE:
10046 case COMPLEX_TYPE:
10047 case BOOLEAN_TYPE:
10048 return 1;
10050 case ARRAY_TYPE:
10051 case RECORD_TYPE:
10052 case UNION_TYPE:
10053 case QUAL_UNION_TYPE:
10054 case ENUMERAL_TYPE:
10055 case FUNCTION_TYPE:
10056 case METHOD_TYPE:
10057 case POINTER_TYPE:
10058 case REFERENCE_TYPE:
10059 case NULLPTR_TYPE:
10060 case OFFSET_TYPE:
10061 case LANG_TYPE:
10062 case VECTOR_TYPE:
10063 return 0;
10065 default:
10066 gcc_unreachable ();
10069 return 0;
10072 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10073 node, return the size in bits for the type if it is a constant, or else
10074 return the alignment for the type if the type's size is not constant, or
10075 else return BITS_PER_WORD if the type actually turns out to be an
10076 ERROR_MARK node. */
10078 static inline unsigned HOST_WIDE_INT
10079 simple_type_size_in_bits (const_tree type)
10081 if (TREE_CODE (type) == ERROR_MARK)
10082 return BITS_PER_WORD;
10083 else if (TYPE_SIZE (type) == NULL_TREE)
10084 return 0;
10085 else if (host_integerp (TYPE_SIZE (type), 1))
10086 return tree_low_cst (TYPE_SIZE (type), 1);
10087 else
10088 return TYPE_ALIGN (type);
10091 /* Similarly, but return a double_int instead of UHWI. */
10093 static inline double_int
10094 double_int_type_size_in_bits (const_tree type)
10096 if (TREE_CODE (type) == ERROR_MARK)
10097 return double_int::from_uhwi (BITS_PER_WORD);
10098 else if (TYPE_SIZE (type) == NULL_TREE)
10099 return double_int_zero;
10100 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10101 return tree_to_double_int (TYPE_SIZE (type));
10102 else
10103 return double_int::from_uhwi (TYPE_ALIGN (type));
10106 /* Given a pointer to a tree node for a subrange type, return a pointer
10107 to a DIE that describes the given type. */
10109 static dw_die_ref
10110 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10112 dw_die_ref subrange_die;
10113 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10115 if (context_die == NULL)
10116 context_die = comp_unit_die ();
10118 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10120 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10122 /* The size of the subrange type and its base type do not match,
10123 so we need to generate a size attribute for the subrange type. */
10124 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10127 if (low)
10128 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10129 if (high)
10130 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10132 return subrange_die;
10135 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10136 entry that chains various modifiers in front of the given type. */
10138 static dw_die_ref
10139 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10140 dw_die_ref context_die)
10142 enum tree_code code = TREE_CODE (type);
10143 dw_die_ref mod_type_die;
10144 dw_die_ref sub_die = NULL;
10145 tree item_type = NULL;
10146 tree qualified_type;
10147 tree name, low, high;
10148 dw_die_ref mod_scope;
10150 if (code == ERROR_MARK)
10151 return NULL;
10153 /* See if we already have the appropriately qualified variant of
10154 this type. */
10155 qualified_type
10156 = get_qualified_type (type,
10157 ((is_const_type ? TYPE_QUAL_CONST : 0)
10158 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10160 if (qualified_type == sizetype
10161 && TYPE_NAME (qualified_type)
10162 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10164 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10166 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10167 && TYPE_PRECISION (t)
10168 == TYPE_PRECISION (qualified_type)
10169 && TYPE_UNSIGNED (t)
10170 == TYPE_UNSIGNED (qualified_type));
10171 qualified_type = t;
10174 /* If we do, then we can just use its DIE, if it exists. */
10175 if (qualified_type)
10177 mod_type_die = lookup_type_die (qualified_type);
10178 if (mod_type_die)
10179 return mod_type_die;
10182 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10184 /* Handle C typedef types. */
10185 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10186 && !DECL_ARTIFICIAL (name))
10188 tree dtype = TREE_TYPE (name);
10190 if (qualified_type == dtype)
10192 /* For a named type, use the typedef. */
10193 gen_type_die (qualified_type, context_die);
10194 return lookup_type_die (qualified_type);
10196 else if (is_const_type < TYPE_READONLY (dtype)
10197 || is_volatile_type < TYPE_VOLATILE (dtype)
10198 || (is_const_type <= TYPE_READONLY (dtype)
10199 && is_volatile_type <= TYPE_VOLATILE (dtype)
10200 && DECL_ORIGINAL_TYPE (name) != type))
10201 /* cv-unqualified version of named type. Just use the unnamed
10202 type to which it refers. */
10203 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10204 is_const_type, is_volatile_type,
10205 context_die);
10206 /* Else cv-qualified version of named type; fall through. */
10209 mod_scope = scope_die_for (type, context_die);
10211 if (is_const_type
10212 /* If both is_const_type and is_volatile_type, prefer the path
10213 which leads to a qualified type. */
10214 && (!is_volatile_type
10215 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10216 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10218 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10219 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10221 else if (is_volatile_type)
10223 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10224 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10226 else if (code == POINTER_TYPE)
10228 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10229 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10230 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10231 item_type = TREE_TYPE (type);
10232 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10233 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10234 TYPE_ADDR_SPACE (item_type));
10236 else if (code == REFERENCE_TYPE)
10238 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10239 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10240 type);
10241 else
10242 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10243 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10244 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10245 item_type = TREE_TYPE (type);
10246 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10247 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10248 TYPE_ADDR_SPACE (item_type));
10250 else if (code == INTEGER_TYPE
10251 && TREE_TYPE (type) != NULL_TREE
10252 && subrange_type_for_debug_p (type, &low, &high))
10254 mod_type_die = subrange_type_die (type, low, high, context_die);
10255 item_type = TREE_TYPE (type);
10257 else if (is_base_type (type))
10258 mod_type_die = base_type_die (type);
10259 else
10261 gen_type_die (type, context_die);
10263 /* We have to get the type_main_variant here (and pass that to the
10264 `lookup_type_die' routine) because the ..._TYPE node we have
10265 might simply be a *copy* of some original type node (where the
10266 copy was created to help us keep track of typedef names) and
10267 that copy might have a different TYPE_UID from the original
10268 ..._TYPE node. */
10269 if (TREE_CODE (type) != VECTOR_TYPE)
10270 return lookup_type_die (type_main_variant (type));
10271 else
10272 /* Vectors have the debugging information in the type,
10273 not the main variant. */
10274 return lookup_type_die (type);
10277 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10278 don't output a DW_TAG_typedef, since there isn't one in the
10279 user's program; just attach a DW_AT_name to the type.
10280 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10281 if the base type already has the same name. */
10282 if (name
10283 && ((TREE_CODE (name) != TYPE_DECL
10284 && (qualified_type == TYPE_MAIN_VARIANT (type)
10285 || (!is_const_type && !is_volatile_type)))
10286 || (TREE_CODE (name) == TYPE_DECL
10287 && TREE_TYPE (name) == qualified_type
10288 && DECL_NAME (name))))
10290 if (TREE_CODE (name) == TYPE_DECL)
10291 /* Could just call add_name_and_src_coords_attributes here,
10292 but since this is a builtin type it doesn't have any
10293 useful source coordinates anyway. */
10294 name = DECL_NAME (name);
10295 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10297 /* This probably indicates a bug. */
10298 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10300 name = TYPE_NAME (type);
10301 if (name
10302 && TREE_CODE (name) == TYPE_DECL)
10303 name = DECL_NAME (name);
10304 add_name_attribute (mod_type_die,
10305 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10308 if (qualified_type)
10309 equate_type_number_to_die (qualified_type, mod_type_die);
10311 if (item_type)
10312 /* We must do this after the equate_type_number_to_die call, in case
10313 this is a recursive type. This ensures that the modified_type_die
10314 recursion will terminate even if the type is recursive. Recursive
10315 types are possible in Ada. */
10316 sub_die = modified_type_die (item_type,
10317 TYPE_READONLY (item_type),
10318 TYPE_VOLATILE (item_type),
10319 context_die);
10321 if (sub_die != NULL)
10322 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10324 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10325 if (TYPE_ARTIFICIAL (type))
10326 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10328 return mod_type_die;
10331 /* Generate DIEs for the generic parameters of T.
10332 T must be either a generic type or a generic function.
10333 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10335 static void
10336 gen_generic_params_dies (tree t)
10338 tree parms, args;
10339 int parms_num, i;
10340 dw_die_ref die = NULL;
10341 int non_default;
10343 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10344 return;
10346 if (TYPE_P (t))
10347 die = lookup_type_die (t);
10348 else if (DECL_P (t))
10349 die = lookup_decl_die (t);
10351 gcc_assert (die);
10353 parms = lang_hooks.get_innermost_generic_parms (t);
10354 if (!parms)
10355 /* T has no generic parameter. It means T is neither a generic type
10356 or function. End of story. */
10357 return;
10359 parms_num = TREE_VEC_LENGTH (parms);
10360 args = lang_hooks.get_innermost_generic_args (t);
10361 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10362 non_default = int_cst_value (TREE_CHAIN (args));
10363 else
10364 non_default = TREE_VEC_LENGTH (args);
10365 for (i = 0; i < parms_num; i++)
10367 tree parm, arg, arg_pack_elems;
10368 dw_die_ref parm_die;
10370 parm = TREE_VEC_ELT (parms, i);
10371 arg = TREE_VEC_ELT (args, i);
10372 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10373 gcc_assert (parm && TREE_VALUE (parm) && arg);
10375 if (parm && TREE_VALUE (parm) && arg)
10377 /* If PARM represents a template parameter pack,
10378 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10379 by DW_TAG_template_*_parameter DIEs for the argument
10380 pack elements of ARG. Note that ARG would then be
10381 an argument pack. */
10382 if (arg_pack_elems)
10383 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10384 arg_pack_elems,
10385 die);
10386 else
10387 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10388 true /* emit name */, die);
10389 if (i >= non_default)
10390 add_AT_flag (parm_die, DW_AT_default_value, 1);
10395 /* Create and return a DIE for PARM which should be
10396 the representation of a generic type parameter.
10397 For instance, in the C++ front end, PARM would be a template parameter.
10398 ARG is the argument to PARM.
10399 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10400 name of the PARM.
10401 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10402 as a child node. */
10404 static dw_die_ref
10405 generic_parameter_die (tree parm, tree arg,
10406 bool emit_name_p,
10407 dw_die_ref parent_die)
10409 dw_die_ref tmpl_die = NULL;
10410 const char *name = NULL;
10412 if (!parm || !DECL_NAME (parm) || !arg)
10413 return NULL;
10415 /* We support non-type generic parameters and arguments,
10416 type generic parameters and arguments, as well as
10417 generic generic parameters (a.k.a. template template parameters in C++)
10418 and arguments. */
10419 if (TREE_CODE (parm) == PARM_DECL)
10420 /* PARM is a nontype generic parameter */
10421 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10422 else if (TREE_CODE (parm) == TYPE_DECL)
10423 /* PARM is a type generic parameter. */
10424 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10425 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10426 /* PARM is a generic generic parameter.
10427 Its DIE is a GNU extension. It shall have a
10428 DW_AT_name attribute to represent the name of the template template
10429 parameter, and a DW_AT_GNU_template_name attribute to represent the
10430 name of the template template argument. */
10431 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10432 parent_die, parm);
10433 else
10434 gcc_unreachable ();
10436 if (tmpl_die)
10438 tree tmpl_type;
10440 /* If PARM is a generic parameter pack, it means we are
10441 emitting debug info for a template argument pack element.
10442 In other terms, ARG is a template argument pack element.
10443 In that case, we don't emit any DW_AT_name attribute for
10444 the die. */
10445 if (emit_name_p)
10447 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10448 gcc_assert (name);
10449 add_AT_string (tmpl_die, DW_AT_name, name);
10452 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10454 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10455 TMPL_DIE should have a child DW_AT_type attribute that is set
10456 to the type of the argument to PARM, which is ARG.
10457 If PARM is a type generic parameter, TMPL_DIE should have a
10458 child DW_AT_type that is set to ARG. */
10459 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10460 add_type_attribute (tmpl_die, tmpl_type, 0,
10461 TREE_THIS_VOLATILE (tmpl_type),
10462 parent_die);
10464 else
10466 /* So TMPL_DIE is a DIE representing a
10467 a generic generic template parameter, a.k.a template template
10468 parameter in C++ and arg is a template. */
10470 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10471 to the name of the argument. */
10472 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10473 if (name)
10474 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10477 if (TREE_CODE (parm) == PARM_DECL)
10478 /* So PARM is a non-type generic parameter.
10479 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10480 attribute of TMPL_DIE which value represents the value
10481 of ARG.
10482 We must be careful here:
10483 The value of ARG might reference some function decls.
10484 We might currently be emitting debug info for a generic
10485 type and types are emitted before function decls, we don't
10486 know if the function decls referenced by ARG will actually be
10487 emitted after cgraph computations.
10488 So must defer the generation of the DW_AT_const_value to
10489 after cgraph is ready. */
10490 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10493 return tmpl_die;
10496 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10497 PARM_PACK must be a template parameter pack. The returned DIE
10498 will be child DIE of PARENT_DIE. */
10500 static dw_die_ref
10501 template_parameter_pack_die (tree parm_pack,
10502 tree parm_pack_args,
10503 dw_die_ref parent_die)
10505 dw_die_ref die;
10506 int j;
10508 gcc_assert (parent_die && parm_pack);
10510 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10511 add_name_and_src_coords_attributes (die, parm_pack);
10512 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10513 generic_parameter_die (parm_pack,
10514 TREE_VEC_ELT (parm_pack_args, j),
10515 false /* Don't emit DW_AT_name */,
10516 die);
10517 return die;
10520 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10521 an enumerated type. */
10523 static inline int
10524 type_is_enum (const_tree type)
10526 return TREE_CODE (type) == ENUMERAL_TYPE;
10529 /* Return the DBX register number described by a given RTL node. */
10531 static unsigned int
10532 dbx_reg_number (const_rtx rtl)
10534 unsigned regno = REGNO (rtl);
10536 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10538 #ifdef LEAF_REG_REMAP
10539 if (crtl->uses_only_leaf_regs)
10541 int leaf_reg = LEAF_REG_REMAP (regno);
10542 if (leaf_reg != -1)
10543 regno = (unsigned) leaf_reg;
10545 #endif
10547 regno = DBX_REGISTER_NUMBER (regno);
10548 gcc_assert (regno != INVALID_REGNUM);
10549 return regno;
10552 /* Optionally add a DW_OP_piece term to a location description expression.
10553 DW_OP_piece is only added if the location description expression already
10554 doesn't end with DW_OP_piece. */
10556 static void
10557 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10559 dw_loc_descr_ref loc;
10561 if (*list_head != NULL)
10563 /* Find the end of the chain. */
10564 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10567 if (loc->dw_loc_opc != DW_OP_piece)
10568 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10572 /* Return a location descriptor that designates a machine register or
10573 zero if there is none. */
10575 static dw_loc_descr_ref
10576 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10578 rtx regs;
10580 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10581 return 0;
10583 /* We only use "frame base" when we're sure we're talking about the
10584 post-prologue local stack frame. We do this by *not* running
10585 register elimination until this point, and recognizing the special
10586 argument pointer and soft frame pointer rtx's.
10587 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10588 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10589 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10591 dw_loc_descr_ref result = NULL;
10593 if (dwarf_version >= 4 || !dwarf_strict)
10595 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10596 initialized);
10597 if (result)
10598 add_loc_descr (&result,
10599 new_loc_descr (DW_OP_stack_value, 0, 0));
10601 return result;
10604 regs = targetm.dwarf_register_span (rtl);
10606 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10607 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10608 else
10610 unsigned int dbx_regnum = dbx_reg_number (rtl);
10611 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10612 return 0;
10613 return one_reg_loc_descriptor (dbx_regnum, initialized);
10617 /* Return a location descriptor that designates a machine register for
10618 a given hard register number. */
10620 static dw_loc_descr_ref
10621 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10623 dw_loc_descr_ref reg_loc_descr;
10625 if (regno <= 31)
10626 reg_loc_descr
10627 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10628 else
10629 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10631 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10632 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10634 return reg_loc_descr;
10637 /* Given an RTL of a register, return a location descriptor that
10638 designates a value that spans more than one register. */
10640 static dw_loc_descr_ref
10641 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10642 enum var_init_status initialized)
10644 int size, i;
10645 dw_loc_descr_ref loc_result = NULL;
10647 /* Simple, contiguous registers. */
10648 if (regs == NULL_RTX)
10650 unsigned reg = REGNO (rtl);
10651 int nregs;
10653 #ifdef LEAF_REG_REMAP
10654 if (crtl->uses_only_leaf_regs)
10656 int leaf_reg = LEAF_REG_REMAP (reg);
10657 if (leaf_reg != -1)
10658 reg = (unsigned) leaf_reg;
10660 #endif
10662 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10663 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10665 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10667 loc_result = NULL;
10668 while (nregs--)
10670 dw_loc_descr_ref t;
10672 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10673 VAR_INIT_STATUS_INITIALIZED);
10674 add_loc_descr (&loc_result, t);
10675 add_loc_descr_op_piece (&loc_result, size);
10676 ++reg;
10678 return loc_result;
10681 /* Now onto stupid register sets in non contiguous locations. */
10683 gcc_assert (GET_CODE (regs) == PARALLEL);
10685 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10686 loc_result = NULL;
10688 for (i = 0; i < XVECLEN (regs, 0); ++i)
10690 dw_loc_descr_ref t;
10692 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10693 VAR_INIT_STATUS_INITIALIZED);
10694 add_loc_descr (&loc_result, t);
10695 add_loc_descr_op_piece (&loc_result, size);
10698 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10699 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10700 return loc_result;
10703 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10705 /* Return a location descriptor that designates a constant i,
10706 as a compound operation from constant (i >> shift), constant shift
10707 and DW_OP_shl. */
10709 static dw_loc_descr_ref
10710 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10712 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10713 add_loc_descr (&ret, int_loc_descriptor (shift));
10714 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10715 return ret;
10718 /* Return a location descriptor that designates a constant. */
10720 static dw_loc_descr_ref
10721 int_loc_descriptor (HOST_WIDE_INT i)
10723 enum dwarf_location_atom op;
10725 /* Pick the smallest representation of a constant, rather than just
10726 defaulting to the LEB encoding. */
10727 if (i >= 0)
10729 int clz = clz_hwi (i);
10730 int ctz = ctz_hwi (i);
10731 if (i <= 31)
10732 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10733 else if (i <= 0xff)
10734 op = DW_OP_const1u;
10735 else if (i <= 0xffff)
10736 op = DW_OP_const2u;
10737 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10738 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10739 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10740 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10741 while DW_OP_const4u is 5 bytes. */
10742 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10743 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10744 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10745 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10746 while DW_OP_const4u is 5 bytes. */
10747 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10748 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10749 op = DW_OP_const4u;
10750 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10751 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10752 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10753 while DW_OP_constu of constant >= 0x100000000 takes at least
10754 6 bytes. */
10755 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10756 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10757 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10758 >= HOST_BITS_PER_WIDE_INT)
10759 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10760 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10761 while DW_OP_constu takes in this case at least 6 bytes. */
10762 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10763 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10764 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10765 && size_of_uleb128 (i) > 6)
10766 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10767 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10768 else
10769 op = DW_OP_constu;
10771 else
10773 if (i >= -0x80)
10774 op = DW_OP_const1s;
10775 else if (i >= -0x8000)
10776 op = DW_OP_const2s;
10777 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10779 if (size_of_int_loc_descriptor (i) < 5)
10781 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10782 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10783 return ret;
10785 op = DW_OP_const4s;
10787 else
10789 if (size_of_int_loc_descriptor (i)
10790 < (unsigned long) 1 + size_of_sleb128 (i))
10792 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10793 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10794 return ret;
10796 op = DW_OP_consts;
10800 return new_loc_descr (op, i, 0);
10803 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10804 without actually allocating it. */
10806 static unsigned long
10807 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10809 return size_of_int_loc_descriptor (i >> shift)
10810 + size_of_int_loc_descriptor (shift)
10811 + 1;
10814 /* Return size_of_locs (int_loc_descriptor (i)) without
10815 actually allocating it. */
10817 static unsigned long
10818 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10820 unsigned long s;
10822 if (i >= 0)
10824 int clz, ctz;
10825 if (i <= 31)
10826 return 1;
10827 else if (i <= 0xff)
10828 return 2;
10829 else if (i <= 0xffff)
10830 return 3;
10831 clz = clz_hwi (i);
10832 ctz = ctz_hwi (i);
10833 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10834 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10835 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10836 - clz - 5);
10837 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10838 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10839 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10840 - clz - 8);
10841 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10842 return 5;
10843 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10844 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10845 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10846 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10847 - clz - 8);
10848 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10849 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10850 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10851 - clz - 16);
10852 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10853 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10854 && s > 6)
10855 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10856 - clz - 32);
10857 else
10858 return 1 + s;
10860 else
10862 if (i >= -0x80)
10863 return 2;
10864 else if (i >= -0x8000)
10865 return 3;
10866 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10868 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10870 s = size_of_int_loc_descriptor (-i) + 1;
10871 if (s < 5)
10872 return s;
10874 return 5;
10876 else
10878 unsigned long r = 1 + size_of_sleb128 (i);
10879 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10881 s = size_of_int_loc_descriptor (-i) + 1;
10882 if (s < r)
10883 return s;
10885 return r;
10890 /* Return loc description representing "address" of integer value.
10891 This can appear only as toplevel expression. */
10893 static dw_loc_descr_ref
10894 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10896 int litsize;
10897 dw_loc_descr_ref loc_result = NULL;
10899 if (!(dwarf_version >= 4 || !dwarf_strict))
10900 return NULL;
10902 litsize = size_of_int_loc_descriptor (i);
10903 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10904 is more compact. For DW_OP_stack_value we need:
10905 litsize + 1 (DW_OP_stack_value)
10906 and for DW_OP_implicit_value:
10907 1 (DW_OP_implicit_value) + 1 (length) + size. */
10908 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10910 loc_result = int_loc_descriptor (i);
10911 add_loc_descr (&loc_result,
10912 new_loc_descr (DW_OP_stack_value, 0, 0));
10913 return loc_result;
10916 loc_result = new_loc_descr (DW_OP_implicit_value,
10917 size, 0);
10918 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10919 loc_result->dw_loc_oprnd2.v.val_int = i;
10920 return loc_result;
10923 /* Return a location descriptor that designates a base+offset location. */
10925 static dw_loc_descr_ref
10926 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10927 enum var_init_status initialized)
10929 unsigned int regno;
10930 dw_loc_descr_ref result;
10931 dw_fde_ref fde = cfun->fde;
10933 /* We only use "frame base" when we're sure we're talking about the
10934 post-prologue local stack frame. We do this by *not* running
10935 register elimination until this point, and recognizing the special
10936 argument pointer and soft frame pointer rtx's. */
10937 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10939 rtx elim = (ira_use_lra_p
10940 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10941 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10943 if (elim != reg)
10945 if (GET_CODE (elim) == PLUS)
10947 offset += INTVAL (XEXP (elim, 1));
10948 elim = XEXP (elim, 0);
10950 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10951 && (elim == hard_frame_pointer_rtx
10952 || elim == stack_pointer_rtx))
10953 || elim == (frame_pointer_needed
10954 ? hard_frame_pointer_rtx
10955 : stack_pointer_rtx));
10957 /* If drap register is used to align stack, use frame
10958 pointer + offset to access stack variables. If stack
10959 is aligned without drap, use stack pointer + offset to
10960 access stack variables. */
10961 if (crtl->stack_realign_tried
10962 && reg == frame_pointer_rtx)
10964 int base_reg
10965 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10966 ? HARD_FRAME_POINTER_REGNUM
10967 : REGNO (elim));
10968 return new_reg_loc_descr (base_reg, offset);
10971 gcc_assert (frame_pointer_fb_offset_valid);
10972 offset += frame_pointer_fb_offset;
10973 return new_loc_descr (DW_OP_fbreg, offset, 0);
10977 regno = REGNO (reg);
10978 #ifdef LEAF_REG_REMAP
10979 if (crtl->uses_only_leaf_regs)
10981 int leaf_reg = LEAF_REG_REMAP (regno);
10982 if (leaf_reg != -1)
10983 regno = (unsigned) leaf_reg;
10985 #endif
10986 regno = DWARF_FRAME_REGNUM (regno);
10988 if (!optimize && fde
10989 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10991 /* Use cfa+offset to represent the location of arguments passed
10992 on the stack when drap is used to align stack.
10993 Only do this when not optimizing, for optimized code var-tracking
10994 is supposed to track where the arguments live and the register
10995 used as vdrap or drap in some spot might be used for something
10996 else in other part of the routine. */
10997 return new_loc_descr (DW_OP_fbreg, offset, 0);
11000 if (regno <= 31)
11001 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11002 offset, 0);
11003 else
11004 result = new_loc_descr (DW_OP_bregx, regno, offset);
11006 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11007 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11009 return result;
11012 /* Return true if this RTL expression describes a base+offset calculation. */
11014 static inline int
11015 is_based_loc (const_rtx rtl)
11017 return (GET_CODE (rtl) == PLUS
11018 && ((REG_P (XEXP (rtl, 0))
11019 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11020 && CONST_INT_P (XEXP (rtl, 1)))));
11023 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11024 failed. */
11026 static dw_loc_descr_ref
11027 tls_mem_loc_descriptor (rtx mem)
11029 tree base;
11030 dw_loc_descr_ref loc_result;
11032 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11033 return NULL;
11035 base = get_base_address (MEM_EXPR (mem));
11036 if (base == NULL
11037 || TREE_CODE (base) != VAR_DECL
11038 || !DECL_THREAD_LOCAL_P (base))
11039 return NULL;
11041 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11042 if (loc_result == NULL)
11043 return NULL;
11045 if (MEM_OFFSET (mem))
11046 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11048 return loc_result;
11051 /* Output debug info about reason why we failed to expand expression as dwarf
11052 expression. */
11054 static void
11055 expansion_failed (tree expr, rtx rtl, char const *reason)
11057 if (dump_file && (dump_flags & TDF_DETAILS))
11059 fprintf (dump_file, "Failed to expand as dwarf: ");
11060 if (expr)
11061 print_generic_expr (dump_file, expr, dump_flags);
11062 if (rtl)
11064 fprintf (dump_file, "\n");
11065 print_rtl (dump_file, rtl);
11067 fprintf (dump_file, "\nReason: %s\n", reason);
11071 /* Helper function for const_ok_for_output, called either directly
11072 or via for_each_rtx. */
11074 static int
11075 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11077 rtx rtl = *rtlp;
11079 if (GET_CODE (rtl) == UNSPEC)
11081 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11082 we can't express it in the debug info. */
11083 #ifdef ENABLE_CHECKING
11084 /* Don't complain about TLS UNSPECs, those are just too hard to
11085 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11086 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11087 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11088 if (XVECLEN (rtl, 0) == 0
11089 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11090 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11091 inform (current_function_decl
11092 ? DECL_SOURCE_LOCATION (current_function_decl)
11093 : UNKNOWN_LOCATION,
11094 #if NUM_UNSPEC_VALUES > 0
11095 "non-delegitimized UNSPEC %s (%d) found in variable location",
11096 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11097 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11098 XINT (rtl, 1));
11099 #else
11100 "non-delegitimized UNSPEC %d found in variable location",
11101 XINT (rtl, 1));
11102 #endif
11103 #endif
11104 expansion_failed (NULL_TREE, rtl,
11105 "UNSPEC hasn't been delegitimized.\n");
11106 return 1;
11109 if (targetm.const_not_ok_for_debug_p (rtl))
11111 expansion_failed (NULL_TREE, rtl,
11112 "Expression rejected for debug by the backend.\n");
11113 return 1;
11116 if (GET_CODE (rtl) != SYMBOL_REF)
11117 return 0;
11119 if (CONSTANT_POOL_ADDRESS_P (rtl))
11121 bool marked;
11122 get_pool_constant_mark (rtl, &marked);
11123 /* If all references to this pool constant were optimized away,
11124 it was not output and thus we can't represent it. */
11125 if (!marked)
11127 expansion_failed (NULL_TREE, rtl,
11128 "Constant was removed from constant pool.\n");
11129 return 1;
11133 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11134 return 1;
11136 /* Avoid references to external symbols in debug info, on several targets
11137 the linker might even refuse to link when linking a shared library,
11138 and in many other cases the relocations for .debug_info/.debug_loc are
11139 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11140 to be defined within the same shared library or executable are fine. */
11141 if (SYMBOL_REF_EXTERNAL_P (rtl))
11143 tree decl = SYMBOL_REF_DECL (rtl);
11145 if (decl == NULL || !targetm.binds_local_p (decl))
11147 expansion_failed (NULL_TREE, rtl,
11148 "Symbol not defined in current TU.\n");
11149 return 1;
11153 return 0;
11156 /* Return true if constant RTL can be emitted in DW_OP_addr or
11157 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11158 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11160 static bool
11161 const_ok_for_output (rtx rtl)
11163 if (GET_CODE (rtl) == SYMBOL_REF)
11164 return const_ok_for_output_1 (&rtl, NULL) == 0;
11166 if (GET_CODE (rtl) == CONST)
11167 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11169 return true;
11172 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11173 if possible, NULL otherwise. */
11175 static dw_die_ref
11176 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11178 dw_die_ref type_die;
11179 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11181 if (type == NULL)
11182 return NULL;
11183 switch (TREE_CODE (type))
11185 case INTEGER_TYPE:
11186 case REAL_TYPE:
11187 break;
11188 default:
11189 return NULL;
11191 type_die = lookup_type_die (type);
11192 if (!type_die)
11193 type_die = modified_type_die (type, false, false, comp_unit_die ());
11194 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11195 return NULL;
11196 return type_die;
11199 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11200 type matching MODE, or, if MODE is narrower than or as wide as
11201 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11202 possible. */
11204 static dw_loc_descr_ref
11205 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11207 enum machine_mode outer_mode = mode;
11208 dw_die_ref type_die;
11209 dw_loc_descr_ref cvt;
11211 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11213 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11214 return op;
11216 type_die = base_type_for_mode (outer_mode, 1);
11217 if (type_die == NULL)
11218 return NULL;
11219 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11220 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11221 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11222 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11223 add_loc_descr (&op, cvt);
11224 return op;
11227 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11229 static dw_loc_descr_ref
11230 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11231 dw_loc_descr_ref op1)
11233 dw_loc_descr_ref ret = op0;
11234 add_loc_descr (&ret, op1);
11235 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11236 if (STORE_FLAG_VALUE != 1)
11238 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11239 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11241 return ret;
11244 /* Return location descriptor for signed comparison OP RTL. */
11246 static dw_loc_descr_ref
11247 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11248 enum machine_mode mem_mode)
11250 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11251 dw_loc_descr_ref op0, op1;
11252 int shift;
11254 if (op_mode == VOIDmode)
11255 op_mode = GET_MODE (XEXP (rtl, 1));
11256 if (op_mode == VOIDmode)
11257 return NULL;
11259 if (dwarf_strict
11260 && (GET_MODE_CLASS (op_mode) != MODE_INT
11261 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11262 return NULL;
11264 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11265 VAR_INIT_STATUS_INITIALIZED);
11266 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11267 VAR_INIT_STATUS_INITIALIZED);
11269 if (op0 == NULL || op1 == NULL)
11270 return NULL;
11272 if (GET_MODE_CLASS (op_mode) != MODE_INT
11273 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11274 return compare_loc_descriptor (op, op0, op1);
11276 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11278 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11279 dw_loc_descr_ref cvt;
11281 if (type_die == NULL)
11282 return NULL;
11283 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11284 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11285 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11286 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11287 add_loc_descr (&op0, cvt);
11288 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11289 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11290 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11291 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11292 add_loc_descr (&op1, cvt);
11293 return compare_loc_descriptor (op, op0, op1);
11296 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11297 /* For eq/ne, if the operands are known to be zero-extended,
11298 there is no need to do the fancy shifting up. */
11299 if (op == DW_OP_eq || op == DW_OP_ne)
11301 dw_loc_descr_ref last0, last1;
11302 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11304 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11306 /* deref_size zero extends, and for constants we can check
11307 whether they are zero extended or not. */
11308 if (((last0->dw_loc_opc == DW_OP_deref_size
11309 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11310 || (CONST_INT_P (XEXP (rtl, 0))
11311 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11312 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11313 && ((last1->dw_loc_opc == DW_OP_deref_size
11314 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11315 || (CONST_INT_P (XEXP (rtl, 1))
11316 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11317 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11318 return compare_loc_descriptor (op, op0, op1);
11320 /* EQ/NE comparison against constant in narrower type than
11321 DWARF2_ADDR_SIZE can be performed either as
11322 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11323 DW_OP_{eq,ne}
11325 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11326 DW_OP_{eq,ne}. Pick whatever is shorter. */
11327 if (CONST_INT_P (XEXP (rtl, 1))
11328 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11329 && (size_of_int_loc_descriptor (shift) + 1
11330 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11331 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11332 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11333 & GET_MODE_MASK (op_mode))))
11335 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11336 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11337 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11338 & GET_MODE_MASK (op_mode));
11339 return compare_loc_descriptor (op, op0, op1);
11342 add_loc_descr (&op0, int_loc_descriptor (shift));
11343 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11344 if (CONST_INT_P (XEXP (rtl, 1)))
11345 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11346 else
11348 add_loc_descr (&op1, int_loc_descriptor (shift));
11349 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11351 return compare_loc_descriptor (op, op0, op1);
11354 /* Return location descriptor for unsigned comparison OP RTL. */
11356 static dw_loc_descr_ref
11357 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11358 enum machine_mode mem_mode)
11360 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11361 dw_loc_descr_ref op0, op1;
11363 if (op_mode == VOIDmode)
11364 op_mode = GET_MODE (XEXP (rtl, 1));
11365 if (op_mode == VOIDmode)
11366 return NULL;
11367 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11368 return NULL;
11370 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11371 return NULL;
11373 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11374 VAR_INIT_STATUS_INITIALIZED);
11375 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11376 VAR_INIT_STATUS_INITIALIZED);
11378 if (op0 == NULL || op1 == NULL)
11379 return NULL;
11381 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11383 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11384 dw_loc_descr_ref last0, last1;
11385 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11387 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11389 if (CONST_INT_P (XEXP (rtl, 0)))
11390 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11391 /* deref_size zero extends, so no need to mask it again. */
11392 else if (last0->dw_loc_opc != DW_OP_deref_size
11393 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11395 add_loc_descr (&op0, int_loc_descriptor (mask));
11396 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11398 if (CONST_INT_P (XEXP (rtl, 1)))
11399 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11400 /* deref_size zero extends, so no need to mask it again. */
11401 else if (last1->dw_loc_opc != DW_OP_deref_size
11402 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11404 add_loc_descr (&op1, int_loc_descriptor (mask));
11405 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11408 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11410 HOST_WIDE_INT bias = 1;
11411 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11412 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11413 if (CONST_INT_P (XEXP (rtl, 1)))
11414 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11415 + INTVAL (XEXP (rtl, 1)));
11416 else
11417 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11418 bias, 0));
11420 return compare_loc_descriptor (op, op0, op1);
11423 /* Return location descriptor for {U,S}{MIN,MAX}. */
11425 static dw_loc_descr_ref
11426 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11427 enum machine_mode mem_mode)
11429 enum dwarf_location_atom op;
11430 dw_loc_descr_ref op0, op1, ret;
11431 dw_loc_descr_ref bra_node, drop_node;
11433 if (dwarf_strict
11434 && (GET_MODE_CLASS (mode) != MODE_INT
11435 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11436 return NULL;
11438 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11439 VAR_INIT_STATUS_INITIALIZED);
11440 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11441 VAR_INIT_STATUS_INITIALIZED);
11443 if (op0 == NULL || op1 == NULL)
11444 return NULL;
11446 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11447 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11448 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11449 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11451 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11453 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11454 add_loc_descr (&op0, int_loc_descriptor (mask));
11455 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11456 add_loc_descr (&op1, int_loc_descriptor (mask));
11457 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11459 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11461 HOST_WIDE_INT bias = 1;
11462 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11463 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11464 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11467 else if (GET_MODE_CLASS (mode) == MODE_INT
11468 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11470 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11471 add_loc_descr (&op0, int_loc_descriptor (shift));
11472 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11473 add_loc_descr (&op1, int_loc_descriptor (shift));
11474 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11476 else if (GET_MODE_CLASS (mode) == MODE_INT
11477 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11479 dw_die_ref type_die = base_type_for_mode (mode, 0);
11480 dw_loc_descr_ref cvt;
11481 if (type_die == NULL)
11482 return NULL;
11483 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11484 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11485 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11486 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11487 add_loc_descr (&op0, cvt);
11488 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11489 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11490 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11491 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11492 add_loc_descr (&op1, cvt);
11495 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11496 op = DW_OP_lt;
11497 else
11498 op = DW_OP_gt;
11499 ret = op0;
11500 add_loc_descr (&ret, op1);
11501 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11502 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11503 add_loc_descr (&ret, bra_node);
11504 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11505 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11506 add_loc_descr (&ret, drop_node);
11507 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11508 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11509 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11510 && GET_MODE_CLASS (mode) == MODE_INT
11511 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11512 ret = convert_descriptor_to_mode (mode, ret);
11513 return ret;
11516 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11517 but after converting arguments to type_die, afterwards
11518 convert back to unsigned. */
11520 static dw_loc_descr_ref
11521 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11522 enum machine_mode mode, enum machine_mode mem_mode)
11524 dw_loc_descr_ref cvt, op0, op1;
11526 if (type_die == NULL)
11527 return NULL;
11528 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11529 VAR_INIT_STATUS_INITIALIZED);
11530 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11531 VAR_INIT_STATUS_INITIALIZED);
11532 if (op0 == NULL || op1 == NULL)
11533 return NULL;
11534 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11535 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11536 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11537 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11538 add_loc_descr (&op0, cvt);
11539 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11540 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11541 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11542 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11543 add_loc_descr (&op1, cvt);
11544 add_loc_descr (&op0, op1);
11545 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11546 return convert_descriptor_to_mode (mode, op0);
11549 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11550 const0 is DW_OP_lit0 or corresponding typed constant,
11551 const1 is DW_OP_lit1 or corresponding typed constant
11552 and constMSB is constant with just the MSB bit set
11553 for the mode):
11554 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11555 L1: const0 DW_OP_swap
11556 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11557 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11558 L3: DW_OP_drop
11559 L4: DW_OP_nop
11561 CTZ is similar:
11562 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11563 L1: const0 DW_OP_swap
11564 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11565 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11566 L3: DW_OP_drop
11567 L4: DW_OP_nop
11569 FFS is similar:
11570 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11571 L1: const1 DW_OP_swap
11572 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11573 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11574 L3: DW_OP_drop
11575 L4: DW_OP_nop */
11577 static dw_loc_descr_ref
11578 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11579 enum machine_mode mem_mode)
11581 dw_loc_descr_ref op0, ret, tmp;
11582 HOST_WIDE_INT valv;
11583 dw_loc_descr_ref l1jump, l1label;
11584 dw_loc_descr_ref l2jump, l2label;
11585 dw_loc_descr_ref l3jump, l3label;
11586 dw_loc_descr_ref l4jump, l4label;
11587 rtx msb;
11589 if (GET_MODE_CLASS (mode) != MODE_INT
11590 || GET_MODE (XEXP (rtl, 0)) != mode
11591 || (GET_CODE (rtl) == CLZ
11592 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11593 return NULL;
11595 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11596 VAR_INIT_STATUS_INITIALIZED);
11597 if (op0 == NULL)
11598 return NULL;
11599 ret = op0;
11600 if (GET_CODE (rtl) == CLZ)
11602 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11603 valv = GET_MODE_BITSIZE (mode);
11605 else if (GET_CODE (rtl) == FFS)
11606 valv = 0;
11607 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11608 valv = GET_MODE_BITSIZE (mode);
11609 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11610 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11611 add_loc_descr (&ret, l1jump);
11612 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11613 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11614 VAR_INIT_STATUS_INITIALIZED);
11615 if (tmp == NULL)
11616 return NULL;
11617 add_loc_descr (&ret, tmp);
11618 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11619 add_loc_descr (&ret, l4jump);
11620 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11621 ? const1_rtx : const0_rtx,
11622 mode, mem_mode,
11623 VAR_INIT_STATUS_INITIALIZED);
11624 if (l1label == NULL)
11625 return NULL;
11626 add_loc_descr (&ret, l1label);
11627 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11628 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11629 add_loc_descr (&ret, l2label);
11630 if (GET_CODE (rtl) != CLZ)
11631 msb = const1_rtx;
11632 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11633 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11634 << (GET_MODE_BITSIZE (mode) - 1));
11635 else
11636 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11637 << (GET_MODE_BITSIZE (mode)
11638 - HOST_BITS_PER_WIDE_INT - 1), mode);
11639 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11640 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11641 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11642 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11643 else
11644 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11645 VAR_INIT_STATUS_INITIALIZED);
11646 if (tmp == NULL)
11647 return NULL;
11648 add_loc_descr (&ret, tmp);
11649 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11650 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11651 add_loc_descr (&ret, l3jump);
11652 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11653 VAR_INIT_STATUS_INITIALIZED);
11654 if (tmp == NULL)
11655 return NULL;
11656 add_loc_descr (&ret, tmp);
11657 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11658 ? DW_OP_shl : DW_OP_shr, 0, 0));
11659 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11660 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11661 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11662 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11663 add_loc_descr (&ret, l2jump);
11664 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11665 add_loc_descr (&ret, l3label);
11666 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11667 add_loc_descr (&ret, l4label);
11668 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11669 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11670 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11671 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11672 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11673 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11674 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11675 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11676 return ret;
11679 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11680 const1 is DW_OP_lit1 or corresponding typed constant):
11681 const0 DW_OP_swap
11682 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11683 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11684 L2: DW_OP_drop
11686 PARITY is similar:
11687 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11688 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11689 L2: DW_OP_drop */
11691 static dw_loc_descr_ref
11692 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11693 enum machine_mode mem_mode)
11695 dw_loc_descr_ref op0, ret, tmp;
11696 dw_loc_descr_ref l1jump, l1label;
11697 dw_loc_descr_ref l2jump, l2label;
11699 if (GET_MODE_CLASS (mode) != MODE_INT
11700 || GET_MODE (XEXP (rtl, 0)) != mode)
11701 return NULL;
11703 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11704 VAR_INIT_STATUS_INITIALIZED);
11705 if (op0 == NULL)
11706 return NULL;
11707 ret = op0;
11708 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11709 VAR_INIT_STATUS_INITIALIZED);
11710 if (tmp == NULL)
11711 return NULL;
11712 add_loc_descr (&ret, tmp);
11713 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11714 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11715 add_loc_descr (&ret, l1label);
11716 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11717 add_loc_descr (&ret, l2jump);
11718 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11719 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11720 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11721 VAR_INIT_STATUS_INITIALIZED);
11722 if (tmp == NULL)
11723 return NULL;
11724 add_loc_descr (&ret, tmp);
11725 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11726 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11727 ? DW_OP_plus : DW_OP_xor, 0, 0));
11728 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11729 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11730 VAR_INIT_STATUS_INITIALIZED);
11731 add_loc_descr (&ret, tmp);
11732 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11733 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11734 add_loc_descr (&ret, l1jump);
11735 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11736 add_loc_descr (&ret, l2label);
11737 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11738 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11739 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11740 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11741 return ret;
11744 /* BSWAP (constS is initial shift count, either 56 or 24):
11745 constS const0
11746 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11747 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11748 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11749 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11750 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11752 static dw_loc_descr_ref
11753 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11754 enum machine_mode mem_mode)
11756 dw_loc_descr_ref op0, ret, tmp;
11757 dw_loc_descr_ref l1jump, l1label;
11758 dw_loc_descr_ref l2jump, l2label;
11760 if (GET_MODE_CLASS (mode) != MODE_INT
11761 || BITS_PER_UNIT != 8
11762 || (GET_MODE_BITSIZE (mode) != 32
11763 && GET_MODE_BITSIZE (mode) != 64))
11764 return NULL;
11766 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11767 VAR_INIT_STATUS_INITIALIZED);
11768 if (op0 == NULL)
11769 return NULL;
11771 ret = op0;
11772 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11773 mode, mem_mode,
11774 VAR_INIT_STATUS_INITIALIZED);
11775 if (tmp == NULL)
11776 return NULL;
11777 add_loc_descr (&ret, tmp);
11778 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11779 VAR_INIT_STATUS_INITIALIZED);
11780 if (tmp == NULL)
11781 return NULL;
11782 add_loc_descr (&ret, tmp);
11783 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11784 add_loc_descr (&ret, l1label);
11785 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11786 mode, mem_mode,
11787 VAR_INIT_STATUS_INITIALIZED);
11788 add_loc_descr (&ret, tmp);
11789 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11790 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11791 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11792 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11793 VAR_INIT_STATUS_INITIALIZED);
11794 if (tmp == NULL)
11795 return NULL;
11796 add_loc_descr (&ret, tmp);
11797 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11798 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11799 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11800 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11801 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11802 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11803 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11804 VAR_INIT_STATUS_INITIALIZED);
11805 add_loc_descr (&ret, tmp);
11806 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11807 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11808 add_loc_descr (&ret, l2jump);
11809 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11810 VAR_INIT_STATUS_INITIALIZED);
11811 add_loc_descr (&ret, tmp);
11812 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11813 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11814 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11815 add_loc_descr (&ret, l1jump);
11816 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11817 add_loc_descr (&ret, l2label);
11818 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11819 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11820 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11821 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11822 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11823 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11824 return ret;
11827 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11828 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11829 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11830 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11832 ROTATERT is similar:
11833 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11834 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11835 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11837 static dw_loc_descr_ref
11838 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11839 enum machine_mode mem_mode)
11841 rtx rtlop1 = XEXP (rtl, 1);
11842 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11843 int i;
11845 if (GET_MODE_CLASS (mode) != MODE_INT)
11846 return NULL;
11848 if (GET_MODE (rtlop1) != VOIDmode
11849 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11850 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11851 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11852 VAR_INIT_STATUS_INITIALIZED);
11853 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11854 VAR_INIT_STATUS_INITIALIZED);
11855 if (op0 == NULL || op1 == NULL)
11856 return NULL;
11857 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11858 for (i = 0; i < 2; i++)
11860 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11861 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11862 mode, mem_mode,
11863 VAR_INIT_STATUS_INITIALIZED);
11864 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11865 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11866 ? DW_OP_const4u
11867 : HOST_BITS_PER_WIDE_INT == 64
11868 ? DW_OP_const8u : DW_OP_constu,
11869 GET_MODE_MASK (mode), 0);
11870 else
11871 mask[i] = NULL;
11872 if (mask[i] == NULL)
11873 return NULL;
11874 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11876 ret = op0;
11877 add_loc_descr (&ret, op1);
11878 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11879 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11880 if (GET_CODE (rtl) == ROTATERT)
11882 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11883 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11884 GET_MODE_BITSIZE (mode), 0));
11886 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11887 if (mask[0] != NULL)
11888 add_loc_descr (&ret, mask[0]);
11889 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11890 if (mask[1] != NULL)
11892 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11893 add_loc_descr (&ret, mask[1]);
11894 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11896 if (GET_CODE (rtl) == ROTATE)
11898 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11899 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11900 GET_MODE_BITSIZE (mode), 0));
11902 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11903 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11904 return ret;
11907 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11908 for DEBUG_PARAMETER_REF RTL. */
11910 static dw_loc_descr_ref
11911 parameter_ref_descriptor (rtx rtl)
11913 dw_loc_descr_ref ret;
11914 dw_die_ref ref;
11916 if (dwarf_strict)
11917 return NULL;
11918 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11919 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11920 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11921 if (ref)
11923 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11924 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11925 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11927 else
11929 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11930 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11932 return ret;
11935 /* The following routine converts the RTL for a variable or parameter
11936 (resident in memory) into an equivalent Dwarf representation of a
11937 mechanism for getting the address of that same variable onto the top of a
11938 hypothetical "address evaluation" stack.
11940 When creating memory location descriptors, we are effectively transforming
11941 the RTL for a memory-resident object into its Dwarf postfix expression
11942 equivalent. This routine recursively descends an RTL tree, turning
11943 it into Dwarf postfix code as it goes.
11945 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11947 MEM_MODE is the mode of the memory reference, needed to handle some
11948 autoincrement addressing modes.
11950 Return 0 if we can't represent the location. */
11952 dw_loc_descr_ref
11953 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11954 enum machine_mode mem_mode,
11955 enum var_init_status initialized)
11957 dw_loc_descr_ref mem_loc_result = NULL;
11958 enum dwarf_location_atom op;
11959 dw_loc_descr_ref op0, op1;
11960 rtx inner = NULL_RTX;
11962 if (mode == VOIDmode)
11963 mode = GET_MODE (rtl);
11965 /* Note that for a dynamically sized array, the location we will generate a
11966 description of here will be the lowest numbered location which is
11967 actually within the array. That's *not* necessarily the same as the
11968 zeroth element of the array. */
11970 rtl = targetm.delegitimize_address (rtl);
11972 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11973 return NULL;
11975 switch (GET_CODE (rtl))
11977 case POST_INC:
11978 case POST_DEC:
11979 case POST_MODIFY:
11980 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11982 case SUBREG:
11983 /* The case of a subreg may arise when we have a local (register)
11984 variable or a formal (register) parameter which doesn't quite fill
11985 up an entire register. For now, just assume that it is
11986 legitimate to make the Dwarf info refer to the whole register which
11987 contains the given subreg. */
11988 if (!subreg_lowpart_p (rtl))
11989 break;
11990 inner = SUBREG_REG (rtl);
11991 case TRUNCATE:
11992 if (inner == NULL_RTX)
11993 inner = XEXP (rtl, 0);
11994 if (GET_MODE_CLASS (mode) == MODE_INT
11995 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11996 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11997 #ifdef POINTERS_EXTEND_UNSIGNED
11998 || (mode == Pmode && mem_mode != VOIDmode)
11999 #endif
12001 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12003 mem_loc_result = mem_loc_descriptor (inner,
12004 GET_MODE (inner),
12005 mem_mode, initialized);
12006 break;
12008 if (dwarf_strict)
12009 break;
12010 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12011 break;
12012 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12013 && (GET_MODE_CLASS (mode) != MODE_INT
12014 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12015 break;
12016 else
12018 dw_die_ref type_die;
12019 dw_loc_descr_ref cvt;
12021 mem_loc_result = mem_loc_descriptor (inner,
12022 GET_MODE (inner),
12023 mem_mode, initialized);
12024 if (mem_loc_result == NULL)
12025 break;
12026 type_die = base_type_for_mode (mode,
12027 GET_MODE_CLASS (mode) == MODE_INT);
12028 if (type_die == NULL)
12030 mem_loc_result = NULL;
12031 break;
12033 if (GET_MODE_SIZE (mode)
12034 != GET_MODE_SIZE (GET_MODE (inner)))
12035 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12036 else
12037 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12038 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12039 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12040 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12041 add_loc_descr (&mem_loc_result, cvt);
12043 break;
12045 case REG:
12046 if (GET_MODE_CLASS (mode) != MODE_INT
12047 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12048 && rtl != arg_pointer_rtx
12049 && rtl != frame_pointer_rtx
12050 #ifdef POINTERS_EXTEND_UNSIGNED
12051 && (mode != Pmode || mem_mode == VOIDmode)
12052 #endif
12055 dw_die_ref type_die;
12056 unsigned int dbx_regnum;
12058 if (dwarf_strict)
12059 break;
12060 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12061 break;
12062 type_die = base_type_for_mode (mode,
12063 GET_MODE_CLASS (mode) == MODE_INT);
12064 if (type_die == NULL)
12065 break;
12067 dbx_regnum = dbx_reg_number (rtl);
12068 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12069 break;
12070 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12071 dbx_regnum, 0);
12072 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12073 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12074 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12075 break;
12077 /* Whenever a register number forms a part of the description of the
12078 method for calculating the (dynamic) address of a memory resident
12079 object, DWARF rules require the register number be referred to as
12080 a "base register". This distinction is not based in any way upon
12081 what category of register the hardware believes the given register
12082 belongs to. This is strictly DWARF terminology we're dealing with
12083 here. Note that in cases where the location of a memory-resident
12084 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12085 OP_CONST (0)) the actual DWARF location descriptor that we generate
12086 may just be OP_BASEREG (basereg). This may look deceptively like
12087 the object in question was allocated to a register (rather than in
12088 memory) so DWARF consumers need to be aware of the subtle
12089 distinction between OP_REG and OP_BASEREG. */
12090 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12091 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12092 else if (stack_realign_drap
12093 && crtl->drap_reg
12094 && crtl->args.internal_arg_pointer == rtl
12095 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12097 /* If RTL is internal_arg_pointer, which has been optimized
12098 out, use DRAP instead. */
12099 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12100 VAR_INIT_STATUS_INITIALIZED);
12102 break;
12104 case SIGN_EXTEND:
12105 case ZERO_EXTEND:
12106 if (GET_MODE_CLASS (mode) != MODE_INT)
12107 break;
12108 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12109 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12110 if (op0 == 0)
12111 break;
12112 else if (GET_CODE (rtl) == ZERO_EXTEND
12113 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12114 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12115 < HOST_BITS_PER_WIDE_INT
12116 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12117 to expand zero extend as two shifts instead of
12118 masking. */
12119 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12121 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12122 mem_loc_result = op0;
12123 add_loc_descr (&mem_loc_result,
12124 int_loc_descriptor (GET_MODE_MASK (imode)));
12125 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12127 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12129 int shift = DWARF2_ADDR_SIZE
12130 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12131 shift *= BITS_PER_UNIT;
12132 if (GET_CODE (rtl) == SIGN_EXTEND)
12133 op = DW_OP_shra;
12134 else
12135 op = DW_OP_shr;
12136 mem_loc_result = op0;
12137 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12138 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12139 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12140 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12142 else if (!dwarf_strict)
12144 dw_die_ref type_die1, type_die2;
12145 dw_loc_descr_ref cvt;
12147 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12148 GET_CODE (rtl) == ZERO_EXTEND);
12149 if (type_die1 == NULL)
12150 break;
12151 type_die2 = base_type_for_mode (mode, 1);
12152 if (type_die2 == NULL)
12153 break;
12154 mem_loc_result = op0;
12155 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12156 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12157 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12158 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12159 add_loc_descr (&mem_loc_result, cvt);
12160 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12161 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12162 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12163 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12164 add_loc_descr (&mem_loc_result, cvt);
12166 break;
12168 case MEM:
12170 rtx new_rtl = avoid_constant_pool_reference (rtl);
12171 if (new_rtl != rtl)
12173 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12174 initialized);
12175 if (mem_loc_result != NULL)
12176 return mem_loc_result;
12179 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12180 get_address_mode (rtl), mode,
12181 VAR_INIT_STATUS_INITIALIZED);
12182 if (mem_loc_result == NULL)
12183 mem_loc_result = tls_mem_loc_descriptor (rtl);
12184 if (mem_loc_result != NULL)
12186 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12187 || GET_MODE_CLASS (mode) != MODE_INT)
12189 dw_die_ref type_die;
12190 dw_loc_descr_ref deref;
12192 if (dwarf_strict)
12193 return NULL;
12194 type_die
12195 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12196 if (type_die == NULL)
12197 return NULL;
12198 deref = new_loc_descr (DW_OP_GNU_deref_type,
12199 GET_MODE_SIZE (mode), 0);
12200 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12201 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12202 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12203 add_loc_descr (&mem_loc_result, deref);
12205 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12206 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12207 else
12208 add_loc_descr (&mem_loc_result,
12209 new_loc_descr (DW_OP_deref_size,
12210 GET_MODE_SIZE (mode), 0));
12212 break;
12214 case LO_SUM:
12215 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12217 case LABEL_REF:
12218 /* Some ports can transform a symbol ref into a label ref, because
12219 the symbol ref is too far away and has to be dumped into a constant
12220 pool. */
12221 case CONST:
12222 case SYMBOL_REF:
12223 if (GET_MODE_CLASS (mode) != MODE_INT
12224 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12225 #ifdef POINTERS_EXTEND_UNSIGNED
12226 && (mode != Pmode || mem_mode == VOIDmode)
12227 #endif
12229 break;
12230 if (GET_CODE (rtl) == SYMBOL_REF
12231 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12233 dw_loc_descr_ref temp;
12235 /* If this is not defined, we have no way to emit the data. */
12236 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12237 break;
12239 temp = new_addr_loc_descr (rtl, dtprel_true);
12241 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12242 add_loc_descr (&mem_loc_result, temp);
12244 break;
12247 if (!const_ok_for_output (rtl))
12248 break;
12250 symref:
12251 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12252 vec_safe_push (used_rtx_array, rtl);
12253 break;
12255 case CONCAT:
12256 case CONCATN:
12257 case VAR_LOCATION:
12258 case DEBUG_IMPLICIT_PTR:
12259 expansion_failed (NULL_TREE, rtl,
12260 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12261 return 0;
12263 case ENTRY_VALUE:
12264 if (dwarf_strict)
12265 return NULL;
12266 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12268 if (GET_MODE_CLASS (mode) != MODE_INT
12269 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12270 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12271 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12272 else
12274 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12275 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12276 return NULL;
12277 op0 = one_reg_loc_descriptor (dbx_regnum,
12278 VAR_INIT_STATUS_INITIALIZED);
12281 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12282 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12284 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12285 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12286 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12287 return NULL;
12289 else
12290 gcc_unreachable ();
12291 if (op0 == NULL)
12292 return NULL;
12293 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12294 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12295 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12296 break;
12298 case DEBUG_PARAMETER_REF:
12299 mem_loc_result = parameter_ref_descriptor (rtl);
12300 break;
12302 case PRE_MODIFY:
12303 /* Extract the PLUS expression nested inside and fall into
12304 PLUS code below. */
12305 rtl = XEXP (rtl, 1);
12306 goto plus;
12308 case PRE_INC:
12309 case PRE_DEC:
12310 /* Turn these into a PLUS expression and fall into the PLUS code
12311 below. */
12312 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12313 GEN_INT (GET_CODE (rtl) == PRE_INC
12314 ? GET_MODE_UNIT_SIZE (mem_mode)
12315 : -GET_MODE_UNIT_SIZE (mem_mode)));
12317 /* ... fall through ... */
12319 case PLUS:
12320 plus:
12321 if (is_based_loc (rtl)
12322 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12323 || XEXP (rtl, 0) == arg_pointer_rtx
12324 || XEXP (rtl, 0) == frame_pointer_rtx)
12325 && GET_MODE_CLASS (mode) == MODE_INT)
12326 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12327 INTVAL (XEXP (rtl, 1)),
12328 VAR_INIT_STATUS_INITIALIZED);
12329 else
12331 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12332 VAR_INIT_STATUS_INITIALIZED);
12333 if (mem_loc_result == 0)
12334 break;
12336 if (CONST_INT_P (XEXP (rtl, 1))
12337 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12338 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12339 else
12341 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12342 VAR_INIT_STATUS_INITIALIZED);
12343 if (op1 == 0)
12344 break;
12345 add_loc_descr (&mem_loc_result, op1);
12346 add_loc_descr (&mem_loc_result,
12347 new_loc_descr (DW_OP_plus, 0, 0));
12350 break;
12352 /* If a pseudo-reg is optimized away, it is possible for it to
12353 be replaced with a MEM containing a multiply or shift. */
12354 case MINUS:
12355 op = DW_OP_minus;
12356 goto do_binop;
12358 case MULT:
12359 op = DW_OP_mul;
12360 goto do_binop;
12362 case DIV:
12363 if (!dwarf_strict
12364 && GET_MODE_CLASS (mode) == MODE_INT
12365 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12367 mem_loc_result = typed_binop (DW_OP_div, rtl,
12368 base_type_for_mode (mode, 0),
12369 mode, mem_mode);
12370 break;
12372 op = DW_OP_div;
12373 goto do_binop;
12375 case UMOD:
12376 op = DW_OP_mod;
12377 goto do_binop;
12379 case ASHIFT:
12380 op = DW_OP_shl;
12381 goto do_shift;
12383 case ASHIFTRT:
12384 op = DW_OP_shra;
12385 goto do_shift;
12387 case LSHIFTRT:
12388 op = DW_OP_shr;
12389 goto do_shift;
12391 do_shift:
12392 if (GET_MODE_CLASS (mode) != MODE_INT)
12393 break;
12394 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12395 VAR_INIT_STATUS_INITIALIZED);
12397 rtx rtlop1 = XEXP (rtl, 1);
12398 if (GET_MODE (rtlop1) != VOIDmode
12399 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12400 < GET_MODE_BITSIZE (mode))
12401 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12402 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12403 VAR_INIT_STATUS_INITIALIZED);
12406 if (op0 == 0 || op1 == 0)
12407 break;
12409 mem_loc_result = op0;
12410 add_loc_descr (&mem_loc_result, op1);
12411 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12412 break;
12414 case AND:
12415 op = DW_OP_and;
12416 goto do_binop;
12418 case IOR:
12419 op = DW_OP_or;
12420 goto do_binop;
12422 case XOR:
12423 op = DW_OP_xor;
12424 goto do_binop;
12426 do_binop:
12427 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12428 VAR_INIT_STATUS_INITIALIZED);
12429 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12430 VAR_INIT_STATUS_INITIALIZED);
12432 if (op0 == 0 || op1 == 0)
12433 break;
12435 mem_loc_result = op0;
12436 add_loc_descr (&mem_loc_result, op1);
12437 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12438 break;
12440 case MOD:
12441 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12443 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12444 base_type_for_mode (mode, 0),
12445 mode, mem_mode);
12446 break;
12449 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12450 VAR_INIT_STATUS_INITIALIZED);
12451 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12452 VAR_INIT_STATUS_INITIALIZED);
12454 if (op0 == 0 || op1 == 0)
12455 break;
12457 mem_loc_result = op0;
12458 add_loc_descr (&mem_loc_result, op1);
12459 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12460 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12461 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12462 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12463 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12464 break;
12466 case UDIV:
12467 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12469 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12471 op = DW_OP_div;
12472 goto do_binop;
12474 mem_loc_result = typed_binop (DW_OP_div, rtl,
12475 base_type_for_mode (mode, 1),
12476 mode, mem_mode);
12478 break;
12480 case NOT:
12481 op = DW_OP_not;
12482 goto do_unop;
12484 case ABS:
12485 op = DW_OP_abs;
12486 goto do_unop;
12488 case NEG:
12489 op = DW_OP_neg;
12490 goto do_unop;
12492 do_unop:
12493 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12494 VAR_INIT_STATUS_INITIALIZED);
12496 if (op0 == 0)
12497 break;
12499 mem_loc_result = op0;
12500 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12501 break;
12503 case CONST_INT:
12504 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12505 #ifdef POINTERS_EXTEND_UNSIGNED
12506 || (mode == Pmode
12507 && mem_mode != VOIDmode
12508 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12509 #endif
12512 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12513 break;
12515 if (!dwarf_strict
12516 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12517 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12519 dw_die_ref type_die = base_type_for_mode (mode, 1);
12520 enum machine_mode amode;
12521 if (type_die == NULL)
12522 return NULL;
12523 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12524 MODE_INT, 0);
12525 if (INTVAL (rtl) >= 0
12526 && amode != BLKmode
12527 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12528 /* const DW_OP_GNU_convert <XXX> vs.
12529 DW_OP_GNU_const_type <XXX, 1, const>. */
12530 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12531 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12533 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12534 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12535 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12536 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12537 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12538 add_loc_descr (&mem_loc_result, op0);
12539 return mem_loc_result;
12541 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12542 INTVAL (rtl));
12543 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12544 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12545 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12546 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12547 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12548 else
12550 mem_loc_result->dw_loc_oprnd2.val_class
12551 = dw_val_class_const_double;
12552 mem_loc_result->dw_loc_oprnd2.v.val_double
12553 = double_int::from_shwi (INTVAL (rtl));
12556 break;
12558 case CONST_DOUBLE:
12559 if (!dwarf_strict)
12561 dw_die_ref type_die;
12563 /* Note that a CONST_DOUBLE rtx could represent either an integer
12564 or a floating-point constant. A CONST_DOUBLE is used whenever
12565 the constant requires more than one word in order to be
12566 adequately represented. We output CONST_DOUBLEs as blocks. */
12567 if (mode == VOIDmode
12568 || (GET_MODE (rtl) == VOIDmode
12569 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12570 break;
12571 type_die = base_type_for_mode (mode,
12572 GET_MODE_CLASS (mode) == MODE_INT);
12573 if (type_die == NULL)
12574 return NULL;
12575 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12576 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12577 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12578 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12579 if (SCALAR_FLOAT_MODE_P (mode))
12581 unsigned int length = GET_MODE_SIZE (mode);
12582 unsigned char *array
12583 = (unsigned char*) ggc_alloc_atomic (length);
12585 insert_float (rtl, array);
12586 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12587 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12588 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12589 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12591 else
12593 mem_loc_result->dw_loc_oprnd2.val_class
12594 = dw_val_class_const_double;
12595 mem_loc_result->dw_loc_oprnd2.v.val_double
12596 = rtx_to_double_int (rtl);
12599 break;
12601 case EQ:
12602 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12603 break;
12605 case GE:
12606 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12607 break;
12609 case GT:
12610 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12611 break;
12613 case LE:
12614 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12615 break;
12617 case LT:
12618 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12619 break;
12621 case NE:
12622 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12623 break;
12625 case GEU:
12626 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12627 break;
12629 case GTU:
12630 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12631 break;
12633 case LEU:
12634 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12635 break;
12637 case LTU:
12638 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12639 break;
12641 case UMIN:
12642 case UMAX:
12643 if (GET_MODE_CLASS (mode) != MODE_INT)
12644 break;
12645 /* FALLTHRU */
12646 case SMIN:
12647 case SMAX:
12648 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12649 break;
12651 case ZERO_EXTRACT:
12652 case SIGN_EXTRACT:
12653 if (CONST_INT_P (XEXP (rtl, 1))
12654 && CONST_INT_P (XEXP (rtl, 2))
12655 && ((unsigned) INTVAL (XEXP (rtl, 1))
12656 + (unsigned) INTVAL (XEXP (rtl, 2))
12657 <= GET_MODE_BITSIZE (mode))
12658 && GET_MODE_CLASS (mode) == MODE_INT
12659 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12660 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12662 int shift, size;
12663 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12664 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12665 if (op0 == 0)
12666 break;
12667 if (GET_CODE (rtl) == SIGN_EXTRACT)
12668 op = DW_OP_shra;
12669 else
12670 op = DW_OP_shr;
12671 mem_loc_result = op0;
12672 size = INTVAL (XEXP (rtl, 1));
12673 shift = INTVAL (XEXP (rtl, 2));
12674 if (BITS_BIG_ENDIAN)
12675 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12676 - shift - size;
12677 if (shift + size != (int) DWARF2_ADDR_SIZE)
12679 add_loc_descr (&mem_loc_result,
12680 int_loc_descriptor (DWARF2_ADDR_SIZE
12681 - shift - size));
12682 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12684 if (size != (int) DWARF2_ADDR_SIZE)
12686 add_loc_descr (&mem_loc_result,
12687 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12688 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12691 break;
12693 case IF_THEN_ELSE:
12695 dw_loc_descr_ref op2, bra_node, drop_node;
12696 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12697 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12698 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12699 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12700 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12701 VAR_INIT_STATUS_INITIALIZED);
12702 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12703 VAR_INIT_STATUS_INITIALIZED);
12704 if (op0 == NULL || op1 == NULL || op2 == NULL)
12705 break;
12707 mem_loc_result = op1;
12708 add_loc_descr (&mem_loc_result, op2);
12709 add_loc_descr (&mem_loc_result, op0);
12710 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12711 add_loc_descr (&mem_loc_result, bra_node);
12712 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12713 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12714 add_loc_descr (&mem_loc_result, drop_node);
12715 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12716 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12718 break;
12720 case FLOAT_EXTEND:
12721 case FLOAT_TRUNCATE:
12722 case FLOAT:
12723 case UNSIGNED_FLOAT:
12724 case FIX:
12725 case UNSIGNED_FIX:
12726 if (!dwarf_strict)
12728 dw_die_ref type_die;
12729 dw_loc_descr_ref cvt;
12731 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12732 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12733 if (op0 == NULL)
12734 break;
12735 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12736 && (GET_CODE (rtl) == FLOAT
12737 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12738 <= DWARF2_ADDR_SIZE))
12740 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12741 GET_CODE (rtl) == UNSIGNED_FLOAT);
12742 if (type_die == NULL)
12743 break;
12744 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12745 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12746 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12747 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12748 add_loc_descr (&op0, cvt);
12750 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12751 if (type_die == NULL)
12752 break;
12753 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12754 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12755 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12756 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12757 add_loc_descr (&op0, cvt);
12758 if (GET_MODE_CLASS (mode) == MODE_INT
12759 && (GET_CODE (rtl) == FIX
12760 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12762 op0 = convert_descriptor_to_mode (mode, op0);
12763 if (op0 == NULL)
12764 break;
12766 mem_loc_result = op0;
12768 break;
12770 case CLZ:
12771 case CTZ:
12772 case FFS:
12773 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12774 break;
12776 case POPCOUNT:
12777 case PARITY:
12778 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12779 break;
12781 case BSWAP:
12782 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12783 break;
12785 case ROTATE:
12786 case ROTATERT:
12787 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12788 break;
12790 case COMPARE:
12791 /* In theory, we could implement the above. */
12792 /* DWARF cannot represent the unsigned compare operations
12793 natively. */
12794 case SS_MULT:
12795 case US_MULT:
12796 case SS_DIV:
12797 case US_DIV:
12798 case SS_PLUS:
12799 case US_PLUS:
12800 case SS_MINUS:
12801 case US_MINUS:
12802 case SS_NEG:
12803 case US_NEG:
12804 case SS_ABS:
12805 case SS_ASHIFT:
12806 case US_ASHIFT:
12807 case SS_TRUNCATE:
12808 case US_TRUNCATE:
12809 case UNORDERED:
12810 case ORDERED:
12811 case UNEQ:
12812 case UNGE:
12813 case UNGT:
12814 case UNLE:
12815 case UNLT:
12816 case LTGT:
12817 case FRACT_CONVERT:
12818 case UNSIGNED_FRACT_CONVERT:
12819 case SAT_FRACT:
12820 case UNSIGNED_SAT_FRACT:
12821 case SQRT:
12822 case ASM_OPERANDS:
12823 case VEC_MERGE:
12824 case VEC_SELECT:
12825 case VEC_CONCAT:
12826 case VEC_DUPLICATE:
12827 case UNSPEC:
12828 case HIGH:
12829 case FMA:
12830 case STRICT_LOW_PART:
12831 case CONST_VECTOR:
12832 case CONST_FIXED:
12833 case CLRSB:
12834 case CLOBBER:
12835 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12836 can't express it in the debug info. This can happen e.g. with some
12837 TLS UNSPECs. */
12838 break;
12840 case CONST_STRING:
12841 resolve_one_addr (&rtl, NULL);
12842 goto symref;
12844 default:
12845 #ifdef ENABLE_CHECKING
12846 print_rtl (stderr, rtl);
12847 gcc_unreachable ();
12848 #else
12849 break;
12850 #endif
12853 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12854 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12856 return mem_loc_result;
12859 /* Return a descriptor that describes the concatenation of two locations.
12860 This is typically a complex variable. */
12862 static dw_loc_descr_ref
12863 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12865 dw_loc_descr_ref cc_loc_result = NULL;
12866 dw_loc_descr_ref x0_ref
12867 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12868 dw_loc_descr_ref x1_ref
12869 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12871 if (x0_ref == 0 || x1_ref == 0)
12872 return 0;
12874 cc_loc_result = x0_ref;
12875 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12877 add_loc_descr (&cc_loc_result, x1_ref);
12878 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12880 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12881 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12883 return cc_loc_result;
12886 /* Return a descriptor that describes the concatenation of N
12887 locations. */
12889 static dw_loc_descr_ref
12890 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12892 unsigned int i;
12893 dw_loc_descr_ref cc_loc_result = NULL;
12894 unsigned int n = XVECLEN (concatn, 0);
12896 for (i = 0; i < n; ++i)
12898 dw_loc_descr_ref ref;
12899 rtx x = XVECEXP (concatn, 0, i);
12901 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12902 if (ref == NULL)
12903 return NULL;
12905 add_loc_descr (&cc_loc_result, ref);
12906 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12909 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12910 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12912 return cc_loc_result;
12915 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12916 for DEBUG_IMPLICIT_PTR RTL. */
12918 static dw_loc_descr_ref
12919 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12921 dw_loc_descr_ref ret;
12922 dw_die_ref ref;
12924 if (dwarf_strict)
12925 return NULL;
12926 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12927 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12928 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12929 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12930 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12931 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12932 if (ref)
12934 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12935 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12936 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12938 else
12940 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12941 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12943 return ret;
12946 /* Output a proper Dwarf location descriptor for a variable or parameter
12947 which is either allocated in a register or in a memory location. For a
12948 register, we just generate an OP_REG and the register number. For a
12949 memory location we provide a Dwarf postfix expression describing how to
12950 generate the (dynamic) address of the object onto the address stack.
12952 MODE is mode of the decl if this loc_descriptor is going to be used in
12953 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12954 allowed, VOIDmode otherwise.
12956 If we don't know how to describe it, return 0. */
12958 static dw_loc_descr_ref
12959 loc_descriptor (rtx rtl, enum machine_mode mode,
12960 enum var_init_status initialized)
12962 dw_loc_descr_ref loc_result = NULL;
12964 switch (GET_CODE (rtl))
12966 case SUBREG:
12967 /* The case of a subreg may arise when we have a local (register)
12968 variable or a formal (register) parameter which doesn't quite fill
12969 up an entire register. For now, just assume that it is
12970 legitimate to make the Dwarf info refer to the whole register which
12971 contains the given subreg. */
12972 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12973 loc_result = loc_descriptor (SUBREG_REG (rtl),
12974 GET_MODE (SUBREG_REG (rtl)), initialized);
12975 else
12976 goto do_default;
12977 break;
12979 case REG:
12980 loc_result = reg_loc_descriptor (rtl, initialized);
12981 break;
12983 case MEM:
12984 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12985 GET_MODE (rtl), initialized);
12986 if (loc_result == NULL)
12987 loc_result = tls_mem_loc_descriptor (rtl);
12988 if (loc_result == NULL)
12990 rtx new_rtl = avoid_constant_pool_reference (rtl);
12991 if (new_rtl != rtl)
12992 loc_result = loc_descriptor (new_rtl, mode, initialized);
12994 break;
12996 case CONCAT:
12997 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12998 initialized);
12999 break;
13001 case CONCATN:
13002 loc_result = concatn_loc_descriptor (rtl, initialized);
13003 break;
13005 case VAR_LOCATION:
13006 /* Single part. */
13007 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13009 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13010 if (GET_CODE (loc) == EXPR_LIST)
13011 loc = XEXP (loc, 0);
13012 loc_result = loc_descriptor (loc, mode, initialized);
13013 break;
13016 rtl = XEXP (rtl, 1);
13017 /* FALLTHRU */
13019 case PARALLEL:
13021 rtvec par_elems = XVEC (rtl, 0);
13022 int num_elem = GET_NUM_ELEM (par_elems);
13023 enum machine_mode mode;
13024 int i;
13026 /* Create the first one, so we have something to add to. */
13027 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13028 VOIDmode, initialized);
13029 if (loc_result == NULL)
13030 return NULL;
13031 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13032 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13033 for (i = 1; i < num_elem; i++)
13035 dw_loc_descr_ref temp;
13037 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13038 VOIDmode, initialized);
13039 if (temp == NULL)
13040 return NULL;
13041 add_loc_descr (&loc_result, temp);
13042 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13043 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13046 break;
13048 case CONST_INT:
13049 if (mode != VOIDmode && mode != BLKmode)
13050 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13051 INTVAL (rtl));
13052 break;
13054 case CONST_DOUBLE:
13055 if (mode == VOIDmode)
13056 mode = GET_MODE (rtl);
13058 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13060 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13062 /* Note that a CONST_DOUBLE rtx could represent either an integer
13063 or a floating-point constant. A CONST_DOUBLE is used whenever
13064 the constant requires more than one word in order to be
13065 adequately represented. We output CONST_DOUBLEs as blocks. */
13066 loc_result = new_loc_descr (DW_OP_implicit_value,
13067 GET_MODE_SIZE (mode), 0);
13068 if (SCALAR_FLOAT_MODE_P (mode))
13070 unsigned int length = GET_MODE_SIZE (mode);
13071 unsigned char *array
13072 = (unsigned char*) ggc_alloc_atomic (length);
13074 insert_float (rtl, array);
13075 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13076 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13077 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13078 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13080 else
13082 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13083 loc_result->dw_loc_oprnd2.v.val_double
13084 = rtx_to_double_int (rtl);
13087 break;
13089 case CONST_VECTOR:
13090 if (mode == VOIDmode)
13091 mode = GET_MODE (rtl);
13093 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13095 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13096 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13097 unsigned char *array = (unsigned char *)
13098 ggc_alloc_atomic (length * elt_size);
13099 unsigned int i;
13100 unsigned char *p;
13102 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13103 switch (GET_MODE_CLASS (mode))
13105 case MODE_VECTOR_INT:
13106 for (i = 0, p = array; i < length; i++, p += elt_size)
13108 rtx elt = CONST_VECTOR_ELT (rtl, i);
13109 double_int val = rtx_to_double_int (elt);
13111 if (elt_size <= sizeof (HOST_WIDE_INT))
13112 insert_int (val.to_shwi (), elt_size, p);
13113 else
13115 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13116 insert_double (val, p);
13119 break;
13121 case MODE_VECTOR_FLOAT:
13122 for (i = 0, p = array; i < length; i++, p += elt_size)
13124 rtx elt = CONST_VECTOR_ELT (rtl, i);
13125 insert_float (elt, p);
13127 break;
13129 default:
13130 gcc_unreachable ();
13133 loc_result = new_loc_descr (DW_OP_implicit_value,
13134 length * elt_size, 0);
13135 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13136 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13137 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13138 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13140 break;
13142 case CONST:
13143 if (mode == VOIDmode
13144 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13145 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13146 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13148 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13149 break;
13151 /* FALLTHROUGH */
13152 case SYMBOL_REF:
13153 if (!const_ok_for_output (rtl))
13154 break;
13155 case LABEL_REF:
13156 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13157 && (dwarf_version >= 4 || !dwarf_strict))
13159 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13160 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13161 vec_safe_push (used_rtx_array, rtl);
13163 break;
13165 case DEBUG_IMPLICIT_PTR:
13166 loc_result = implicit_ptr_descriptor (rtl, 0);
13167 break;
13169 case PLUS:
13170 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13171 && CONST_INT_P (XEXP (rtl, 1)))
13173 loc_result
13174 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13175 break;
13177 /* FALLTHRU */
13178 do_default:
13179 default:
13180 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13181 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13182 && dwarf_version >= 4)
13183 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13185 /* Value expression. */
13186 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13187 if (loc_result)
13188 add_loc_descr (&loc_result,
13189 new_loc_descr (DW_OP_stack_value, 0, 0));
13191 break;
13194 return loc_result;
13197 /* We need to figure out what section we should use as the base for the
13198 address ranges where a given location is valid.
13199 1. If this particular DECL has a section associated with it, use that.
13200 2. If this function has a section associated with it, use that.
13201 3. Otherwise, use the text section.
13202 XXX: If you split a variable across multiple sections, we won't notice. */
13204 static const char *
13205 secname_for_decl (const_tree decl)
13207 const char *secname;
13209 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13211 tree sectree = DECL_SECTION_NAME (decl);
13212 secname = TREE_STRING_POINTER (sectree);
13214 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13216 tree sectree = DECL_SECTION_NAME (current_function_decl);
13217 secname = TREE_STRING_POINTER (sectree);
13219 else if (cfun && in_cold_section_p)
13220 secname = crtl->subsections.cold_section_label;
13221 else
13222 secname = text_section_label;
13224 return secname;
13227 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13229 static bool
13230 decl_by_reference_p (tree decl)
13232 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13233 || TREE_CODE (decl) == VAR_DECL)
13234 && DECL_BY_REFERENCE (decl));
13237 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13238 for VARLOC. */
13240 static dw_loc_descr_ref
13241 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13242 enum var_init_status initialized)
13244 int have_address = 0;
13245 dw_loc_descr_ref descr;
13246 enum machine_mode mode;
13248 if (want_address != 2)
13250 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13251 /* Single part. */
13252 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13254 varloc = PAT_VAR_LOCATION_LOC (varloc);
13255 if (GET_CODE (varloc) == EXPR_LIST)
13256 varloc = XEXP (varloc, 0);
13257 mode = GET_MODE (varloc);
13258 if (MEM_P (varloc))
13260 rtx addr = XEXP (varloc, 0);
13261 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13262 mode, initialized);
13263 if (descr)
13264 have_address = 1;
13265 else
13267 rtx x = avoid_constant_pool_reference (varloc);
13268 if (x != varloc)
13269 descr = mem_loc_descriptor (x, mode, VOIDmode,
13270 initialized);
13273 else
13274 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13276 else
13277 return 0;
13279 else
13281 if (GET_CODE (varloc) == VAR_LOCATION)
13282 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13283 else
13284 mode = DECL_MODE (loc);
13285 descr = loc_descriptor (varloc, mode, initialized);
13286 have_address = 1;
13289 if (!descr)
13290 return 0;
13292 if (want_address == 2 && !have_address
13293 && (dwarf_version >= 4 || !dwarf_strict))
13295 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13297 expansion_failed (loc, NULL_RTX,
13298 "DWARF address size mismatch");
13299 return 0;
13301 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13302 have_address = 1;
13304 /* Show if we can't fill the request for an address. */
13305 if (want_address && !have_address)
13307 expansion_failed (loc, NULL_RTX,
13308 "Want address and only have value");
13309 return 0;
13312 /* If we've got an address and don't want one, dereference. */
13313 if (!want_address && have_address)
13315 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13316 enum dwarf_location_atom op;
13318 if (size > DWARF2_ADDR_SIZE || size == -1)
13320 expansion_failed (loc, NULL_RTX,
13321 "DWARF address size mismatch");
13322 return 0;
13324 else if (size == DWARF2_ADDR_SIZE)
13325 op = DW_OP_deref;
13326 else
13327 op = DW_OP_deref_size;
13329 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13332 return descr;
13335 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13336 if it is not possible. */
13338 static dw_loc_descr_ref
13339 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13341 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13342 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13343 else if (dwarf_version >= 3 || !dwarf_strict)
13344 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13345 else
13346 return NULL;
13349 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13350 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13352 static dw_loc_descr_ref
13353 dw_sra_loc_expr (tree decl, rtx loc)
13355 rtx p;
13356 unsigned int padsize = 0;
13357 dw_loc_descr_ref descr, *descr_tail;
13358 unsigned HOST_WIDE_INT decl_size;
13359 rtx varloc;
13360 enum var_init_status initialized;
13362 if (DECL_SIZE (decl) == NULL
13363 || !host_integerp (DECL_SIZE (decl), 1))
13364 return NULL;
13366 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13367 descr = NULL;
13368 descr_tail = &descr;
13370 for (p = loc; p; p = XEXP (p, 1))
13372 unsigned int bitsize = decl_piece_bitsize (p);
13373 rtx loc_note = *decl_piece_varloc_ptr (p);
13374 dw_loc_descr_ref cur_descr;
13375 dw_loc_descr_ref *tail, last = NULL;
13376 unsigned int opsize = 0;
13378 if (loc_note == NULL_RTX
13379 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13381 padsize += bitsize;
13382 continue;
13384 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13385 varloc = NOTE_VAR_LOCATION (loc_note);
13386 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13387 if (cur_descr == NULL)
13389 padsize += bitsize;
13390 continue;
13393 /* Check that cur_descr either doesn't use
13394 DW_OP_*piece operations, or their sum is equal
13395 to bitsize. Otherwise we can't embed it. */
13396 for (tail = &cur_descr; *tail != NULL;
13397 tail = &(*tail)->dw_loc_next)
13398 if ((*tail)->dw_loc_opc == DW_OP_piece)
13400 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13401 * BITS_PER_UNIT;
13402 last = *tail;
13404 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13406 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13407 last = *tail;
13410 if (last != NULL && opsize != bitsize)
13412 padsize += bitsize;
13413 continue;
13416 /* If there is a hole, add DW_OP_*piece after empty DWARF
13417 expression, which means that those bits are optimized out. */
13418 if (padsize)
13420 if (padsize > decl_size)
13421 return NULL;
13422 decl_size -= padsize;
13423 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13424 if (*descr_tail == NULL)
13425 return NULL;
13426 descr_tail = &(*descr_tail)->dw_loc_next;
13427 padsize = 0;
13429 *descr_tail = cur_descr;
13430 descr_tail = tail;
13431 if (bitsize > decl_size)
13432 return NULL;
13433 decl_size -= bitsize;
13434 if (last == NULL)
13436 HOST_WIDE_INT offset = 0;
13437 if (GET_CODE (varloc) == VAR_LOCATION
13438 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13440 varloc = PAT_VAR_LOCATION_LOC (varloc);
13441 if (GET_CODE (varloc) == EXPR_LIST)
13442 varloc = XEXP (varloc, 0);
13446 if (GET_CODE (varloc) == CONST
13447 || GET_CODE (varloc) == SIGN_EXTEND
13448 || GET_CODE (varloc) == ZERO_EXTEND)
13449 varloc = XEXP (varloc, 0);
13450 else if (GET_CODE (varloc) == SUBREG)
13451 varloc = SUBREG_REG (varloc);
13452 else
13453 break;
13455 while (1);
13456 /* DW_OP_bit_size offset should be zero for register
13457 or implicit location descriptions and empty location
13458 descriptions, but for memory addresses needs big endian
13459 adjustment. */
13460 if (MEM_P (varloc))
13462 unsigned HOST_WIDE_INT memsize
13463 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13464 if (memsize != bitsize)
13466 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13467 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13468 return NULL;
13469 if (memsize < bitsize)
13470 return NULL;
13471 if (BITS_BIG_ENDIAN)
13472 offset = memsize - bitsize;
13476 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13477 if (*descr_tail == NULL)
13478 return NULL;
13479 descr_tail = &(*descr_tail)->dw_loc_next;
13483 /* If there were any non-empty expressions, add padding till the end of
13484 the decl. */
13485 if (descr != NULL && decl_size != 0)
13487 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13488 if (*descr_tail == NULL)
13489 return NULL;
13491 return descr;
13494 /* Return the dwarf representation of the location list LOC_LIST of
13495 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13496 function. */
13498 static dw_loc_list_ref
13499 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13501 const char *endname, *secname;
13502 rtx varloc;
13503 enum var_init_status initialized;
13504 struct var_loc_node *node;
13505 dw_loc_descr_ref descr;
13506 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13507 dw_loc_list_ref list = NULL;
13508 dw_loc_list_ref *listp = &list;
13510 /* Now that we know what section we are using for a base,
13511 actually construct the list of locations.
13512 The first location information is what is passed to the
13513 function that creates the location list, and the remaining
13514 locations just get added on to that list.
13515 Note that we only know the start address for a location
13516 (IE location changes), so to build the range, we use
13517 the range [current location start, next location start].
13518 This means we have to special case the last node, and generate
13519 a range of [last location start, end of function label]. */
13521 secname = secname_for_decl (decl);
13523 for (node = loc_list->first; node; node = node->next)
13524 if (GET_CODE (node->loc) == EXPR_LIST
13525 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13527 if (GET_CODE (node->loc) == EXPR_LIST)
13529 /* This requires DW_OP_{,bit_}piece, which is not usable
13530 inside DWARF expressions. */
13531 if (want_address != 2)
13532 continue;
13533 descr = dw_sra_loc_expr (decl, node->loc);
13534 if (descr == NULL)
13535 continue;
13537 else
13539 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13540 varloc = NOTE_VAR_LOCATION (node->loc);
13541 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13543 if (descr)
13545 bool range_across_switch = false;
13546 /* If section switch happens in between node->label
13547 and node->next->label (or end of function) and
13548 we can't emit it as a single entry list,
13549 emit two ranges, first one ending at the end
13550 of first partition and second one starting at the
13551 beginning of second partition. */
13552 if (node == loc_list->last_before_switch
13553 && (node != loc_list->first || loc_list->first->next)
13554 && current_function_decl)
13556 endname = cfun->fde->dw_fde_end;
13557 range_across_switch = true;
13559 /* The variable has a location between NODE->LABEL and
13560 NODE->NEXT->LABEL. */
13561 else if (node->next)
13562 endname = node->next->label;
13563 /* If the variable has a location at the last label
13564 it keeps its location until the end of function. */
13565 else if (!current_function_decl)
13566 endname = text_end_label;
13567 else
13569 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13570 current_function_funcdef_no);
13571 endname = ggc_strdup (label_id);
13574 *listp = new_loc_list (descr, node->label, endname, secname);
13575 if (TREE_CODE (decl) == PARM_DECL
13576 && node == loc_list->first
13577 && NOTE_P (node->loc)
13578 && strcmp (node->label, endname) == 0)
13579 (*listp)->force = true;
13580 listp = &(*listp)->dw_loc_next;
13582 if (range_across_switch)
13584 if (GET_CODE (node->loc) == EXPR_LIST)
13585 descr = dw_sra_loc_expr (decl, node->loc);
13586 else
13588 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13589 varloc = NOTE_VAR_LOCATION (node->loc);
13590 descr = dw_loc_list_1 (decl, varloc, want_address,
13591 initialized);
13593 gcc_assert (descr);
13594 /* The variable has a location between NODE->LABEL and
13595 NODE->NEXT->LABEL. */
13596 if (node->next)
13597 endname = node->next->label;
13598 else
13599 endname = cfun->fde->dw_fde_second_end;
13600 *listp = new_loc_list (descr,
13601 cfun->fde->dw_fde_second_begin,
13602 endname, secname);
13603 listp = &(*listp)->dw_loc_next;
13608 /* Try to avoid the overhead of a location list emitting a location
13609 expression instead, but only if we didn't have more than one
13610 location entry in the first place. If some entries were not
13611 representable, we don't want to pretend a single entry that was
13612 applies to the entire scope in which the variable is
13613 available. */
13614 if (list && loc_list->first->next)
13615 gen_llsym (list);
13617 return list;
13620 /* Return if the loc_list has only single element and thus can be represented
13621 as location description. */
13623 static bool
13624 single_element_loc_list_p (dw_loc_list_ref list)
13626 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13627 return !list->ll_symbol;
13630 /* To each location in list LIST add loc descr REF. */
13632 static void
13633 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13635 dw_loc_descr_ref copy;
13636 add_loc_descr (&list->expr, ref);
13637 list = list->dw_loc_next;
13638 while (list)
13640 copy = ggc_alloc_dw_loc_descr_node ();
13641 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13642 add_loc_descr (&list->expr, copy);
13643 while (copy->dw_loc_next)
13645 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13646 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13647 copy->dw_loc_next = new_copy;
13648 copy = new_copy;
13650 list = list->dw_loc_next;
13654 /* Given two lists RET and LIST
13655 produce location list that is result of adding expression in LIST
13656 to expression in RET on each position in program.
13657 Might be destructive on both RET and LIST.
13659 TODO: We handle only simple cases of RET or LIST having at most one
13660 element. General case would inolve sorting the lists in program order
13661 and merging them that will need some additional work.
13662 Adding that will improve quality of debug info especially for SRA-ed
13663 structures. */
13665 static void
13666 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13668 if (!list)
13669 return;
13670 if (!*ret)
13672 *ret = list;
13673 return;
13675 if (!list->dw_loc_next)
13677 add_loc_descr_to_each (*ret, list->expr);
13678 return;
13680 if (!(*ret)->dw_loc_next)
13682 add_loc_descr_to_each (list, (*ret)->expr);
13683 *ret = list;
13684 return;
13686 expansion_failed (NULL_TREE, NULL_RTX,
13687 "Don't know how to merge two non-trivial"
13688 " location lists.\n");
13689 *ret = NULL;
13690 return;
13693 /* LOC is constant expression. Try a luck, look it up in constant
13694 pool and return its loc_descr of its address. */
13696 static dw_loc_descr_ref
13697 cst_pool_loc_descr (tree loc)
13699 /* Get an RTL for this, if something has been emitted. */
13700 rtx rtl = lookup_constant_def (loc);
13702 if (!rtl || !MEM_P (rtl))
13704 gcc_assert (!rtl);
13705 return 0;
13707 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13709 /* TODO: We might get more coverage if we was actually delaying expansion
13710 of all expressions till end of compilation when constant pools are fully
13711 populated. */
13712 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13714 expansion_failed (loc, NULL_RTX,
13715 "CST value in contant pool but not marked.");
13716 return 0;
13718 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13719 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13722 /* Return dw_loc_list representing address of addr_expr LOC
13723 by looking for inner INDIRECT_REF expression and turning
13724 it into simple arithmetics. */
13726 static dw_loc_list_ref
13727 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13729 tree obj, offset;
13730 HOST_WIDE_INT bitsize, bitpos, bytepos;
13731 enum machine_mode mode;
13732 int unsignedp, volatilep = 0;
13733 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13735 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13736 &bitsize, &bitpos, &offset, &mode,
13737 &unsignedp, &volatilep, false);
13738 STRIP_NOPS (obj);
13739 if (bitpos % BITS_PER_UNIT)
13741 expansion_failed (loc, NULL_RTX, "bitfield access");
13742 return 0;
13744 if (!INDIRECT_REF_P (obj))
13746 expansion_failed (obj,
13747 NULL_RTX, "no indirect ref in inner refrence");
13748 return 0;
13750 if (!offset && !bitpos)
13751 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13752 else if (toplev
13753 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13754 && (dwarf_version >= 4 || !dwarf_strict))
13756 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13757 if (!list_ret)
13758 return 0;
13759 if (offset)
13761 /* Variable offset. */
13762 list_ret1 = loc_list_from_tree (offset, 0);
13763 if (list_ret1 == 0)
13764 return 0;
13765 add_loc_list (&list_ret, list_ret1);
13766 if (!list_ret)
13767 return 0;
13768 add_loc_descr_to_each (list_ret,
13769 new_loc_descr (DW_OP_plus, 0, 0));
13771 bytepos = bitpos / BITS_PER_UNIT;
13772 if (bytepos > 0)
13773 add_loc_descr_to_each (list_ret,
13774 new_loc_descr (DW_OP_plus_uconst,
13775 bytepos, 0));
13776 else if (bytepos < 0)
13777 loc_list_plus_const (list_ret, bytepos);
13778 add_loc_descr_to_each (list_ret,
13779 new_loc_descr (DW_OP_stack_value, 0, 0));
13781 return list_ret;
13785 /* Generate Dwarf location list representing LOC.
13786 If WANT_ADDRESS is false, expression computing LOC will be computed
13787 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13788 if WANT_ADDRESS is 2, expression computing address useable in location
13789 will be returned (i.e. DW_OP_reg can be used
13790 to refer to register values). */
13792 static dw_loc_list_ref
13793 loc_list_from_tree (tree loc, int want_address)
13795 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13796 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13797 int have_address = 0;
13798 enum dwarf_location_atom op;
13800 /* ??? Most of the time we do not take proper care for sign/zero
13801 extending the values properly. Hopefully this won't be a real
13802 problem... */
13804 switch (TREE_CODE (loc))
13806 case ERROR_MARK:
13807 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13808 return 0;
13810 case PLACEHOLDER_EXPR:
13811 /* This case involves extracting fields from an object to determine the
13812 position of other fields. We don't try to encode this here. The
13813 only user of this is Ada, which encodes the needed information using
13814 the names of types. */
13815 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13816 return 0;
13818 case CALL_EXPR:
13819 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13820 /* There are no opcodes for these operations. */
13821 return 0;
13823 case PREINCREMENT_EXPR:
13824 case PREDECREMENT_EXPR:
13825 case POSTINCREMENT_EXPR:
13826 case POSTDECREMENT_EXPR:
13827 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13828 /* There are no opcodes for these operations. */
13829 return 0;
13831 case ADDR_EXPR:
13832 /* If we already want an address, see if there is INDIRECT_REF inside
13833 e.g. for &this->field. */
13834 if (want_address)
13836 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13837 (loc, want_address == 2);
13838 if (list_ret)
13839 have_address = 1;
13840 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13841 && (ret = cst_pool_loc_descr (loc)))
13842 have_address = 1;
13844 /* Otherwise, process the argument and look for the address. */
13845 if (!list_ret && !ret)
13846 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13847 else
13849 if (want_address)
13850 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13851 return NULL;
13853 break;
13855 case VAR_DECL:
13856 if (DECL_THREAD_LOCAL_P (loc))
13858 rtx rtl;
13859 enum dwarf_location_atom tls_op;
13860 enum dtprel_bool dtprel = dtprel_false;
13862 if (targetm.have_tls)
13864 /* If this is not defined, we have no way to emit the
13865 data. */
13866 if (!targetm.asm_out.output_dwarf_dtprel)
13867 return 0;
13869 /* The way DW_OP_GNU_push_tls_address is specified, we
13870 can only look up addresses of objects in the current
13871 module. We used DW_OP_addr as first op, but that's
13872 wrong, because DW_OP_addr is relocated by the debug
13873 info consumer, while DW_OP_GNU_push_tls_address
13874 operand shouldn't be. */
13875 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13876 return 0;
13877 dtprel = dtprel_true;
13878 tls_op = DW_OP_GNU_push_tls_address;
13880 else
13882 if (!targetm.emutls.debug_form_tls_address
13883 || !(dwarf_version >= 3 || !dwarf_strict))
13884 return 0;
13885 /* We stuffed the control variable into the DECL_VALUE_EXPR
13886 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13887 no longer appear in gimple code. We used the control
13888 variable in specific so that we could pick it up here. */
13889 loc = DECL_VALUE_EXPR (loc);
13890 tls_op = DW_OP_form_tls_address;
13893 rtl = rtl_for_decl_location (loc);
13894 if (rtl == NULL_RTX)
13895 return 0;
13897 if (!MEM_P (rtl))
13898 return 0;
13899 rtl = XEXP (rtl, 0);
13900 if (! CONSTANT_P (rtl))
13901 return 0;
13903 ret = new_addr_loc_descr (rtl, dtprel);
13904 ret1 = new_loc_descr (tls_op, 0, 0);
13905 add_loc_descr (&ret, ret1);
13907 have_address = 1;
13908 break;
13910 /* FALLTHRU */
13912 case PARM_DECL:
13913 case RESULT_DECL:
13914 if (DECL_HAS_VALUE_EXPR_P (loc))
13915 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13916 want_address);
13917 /* FALLTHRU */
13919 case FUNCTION_DECL:
13921 rtx rtl;
13922 var_loc_list *loc_list = lookup_decl_loc (loc);
13924 if (loc_list && loc_list->first)
13926 list_ret = dw_loc_list (loc_list, loc, want_address);
13927 have_address = want_address != 0;
13928 break;
13930 rtl = rtl_for_decl_location (loc);
13931 if (rtl == NULL_RTX)
13933 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13934 return 0;
13936 else if (CONST_INT_P (rtl))
13938 HOST_WIDE_INT val = INTVAL (rtl);
13939 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13940 val &= GET_MODE_MASK (DECL_MODE (loc));
13941 ret = int_loc_descriptor (val);
13943 else if (GET_CODE (rtl) == CONST_STRING)
13945 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13946 return 0;
13948 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13949 ret = new_addr_loc_descr (rtl, dtprel_false);
13950 else
13952 enum machine_mode mode, mem_mode;
13954 /* Certain constructs can only be represented at top-level. */
13955 if (want_address == 2)
13957 ret = loc_descriptor (rtl, VOIDmode,
13958 VAR_INIT_STATUS_INITIALIZED);
13959 have_address = 1;
13961 else
13963 mode = GET_MODE (rtl);
13964 mem_mode = VOIDmode;
13965 if (MEM_P (rtl))
13967 mem_mode = mode;
13968 mode = get_address_mode (rtl);
13969 rtl = XEXP (rtl, 0);
13970 have_address = 1;
13972 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13973 VAR_INIT_STATUS_INITIALIZED);
13975 if (!ret)
13976 expansion_failed (loc, rtl,
13977 "failed to produce loc descriptor for rtl");
13980 break;
13982 case MEM_REF:
13983 /* ??? FIXME. */
13984 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13985 return 0;
13986 /* Fallthru. */
13987 case INDIRECT_REF:
13988 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13989 have_address = 1;
13990 break;
13992 case COMPOUND_EXPR:
13993 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13995 CASE_CONVERT:
13996 case VIEW_CONVERT_EXPR:
13997 case SAVE_EXPR:
13998 case MODIFY_EXPR:
13999 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14001 case COMPONENT_REF:
14002 case BIT_FIELD_REF:
14003 case ARRAY_REF:
14004 case ARRAY_RANGE_REF:
14005 case REALPART_EXPR:
14006 case IMAGPART_EXPR:
14008 tree obj, offset;
14009 HOST_WIDE_INT bitsize, bitpos, bytepos;
14010 enum machine_mode mode;
14011 int unsignedp, volatilep = 0;
14013 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14014 &unsignedp, &volatilep, false);
14016 gcc_assert (obj != loc);
14018 list_ret = loc_list_from_tree (obj,
14019 want_address == 2
14020 && !bitpos && !offset ? 2 : 1);
14021 /* TODO: We can extract value of the small expression via shifting even
14022 for nonzero bitpos. */
14023 if (list_ret == 0)
14024 return 0;
14025 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14027 expansion_failed (loc, NULL_RTX,
14028 "bitfield access");
14029 return 0;
14032 if (offset != NULL_TREE)
14034 /* Variable offset. */
14035 list_ret1 = loc_list_from_tree (offset, 0);
14036 if (list_ret1 == 0)
14037 return 0;
14038 add_loc_list (&list_ret, list_ret1);
14039 if (!list_ret)
14040 return 0;
14041 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14044 bytepos = bitpos / BITS_PER_UNIT;
14045 if (bytepos > 0)
14046 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14047 else if (bytepos < 0)
14048 loc_list_plus_const (list_ret, bytepos);
14050 have_address = 1;
14051 break;
14054 case INTEGER_CST:
14055 if ((want_address || !host_integerp (loc, 0))
14056 && (ret = cst_pool_loc_descr (loc)))
14057 have_address = 1;
14058 else if (want_address == 2
14059 && host_integerp (loc, 0)
14060 && (ret = address_of_int_loc_descriptor
14061 (int_size_in_bytes (TREE_TYPE (loc)),
14062 tree_low_cst (loc, 0))))
14063 have_address = 1;
14064 else if (host_integerp (loc, 0))
14065 ret = int_loc_descriptor (tree_low_cst (loc, 0));
14066 else
14068 expansion_failed (loc, NULL_RTX,
14069 "Integer operand is not host integer");
14070 return 0;
14072 break;
14074 case CONSTRUCTOR:
14075 case REAL_CST:
14076 case STRING_CST:
14077 case COMPLEX_CST:
14078 if ((ret = cst_pool_loc_descr (loc)))
14079 have_address = 1;
14080 else
14081 /* We can construct small constants here using int_loc_descriptor. */
14082 expansion_failed (loc, NULL_RTX,
14083 "constructor or constant not in constant pool");
14084 break;
14086 case TRUTH_AND_EXPR:
14087 case TRUTH_ANDIF_EXPR:
14088 case BIT_AND_EXPR:
14089 op = DW_OP_and;
14090 goto do_binop;
14092 case TRUTH_XOR_EXPR:
14093 case BIT_XOR_EXPR:
14094 op = DW_OP_xor;
14095 goto do_binop;
14097 case TRUTH_OR_EXPR:
14098 case TRUTH_ORIF_EXPR:
14099 case BIT_IOR_EXPR:
14100 op = DW_OP_or;
14101 goto do_binop;
14103 case FLOOR_DIV_EXPR:
14104 case CEIL_DIV_EXPR:
14105 case ROUND_DIV_EXPR:
14106 case TRUNC_DIV_EXPR:
14107 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14108 return 0;
14109 op = DW_OP_div;
14110 goto do_binop;
14112 case MINUS_EXPR:
14113 op = DW_OP_minus;
14114 goto do_binop;
14116 case FLOOR_MOD_EXPR:
14117 case CEIL_MOD_EXPR:
14118 case ROUND_MOD_EXPR:
14119 case TRUNC_MOD_EXPR:
14120 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14122 op = DW_OP_mod;
14123 goto do_binop;
14125 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14126 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14127 if (list_ret == 0 || list_ret1 == 0)
14128 return 0;
14130 add_loc_list (&list_ret, list_ret1);
14131 if (list_ret == 0)
14132 return 0;
14133 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14134 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14135 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14136 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14137 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14138 break;
14140 case MULT_EXPR:
14141 op = DW_OP_mul;
14142 goto do_binop;
14144 case LSHIFT_EXPR:
14145 op = DW_OP_shl;
14146 goto do_binop;
14148 case RSHIFT_EXPR:
14149 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14150 goto do_binop;
14152 case POINTER_PLUS_EXPR:
14153 case PLUS_EXPR:
14154 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14156 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14157 if (list_ret == 0)
14158 return 0;
14160 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14161 break;
14164 op = DW_OP_plus;
14165 goto do_binop;
14167 case LE_EXPR:
14168 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14169 return 0;
14171 op = DW_OP_le;
14172 goto do_binop;
14174 case GE_EXPR:
14175 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14176 return 0;
14178 op = DW_OP_ge;
14179 goto do_binop;
14181 case LT_EXPR:
14182 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14183 return 0;
14185 op = DW_OP_lt;
14186 goto do_binop;
14188 case GT_EXPR:
14189 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14190 return 0;
14192 op = DW_OP_gt;
14193 goto do_binop;
14195 case EQ_EXPR:
14196 op = DW_OP_eq;
14197 goto do_binop;
14199 case NE_EXPR:
14200 op = DW_OP_ne;
14201 goto do_binop;
14203 do_binop:
14204 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14205 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14206 if (list_ret == 0 || list_ret1 == 0)
14207 return 0;
14209 add_loc_list (&list_ret, list_ret1);
14210 if (list_ret == 0)
14211 return 0;
14212 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14213 break;
14215 case TRUTH_NOT_EXPR:
14216 case BIT_NOT_EXPR:
14217 op = DW_OP_not;
14218 goto do_unop;
14220 case ABS_EXPR:
14221 op = DW_OP_abs;
14222 goto do_unop;
14224 case NEGATE_EXPR:
14225 op = DW_OP_neg;
14226 goto do_unop;
14228 do_unop:
14229 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14230 if (list_ret == 0)
14231 return 0;
14233 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14234 break;
14236 case MIN_EXPR:
14237 case MAX_EXPR:
14239 const enum tree_code code =
14240 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14242 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14243 build2 (code, integer_type_node,
14244 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14245 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14248 /* ... fall through ... */
14250 case COND_EXPR:
14252 dw_loc_descr_ref lhs
14253 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14254 dw_loc_list_ref rhs
14255 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14256 dw_loc_descr_ref bra_node, jump_node, tmp;
14258 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14259 if (list_ret == 0 || lhs == 0 || rhs == 0)
14260 return 0;
14262 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14263 add_loc_descr_to_each (list_ret, bra_node);
14265 add_loc_list (&list_ret, rhs);
14266 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14267 add_loc_descr_to_each (list_ret, jump_node);
14269 add_loc_descr_to_each (list_ret, lhs);
14270 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14271 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14273 /* ??? Need a node to point the skip at. Use a nop. */
14274 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14275 add_loc_descr_to_each (list_ret, tmp);
14276 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14277 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14279 break;
14281 case FIX_TRUNC_EXPR:
14282 return 0;
14284 default:
14285 /* Leave front-end specific codes as simply unknown. This comes
14286 up, for instance, with the C STMT_EXPR. */
14287 if ((unsigned int) TREE_CODE (loc)
14288 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14290 expansion_failed (loc, NULL_RTX,
14291 "language specific tree node");
14292 return 0;
14295 #ifdef ENABLE_CHECKING
14296 /* Otherwise this is a generic code; we should just lists all of
14297 these explicitly. We forgot one. */
14298 gcc_unreachable ();
14299 #else
14300 /* In a release build, we want to degrade gracefully: better to
14301 generate incomplete debugging information than to crash. */
14302 return NULL;
14303 #endif
14306 if (!ret && !list_ret)
14307 return 0;
14309 if (want_address == 2 && !have_address
14310 && (dwarf_version >= 4 || !dwarf_strict))
14312 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14314 expansion_failed (loc, NULL_RTX,
14315 "DWARF address size mismatch");
14316 return 0;
14318 if (ret)
14319 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14320 else
14321 add_loc_descr_to_each (list_ret,
14322 new_loc_descr (DW_OP_stack_value, 0, 0));
14323 have_address = 1;
14325 /* Show if we can't fill the request for an address. */
14326 if (want_address && !have_address)
14328 expansion_failed (loc, NULL_RTX,
14329 "Want address and only have value");
14330 return 0;
14333 gcc_assert (!ret || !list_ret);
14335 /* If we've got an address and don't want one, dereference. */
14336 if (!want_address && have_address)
14338 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14340 if (size > DWARF2_ADDR_SIZE || size == -1)
14342 expansion_failed (loc, NULL_RTX,
14343 "DWARF address size mismatch");
14344 return 0;
14346 else if (size == DWARF2_ADDR_SIZE)
14347 op = DW_OP_deref;
14348 else
14349 op = DW_OP_deref_size;
14351 if (ret)
14352 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14353 else
14354 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14356 if (ret)
14357 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14359 return list_ret;
14362 /* Same as above but return only single location expression. */
14363 static dw_loc_descr_ref
14364 loc_descriptor_from_tree (tree loc, int want_address)
14366 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14367 if (!ret)
14368 return NULL;
14369 if (ret->dw_loc_next)
14371 expansion_failed (loc, NULL_RTX,
14372 "Location list where only loc descriptor needed");
14373 return NULL;
14375 return ret->expr;
14378 /* Given a value, round it up to the lowest multiple of `boundary'
14379 which is not less than the value itself. */
14381 static inline HOST_WIDE_INT
14382 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14384 return (((value + boundary - 1) / boundary) * boundary);
14387 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14388 pointer to the declared type for the relevant field variable, or return
14389 `integer_type_node' if the given node turns out to be an
14390 ERROR_MARK node. */
14392 static inline tree
14393 field_type (const_tree decl)
14395 tree type;
14397 if (TREE_CODE (decl) == ERROR_MARK)
14398 return integer_type_node;
14400 type = DECL_BIT_FIELD_TYPE (decl);
14401 if (type == NULL_TREE)
14402 type = TREE_TYPE (decl);
14404 return type;
14407 /* Given a pointer to a tree node, return the alignment in bits for
14408 it, or else return BITS_PER_WORD if the node actually turns out to
14409 be an ERROR_MARK node. */
14411 static inline unsigned
14412 simple_type_align_in_bits (const_tree type)
14414 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14417 static inline unsigned
14418 simple_decl_align_in_bits (const_tree decl)
14420 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14423 /* Return the result of rounding T up to ALIGN. */
14425 static inline double_int
14426 round_up_to_align (double_int t, unsigned int align)
14428 double_int alignd = double_int::from_uhwi (align);
14429 t += alignd;
14430 t += double_int_minus_one;
14431 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14432 t *= alignd;
14433 return t;
14436 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14437 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14438 or return 0 if we are unable to determine what that offset is, either
14439 because the argument turns out to be a pointer to an ERROR_MARK node, or
14440 because the offset is actually variable. (We can't handle the latter case
14441 just yet). */
14443 static HOST_WIDE_INT
14444 field_byte_offset (const_tree decl)
14446 double_int object_offset_in_bits;
14447 double_int object_offset_in_bytes;
14448 double_int bitpos_int;
14450 if (TREE_CODE (decl) == ERROR_MARK)
14451 return 0;
14453 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14455 /* We cannot yet cope with fields whose positions are variable, so
14456 for now, when we see such things, we simply return 0. Someday, we may
14457 be able to handle such cases, but it will be damn difficult. */
14458 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14459 return 0;
14461 bitpos_int = tree_to_double_int (bit_position (decl));
14463 #ifdef PCC_BITFIELD_TYPE_MATTERS
14464 if (PCC_BITFIELD_TYPE_MATTERS)
14466 tree type;
14467 tree field_size_tree;
14468 double_int deepest_bitpos;
14469 double_int field_size_in_bits;
14470 unsigned int type_align_in_bits;
14471 unsigned int decl_align_in_bits;
14472 double_int type_size_in_bits;
14474 type = field_type (decl);
14475 type_size_in_bits = double_int_type_size_in_bits (type);
14476 type_align_in_bits = simple_type_align_in_bits (type);
14478 field_size_tree = DECL_SIZE (decl);
14480 /* The size could be unspecified if there was an error, or for
14481 a flexible array member. */
14482 if (!field_size_tree)
14483 field_size_tree = bitsize_zero_node;
14485 /* If the size of the field is not constant, use the type size. */
14486 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14487 field_size_in_bits = tree_to_double_int (field_size_tree);
14488 else
14489 field_size_in_bits = type_size_in_bits;
14491 decl_align_in_bits = simple_decl_align_in_bits (decl);
14493 /* The GCC front-end doesn't make any attempt to keep track of the
14494 starting bit offset (relative to the start of the containing
14495 structure type) of the hypothetical "containing object" for a
14496 bit-field. Thus, when computing the byte offset value for the
14497 start of the "containing object" of a bit-field, we must deduce
14498 this information on our own. This can be rather tricky to do in
14499 some cases. For example, handling the following structure type
14500 definition when compiling for an i386/i486 target (which only
14501 aligns long long's to 32-bit boundaries) can be very tricky:
14503 struct S { int field1; long long field2:31; };
14505 Fortunately, there is a simple rule-of-thumb which can be used
14506 in such cases. When compiling for an i386/i486, GCC will
14507 allocate 8 bytes for the structure shown above. It decides to
14508 do this based upon one simple rule for bit-field allocation.
14509 GCC allocates each "containing object" for each bit-field at
14510 the first (i.e. lowest addressed) legitimate alignment boundary
14511 (based upon the required minimum alignment for the declared
14512 type of the field) which it can possibly use, subject to the
14513 condition that there is still enough available space remaining
14514 in the containing object (when allocated at the selected point)
14515 to fully accommodate all of the bits of the bit-field itself.
14517 This simple rule makes it obvious why GCC allocates 8 bytes for
14518 each object of the structure type shown above. When looking
14519 for a place to allocate the "containing object" for `field2',
14520 the compiler simply tries to allocate a 64-bit "containing
14521 object" at each successive 32-bit boundary (starting at zero)
14522 until it finds a place to allocate that 64- bit field such that
14523 at least 31 contiguous (and previously unallocated) bits remain
14524 within that selected 64 bit field. (As it turns out, for the
14525 example above, the compiler finds it is OK to allocate the
14526 "containing object" 64-bit field at bit-offset zero within the
14527 structure type.)
14529 Here we attempt to work backwards from the limited set of facts
14530 we're given, and we try to deduce from those facts, where GCC
14531 must have believed that the containing object started (within
14532 the structure type). The value we deduce is then used (by the
14533 callers of this routine) to generate DW_AT_location and
14534 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14535 the case of DW_AT_location, regular fields as well). */
14537 /* Figure out the bit-distance from the start of the structure to
14538 the "deepest" bit of the bit-field. */
14539 deepest_bitpos = bitpos_int + field_size_in_bits;
14541 /* This is the tricky part. Use some fancy footwork to deduce
14542 where the lowest addressed bit of the containing object must
14543 be. */
14544 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14546 /* Round up to type_align by default. This works best for
14547 bitfields. */
14548 object_offset_in_bits
14549 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14551 if (object_offset_in_bits.ugt (bitpos_int))
14553 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14555 /* Round up to decl_align instead. */
14556 object_offset_in_bits
14557 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14560 else
14561 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14562 object_offset_in_bits = bitpos_int;
14564 object_offset_in_bytes
14565 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14566 true, TRUNC_DIV_EXPR);
14567 return object_offset_in_bytes.to_shwi ();
14570 /* The following routines define various Dwarf attributes and any data
14571 associated with them. */
14573 /* Add a location description attribute value to a DIE.
14575 This emits location attributes suitable for whole variables and
14576 whole parameters. Note that the location attributes for struct fields are
14577 generated by the routine `data_member_location_attribute' below. */
14579 static inline void
14580 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14581 dw_loc_list_ref descr)
14583 if (descr == 0)
14584 return;
14585 if (single_element_loc_list_p (descr))
14586 add_AT_loc (die, attr_kind, descr->expr);
14587 else
14588 add_AT_loc_list (die, attr_kind, descr);
14591 /* Add DW_AT_accessibility attribute to DIE if needed. */
14593 static void
14594 add_accessibility_attribute (dw_die_ref die, tree decl)
14596 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14597 children, otherwise the default is DW_ACCESS_public. In DWARF2
14598 the default has always been DW_ACCESS_public. */
14599 if (TREE_PROTECTED (decl))
14600 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14601 else if (TREE_PRIVATE (decl))
14603 if (dwarf_version == 2
14604 || die->die_parent == NULL
14605 || die->die_parent->die_tag != DW_TAG_class_type)
14606 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14608 else if (dwarf_version > 2
14609 && die->die_parent
14610 && die->die_parent->die_tag == DW_TAG_class_type)
14611 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14614 /* Attach the specialized form of location attribute used for data members of
14615 struct and union types. In the special case of a FIELD_DECL node which
14616 represents a bit-field, the "offset" part of this special location
14617 descriptor must indicate the distance in bytes from the lowest-addressed
14618 byte of the containing struct or union type to the lowest-addressed byte of
14619 the "containing object" for the bit-field. (See the `field_byte_offset'
14620 function above).
14622 For any given bit-field, the "containing object" is a hypothetical object
14623 (of some integral or enum type) within which the given bit-field lives. The
14624 type of this hypothetical "containing object" is always the same as the
14625 declared type of the individual bit-field itself (for GCC anyway... the
14626 DWARF spec doesn't actually mandate this). Note that it is the size (in
14627 bytes) of the hypothetical "containing object" which will be given in the
14628 DW_AT_byte_size attribute for this bit-field. (See the
14629 `byte_size_attribute' function below.) It is also used when calculating the
14630 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14631 function below.) */
14633 static void
14634 add_data_member_location_attribute (dw_die_ref die, tree decl)
14636 HOST_WIDE_INT offset;
14637 dw_loc_descr_ref loc_descr = 0;
14639 if (TREE_CODE (decl) == TREE_BINFO)
14641 /* We're working on the TAG_inheritance for a base class. */
14642 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14644 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14645 aren't at a fixed offset from all (sub)objects of the same
14646 type. We need to extract the appropriate offset from our
14647 vtable. The following dwarf expression means
14649 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14651 This is specific to the V3 ABI, of course. */
14653 dw_loc_descr_ref tmp;
14655 /* Make a copy of the object address. */
14656 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14657 add_loc_descr (&loc_descr, tmp);
14659 /* Extract the vtable address. */
14660 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14661 add_loc_descr (&loc_descr, tmp);
14663 /* Calculate the address of the offset. */
14664 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14665 gcc_assert (offset < 0);
14667 tmp = int_loc_descriptor (-offset);
14668 add_loc_descr (&loc_descr, tmp);
14669 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14670 add_loc_descr (&loc_descr, tmp);
14672 /* Extract the offset. */
14673 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14674 add_loc_descr (&loc_descr, tmp);
14676 /* Add it to the object address. */
14677 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14678 add_loc_descr (&loc_descr, tmp);
14680 else
14681 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14683 else
14684 offset = field_byte_offset (decl);
14686 if (! loc_descr)
14688 if (dwarf_version > 2)
14690 /* Don't need to output a location expression, just the constant. */
14691 if (offset < 0)
14692 add_AT_int (die, DW_AT_data_member_location, offset);
14693 else
14694 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14695 return;
14697 else
14699 enum dwarf_location_atom op;
14701 /* The DWARF2 standard says that we should assume that the structure
14702 address is already on the stack, so we can specify a structure
14703 field address by using DW_OP_plus_uconst. */
14704 op = DW_OP_plus_uconst;
14705 loc_descr = new_loc_descr (op, offset, 0);
14709 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14712 /* Writes integer values to dw_vec_const array. */
14714 static void
14715 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14717 while (size != 0)
14719 *dest++ = val & 0xff;
14720 val >>= 8;
14721 --size;
14725 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14727 static HOST_WIDE_INT
14728 extract_int (const unsigned char *src, unsigned int size)
14730 HOST_WIDE_INT val = 0;
14732 src += size;
14733 while (size != 0)
14735 val <<= 8;
14736 val |= *--src & 0xff;
14737 --size;
14739 return val;
14742 /* Writes double_int values to dw_vec_const array. */
14744 static void
14745 insert_double (double_int val, unsigned char *dest)
14747 unsigned char *p0 = dest;
14748 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14750 if (WORDS_BIG_ENDIAN)
14752 p0 = p1;
14753 p1 = dest;
14756 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14757 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14760 /* Writes floating point values to dw_vec_const array. */
14762 static void
14763 insert_float (const_rtx rtl, unsigned char *array)
14765 REAL_VALUE_TYPE rv;
14766 long val[4];
14767 int i;
14769 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14770 real_to_target (val, &rv, GET_MODE (rtl));
14772 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14773 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14775 insert_int (val[i], 4, array);
14776 array += 4;
14780 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14781 does not have a "location" either in memory or in a register. These
14782 things can arise in GNU C when a constant is passed as an actual parameter
14783 to an inlined function. They can also arise in C++ where declared
14784 constants do not necessarily get memory "homes". */
14786 static bool
14787 add_const_value_attribute (dw_die_ref die, rtx rtl)
14789 switch (GET_CODE (rtl))
14791 case CONST_INT:
14793 HOST_WIDE_INT val = INTVAL (rtl);
14795 if (val < 0)
14796 add_AT_int (die, DW_AT_const_value, val);
14797 else
14798 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14800 return true;
14802 case CONST_DOUBLE:
14803 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14804 floating-point constant. A CONST_DOUBLE is used whenever the
14805 constant requires more than one word in order to be adequately
14806 represented. */
14808 enum machine_mode mode = GET_MODE (rtl);
14810 if (SCALAR_FLOAT_MODE_P (mode))
14812 unsigned int length = GET_MODE_SIZE (mode);
14813 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14815 insert_float (rtl, array);
14816 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14818 else
14819 add_AT_double (die, DW_AT_const_value,
14820 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14822 return true;
14824 case CONST_VECTOR:
14826 enum machine_mode mode = GET_MODE (rtl);
14827 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14828 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14829 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14830 (length * elt_size);
14831 unsigned int i;
14832 unsigned char *p;
14834 switch (GET_MODE_CLASS (mode))
14836 case MODE_VECTOR_INT:
14837 for (i = 0, p = array; i < length; i++, p += elt_size)
14839 rtx elt = CONST_VECTOR_ELT (rtl, i);
14840 double_int val = rtx_to_double_int (elt);
14842 if (elt_size <= sizeof (HOST_WIDE_INT))
14843 insert_int (val.to_shwi (), elt_size, p);
14844 else
14846 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14847 insert_double (val, p);
14850 break;
14852 case MODE_VECTOR_FLOAT:
14853 for (i = 0, p = array; i < length; i++, p += elt_size)
14855 rtx elt = CONST_VECTOR_ELT (rtl, i);
14856 insert_float (elt, p);
14858 break;
14860 default:
14861 gcc_unreachable ();
14864 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14866 return true;
14868 case CONST_STRING:
14869 if (dwarf_version >= 4 || !dwarf_strict)
14871 dw_loc_descr_ref loc_result;
14872 resolve_one_addr (&rtl, NULL);
14873 rtl_addr:
14874 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14875 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14876 add_AT_loc (die, DW_AT_location, loc_result);
14877 vec_safe_push (used_rtx_array, rtl);
14878 return true;
14880 return false;
14882 case CONST:
14883 if (CONSTANT_P (XEXP (rtl, 0)))
14884 return add_const_value_attribute (die, XEXP (rtl, 0));
14885 /* FALLTHROUGH */
14886 case SYMBOL_REF:
14887 if (!const_ok_for_output (rtl))
14888 return false;
14889 case LABEL_REF:
14890 if (dwarf_version >= 4 || !dwarf_strict)
14891 goto rtl_addr;
14892 return false;
14894 case PLUS:
14895 /* In cases where an inlined instance of an inline function is passed
14896 the address of an `auto' variable (which is local to the caller) we
14897 can get a situation where the DECL_RTL of the artificial local
14898 variable (for the inlining) which acts as a stand-in for the
14899 corresponding formal parameter (of the inline function) will look
14900 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14901 exactly a compile-time constant expression, but it isn't the address
14902 of the (artificial) local variable either. Rather, it represents the
14903 *value* which the artificial local variable always has during its
14904 lifetime. We currently have no way to represent such quasi-constant
14905 values in Dwarf, so for now we just punt and generate nothing. */
14906 return false;
14908 case HIGH:
14909 case CONST_FIXED:
14910 return false;
14912 case MEM:
14913 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14914 && MEM_READONLY_P (rtl)
14915 && GET_MODE (rtl) == BLKmode)
14917 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14918 return true;
14920 return false;
14922 default:
14923 /* No other kinds of rtx should be possible here. */
14924 gcc_unreachable ();
14926 return false;
14929 /* Determine whether the evaluation of EXPR references any variables
14930 or functions which aren't otherwise used (and therefore may not be
14931 output). */
14932 static tree
14933 reference_to_unused (tree * tp, int * walk_subtrees,
14934 void * data ATTRIBUTE_UNUSED)
14936 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14937 *walk_subtrees = 0;
14939 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14940 && ! TREE_ASM_WRITTEN (*tp))
14941 return *tp;
14942 /* ??? The C++ FE emits debug information for using decls, so
14943 putting gcc_unreachable here falls over. See PR31899. For now
14944 be conservative. */
14945 else if (!cgraph_global_info_ready
14946 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14947 return *tp;
14948 else if (TREE_CODE (*tp) == VAR_DECL)
14950 struct varpool_node *node = varpool_get_node (*tp);
14951 if (!node || !node->symbol.definition)
14952 return *tp;
14954 else if (TREE_CODE (*tp) == FUNCTION_DECL
14955 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14957 /* The call graph machinery must have finished analyzing,
14958 optimizing and gimplifying the CU by now.
14959 So if *TP has no call graph node associated
14960 to it, it means *TP will not be emitted. */
14961 if (!cgraph_get_node (*tp))
14962 return *tp;
14964 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14965 return *tp;
14967 return NULL_TREE;
14970 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14971 for use in a later add_const_value_attribute call. */
14973 static rtx
14974 rtl_for_decl_init (tree init, tree type)
14976 rtx rtl = NULL_RTX;
14978 STRIP_NOPS (init);
14980 /* If a variable is initialized with a string constant without embedded
14981 zeros, build CONST_STRING. */
14982 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14984 tree enttype = TREE_TYPE (type);
14985 tree domain = TYPE_DOMAIN (type);
14986 enum machine_mode mode = TYPE_MODE (enttype);
14988 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14989 && domain
14990 && integer_zerop (TYPE_MIN_VALUE (domain))
14991 && compare_tree_int (TYPE_MAX_VALUE (domain),
14992 TREE_STRING_LENGTH (init) - 1) == 0
14993 && ((size_t) TREE_STRING_LENGTH (init)
14994 == strlen (TREE_STRING_POINTER (init)) + 1))
14996 rtl = gen_rtx_CONST_STRING (VOIDmode,
14997 ggc_strdup (TREE_STRING_POINTER (init)));
14998 rtl = gen_rtx_MEM (BLKmode, rtl);
14999 MEM_READONLY_P (rtl) = 1;
15002 /* Other aggregates, and complex values, could be represented using
15003 CONCAT: FIXME! */
15004 else if (AGGREGATE_TYPE_P (type)
15005 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15006 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15007 || TREE_CODE (type) == COMPLEX_TYPE)
15009 /* Vectors only work if their mode is supported by the target.
15010 FIXME: generic vectors ought to work too. */
15011 else if (TREE_CODE (type) == VECTOR_TYPE
15012 && !VECTOR_MODE_P (TYPE_MODE (type)))
15014 /* If the initializer is something that we know will expand into an
15015 immediate RTL constant, expand it now. We must be careful not to
15016 reference variables which won't be output. */
15017 else if (initializer_constant_valid_p (init, type)
15018 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15020 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15021 possible. */
15022 if (TREE_CODE (type) == VECTOR_TYPE)
15023 switch (TREE_CODE (init))
15025 case VECTOR_CST:
15026 break;
15027 case CONSTRUCTOR:
15028 if (TREE_CONSTANT (init))
15030 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15031 bool constant_p = true;
15032 tree value;
15033 unsigned HOST_WIDE_INT ix;
15035 /* Even when ctor is constant, it might contain non-*_CST
15036 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15037 belong into VECTOR_CST nodes. */
15038 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15039 if (!CONSTANT_CLASS_P (value))
15041 constant_p = false;
15042 break;
15045 if (constant_p)
15047 init = build_vector_from_ctor (type, elts);
15048 break;
15051 /* FALLTHRU */
15053 default:
15054 return NULL;
15057 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15059 /* If expand_expr returns a MEM, it wasn't immediate. */
15060 gcc_assert (!rtl || !MEM_P (rtl));
15063 return rtl;
15066 /* Generate RTL for the variable DECL to represent its location. */
15068 static rtx
15069 rtl_for_decl_location (tree decl)
15071 rtx rtl;
15073 /* Here we have to decide where we are going to say the parameter "lives"
15074 (as far as the debugger is concerned). We only have a couple of
15075 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15077 DECL_RTL normally indicates where the parameter lives during most of the
15078 activation of the function. If optimization is enabled however, this
15079 could be either NULL or else a pseudo-reg. Both of those cases indicate
15080 that the parameter doesn't really live anywhere (as far as the code
15081 generation parts of GCC are concerned) during most of the function's
15082 activation. That will happen (for example) if the parameter is never
15083 referenced within the function.
15085 We could just generate a location descriptor here for all non-NULL
15086 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15087 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15088 where DECL_RTL is NULL or is a pseudo-reg.
15090 Note however that we can only get away with using DECL_INCOMING_RTL as
15091 a backup substitute for DECL_RTL in certain limited cases. In cases
15092 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15093 we can be sure that the parameter was passed using the same type as it is
15094 declared to have within the function, and that its DECL_INCOMING_RTL
15095 points us to a place where a value of that type is passed.
15097 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15098 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15099 because in these cases DECL_INCOMING_RTL points us to a value of some
15100 type which is *different* from the type of the parameter itself. Thus,
15101 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15102 such cases, the debugger would end up (for example) trying to fetch a
15103 `float' from a place which actually contains the first part of a
15104 `double'. That would lead to really incorrect and confusing
15105 output at debug-time.
15107 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15108 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15109 are a couple of exceptions however. On little-endian machines we can
15110 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15111 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15112 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15113 when (on a little-endian machine) a non-prototyped function has a
15114 parameter declared to be of type `short' or `char'. In such cases,
15115 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15116 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15117 passed `int' value. If the debugger then uses that address to fetch
15118 a `short' or a `char' (on a little-endian machine) the result will be
15119 the correct data, so we allow for such exceptional cases below.
15121 Note that our goal here is to describe the place where the given formal
15122 parameter lives during most of the function's activation (i.e. between the
15123 end of the prologue and the start of the epilogue). We'll do that as best
15124 as we can. Note however that if the given formal parameter is modified
15125 sometime during the execution of the function, then a stack backtrace (at
15126 debug-time) will show the function as having been called with the *new*
15127 value rather than the value which was originally passed in. This happens
15128 rarely enough that it is not a major problem, but it *is* a problem, and
15129 I'd like to fix it.
15131 A future version of dwarf2out.c may generate two additional attributes for
15132 any given DW_TAG_formal_parameter DIE which will describe the "passed
15133 type" and the "passed location" for the given formal parameter in addition
15134 to the attributes we now generate to indicate the "declared type" and the
15135 "active location" for each parameter. This additional set of attributes
15136 could be used by debuggers for stack backtraces. Separately, note that
15137 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15138 This happens (for example) for inlined-instances of inline function formal
15139 parameters which are never referenced. This really shouldn't be
15140 happening. All PARM_DECL nodes should get valid non-NULL
15141 DECL_INCOMING_RTL values. FIXME. */
15143 /* Use DECL_RTL as the "location" unless we find something better. */
15144 rtl = DECL_RTL_IF_SET (decl);
15146 /* When generating abstract instances, ignore everything except
15147 constants, symbols living in memory, and symbols living in
15148 fixed registers. */
15149 if (! reload_completed)
15151 if (rtl
15152 && (CONSTANT_P (rtl)
15153 || (MEM_P (rtl)
15154 && CONSTANT_P (XEXP (rtl, 0)))
15155 || (REG_P (rtl)
15156 && TREE_CODE (decl) == VAR_DECL
15157 && TREE_STATIC (decl))))
15159 rtl = targetm.delegitimize_address (rtl);
15160 return rtl;
15162 rtl = NULL_RTX;
15164 else if (TREE_CODE (decl) == PARM_DECL)
15166 if (rtl == NULL_RTX
15167 || is_pseudo_reg (rtl)
15168 || (MEM_P (rtl)
15169 && is_pseudo_reg (XEXP (rtl, 0))
15170 && DECL_INCOMING_RTL (decl)
15171 && MEM_P (DECL_INCOMING_RTL (decl))
15172 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15174 tree declared_type = TREE_TYPE (decl);
15175 tree passed_type = DECL_ARG_TYPE (decl);
15176 enum machine_mode dmode = TYPE_MODE (declared_type);
15177 enum machine_mode pmode = TYPE_MODE (passed_type);
15179 /* This decl represents a formal parameter which was optimized out.
15180 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15181 all cases where (rtl == NULL_RTX) just below. */
15182 if (dmode == pmode)
15183 rtl = DECL_INCOMING_RTL (decl);
15184 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15185 && SCALAR_INT_MODE_P (dmode)
15186 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15187 && DECL_INCOMING_RTL (decl))
15189 rtx inc = DECL_INCOMING_RTL (decl);
15190 if (REG_P (inc))
15191 rtl = inc;
15192 else if (MEM_P (inc))
15194 if (BYTES_BIG_ENDIAN)
15195 rtl = adjust_address_nv (inc, dmode,
15196 GET_MODE_SIZE (pmode)
15197 - GET_MODE_SIZE (dmode));
15198 else
15199 rtl = inc;
15204 /* If the parm was passed in registers, but lives on the stack, then
15205 make a big endian correction if the mode of the type of the
15206 parameter is not the same as the mode of the rtl. */
15207 /* ??? This is the same series of checks that are made in dbxout.c before
15208 we reach the big endian correction code there. It isn't clear if all
15209 of these checks are necessary here, but keeping them all is the safe
15210 thing to do. */
15211 else if (MEM_P (rtl)
15212 && XEXP (rtl, 0) != const0_rtx
15213 && ! CONSTANT_P (XEXP (rtl, 0))
15214 /* Not passed in memory. */
15215 && !MEM_P (DECL_INCOMING_RTL (decl))
15216 /* Not passed by invisible reference. */
15217 && (!REG_P (XEXP (rtl, 0))
15218 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15219 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15220 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15221 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15222 #endif
15224 /* Big endian correction check. */
15225 && BYTES_BIG_ENDIAN
15226 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15227 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15228 < UNITS_PER_WORD))
15230 enum machine_mode addr_mode = get_address_mode (rtl);
15231 int offset = (UNITS_PER_WORD
15232 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15234 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15235 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15238 else if (TREE_CODE (decl) == VAR_DECL
15239 && rtl
15240 && MEM_P (rtl)
15241 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15242 && BYTES_BIG_ENDIAN)
15244 enum machine_mode addr_mode = get_address_mode (rtl);
15245 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15246 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15248 /* If a variable is declared "register" yet is smaller than
15249 a register, then if we store the variable to memory, it
15250 looks like we're storing a register-sized value, when in
15251 fact we are not. We need to adjust the offset of the
15252 storage location to reflect the actual value's bytes,
15253 else gdb will not be able to display it. */
15254 if (rsize > dsize)
15255 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15256 plus_constant (addr_mode, XEXP (rtl, 0),
15257 rsize - dsize));
15260 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15261 and will have been substituted directly into all expressions that use it.
15262 C does not have such a concept, but C++ and other languages do. */
15263 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15264 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15266 if (rtl)
15267 rtl = targetm.delegitimize_address (rtl);
15269 /* If we don't look past the constant pool, we risk emitting a
15270 reference to a constant pool entry that isn't referenced from
15271 code, and thus is not emitted. */
15272 if (rtl)
15273 rtl = avoid_constant_pool_reference (rtl);
15275 /* Try harder to get a rtl. If this symbol ends up not being emitted
15276 in the current CU, resolve_addr will remove the expression referencing
15277 it. */
15278 if (rtl == NULL_RTX
15279 && TREE_CODE (decl) == VAR_DECL
15280 && !DECL_EXTERNAL (decl)
15281 && TREE_STATIC (decl)
15282 && DECL_NAME (decl)
15283 && !DECL_HARD_REGISTER (decl)
15284 && DECL_MODE (decl) != VOIDmode)
15286 rtl = make_decl_rtl_for_debug (decl);
15287 if (!MEM_P (rtl)
15288 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15289 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15290 rtl = NULL_RTX;
15293 return rtl;
15296 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15297 returned. If so, the decl for the COMMON block is returned, and the
15298 value is the offset into the common block for the symbol. */
15300 static tree
15301 fortran_common (tree decl, HOST_WIDE_INT *value)
15303 tree val_expr, cvar;
15304 enum machine_mode mode;
15305 HOST_WIDE_INT bitsize, bitpos;
15306 tree offset;
15307 int unsignedp, volatilep = 0;
15309 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15310 it does not have a value (the offset into the common area), or if it
15311 is thread local (as opposed to global) then it isn't common, and shouldn't
15312 be handled as such. */
15313 if (TREE_CODE (decl) != VAR_DECL
15314 || !TREE_STATIC (decl)
15315 || !DECL_HAS_VALUE_EXPR_P (decl)
15316 || !is_fortran ())
15317 return NULL_TREE;
15319 val_expr = DECL_VALUE_EXPR (decl);
15320 if (TREE_CODE (val_expr) != COMPONENT_REF)
15321 return NULL_TREE;
15323 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15324 &mode, &unsignedp, &volatilep, true);
15326 if (cvar == NULL_TREE
15327 || TREE_CODE (cvar) != VAR_DECL
15328 || DECL_ARTIFICIAL (cvar)
15329 || !TREE_PUBLIC (cvar))
15330 return NULL_TREE;
15332 *value = 0;
15333 if (offset != NULL)
15335 if (!host_integerp (offset, 0))
15336 return NULL_TREE;
15337 *value = tree_low_cst (offset, 0);
15339 if (bitpos != 0)
15340 *value += bitpos / BITS_PER_UNIT;
15342 return cvar;
15345 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15346 data attribute for a variable or a parameter. We generate the
15347 DW_AT_const_value attribute only in those cases where the given variable
15348 or parameter does not have a true "location" either in memory or in a
15349 register. This can happen (for example) when a constant is passed as an
15350 actual argument in a call to an inline function. (It's possible that
15351 these things can crop up in other ways also.) Note that one type of
15352 constant value which can be passed into an inlined function is a constant
15353 pointer. This can happen for example if an actual argument in an inlined
15354 function call evaluates to a compile-time constant address.
15356 CACHE_P is true if it is worth caching the location list for DECL,
15357 so that future calls can reuse it rather than regenerate it from scratch.
15358 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15359 since we will need to refer to them each time the function is inlined. */
15361 static bool
15362 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15363 enum dwarf_attribute attr)
15365 rtx rtl;
15366 dw_loc_list_ref list;
15367 var_loc_list *loc_list;
15368 cached_dw_loc_list *cache;
15369 void **slot;
15371 if (TREE_CODE (decl) == ERROR_MARK)
15372 return false;
15374 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15375 || TREE_CODE (decl) == RESULT_DECL);
15377 /* Try to get some constant RTL for this decl, and use that as the value of
15378 the location. */
15380 rtl = rtl_for_decl_location (decl);
15381 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15382 && add_const_value_attribute (die, rtl))
15383 return true;
15385 /* See if we have single element location list that is equivalent to
15386 a constant value. That way we are better to use add_const_value_attribute
15387 rather than expanding constant value equivalent. */
15388 loc_list = lookup_decl_loc (decl);
15389 if (loc_list
15390 && loc_list->first
15391 && loc_list->first->next == NULL
15392 && NOTE_P (loc_list->first->loc)
15393 && NOTE_VAR_LOCATION (loc_list->first->loc)
15394 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15396 struct var_loc_node *node;
15398 node = loc_list->first;
15399 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15400 if (GET_CODE (rtl) == EXPR_LIST)
15401 rtl = XEXP (rtl, 0);
15402 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15403 && add_const_value_attribute (die, rtl))
15404 return true;
15406 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15407 list several times. See if we've already cached the contents. */
15408 list = NULL;
15409 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15410 cache_p = false;
15411 if (cache_p)
15413 cache = (cached_dw_loc_list *)
15414 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15415 if (cache)
15416 list = cache->loc_list;
15418 if (list == NULL)
15420 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15421 /* It is usually worth caching this result if the decl is from
15422 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15423 if (cache_p && list && list->dw_loc_next)
15425 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15426 DECL_UID (decl), INSERT);
15427 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15428 cache->decl_id = DECL_UID (decl);
15429 cache->loc_list = list;
15430 *slot = cache;
15433 if (list)
15435 add_AT_location_description (die, attr, list);
15436 return true;
15438 /* None of that worked, so it must not really have a location;
15439 try adding a constant value attribute from the DECL_INITIAL. */
15440 return tree_add_const_value_attribute_for_decl (die, decl);
15443 /* Add VARIABLE and DIE into deferred locations list. */
15445 static void
15446 defer_location (tree variable, dw_die_ref die)
15448 deferred_locations entry;
15449 entry.variable = variable;
15450 entry.die = die;
15451 vec_safe_push (deferred_locations_list, entry);
15454 /* Helper function for tree_add_const_value_attribute. Natively encode
15455 initializer INIT into an array. Return true if successful. */
15457 static bool
15458 native_encode_initializer (tree init, unsigned char *array, int size)
15460 tree type;
15462 if (init == NULL_TREE)
15463 return false;
15465 STRIP_NOPS (init);
15466 switch (TREE_CODE (init))
15468 case STRING_CST:
15469 type = TREE_TYPE (init);
15470 if (TREE_CODE (type) == ARRAY_TYPE)
15472 tree enttype = TREE_TYPE (type);
15473 enum machine_mode mode = TYPE_MODE (enttype);
15475 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15476 return false;
15477 if (int_size_in_bytes (type) != size)
15478 return false;
15479 if (size > TREE_STRING_LENGTH (init))
15481 memcpy (array, TREE_STRING_POINTER (init),
15482 TREE_STRING_LENGTH (init));
15483 memset (array + TREE_STRING_LENGTH (init),
15484 '\0', size - TREE_STRING_LENGTH (init));
15486 else
15487 memcpy (array, TREE_STRING_POINTER (init), size);
15488 return true;
15490 return false;
15491 case CONSTRUCTOR:
15492 type = TREE_TYPE (init);
15493 if (int_size_in_bytes (type) != size)
15494 return false;
15495 if (TREE_CODE (type) == ARRAY_TYPE)
15497 HOST_WIDE_INT min_index;
15498 unsigned HOST_WIDE_INT cnt;
15499 int curpos = 0, fieldsize;
15500 constructor_elt *ce;
15502 if (TYPE_DOMAIN (type) == NULL_TREE
15503 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15504 return false;
15506 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15507 if (fieldsize <= 0)
15508 return false;
15510 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15511 memset (array, '\0', size);
15512 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15514 tree val = ce->value;
15515 tree index = ce->index;
15516 int pos = curpos;
15517 if (index && TREE_CODE (index) == RANGE_EXPR)
15518 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15519 * fieldsize;
15520 else if (index)
15521 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15523 if (val)
15525 STRIP_NOPS (val);
15526 if (!native_encode_initializer (val, array + pos, fieldsize))
15527 return false;
15529 curpos = pos + fieldsize;
15530 if (index && TREE_CODE (index) == RANGE_EXPR)
15532 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15533 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15534 while (count-- > 0)
15536 if (val)
15537 memcpy (array + curpos, array + pos, fieldsize);
15538 curpos += fieldsize;
15541 gcc_assert (curpos <= size);
15543 return true;
15545 else if (TREE_CODE (type) == RECORD_TYPE
15546 || TREE_CODE (type) == UNION_TYPE)
15548 tree field = NULL_TREE;
15549 unsigned HOST_WIDE_INT cnt;
15550 constructor_elt *ce;
15552 if (int_size_in_bytes (type) != size)
15553 return false;
15555 if (TREE_CODE (type) == RECORD_TYPE)
15556 field = TYPE_FIELDS (type);
15558 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15560 tree val = ce->value;
15561 int pos, fieldsize;
15563 if (ce->index != 0)
15564 field = ce->index;
15566 if (val)
15567 STRIP_NOPS (val);
15569 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15570 return false;
15572 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15573 && TYPE_DOMAIN (TREE_TYPE (field))
15574 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15575 return false;
15576 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15577 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15578 return false;
15579 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15580 pos = int_byte_position (field);
15581 gcc_assert (pos + fieldsize <= size);
15582 if (val
15583 && !native_encode_initializer (val, array + pos, fieldsize))
15584 return false;
15586 return true;
15588 return false;
15589 case VIEW_CONVERT_EXPR:
15590 case NON_LVALUE_EXPR:
15591 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15592 default:
15593 return native_encode_expr (init, array, size) == size;
15597 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15598 attribute is the const value T. */
15600 static bool
15601 tree_add_const_value_attribute (dw_die_ref die, tree t)
15603 tree init;
15604 tree type = TREE_TYPE (t);
15605 rtx rtl;
15607 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15608 return false;
15610 init = t;
15611 gcc_assert (!DECL_P (init));
15613 rtl = rtl_for_decl_init (init, type);
15614 if (rtl)
15615 return add_const_value_attribute (die, rtl);
15616 /* If the host and target are sane, try harder. */
15617 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15618 && initializer_constant_valid_p (init, type))
15620 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15621 if (size > 0 && (int) size == size)
15623 unsigned char *array = (unsigned char *)
15624 ggc_alloc_cleared_atomic (size);
15626 if (native_encode_initializer (init, array, size))
15628 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15629 return true;
15631 ggc_free (array);
15634 return false;
15637 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15638 attribute is the const value of T, where T is an integral constant
15639 variable with static storage duration
15640 (so it can't be a PARM_DECL or a RESULT_DECL). */
15642 static bool
15643 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15646 if (!decl
15647 || (TREE_CODE (decl) != VAR_DECL
15648 && TREE_CODE (decl) != CONST_DECL)
15649 || (TREE_CODE (decl) == VAR_DECL
15650 && !TREE_STATIC (decl)))
15651 return false;
15653 if (TREE_READONLY (decl)
15654 && ! TREE_THIS_VOLATILE (decl)
15655 && DECL_INITIAL (decl))
15656 /* OK */;
15657 else
15658 return false;
15660 /* Don't add DW_AT_const_value if abstract origin already has one. */
15661 if (get_AT (var_die, DW_AT_const_value))
15662 return false;
15664 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15667 /* Convert the CFI instructions for the current function into a
15668 location list. This is used for DW_AT_frame_base when we targeting
15669 a dwarf2 consumer that does not support the dwarf3
15670 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15671 expressions. */
15673 static dw_loc_list_ref
15674 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15676 int ix;
15677 dw_fde_ref fde;
15678 dw_loc_list_ref list, *list_tail;
15679 dw_cfi_ref cfi;
15680 dw_cfa_location last_cfa, next_cfa;
15681 const char *start_label, *last_label, *section;
15682 dw_cfa_location remember;
15684 fde = cfun->fde;
15685 gcc_assert (fde != NULL);
15687 section = secname_for_decl (current_function_decl);
15688 list_tail = &list;
15689 list = NULL;
15691 memset (&next_cfa, 0, sizeof (next_cfa));
15692 next_cfa.reg = INVALID_REGNUM;
15693 remember = next_cfa;
15695 start_label = fde->dw_fde_begin;
15697 /* ??? Bald assumption that the CIE opcode list does not contain
15698 advance opcodes. */
15699 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15700 lookup_cfa_1 (cfi, &next_cfa, &remember);
15702 last_cfa = next_cfa;
15703 last_label = start_label;
15705 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15707 /* If the first partition contained no CFI adjustments, the
15708 CIE opcodes apply to the whole first partition. */
15709 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15710 fde->dw_fde_begin, fde->dw_fde_end, section);
15711 list_tail =&(*list_tail)->dw_loc_next;
15712 start_label = last_label = fde->dw_fde_second_begin;
15715 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15717 switch (cfi->dw_cfi_opc)
15719 case DW_CFA_set_loc:
15720 case DW_CFA_advance_loc1:
15721 case DW_CFA_advance_loc2:
15722 case DW_CFA_advance_loc4:
15723 if (!cfa_equal_p (&last_cfa, &next_cfa))
15725 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15726 start_label, last_label, section);
15728 list_tail = &(*list_tail)->dw_loc_next;
15729 last_cfa = next_cfa;
15730 start_label = last_label;
15732 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15733 break;
15735 case DW_CFA_advance_loc:
15736 /* The encoding is complex enough that we should never emit this. */
15737 gcc_unreachable ();
15739 default:
15740 lookup_cfa_1 (cfi, &next_cfa, &remember);
15741 break;
15743 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15745 if (!cfa_equal_p (&last_cfa, &next_cfa))
15747 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15748 start_label, last_label, section);
15750 list_tail = &(*list_tail)->dw_loc_next;
15751 last_cfa = next_cfa;
15752 start_label = last_label;
15754 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15755 start_label, fde->dw_fde_end, section);
15756 list_tail = &(*list_tail)->dw_loc_next;
15757 start_label = last_label = fde->dw_fde_second_begin;
15761 if (!cfa_equal_p (&last_cfa, &next_cfa))
15763 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15764 start_label, last_label, section);
15765 list_tail = &(*list_tail)->dw_loc_next;
15766 start_label = last_label;
15769 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15770 start_label,
15771 fde->dw_fde_second_begin
15772 ? fde->dw_fde_second_end : fde->dw_fde_end,
15773 section);
15775 if (list && list->dw_loc_next)
15776 gen_llsym (list);
15778 return list;
15781 /* Compute a displacement from the "steady-state frame pointer" to the
15782 frame base (often the same as the CFA), and store it in
15783 frame_pointer_fb_offset. OFFSET is added to the displacement
15784 before the latter is negated. */
15786 static void
15787 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15789 rtx reg, elim;
15791 #ifdef FRAME_POINTER_CFA_OFFSET
15792 reg = frame_pointer_rtx;
15793 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15794 #else
15795 reg = arg_pointer_rtx;
15796 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15797 #endif
15799 elim = (ira_use_lra_p
15800 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15801 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15802 if (GET_CODE (elim) == PLUS)
15804 offset += INTVAL (XEXP (elim, 1));
15805 elim = XEXP (elim, 0);
15808 frame_pointer_fb_offset = -offset;
15810 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15811 in which to eliminate. This is because it's stack pointer isn't
15812 directly accessible as a register within the ISA. To work around
15813 this, assume that while we cannot provide a proper value for
15814 frame_pointer_fb_offset, we won't need one either. */
15815 frame_pointer_fb_offset_valid
15816 = ((SUPPORTS_STACK_ALIGNMENT
15817 && (elim == hard_frame_pointer_rtx
15818 || elim == stack_pointer_rtx))
15819 || elim == (frame_pointer_needed
15820 ? hard_frame_pointer_rtx
15821 : stack_pointer_rtx));
15824 /* Generate a DW_AT_name attribute given some string value to be included as
15825 the value of the attribute. */
15827 static void
15828 add_name_attribute (dw_die_ref die, const char *name_string)
15830 if (name_string != NULL && *name_string != 0)
15832 if (demangle_name_func)
15833 name_string = (*demangle_name_func) (name_string);
15835 add_AT_string (die, DW_AT_name, name_string);
15839 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15840 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15841 of TYPE accordingly.
15843 ??? This is a temporary measure until after we're able to generate
15844 regular DWARF for the complex Ada type system. */
15846 static void
15847 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15848 dw_die_ref context_die)
15850 tree dtype;
15851 dw_die_ref dtype_die;
15853 if (!lang_hooks.types.descriptive_type)
15854 return;
15856 dtype = lang_hooks.types.descriptive_type (type);
15857 if (!dtype)
15858 return;
15860 dtype_die = lookup_type_die (dtype);
15861 if (!dtype_die)
15863 gen_type_die (dtype, context_die);
15864 dtype_die = lookup_type_die (dtype);
15865 gcc_assert (dtype_die);
15868 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15871 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
15873 static const char *
15874 comp_dir_string (void)
15876 const char *wd;
15877 char *wd1;
15878 static const char *cached_wd = NULL;
15880 if (cached_wd != NULL)
15881 return cached_wd;
15883 wd = get_src_pwd ();
15884 if (wd == NULL)
15885 return NULL;
15887 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15889 int wdlen;
15891 wdlen = strlen (wd);
15892 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15893 strcpy (wd1, wd);
15894 wd1 [wdlen] = DIR_SEPARATOR;
15895 wd1 [wdlen + 1] = 0;
15896 wd = wd1;
15899 cached_wd = remap_debug_filename (wd);
15900 return cached_wd;
15903 /* Generate a DW_AT_comp_dir attribute for DIE. */
15905 static void
15906 add_comp_dir_attribute (dw_die_ref die)
15908 const char * wd = comp_dir_string ();
15909 if (wd != NULL)
15910 add_AT_string (die, DW_AT_comp_dir, wd);
15913 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15914 default. */
15916 static int
15917 lower_bound_default (void)
15919 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15921 case DW_LANG_C:
15922 case DW_LANG_C89:
15923 case DW_LANG_C99:
15924 case DW_LANG_C_plus_plus:
15925 case DW_LANG_ObjC:
15926 case DW_LANG_ObjC_plus_plus:
15927 case DW_LANG_Java:
15928 return 0;
15929 case DW_LANG_Fortran77:
15930 case DW_LANG_Fortran90:
15931 case DW_LANG_Fortran95:
15932 return 1;
15933 case DW_LANG_UPC:
15934 case DW_LANG_D:
15935 case DW_LANG_Python:
15936 return dwarf_version >= 4 ? 0 : -1;
15937 case DW_LANG_Ada95:
15938 case DW_LANG_Ada83:
15939 case DW_LANG_Cobol74:
15940 case DW_LANG_Cobol85:
15941 case DW_LANG_Pascal83:
15942 case DW_LANG_Modula2:
15943 case DW_LANG_PLI:
15944 return dwarf_version >= 4 ? 1 : -1;
15945 default:
15946 return -1;
15950 /* Given a tree node describing an array bound (either lower or upper) output
15951 a representation for that bound. */
15953 static void
15954 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15956 switch (TREE_CODE (bound))
15958 case ERROR_MARK:
15959 return;
15961 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15962 case INTEGER_CST:
15964 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15965 int dflt;
15967 /* Use the default if possible. */
15968 if (bound_attr == DW_AT_lower_bound
15969 && host_integerp (bound, 0)
15970 && (dflt = lower_bound_default ()) != -1
15971 && tree_low_cst (bound, 0) == dflt)
15974 /* Otherwise represent the bound as an unsigned value with the
15975 precision of its type. The precision and signedness of the
15976 type will be necessary to re-interpret it unambiguously. */
15977 else if (prec < HOST_BITS_PER_WIDE_INT)
15979 unsigned HOST_WIDE_INT mask
15980 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15981 add_AT_unsigned (subrange_die, bound_attr,
15982 TREE_INT_CST_LOW (bound) & mask);
15984 else if (prec == HOST_BITS_PER_WIDE_INT
15985 || TREE_INT_CST_HIGH (bound) == 0)
15986 add_AT_unsigned (subrange_die, bound_attr,
15987 TREE_INT_CST_LOW (bound));
15988 else
15989 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15990 TREE_INT_CST_LOW (bound));
15992 break;
15994 CASE_CONVERT:
15995 case VIEW_CONVERT_EXPR:
15996 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15997 break;
15999 case SAVE_EXPR:
16000 break;
16002 case VAR_DECL:
16003 case PARM_DECL:
16004 case RESULT_DECL:
16006 dw_die_ref decl_die = lookup_decl_die (bound);
16008 /* ??? Can this happen, or should the variable have been bound
16009 first? Probably it can, since I imagine that we try to create
16010 the types of parameters in the order in which they exist in
16011 the list, and won't have created a forward reference to a
16012 later parameter. */
16013 if (decl_die != NULL)
16015 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16016 break;
16019 /* FALLTHRU */
16021 default:
16023 /* Otherwise try to create a stack operation procedure to
16024 evaluate the value of the array bound. */
16026 dw_die_ref ctx, decl_die;
16027 dw_loc_list_ref list;
16029 list = loc_list_from_tree (bound, 2);
16030 if (list == NULL || single_element_loc_list_p (list))
16032 /* If DW_AT_*bound is not a reference nor constant, it is
16033 a DWARF expression rather than location description.
16034 For that loc_list_from_tree (bound, 0) is needed.
16035 If that fails to give a single element list,
16036 fall back to outputting this as a reference anyway. */
16037 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16038 if (list2 && single_element_loc_list_p (list2))
16040 add_AT_loc (subrange_die, bound_attr, list2->expr);
16041 break;
16044 if (list == NULL)
16045 break;
16047 if (current_function_decl == 0)
16048 ctx = comp_unit_die ();
16049 else
16050 ctx = lookup_decl_die (current_function_decl);
16052 decl_die = new_die (DW_TAG_variable, ctx, bound);
16053 add_AT_flag (decl_die, DW_AT_artificial, 1);
16054 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16055 add_AT_location_description (decl_die, DW_AT_location, list);
16056 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16057 break;
16062 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16063 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16064 Note that the block of subscript information for an array type also
16065 includes information about the element type of the given array type. */
16067 static void
16068 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16070 unsigned dimension_number;
16071 tree lower, upper;
16072 dw_die_ref subrange_die;
16074 for (dimension_number = 0;
16075 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16076 type = TREE_TYPE (type), dimension_number++)
16078 tree domain = TYPE_DOMAIN (type);
16080 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16081 break;
16083 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16084 and (in GNU C only) variable bounds. Handle all three forms
16085 here. */
16086 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16087 if (domain)
16089 /* We have an array type with specified bounds. */
16090 lower = TYPE_MIN_VALUE (domain);
16091 upper = TYPE_MAX_VALUE (domain);
16093 /* Define the index type. */
16094 if (TREE_TYPE (domain))
16096 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16097 TREE_TYPE field. We can't emit debug info for this
16098 because it is an unnamed integral type. */
16099 if (TREE_CODE (domain) == INTEGER_TYPE
16100 && TYPE_NAME (domain) == NULL_TREE
16101 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16102 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16104 else
16105 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16106 type_die);
16109 /* ??? If upper is NULL, the array has unspecified length,
16110 but it does have a lower bound. This happens with Fortran
16111 dimension arr(N:*)
16112 Since the debugger is definitely going to need to know N
16113 to produce useful results, go ahead and output the lower
16114 bound solo, and hope the debugger can cope. */
16116 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16117 if (upper)
16118 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16121 /* Otherwise we have an array type with an unspecified length. The
16122 DWARF-2 spec does not say how to handle this; let's just leave out the
16123 bounds. */
16127 static void
16128 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16130 dw_die_ref decl_die;
16131 unsigned size;
16133 switch (TREE_CODE (tree_node))
16135 case ERROR_MARK:
16136 size = 0;
16137 break;
16138 case ENUMERAL_TYPE:
16139 case RECORD_TYPE:
16140 case UNION_TYPE:
16141 case QUAL_UNION_TYPE:
16142 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16143 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16145 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16146 return;
16148 size = int_size_in_bytes (tree_node);
16149 break;
16150 case FIELD_DECL:
16151 /* For a data member of a struct or union, the DW_AT_byte_size is
16152 generally given as the number of bytes normally allocated for an
16153 object of the *declared* type of the member itself. This is true
16154 even for bit-fields. */
16155 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16156 break;
16157 default:
16158 gcc_unreachable ();
16161 /* Note that `size' might be -1 when we get to this point. If it is, that
16162 indicates that the byte size of the entity in question is variable. We
16163 have no good way of expressing this fact in Dwarf at the present time,
16164 so just let the -1 pass on through. */
16165 add_AT_unsigned (die, DW_AT_byte_size, size);
16168 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16169 which specifies the distance in bits from the highest order bit of the
16170 "containing object" for the bit-field to the highest order bit of the
16171 bit-field itself.
16173 For any given bit-field, the "containing object" is a hypothetical object
16174 (of some integral or enum type) within which the given bit-field lives. The
16175 type of this hypothetical "containing object" is always the same as the
16176 declared type of the individual bit-field itself. The determination of the
16177 exact location of the "containing object" for a bit-field is rather
16178 complicated. It's handled by the `field_byte_offset' function (above).
16180 Note that it is the size (in bytes) of the hypothetical "containing object"
16181 which will be given in the DW_AT_byte_size attribute for this bit-field.
16182 (See `byte_size_attribute' above). */
16184 static inline void
16185 add_bit_offset_attribute (dw_die_ref die, tree decl)
16187 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16188 tree type = DECL_BIT_FIELD_TYPE (decl);
16189 HOST_WIDE_INT bitpos_int;
16190 HOST_WIDE_INT highest_order_object_bit_offset;
16191 HOST_WIDE_INT highest_order_field_bit_offset;
16192 HOST_WIDE_INT bit_offset;
16194 /* Must be a field and a bit field. */
16195 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16197 /* We can't yet handle bit-fields whose offsets are variable, so if we
16198 encounter such things, just return without generating any attribute
16199 whatsoever. Likewise for variable or too large size. */
16200 if (! host_integerp (bit_position (decl), 0)
16201 || ! host_integerp (DECL_SIZE (decl), 1))
16202 return;
16204 bitpos_int = int_bit_position (decl);
16206 /* Note that the bit offset is always the distance (in bits) from the
16207 highest-order bit of the "containing object" to the highest-order bit of
16208 the bit-field itself. Since the "high-order end" of any object or field
16209 is different on big-endian and little-endian machines, the computation
16210 below must take account of these differences. */
16211 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16212 highest_order_field_bit_offset = bitpos_int;
16214 if (! BYTES_BIG_ENDIAN)
16216 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16217 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16220 bit_offset
16221 = (! BYTES_BIG_ENDIAN
16222 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16223 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16225 if (bit_offset < 0)
16226 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16227 else
16228 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16231 /* For a FIELD_DECL node which represents a bit field, output an attribute
16232 which specifies the length in bits of the given field. */
16234 static inline void
16235 add_bit_size_attribute (dw_die_ref die, tree decl)
16237 /* Must be a field and a bit field. */
16238 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16239 && DECL_BIT_FIELD_TYPE (decl));
16241 if (host_integerp (DECL_SIZE (decl), 1))
16242 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16245 /* If the compiled language is ANSI C, then add a 'prototyped'
16246 attribute, if arg types are given for the parameters of a function. */
16248 static inline void
16249 add_prototyped_attribute (dw_die_ref die, tree func_type)
16251 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16252 && prototype_p (func_type))
16253 add_AT_flag (die, DW_AT_prototyped, 1);
16256 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16257 by looking in either the type declaration or object declaration
16258 equate table. */
16260 static inline dw_die_ref
16261 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16263 dw_die_ref origin_die = NULL;
16265 if (TREE_CODE (origin) != FUNCTION_DECL)
16267 /* We may have gotten separated from the block for the inlined
16268 function, if we're in an exception handler or some such; make
16269 sure that the abstract function has been written out.
16271 Doing this for nested functions is wrong, however; functions are
16272 distinct units, and our context might not even be inline. */
16273 tree fn = origin;
16275 if (TYPE_P (fn))
16276 fn = TYPE_STUB_DECL (fn);
16278 fn = decl_function_context (fn);
16279 if (fn)
16280 dwarf2out_abstract_function (fn);
16283 if (DECL_P (origin))
16284 origin_die = lookup_decl_die (origin);
16285 else if (TYPE_P (origin))
16286 origin_die = lookup_type_die (origin);
16288 /* XXX: Functions that are never lowered don't always have correct block
16289 trees (in the case of java, they simply have no block tree, in some other
16290 languages). For these functions, there is nothing we can really do to
16291 output correct debug info for inlined functions in all cases. Rather
16292 than die, we'll just produce deficient debug info now, in that we will
16293 have variables without a proper abstract origin. In the future, when all
16294 functions are lowered, we should re-add a gcc_assert (origin_die)
16295 here. */
16297 if (origin_die)
16298 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16299 return origin_die;
16302 /* We do not currently support the pure_virtual attribute. */
16304 static inline void
16305 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16307 if (DECL_VINDEX (func_decl))
16309 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16311 if (host_integerp (DECL_VINDEX (func_decl), 0))
16312 add_AT_loc (die, DW_AT_vtable_elem_location,
16313 new_loc_descr (DW_OP_constu,
16314 tree_low_cst (DECL_VINDEX (func_decl), 0),
16315 0));
16317 /* GNU extension: Record what type this method came from originally. */
16318 if (debug_info_level > DINFO_LEVEL_TERSE
16319 && DECL_CONTEXT (func_decl))
16320 add_AT_die_ref (die, DW_AT_containing_type,
16321 lookup_type_die (DECL_CONTEXT (func_decl)));
16325 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16326 given decl. This used to be a vendor extension until after DWARF 4
16327 standardized it. */
16329 static void
16330 add_linkage_attr (dw_die_ref die, tree decl)
16332 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16334 /* Mimic what assemble_name_raw does with a leading '*'. */
16335 if (name[0] == '*')
16336 name = &name[1];
16338 if (dwarf_version >= 4)
16339 add_AT_string (die, DW_AT_linkage_name, name);
16340 else
16341 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16344 /* Add source coordinate attributes for the given decl. */
16346 static void
16347 add_src_coords_attributes (dw_die_ref die, tree decl)
16349 expanded_location s;
16351 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16352 return;
16353 s = expand_location (DECL_SOURCE_LOCATION (decl));
16354 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16355 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16358 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16360 static void
16361 add_linkage_name (dw_die_ref die, tree decl)
16363 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16364 && TREE_PUBLIC (decl)
16365 && !DECL_ABSTRACT (decl)
16366 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16367 && die->die_tag != DW_TAG_member)
16369 /* Defer until we have an assembler name set. */
16370 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16372 limbo_die_node *asm_name;
16374 asm_name = ggc_alloc_cleared_limbo_die_node ();
16375 asm_name->die = die;
16376 asm_name->created_for = decl;
16377 asm_name->next = deferred_asm_name;
16378 deferred_asm_name = asm_name;
16380 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16381 add_linkage_attr (die, decl);
16385 /* Add a DW_AT_name attribute and source coordinate attribute for the
16386 given decl, but only if it actually has a name. */
16388 static void
16389 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16391 tree decl_name;
16393 decl_name = DECL_NAME (decl);
16394 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16396 const char *name = dwarf2_name (decl, 0);
16397 if (name)
16398 add_name_attribute (die, name);
16399 if (! DECL_ARTIFICIAL (decl))
16400 add_src_coords_attributes (die, decl);
16402 add_linkage_name (die, decl);
16405 #ifdef VMS_DEBUGGING_INFO
16406 /* Get the function's name, as described by its RTL. This may be different
16407 from the DECL_NAME name used in the source file. */
16408 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16410 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16411 XEXP (DECL_RTL (decl), 0), false);
16412 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16414 #endif /* VMS_DEBUGGING_INFO */
16417 #ifdef VMS_DEBUGGING_INFO
16418 /* Output the debug main pointer die for VMS */
16420 void
16421 dwarf2out_vms_debug_main_pointer (void)
16423 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16424 dw_die_ref die;
16426 /* Allocate the VMS debug main subprogram die. */
16427 die = ggc_alloc_cleared_die_node ();
16428 die->die_tag = DW_TAG_subprogram;
16429 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16430 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16431 current_function_funcdef_no);
16432 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16434 /* Make it the first child of comp_unit_die (). */
16435 die->die_parent = comp_unit_die ();
16436 if (comp_unit_die ()->die_child)
16438 die->die_sib = comp_unit_die ()->die_child->die_sib;
16439 comp_unit_die ()->die_child->die_sib = die;
16441 else
16443 die->die_sib = die;
16444 comp_unit_die ()->die_child = die;
16447 #endif /* VMS_DEBUGGING_INFO */
16449 /* Push a new declaration scope. */
16451 static void
16452 push_decl_scope (tree scope)
16454 vec_safe_push (decl_scope_table, scope);
16457 /* Pop a declaration scope. */
16459 static inline void
16460 pop_decl_scope (void)
16462 decl_scope_table->pop ();
16465 /* walk_tree helper function for uses_local_type, below. */
16467 static tree
16468 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16470 if (!TYPE_P (*tp))
16471 *walk_subtrees = 0;
16472 else
16474 tree name = TYPE_NAME (*tp);
16475 if (name && DECL_P (name) && decl_function_context (name))
16476 return *tp;
16478 return NULL_TREE;
16481 /* If TYPE involves a function-local type (including a local typedef to a
16482 non-local type), returns that type; otherwise returns NULL_TREE. */
16484 static tree
16485 uses_local_type (tree type)
16487 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16488 return used;
16491 /* Return the DIE for the scope that immediately contains this type.
16492 Non-named types that do not involve a function-local type get global
16493 scope. Named types nested in namespaces or other types get their
16494 containing scope. All other types (i.e. function-local named types) get
16495 the current active scope. */
16497 static dw_die_ref
16498 scope_die_for (tree t, dw_die_ref context_die)
16500 dw_die_ref scope_die = NULL;
16501 tree containing_scope;
16503 /* Non-types always go in the current scope. */
16504 gcc_assert (TYPE_P (t));
16506 /* Use the scope of the typedef, rather than the scope of the type
16507 it refers to. */
16508 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16509 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16510 else
16511 containing_scope = TYPE_CONTEXT (t);
16513 /* Use the containing namespace if there is one. */
16514 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16516 if (context_die == lookup_decl_die (containing_scope))
16517 /* OK */;
16518 else if (debug_info_level > DINFO_LEVEL_TERSE)
16519 context_die = get_context_die (containing_scope);
16520 else
16521 containing_scope = NULL_TREE;
16524 /* Ignore function type "scopes" from the C frontend. They mean that
16525 a tagged type is local to a parmlist of a function declarator, but
16526 that isn't useful to DWARF. */
16527 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16528 containing_scope = NULL_TREE;
16530 if (SCOPE_FILE_SCOPE_P (containing_scope))
16532 /* If T uses a local type keep it local as well, to avoid references
16533 to function-local DIEs from outside the function. */
16534 if (current_function_decl && uses_local_type (t))
16535 scope_die = context_die;
16536 else
16537 scope_die = comp_unit_die ();
16539 else if (TYPE_P (containing_scope))
16541 /* For types, we can just look up the appropriate DIE. */
16542 if (debug_info_level > DINFO_LEVEL_TERSE)
16543 scope_die = get_context_die (containing_scope);
16544 else
16546 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16547 if (scope_die == NULL)
16548 scope_die = comp_unit_die ();
16551 else
16552 scope_die = context_die;
16554 return scope_die;
16557 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16559 static inline int
16560 local_scope_p (dw_die_ref context_die)
16562 for (; context_die; context_die = context_die->die_parent)
16563 if (context_die->die_tag == DW_TAG_inlined_subroutine
16564 || context_die->die_tag == DW_TAG_subprogram)
16565 return 1;
16567 return 0;
16570 /* Returns nonzero if CONTEXT_DIE is a class. */
16572 static inline int
16573 class_scope_p (dw_die_ref context_die)
16575 return (context_die
16576 && (context_die->die_tag == DW_TAG_structure_type
16577 || context_die->die_tag == DW_TAG_class_type
16578 || context_die->die_tag == DW_TAG_interface_type
16579 || context_die->die_tag == DW_TAG_union_type));
16582 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16583 whether or not to treat a DIE in this context as a declaration. */
16585 static inline int
16586 class_or_namespace_scope_p (dw_die_ref context_die)
16588 return (class_scope_p (context_die)
16589 || (context_die && context_die->die_tag == DW_TAG_namespace));
16592 /* Many forms of DIEs require a "type description" attribute. This
16593 routine locates the proper "type descriptor" die for the type given
16594 by 'type', and adds a DW_AT_type attribute below the given die. */
16596 static void
16597 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16598 int decl_volatile, dw_die_ref context_die)
16600 enum tree_code code = TREE_CODE (type);
16601 dw_die_ref type_die = NULL;
16603 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16604 or fixed-point type, use the inner type. This is because we have no
16605 support for unnamed types in base_type_die. This can happen if this is
16606 an Ada subrange type. Correct solution is emit a subrange type die. */
16607 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16608 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16609 type = TREE_TYPE (type), code = TREE_CODE (type);
16611 if (code == ERROR_MARK
16612 /* Handle a special case. For functions whose return type is void, we
16613 generate *no* type attribute. (Note that no object may have type
16614 `void', so this only applies to function return types). */
16615 || code == VOID_TYPE)
16616 return;
16618 type_die = modified_type_die (type,
16619 decl_const || TYPE_READONLY (type),
16620 decl_volatile || TYPE_VOLATILE (type),
16621 context_die);
16623 if (type_die != NULL)
16624 add_AT_die_ref (object_die, DW_AT_type, type_die);
16627 /* Given an object die, add the calling convention attribute for the
16628 function call type. */
16629 static void
16630 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16632 enum dwarf_calling_convention value = DW_CC_normal;
16634 value = ((enum dwarf_calling_convention)
16635 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16637 if (is_fortran ()
16638 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16640 /* DWARF 2 doesn't provide a way to identify a program's source-level
16641 entry point. DW_AT_calling_convention attributes are only meant
16642 to describe functions' calling conventions. However, lacking a
16643 better way to signal the Fortran main program, we used this for
16644 a long time, following existing custom. Now, DWARF 4 has
16645 DW_AT_main_subprogram, which we add below, but some tools still
16646 rely on the old way, which we thus keep. */
16647 value = DW_CC_program;
16649 if (dwarf_version >= 4 || !dwarf_strict)
16650 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16653 /* Only add the attribute if the backend requests it, and
16654 is not DW_CC_normal. */
16655 if (value && (value != DW_CC_normal))
16656 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16659 /* Given a tree pointer to a struct, class, union, or enum type node, return
16660 a pointer to the (string) tag name for the given type, or zero if the type
16661 was declared without a tag. */
16663 static const char *
16664 type_tag (const_tree type)
16666 const char *name = 0;
16668 if (TYPE_NAME (type) != 0)
16670 tree t = 0;
16672 /* Find the IDENTIFIER_NODE for the type name. */
16673 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16674 && !TYPE_NAMELESS (type))
16675 t = TYPE_NAME (type);
16677 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16678 a TYPE_DECL node, regardless of whether or not a `typedef' was
16679 involved. */
16680 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16681 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16683 /* We want to be extra verbose. Don't call dwarf_name if
16684 DECL_NAME isn't set. The default hook for decl_printable_name
16685 doesn't like that, and in this context it's correct to return
16686 0, instead of "<anonymous>" or the like. */
16687 if (DECL_NAME (TYPE_NAME (type))
16688 && !DECL_NAMELESS (TYPE_NAME (type)))
16689 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16692 /* Now get the name as a string, or invent one. */
16693 if (!name && t != 0)
16694 name = IDENTIFIER_POINTER (t);
16697 return (name == 0 || *name == '\0') ? 0 : name;
16700 /* Return the type associated with a data member, make a special check
16701 for bit field types. */
16703 static inline tree
16704 member_declared_type (const_tree member)
16706 return (DECL_BIT_FIELD_TYPE (member)
16707 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16710 /* Get the decl's label, as described by its RTL. This may be different
16711 from the DECL_NAME name used in the source file. */
16713 #if 0
16714 static const char *
16715 decl_start_label (tree decl)
16717 rtx x;
16718 const char *fnname;
16720 x = DECL_RTL (decl);
16721 gcc_assert (MEM_P (x));
16723 x = XEXP (x, 0);
16724 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16726 fnname = XSTR (x, 0);
16727 return fnname;
16729 #endif
16731 /* These routines generate the internal representation of the DIE's for
16732 the compilation unit. Debugging information is collected by walking
16733 the declaration trees passed in from dwarf2out_decl(). */
16735 static void
16736 gen_array_type_die (tree type, dw_die_ref context_die)
16738 dw_die_ref scope_die = scope_die_for (type, context_die);
16739 dw_die_ref array_die;
16741 /* GNU compilers represent multidimensional array types as sequences of one
16742 dimensional array types whose element types are themselves array types.
16743 We sometimes squish that down to a single array_type DIE with multiple
16744 subscripts in the Dwarf debugging info. The draft Dwarf specification
16745 say that we are allowed to do this kind of compression in C, because
16746 there is no difference between an array of arrays and a multidimensional
16747 array. We don't do this for Ada to remain as close as possible to the
16748 actual representation, which is especially important against the language
16749 flexibilty wrt arrays of variable size. */
16751 bool collapse_nested_arrays = !is_ada ();
16752 tree element_type;
16754 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16755 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16756 if (TYPE_STRING_FLAG (type)
16757 && TREE_CODE (type) == ARRAY_TYPE
16758 && is_fortran ()
16759 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16761 HOST_WIDE_INT size;
16763 array_die = new_die (DW_TAG_string_type, scope_die, type);
16764 add_name_attribute (array_die, type_tag (type));
16765 equate_type_number_to_die (type, array_die);
16766 size = int_size_in_bytes (type);
16767 if (size >= 0)
16768 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16769 else if (TYPE_DOMAIN (type) != NULL_TREE
16770 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16771 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16773 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16774 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16776 size = int_size_in_bytes (TREE_TYPE (szdecl));
16777 if (loc && size > 0)
16779 add_AT_location_description (array_die, DW_AT_string_length, loc);
16780 if (size != DWARF2_ADDR_SIZE)
16781 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16784 return;
16787 array_die = new_die (DW_TAG_array_type, scope_die, type);
16788 add_name_attribute (array_die, type_tag (type));
16789 equate_type_number_to_die (type, array_die);
16791 if (TREE_CODE (type) == VECTOR_TYPE)
16792 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16794 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16795 if (is_fortran ()
16796 && TREE_CODE (type) == ARRAY_TYPE
16797 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16798 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16799 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16801 #if 0
16802 /* We default the array ordering. SDB will probably do
16803 the right things even if DW_AT_ordering is not present. It's not even
16804 an issue until we start to get into multidimensional arrays anyway. If
16805 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16806 then we'll have to put the DW_AT_ordering attribute back in. (But if
16807 and when we find out that we need to put these in, we will only do so
16808 for multidimensional arrays. */
16809 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16810 #endif
16812 if (TREE_CODE (type) == VECTOR_TYPE)
16814 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16815 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16816 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16817 add_bound_info (subrange_die, DW_AT_upper_bound,
16818 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16820 else
16821 add_subscript_info (array_die, type, collapse_nested_arrays);
16823 /* Add representation of the type of the elements of this array type and
16824 emit the corresponding DIE if we haven't done it already. */
16825 element_type = TREE_TYPE (type);
16826 if (collapse_nested_arrays)
16827 while (TREE_CODE (element_type) == ARRAY_TYPE)
16829 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16830 break;
16831 element_type = TREE_TYPE (element_type);
16834 add_type_attribute (array_die, element_type, 0, 0, context_die);
16836 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16837 if (TYPE_ARTIFICIAL (type))
16838 add_AT_flag (array_die, DW_AT_artificial, 1);
16840 if (get_AT (array_die, DW_AT_name))
16841 add_pubtype (type, array_die);
16844 static dw_loc_descr_ref
16845 descr_info_loc (tree val, tree base_decl)
16847 HOST_WIDE_INT size;
16848 dw_loc_descr_ref loc, loc2;
16849 enum dwarf_location_atom op;
16851 if (val == base_decl)
16852 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16854 switch (TREE_CODE (val))
16856 CASE_CONVERT:
16857 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16858 case VAR_DECL:
16859 return loc_descriptor_from_tree (val, 0);
16860 case INTEGER_CST:
16861 if (host_integerp (val, 0))
16862 return int_loc_descriptor (tree_low_cst (val, 0));
16863 break;
16864 case INDIRECT_REF:
16865 size = int_size_in_bytes (TREE_TYPE (val));
16866 if (size < 0)
16867 break;
16868 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16869 if (!loc)
16870 break;
16871 if (size == DWARF2_ADDR_SIZE)
16872 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16873 else
16874 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16875 return loc;
16876 case POINTER_PLUS_EXPR:
16877 case PLUS_EXPR:
16878 if (host_integerp (TREE_OPERAND (val, 1), 1)
16879 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16880 < 16384)
16882 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16883 if (!loc)
16884 break;
16885 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16887 else
16889 op = DW_OP_plus;
16890 do_binop:
16891 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16892 if (!loc)
16893 break;
16894 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16895 if (!loc2)
16896 break;
16897 add_loc_descr (&loc, loc2);
16898 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16900 return loc;
16901 case MINUS_EXPR:
16902 op = DW_OP_minus;
16903 goto do_binop;
16904 case MULT_EXPR:
16905 op = DW_OP_mul;
16906 goto do_binop;
16907 case EQ_EXPR:
16908 op = DW_OP_eq;
16909 goto do_binop;
16910 case NE_EXPR:
16911 op = DW_OP_ne;
16912 goto do_binop;
16913 default:
16914 break;
16916 return NULL;
16919 static void
16920 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16921 tree val, tree base_decl)
16923 dw_loc_descr_ref loc;
16925 if (host_integerp (val, 0))
16927 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16928 return;
16931 loc = descr_info_loc (val, base_decl);
16932 if (!loc)
16933 return;
16935 add_AT_loc (die, attr, loc);
16938 /* This routine generates DIE for array with hidden descriptor, details
16939 are filled into *info by a langhook. */
16941 static void
16942 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16943 dw_die_ref context_die)
16945 dw_die_ref scope_die = scope_die_for (type, context_die);
16946 dw_die_ref array_die;
16947 int dim;
16949 array_die = new_die (DW_TAG_array_type, scope_die, type);
16950 add_name_attribute (array_die, type_tag (type));
16951 equate_type_number_to_die (type, array_die);
16953 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16954 if (is_fortran ()
16955 && info->ndimensions >= 2)
16956 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16958 if (info->data_location)
16959 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16960 info->base_decl);
16961 if (info->associated)
16962 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16963 info->base_decl);
16964 if (info->allocated)
16965 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16966 info->base_decl);
16968 for (dim = 0; dim < info->ndimensions; dim++)
16970 dw_die_ref subrange_die
16971 = new_die (DW_TAG_subrange_type, array_die, NULL);
16973 if (info->dimen[dim].lower_bound)
16975 /* If it is the default value, omit it. */
16976 int dflt;
16978 if (host_integerp (info->dimen[dim].lower_bound, 0)
16979 && (dflt = lower_bound_default ()) != -1
16980 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16982 else
16983 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16984 info->dimen[dim].lower_bound,
16985 info->base_decl);
16987 if (info->dimen[dim].upper_bound)
16988 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16989 info->dimen[dim].upper_bound,
16990 info->base_decl);
16991 if (info->dimen[dim].stride)
16992 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16993 info->dimen[dim].stride,
16994 info->base_decl);
16997 gen_type_die (info->element_type, context_die);
16998 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17000 if (get_AT (array_die, DW_AT_name))
17001 add_pubtype (type, array_die);
17004 #if 0
17005 static void
17006 gen_entry_point_die (tree decl, dw_die_ref context_die)
17008 tree origin = decl_ultimate_origin (decl);
17009 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17011 if (origin != NULL)
17012 add_abstract_origin_attribute (decl_die, origin);
17013 else
17015 add_name_and_src_coords_attributes (decl_die, decl);
17016 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17017 0, 0, context_die);
17020 if (DECL_ABSTRACT (decl))
17021 equate_decl_number_to_die (decl, decl_die);
17022 else
17023 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17025 #endif
17027 /* Walk through the list of incomplete types again, trying once more to
17028 emit full debugging info for them. */
17030 static void
17031 retry_incomplete_types (void)
17033 int i;
17035 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17036 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17037 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17040 /* Determine what tag to use for a record type. */
17042 static enum dwarf_tag
17043 record_type_tag (tree type)
17045 if (! lang_hooks.types.classify_record)
17046 return DW_TAG_structure_type;
17048 switch (lang_hooks.types.classify_record (type))
17050 case RECORD_IS_STRUCT:
17051 return DW_TAG_structure_type;
17053 case RECORD_IS_CLASS:
17054 return DW_TAG_class_type;
17056 case RECORD_IS_INTERFACE:
17057 if (dwarf_version >= 3 || !dwarf_strict)
17058 return DW_TAG_interface_type;
17059 return DW_TAG_structure_type;
17061 default:
17062 gcc_unreachable ();
17066 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17067 include all of the information about the enumeration values also. Each
17068 enumerated type name/value is listed as a child of the enumerated type
17069 DIE. */
17071 static dw_die_ref
17072 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17074 dw_die_ref type_die = lookup_type_die (type);
17076 if (type_die == NULL)
17078 type_die = new_die (DW_TAG_enumeration_type,
17079 scope_die_for (type, context_die), type);
17080 equate_type_number_to_die (type, type_die);
17081 add_name_attribute (type_die, type_tag (type));
17082 if (dwarf_version >= 4 || !dwarf_strict)
17084 if (ENUM_IS_SCOPED (type))
17085 add_AT_flag (type_die, DW_AT_enum_class, 1);
17086 if (ENUM_IS_OPAQUE (type))
17087 add_AT_flag (type_die, DW_AT_declaration, 1);
17090 else if (! TYPE_SIZE (type))
17091 return type_die;
17092 else
17093 remove_AT (type_die, DW_AT_declaration);
17095 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17096 given enum type is incomplete, do not generate the DW_AT_byte_size
17097 attribute or the DW_AT_element_list attribute. */
17098 if (TYPE_SIZE (type))
17100 tree link;
17102 TREE_ASM_WRITTEN (type) = 1;
17103 add_byte_size_attribute (type_die, type);
17104 if (TYPE_STUB_DECL (type) != NULL_TREE)
17106 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17107 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17110 /* If the first reference to this type was as the return type of an
17111 inline function, then it may not have a parent. Fix this now. */
17112 if (type_die->die_parent == NULL)
17113 add_child_die (scope_die_for (type, context_die), type_die);
17115 for (link = TYPE_VALUES (type);
17116 link != NULL; link = TREE_CHAIN (link))
17118 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17119 tree value = TREE_VALUE (link);
17121 add_name_attribute (enum_die,
17122 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17124 if (TREE_CODE (value) == CONST_DECL)
17125 value = DECL_INITIAL (value);
17127 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value)))
17128 && (simple_type_size_in_bits (TREE_TYPE (value))
17129 <= HOST_BITS_PER_WIDE_INT || host_integerp (value, 0)))
17130 /* DWARF2 does not provide a way of indicating whether or
17131 not enumeration constants are signed or unsigned. GDB
17132 always assumes the values are signed, so we output all
17133 values as if they were signed. That means that
17134 enumeration constants with very large unsigned values
17135 will appear to have negative values in the debugger.
17137 TODO: the above comment is wrong, DWARF2 does provide
17138 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17139 This should be re-worked to use correct signed/unsigned
17140 int/double tags for all cases, instead of always treating as
17141 signed. */
17142 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17143 else
17144 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17145 that here. */
17146 add_AT_double (enum_die, DW_AT_const_value,
17147 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17150 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17151 if (TYPE_ARTIFICIAL (type))
17152 add_AT_flag (type_die, DW_AT_artificial, 1);
17154 else
17155 add_AT_flag (type_die, DW_AT_declaration, 1);
17157 add_pubtype (type, type_die);
17159 return type_die;
17162 /* Generate a DIE to represent either a real live formal parameter decl or to
17163 represent just the type of some formal parameter position in some function
17164 type.
17166 Note that this routine is a bit unusual because its argument may be a
17167 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17168 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17169 node. If it's the former then this function is being called to output a
17170 DIE to represent a formal parameter object (or some inlining thereof). If
17171 it's the latter, then this function is only being called to output a
17172 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17173 argument type of some subprogram type.
17174 If EMIT_NAME_P is true, name and source coordinate attributes
17175 are emitted. */
17177 static dw_die_ref
17178 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17179 dw_die_ref context_die)
17181 tree node_or_origin = node ? node : origin;
17182 tree ultimate_origin;
17183 dw_die_ref parm_die
17184 = new_die (DW_TAG_formal_parameter, context_die, node);
17186 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17188 case tcc_declaration:
17189 ultimate_origin = decl_ultimate_origin (node_or_origin);
17190 if (node || ultimate_origin)
17191 origin = ultimate_origin;
17192 if (origin != NULL)
17193 add_abstract_origin_attribute (parm_die, origin);
17194 else if (emit_name_p)
17195 add_name_and_src_coords_attributes (parm_die, node);
17196 if (origin == NULL
17197 || (! DECL_ABSTRACT (node_or_origin)
17198 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17199 decl_function_context
17200 (node_or_origin))))
17202 tree type = TREE_TYPE (node_or_origin);
17203 if (decl_by_reference_p (node_or_origin))
17204 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17205 context_die);
17206 else
17207 add_type_attribute (parm_die, type,
17208 TREE_READONLY (node_or_origin),
17209 TREE_THIS_VOLATILE (node_or_origin),
17210 context_die);
17212 if (origin == NULL && DECL_ARTIFICIAL (node))
17213 add_AT_flag (parm_die, DW_AT_artificial, 1);
17215 if (node && node != origin)
17216 equate_decl_number_to_die (node, parm_die);
17217 if (! DECL_ABSTRACT (node_or_origin))
17218 add_location_or_const_value_attribute (parm_die, node_or_origin,
17219 node == NULL, DW_AT_location);
17221 break;
17223 case tcc_type:
17224 /* We were called with some kind of a ..._TYPE node. */
17225 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17226 break;
17228 default:
17229 gcc_unreachable ();
17232 return parm_die;
17235 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17236 children DW_TAG_formal_parameter DIEs representing the arguments of the
17237 parameter pack.
17239 PARM_PACK must be a function parameter pack.
17240 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17241 must point to the subsequent arguments of the function PACK_ARG belongs to.
17242 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17243 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17244 following the last one for which a DIE was generated. */
17246 static dw_die_ref
17247 gen_formal_parameter_pack_die (tree parm_pack,
17248 tree pack_arg,
17249 dw_die_ref subr_die,
17250 tree *next_arg)
17252 tree arg;
17253 dw_die_ref parm_pack_die;
17255 gcc_assert (parm_pack
17256 && lang_hooks.function_parameter_pack_p (parm_pack)
17257 && subr_die);
17259 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17260 add_src_coords_attributes (parm_pack_die, parm_pack);
17262 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17264 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17265 parm_pack))
17266 break;
17267 gen_formal_parameter_die (arg, NULL,
17268 false /* Don't emit name attribute. */,
17269 parm_pack_die);
17271 if (next_arg)
17272 *next_arg = arg;
17273 return parm_pack_die;
17276 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17277 at the end of an (ANSI prototyped) formal parameters list. */
17279 static void
17280 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17282 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17285 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17286 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17287 parameters as specified in some function type specification (except for
17288 those which appear as part of a function *definition*). */
17290 static void
17291 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17293 tree link;
17294 tree formal_type = NULL;
17295 tree first_parm_type;
17296 tree arg;
17298 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17300 arg = DECL_ARGUMENTS (function_or_method_type);
17301 function_or_method_type = TREE_TYPE (function_or_method_type);
17303 else
17304 arg = NULL_TREE;
17306 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17308 /* Make our first pass over the list of formal parameter types and output a
17309 DW_TAG_formal_parameter DIE for each one. */
17310 for (link = first_parm_type; link; )
17312 dw_die_ref parm_die;
17314 formal_type = TREE_VALUE (link);
17315 if (formal_type == void_type_node)
17316 break;
17318 /* Output a (nameless) DIE to represent the formal parameter itself. */
17319 parm_die = gen_formal_parameter_die (formal_type, NULL,
17320 true /* Emit name attribute. */,
17321 context_die);
17322 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17323 && link == first_parm_type)
17325 add_AT_flag (parm_die, DW_AT_artificial, 1);
17326 if (dwarf_version >= 3 || !dwarf_strict)
17327 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17329 else if (arg && DECL_ARTIFICIAL (arg))
17330 add_AT_flag (parm_die, DW_AT_artificial, 1);
17332 link = TREE_CHAIN (link);
17333 if (arg)
17334 arg = DECL_CHAIN (arg);
17337 /* If this function type has an ellipsis, add a
17338 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17339 if (formal_type != void_type_node)
17340 gen_unspecified_parameters_die (function_or_method_type, context_die);
17342 /* Make our second (and final) pass over the list of formal parameter types
17343 and output DIEs to represent those types (as necessary). */
17344 for (link = TYPE_ARG_TYPES (function_or_method_type);
17345 link && TREE_VALUE (link);
17346 link = TREE_CHAIN (link))
17347 gen_type_die (TREE_VALUE (link), context_die);
17350 /* We want to generate the DIE for TYPE so that we can generate the
17351 die for MEMBER, which has been defined; we will need to refer back
17352 to the member declaration nested within TYPE. If we're trying to
17353 generate minimal debug info for TYPE, processing TYPE won't do the
17354 trick; we need to attach the member declaration by hand. */
17356 static void
17357 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17359 gen_type_die (type, context_die);
17361 /* If we're trying to avoid duplicate debug info, we may not have
17362 emitted the member decl for this function. Emit it now. */
17363 if (TYPE_STUB_DECL (type)
17364 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17365 && ! lookup_decl_die (member))
17367 dw_die_ref type_die;
17368 gcc_assert (!decl_ultimate_origin (member));
17370 push_decl_scope (type);
17371 type_die = lookup_type_die_strip_naming_typedef (type);
17372 if (TREE_CODE (member) == FUNCTION_DECL)
17373 gen_subprogram_die (member, type_die);
17374 else if (TREE_CODE (member) == FIELD_DECL)
17376 /* Ignore the nameless fields that are used to skip bits but handle
17377 C++ anonymous unions and structs. */
17378 if (DECL_NAME (member) != NULL_TREE
17379 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17380 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17382 gen_type_die (member_declared_type (member), type_die);
17383 gen_field_die (member, type_die);
17386 else
17387 gen_variable_die (member, NULL_TREE, type_die);
17389 pop_decl_scope ();
17393 /* Forward declare these functions, because they are mutually recursive
17394 with their set_block_* pairing functions. */
17395 static void set_decl_origin_self (tree);
17396 static void set_decl_abstract_flags (tree, int);
17398 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17399 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17400 that it points to the node itself, thus indicating that the node is its
17401 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17402 the given node is NULL, recursively descend the decl/block tree which
17403 it is the root of, and for each other ..._DECL or BLOCK node contained
17404 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17405 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17406 values to point to themselves. */
17408 static void
17409 set_block_origin_self (tree stmt)
17411 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17413 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17416 tree local_decl;
17418 for (local_decl = BLOCK_VARS (stmt);
17419 local_decl != NULL_TREE;
17420 local_decl = DECL_CHAIN (local_decl))
17421 if (! DECL_EXTERNAL (local_decl))
17422 set_decl_origin_self (local_decl); /* Potential recursion. */
17426 tree subblock;
17428 for (subblock = BLOCK_SUBBLOCKS (stmt);
17429 subblock != NULL_TREE;
17430 subblock = BLOCK_CHAIN (subblock))
17431 set_block_origin_self (subblock); /* Recurse. */
17436 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17437 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17438 node to so that it points to the node itself, thus indicating that the
17439 node represents its own (abstract) origin. Additionally, if the
17440 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17441 the decl/block tree of which the given node is the root of, and for
17442 each other ..._DECL or BLOCK node contained therein whose
17443 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17444 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17445 point to themselves. */
17447 static void
17448 set_decl_origin_self (tree decl)
17450 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17452 DECL_ABSTRACT_ORIGIN (decl) = decl;
17453 if (TREE_CODE (decl) == FUNCTION_DECL)
17455 tree arg;
17457 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17458 DECL_ABSTRACT_ORIGIN (arg) = arg;
17459 if (DECL_INITIAL (decl) != NULL_TREE
17460 && DECL_INITIAL (decl) != error_mark_node)
17461 set_block_origin_self (DECL_INITIAL (decl));
17466 /* Given a pointer to some BLOCK node, and a boolean value to set the
17467 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17468 the given block, and for all local decls and all local sub-blocks
17469 (recursively) which are contained therein. */
17471 static void
17472 set_block_abstract_flags (tree stmt, int setting)
17474 tree local_decl;
17475 tree subblock;
17476 unsigned int i;
17478 BLOCK_ABSTRACT (stmt) = setting;
17480 for (local_decl = BLOCK_VARS (stmt);
17481 local_decl != NULL_TREE;
17482 local_decl = DECL_CHAIN (local_decl))
17483 if (! DECL_EXTERNAL (local_decl))
17484 set_decl_abstract_flags (local_decl, setting);
17486 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17488 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17489 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17490 || TREE_CODE (local_decl) == PARM_DECL)
17491 set_decl_abstract_flags (local_decl, setting);
17494 for (subblock = BLOCK_SUBBLOCKS (stmt);
17495 subblock != NULL_TREE;
17496 subblock = BLOCK_CHAIN (subblock))
17497 set_block_abstract_flags (subblock, setting);
17500 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17501 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17502 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17503 set the abstract flags for all of the parameters, local vars, local
17504 blocks and sub-blocks (recursively) to the same setting. */
17506 static void
17507 set_decl_abstract_flags (tree decl, int setting)
17509 DECL_ABSTRACT (decl) = setting;
17510 if (TREE_CODE (decl) == FUNCTION_DECL)
17512 tree arg;
17514 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17515 DECL_ABSTRACT (arg) = setting;
17516 if (DECL_INITIAL (decl) != NULL_TREE
17517 && DECL_INITIAL (decl) != error_mark_node)
17518 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17522 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17523 may later generate inlined and/or out-of-line instances of. */
17525 static void
17526 dwarf2out_abstract_function (tree decl)
17528 dw_die_ref old_die;
17529 tree save_fn;
17530 tree context;
17531 int was_abstract;
17532 htab_t old_decl_loc_table;
17533 htab_t old_cached_dw_loc_list_table;
17534 int old_call_site_count, old_tail_call_site_count;
17535 struct call_arg_loc_node *old_call_arg_locations;
17537 /* Make sure we have the actual abstract inline, not a clone. */
17538 decl = DECL_ORIGIN (decl);
17540 old_die = lookup_decl_die (decl);
17541 if (old_die && get_AT (old_die, DW_AT_inline))
17542 /* We've already generated the abstract instance. */
17543 return;
17545 /* We can be called while recursively when seeing block defining inlined subroutine
17546 DIE. Be sure to not clobber the outer location table nor use it or we would
17547 get locations in abstract instantces. */
17548 old_decl_loc_table = decl_loc_table;
17549 decl_loc_table = NULL;
17550 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17551 cached_dw_loc_list_table = NULL;
17552 old_call_arg_locations = call_arg_locations;
17553 call_arg_locations = NULL;
17554 old_call_site_count = call_site_count;
17555 call_site_count = -1;
17556 old_tail_call_site_count = tail_call_site_count;
17557 tail_call_site_count = -1;
17559 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17560 we don't get confused by DECL_ABSTRACT. */
17561 if (debug_info_level > DINFO_LEVEL_TERSE)
17563 context = decl_class_context (decl);
17564 if (context)
17565 gen_type_die_for_member
17566 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17569 /* Pretend we've just finished compiling this function. */
17570 save_fn = current_function_decl;
17571 current_function_decl = decl;
17573 was_abstract = DECL_ABSTRACT (decl);
17574 set_decl_abstract_flags (decl, 1);
17575 dwarf2out_decl (decl);
17576 if (! was_abstract)
17577 set_decl_abstract_flags (decl, 0);
17579 current_function_decl = save_fn;
17580 decl_loc_table = old_decl_loc_table;
17581 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17582 call_arg_locations = old_call_arg_locations;
17583 call_site_count = old_call_site_count;
17584 tail_call_site_count = old_tail_call_site_count;
17587 /* Helper function of premark_used_types() which gets called through
17588 htab_traverse.
17590 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17591 marked as unused by prune_unused_types. */
17593 static int
17594 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17596 tree type;
17597 dw_die_ref die;
17599 type = (tree) *slot;
17600 die = lookup_type_die (type);
17601 if (die != NULL)
17602 die->die_perennial_p = 1;
17603 return 1;
17606 /* Helper function of premark_types_used_by_global_vars which gets called
17607 through htab_traverse.
17609 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17610 marked as unused by prune_unused_types. The DIE of the type is marked
17611 only if the global variable using the type will actually be emitted. */
17613 static int
17614 premark_types_used_by_global_vars_helper (void **slot,
17615 void *data ATTRIBUTE_UNUSED)
17617 struct types_used_by_vars_entry *entry;
17618 dw_die_ref die;
17620 entry = (struct types_used_by_vars_entry *) *slot;
17621 gcc_assert (entry->type != NULL
17622 && entry->var_decl != NULL);
17623 die = lookup_type_die (entry->type);
17624 if (die)
17626 /* Ask cgraph if the global variable really is to be emitted.
17627 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17628 struct varpool_node *node = varpool_get_node (entry->var_decl);
17629 if (node && node->symbol.definition)
17631 die->die_perennial_p = 1;
17632 /* Keep the parent DIEs as well. */
17633 while ((die = die->die_parent) && die->die_perennial_p == 0)
17634 die->die_perennial_p = 1;
17637 return 1;
17640 /* Mark all members of used_types_hash as perennial. */
17642 static void
17643 premark_used_types (struct function *fun)
17645 if (fun && fun->used_types_hash)
17646 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17649 /* Mark all members of types_used_by_vars_entry as perennial. */
17651 static void
17652 premark_types_used_by_global_vars (void)
17654 if (types_used_by_vars_hash)
17655 htab_traverse (types_used_by_vars_hash,
17656 premark_types_used_by_global_vars_helper, NULL);
17659 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17660 for CA_LOC call arg loc node. */
17662 static dw_die_ref
17663 gen_call_site_die (tree decl, dw_die_ref subr_die,
17664 struct call_arg_loc_node *ca_loc)
17666 dw_die_ref stmt_die = NULL, die;
17667 tree block = ca_loc->block;
17669 while (block
17670 && block != DECL_INITIAL (decl)
17671 && TREE_CODE (block) == BLOCK)
17673 if (block_map.length () > BLOCK_NUMBER (block))
17674 stmt_die = block_map[BLOCK_NUMBER (block)];
17675 if (stmt_die)
17676 break;
17677 block = BLOCK_SUPERCONTEXT (block);
17679 if (stmt_die == NULL)
17680 stmt_die = subr_die;
17681 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17682 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17683 if (ca_loc->tail_call_p)
17684 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17685 if (ca_loc->symbol_ref)
17687 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17688 if (tdie)
17689 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17690 else
17691 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17693 return die;
17696 /* Generate a DIE to represent a declared function (either file-scope or
17697 block-local). */
17699 static void
17700 gen_subprogram_die (tree decl, dw_die_ref context_die)
17702 tree origin = decl_ultimate_origin (decl);
17703 dw_die_ref subr_die;
17704 tree outer_scope;
17705 dw_die_ref old_die = lookup_decl_die (decl);
17706 int declaration = (current_function_decl != decl
17707 || class_or_namespace_scope_p (context_die));
17709 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17711 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17712 started to generate the abstract instance of an inline, decided to output
17713 its containing class, and proceeded to emit the declaration of the inline
17714 from the member list for the class. If so, DECLARATION takes priority;
17715 we'll get back to the abstract instance when done with the class. */
17717 /* The class-scope declaration DIE must be the primary DIE. */
17718 if (origin && declaration && class_or_namespace_scope_p (context_die))
17720 origin = NULL;
17721 gcc_assert (!old_die);
17724 /* Now that the C++ front end lazily declares artificial member fns, we
17725 might need to retrofit the declaration into its class. */
17726 if (!declaration && !origin && !old_die
17727 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17728 && !class_or_namespace_scope_p (context_die)
17729 && debug_info_level > DINFO_LEVEL_TERSE)
17730 old_die = force_decl_die (decl);
17732 if (origin != NULL)
17734 gcc_assert (!declaration || local_scope_p (context_die));
17736 /* Fixup die_parent for the abstract instance of a nested
17737 inline function. */
17738 if (old_die && old_die->die_parent == NULL)
17739 add_child_die (context_die, old_die);
17741 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17742 add_abstract_origin_attribute (subr_die, origin);
17743 /* This is where the actual code for a cloned function is.
17744 Let's emit linkage name attribute for it. This helps
17745 debuggers to e.g, set breakpoints into
17746 constructors/destructors when the user asks "break
17747 K::K". */
17748 add_linkage_name (subr_die, decl);
17750 else if (old_die)
17752 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17753 struct dwarf_file_data * file_index = lookup_filename (s.file);
17755 if (!get_AT_flag (old_die, DW_AT_declaration)
17756 /* We can have a normal definition following an inline one in the
17757 case of redefinition of GNU C extern inlines.
17758 It seems reasonable to use AT_specification in this case. */
17759 && !get_AT (old_die, DW_AT_inline))
17761 /* Detect and ignore this case, where we are trying to output
17762 something we have already output. */
17763 return;
17766 /* If the definition comes from the same place as the declaration,
17767 maybe use the old DIE. We always want the DIE for this function
17768 that has the *_pc attributes to be under comp_unit_die so the
17769 debugger can find it. We also need to do this for abstract
17770 instances of inlines, since the spec requires the out-of-line copy
17771 to have the same parent. For local class methods, this doesn't
17772 apply; we just use the old DIE. */
17773 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17774 && (DECL_ARTIFICIAL (decl)
17775 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17776 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17777 == (unsigned) s.line))))
17779 subr_die = old_die;
17781 /* Clear out the declaration attribute and the formal parameters.
17782 Do not remove all children, because it is possible that this
17783 declaration die was forced using force_decl_die(). In such
17784 cases die that forced declaration die (e.g. TAG_imported_module)
17785 is one of the children that we do not want to remove. */
17786 remove_AT (subr_die, DW_AT_declaration);
17787 remove_AT (subr_die, DW_AT_object_pointer);
17788 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17790 else
17792 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17793 add_AT_specification (subr_die, old_die);
17794 add_pubname (decl, subr_die);
17795 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17796 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17797 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17798 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17801 else
17803 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17805 if (TREE_PUBLIC (decl))
17806 add_AT_flag (subr_die, DW_AT_external, 1);
17808 add_name_and_src_coords_attributes (subr_die, decl);
17809 add_pubname (decl, subr_die);
17810 if (debug_info_level > DINFO_LEVEL_TERSE)
17812 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17813 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17814 0, 0, context_die);
17817 add_pure_or_virtual_attribute (subr_die, decl);
17818 if (DECL_ARTIFICIAL (decl))
17819 add_AT_flag (subr_die, DW_AT_artificial, 1);
17821 add_accessibility_attribute (subr_die, decl);
17824 if (declaration)
17826 if (!old_die || !get_AT (old_die, DW_AT_inline))
17828 add_AT_flag (subr_die, DW_AT_declaration, 1);
17830 /* If this is an explicit function declaration then generate
17831 a DW_AT_explicit attribute. */
17832 if (lang_hooks.decls.function_decl_explicit_p (decl)
17833 && (dwarf_version >= 3 || !dwarf_strict))
17834 add_AT_flag (subr_die, DW_AT_explicit, 1);
17836 /* The first time we see a member function, it is in the context of
17837 the class to which it belongs. We make sure of this by emitting
17838 the class first. The next time is the definition, which is
17839 handled above. The two may come from the same source text.
17841 Note that force_decl_die() forces function declaration die. It is
17842 later reused to represent definition. */
17843 equate_decl_number_to_die (decl, subr_die);
17846 else if (DECL_ABSTRACT (decl))
17848 if (DECL_DECLARED_INLINE_P (decl))
17850 if (cgraph_function_possibly_inlined_p (decl))
17851 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17852 else
17853 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17855 else
17857 if (cgraph_function_possibly_inlined_p (decl))
17858 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17859 else
17860 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17863 if (DECL_DECLARED_INLINE_P (decl)
17864 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17865 add_AT_flag (subr_die, DW_AT_artificial, 1);
17867 equate_decl_number_to_die (decl, subr_die);
17869 else if (!DECL_EXTERNAL (decl))
17871 HOST_WIDE_INT cfa_fb_offset;
17872 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17874 if (!old_die || !get_AT (old_die, DW_AT_inline))
17875 equate_decl_number_to_die (decl, subr_die);
17877 gcc_checking_assert (fun);
17878 if (!flag_reorder_blocks_and_partition)
17880 dw_fde_ref fde = fun->fde;
17881 if (fde->dw_fde_begin)
17883 /* We have already generated the labels. */
17884 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17885 fde->dw_fde_end, false);
17887 else
17889 /* Create start/end labels and add the range. */
17890 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17891 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17892 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17893 current_function_funcdef_no);
17894 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17895 current_function_funcdef_no);
17896 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17897 false);
17900 #if VMS_DEBUGGING_INFO
17901 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17902 Section 2.3 Prologue and Epilogue Attributes:
17903 When a breakpoint is set on entry to a function, it is generally
17904 desirable for execution to be suspended, not on the very first
17905 instruction of the function, but rather at a point after the
17906 function's frame has been set up, after any language defined local
17907 declaration processing has been completed, and before execution of
17908 the first statement of the function begins. Debuggers generally
17909 cannot properly determine where this point is. Similarly for a
17910 breakpoint set on exit from a function. The prologue and epilogue
17911 attributes allow a compiler to communicate the location(s) to use. */
17914 if (fde->dw_fde_vms_end_prologue)
17915 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17916 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17918 if (fde->dw_fde_vms_begin_epilogue)
17919 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17920 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17922 #endif
17925 else
17927 /* Generate pubnames entries for the split function code ranges. */
17928 dw_fde_ref fde = fun->fde;
17930 if (fde->dw_fde_second_begin)
17932 if (dwarf_version >= 3 || !dwarf_strict)
17934 /* We should use ranges for non-contiguous code section
17935 addresses. Use the actual code range for the initial
17936 section, since the HOT/COLD labels might precede an
17937 alignment offset. */
17938 bool range_list_added = false;
17939 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17940 fde->dw_fde_end, &range_list_added,
17941 false);
17942 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17943 fde->dw_fde_second_end,
17944 &range_list_added, false);
17945 if (range_list_added)
17946 add_ranges (NULL);
17948 else
17950 /* There is no real support in DW2 for this .. so we make
17951 a work-around. First, emit the pub name for the segment
17952 containing the function label. Then make and emit a
17953 simplified subprogram DIE for the second segment with the
17954 name pre-fixed by __hot/cold_sect_of_. We use the same
17955 linkage name for the second die so that gdb will find both
17956 sections when given "b foo". */
17957 const char *name = NULL;
17958 tree decl_name = DECL_NAME (decl);
17959 dw_die_ref seg_die;
17961 /* Do the 'primary' section. */
17962 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17963 fde->dw_fde_end, false);
17965 /* Build a minimal DIE for the secondary section. */
17966 seg_die = new_die (DW_TAG_subprogram,
17967 subr_die->die_parent, decl);
17969 if (TREE_PUBLIC (decl))
17970 add_AT_flag (seg_die, DW_AT_external, 1);
17972 if (decl_name != NULL
17973 && IDENTIFIER_POINTER (decl_name) != NULL)
17975 name = dwarf2_name (decl, 1);
17976 if (! DECL_ARTIFICIAL (decl))
17977 add_src_coords_attributes (seg_die, decl);
17979 add_linkage_name (seg_die, decl);
17981 gcc_assert (name != NULL);
17982 add_pure_or_virtual_attribute (seg_die, decl);
17983 if (DECL_ARTIFICIAL (decl))
17984 add_AT_flag (seg_die, DW_AT_artificial, 1);
17986 name = concat ("__second_sect_of_", name, NULL);
17987 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17988 fde->dw_fde_second_end, false);
17989 add_name_attribute (seg_die, name);
17990 if (want_pubnames ())
17991 add_pubname_string (name, seg_die);
17994 else
17995 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17996 false);
17999 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18001 /* We define the "frame base" as the function's CFA. This is more
18002 convenient for several reasons: (1) It's stable across the prologue
18003 and epilogue, which makes it better than just a frame pointer,
18004 (2) With dwarf3, there exists a one-byte encoding that allows us
18005 to reference the .debug_frame data by proxy, but failing that,
18006 (3) We can at least reuse the code inspection and interpretation
18007 code that determines the CFA position at various points in the
18008 function. */
18009 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18011 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18012 add_AT_loc (subr_die, DW_AT_frame_base, op);
18014 else
18016 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18017 if (list->dw_loc_next)
18018 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18019 else
18020 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18023 /* Compute a displacement from the "steady-state frame pointer" to
18024 the CFA. The former is what all stack slots and argument slots
18025 will reference in the rtl; the latter is what we've told the
18026 debugger about. We'll need to adjust all frame_base references
18027 by this displacement. */
18028 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18030 if (fun->static_chain_decl)
18031 add_AT_location_description (subr_die, DW_AT_static_link,
18032 loc_list_from_tree (fun->static_chain_decl, 2));
18035 /* Generate child dies for template paramaters. */
18036 if (debug_info_level > DINFO_LEVEL_TERSE)
18037 gen_generic_params_dies (decl);
18039 /* Now output descriptions of the arguments for this function. This gets
18040 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18041 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18042 `...' at the end of the formal parameter list. In order to find out if
18043 there was a trailing ellipsis or not, we must instead look at the type
18044 associated with the FUNCTION_DECL. This will be a node of type
18045 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18046 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18047 an ellipsis at the end. */
18049 /* In the case where we are describing a mere function declaration, all we
18050 need to do here (and all we *can* do here) is to describe the *types* of
18051 its formal parameters. */
18052 if (debug_info_level <= DINFO_LEVEL_TERSE)
18054 else if (declaration)
18055 gen_formal_types_die (decl, subr_die);
18056 else
18058 /* Generate DIEs to represent all known formal parameters. */
18059 tree parm = DECL_ARGUMENTS (decl);
18060 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18061 tree generic_decl_parm = generic_decl
18062 ? DECL_ARGUMENTS (generic_decl)
18063 : NULL;
18065 /* Now we want to walk the list of parameters of the function and
18066 emit their relevant DIEs.
18068 We consider the case of DECL being an instance of a generic function
18069 as well as it being a normal function.
18071 If DECL is an instance of a generic function we walk the
18072 parameters of the generic function declaration _and_ the parameters of
18073 DECL itself. This is useful because we want to emit specific DIEs for
18074 function parameter packs and those are declared as part of the
18075 generic function declaration. In that particular case,
18076 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18077 That DIE has children DIEs representing the set of arguments
18078 of the pack. Note that the set of pack arguments can be empty.
18079 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18080 children DIE.
18082 Otherwise, we just consider the parameters of DECL. */
18083 while (generic_decl_parm || parm)
18085 if (generic_decl_parm
18086 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18087 gen_formal_parameter_pack_die (generic_decl_parm,
18088 parm, subr_die,
18089 &parm);
18090 else if (parm)
18092 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18094 if (parm == DECL_ARGUMENTS (decl)
18095 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18096 && parm_die
18097 && (dwarf_version >= 3 || !dwarf_strict))
18098 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18100 parm = DECL_CHAIN (parm);
18103 if (generic_decl_parm)
18104 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18107 /* Decide whether we need an unspecified_parameters DIE at the end.
18108 There are 2 more cases to do this for: 1) the ansi ... declaration -
18109 this is detectable when the end of the arg list is not a
18110 void_type_node 2) an unprototyped function declaration (not a
18111 definition). This just means that we have no info about the
18112 parameters at all. */
18113 if (prototype_p (TREE_TYPE (decl)))
18115 /* This is the prototyped case, check for.... */
18116 if (stdarg_p (TREE_TYPE (decl)))
18117 gen_unspecified_parameters_die (decl, subr_die);
18119 else if (DECL_INITIAL (decl) == NULL_TREE)
18120 gen_unspecified_parameters_die (decl, subr_die);
18123 /* Output Dwarf info for all of the stuff within the body of the function
18124 (if it has one - it may be just a declaration). */
18125 outer_scope = DECL_INITIAL (decl);
18127 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18128 a function. This BLOCK actually represents the outermost binding contour
18129 for the function, i.e. the contour in which the function's formal
18130 parameters and labels get declared. Curiously, it appears that the front
18131 end doesn't actually put the PARM_DECL nodes for the current function onto
18132 the BLOCK_VARS list for this outer scope, but are strung off of the
18133 DECL_ARGUMENTS list for the function instead.
18135 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18136 the LABEL_DECL nodes for the function however, and we output DWARF info
18137 for those in decls_for_scope. Just within the `outer_scope' there will be
18138 a BLOCK node representing the function's outermost pair of curly braces,
18139 and any blocks used for the base and member initializers of a C++
18140 constructor function. */
18141 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18143 int call_site_note_count = 0;
18144 int tail_call_site_note_count = 0;
18146 /* Emit a DW_TAG_variable DIE for a named return value. */
18147 if (DECL_NAME (DECL_RESULT (decl)))
18148 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18150 current_function_has_inlines = 0;
18151 decls_for_scope (outer_scope, subr_die, 0);
18153 if (call_arg_locations && !dwarf_strict)
18155 struct call_arg_loc_node *ca_loc;
18156 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18158 dw_die_ref die = NULL;
18159 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18160 rtx arg, next_arg;
18162 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18163 arg; arg = next_arg)
18165 dw_loc_descr_ref reg, val;
18166 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18167 dw_die_ref cdie, tdie = NULL;
18169 next_arg = XEXP (arg, 1);
18170 if (REG_P (XEXP (XEXP (arg, 0), 0))
18171 && next_arg
18172 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18173 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18174 && REGNO (XEXP (XEXP (arg, 0), 0))
18175 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18176 next_arg = XEXP (next_arg, 1);
18177 if (mode == VOIDmode)
18179 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18180 if (mode == VOIDmode)
18181 mode = GET_MODE (XEXP (arg, 0));
18183 if (mode == VOIDmode || mode == BLKmode)
18184 continue;
18185 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18187 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18188 tloc = XEXP (XEXP (arg, 0), 1);
18189 continue;
18191 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18192 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18194 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18195 tlocc = XEXP (XEXP (arg, 0), 1);
18196 continue;
18198 reg = NULL;
18199 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18200 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18201 VAR_INIT_STATUS_INITIALIZED);
18202 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18204 rtx mem = XEXP (XEXP (arg, 0), 0);
18205 reg = mem_loc_descriptor (XEXP (mem, 0),
18206 get_address_mode (mem),
18207 GET_MODE (mem),
18208 VAR_INIT_STATUS_INITIALIZED);
18210 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18211 == DEBUG_PARAMETER_REF)
18213 tree tdecl
18214 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18215 tdie = lookup_decl_die (tdecl);
18216 if (tdie == NULL)
18217 continue;
18219 else
18220 continue;
18221 if (reg == NULL
18222 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18223 != DEBUG_PARAMETER_REF)
18224 continue;
18225 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18226 VOIDmode,
18227 VAR_INIT_STATUS_INITIALIZED);
18228 if (val == NULL)
18229 continue;
18230 if (die == NULL)
18231 die = gen_call_site_die (decl, subr_die, ca_loc);
18232 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18233 NULL_TREE);
18234 if (reg != NULL)
18235 add_AT_loc (cdie, DW_AT_location, reg);
18236 else if (tdie != NULL)
18237 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18238 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18239 if (next_arg != XEXP (arg, 1))
18241 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18242 if (mode == VOIDmode)
18243 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18244 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18245 0), 1),
18246 mode, VOIDmode,
18247 VAR_INIT_STATUS_INITIALIZED);
18248 if (val != NULL)
18249 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18252 if (die == NULL
18253 && (ca_loc->symbol_ref || tloc))
18254 die = gen_call_site_die (decl, subr_die, ca_loc);
18255 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18257 dw_loc_descr_ref tval = NULL;
18259 if (tloc != NULL_RTX)
18260 tval = mem_loc_descriptor (tloc,
18261 GET_MODE (tloc) == VOIDmode
18262 ? Pmode : GET_MODE (tloc),
18263 VOIDmode,
18264 VAR_INIT_STATUS_INITIALIZED);
18265 if (tval)
18266 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18267 else if (tlocc != NULL_RTX)
18269 tval = mem_loc_descriptor (tlocc,
18270 GET_MODE (tlocc) == VOIDmode
18271 ? Pmode : GET_MODE (tlocc),
18272 VOIDmode,
18273 VAR_INIT_STATUS_INITIALIZED);
18274 if (tval)
18275 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18276 tval);
18279 if (die != NULL)
18281 call_site_note_count++;
18282 if (ca_loc->tail_call_p)
18283 tail_call_site_note_count++;
18287 call_arg_locations = NULL;
18288 call_arg_loc_last = NULL;
18289 if (tail_call_site_count >= 0
18290 && tail_call_site_count == tail_call_site_note_count
18291 && !dwarf_strict)
18293 if (call_site_count >= 0
18294 && call_site_count == call_site_note_count)
18295 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18296 else
18297 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18299 call_site_count = -1;
18300 tail_call_site_count = -1;
18302 /* Add the calling convention attribute if requested. */
18303 add_calling_convention_attribute (subr_die, decl);
18307 /* Returns a hash value for X (which really is a die_struct). */
18309 static hashval_t
18310 common_block_die_table_hash (const void *x)
18312 const_dw_die_ref d = (const_dw_die_ref) x;
18313 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18316 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18317 as decl_id and die_parent of die_struct Y. */
18319 static int
18320 common_block_die_table_eq (const void *x, const void *y)
18322 const_dw_die_ref d = (const_dw_die_ref) x;
18323 const_dw_die_ref e = (const_dw_die_ref) y;
18324 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18327 /* Generate a DIE to represent a declared data object.
18328 Either DECL or ORIGIN must be non-null. */
18330 static void
18331 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18333 HOST_WIDE_INT off = 0;
18334 tree com_decl;
18335 tree decl_or_origin = decl ? decl : origin;
18336 tree ultimate_origin;
18337 dw_die_ref var_die;
18338 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18339 dw_die_ref origin_die;
18340 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18341 || class_or_namespace_scope_p (context_die));
18342 bool specialization_p = false;
18344 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18345 if (decl || ultimate_origin)
18346 origin = ultimate_origin;
18347 com_decl = fortran_common (decl_or_origin, &off);
18349 /* Symbol in common gets emitted as a child of the common block, in the form
18350 of a data member. */
18351 if (com_decl)
18353 dw_die_ref com_die;
18354 dw_loc_list_ref loc;
18355 die_node com_die_arg;
18357 var_die = lookup_decl_die (decl_or_origin);
18358 if (var_die)
18360 if (get_AT (var_die, DW_AT_location) == NULL)
18362 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18363 if (loc)
18365 if (off)
18367 /* Optimize the common case. */
18368 if (single_element_loc_list_p (loc)
18369 && loc->expr->dw_loc_opc == DW_OP_addr
18370 && loc->expr->dw_loc_next == NULL
18371 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18372 == SYMBOL_REF)
18374 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18375 loc->expr->dw_loc_oprnd1.v.val_addr
18376 = plus_constant (GET_MODE (x), x , off);
18378 else
18379 loc_list_plus_const (loc, off);
18381 add_AT_location_description (var_die, DW_AT_location, loc);
18382 remove_AT (var_die, DW_AT_declaration);
18385 return;
18388 if (common_block_die_table == NULL)
18389 common_block_die_table
18390 = htab_create_ggc (10, common_block_die_table_hash,
18391 common_block_die_table_eq, NULL);
18393 com_die_arg.decl_id = DECL_UID (com_decl);
18394 com_die_arg.die_parent = context_die;
18395 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18396 loc = loc_list_from_tree (com_decl, 2);
18397 if (com_die == NULL)
18399 const char *cnam
18400 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18401 void **slot;
18403 com_die = new_die (DW_TAG_common_block, context_die, decl);
18404 add_name_and_src_coords_attributes (com_die, com_decl);
18405 if (loc)
18407 add_AT_location_description (com_die, DW_AT_location, loc);
18408 /* Avoid sharing the same loc descriptor between
18409 DW_TAG_common_block and DW_TAG_variable. */
18410 loc = loc_list_from_tree (com_decl, 2);
18412 else if (DECL_EXTERNAL (decl))
18413 add_AT_flag (com_die, DW_AT_declaration, 1);
18414 if (want_pubnames ())
18415 add_pubname_string (cnam, com_die); /* ??? needed? */
18416 com_die->decl_id = DECL_UID (com_decl);
18417 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18418 *slot = (void *) com_die;
18420 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18422 add_AT_location_description (com_die, DW_AT_location, loc);
18423 loc = loc_list_from_tree (com_decl, 2);
18424 remove_AT (com_die, DW_AT_declaration);
18426 var_die = new_die (DW_TAG_variable, com_die, decl);
18427 add_name_and_src_coords_attributes (var_die, decl);
18428 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18429 TREE_THIS_VOLATILE (decl), context_die);
18430 add_AT_flag (var_die, DW_AT_external, 1);
18431 if (loc)
18433 if (off)
18435 /* Optimize the common case. */
18436 if (single_element_loc_list_p (loc)
18437 && loc->expr->dw_loc_opc == DW_OP_addr
18438 && loc->expr->dw_loc_next == NULL
18439 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18441 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18442 loc->expr->dw_loc_oprnd1.v.val_addr
18443 = plus_constant (GET_MODE (x), x, off);
18445 else
18446 loc_list_plus_const (loc, off);
18448 add_AT_location_description (var_die, DW_AT_location, loc);
18450 else if (DECL_EXTERNAL (decl))
18451 add_AT_flag (var_die, DW_AT_declaration, 1);
18452 equate_decl_number_to_die (decl, var_die);
18453 return;
18456 /* If the compiler emitted a definition for the DECL declaration
18457 and if we already emitted a DIE for it, don't emit a second
18458 DIE for it again. Allow re-declarations of DECLs that are
18459 inside functions, though. */
18460 if (old_die && declaration && !local_scope_p (context_die))
18461 return;
18463 /* For static data members, the declaration in the class is supposed
18464 to have DW_TAG_member tag; the specification should still be
18465 DW_TAG_variable referencing the DW_TAG_member DIE. */
18466 if (declaration && class_scope_p (context_die))
18467 var_die = new_die (DW_TAG_member, context_die, decl);
18468 else
18469 var_die = new_die (DW_TAG_variable, context_die, decl);
18471 origin_die = NULL;
18472 if (origin != NULL)
18473 origin_die = add_abstract_origin_attribute (var_die, origin);
18475 /* Loop unrolling can create multiple blocks that refer to the same
18476 static variable, so we must test for the DW_AT_declaration flag.
18478 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18479 copy decls and set the DECL_ABSTRACT flag on them instead of
18480 sharing them.
18482 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18484 ??? The declare_in_namespace support causes us to get two DIEs for one
18485 variable, both of which are declarations. We want to avoid considering
18486 one to be a specification, so we must test that this DIE is not a
18487 declaration. */
18488 else if (old_die && TREE_STATIC (decl) && ! declaration
18489 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18491 /* This is a definition of a C++ class level static. */
18492 add_AT_specification (var_die, old_die);
18493 specialization_p = true;
18494 if (DECL_NAME (decl))
18496 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18497 struct dwarf_file_data * file_index = lookup_filename (s.file);
18499 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18500 add_AT_file (var_die, DW_AT_decl_file, file_index);
18502 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18503 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18505 if (old_die->die_tag == DW_TAG_member)
18506 add_linkage_name (var_die, decl);
18509 else
18510 add_name_and_src_coords_attributes (var_die, decl);
18512 if ((origin == NULL && !specialization_p)
18513 || (origin != NULL
18514 && !DECL_ABSTRACT (decl_or_origin)
18515 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18516 decl_function_context
18517 (decl_or_origin))))
18519 tree type = TREE_TYPE (decl_or_origin);
18521 if (decl_by_reference_p (decl_or_origin))
18522 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18523 else
18524 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18525 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18528 if (origin == NULL && !specialization_p)
18530 if (TREE_PUBLIC (decl))
18531 add_AT_flag (var_die, DW_AT_external, 1);
18533 if (DECL_ARTIFICIAL (decl))
18534 add_AT_flag (var_die, DW_AT_artificial, 1);
18536 add_accessibility_attribute (var_die, decl);
18539 if (declaration)
18540 add_AT_flag (var_die, DW_AT_declaration, 1);
18542 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18543 equate_decl_number_to_die (decl, var_die);
18545 if (! declaration
18546 && (! DECL_ABSTRACT (decl_or_origin)
18547 /* Local static vars are shared between all clones/inlines,
18548 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18549 already set. */
18550 || (TREE_CODE (decl_or_origin) == VAR_DECL
18551 && TREE_STATIC (decl_or_origin)
18552 && DECL_RTL_SET_P (decl_or_origin)))
18553 /* When abstract origin already has DW_AT_location attribute, no need
18554 to add it again. */
18555 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18557 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18558 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18559 defer_location (decl_or_origin, var_die);
18560 else
18561 add_location_or_const_value_attribute (var_die, decl_or_origin,
18562 decl == NULL, DW_AT_location);
18563 add_pubname (decl_or_origin, var_die);
18565 else
18566 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18569 /* Generate a DIE to represent a named constant. */
18571 static void
18572 gen_const_die (tree decl, dw_die_ref context_die)
18574 dw_die_ref const_die;
18575 tree type = TREE_TYPE (decl);
18577 const_die = new_die (DW_TAG_constant, context_die, decl);
18578 add_name_and_src_coords_attributes (const_die, decl);
18579 add_type_attribute (const_die, type, 1, 0, context_die);
18580 if (TREE_PUBLIC (decl))
18581 add_AT_flag (const_die, DW_AT_external, 1);
18582 if (DECL_ARTIFICIAL (decl))
18583 add_AT_flag (const_die, DW_AT_artificial, 1);
18584 tree_add_const_value_attribute_for_decl (const_die, decl);
18587 /* Generate a DIE to represent a label identifier. */
18589 static void
18590 gen_label_die (tree decl, dw_die_ref context_die)
18592 tree origin = decl_ultimate_origin (decl);
18593 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18594 rtx insn;
18595 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18597 if (origin != NULL)
18598 add_abstract_origin_attribute (lbl_die, origin);
18599 else
18600 add_name_and_src_coords_attributes (lbl_die, decl);
18602 if (DECL_ABSTRACT (decl))
18603 equate_decl_number_to_die (decl, lbl_die);
18604 else
18606 insn = DECL_RTL_IF_SET (decl);
18608 /* Deleted labels are programmer specified labels which have been
18609 eliminated because of various optimizations. We still emit them
18610 here so that it is possible to put breakpoints on them. */
18611 if (insn
18612 && (LABEL_P (insn)
18613 || ((NOTE_P (insn)
18614 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18616 /* When optimization is enabled (via -O) some parts of the compiler
18617 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18618 represent source-level labels which were explicitly declared by
18619 the user. This really shouldn't be happening though, so catch
18620 it if it ever does happen. */
18621 gcc_assert (!INSN_DELETED_P (insn));
18623 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18624 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18626 else if (insn
18627 && NOTE_P (insn)
18628 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18629 && CODE_LABEL_NUMBER (insn) != -1)
18631 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18632 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18637 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18638 attributes to the DIE for a block STMT, to describe where the inlined
18639 function was called from. This is similar to add_src_coords_attributes. */
18641 static inline void
18642 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18644 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18646 if (dwarf_version >= 3 || !dwarf_strict)
18648 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18649 add_AT_unsigned (die, DW_AT_call_line, s.line);
18654 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18655 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18657 static inline void
18658 add_high_low_attributes (tree stmt, dw_die_ref die)
18660 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18662 if (BLOCK_FRAGMENT_CHAIN (stmt)
18663 && (dwarf_version >= 3 || !dwarf_strict))
18665 tree chain, superblock = NULL_TREE;
18666 dw_die_ref pdie;
18667 dw_attr_ref attr = NULL;
18669 if (inlined_function_outer_scope_p (stmt))
18671 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18672 BLOCK_NUMBER (stmt));
18673 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18676 /* Optimize duplicate .debug_ranges lists or even tails of
18677 lists. If this BLOCK has same ranges as its supercontext,
18678 lookup DW_AT_ranges attribute in the supercontext (and
18679 recursively so), verify that the ranges_table contains the
18680 right values and use it instead of adding a new .debug_range. */
18681 for (chain = stmt, pdie = die;
18682 BLOCK_SAME_RANGE (chain);
18683 chain = BLOCK_SUPERCONTEXT (chain))
18685 dw_attr_ref new_attr;
18687 pdie = pdie->die_parent;
18688 if (pdie == NULL)
18689 break;
18690 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18691 break;
18692 new_attr = get_AT (pdie, DW_AT_ranges);
18693 if (new_attr == NULL
18694 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18695 break;
18696 attr = new_attr;
18697 superblock = BLOCK_SUPERCONTEXT (chain);
18699 if (attr != NULL
18700 && (ranges_table[attr->dw_attr_val.v.val_offset
18701 / 2 / DWARF2_ADDR_SIZE].num
18702 == BLOCK_NUMBER (superblock))
18703 && BLOCK_FRAGMENT_CHAIN (superblock))
18705 unsigned long off = attr->dw_attr_val.v.val_offset
18706 / 2 / DWARF2_ADDR_SIZE;
18707 unsigned long supercnt = 0, thiscnt = 0;
18708 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18709 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18711 ++supercnt;
18712 gcc_checking_assert (ranges_table[off + supercnt].num
18713 == BLOCK_NUMBER (chain));
18715 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18716 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18717 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18718 ++thiscnt;
18719 gcc_assert (supercnt >= thiscnt);
18720 add_AT_range_list (die, DW_AT_ranges,
18721 ((off + supercnt - thiscnt)
18722 * 2 * DWARF2_ADDR_SIZE),
18723 false);
18724 return;
18727 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18729 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18732 add_ranges (chain);
18733 chain = BLOCK_FRAGMENT_CHAIN (chain);
18735 while (chain);
18736 add_ranges (NULL);
18738 else
18740 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18741 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18742 BLOCK_NUMBER (stmt));
18743 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18744 BLOCK_NUMBER (stmt));
18745 add_AT_low_high_pc (die, label, label_high, false);
18749 /* Generate a DIE for a lexical block. */
18751 static void
18752 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18754 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18756 if (call_arg_locations)
18758 if (block_map.length () <= BLOCK_NUMBER (stmt))
18759 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18760 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18763 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18764 add_high_low_attributes (stmt, stmt_die);
18766 decls_for_scope (stmt, stmt_die, depth);
18769 /* Generate a DIE for an inlined subprogram. */
18771 static void
18772 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18774 tree decl;
18776 /* The instance of function that is effectively being inlined shall not
18777 be abstract. */
18778 gcc_assert (! BLOCK_ABSTRACT (stmt));
18780 decl = block_ultimate_origin (stmt);
18782 /* Emit info for the abstract instance first, if we haven't yet. We
18783 must emit this even if the block is abstract, otherwise when we
18784 emit the block below (or elsewhere), we may end up trying to emit
18785 a die whose origin die hasn't been emitted, and crashing. */
18786 dwarf2out_abstract_function (decl);
18788 if (! BLOCK_ABSTRACT (stmt))
18790 dw_die_ref subr_die
18791 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18793 if (call_arg_locations)
18795 if (block_map.length () <= BLOCK_NUMBER (stmt))
18796 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18797 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18799 add_abstract_origin_attribute (subr_die, decl);
18800 if (TREE_ASM_WRITTEN (stmt))
18801 add_high_low_attributes (stmt, subr_die);
18802 add_call_src_coords_attributes (stmt, subr_die);
18804 decls_for_scope (stmt, subr_die, depth);
18805 current_function_has_inlines = 1;
18809 /* Generate a DIE for a field in a record, or structure. */
18811 static void
18812 gen_field_die (tree decl, dw_die_ref context_die)
18814 dw_die_ref decl_die;
18816 if (TREE_TYPE (decl) == error_mark_node)
18817 return;
18819 decl_die = new_die (DW_TAG_member, context_die, decl);
18820 add_name_and_src_coords_attributes (decl_die, decl);
18821 add_type_attribute (decl_die, member_declared_type (decl),
18822 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18823 context_die);
18825 if (DECL_BIT_FIELD_TYPE (decl))
18827 add_byte_size_attribute (decl_die, decl);
18828 add_bit_size_attribute (decl_die, decl);
18829 add_bit_offset_attribute (decl_die, decl);
18832 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18833 add_data_member_location_attribute (decl_die, decl);
18835 if (DECL_ARTIFICIAL (decl))
18836 add_AT_flag (decl_die, DW_AT_artificial, 1);
18838 add_accessibility_attribute (decl_die, decl);
18840 /* Equate decl number to die, so that we can look up this decl later on. */
18841 equate_decl_number_to_die (decl, decl_die);
18844 #if 0
18845 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18846 Use modified_type_die instead.
18847 We keep this code here just in case these types of DIEs may be needed to
18848 represent certain things in other languages (e.g. Pascal) someday. */
18850 static void
18851 gen_pointer_type_die (tree type, dw_die_ref context_die)
18853 dw_die_ref ptr_die
18854 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18856 equate_type_number_to_die (type, ptr_die);
18857 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18858 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18861 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18862 Use modified_type_die instead.
18863 We keep this code here just in case these types of DIEs may be needed to
18864 represent certain things in other languages (e.g. Pascal) someday. */
18866 static void
18867 gen_reference_type_die (tree type, dw_die_ref context_die)
18869 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18871 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18872 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18873 else
18874 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18876 equate_type_number_to_die (type, ref_die);
18877 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18878 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18880 #endif
18882 /* Generate a DIE for a pointer to a member type. */
18884 static void
18885 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18887 dw_die_ref ptr_die
18888 = new_die (DW_TAG_ptr_to_member_type,
18889 scope_die_for (type, context_die), type);
18891 equate_type_number_to_die (type, ptr_die);
18892 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18893 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18894 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18897 typedef const char *dchar_p; /* For DEF_VEC_P. */
18899 static char *producer_string;
18901 /* Return a heap allocated producer string including command line options
18902 if -grecord-gcc-switches. */
18904 static char *
18905 gen_producer_string (void)
18907 size_t j;
18908 vec<dchar_p> switches = vNULL;
18909 const char *language_string = lang_hooks.name;
18910 char *producer, *tail;
18911 const char *p;
18912 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18913 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18915 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18916 switch (save_decoded_options[j].opt_index)
18918 case OPT_o:
18919 case OPT_d:
18920 case OPT_dumpbase:
18921 case OPT_dumpdir:
18922 case OPT_auxbase:
18923 case OPT_auxbase_strip:
18924 case OPT_quiet:
18925 case OPT_version:
18926 case OPT_v:
18927 case OPT_w:
18928 case OPT_L:
18929 case OPT_D:
18930 case OPT_I:
18931 case OPT_U:
18932 case OPT_SPECIAL_unknown:
18933 case OPT_SPECIAL_ignore:
18934 case OPT_SPECIAL_program_name:
18935 case OPT_SPECIAL_input_file:
18936 case OPT_grecord_gcc_switches:
18937 case OPT_gno_record_gcc_switches:
18938 case OPT__output_pch_:
18939 case OPT_fdiagnostics_show_location_:
18940 case OPT_fdiagnostics_show_option:
18941 case OPT_fdiagnostics_show_caret:
18942 case OPT_fdiagnostics_color_:
18943 case OPT_fverbose_asm:
18944 case OPT____:
18945 case OPT__sysroot_:
18946 case OPT_nostdinc:
18947 case OPT_nostdinc__:
18948 /* Ignore these. */
18949 continue;
18950 default:
18951 if (cl_options[save_decoded_options[j].opt_index].flags
18952 & CL_NO_DWARF_RECORD)
18953 continue;
18954 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18955 == '-');
18956 switch (save_decoded_options[j].canonical_option[0][1])
18958 case 'M':
18959 case 'i':
18960 case 'W':
18961 continue;
18962 case 'f':
18963 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18964 "dump", 4) == 0)
18965 continue;
18966 break;
18967 default:
18968 break;
18970 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18971 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18972 break;
18975 producer = XNEWVEC (char, plen + 1 + len + 1);
18976 tail = producer;
18977 sprintf (tail, "%s %s", language_string, version_string);
18978 tail += plen;
18980 FOR_EACH_VEC_ELT (switches, j, p)
18982 len = strlen (p);
18983 *tail = ' ';
18984 memcpy (tail + 1, p, len);
18985 tail += len + 1;
18988 *tail = '\0';
18989 switches.release ();
18990 return producer;
18993 /* Generate the DIE for the compilation unit. */
18995 static dw_die_ref
18996 gen_compile_unit_die (const char *filename)
18998 dw_die_ref die;
18999 const char *language_string = lang_hooks.name;
19000 int language;
19002 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19004 if (filename)
19006 add_name_attribute (die, filename);
19007 /* Don't add cwd for <built-in>. */
19008 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19009 add_comp_dir_attribute (die);
19012 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19014 /* If our producer is LTO try to figure out a common language to use
19015 from the global list of translation units. */
19016 if (strcmp (language_string, "GNU GIMPLE") == 0)
19018 unsigned i;
19019 tree t;
19020 const char *common_lang = NULL;
19022 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19024 if (!TRANSLATION_UNIT_LANGUAGE (t))
19025 continue;
19026 if (!common_lang)
19027 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19028 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19030 else if (strncmp (common_lang, "GNU C", 5) == 0
19031 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19032 /* Mixing C and C++ is ok, use C++ in that case. */
19033 common_lang = "GNU C++";
19034 else
19036 /* Fall back to C. */
19037 common_lang = NULL;
19038 break;
19042 if (common_lang)
19043 language_string = common_lang;
19046 language = DW_LANG_C89;
19047 if (strcmp (language_string, "GNU C++") == 0)
19048 language = DW_LANG_C_plus_plus;
19049 else if (strcmp (language_string, "GNU F77") == 0)
19050 language = DW_LANG_Fortran77;
19051 else if (strcmp (language_string, "GNU Pascal") == 0)
19052 language = DW_LANG_Pascal83;
19053 else if (dwarf_version >= 3 || !dwarf_strict)
19055 if (strcmp (language_string, "GNU Ada") == 0)
19056 language = DW_LANG_Ada95;
19057 else if (strcmp (language_string, "GNU Fortran") == 0)
19058 language = DW_LANG_Fortran95;
19059 else if (strcmp (language_string, "GNU Java") == 0)
19060 language = DW_LANG_Java;
19061 else if (strcmp (language_string, "GNU Objective-C") == 0)
19062 language = DW_LANG_ObjC;
19063 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19064 language = DW_LANG_ObjC_plus_plus;
19065 else if (dwarf_version >= 5 || !dwarf_strict)
19067 if (strcmp (language_string, "GNU Go") == 0)
19068 language = DW_LANG_Go;
19071 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19072 else if (strcmp (language_string, "GNU Fortran") == 0)
19073 language = DW_LANG_Fortran90;
19075 add_AT_unsigned (die, DW_AT_language, language);
19077 switch (language)
19079 case DW_LANG_Fortran77:
19080 case DW_LANG_Fortran90:
19081 case DW_LANG_Fortran95:
19082 /* Fortran has case insensitive identifiers and the front-end
19083 lowercases everything. */
19084 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19085 break;
19086 default:
19087 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19088 break;
19090 return die;
19093 /* Generate the DIE for a base class. */
19095 static void
19096 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19098 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19100 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19101 add_data_member_location_attribute (die, binfo);
19103 if (BINFO_VIRTUAL_P (binfo))
19104 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19106 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19107 children, otherwise the default is DW_ACCESS_public. In DWARF2
19108 the default has always been DW_ACCESS_private. */
19109 if (access == access_public_node)
19111 if (dwarf_version == 2
19112 || context_die->die_tag == DW_TAG_class_type)
19113 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19115 else if (access == access_protected_node)
19116 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19117 else if (dwarf_version > 2
19118 && context_die->die_tag != DW_TAG_class_type)
19119 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19122 /* Generate a DIE for a class member. */
19124 static void
19125 gen_member_die (tree type, dw_die_ref context_die)
19127 tree member;
19128 tree binfo = TYPE_BINFO (type);
19129 dw_die_ref child;
19131 /* If this is not an incomplete type, output descriptions of each of its
19132 members. Note that as we output the DIEs necessary to represent the
19133 members of this record or union type, we will also be trying to output
19134 DIEs to represent the *types* of those members. However the `type'
19135 function (above) will specifically avoid generating type DIEs for member
19136 types *within* the list of member DIEs for this (containing) type except
19137 for those types (of members) which are explicitly marked as also being
19138 members of this (containing) type themselves. The g++ front- end can
19139 force any given type to be treated as a member of some other (containing)
19140 type by setting the TYPE_CONTEXT of the given (member) type to point to
19141 the TREE node representing the appropriate (containing) type. */
19143 /* First output info about the base classes. */
19144 if (binfo)
19146 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19147 int i;
19148 tree base;
19150 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19151 gen_inheritance_die (base,
19152 (accesses ? (*accesses)[i] : access_public_node),
19153 context_die);
19156 /* Now output info about the data members and type members. */
19157 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19159 /* If we thought we were generating minimal debug info for TYPE
19160 and then changed our minds, some of the member declarations
19161 may have already been defined. Don't define them again, but
19162 do put them in the right order. */
19164 child = lookup_decl_die (member);
19165 if (child)
19166 splice_child_die (context_die, child);
19167 else
19168 gen_decl_die (member, NULL, context_die);
19171 /* Now output info about the function members (if any). */
19172 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19174 /* Don't include clones in the member list. */
19175 if (DECL_ABSTRACT_ORIGIN (member))
19176 continue;
19178 child = lookup_decl_die (member);
19179 if (child)
19180 splice_child_die (context_die, child);
19181 else
19182 gen_decl_die (member, NULL, context_die);
19186 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19187 is set, we pretend that the type was never defined, so we only get the
19188 member DIEs needed by later specification DIEs. */
19190 static void
19191 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19192 enum debug_info_usage usage)
19194 dw_die_ref type_die = lookup_type_die (type);
19195 dw_die_ref scope_die = 0;
19196 int nested = 0;
19197 int complete = (TYPE_SIZE (type)
19198 && (! TYPE_STUB_DECL (type)
19199 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19200 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19201 complete = complete && should_emit_struct_debug (type, usage);
19203 if (type_die && ! complete)
19204 return;
19206 if (TYPE_CONTEXT (type) != NULL_TREE
19207 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19208 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19209 nested = 1;
19211 scope_die = scope_die_for (type, context_die);
19213 /* Generate child dies for template paramaters. */
19214 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19215 schedule_generic_params_dies_gen (type);
19217 if (! type_die || (nested && is_cu_die (scope_die)))
19218 /* First occurrence of type or toplevel definition of nested class. */
19220 dw_die_ref old_die = type_die;
19222 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19223 ? record_type_tag (type) : DW_TAG_union_type,
19224 scope_die, type);
19225 equate_type_number_to_die (type, type_die);
19226 if (old_die)
19227 add_AT_specification (type_die, old_die);
19228 else
19229 add_name_attribute (type_die, type_tag (type));
19231 else
19232 remove_AT (type_die, DW_AT_declaration);
19234 /* If this type has been completed, then give it a byte_size attribute and
19235 then give a list of members. */
19236 if (complete && !ns_decl)
19238 /* Prevent infinite recursion in cases where the type of some member of
19239 this type is expressed in terms of this type itself. */
19240 TREE_ASM_WRITTEN (type) = 1;
19241 add_byte_size_attribute (type_die, type);
19242 if (TYPE_STUB_DECL (type) != NULL_TREE)
19244 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19245 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19248 /* If the first reference to this type was as the return type of an
19249 inline function, then it may not have a parent. Fix this now. */
19250 if (type_die->die_parent == NULL)
19251 add_child_die (scope_die, type_die);
19253 push_decl_scope (type);
19254 gen_member_die (type, type_die);
19255 pop_decl_scope ();
19257 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19258 if (TYPE_ARTIFICIAL (type))
19259 add_AT_flag (type_die, DW_AT_artificial, 1);
19261 /* GNU extension: Record what type our vtable lives in. */
19262 if (TYPE_VFIELD (type))
19264 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19266 gen_type_die (vtype, context_die);
19267 add_AT_die_ref (type_die, DW_AT_containing_type,
19268 lookup_type_die (vtype));
19271 else
19273 add_AT_flag (type_die, DW_AT_declaration, 1);
19275 /* We don't need to do this for function-local types. */
19276 if (TYPE_STUB_DECL (type)
19277 && ! decl_function_context (TYPE_STUB_DECL (type)))
19278 vec_safe_push (incomplete_types, type);
19281 if (get_AT (type_die, DW_AT_name))
19282 add_pubtype (type, type_die);
19285 /* Generate a DIE for a subroutine _type_. */
19287 static void
19288 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19290 tree return_type = TREE_TYPE (type);
19291 dw_die_ref subr_die
19292 = new_die (DW_TAG_subroutine_type,
19293 scope_die_for (type, context_die), type);
19295 equate_type_number_to_die (type, subr_die);
19296 add_prototyped_attribute (subr_die, type);
19297 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19298 gen_formal_types_die (type, subr_die);
19300 if (get_AT (subr_die, DW_AT_name))
19301 add_pubtype (type, subr_die);
19304 /* Generate a DIE for a type definition. */
19306 static void
19307 gen_typedef_die (tree decl, dw_die_ref context_die)
19309 dw_die_ref type_die;
19310 tree origin;
19312 if (TREE_ASM_WRITTEN (decl))
19313 return;
19315 TREE_ASM_WRITTEN (decl) = 1;
19316 type_die = new_die (DW_TAG_typedef, context_die, decl);
19317 origin = decl_ultimate_origin (decl);
19318 if (origin != NULL)
19319 add_abstract_origin_attribute (type_die, origin);
19320 else
19322 tree type;
19324 add_name_and_src_coords_attributes (type_die, decl);
19325 if (DECL_ORIGINAL_TYPE (decl))
19327 type = DECL_ORIGINAL_TYPE (decl);
19329 gcc_assert (type != TREE_TYPE (decl));
19330 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19332 else
19334 type = TREE_TYPE (decl);
19336 if (is_naming_typedef_decl (TYPE_NAME (type)))
19338 /* Here, we are in the case of decl being a typedef naming
19339 an anonymous type, e.g:
19340 typedef struct {...} foo;
19341 In that case TREE_TYPE (decl) is not a typedef variant
19342 type and TYPE_NAME of the anonymous type is set to the
19343 TYPE_DECL of the typedef. This construct is emitted by
19344 the C++ FE.
19346 TYPE is the anonymous struct named by the typedef
19347 DECL. As we need the DW_AT_type attribute of the
19348 DW_TAG_typedef to point to the DIE of TYPE, let's
19349 generate that DIE right away. add_type_attribute
19350 called below will then pick (via lookup_type_die) that
19351 anonymous struct DIE. */
19352 if (!TREE_ASM_WRITTEN (type))
19353 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19355 /* This is a GNU Extension. We are adding a
19356 DW_AT_linkage_name attribute to the DIE of the
19357 anonymous struct TYPE. The value of that attribute
19358 is the name of the typedef decl naming the anonymous
19359 struct. This greatly eases the work of consumers of
19360 this debug info. */
19361 add_linkage_attr (lookup_type_die (type), decl);
19365 add_type_attribute (type_die, type, TREE_READONLY (decl),
19366 TREE_THIS_VOLATILE (decl), context_die);
19368 if (is_naming_typedef_decl (decl))
19369 /* We want that all subsequent calls to lookup_type_die with
19370 TYPE in argument yield the DW_TAG_typedef we have just
19371 created. */
19372 equate_type_number_to_die (type, type_die);
19374 add_accessibility_attribute (type_die, decl);
19377 if (DECL_ABSTRACT (decl))
19378 equate_decl_number_to_die (decl, type_die);
19380 if (get_AT (type_die, DW_AT_name))
19381 add_pubtype (decl, type_die);
19384 /* Generate a DIE for a struct, class, enum or union type. */
19386 static void
19387 gen_tagged_type_die (tree type,
19388 dw_die_ref context_die,
19389 enum debug_info_usage usage)
19391 int need_pop;
19393 if (type == NULL_TREE
19394 || !is_tagged_type (type))
19395 return;
19397 /* If this is a nested type whose containing class hasn't been written
19398 out yet, writing it out will cover this one, too. This does not apply
19399 to instantiations of member class templates; they need to be added to
19400 the containing class as they are generated. FIXME: This hurts the
19401 idea of combining type decls from multiple TUs, since we can't predict
19402 what set of template instantiations we'll get. */
19403 if (TYPE_CONTEXT (type)
19404 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19405 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19407 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19409 if (TREE_ASM_WRITTEN (type))
19410 return;
19412 /* If that failed, attach ourselves to the stub. */
19413 push_decl_scope (TYPE_CONTEXT (type));
19414 context_die = lookup_type_die (TYPE_CONTEXT (type));
19415 need_pop = 1;
19417 else if (TYPE_CONTEXT (type) != NULL_TREE
19418 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19420 /* If this type is local to a function that hasn't been written
19421 out yet, use a NULL context for now; it will be fixed up in
19422 decls_for_scope. */
19423 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19424 /* A declaration DIE doesn't count; nested types need to go in the
19425 specification. */
19426 if (context_die && is_declaration_die (context_die))
19427 context_die = NULL;
19428 need_pop = 0;
19430 else
19432 context_die = declare_in_namespace (type, context_die);
19433 need_pop = 0;
19436 if (TREE_CODE (type) == ENUMERAL_TYPE)
19438 /* This might have been written out by the call to
19439 declare_in_namespace. */
19440 if (!TREE_ASM_WRITTEN (type))
19441 gen_enumeration_type_die (type, context_die);
19443 else
19444 gen_struct_or_union_type_die (type, context_die, usage);
19446 if (need_pop)
19447 pop_decl_scope ();
19449 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19450 it up if it is ever completed. gen_*_type_die will set it for us
19451 when appropriate. */
19454 /* Generate a type description DIE. */
19456 static void
19457 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19458 enum debug_info_usage usage)
19460 struct array_descr_info info;
19462 if (type == NULL_TREE || type == error_mark_node)
19463 return;
19465 if (TYPE_NAME (type) != NULL_TREE
19466 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19467 && is_redundant_typedef (TYPE_NAME (type))
19468 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19469 /* The DECL of this type is a typedef we don't want to emit debug
19470 info for but we want debug info for its underlying typedef.
19471 This can happen for e.g, the injected-class-name of a C++
19472 type. */
19473 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19475 /* If TYPE is a typedef type variant, let's generate debug info
19476 for the parent typedef which TYPE is a type of. */
19477 if (typedef_variant_p (type))
19479 if (TREE_ASM_WRITTEN (type))
19480 return;
19482 /* Prevent broken recursion; we can't hand off to the same type. */
19483 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19485 /* Give typedefs the right scope. */
19486 context_die = scope_die_for (type, context_die);
19488 TREE_ASM_WRITTEN (type) = 1;
19490 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19491 return;
19494 /* If type is an anonymous tagged type named by a typedef, let's
19495 generate debug info for the typedef. */
19496 if (is_naming_typedef_decl (TYPE_NAME (type)))
19498 /* Use the DIE of the containing namespace as the parent DIE of
19499 the type description DIE we want to generate. */
19500 if (DECL_CONTEXT (TYPE_NAME (type))
19501 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19502 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19504 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19505 return;
19508 /* If this is an array type with hidden descriptor, handle it first. */
19509 if (!TREE_ASM_WRITTEN (type)
19510 && lang_hooks.types.get_array_descr_info
19511 && lang_hooks.types.get_array_descr_info (type, &info)
19512 && (dwarf_version >= 3 || !dwarf_strict))
19514 gen_descr_array_type_die (type, &info, context_die);
19515 TREE_ASM_WRITTEN (type) = 1;
19516 return;
19519 /* We are going to output a DIE to represent the unqualified version
19520 of this type (i.e. without any const or volatile qualifiers) so
19521 get the main variant (i.e. the unqualified version) of this type
19522 now. (Vectors are special because the debugging info is in the
19523 cloned type itself). */
19524 if (TREE_CODE (type) != VECTOR_TYPE)
19525 type = type_main_variant (type);
19527 if (TREE_ASM_WRITTEN (type))
19528 return;
19530 switch (TREE_CODE (type))
19532 case ERROR_MARK:
19533 break;
19535 case POINTER_TYPE:
19536 case REFERENCE_TYPE:
19537 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19538 ensures that the gen_type_die recursion will terminate even if the
19539 type is recursive. Recursive types are possible in Ada. */
19540 /* ??? We could perhaps do this for all types before the switch
19541 statement. */
19542 TREE_ASM_WRITTEN (type) = 1;
19544 /* For these types, all that is required is that we output a DIE (or a
19545 set of DIEs) to represent the "basis" type. */
19546 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19547 DINFO_USAGE_IND_USE);
19548 break;
19550 case OFFSET_TYPE:
19551 /* This code is used for C++ pointer-to-data-member types.
19552 Output a description of the relevant class type. */
19553 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19554 DINFO_USAGE_IND_USE);
19556 /* Output a description of the type of the object pointed to. */
19557 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19558 DINFO_USAGE_IND_USE);
19560 /* Now output a DIE to represent this pointer-to-data-member type
19561 itself. */
19562 gen_ptr_to_mbr_type_die (type, context_die);
19563 break;
19565 case FUNCTION_TYPE:
19566 /* Force out return type (in case it wasn't forced out already). */
19567 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19568 DINFO_USAGE_DIR_USE);
19569 gen_subroutine_type_die (type, context_die);
19570 break;
19572 case METHOD_TYPE:
19573 /* Force out return type (in case it wasn't forced out already). */
19574 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19575 DINFO_USAGE_DIR_USE);
19576 gen_subroutine_type_die (type, context_die);
19577 break;
19579 case ARRAY_TYPE:
19580 gen_array_type_die (type, context_die);
19581 break;
19583 case VECTOR_TYPE:
19584 gen_array_type_die (type, context_die);
19585 break;
19587 case ENUMERAL_TYPE:
19588 case RECORD_TYPE:
19589 case UNION_TYPE:
19590 case QUAL_UNION_TYPE:
19591 gen_tagged_type_die (type, context_die, usage);
19592 return;
19594 case VOID_TYPE:
19595 case INTEGER_TYPE:
19596 case REAL_TYPE:
19597 case FIXED_POINT_TYPE:
19598 case COMPLEX_TYPE:
19599 case BOOLEAN_TYPE:
19600 /* No DIEs needed for fundamental types. */
19601 break;
19603 case NULLPTR_TYPE:
19604 case LANG_TYPE:
19605 /* Just use DW_TAG_unspecified_type. */
19607 dw_die_ref type_die = lookup_type_die (type);
19608 if (type_die == NULL)
19610 tree name = TYPE_NAME (type);
19611 if (TREE_CODE (name) == TYPE_DECL)
19612 name = DECL_NAME (name);
19613 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19614 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19615 equate_type_number_to_die (type, type_die);
19618 break;
19620 default:
19621 gcc_unreachable ();
19624 TREE_ASM_WRITTEN (type) = 1;
19627 static void
19628 gen_type_die (tree type, dw_die_ref context_die)
19630 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19633 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19634 things which are local to the given block. */
19636 static void
19637 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19639 int must_output_die = 0;
19640 bool inlined_func;
19642 /* Ignore blocks that are NULL. */
19643 if (stmt == NULL_TREE)
19644 return;
19646 inlined_func = inlined_function_outer_scope_p (stmt);
19648 /* If the block is one fragment of a non-contiguous block, do not
19649 process the variables, since they will have been done by the
19650 origin block. Do process subblocks. */
19651 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19653 tree sub;
19655 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19656 gen_block_die (sub, context_die, depth + 1);
19658 return;
19661 /* Determine if we need to output any Dwarf DIEs at all to represent this
19662 block. */
19663 if (inlined_func)
19664 /* The outer scopes for inlinings *must* always be represented. We
19665 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19666 must_output_die = 1;
19667 else
19669 /* Determine if this block directly contains any "significant"
19670 local declarations which we will need to output DIEs for. */
19671 if (debug_info_level > DINFO_LEVEL_TERSE)
19672 /* We are not in terse mode so *any* local declaration counts
19673 as being a "significant" one. */
19674 must_output_die = ((BLOCK_VARS (stmt) != NULL
19675 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19676 && (TREE_USED (stmt)
19677 || TREE_ASM_WRITTEN (stmt)
19678 || BLOCK_ABSTRACT (stmt)));
19679 else if ((TREE_USED (stmt)
19680 || TREE_ASM_WRITTEN (stmt)
19681 || BLOCK_ABSTRACT (stmt))
19682 && !dwarf2out_ignore_block (stmt))
19683 must_output_die = 1;
19686 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19687 DIE for any block which contains no significant local declarations at
19688 all. Rather, in such cases we just call `decls_for_scope' so that any
19689 needed Dwarf info for any sub-blocks will get properly generated. Note
19690 that in terse mode, our definition of what constitutes a "significant"
19691 local declaration gets restricted to include only inlined function
19692 instances and local (nested) function definitions. */
19693 if (must_output_die)
19695 if (inlined_func)
19697 /* If STMT block is abstract, that means we have been called
19698 indirectly from dwarf2out_abstract_function.
19699 That function rightfully marks the descendent blocks (of
19700 the abstract function it is dealing with) as being abstract,
19701 precisely to prevent us from emitting any
19702 DW_TAG_inlined_subroutine DIE as a descendent
19703 of an abstract function instance. So in that case, we should
19704 not call gen_inlined_subroutine_die.
19706 Later though, when cgraph asks dwarf2out to emit info
19707 for the concrete instance of the function decl into which
19708 the concrete instance of STMT got inlined, the later will lead
19709 to the generation of a DW_TAG_inlined_subroutine DIE. */
19710 if (! BLOCK_ABSTRACT (stmt))
19711 gen_inlined_subroutine_die (stmt, context_die, depth);
19713 else
19714 gen_lexical_block_die (stmt, context_die, depth);
19716 else
19717 decls_for_scope (stmt, context_die, depth);
19720 /* Process variable DECL (or variable with origin ORIGIN) within
19721 block STMT and add it to CONTEXT_DIE. */
19722 static void
19723 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19725 dw_die_ref die;
19726 tree decl_or_origin = decl ? decl : origin;
19728 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19729 die = lookup_decl_die (decl_or_origin);
19730 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19731 && TYPE_DECL_IS_STUB (decl_or_origin))
19732 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19733 else
19734 die = NULL;
19736 if (die != NULL && die->die_parent == NULL)
19737 add_child_die (context_die, die);
19738 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19739 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19740 stmt, context_die);
19741 else
19742 gen_decl_die (decl, origin, context_die);
19745 /* Generate all of the decls declared within a given scope and (recursively)
19746 all of its sub-blocks. */
19748 static void
19749 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19751 tree decl;
19752 unsigned int i;
19753 tree subblocks;
19755 /* Ignore NULL blocks. */
19756 if (stmt == NULL_TREE)
19757 return;
19759 /* Output the DIEs to represent all of the data objects and typedefs
19760 declared directly within this block but not within any nested
19761 sub-blocks. Also, nested function and tag DIEs have been
19762 generated with a parent of NULL; fix that up now. */
19763 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19764 process_scope_var (stmt, decl, NULL_TREE, context_die);
19765 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19766 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19767 context_die);
19769 /* If we're at -g1, we're not interested in subblocks. */
19770 if (debug_info_level <= DINFO_LEVEL_TERSE)
19771 return;
19773 /* Output the DIEs to represent all sub-blocks (and the items declared
19774 therein) of this block. */
19775 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19776 subblocks != NULL;
19777 subblocks = BLOCK_CHAIN (subblocks))
19778 gen_block_die (subblocks, context_die, depth + 1);
19781 /* Is this a typedef we can avoid emitting? */
19783 static inline int
19784 is_redundant_typedef (const_tree decl)
19786 if (TYPE_DECL_IS_STUB (decl))
19787 return 1;
19789 if (DECL_ARTIFICIAL (decl)
19790 && DECL_CONTEXT (decl)
19791 && is_tagged_type (DECL_CONTEXT (decl))
19792 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19793 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19794 /* Also ignore the artificial member typedef for the class name. */
19795 return 1;
19797 return 0;
19800 /* Return TRUE if TYPE is a typedef that names a type for linkage
19801 purposes. This kind of typedefs is produced by the C++ FE for
19802 constructs like:
19804 typedef struct {...} foo;
19806 In that case, there is no typedef variant type produced for foo.
19807 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19808 struct type. */
19810 static bool
19811 is_naming_typedef_decl (const_tree decl)
19813 if (decl == NULL_TREE
19814 || TREE_CODE (decl) != TYPE_DECL
19815 || !is_tagged_type (TREE_TYPE (decl))
19816 || DECL_IS_BUILTIN (decl)
19817 || is_redundant_typedef (decl)
19818 /* It looks like Ada produces TYPE_DECLs that are very similar
19819 to C++ naming typedefs but that have different
19820 semantics. Let's be specific to c++ for now. */
19821 || !is_cxx ())
19822 return FALSE;
19824 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19825 && TYPE_NAME (TREE_TYPE (decl)) == decl
19826 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19827 != TYPE_NAME (TREE_TYPE (decl))));
19830 /* Returns the DIE for a context. */
19832 static inline dw_die_ref
19833 get_context_die (tree context)
19835 if (context)
19837 /* Find die that represents this context. */
19838 if (TYPE_P (context))
19840 context = TYPE_MAIN_VARIANT (context);
19841 return strip_naming_typedef (context, force_type_die (context));
19843 else
19844 return force_decl_die (context);
19846 return comp_unit_die ();
19849 /* Returns the DIE for decl. A DIE will always be returned. */
19851 static dw_die_ref
19852 force_decl_die (tree decl)
19854 dw_die_ref decl_die;
19855 unsigned saved_external_flag;
19856 tree save_fn = NULL_TREE;
19857 decl_die = lookup_decl_die (decl);
19858 if (!decl_die)
19860 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19862 decl_die = lookup_decl_die (decl);
19863 if (decl_die)
19864 return decl_die;
19866 switch (TREE_CODE (decl))
19868 case FUNCTION_DECL:
19869 /* Clear current_function_decl, so that gen_subprogram_die thinks
19870 that this is a declaration. At this point, we just want to force
19871 declaration die. */
19872 save_fn = current_function_decl;
19873 current_function_decl = NULL_TREE;
19874 gen_subprogram_die (decl, context_die);
19875 current_function_decl = save_fn;
19876 break;
19878 case VAR_DECL:
19879 /* Set external flag to force declaration die. Restore it after
19880 gen_decl_die() call. */
19881 saved_external_flag = DECL_EXTERNAL (decl);
19882 DECL_EXTERNAL (decl) = 1;
19883 gen_decl_die (decl, NULL, context_die);
19884 DECL_EXTERNAL (decl) = saved_external_flag;
19885 break;
19887 case NAMESPACE_DECL:
19888 if (dwarf_version >= 3 || !dwarf_strict)
19889 dwarf2out_decl (decl);
19890 else
19891 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19892 decl_die = comp_unit_die ();
19893 break;
19895 case TRANSLATION_UNIT_DECL:
19896 decl_die = comp_unit_die ();
19897 break;
19899 default:
19900 gcc_unreachable ();
19903 /* We should be able to find the DIE now. */
19904 if (!decl_die)
19905 decl_die = lookup_decl_die (decl);
19906 gcc_assert (decl_die);
19909 return decl_die;
19912 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19913 always returned. */
19915 static dw_die_ref
19916 force_type_die (tree type)
19918 dw_die_ref type_die;
19920 type_die = lookup_type_die (type);
19921 if (!type_die)
19923 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19925 type_die = modified_type_die (type, TYPE_READONLY (type),
19926 TYPE_VOLATILE (type), context_die);
19927 gcc_assert (type_die);
19929 return type_die;
19932 /* Force out any required namespaces to be able to output DECL,
19933 and return the new context_die for it, if it's changed. */
19935 static dw_die_ref
19936 setup_namespace_context (tree thing, dw_die_ref context_die)
19938 tree context = (DECL_P (thing)
19939 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19940 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19941 /* Force out the namespace. */
19942 context_die = force_decl_die (context);
19944 return context_die;
19947 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19948 type) within its namespace, if appropriate.
19950 For compatibility with older debuggers, namespace DIEs only contain
19951 declarations; all definitions are emitted at CU scope. */
19953 static dw_die_ref
19954 declare_in_namespace (tree thing, dw_die_ref context_die)
19956 dw_die_ref ns_context;
19958 if (debug_info_level <= DINFO_LEVEL_TERSE)
19959 return context_die;
19961 /* If this decl is from an inlined function, then don't try to emit it in its
19962 namespace, as we will get confused. It would have already been emitted
19963 when the abstract instance of the inline function was emitted anyways. */
19964 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19965 return context_die;
19967 ns_context = setup_namespace_context (thing, context_die);
19969 if (ns_context != context_die)
19971 if (is_fortran ())
19972 return ns_context;
19973 if (DECL_P (thing))
19974 gen_decl_die (thing, NULL, ns_context);
19975 else
19976 gen_type_die (thing, ns_context);
19978 return context_die;
19981 /* Generate a DIE for a namespace or namespace alias. */
19983 static void
19984 gen_namespace_die (tree decl, dw_die_ref context_die)
19986 dw_die_ref namespace_die;
19988 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19989 they are an alias of. */
19990 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19992 /* Output a real namespace or module. */
19993 context_die = setup_namespace_context (decl, comp_unit_die ());
19994 namespace_die = new_die (is_fortran ()
19995 ? DW_TAG_module : DW_TAG_namespace,
19996 context_die, decl);
19997 /* For Fortran modules defined in different CU don't add src coords. */
19998 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20000 const char *name = dwarf2_name (decl, 0);
20001 if (name)
20002 add_name_attribute (namespace_die, name);
20004 else
20005 add_name_and_src_coords_attributes (namespace_die, decl);
20006 if (DECL_EXTERNAL (decl))
20007 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20008 equate_decl_number_to_die (decl, namespace_die);
20010 else
20012 /* Output a namespace alias. */
20014 /* Force out the namespace we are an alias of, if necessary. */
20015 dw_die_ref origin_die
20016 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20018 if (DECL_FILE_SCOPE_P (decl)
20019 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20020 context_die = setup_namespace_context (decl, comp_unit_die ());
20021 /* Now create the namespace alias DIE. */
20022 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20023 add_name_and_src_coords_attributes (namespace_die, decl);
20024 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20025 equate_decl_number_to_die (decl, namespace_die);
20027 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20028 if (want_pubnames ())
20029 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20032 /* Generate Dwarf debug information for a decl described by DECL.
20033 The return value is currently only meaningful for PARM_DECLs,
20034 for all other decls it returns NULL. */
20036 static dw_die_ref
20037 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20039 tree decl_or_origin = decl ? decl : origin;
20040 tree class_origin = NULL, ultimate_origin;
20042 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20043 return NULL;
20045 switch (TREE_CODE (decl_or_origin))
20047 case ERROR_MARK:
20048 break;
20050 case CONST_DECL:
20051 if (!is_fortran () && !is_ada ())
20053 /* The individual enumerators of an enum type get output when we output
20054 the Dwarf representation of the relevant enum type itself. */
20055 break;
20058 /* Emit its type. */
20059 gen_type_die (TREE_TYPE (decl), context_die);
20061 /* And its containing namespace. */
20062 context_die = declare_in_namespace (decl, context_die);
20064 gen_const_die (decl, context_die);
20065 break;
20067 case FUNCTION_DECL:
20068 /* Don't output any DIEs to represent mere function declarations,
20069 unless they are class members or explicit block externs. */
20070 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20071 && DECL_FILE_SCOPE_P (decl_or_origin)
20072 && (current_function_decl == NULL_TREE
20073 || DECL_ARTIFICIAL (decl_or_origin)))
20074 break;
20076 #if 0
20077 /* FIXME */
20078 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20079 on local redeclarations of global functions. That seems broken. */
20080 if (current_function_decl != decl)
20081 /* This is only a declaration. */;
20082 #endif
20084 /* If we're emitting a clone, emit info for the abstract instance. */
20085 if (origin || DECL_ORIGIN (decl) != decl)
20086 dwarf2out_abstract_function (origin
20087 ? DECL_ORIGIN (origin)
20088 : DECL_ABSTRACT_ORIGIN (decl));
20090 /* If we're emitting an out-of-line copy of an inline function,
20091 emit info for the abstract instance and set up to refer to it. */
20092 else if (cgraph_function_possibly_inlined_p (decl)
20093 && ! DECL_ABSTRACT (decl)
20094 && ! class_or_namespace_scope_p (context_die)
20095 /* dwarf2out_abstract_function won't emit a die if this is just
20096 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20097 that case, because that works only if we have a die. */
20098 && DECL_INITIAL (decl) != NULL_TREE)
20100 dwarf2out_abstract_function (decl);
20101 set_decl_origin_self (decl);
20104 /* Otherwise we're emitting the primary DIE for this decl. */
20105 else if (debug_info_level > DINFO_LEVEL_TERSE)
20107 /* Before we describe the FUNCTION_DECL itself, make sure that we
20108 have its containing type. */
20109 if (!origin)
20110 origin = decl_class_context (decl);
20111 if (origin != NULL_TREE)
20112 gen_type_die (origin, context_die);
20114 /* And its return type. */
20115 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20117 /* And its virtual context. */
20118 if (DECL_VINDEX (decl) != NULL_TREE)
20119 gen_type_die (DECL_CONTEXT (decl), context_die);
20121 /* Make sure we have a member DIE for decl. */
20122 if (origin != NULL_TREE)
20123 gen_type_die_for_member (origin, decl, context_die);
20125 /* And its containing namespace. */
20126 context_die = declare_in_namespace (decl, context_die);
20129 /* Now output a DIE to represent the function itself. */
20130 if (decl)
20131 gen_subprogram_die (decl, context_die);
20132 break;
20134 case TYPE_DECL:
20135 /* If we are in terse mode, don't generate any DIEs to represent any
20136 actual typedefs. */
20137 if (debug_info_level <= DINFO_LEVEL_TERSE)
20138 break;
20140 /* In the special case of a TYPE_DECL node representing the declaration
20141 of some type tag, if the given TYPE_DECL is marked as having been
20142 instantiated from some other (original) TYPE_DECL node (e.g. one which
20143 was generated within the original definition of an inline function) we
20144 used to generate a special (abbreviated) DW_TAG_structure_type,
20145 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20146 should be actually referencing those DIEs, as variable DIEs with that
20147 type would be emitted already in the abstract origin, so it was always
20148 removed during unused type prunning. Don't add anything in this
20149 case. */
20150 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20151 break;
20153 if (is_redundant_typedef (decl))
20154 gen_type_die (TREE_TYPE (decl), context_die);
20155 else
20156 /* Output a DIE to represent the typedef itself. */
20157 gen_typedef_die (decl, context_die);
20158 break;
20160 case LABEL_DECL:
20161 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20162 gen_label_die (decl, context_die);
20163 break;
20165 case VAR_DECL:
20166 case RESULT_DECL:
20167 /* If we are in terse mode, don't generate any DIEs to represent any
20168 variable declarations or definitions. */
20169 if (debug_info_level <= DINFO_LEVEL_TERSE)
20170 break;
20172 /* Output any DIEs that are needed to specify the type of this data
20173 object. */
20174 if (decl_by_reference_p (decl_or_origin))
20175 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20176 else
20177 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20179 /* And its containing type. */
20180 class_origin = decl_class_context (decl_or_origin);
20181 if (class_origin != NULL_TREE)
20182 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20184 /* And its containing namespace. */
20185 context_die = declare_in_namespace (decl_or_origin, context_die);
20187 /* Now output the DIE to represent the data object itself. This gets
20188 complicated because of the possibility that the VAR_DECL really
20189 represents an inlined instance of a formal parameter for an inline
20190 function. */
20191 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20192 if (ultimate_origin != NULL_TREE
20193 && TREE_CODE (ultimate_origin) == PARM_DECL)
20194 gen_formal_parameter_die (decl, origin,
20195 true /* Emit name attribute. */,
20196 context_die);
20197 else
20198 gen_variable_die (decl, origin, context_die);
20199 break;
20201 case FIELD_DECL:
20202 /* Ignore the nameless fields that are used to skip bits but handle C++
20203 anonymous unions and structs. */
20204 if (DECL_NAME (decl) != NULL_TREE
20205 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20206 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20208 gen_type_die (member_declared_type (decl), context_die);
20209 gen_field_die (decl, context_die);
20211 break;
20213 case PARM_DECL:
20214 if (DECL_BY_REFERENCE (decl_or_origin))
20215 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20216 else
20217 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20218 return gen_formal_parameter_die (decl, origin,
20219 true /* Emit name attribute. */,
20220 context_die);
20222 case NAMESPACE_DECL:
20223 case IMPORTED_DECL:
20224 if (dwarf_version >= 3 || !dwarf_strict)
20225 gen_namespace_die (decl, context_die);
20226 break;
20228 default:
20229 /* Probably some frontend-internal decl. Assume we don't care. */
20230 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20231 break;
20234 return NULL;
20237 /* Output debug information for global decl DECL. Called from toplev.c after
20238 compilation proper has finished. */
20240 static void
20241 dwarf2out_global_decl (tree decl)
20243 /* Output DWARF2 information for file-scope tentative data object
20244 declarations, file-scope (extern) function declarations (which
20245 had no corresponding body) and file-scope tagged type declarations
20246 and definitions which have not yet been forced out. */
20247 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20248 dwarf2out_decl (decl);
20251 /* Output debug information for type decl DECL. Called from toplev.c
20252 and from language front ends (to record built-in types). */
20253 static void
20254 dwarf2out_type_decl (tree decl, int local)
20256 if (!local)
20257 dwarf2out_decl (decl);
20260 /* Output debug information for imported module or decl DECL.
20261 NAME is non-NULL name in the lexical block if the decl has been renamed.
20262 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20263 that DECL belongs to.
20264 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20265 static void
20266 dwarf2out_imported_module_or_decl_1 (tree decl,
20267 tree name,
20268 tree lexical_block,
20269 dw_die_ref lexical_block_die)
20271 expanded_location xloc;
20272 dw_die_ref imported_die = NULL;
20273 dw_die_ref at_import_die;
20275 if (TREE_CODE (decl) == IMPORTED_DECL)
20277 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20278 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20279 gcc_assert (decl);
20281 else
20282 xloc = expand_location (input_location);
20284 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20286 at_import_die = force_type_die (TREE_TYPE (decl));
20287 /* For namespace N { typedef void T; } using N::T; base_type_die
20288 returns NULL, but DW_TAG_imported_declaration requires
20289 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20290 if (!at_import_die)
20292 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20293 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20294 at_import_die = lookup_type_die (TREE_TYPE (decl));
20295 gcc_assert (at_import_die);
20298 else
20300 at_import_die = lookup_decl_die (decl);
20301 if (!at_import_die)
20303 /* If we're trying to avoid duplicate debug info, we may not have
20304 emitted the member decl for this field. Emit it now. */
20305 if (TREE_CODE (decl) == FIELD_DECL)
20307 tree type = DECL_CONTEXT (decl);
20309 if (TYPE_CONTEXT (type)
20310 && TYPE_P (TYPE_CONTEXT (type))
20311 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20312 DINFO_USAGE_DIR_USE))
20313 return;
20314 gen_type_die_for_member (type, decl,
20315 get_context_die (TYPE_CONTEXT (type)));
20317 at_import_die = force_decl_die (decl);
20321 if (TREE_CODE (decl) == NAMESPACE_DECL)
20323 if (dwarf_version >= 3 || !dwarf_strict)
20324 imported_die = new_die (DW_TAG_imported_module,
20325 lexical_block_die,
20326 lexical_block);
20327 else
20328 return;
20330 else
20331 imported_die = new_die (DW_TAG_imported_declaration,
20332 lexical_block_die,
20333 lexical_block);
20335 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20336 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20337 if (name)
20338 add_AT_string (imported_die, DW_AT_name,
20339 IDENTIFIER_POINTER (name));
20340 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20343 /* Output debug information for imported module or decl DECL.
20344 NAME is non-NULL name in context if the decl has been renamed.
20345 CHILD is true if decl is one of the renamed decls as part of
20346 importing whole module. */
20348 static void
20349 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20350 bool child)
20352 /* dw_die_ref at_import_die; */
20353 dw_die_ref scope_die;
20355 if (debug_info_level <= DINFO_LEVEL_TERSE)
20356 return;
20358 gcc_assert (decl);
20360 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20361 We need decl DIE for reference and scope die. First, get DIE for the decl
20362 itself. */
20364 /* Get the scope die for decl context. Use comp_unit_die for global module
20365 or decl. If die is not found for non globals, force new die. */
20366 if (context
20367 && TYPE_P (context)
20368 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20369 return;
20371 if (!(dwarf_version >= 3 || !dwarf_strict))
20372 return;
20374 scope_die = get_context_die (context);
20376 if (child)
20378 gcc_assert (scope_die->die_child);
20379 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20380 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20381 scope_die = scope_die->die_child;
20384 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20385 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20389 /* Write the debugging output for DECL. */
20391 void
20392 dwarf2out_decl (tree decl)
20394 dw_die_ref context_die = comp_unit_die ();
20396 switch (TREE_CODE (decl))
20398 case ERROR_MARK:
20399 return;
20401 case FUNCTION_DECL:
20402 /* What we would really like to do here is to filter out all mere
20403 file-scope declarations of file-scope functions which are never
20404 referenced later within this translation unit (and keep all of ones
20405 that *are* referenced later on) but we aren't clairvoyant, so we have
20406 no idea which functions will be referenced in the future (i.e. later
20407 on within the current translation unit). So here we just ignore all
20408 file-scope function declarations which are not also definitions. If
20409 and when the debugger needs to know something about these functions,
20410 it will have to hunt around and find the DWARF information associated
20411 with the definition of the function.
20413 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20414 nodes represent definitions and which ones represent mere
20415 declarations. We have to check DECL_INITIAL instead. That's because
20416 the C front-end supports some weird semantics for "extern inline"
20417 function definitions. These can get inlined within the current
20418 translation unit (and thus, we need to generate Dwarf info for their
20419 abstract instances so that the Dwarf info for the concrete inlined
20420 instances can have something to refer to) but the compiler never
20421 generates any out-of-lines instances of such things (despite the fact
20422 that they *are* definitions).
20424 The important point is that the C front-end marks these "extern
20425 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20426 them anyway. Note that the C++ front-end also plays some similar games
20427 for inline function definitions appearing within include files which
20428 also contain `#pragma interface' pragmas.
20430 If we are called from dwarf2out_abstract_function output a DIE
20431 anyway. We can end up here this way with early inlining and LTO
20432 where the inlined function is output in a different LTRANS unit
20433 or not at all. */
20434 if (DECL_INITIAL (decl) == NULL_TREE
20435 && ! DECL_ABSTRACT (decl))
20436 return;
20438 /* If we're a nested function, initially use a parent of NULL; if we're
20439 a plain function, this will be fixed up in decls_for_scope. If
20440 we're a method, it will be ignored, since we already have a DIE. */
20441 if (decl_function_context (decl)
20442 /* But if we're in terse mode, we don't care about scope. */
20443 && debug_info_level > DINFO_LEVEL_TERSE)
20444 context_die = NULL;
20445 break;
20447 case VAR_DECL:
20448 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20449 declaration and if the declaration was never even referenced from
20450 within this entire compilation unit. We suppress these DIEs in
20451 order to save space in the .debug section (by eliminating entries
20452 which are probably useless). Note that we must not suppress
20453 block-local extern declarations (whether used or not) because that
20454 would screw-up the debugger's name lookup mechanism and cause it to
20455 miss things which really ought to be in scope at a given point. */
20456 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20457 return;
20459 /* For local statics lookup proper context die. */
20460 if (TREE_STATIC (decl)
20461 && DECL_CONTEXT (decl)
20462 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20463 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20465 /* If we are in terse mode, don't generate any DIEs to represent any
20466 variable declarations or definitions. */
20467 if (debug_info_level <= DINFO_LEVEL_TERSE)
20468 return;
20469 break;
20471 case CONST_DECL:
20472 if (debug_info_level <= DINFO_LEVEL_TERSE)
20473 return;
20474 if (!is_fortran () && !is_ada ())
20475 return;
20476 if (TREE_STATIC (decl) && decl_function_context (decl))
20477 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20478 break;
20480 case NAMESPACE_DECL:
20481 case IMPORTED_DECL:
20482 if (debug_info_level <= DINFO_LEVEL_TERSE)
20483 return;
20484 if (lookup_decl_die (decl) != NULL)
20485 return;
20486 break;
20488 case TYPE_DECL:
20489 /* Don't emit stubs for types unless they are needed by other DIEs. */
20490 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20491 return;
20493 /* Don't bother trying to generate any DIEs to represent any of the
20494 normal built-in types for the language we are compiling. */
20495 if (DECL_IS_BUILTIN (decl))
20496 return;
20498 /* If we are in terse mode, don't generate any DIEs for types. */
20499 if (debug_info_level <= DINFO_LEVEL_TERSE)
20500 return;
20502 /* If we're a function-scope tag, initially use a parent of NULL;
20503 this will be fixed up in decls_for_scope. */
20504 if (decl_function_context (decl))
20505 context_die = NULL;
20507 break;
20509 default:
20510 return;
20513 gen_decl_die (decl, NULL, context_die);
20516 /* Write the debugging output for DECL. */
20518 static void
20519 dwarf2out_function_decl (tree decl)
20521 dwarf2out_decl (decl);
20522 call_arg_locations = NULL;
20523 call_arg_loc_last = NULL;
20524 call_site_count = -1;
20525 tail_call_site_count = -1;
20526 block_map.release ();
20527 htab_empty (decl_loc_table);
20528 htab_empty (cached_dw_loc_list_table);
20531 /* Output a marker (i.e. a label) for the beginning of the generated code for
20532 a lexical block. */
20534 static void
20535 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20536 unsigned int blocknum)
20538 switch_to_section (current_function_section ());
20539 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20542 /* Output a marker (i.e. a label) for the end of the generated code for a
20543 lexical block. */
20545 static void
20546 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20548 switch_to_section (current_function_section ());
20549 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20552 /* Returns nonzero if it is appropriate not to emit any debugging
20553 information for BLOCK, because it doesn't contain any instructions.
20555 Don't allow this for blocks with nested functions or local classes
20556 as we would end up with orphans, and in the presence of scheduling
20557 we may end up calling them anyway. */
20559 static bool
20560 dwarf2out_ignore_block (const_tree block)
20562 tree decl;
20563 unsigned int i;
20565 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20566 if (TREE_CODE (decl) == FUNCTION_DECL
20567 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20568 return 0;
20569 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20571 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20572 if (TREE_CODE (decl) == FUNCTION_DECL
20573 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20574 return 0;
20577 return 1;
20580 /* Hash table routines for file_hash. */
20582 static int
20583 file_table_eq (const void *p1_p, const void *p2_p)
20585 const struct dwarf_file_data *const p1 =
20586 (const struct dwarf_file_data *) p1_p;
20587 const char *const p2 = (const char *) p2_p;
20588 return filename_cmp (p1->filename, p2) == 0;
20591 static hashval_t
20592 file_table_hash (const void *p_p)
20594 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20595 return htab_hash_string (p->filename);
20598 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20599 dwarf2out.c) and return its "index". The index of each (known) filename is
20600 just a unique number which is associated with only that one filename. We
20601 need such numbers for the sake of generating labels (in the .debug_sfnames
20602 section) and references to those files numbers (in the .debug_srcinfo
20603 and.debug_macinfo sections). If the filename given as an argument is not
20604 found in our current list, add it to the list and assign it the next
20605 available unique index number. In order to speed up searches, we remember
20606 the index of the filename was looked up last. This handles the majority of
20607 all searches. */
20609 static struct dwarf_file_data *
20610 lookup_filename (const char *file_name)
20612 void ** slot;
20613 struct dwarf_file_data * created;
20615 /* Check to see if the file name that was searched on the previous
20616 call matches this file name. If so, return the index. */
20617 if (file_table_last_lookup
20618 && (file_name == file_table_last_lookup->filename
20619 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20620 return file_table_last_lookup;
20622 /* Didn't match the previous lookup, search the table. */
20623 slot = htab_find_slot_with_hash (file_table, file_name,
20624 htab_hash_string (file_name), INSERT);
20625 if (*slot)
20626 return (struct dwarf_file_data *) *slot;
20628 created = ggc_alloc_dwarf_file_data ();
20629 created->filename = file_name;
20630 created->emitted_number = 0;
20631 *slot = created;
20632 return created;
20635 /* If the assembler will construct the file table, then translate the compiler
20636 internal file table number into the assembler file table number, and emit
20637 a .file directive if we haven't already emitted one yet. The file table
20638 numbers are different because we prune debug info for unused variables and
20639 types, which may include filenames. */
20641 static int
20642 maybe_emit_file (struct dwarf_file_data * fd)
20644 if (! fd->emitted_number)
20646 if (last_emitted_file)
20647 fd->emitted_number = last_emitted_file->emitted_number + 1;
20648 else
20649 fd->emitted_number = 1;
20650 last_emitted_file = fd;
20652 if (DWARF2_ASM_LINE_DEBUG_INFO)
20654 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20655 output_quoted_string (asm_out_file,
20656 remap_debug_filename (fd->filename));
20657 fputc ('\n', asm_out_file);
20661 return fd->emitted_number;
20664 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20665 That generation should happen after function debug info has been
20666 generated. The value of the attribute is the constant value of ARG. */
20668 static void
20669 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20671 die_arg_entry entry;
20673 if (!die || !arg)
20674 return;
20676 if (!tmpl_value_parm_die_table)
20677 vec_alloc (tmpl_value_parm_die_table, 32);
20679 entry.die = die;
20680 entry.arg = arg;
20681 vec_safe_push (tmpl_value_parm_die_table, entry);
20684 /* Return TRUE if T is an instance of generic type, FALSE
20685 otherwise. */
20687 static bool
20688 generic_type_p (tree t)
20690 if (t == NULL_TREE || !TYPE_P (t))
20691 return false;
20692 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20695 /* Schedule the generation of the generic parameter dies for the
20696 instance of generic type T. The proper generation itself is later
20697 done by gen_scheduled_generic_parms_dies. */
20699 static void
20700 schedule_generic_params_dies_gen (tree t)
20702 if (!generic_type_p (t))
20703 return;
20705 if (!generic_type_instances)
20706 vec_alloc (generic_type_instances, 256);
20708 vec_safe_push (generic_type_instances, t);
20711 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20712 by append_entry_to_tmpl_value_parm_die_table. This function must
20713 be called after function DIEs have been generated. */
20715 static void
20716 gen_remaining_tmpl_value_param_die_attribute (void)
20718 if (tmpl_value_parm_die_table)
20720 unsigned i;
20721 die_arg_entry *e;
20723 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20724 tree_add_const_value_attribute (e->die, e->arg);
20728 /* Generate generic parameters DIEs for instances of generic types
20729 that have been previously scheduled by
20730 schedule_generic_params_dies_gen. This function must be called
20731 after all the types of the CU have been laid out. */
20733 static void
20734 gen_scheduled_generic_parms_dies (void)
20736 unsigned i;
20737 tree t;
20739 if (!generic_type_instances)
20740 return;
20742 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20743 if (COMPLETE_TYPE_P (t))
20744 gen_generic_params_dies (t);
20748 /* Replace DW_AT_name for the decl with name. */
20750 static void
20751 dwarf2out_set_name (tree decl, tree name)
20753 dw_die_ref die;
20754 dw_attr_ref attr;
20755 const char *dname;
20757 die = TYPE_SYMTAB_DIE (decl);
20758 if (!die)
20759 return;
20761 dname = dwarf2_name (name, 0);
20762 if (!dname)
20763 return;
20765 attr = get_AT (die, DW_AT_name);
20766 if (attr)
20768 struct indirect_string_node *node;
20770 node = find_AT_string (dname);
20771 /* replace the string. */
20772 attr->dw_attr_val.v.val_str = node;
20775 else
20776 add_name_attribute (die, dname);
20779 /* True if before or during processing of the first function being emitted. */
20780 static bool in_first_function_p = true;
20781 /* True if loc_note during dwarf2out_var_location call might still be
20782 before first real instruction at address equal to .Ltext0. */
20783 static bool maybe_at_text_label_p = true;
20784 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20785 static unsigned int first_loclabel_num_not_at_text_label;
20787 /* Called by the final INSN scan whenever we see a var location. We
20788 use it to drop labels in the right places, and throw the location in
20789 our lookup table. */
20791 static void
20792 dwarf2out_var_location (rtx loc_note)
20794 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20795 struct var_loc_node *newloc;
20796 rtx next_real, next_note;
20797 static const char *last_label;
20798 static const char *last_postcall_label;
20799 static bool last_in_cold_section_p;
20800 static rtx expected_next_loc_note;
20801 tree decl;
20802 bool var_loc_p;
20804 if (!NOTE_P (loc_note))
20806 if (CALL_P (loc_note))
20808 call_site_count++;
20809 if (SIBLING_CALL_P (loc_note))
20810 tail_call_site_count++;
20812 return;
20815 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20816 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20817 return;
20819 /* Optimize processing a large consecutive sequence of location
20820 notes so we don't spend too much time in next_real_insn. If the
20821 next insn is another location note, remember the next_real_insn
20822 calculation for next time. */
20823 next_real = cached_next_real_insn;
20824 if (next_real)
20826 if (expected_next_loc_note != loc_note)
20827 next_real = NULL_RTX;
20830 next_note = NEXT_INSN (loc_note);
20831 if (! next_note
20832 || INSN_DELETED_P (next_note)
20833 || ! NOTE_P (next_note)
20834 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20835 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20836 next_note = NULL_RTX;
20838 if (! next_real)
20839 next_real = next_real_insn (loc_note);
20841 if (next_note)
20843 expected_next_loc_note = next_note;
20844 cached_next_real_insn = next_real;
20846 else
20847 cached_next_real_insn = NULL_RTX;
20849 /* If there are no instructions which would be affected by this note,
20850 don't do anything. */
20851 if (var_loc_p
20852 && next_real == NULL_RTX
20853 && !NOTE_DURING_CALL_P (loc_note))
20854 return;
20856 if (next_real == NULL_RTX)
20857 next_real = get_last_insn ();
20859 /* If there were any real insns between note we processed last time
20860 and this note (or if it is the first note), clear
20861 last_{,postcall_}label so that they are not reused this time. */
20862 if (last_var_location_insn == NULL_RTX
20863 || last_var_location_insn != next_real
20864 || last_in_cold_section_p != in_cold_section_p)
20866 last_label = NULL;
20867 last_postcall_label = NULL;
20870 if (var_loc_p)
20872 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20873 newloc = add_var_loc_to_decl (decl, loc_note,
20874 NOTE_DURING_CALL_P (loc_note)
20875 ? last_postcall_label : last_label);
20876 if (newloc == NULL)
20877 return;
20879 else
20881 decl = NULL_TREE;
20882 newloc = NULL;
20885 /* If there were no real insns between note we processed last time
20886 and this note, use the label we emitted last time. Otherwise
20887 create a new label and emit it. */
20888 if (last_label == NULL)
20890 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20891 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20892 loclabel_num++;
20893 last_label = ggc_strdup (loclabel);
20894 /* See if loclabel might be equal to .Ltext0. If yes,
20895 bump first_loclabel_num_not_at_text_label. */
20896 if (!have_multiple_function_sections
20897 && in_first_function_p
20898 && maybe_at_text_label_p)
20900 static rtx last_start;
20901 rtx insn;
20902 for (insn = loc_note; insn; insn = previous_insn (insn))
20903 if (insn == last_start)
20904 break;
20905 else if (!NONDEBUG_INSN_P (insn))
20906 continue;
20907 else
20909 rtx body = PATTERN (insn);
20910 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
20911 continue;
20912 /* Inline asm could occupy zero bytes. */
20913 else if (GET_CODE (body) == ASM_INPUT
20914 || asm_noperands (body) >= 0)
20915 continue;
20916 #ifdef HAVE_attr_length
20917 else if (get_attr_min_length (insn) == 0)
20918 continue;
20919 #endif
20920 else
20922 /* Assume insn has non-zero length. */
20923 maybe_at_text_label_p = false;
20924 break;
20927 if (maybe_at_text_label_p)
20929 last_start = loc_note;
20930 first_loclabel_num_not_at_text_label = loclabel_num;
20935 if (!var_loc_p)
20937 struct call_arg_loc_node *ca_loc
20938 = ggc_alloc_cleared_call_arg_loc_node ();
20939 rtx prev = prev_real_insn (loc_note), x;
20940 ca_loc->call_arg_loc_note = loc_note;
20941 ca_loc->next = NULL;
20942 ca_loc->label = last_label;
20943 gcc_assert (prev
20944 && (CALL_P (prev)
20945 || (NONJUMP_INSN_P (prev)
20946 && GET_CODE (PATTERN (prev)) == SEQUENCE
20947 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20948 if (!CALL_P (prev))
20949 prev = XVECEXP (PATTERN (prev), 0, 0);
20950 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20951 x = get_call_rtx_from (PATTERN (prev));
20952 if (x)
20954 x = XEXP (XEXP (x, 0), 0);
20955 if (GET_CODE (x) == SYMBOL_REF
20956 && SYMBOL_REF_DECL (x)
20957 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20958 ca_loc->symbol_ref = x;
20960 ca_loc->block = insn_scope (prev);
20961 if (call_arg_locations)
20962 call_arg_loc_last->next = ca_loc;
20963 else
20964 call_arg_locations = ca_loc;
20965 call_arg_loc_last = ca_loc;
20967 else if (!NOTE_DURING_CALL_P (loc_note))
20968 newloc->label = last_label;
20969 else
20971 if (!last_postcall_label)
20973 sprintf (loclabel, "%s-1", last_label);
20974 last_postcall_label = ggc_strdup (loclabel);
20976 newloc->label = last_postcall_label;
20979 last_var_location_insn = next_real;
20980 last_in_cold_section_p = in_cold_section_p;
20983 /* Note in one location list that text section has changed. */
20985 static int
20986 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20988 var_loc_list *list = (var_loc_list *) *slot;
20989 if (list->first)
20990 list->last_before_switch
20991 = list->last->next ? list->last->next : list->last;
20992 return 1;
20995 /* Note in all location lists that text section has changed. */
20997 static void
20998 var_location_switch_text_section (void)
21000 if (decl_loc_table == NULL)
21001 return;
21003 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21006 /* Create a new line number table. */
21008 static dw_line_info_table *
21009 new_line_info_table (void)
21011 dw_line_info_table *table;
21013 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21014 table->file_num = 1;
21015 table->line_num = 1;
21016 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21018 return table;
21021 /* Lookup the "current" table into which we emit line info, so
21022 that we don't have to do it for every source line. */
21024 static void
21025 set_cur_line_info_table (section *sec)
21027 dw_line_info_table *table;
21029 if (sec == text_section)
21030 table = text_section_line_info;
21031 else if (sec == cold_text_section)
21033 table = cold_text_section_line_info;
21034 if (!table)
21036 cold_text_section_line_info = table = new_line_info_table ();
21037 table->end_label = cold_end_label;
21040 else
21042 const char *end_label;
21044 if (flag_reorder_blocks_and_partition)
21046 if (in_cold_section_p)
21047 end_label = crtl->subsections.cold_section_end_label;
21048 else
21049 end_label = crtl->subsections.hot_section_end_label;
21051 else
21053 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21054 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21055 current_function_funcdef_no);
21056 end_label = ggc_strdup (label);
21059 table = new_line_info_table ();
21060 table->end_label = end_label;
21062 vec_safe_push (separate_line_info, table);
21065 if (DWARF2_ASM_LINE_DEBUG_INFO)
21066 table->is_stmt = (cur_line_info_table
21067 ? cur_line_info_table->is_stmt
21068 : DWARF_LINE_DEFAULT_IS_STMT_START);
21069 cur_line_info_table = table;
21073 /* We need to reset the locations at the beginning of each
21074 function. We can't do this in the end_function hook, because the
21075 declarations that use the locations won't have been output when
21076 that hook is called. Also compute have_multiple_function_sections here. */
21078 static void
21079 dwarf2out_begin_function (tree fun)
21081 section *sec = function_section (fun);
21083 if (sec != text_section)
21084 have_multiple_function_sections = true;
21086 if (flag_reorder_blocks_and_partition && !cold_text_section)
21088 gcc_assert (current_function_decl == fun);
21089 cold_text_section = unlikely_text_section ();
21090 switch_to_section (cold_text_section);
21091 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21092 switch_to_section (sec);
21095 dwarf2out_note_section_used ();
21096 call_site_count = 0;
21097 tail_call_site_count = 0;
21099 set_cur_line_info_table (sec);
21102 /* Helper function of dwarf2out_end_function, called only after emitting
21103 the very first function into assembly. Check if some .debug_loc range
21104 might end with a .LVL* label that could be equal to .Ltext0.
21105 In that case we must force using absolute addresses in .debug_loc ranges,
21106 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21107 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21108 list terminator.
21109 Set have_multiple_function_sections to true in that case and
21110 terminate htab traversal. */
21112 static int
21113 find_empty_loc_ranges_at_text_label (void **slot, void *)
21115 var_loc_list *entry;
21116 struct var_loc_node *node;
21118 entry = (var_loc_list *) *slot;
21119 node = entry->first;
21120 if (node && node->next && node->next->label)
21122 unsigned int i;
21123 const char *label = node->next->label;
21124 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21126 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21128 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21129 if (strcmp (label, loclabel) == 0)
21131 have_multiple_function_sections = true;
21132 return 0;
21136 return 1;
21139 /* Hook called after emitting a function into assembly.
21140 This does something only for the very first function emitted. */
21142 static void
21143 dwarf2out_end_function (unsigned int)
21145 if (in_first_function_p
21146 && !have_multiple_function_sections
21147 && first_loclabel_num_not_at_text_label
21148 && decl_loc_table)
21149 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21150 NULL);
21151 in_first_function_p = false;
21152 maybe_at_text_label_p = false;
21155 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21157 static void
21158 push_dw_line_info_entry (dw_line_info_table *table,
21159 enum dw_line_info_opcode opcode, unsigned int val)
21161 dw_line_info_entry e;
21162 e.opcode = opcode;
21163 e.val = val;
21164 vec_safe_push (table->entries, e);
21167 /* Output a label to mark the beginning of a source code line entry
21168 and record information relating to this source line, in
21169 'line_info_table' for later output of the .debug_line section. */
21170 /* ??? The discriminator parameter ought to be unsigned. */
21172 static void
21173 dwarf2out_source_line (unsigned int line, const char *filename,
21174 int discriminator, bool is_stmt)
21176 unsigned int file_num;
21177 dw_line_info_table *table;
21179 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21180 return;
21182 /* The discriminator column was added in dwarf4. Simplify the below
21183 by simply removing it if we're not supposed to output it. */
21184 if (dwarf_version < 4 && dwarf_strict)
21185 discriminator = 0;
21187 table = cur_line_info_table;
21188 file_num = maybe_emit_file (lookup_filename (filename));
21190 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21191 the debugger has used the second (possibly duplicate) line number
21192 at the beginning of the function to mark the end of the prologue.
21193 We could eliminate any other duplicates within the function. For
21194 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21195 that second line number entry. */
21196 /* Recall that this end-of-prologue indication is *not* the same thing
21197 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21198 to which the hook corresponds, follows the last insn that was
21199 emitted by gen_prologue. What we need is to precede the first insn
21200 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21201 insn that corresponds to something the user wrote. These may be
21202 very different locations once scheduling is enabled. */
21204 if (0 && file_num == table->file_num
21205 && line == table->line_num
21206 && discriminator == table->discrim_num
21207 && is_stmt == table->is_stmt)
21208 return;
21210 switch_to_section (current_function_section ());
21212 /* If requested, emit something human-readable. */
21213 if (flag_debug_asm)
21214 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21216 if (DWARF2_ASM_LINE_DEBUG_INFO)
21218 /* Emit the .loc directive understood by GNU as. */
21219 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21220 file_num, line, is_stmt, discriminator */
21221 fputs ("\t.loc ", asm_out_file);
21222 fprint_ul (asm_out_file, file_num);
21223 putc (' ', asm_out_file);
21224 fprint_ul (asm_out_file, line);
21225 putc (' ', asm_out_file);
21226 putc ('0', asm_out_file);
21228 if (is_stmt != table->is_stmt)
21230 fputs (" is_stmt ", asm_out_file);
21231 putc (is_stmt ? '1' : '0', asm_out_file);
21233 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21235 gcc_assert (discriminator > 0);
21236 fputs (" discriminator ", asm_out_file);
21237 fprint_ul (asm_out_file, (unsigned long) discriminator);
21239 putc ('\n', asm_out_file);
21241 else
21243 unsigned int label_num = ++line_info_label_num;
21245 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21247 push_dw_line_info_entry (table, LI_set_address, label_num);
21248 if (file_num != table->file_num)
21249 push_dw_line_info_entry (table, LI_set_file, file_num);
21250 if (discriminator != table->discrim_num)
21251 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21252 if (is_stmt != table->is_stmt)
21253 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21254 push_dw_line_info_entry (table, LI_set_line, line);
21257 table->file_num = file_num;
21258 table->line_num = line;
21259 table->discrim_num = discriminator;
21260 table->is_stmt = is_stmt;
21261 table->in_use = true;
21264 /* Record the beginning of a new source file. */
21266 static void
21267 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21269 if (flag_eliminate_dwarf2_dups)
21271 /* Record the beginning of the file for break_out_includes. */
21272 dw_die_ref bincl_die;
21274 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21275 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21278 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21280 macinfo_entry e;
21281 e.code = DW_MACINFO_start_file;
21282 e.lineno = lineno;
21283 e.info = ggc_strdup (filename);
21284 vec_safe_push (macinfo_table, e);
21288 /* Record the end of a source file. */
21290 static void
21291 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21293 if (flag_eliminate_dwarf2_dups)
21294 /* Record the end of the file for break_out_includes. */
21295 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21297 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21299 macinfo_entry e;
21300 e.code = DW_MACINFO_end_file;
21301 e.lineno = lineno;
21302 e.info = NULL;
21303 vec_safe_push (macinfo_table, e);
21307 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21308 the tail part of the directive line, i.e. the part which is past the
21309 initial whitespace, #, whitespace, directive-name, whitespace part. */
21311 static void
21312 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21313 const char *buffer ATTRIBUTE_UNUSED)
21315 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21317 macinfo_entry e;
21318 /* Insert a dummy first entry to be able to optimize the whole
21319 predefined macro block using DW_MACRO_GNU_transparent_include. */
21320 if (macinfo_table->is_empty () && lineno <= 1)
21322 e.code = 0;
21323 e.lineno = 0;
21324 e.info = NULL;
21325 vec_safe_push (macinfo_table, e);
21327 e.code = DW_MACINFO_define;
21328 e.lineno = lineno;
21329 e.info = ggc_strdup (buffer);
21330 vec_safe_push (macinfo_table, e);
21334 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21335 the tail part of the directive line, i.e. the part which is past the
21336 initial whitespace, #, whitespace, directive-name, whitespace part. */
21338 static void
21339 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21340 const char *buffer ATTRIBUTE_UNUSED)
21342 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21344 macinfo_entry e;
21345 /* Insert a dummy first entry to be able to optimize the whole
21346 predefined macro block using DW_MACRO_GNU_transparent_include. */
21347 if (macinfo_table->is_empty () && lineno <= 1)
21349 e.code = 0;
21350 e.lineno = 0;
21351 e.info = NULL;
21352 vec_safe_push (macinfo_table, e);
21354 e.code = DW_MACINFO_undef;
21355 e.lineno = lineno;
21356 e.info = ggc_strdup (buffer);
21357 vec_safe_push (macinfo_table, e);
21361 /* Helpers to manipulate hash table of CUs. */
21363 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21365 typedef macinfo_entry value_type;
21366 typedef macinfo_entry compare_type;
21367 static inline hashval_t hash (const value_type *);
21368 static inline bool equal (const value_type *, const compare_type *);
21371 inline hashval_t
21372 macinfo_entry_hasher::hash (const value_type *entry)
21374 return htab_hash_string (entry->info);
21377 inline bool
21378 macinfo_entry_hasher::equal (const value_type *entry1,
21379 const compare_type *entry2)
21381 return !strcmp (entry1->info, entry2->info);
21384 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21386 /* Output a single .debug_macinfo entry. */
21388 static void
21389 output_macinfo_op (macinfo_entry *ref)
21391 int file_num;
21392 size_t len;
21393 struct indirect_string_node *node;
21394 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21395 struct dwarf_file_data *fd;
21397 switch (ref->code)
21399 case DW_MACINFO_start_file:
21400 fd = lookup_filename (ref->info);
21401 file_num = maybe_emit_file (fd);
21402 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21403 dw2_asm_output_data_uleb128 (ref->lineno,
21404 "Included from line number %lu",
21405 (unsigned long) ref->lineno);
21406 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21407 break;
21408 case DW_MACINFO_end_file:
21409 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21410 break;
21411 case DW_MACINFO_define:
21412 case DW_MACINFO_undef:
21413 len = strlen (ref->info) + 1;
21414 if (!dwarf_strict
21415 && len > DWARF_OFFSET_SIZE
21416 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21417 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21419 ref->code = ref->code == DW_MACINFO_define
21420 ? DW_MACRO_GNU_define_indirect
21421 : DW_MACRO_GNU_undef_indirect;
21422 output_macinfo_op (ref);
21423 return;
21425 dw2_asm_output_data (1, ref->code,
21426 ref->code == DW_MACINFO_define
21427 ? "Define macro" : "Undefine macro");
21428 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21429 (unsigned long) ref->lineno);
21430 dw2_asm_output_nstring (ref->info, -1, "The macro");
21431 break;
21432 case DW_MACRO_GNU_define_indirect:
21433 case DW_MACRO_GNU_undef_indirect:
21434 node = find_AT_string (ref->info);
21435 gcc_assert (node
21436 && ((node->form == DW_FORM_strp)
21437 || (node->form == DW_FORM_GNU_str_index)));
21438 dw2_asm_output_data (1, ref->code,
21439 ref->code == DW_MACRO_GNU_define_indirect
21440 ? "Define macro indirect"
21441 : "Undefine macro indirect");
21442 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21443 (unsigned long) ref->lineno);
21444 if (node->form == DW_FORM_strp)
21445 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21446 debug_str_section, "The macro: \"%s\"",
21447 ref->info);
21448 else
21449 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21450 ref->info);
21451 break;
21452 case DW_MACRO_GNU_transparent_include:
21453 dw2_asm_output_data (1, ref->code, "Transparent include");
21454 ASM_GENERATE_INTERNAL_LABEL (label,
21455 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21456 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21457 break;
21458 default:
21459 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21460 ASM_COMMENT_START, (unsigned long) ref->code);
21461 break;
21465 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21466 other compilation unit .debug_macinfo sections. IDX is the first
21467 index of a define/undef, return the number of ops that should be
21468 emitted in a comdat .debug_macinfo section and emit
21469 a DW_MACRO_GNU_transparent_include entry referencing it.
21470 If the define/undef entry should be emitted normally, return 0. */
21472 static unsigned
21473 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21474 macinfo_hash_type *macinfo_htab)
21476 macinfo_entry *first, *second, *cur, *inc;
21477 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21478 unsigned char checksum[16];
21479 struct md5_ctx ctx;
21480 char *grp_name, *tail;
21481 const char *base;
21482 unsigned int i, count, encoded_filename_len, linebuf_len;
21483 macinfo_entry **slot;
21485 first = &(*macinfo_table)[idx];
21486 second = &(*macinfo_table)[idx + 1];
21488 /* Optimize only if there are at least two consecutive define/undef ops,
21489 and either all of them are before first DW_MACINFO_start_file
21490 with lineno {0,1} (i.e. predefined macro block), or all of them are
21491 in some included header file. */
21492 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21493 return 0;
21494 if (vec_safe_is_empty (files))
21496 if (first->lineno > 1 || second->lineno > 1)
21497 return 0;
21499 else if (first->lineno == 0)
21500 return 0;
21502 /* Find the last define/undef entry that can be grouped together
21503 with first and at the same time compute md5 checksum of their
21504 codes, linenumbers and strings. */
21505 md5_init_ctx (&ctx);
21506 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21507 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21508 break;
21509 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21510 break;
21511 else
21513 unsigned char code = cur->code;
21514 md5_process_bytes (&code, 1, &ctx);
21515 checksum_uleb128 (cur->lineno, &ctx);
21516 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21518 md5_finish_ctx (&ctx, checksum);
21519 count = i - idx;
21521 /* From the containing include filename (if any) pick up just
21522 usable characters from its basename. */
21523 if (vec_safe_is_empty (files))
21524 base = "";
21525 else
21526 base = lbasename (files->last ().info);
21527 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21528 if (ISIDNUM (base[i]) || base[i] == '.')
21529 encoded_filename_len++;
21530 /* Count . at the end. */
21531 if (encoded_filename_len)
21532 encoded_filename_len++;
21534 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21535 linebuf_len = strlen (linebuf);
21537 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21538 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21539 + 16 * 2 + 1);
21540 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21541 tail = grp_name + 4;
21542 if (encoded_filename_len)
21544 for (i = 0; base[i]; i++)
21545 if (ISIDNUM (base[i]) || base[i] == '.')
21546 *tail++ = base[i];
21547 *tail++ = '.';
21549 memcpy (tail, linebuf, linebuf_len);
21550 tail += linebuf_len;
21551 *tail++ = '.';
21552 for (i = 0; i < 16; i++)
21553 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21555 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21556 in the empty vector entry before the first define/undef. */
21557 inc = &(*macinfo_table)[idx - 1];
21558 inc->code = DW_MACRO_GNU_transparent_include;
21559 inc->lineno = 0;
21560 inc->info = ggc_strdup (grp_name);
21561 if (!macinfo_htab->is_created ())
21562 macinfo_htab->create (10);
21563 /* Avoid emitting duplicates. */
21564 slot = macinfo_htab->find_slot (inc, INSERT);
21565 if (*slot != NULL)
21567 inc->code = 0;
21568 inc->info = NULL;
21569 /* If such an entry has been used before, just emit
21570 a DW_MACRO_GNU_transparent_include op. */
21571 inc = *slot;
21572 output_macinfo_op (inc);
21573 /* And clear all macinfo_entry in the range to avoid emitting them
21574 in the second pass. */
21575 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21577 cur->code = 0;
21578 cur->info = NULL;
21581 else
21583 *slot = inc;
21584 inc->lineno = macinfo_htab->elements ();
21585 output_macinfo_op (inc);
21587 return count;
21590 /* Save any strings needed by the macinfo table in the debug str
21591 table. All strings must be collected into the table by the time
21592 index_string is called. */
21594 static void
21595 save_macinfo_strings (void)
21597 unsigned len;
21598 unsigned i;
21599 macinfo_entry *ref;
21601 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21603 switch (ref->code)
21605 /* Match the logic in output_macinfo_op to decide on
21606 indirect strings. */
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)
21614 set_indirect_string (find_AT_string (ref->info));
21615 break;
21616 case DW_MACRO_GNU_define_indirect:
21617 case DW_MACRO_GNU_undef_indirect:
21618 set_indirect_string (find_AT_string (ref->info));
21619 break;
21620 default:
21621 break;
21626 /* Output macinfo section(s). */
21628 static void
21629 output_macinfo (void)
21631 unsigned i;
21632 unsigned long length = vec_safe_length (macinfo_table);
21633 macinfo_entry *ref;
21634 vec<macinfo_entry, va_gc> *files = NULL;
21635 macinfo_hash_type macinfo_htab;
21637 if (! length)
21638 return;
21640 /* output_macinfo* uses these interchangeably. */
21641 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21642 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21643 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21644 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21646 /* For .debug_macro emit the section header. */
21647 if (!dwarf_strict)
21649 dw2_asm_output_data (2, 4, "DWARF macro version number");
21650 if (DWARF_OFFSET_SIZE == 8)
21651 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21652 else
21653 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21654 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21655 (!dwarf_split_debug_info ? debug_line_section_label
21656 : debug_skeleton_line_section_label),
21657 debug_line_section, NULL);
21660 /* In the first loop, it emits the primary .debug_macinfo section
21661 and after each emitted op the macinfo_entry is cleared.
21662 If a longer range of define/undef ops can be optimized using
21663 DW_MACRO_GNU_transparent_include, the
21664 DW_MACRO_GNU_transparent_include op is emitted and kept in
21665 the vector before the first define/undef in the range and the
21666 whole range of define/undef ops is not emitted and kept. */
21667 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21669 switch (ref->code)
21671 case DW_MACINFO_start_file:
21672 vec_safe_push (files, *ref);
21673 break;
21674 case DW_MACINFO_end_file:
21675 if (!vec_safe_is_empty (files))
21676 files->pop ();
21677 break;
21678 case DW_MACINFO_define:
21679 case DW_MACINFO_undef:
21680 if (!dwarf_strict
21681 && HAVE_COMDAT_GROUP
21682 && vec_safe_length (files) != 1
21683 && i > 0
21684 && i + 1 < length
21685 && (*macinfo_table)[i - 1].code == 0)
21687 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21688 if (count)
21690 i += count - 1;
21691 continue;
21694 break;
21695 case 0:
21696 /* A dummy entry may be inserted at the beginning to be able
21697 to optimize the whole block of predefined macros. */
21698 if (i == 0)
21699 continue;
21700 default:
21701 break;
21703 output_macinfo_op (ref);
21704 ref->info = NULL;
21705 ref->code = 0;
21708 if (!macinfo_htab.is_created ())
21709 return;
21711 macinfo_htab.dispose ();
21713 /* If any DW_MACRO_GNU_transparent_include were used, on those
21714 DW_MACRO_GNU_transparent_include entries terminate the
21715 current chain and switch to a new comdat .debug_macinfo
21716 section and emit the define/undef entries within it. */
21717 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21718 switch (ref->code)
21720 case 0:
21721 continue;
21722 case DW_MACRO_GNU_transparent_include:
21724 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21725 tree comdat_key = get_identifier (ref->info);
21726 /* Terminate the previous .debug_macinfo section. */
21727 dw2_asm_output_data (1, 0, "End compilation unit");
21728 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21729 SECTION_DEBUG
21730 | SECTION_LINKONCE,
21731 comdat_key);
21732 ASM_GENERATE_INTERNAL_LABEL (label,
21733 DEBUG_MACRO_SECTION_LABEL,
21734 ref->lineno);
21735 ASM_OUTPUT_LABEL (asm_out_file, label);
21736 ref->code = 0;
21737 ref->info = NULL;
21738 dw2_asm_output_data (2, 4, "DWARF macro version number");
21739 if (DWARF_OFFSET_SIZE == 8)
21740 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21741 else
21742 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21744 break;
21745 case DW_MACINFO_define:
21746 case DW_MACINFO_undef:
21747 output_macinfo_op (ref);
21748 ref->code = 0;
21749 ref->info = NULL;
21750 break;
21751 default:
21752 gcc_unreachable ();
21756 /* Set up for Dwarf output at the start of compilation. */
21758 static void
21759 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21761 /* Allocate the file_table. */
21762 file_table = htab_create_ggc (50, file_table_hash,
21763 file_table_eq, NULL);
21765 /* Allocate the decl_die_table. */
21766 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21767 decl_die_table_eq, NULL);
21769 /* Allocate the decl_loc_table. */
21770 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21771 decl_loc_table_eq, NULL);
21773 /* Allocate the cached_dw_loc_list_table. */
21774 cached_dw_loc_list_table
21775 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21776 cached_dw_loc_list_table_eq, NULL);
21778 /* Allocate the initial hunk of the decl_scope_table. */
21779 vec_alloc (decl_scope_table, 256);
21781 /* Allocate the initial hunk of the abbrev_die_table. */
21782 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21783 (ABBREV_DIE_TABLE_INCREMENT);
21784 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21785 /* Zero-th entry is allocated, but unused. */
21786 abbrev_die_table_in_use = 1;
21788 /* Allocate the pubtypes and pubnames vectors. */
21789 vec_alloc (pubname_table, 32);
21790 vec_alloc (pubtype_table, 32);
21792 vec_alloc (incomplete_types, 64);
21794 vec_alloc (used_rtx_array, 32);
21796 if (!dwarf_split_debug_info)
21798 debug_info_section = get_section (DEBUG_INFO_SECTION,
21799 SECTION_DEBUG, NULL);
21800 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21801 SECTION_DEBUG, NULL);
21802 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21803 SECTION_DEBUG, NULL);
21805 else
21807 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21808 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21809 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21810 SECTION_DEBUG | SECTION_EXCLUDE,
21811 NULL);
21812 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21813 SECTION_DEBUG, NULL);
21814 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21815 SECTION_DEBUG, NULL);
21816 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21817 SECTION_DEBUG, NULL);
21818 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21819 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21821 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21822 the main .o, but the skeleton_line goes into the split off dwo. */
21823 debug_skeleton_line_section
21824 = get_section (DEBUG_DWO_LINE_SECTION,
21825 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21826 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21827 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21828 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21829 SECTION_DEBUG | SECTION_EXCLUDE,
21830 NULL);
21831 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21832 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21833 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21834 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21835 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
21836 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
21838 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21839 SECTION_DEBUG, NULL);
21840 debug_macinfo_section = get_section (dwarf_strict
21841 ? DEBUG_MACINFO_SECTION
21842 : DEBUG_MACRO_SECTION,
21843 DEBUG_MACRO_SECTION_FLAGS, NULL);
21844 debug_line_section = get_section (DEBUG_LINE_SECTION,
21845 SECTION_DEBUG, NULL);
21846 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21847 SECTION_DEBUG, NULL);
21848 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21849 SECTION_DEBUG, NULL);
21850 debug_str_section = get_section (DEBUG_STR_SECTION,
21851 DEBUG_STR_SECTION_FLAGS, NULL);
21852 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21853 SECTION_DEBUG, NULL);
21854 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21855 SECTION_DEBUG, NULL);
21857 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21858 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21859 DEBUG_ABBREV_SECTION_LABEL, 0);
21860 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21861 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21862 COLD_TEXT_SECTION_LABEL, 0);
21863 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21865 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21866 DEBUG_INFO_SECTION_LABEL, 0);
21867 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21868 DEBUG_LINE_SECTION_LABEL, 0);
21869 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21870 DEBUG_RANGES_SECTION_LABEL, 0);
21871 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21872 DEBUG_ADDR_SECTION_LABEL, 0);
21873 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21874 dwarf_strict
21875 ? DEBUG_MACINFO_SECTION_LABEL
21876 : DEBUG_MACRO_SECTION_LABEL, 0);
21877 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21879 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21880 vec_alloc (macinfo_table, 64);
21882 switch_to_section (text_section);
21883 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21885 /* Make sure the line number table for .text always exists. */
21886 text_section_line_info = new_line_info_table ();
21887 text_section_line_info->end_label = text_end_label;
21890 /* Called before compile () starts outputtting functions, variables
21891 and toplevel asms into assembly. */
21893 static void
21894 dwarf2out_assembly_start (void)
21896 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21897 && dwarf2out_do_cfi_asm ()
21898 && (!(flag_unwind_tables || flag_exceptions)
21899 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21900 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21903 /* A helper function for dwarf2out_finish called through
21904 htab_traverse. Assign a string its index. All strings must be
21905 collected into the table by the time index_string is called,
21906 because the indexing code relies on htab_traverse to traverse nodes
21907 in the same order for each run. */
21909 static int
21910 index_string (void **h, void *v)
21912 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21913 unsigned int *index = (unsigned int *) v;
21915 find_string_form (node);
21916 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21918 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21919 node->index = *index;
21920 *index += 1;
21922 return 1;
21925 /* A helper function for output_indirect_strings called through
21926 htab_traverse. Output the offset to a string and update the
21927 current offset. */
21929 static int
21930 output_index_string_offset (void **h, void *v)
21932 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21933 unsigned int *offset = (unsigned int *) v;
21935 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21937 /* Assert that this node has been assigned an index. */
21938 gcc_assert (node->index != NO_INDEX_ASSIGNED
21939 && node->index != NOT_INDEXED);
21940 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21941 "indexed string 0x%x: %s", node->index, node->str);
21942 *offset += strlen (node->str) + 1;
21944 return 1;
21947 /* A helper function for dwarf2out_finish called through
21948 htab_traverse. Output the indexed string. */
21950 static int
21951 output_index_string (void **h, void *v)
21953 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21954 unsigned int *cur_idx = (unsigned int *) v;
21956 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21958 /* Assert that the strings are output in the same order as their
21959 indexes were assigned. */
21960 gcc_assert (*cur_idx == node->index);
21961 assemble_string (node->str, strlen (node->str) + 1);
21962 *cur_idx += 1;
21964 return 1;
21967 /* A helper function for dwarf2out_finish called through
21968 htab_traverse. Emit one queued .debug_str string. */
21970 static int
21971 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21973 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21975 node->form = find_string_form (node);
21976 if (node->form == DW_FORM_strp && node->refcount > 0)
21978 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21979 assemble_string (node->str, strlen (node->str) + 1);
21982 return 1;
21985 /* Output the indexed string table. */
21987 static void
21988 output_indirect_strings (void)
21990 switch_to_section (debug_str_section);
21991 if (!dwarf_split_debug_info)
21992 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21993 else
21995 unsigned int offset = 0;
21996 unsigned int cur_idx = 0;
21998 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22000 switch_to_section (debug_str_offsets_section);
22001 htab_traverse_noresize (debug_str_hash,
22002 output_index_string_offset,
22003 &offset);
22004 switch_to_section (debug_str_dwo_section);
22005 htab_traverse_noresize (debug_str_hash,
22006 output_index_string,
22007 &cur_idx);
22011 /* Callback for htab_traverse to assign an index to an entry in the
22012 table, and to write that entry to the .debug_addr section. */
22014 static int
22015 output_addr_table_entry (void **slot, void *data)
22017 addr_table_entry *entry = (addr_table_entry *) *slot;
22018 unsigned int *cur_index = (unsigned int *)data;
22020 if (entry->refcount == 0)
22022 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22023 || entry->index == NOT_INDEXED);
22024 return 1;
22027 gcc_assert (entry->index == *cur_index);
22028 (*cur_index)++;
22030 switch (entry->kind)
22032 case ate_kind_rtx:
22033 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22034 "0x%x", entry->index);
22035 break;
22036 case ate_kind_rtx_dtprel:
22037 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22038 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22039 DWARF2_ADDR_SIZE,
22040 entry->addr.rtl);
22041 fputc ('\n', asm_out_file);
22042 break;
22043 case ate_kind_label:
22044 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22045 "0x%x", entry->index);
22046 break;
22047 default:
22048 gcc_unreachable ();
22050 return 1;
22053 /* Produce the .debug_addr section. */
22055 static void
22056 output_addr_table (void)
22058 unsigned int index = 0;
22059 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22060 return;
22062 switch_to_section (debug_addr_section);
22063 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22066 #if ENABLE_ASSERT_CHECKING
22067 /* Verify that all marks are clear. */
22069 static void
22070 verify_marks_clear (dw_die_ref die)
22072 dw_die_ref c;
22074 gcc_assert (! die->die_mark);
22075 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22077 #endif /* ENABLE_ASSERT_CHECKING */
22079 /* Clear the marks for a die and its children.
22080 Be cool if the mark isn't set. */
22082 static void
22083 prune_unmark_dies (dw_die_ref die)
22085 dw_die_ref c;
22087 if (die->die_mark)
22088 die->die_mark = 0;
22089 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22092 /* Given DIE that we're marking as used, find any other dies
22093 it references as attributes and mark them as used. */
22095 static void
22096 prune_unused_types_walk_attribs (dw_die_ref die)
22098 dw_attr_ref a;
22099 unsigned ix;
22101 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22103 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22105 /* A reference to another DIE.
22106 Make sure that it will get emitted.
22107 If it was broken out into a comdat group, don't follow it. */
22108 if (! AT_ref (a)->comdat_type_p
22109 || a->dw_attr == DW_AT_specification)
22110 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22112 /* Set the string's refcount to 0 so that prune_unused_types_mark
22113 accounts properly for it. */
22114 if (AT_class (a) == dw_val_class_str)
22115 a->dw_attr_val.v.val_str->refcount = 0;
22119 /* Mark the generic parameters and arguments children DIEs of DIE. */
22121 static void
22122 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22124 dw_die_ref c;
22126 if (die == NULL || die->die_child == NULL)
22127 return;
22128 c = die->die_child;
22131 switch (c->die_tag)
22133 case DW_TAG_template_type_param:
22134 case DW_TAG_template_value_param:
22135 case DW_TAG_GNU_template_template_param:
22136 case DW_TAG_GNU_template_parameter_pack:
22137 prune_unused_types_mark (c, 1);
22138 break;
22139 default:
22140 break;
22142 c = c->die_sib;
22143 } while (c && c != die->die_child);
22146 /* Mark DIE as being used. If DOKIDS is true, then walk down
22147 to DIE's children. */
22149 static void
22150 prune_unused_types_mark (dw_die_ref die, int dokids)
22152 dw_die_ref c;
22154 if (die->die_mark == 0)
22156 /* We haven't done this node yet. Mark it as used. */
22157 die->die_mark = 1;
22158 /* If this is the DIE of a generic type instantiation,
22159 mark the children DIEs that describe its generic parms and
22160 args. */
22161 prune_unused_types_mark_generic_parms_dies (die);
22163 /* We also have to mark its parents as used.
22164 (But we don't want to mark our parent's kids due to this,
22165 unless it is a class.) */
22166 if (die->die_parent)
22167 prune_unused_types_mark (die->die_parent,
22168 class_scope_p (die->die_parent));
22170 /* Mark any referenced nodes. */
22171 prune_unused_types_walk_attribs (die);
22173 /* If this node is a specification,
22174 also mark the definition, if it exists. */
22175 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22176 prune_unused_types_mark (die->die_definition, 1);
22179 if (dokids && die->die_mark != 2)
22181 /* We need to walk the children, but haven't done so yet.
22182 Remember that we've walked the kids. */
22183 die->die_mark = 2;
22185 /* If this is an array type, we need to make sure our
22186 kids get marked, even if they're types. If we're
22187 breaking out types into comdat sections, do this
22188 for all type definitions. */
22189 if (die->die_tag == DW_TAG_array_type
22190 || (use_debug_types
22191 && is_type_die (die) && ! is_declaration_die (die)))
22192 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22193 else
22194 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22198 /* For local classes, look if any static member functions were emitted
22199 and if so, mark them. */
22201 static void
22202 prune_unused_types_walk_local_classes (dw_die_ref die)
22204 dw_die_ref c;
22206 if (die->die_mark == 2)
22207 return;
22209 switch (die->die_tag)
22211 case DW_TAG_structure_type:
22212 case DW_TAG_union_type:
22213 case DW_TAG_class_type:
22214 break;
22216 case DW_TAG_subprogram:
22217 if (!get_AT_flag (die, DW_AT_declaration)
22218 || die->die_definition != NULL)
22219 prune_unused_types_mark (die, 1);
22220 return;
22222 default:
22223 return;
22226 /* Mark children. */
22227 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22230 /* Walk the tree DIE and mark types that we actually use. */
22232 static void
22233 prune_unused_types_walk (dw_die_ref die)
22235 dw_die_ref c;
22237 /* Don't do anything if this node is already marked and
22238 children have been marked as well. */
22239 if (die->die_mark == 2)
22240 return;
22242 switch (die->die_tag)
22244 case DW_TAG_structure_type:
22245 case DW_TAG_union_type:
22246 case DW_TAG_class_type:
22247 if (die->die_perennial_p)
22248 break;
22250 for (c = die->die_parent; c; c = c->die_parent)
22251 if (c->die_tag == DW_TAG_subprogram)
22252 break;
22254 /* Finding used static member functions inside of classes
22255 is needed just for local classes, because for other classes
22256 static member function DIEs with DW_AT_specification
22257 are emitted outside of the DW_TAG_*_type. If we ever change
22258 it, we'd need to call this even for non-local classes. */
22259 if (c)
22260 prune_unused_types_walk_local_classes (die);
22262 /* It's a type node --- don't mark it. */
22263 return;
22265 case DW_TAG_const_type:
22266 case DW_TAG_packed_type:
22267 case DW_TAG_pointer_type:
22268 case DW_TAG_reference_type:
22269 case DW_TAG_rvalue_reference_type:
22270 case DW_TAG_volatile_type:
22271 case DW_TAG_typedef:
22272 case DW_TAG_array_type:
22273 case DW_TAG_interface_type:
22274 case DW_TAG_friend:
22275 case DW_TAG_variant_part:
22276 case DW_TAG_enumeration_type:
22277 case DW_TAG_subroutine_type:
22278 case DW_TAG_string_type:
22279 case DW_TAG_set_type:
22280 case DW_TAG_subrange_type:
22281 case DW_TAG_ptr_to_member_type:
22282 case DW_TAG_file_type:
22283 if (die->die_perennial_p)
22284 break;
22286 /* It's a type node --- don't mark it. */
22287 return;
22289 default:
22290 /* Mark everything else. */
22291 break;
22294 if (die->die_mark == 0)
22296 die->die_mark = 1;
22298 /* Now, mark any dies referenced from here. */
22299 prune_unused_types_walk_attribs (die);
22302 die->die_mark = 2;
22304 /* Mark children. */
22305 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22308 /* Increment the string counts on strings referred to from DIE's
22309 attributes. */
22311 static void
22312 prune_unused_types_update_strings (dw_die_ref die)
22314 dw_attr_ref a;
22315 unsigned ix;
22317 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22318 if (AT_class (a) == dw_val_class_str)
22320 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22321 s->refcount++;
22322 /* Avoid unnecessarily putting strings that are used less than
22323 twice in the hash table. */
22324 if (s->refcount
22325 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22327 void ** slot;
22328 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22329 htab_hash_string (s->str),
22330 INSERT);
22331 gcc_assert (*slot == NULL);
22332 *slot = s;
22337 /* Remove from the tree DIE any dies that aren't marked. */
22339 static void
22340 prune_unused_types_prune (dw_die_ref die)
22342 dw_die_ref c;
22344 gcc_assert (die->die_mark);
22345 prune_unused_types_update_strings (die);
22347 if (! die->die_child)
22348 return;
22350 c = die->die_child;
22351 do {
22352 dw_die_ref prev = c;
22353 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22354 if (c == die->die_child)
22356 /* No marked children between 'prev' and the end of the list. */
22357 if (prev == c)
22358 /* No marked children at all. */
22359 die->die_child = NULL;
22360 else
22362 prev->die_sib = c->die_sib;
22363 die->die_child = prev;
22365 return;
22368 if (c != prev->die_sib)
22369 prev->die_sib = c;
22370 prune_unused_types_prune (c);
22371 } while (c != die->die_child);
22374 /* Remove dies representing declarations that we never use. */
22376 static void
22377 prune_unused_types (void)
22379 unsigned int i;
22380 limbo_die_node *node;
22381 comdat_type_node *ctnode;
22382 pubname_ref pub;
22383 dw_die_ref base_type;
22385 #if ENABLE_ASSERT_CHECKING
22386 /* All the marks should already be clear. */
22387 verify_marks_clear (comp_unit_die ());
22388 for (node = limbo_die_list; node; node = node->next)
22389 verify_marks_clear (node->die);
22390 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22391 verify_marks_clear (ctnode->root_die);
22392 #endif /* ENABLE_ASSERT_CHECKING */
22394 /* Mark types that are used in global variables. */
22395 premark_types_used_by_global_vars ();
22397 /* Set the mark on nodes that are actually used. */
22398 prune_unused_types_walk (comp_unit_die ());
22399 for (node = limbo_die_list; node; node = node->next)
22400 prune_unused_types_walk (node->die);
22401 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22403 prune_unused_types_walk (ctnode->root_die);
22404 prune_unused_types_mark (ctnode->type_die, 1);
22407 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22408 are unusual in that they are pubnames that are the children of pubtypes.
22409 They should only be marked via their parent DW_TAG_enumeration_type die,
22410 not as roots in themselves. */
22411 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22412 if (pub->die->die_tag != DW_TAG_enumerator)
22413 prune_unused_types_mark (pub->die, 1);
22414 for (i = 0; base_types.iterate (i, &base_type); i++)
22415 prune_unused_types_mark (base_type, 1);
22417 if (debug_str_hash)
22418 htab_empty (debug_str_hash);
22419 if (skeleton_debug_str_hash)
22420 htab_empty (skeleton_debug_str_hash);
22421 prune_unused_types_prune (comp_unit_die ());
22422 for (node = limbo_die_list; node; node = node->next)
22423 prune_unused_types_prune (node->die);
22424 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22425 prune_unused_types_prune (ctnode->root_die);
22427 /* Leave the marks clear. */
22428 prune_unmark_dies (comp_unit_die ());
22429 for (node = limbo_die_list; node; node = node->next)
22430 prune_unmark_dies (node->die);
22431 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22432 prune_unmark_dies (ctnode->root_die);
22435 /* Set the parameter to true if there are any relative pathnames in
22436 the file table. */
22437 static int
22438 file_table_relative_p (void ** slot, void *param)
22440 bool *p = (bool *) param;
22441 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22442 if (!IS_ABSOLUTE_PATH (d->filename))
22444 *p = true;
22445 return 0;
22447 return 1;
22450 /* Helpers to manipulate hash table of comdat type units. */
22452 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22454 typedef comdat_type_node value_type;
22455 typedef comdat_type_node compare_type;
22456 static inline hashval_t hash (const value_type *);
22457 static inline bool equal (const value_type *, const compare_type *);
22460 inline hashval_t
22461 comdat_type_hasher::hash (const value_type *type_node)
22463 hashval_t h;
22464 memcpy (&h, type_node->signature, sizeof (h));
22465 return h;
22468 inline bool
22469 comdat_type_hasher::equal (const value_type *type_node_1,
22470 const compare_type *type_node_2)
22472 return (! memcmp (type_node_1->signature, type_node_2->signature,
22473 DWARF_TYPE_SIGNATURE_SIZE));
22476 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22477 to the location it would have been added, should we know its
22478 DECL_ASSEMBLER_NAME when we added other attributes. This will
22479 probably improve compactness of debug info, removing equivalent
22480 abbrevs, and hide any differences caused by deferring the
22481 computation of the assembler name, triggered by e.g. PCH. */
22483 static inline void
22484 move_linkage_attr (dw_die_ref die)
22486 unsigned ix = vec_safe_length (die->die_attr);
22487 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22489 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22490 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22492 while (--ix > 0)
22494 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22496 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22497 break;
22500 if (ix != vec_safe_length (die->die_attr) - 1)
22502 die->die_attr->pop ();
22503 die->die_attr->quick_insert (ix, linkage);
22507 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22508 referenced from typed stack ops and count how often they are used. */
22510 static void
22511 mark_base_types (dw_loc_descr_ref loc)
22513 dw_die_ref base_type = NULL;
22515 for (; loc; loc = loc->dw_loc_next)
22517 switch (loc->dw_loc_opc)
22519 case DW_OP_GNU_regval_type:
22520 case DW_OP_GNU_deref_type:
22521 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22522 break;
22523 case DW_OP_GNU_convert:
22524 case DW_OP_GNU_reinterpret:
22525 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22526 continue;
22527 /* FALLTHRU */
22528 case DW_OP_GNU_const_type:
22529 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22530 break;
22531 case DW_OP_GNU_entry_value:
22532 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22533 continue;
22534 default:
22535 continue;
22537 gcc_assert (base_type->die_parent == comp_unit_die ());
22538 if (base_type->die_mark)
22539 base_type->die_mark++;
22540 else
22542 base_types.safe_push (base_type);
22543 base_type->die_mark = 1;
22548 /* Comparison function for sorting marked base types. */
22550 static int
22551 base_type_cmp (const void *x, const void *y)
22553 dw_die_ref dx = *(const dw_die_ref *) x;
22554 dw_die_ref dy = *(const dw_die_ref *) y;
22555 unsigned int byte_size1, byte_size2;
22556 unsigned int encoding1, encoding2;
22557 if (dx->die_mark > dy->die_mark)
22558 return -1;
22559 if (dx->die_mark < dy->die_mark)
22560 return 1;
22561 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22562 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22563 if (byte_size1 < byte_size2)
22564 return 1;
22565 if (byte_size1 > byte_size2)
22566 return -1;
22567 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22568 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22569 if (encoding1 < encoding2)
22570 return 1;
22571 if (encoding1 > encoding2)
22572 return -1;
22573 return 0;
22576 /* Move base types marked by mark_base_types as early as possible
22577 in the CU, sorted by decreasing usage count both to make the
22578 uleb128 references as small as possible and to make sure they
22579 will have die_offset already computed by calc_die_sizes when
22580 sizes of typed stack loc ops is computed. */
22582 static void
22583 move_marked_base_types (void)
22585 unsigned int i;
22586 dw_die_ref base_type, die, c;
22588 if (base_types.is_empty ())
22589 return;
22591 /* Sort by decreasing usage count, they will be added again in that
22592 order later on. */
22593 base_types.qsort (base_type_cmp);
22594 die = comp_unit_die ();
22595 c = die->die_child;
22598 dw_die_ref prev = c;
22599 c = c->die_sib;
22600 while (c->die_mark)
22602 remove_child_with_prev (c, prev);
22603 /* As base types got marked, there must be at least
22604 one node other than DW_TAG_base_type. */
22605 gcc_assert (c != c->die_sib);
22606 c = c->die_sib;
22609 while (c != die->die_child);
22610 gcc_assert (die->die_child);
22611 c = die->die_child;
22612 for (i = 0; base_types.iterate (i, &base_type); i++)
22614 base_type->die_mark = 0;
22615 base_type->die_sib = c->die_sib;
22616 c->die_sib = base_type;
22617 c = base_type;
22621 /* Helper function for resolve_addr, attempt to resolve
22622 one CONST_STRING, return non-zero if not successful. Similarly verify that
22623 SYMBOL_REFs refer to variables emitted in the current CU. */
22625 static int
22626 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22628 rtx rtl = *addr;
22630 if (GET_CODE (rtl) == CONST_STRING)
22632 size_t len = strlen (XSTR (rtl, 0)) + 1;
22633 tree t = build_string (len, XSTR (rtl, 0));
22634 tree tlen = size_int (len - 1);
22635 TREE_TYPE (t)
22636 = build_array_type (char_type_node, build_index_type (tlen));
22637 rtl = lookup_constant_def (t);
22638 if (!rtl || !MEM_P (rtl))
22639 return 1;
22640 rtl = XEXP (rtl, 0);
22641 if (GET_CODE (rtl) == SYMBOL_REF
22642 && SYMBOL_REF_DECL (rtl)
22643 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22644 return 1;
22645 vec_safe_push (used_rtx_array, rtl);
22646 *addr = rtl;
22647 return 0;
22650 if (GET_CODE (rtl) == SYMBOL_REF
22651 && SYMBOL_REF_DECL (rtl))
22653 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22655 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22656 return 1;
22658 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22659 return 1;
22662 if (GET_CODE (rtl) == CONST
22663 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22664 return 1;
22666 return 0;
22669 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22670 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22671 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22673 static rtx
22674 string_cst_pool_decl (tree t)
22676 rtx rtl = output_constant_def (t, 1);
22677 unsigned char *array;
22678 dw_loc_descr_ref l;
22679 tree decl;
22680 size_t len;
22681 dw_die_ref ref;
22683 if (!rtl || !MEM_P (rtl))
22684 return NULL_RTX;
22685 rtl = XEXP (rtl, 0);
22686 if (GET_CODE (rtl) != SYMBOL_REF
22687 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22688 return NULL_RTX;
22690 decl = SYMBOL_REF_DECL (rtl);
22691 if (!lookup_decl_die (decl))
22693 len = TREE_STRING_LENGTH (t);
22694 vec_safe_push (used_rtx_array, rtl);
22695 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22696 array = (unsigned char *) ggc_alloc_atomic (len);
22697 memcpy (array, TREE_STRING_POINTER (t), len);
22698 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22699 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22700 l->dw_loc_oprnd2.v.val_vec.length = len;
22701 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22702 l->dw_loc_oprnd2.v.val_vec.array = array;
22703 add_AT_loc (ref, DW_AT_location, l);
22704 equate_decl_number_to_die (decl, ref);
22706 return rtl;
22709 /* Helper function of resolve_addr_in_expr. LOC is
22710 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22711 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22712 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22713 with DW_OP_GNU_implicit_pointer if possible
22714 and return true, if unsuccessful, return false. */
22716 static bool
22717 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22719 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22720 HOST_WIDE_INT offset = 0;
22721 dw_die_ref ref = NULL;
22722 tree decl;
22724 if (GET_CODE (rtl) == CONST
22725 && GET_CODE (XEXP (rtl, 0)) == PLUS
22726 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22728 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22729 rtl = XEXP (XEXP (rtl, 0), 0);
22731 if (GET_CODE (rtl) == CONST_STRING)
22733 size_t len = strlen (XSTR (rtl, 0)) + 1;
22734 tree t = build_string (len, XSTR (rtl, 0));
22735 tree tlen = size_int (len - 1);
22737 TREE_TYPE (t)
22738 = build_array_type (char_type_node, build_index_type (tlen));
22739 rtl = string_cst_pool_decl (t);
22740 if (!rtl)
22741 return false;
22743 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22745 decl = SYMBOL_REF_DECL (rtl);
22746 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22748 ref = lookup_decl_die (decl);
22749 if (ref && (get_AT (ref, DW_AT_location)
22750 || get_AT (ref, DW_AT_const_value)))
22752 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22753 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22754 loc->dw_loc_oprnd1.val_entry = NULL;
22755 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22756 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22757 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22758 loc->dw_loc_oprnd2.v.val_int = offset;
22759 return true;
22763 return false;
22766 /* Helper function for resolve_addr, handle one location
22767 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22768 the location list couldn't be resolved. */
22770 static bool
22771 resolve_addr_in_expr (dw_loc_descr_ref loc)
22773 dw_loc_descr_ref keep = NULL;
22774 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22775 switch (loc->dw_loc_opc)
22777 case DW_OP_addr:
22778 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22780 if ((prev == NULL
22781 || prev->dw_loc_opc == DW_OP_piece
22782 || prev->dw_loc_opc == DW_OP_bit_piece)
22783 && loc->dw_loc_next
22784 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22785 && !dwarf_strict
22786 && optimize_one_addr_into_implicit_ptr (loc))
22787 break;
22788 return false;
22790 break;
22791 case DW_OP_GNU_addr_index:
22792 case DW_OP_GNU_const_index:
22793 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22794 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22795 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22796 NULL))
22797 return false;
22798 break;
22799 case DW_OP_const4u:
22800 case DW_OP_const8u:
22801 if (loc->dtprel
22802 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22803 return false;
22804 break;
22805 case DW_OP_plus_uconst:
22806 if (size_of_loc_descr (loc)
22807 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22809 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22811 dw_loc_descr_ref repl
22812 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22813 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22814 add_loc_descr (&repl, loc->dw_loc_next);
22815 *loc = *repl;
22817 break;
22818 case DW_OP_implicit_value:
22819 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22820 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22821 return false;
22822 break;
22823 case DW_OP_GNU_implicit_pointer:
22824 case DW_OP_GNU_parameter_ref:
22825 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22827 dw_die_ref ref
22828 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22829 if (ref == NULL)
22830 return false;
22831 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22832 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22833 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22835 break;
22836 case DW_OP_GNU_const_type:
22837 case DW_OP_GNU_regval_type:
22838 case DW_OP_GNU_deref_type:
22839 case DW_OP_GNU_convert:
22840 case DW_OP_GNU_reinterpret:
22841 while (loc->dw_loc_next
22842 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22844 dw_die_ref base1, base2;
22845 unsigned enc1, enc2, size1, size2;
22846 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22847 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22848 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22849 else if (loc->dw_loc_oprnd1.val_class
22850 == dw_val_class_unsigned_const)
22851 break;
22852 else
22853 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22854 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22855 == dw_val_class_unsigned_const)
22856 break;
22857 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22858 gcc_assert (base1->die_tag == DW_TAG_base_type
22859 && base2->die_tag == DW_TAG_base_type);
22860 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22861 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22862 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22863 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22864 if (size1 == size2
22865 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22866 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22867 && loc != keep)
22868 || enc1 == enc2))
22870 /* Optimize away next DW_OP_GNU_convert after
22871 adjusting LOC's base type die reference. */
22872 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22873 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22874 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22875 else
22876 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22877 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22878 continue;
22880 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22881 point typed stack entry. */
22882 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22883 keep = loc->dw_loc_next;
22884 break;
22886 break;
22887 default:
22888 break;
22890 return true;
22893 /* Helper function of resolve_addr. DIE had DW_AT_location of
22894 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
22895 and DW_OP_addr couldn't be resolved. resolve_addr has already
22896 removed the DW_AT_location attribute. This function attempts to
22897 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
22898 to it or DW_AT_const_value attribute, if possible. */
22900 static void
22901 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
22903 if (TREE_CODE (decl) != VAR_DECL
22904 || lookup_decl_die (decl) != die
22905 || DECL_EXTERNAL (decl)
22906 || !TREE_STATIC (decl)
22907 || DECL_INITIAL (decl) == NULL_TREE
22908 || DECL_P (DECL_INITIAL (decl))
22909 || get_AT (die, DW_AT_const_value))
22910 return;
22912 tree init = DECL_INITIAL (decl);
22913 HOST_WIDE_INT offset = 0;
22914 /* For variables that have been optimized away and thus
22915 don't have a memory location, see if we can emit
22916 DW_AT_const_value instead. */
22917 if (tree_add_const_value_attribute (die, init))
22918 return;
22919 if (dwarf_strict)
22920 return;
22921 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
22922 and ADDR_EXPR refers to a decl that has DW_AT_location or
22923 DW_AT_const_value (but isn't addressable, otherwise
22924 resolving the original DW_OP_addr wouldn't fail), see if
22925 we can add DW_OP_GNU_implicit_pointer. */
22926 STRIP_NOPS (init);
22927 if (TREE_CODE (init) == POINTER_PLUS_EXPR
22928 && host_integerp (TREE_OPERAND (init, 1), 0))
22930 offset = tree_low_cst (TREE_OPERAND (init, 1), 0);
22931 init = TREE_OPERAND (init, 0);
22932 STRIP_NOPS (init);
22934 if (TREE_CODE (init) != ADDR_EXPR)
22935 return;
22936 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
22937 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
22938 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
22939 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
22940 && TREE_OPERAND (init, 0) != decl))
22942 dw_die_ref ref;
22943 dw_loc_descr_ref l;
22945 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
22947 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
22948 if (!rtl)
22949 return;
22950 decl = SYMBOL_REF_DECL (rtl);
22952 else
22953 decl = TREE_OPERAND (init, 0);
22954 ref = lookup_decl_die (decl);
22955 if (ref == NULL
22956 || (!get_AT (ref, DW_AT_location)
22957 && !get_AT (ref, DW_AT_const_value)))
22958 return;
22959 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
22960 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22961 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
22962 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
22963 add_AT_loc (die, DW_AT_location, l);
22967 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22968 an address in .rodata section if the string literal is emitted there,
22969 or remove the containing location list or replace DW_AT_const_value
22970 with DW_AT_location and empty location expression, if it isn't found
22971 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22972 to something that has been emitted in the current CU. */
22974 static void
22975 resolve_addr (dw_die_ref die)
22977 dw_die_ref c;
22978 dw_attr_ref a;
22979 dw_loc_list_ref *curr, *start, loc;
22980 unsigned ix;
22982 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22983 switch (AT_class (a))
22985 case dw_val_class_loc_list:
22986 start = curr = AT_loc_list_ptr (a);
22987 loc = *curr;
22988 gcc_assert (loc);
22989 /* The same list can be referenced more than once. See if we have
22990 already recorded the result from a previous pass. */
22991 if (loc->replaced)
22992 *curr = loc->dw_loc_next;
22993 else if (!loc->resolved_addr)
22995 /* As things stand, we do not expect or allow one die to
22996 reference a suffix of another die's location list chain.
22997 References must be identical or completely separate.
22998 There is therefore no need to cache the result of this
22999 pass on any list other than the first; doing so
23000 would lead to unnecessary writes. */
23001 while (*curr)
23003 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23004 if (!resolve_addr_in_expr ((*curr)->expr))
23006 dw_loc_list_ref next = (*curr)->dw_loc_next;
23007 dw_loc_descr_ref l = (*curr)->expr;
23009 if (next && (*curr)->ll_symbol)
23011 gcc_assert (!next->ll_symbol);
23012 next->ll_symbol = (*curr)->ll_symbol;
23014 if (dwarf_split_debug_info)
23015 remove_loc_list_addr_table_entries (l);
23016 *curr = next;
23018 else
23020 mark_base_types ((*curr)->expr);
23021 curr = &(*curr)->dw_loc_next;
23024 if (loc == *start)
23025 loc->resolved_addr = 1;
23026 else
23028 loc->replaced = 1;
23029 loc->dw_loc_next = *start;
23032 if (!*start)
23034 remove_AT (die, a->dw_attr);
23035 ix--;
23037 break;
23038 case dw_val_class_loc:
23040 dw_loc_descr_ref l = AT_loc (a);
23041 /* For -gdwarf-2 don't attempt to optimize
23042 DW_AT_data_member_location containing
23043 DW_OP_plus_uconst - older consumers might
23044 rely on it being that op instead of a more complex,
23045 but shorter, location description. */
23046 if ((dwarf_version > 2
23047 || a->dw_attr != DW_AT_data_member_location
23048 || l == NULL
23049 || l->dw_loc_opc != DW_OP_plus_uconst
23050 || l->dw_loc_next != NULL)
23051 && !resolve_addr_in_expr (l))
23053 if (dwarf_split_debug_info)
23054 remove_loc_list_addr_table_entries (l);
23055 if (l != NULL
23056 && l->dw_loc_next == NULL
23057 && l->dw_loc_opc == DW_OP_addr
23058 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23059 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23060 && a->dw_attr == DW_AT_location)
23062 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23063 remove_AT (die, a->dw_attr);
23064 ix--;
23065 optimize_location_into_implicit_ptr (die, decl);
23066 break;
23068 remove_AT (die, a->dw_attr);
23069 ix--;
23071 else
23072 mark_base_types (l);
23074 break;
23075 case dw_val_class_addr:
23076 if (a->dw_attr == DW_AT_const_value
23077 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23079 if (AT_index (a) != NOT_INDEXED)
23080 remove_addr_table_entry (a->dw_attr_val.val_entry);
23081 remove_AT (die, a->dw_attr);
23082 ix--;
23084 if (die->die_tag == DW_TAG_GNU_call_site
23085 && a->dw_attr == DW_AT_abstract_origin)
23087 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23088 dw_die_ref tdie = lookup_decl_die (tdecl);
23089 if (tdie == NULL
23090 && DECL_EXTERNAL (tdecl)
23091 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23093 force_decl_die (tdecl);
23094 tdie = lookup_decl_die (tdecl);
23096 if (tdie)
23098 a->dw_attr_val.val_class = dw_val_class_die_ref;
23099 a->dw_attr_val.v.val_die_ref.die = tdie;
23100 a->dw_attr_val.v.val_die_ref.external = 0;
23102 else
23104 if (AT_index (a) != NOT_INDEXED)
23105 remove_addr_table_entry (a->dw_attr_val.val_entry);
23106 remove_AT (die, a->dw_attr);
23107 ix--;
23110 break;
23111 default:
23112 break;
23115 FOR_EACH_CHILD (die, c, resolve_addr (c));
23118 /* Helper routines for optimize_location_lists.
23119 This pass tries to share identical local lists in .debug_loc
23120 section. */
23122 /* Iteratively hash operands of LOC opcode. */
23124 static hashval_t
23125 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23127 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23128 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23130 switch (loc->dw_loc_opc)
23132 case DW_OP_const4u:
23133 case DW_OP_const8u:
23134 if (loc->dtprel)
23135 goto hash_addr;
23136 /* FALLTHRU */
23137 case DW_OP_const1u:
23138 case DW_OP_const1s:
23139 case DW_OP_const2u:
23140 case DW_OP_const2s:
23141 case DW_OP_const4s:
23142 case DW_OP_const8s:
23143 case DW_OP_constu:
23144 case DW_OP_consts:
23145 case DW_OP_pick:
23146 case DW_OP_plus_uconst:
23147 case DW_OP_breg0:
23148 case DW_OP_breg1:
23149 case DW_OP_breg2:
23150 case DW_OP_breg3:
23151 case DW_OP_breg4:
23152 case DW_OP_breg5:
23153 case DW_OP_breg6:
23154 case DW_OP_breg7:
23155 case DW_OP_breg8:
23156 case DW_OP_breg9:
23157 case DW_OP_breg10:
23158 case DW_OP_breg11:
23159 case DW_OP_breg12:
23160 case DW_OP_breg13:
23161 case DW_OP_breg14:
23162 case DW_OP_breg15:
23163 case DW_OP_breg16:
23164 case DW_OP_breg17:
23165 case DW_OP_breg18:
23166 case DW_OP_breg19:
23167 case DW_OP_breg20:
23168 case DW_OP_breg21:
23169 case DW_OP_breg22:
23170 case DW_OP_breg23:
23171 case DW_OP_breg24:
23172 case DW_OP_breg25:
23173 case DW_OP_breg26:
23174 case DW_OP_breg27:
23175 case DW_OP_breg28:
23176 case DW_OP_breg29:
23177 case DW_OP_breg30:
23178 case DW_OP_breg31:
23179 case DW_OP_regx:
23180 case DW_OP_fbreg:
23181 case DW_OP_piece:
23182 case DW_OP_deref_size:
23183 case DW_OP_xderef_size:
23184 hash = iterative_hash_object (val1->v.val_int, hash);
23185 break;
23186 case DW_OP_skip:
23187 case DW_OP_bra:
23189 int offset;
23191 gcc_assert (val1->val_class == dw_val_class_loc);
23192 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23193 hash = iterative_hash_object (offset, hash);
23195 break;
23196 case DW_OP_implicit_value:
23197 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23198 switch (val2->val_class)
23200 case dw_val_class_const:
23201 hash = iterative_hash_object (val2->v.val_int, hash);
23202 break;
23203 case dw_val_class_vec:
23205 unsigned int elt_size = val2->v.val_vec.elt_size;
23206 unsigned int len = val2->v.val_vec.length;
23208 hash = iterative_hash_object (elt_size, hash);
23209 hash = iterative_hash_object (len, hash);
23210 hash = iterative_hash (val2->v.val_vec.array,
23211 len * elt_size, hash);
23213 break;
23214 case dw_val_class_const_double:
23215 hash = iterative_hash_object (val2->v.val_double.low, hash);
23216 hash = iterative_hash_object (val2->v.val_double.high, hash);
23217 break;
23218 case dw_val_class_addr:
23219 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23220 break;
23221 default:
23222 gcc_unreachable ();
23224 break;
23225 case DW_OP_bregx:
23226 case DW_OP_bit_piece:
23227 hash = iterative_hash_object (val1->v.val_int, hash);
23228 hash = iterative_hash_object (val2->v.val_int, hash);
23229 break;
23230 case DW_OP_addr:
23231 hash_addr:
23232 if (loc->dtprel)
23234 unsigned char dtprel = 0xd1;
23235 hash = iterative_hash_object (dtprel, hash);
23237 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23238 break;
23239 case DW_OP_GNU_addr_index:
23240 case DW_OP_GNU_const_index:
23242 if (loc->dtprel)
23244 unsigned char dtprel = 0xd1;
23245 hash = iterative_hash_object (dtprel, hash);
23247 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23249 break;
23250 case DW_OP_GNU_implicit_pointer:
23251 hash = iterative_hash_object (val2->v.val_int, hash);
23252 break;
23253 case DW_OP_GNU_entry_value:
23254 hash = hash_loc_operands (val1->v.val_loc, hash);
23255 break;
23256 case DW_OP_GNU_regval_type:
23257 case DW_OP_GNU_deref_type:
23259 unsigned int byte_size
23260 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23261 unsigned int encoding
23262 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23263 hash = iterative_hash_object (val1->v.val_int, hash);
23264 hash = iterative_hash_object (byte_size, hash);
23265 hash = iterative_hash_object (encoding, hash);
23267 break;
23268 case DW_OP_GNU_convert:
23269 case DW_OP_GNU_reinterpret:
23270 if (val1->val_class == dw_val_class_unsigned_const)
23272 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23273 break;
23275 /* FALLTHRU */
23276 case DW_OP_GNU_const_type:
23278 unsigned int byte_size
23279 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23280 unsigned int encoding
23281 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23282 hash = iterative_hash_object (byte_size, hash);
23283 hash = iterative_hash_object (encoding, hash);
23284 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23285 break;
23286 hash = iterative_hash_object (val2->val_class, hash);
23287 switch (val2->val_class)
23289 case dw_val_class_const:
23290 hash = iterative_hash_object (val2->v.val_int, hash);
23291 break;
23292 case dw_val_class_vec:
23294 unsigned int elt_size = val2->v.val_vec.elt_size;
23295 unsigned int len = val2->v.val_vec.length;
23297 hash = iterative_hash_object (elt_size, hash);
23298 hash = iterative_hash_object (len, hash);
23299 hash = iterative_hash (val2->v.val_vec.array,
23300 len * elt_size, hash);
23302 break;
23303 case dw_val_class_const_double:
23304 hash = iterative_hash_object (val2->v.val_double.low, hash);
23305 hash = iterative_hash_object (val2->v.val_double.high, hash);
23306 break;
23307 default:
23308 gcc_unreachable ();
23311 break;
23313 default:
23314 /* Other codes have no operands. */
23315 break;
23317 return hash;
23320 /* Iteratively hash the whole DWARF location expression LOC. */
23322 static inline hashval_t
23323 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23325 dw_loc_descr_ref l;
23326 bool sizes_computed = false;
23327 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23328 size_of_locs (loc);
23330 for (l = loc; l != NULL; l = l->dw_loc_next)
23332 enum dwarf_location_atom opc = l->dw_loc_opc;
23333 hash = iterative_hash_object (opc, hash);
23334 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23336 size_of_locs (loc);
23337 sizes_computed = true;
23339 hash = hash_loc_operands (l, hash);
23341 return hash;
23344 /* Compute hash of the whole location list LIST_HEAD. */
23346 static inline void
23347 hash_loc_list (dw_loc_list_ref list_head)
23349 dw_loc_list_ref curr = list_head;
23350 hashval_t hash = 0;
23352 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23354 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23355 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23356 if (curr->section)
23357 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23358 hash);
23359 hash = hash_locs (curr->expr, hash);
23361 list_head->hash = hash;
23364 /* Return true if X and Y opcodes have the same operands. */
23366 static inline bool
23367 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23369 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23370 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23371 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23372 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23374 switch (x->dw_loc_opc)
23376 case DW_OP_const4u:
23377 case DW_OP_const8u:
23378 if (x->dtprel)
23379 goto hash_addr;
23380 /* FALLTHRU */
23381 case DW_OP_const1u:
23382 case DW_OP_const1s:
23383 case DW_OP_const2u:
23384 case DW_OP_const2s:
23385 case DW_OP_const4s:
23386 case DW_OP_const8s:
23387 case DW_OP_constu:
23388 case DW_OP_consts:
23389 case DW_OP_pick:
23390 case DW_OP_plus_uconst:
23391 case DW_OP_breg0:
23392 case DW_OP_breg1:
23393 case DW_OP_breg2:
23394 case DW_OP_breg3:
23395 case DW_OP_breg4:
23396 case DW_OP_breg5:
23397 case DW_OP_breg6:
23398 case DW_OP_breg7:
23399 case DW_OP_breg8:
23400 case DW_OP_breg9:
23401 case DW_OP_breg10:
23402 case DW_OP_breg11:
23403 case DW_OP_breg12:
23404 case DW_OP_breg13:
23405 case DW_OP_breg14:
23406 case DW_OP_breg15:
23407 case DW_OP_breg16:
23408 case DW_OP_breg17:
23409 case DW_OP_breg18:
23410 case DW_OP_breg19:
23411 case DW_OP_breg20:
23412 case DW_OP_breg21:
23413 case DW_OP_breg22:
23414 case DW_OP_breg23:
23415 case DW_OP_breg24:
23416 case DW_OP_breg25:
23417 case DW_OP_breg26:
23418 case DW_OP_breg27:
23419 case DW_OP_breg28:
23420 case DW_OP_breg29:
23421 case DW_OP_breg30:
23422 case DW_OP_breg31:
23423 case DW_OP_regx:
23424 case DW_OP_fbreg:
23425 case DW_OP_piece:
23426 case DW_OP_deref_size:
23427 case DW_OP_xderef_size:
23428 return valx1->v.val_int == valy1->v.val_int;
23429 case DW_OP_skip:
23430 case DW_OP_bra:
23431 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23432 can cause irrelevant differences in dw_loc_addr. */
23433 gcc_assert (valx1->val_class == dw_val_class_loc
23434 && valy1->val_class == dw_val_class_loc
23435 && (dwarf_split_debug_info
23436 || x->dw_loc_addr == y->dw_loc_addr));
23437 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23438 case DW_OP_implicit_value:
23439 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23440 || valx2->val_class != valy2->val_class)
23441 return false;
23442 switch (valx2->val_class)
23444 case dw_val_class_const:
23445 return valx2->v.val_int == valy2->v.val_int;
23446 case dw_val_class_vec:
23447 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23448 && valx2->v.val_vec.length == valy2->v.val_vec.length
23449 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23450 valx2->v.val_vec.elt_size
23451 * valx2->v.val_vec.length) == 0;
23452 case dw_val_class_const_double:
23453 return valx2->v.val_double.low == valy2->v.val_double.low
23454 && valx2->v.val_double.high == valy2->v.val_double.high;
23455 case dw_val_class_addr:
23456 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23457 default:
23458 gcc_unreachable ();
23460 case DW_OP_bregx:
23461 case DW_OP_bit_piece:
23462 return valx1->v.val_int == valy1->v.val_int
23463 && valx2->v.val_int == valy2->v.val_int;
23464 case DW_OP_addr:
23465 hash_addr:
23466 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23467 case DW_OP_GNU_addr_index:
23468 case DW_OP_GNU_const_index:
23470 rtx ax1 = valx1->val_entry->addr.rtl;
23471 rtx ay1 = valy1->val_entry->addr.rtl;
23472 return rtx_equal_p (ax1, ay1);
23474 case DW_OP_GNU_implicit_pointer:
23475 return valx1->val_class == dw_val_class_die_ref
23476 && valx1->val_class == valy1->val_class
23477 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23478 && valx2->v.val_int == valy2->v.val_int;
23479 case DW_OP_GNU_entry_value:
23480 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23481 case DW_OP_GNU_const_type:
23482 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23483 || valx2->val_class != valy2->val_class)
23484 return false;
23485 switch (valx2->val_class)
23487 case dw_val_class_const:
23488 return valx2->v.val_int == valy2->v.val_int;
23489 case dw_val_class_vec:
23490 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23491 && valx2->v.val_vec.length == valy2->v.val_vec.length
23492 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23493 valx2->v.val_vec.elt_size
23494 * valx2->v.val_vec.length) == 0;
23495 case dw_val_class_const_double:
23496 return valx2->v.val_double.low == valy2->v.val_double.low
23497 && valx2->v.val_double.high == valy2->v.val_double.high;
23498 default:
23499 gcc_unreachable ();
23501 case DW_OP_GNU_regval_type:
23502 case DW_OP_GNU_deref_type:
23503 return valx1->v.val_int == valy1->v.val_int
23504 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23505 case DW_OP_GNU_convert:
23506 case DW_OP_GNU_reinterpret:
23507 if (valx1->val_class != valy1->val_class)
23508 return false;
23509 if (valx1->val_class == dw_val_class_unsigned_const)
23510 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23511 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23512 case DW_OP_GNU_parameter_ref:
23513 return valx1->val_class == dw_val_class_die_ref
23514 && valx1->val_class == valy1->val_class
23515 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23516 default:
23517 /* Other codes have no operands. */
23518 return true;
23522 /* Return true if DWARF location expressions X and Y are the same. */
23524 static inline bool
23525 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23527 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23528 if (x->dw_loc_opc != y->dw_loc_opc
23529 || x->dtprel != y->dtprel
23530 || !compare_loc_operands (x, y))
23531 break;
23532 return x == NULL && y == NULL;
23535 /* Hashtable helpers. */
23537 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23539 typedef dw_loc_list_struct value_type;
23540 typedef dw_loc_list_struct compare_type;
23541 static inline hashval_t hash (const value_type *);
23542 static inline bool equal (const value_type *, const compare_type *);
23545 /* Return precomputed hash of location list X. */
23547 inline hashval_t
23548 loc_list_hasher::hash (const value_type *x)
23550 return x->hash;
23553 /* Return true if location lists A and B are the same. */
23555 inline bool
23556 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23558 if (a == b)
23559 return 1;
23560 if (a->hash != b->hash)
23561 return 0;
23562 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23563 if (strcmp (a->begin, b->begin) != 0
23564 || strcmp (a->end, b->end) != 0
23565 || (a->section == NULL) != (b->section == NULL)
23566 || (a->section && strcmp (a->section, b->section) != 0)
23567 || !compare_locs (a->expr, b->expr))
23568 break;
23569 return a == NULL && b == NULL;
23572 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23575 /* Recursively optimize location lists referenced from DIE
23576 children and share them whenever possible. */
23578 static void
23579 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23581 dw_die_ref c;
23582 dw_attr_ref a;
23583 unsigned ix;
23584 dw_loc_list_struct **slot;
23586 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23587 if (AT_class (a) == dw_val_class_loc_list)
23589 dw_loc_list_ref list = AT_loc_list (a);
23590 /* TODO: perform some optimizations here, before hashing
23591 it and storing into the hash table. */
23592 hash_loc_list (list);
23593 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23594 if (*slot == NULL)
23595 *slot = list;
23596 else
23597 a->dw_attr_val.v.val_loc_list = *slot;
23600 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23604 /* Recursively assign each location list a unique index into the debug_addr
23605 section. */
23607 static void
23608 index_location_lists (dw_die_ref die)
23610 dw_die_ref c;
23611 dw_attr_ref a;
23612 unsigned ix;
23614 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23615 if (AT_class (a) == dw_val_class_loc_list)
23617 dw_loc_list_ref list = AT_loc_list (a);
23618 dw_loc_list_ref curr;
23619 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23621 /* Don't index an entry that has already been indexed
23622 or won't be output. */
23623 if (curr->begin_entry != NULL
23624 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23625 continue;
23627 curr->begin_entry
23628 = add_addr_table_entry (xstrdup (curr->begin),
23629 ate_kind_label);
23633 FOR_EACH_CHILD (die, c, index_location_lists (c));
23636 /* Optimize location lists referenced from DIE
23637 children and share them whenever possible. */
23639 static void
23640 optimize_location_lists (dw_die_ref die)
23642 loc_list_hash_type htab;
23643 htab.create (500);
23644 optimize_location_lists_1 (die, htab);
23645 htab.dispose ();
23648 /* Output stuff that dwarf requires at the end of every file,
23649 and generate the DWARF-2 debugging info. */
23651 static void
23652 dwarf2out_finish (const char *filename)
23654 limbo_die_node *node, *next_node;
23655 comdat_type_node *ctnode;
23656 hash_table <comdat_type_hasher> comdat_type_table;
23657 unsigned int i;
23658 dw_die_ref main_comp_unit_die;
23660 /* PCH might result in DW_AT_producer string being restored from the
23661 header compilation, so always fill it with empty string initially
23662 and overwrite only here. */
23663 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23664 producer_string = gen_producer_string ();
23665 producer->dw_attr_val.v.val_str->refcount--;
23666 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23668 gen_scheduled_generic_parms_dies ();
23669 gen_remaining_tmpl_value_param_die_attribute ();
23671 /* Add the name for the main input file now. We delayed this from
23672 dwarf2out_init to avoid complications with PCH. */
23673 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23674 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23675 add_comp_dir_attribute (comp_unit_die ());
23676 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23678 bool p = false;
23679 htab_traverse (file_table, file_table_relative_p, &p);
23680 if (p)
23681 add_comp_dir_attribute (comp_unit_die ());
23684 if (deferred_locations_list)
23685 for (i = 0; i < deferred_locations_list->length (); i++)
23687 add_location_or_const_value_attribute (
23688 (*deferred_locations_list)[i].die,
23689 (*deferred_locations_list)[i].variable,
23690 false,
23691 DW_AT_location);
23694 /* Traverse the limbo die list, and add parent/child links. The only
23695 dies without parents that should be here are concrete instances of
23696 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23697 For concrete instances, we can get the parent die from the abstract
23698 instance. */
23699 for (node = limbo_die_list; node; node = next_node)
23701 dw_die_ref die = node->die;
23702 next_node = node->next;
23704 if (die->die_parent == NULL)
23706 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23708 if (origin && origin->die_parent)
23709 add_child_die (origin->die_parent, die);
23710 else if (is_cu_die (die))
23712 else if (seen_error ())
23713 /* It's OK to be confused by errors in the input. */
23714 add_child_die (comp_unit_die (), die);
23715 else
23717 /* In certain situations, the lexical block containing a
23718 nested function can be optimized away, which results
23719 in the nested function die being orphaned. Likewise
23720 with the return type of that nested function. Force
23721 this to be a child of the containing function.
23723 It may happen that even the containing function got fully
23724 inlined and optimized out. In that case we are lost and
23725 assign the empty child. This should not be big issue as
23726 the function is likely unreachable too. */
23727 gcc_assert (node->created_for);
23729 if (DECL_P (node->created_for))
23730 origin = get_context_die (DECL_CONTEXT (node->created_for));
23731 else if (TYPE_P (node->created_for))
23732 origin = scope_die_for (node->created_for, comp_unit_die ());
23733 else
23734 origin = comp_unit_die ();
23736 add_child_die (origin, die);
23741 limbo_die_list = NULL;
23743 #if ENABLE_ASSERT_CHECKING
23745 dw_die_ref die = comp_unit_die (), c;
23746 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23748 #endif
23749 resolve_addr (comp_unit_die ());
23750 move_marked_base_types ();
23752 for (node = deferred_asm_name; node; node = node->next)
23754 tree decl = node->created_for;
23755 /* When generating LTO bytecode we can not generate new assembler
23756 names at this point and all important decls got theirs via
23757 free-lang-data. */
23758 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23759 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23761 add_linkage_attr (node->die, decl);
23762 move_linkage_attr (node->die);
23766 deferred_asm_name = NULL;
23768 /* Walk through the list of incomplete types again, trying once more to
23769 emit full debugging info for them. */
23770 retry_incomplete_types ();
23772 if (flag_eliminate_unused_debug_types)
23773 prune_unused_types ();
23775 /* Generate separate COMDAT sections for type DIEs. */
23776 if (use_debug_types)
23778 break_out_comdat_types (comp_unit_die ());
23780 /* Each new type_unit DIE was added to the limbo die list when created.
23781 Since these have all been added to comdat_type_list, clear the
23782 limbo die list. */
23783 limbo_die_list = NULL;
23785 /* For each new comdat type unit, copy declarations for incomplete
23786 types to make the new unit self-contained (i.e., no direct
23787 references to the main compile unit). */
23788 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23789 copy_decls_for_unworthy_types (ctnode->root_die);
23790 copy_decls_for_unworthy_types (comp_unit_die ());
23792 /* In the process of copying declarations from one unit to another,
23793 we may have left some declarations behind that are no longer
23794 referenced. Prune them. */
23795 prune_unused_types ();
23798 /* Generate separate CUs for each of the include files we've seen.
23799 They will go into limbo_die_list. */
23800 if (flag_eliminate_dwarf2_dups)
23801 break_out_includes (comp_unit_die ());
23803 /* Traverse the DIE's and add add sibling attributes to those DIE's
23804 that have children. */
23805 add_sibling_attributes (comp_unit_die ());
23806 for (node = limbo_die_list; node; node = node->next)
23807 add_sibling_attributes (node->die);
23808 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23809 add_sibling_attributes (ctnode->root_die);
23811 /* When splitting DWARF info, we put some attributes in the
23812 skeleton compile_unit DIE that remains in the .o, while
23813 most attributes go in the DWO compile_unit_die. */
23814 if (dwarf_split_debug_info)
23815 main_comp_unit_die = gen_compile_unit_die (NULL);
23816 else
23817 main_comp_unit_die = comp_unit_die ();
23819 /* Output a terminator label for the .text section. */
23820 switch_to_section (text_section);
23821 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23822 if (cold_text_section)
23824 switch_to_section (cold_text_section);
23825 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23828 /* We can only use the low/high_pc attributes if all of the code was
23829 in .text. */
23830 if (!have_multiple_function_sections
23831 || (dwarf_version < 3 && dwarf_strict))
23833 /* Don't add if the CU has no associated code. */
23834 if (text_section_used)
23835 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23836 text_end_label, true);
23838 else
23840 unsigned fde_idx;
23841 dw_fde_ref fde;
23842 bool range_list_added = false;
23844 if (text_section_used)
23845 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23846 text_end_label, &range_list_added, true);
23847 if (cold_text_section_used)
23848 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23849 cold_end_label, &range_list_added, true);
23851 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23853 if (DECL_IGNORED_P (fde->decl))
23854 continue;
23855 if (!fde->in_std_section)
23856 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23857 fde->dw_fde_end, &range_list_added,
23858 true);
23859 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23860 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23861 fde->dw_fde_second_end, &range_list_added,
23862 true);
23865 if (range_list_added)
23867 /* We need to give .debug_loc and .debug_ranges an appropriate
23868 "base address". Use zero so that these addresses become
23869 absolute. Historically, we've emitted the unexpected
23870 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23871 Emit both to give time for other tools to adapt. */
23872 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23873 if (! dwarf_strict && dwarf_version < 4)
23874 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23876 add_ranges (NULL);
23880 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23881 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23882 debug_line_section_label);
23884 if (have_macinfo)
23885 add_AT_macptr (comp_unit_die (),
23886 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23887 macinfo_section_label);
23889 if (dwarf_split_debug_info && addr_index_table != NULL)
23891 /* optimize_location_lists calculates the size of the lists,
23892 so index them first, and assign indices to the entries.
23893 Although optimize_location_lists will remove entries from
23894 the table, it only does so for duplicates, and therefore
23895 only reduces ref_counts to 1. */
23896 unsigned int index = 0;
23897 index_location_lists (comp_unit_die ());
23898 htab_traverse_noresize (addr_index_table,
23899 index_addr_table_entry, &index);
23901 if (have_location_lists)
23902 optimize_location_lists (comp_unit_die ());
23904 save_macinfo_strings ();
23906 if (dwarf_split_debug_info)
23908 unsigned int index = 0;
23910 /* Add attributes common to skeleton compile_units and
23911 type_units. Because these attributes include strings, it
23912 must be done before freezing the string table. Top-level
23913 skeleton die attrs are added when the skeleton type unit is
23914 created, so ensure it is created by this point. */
23915 add_top_level_skeleton_die_attrs (main_comp_unit_die);
23916 (void) get_skeleton_type_unit ();
23917 htab_traverse_noresize (debug_str_hash, index_string, &index);
23920 /* Output all of the compilation units. We put the main one last so that
23921 the offsets are available to output_pubnames. */
23922 for (node = limbo_die_list; node; node = node->next)
23923 output_comp_unit (node->die, 0);
23925 comdat_type_table.create (100);
23926 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23928 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
23930 /* Don't output duplicate types. */
23931 if (*slot != HTAB_EMPTY_ENTRY)
23932 continue;
23934 /* Add a pointer to the line table for the main compilation unit
23935 so that the debugger can make sense of DW_AT_decl_file
23936 attributes. */
23937 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23938 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23939 (!dwarf_split_debug_info
23940 ? debug_line_section_label
23941 : debug_skeleton_line_section_label));
23943 output_comdat_type_unit (ctnode);
23944 *slot = ctnode;
23946 comdat_type_table.dispose ();
23948 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23949 both the main_cu and all skeleton TUs. Making this call unconditional
23950 would end up either adding a second copy of the AT_pubnames attribute, or
23951 requiring a special case in add_top_level_skeleton_die_attrs. */
23952 if (!dwarf_split_debug_info)
23953 add_AT_pubnames (comp_unit_die ());
23955 if (dwarf_split_debug_info)
23957 int mark;
23958 unsigned char checksum[16];
23959 struct md5_ctx ctx;
23961 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23962 md5_init_ctx (&ctx);
23963 mark = 0;
23964 die_checksum (comp_unit_die (), &ctx, &mark);
23965 unmark_all_dies (comp_unit_die ());
23966 md5_finish_ctx (&ctx, checksum);
23968 /* Use the first 8 bytes of the checksum as the dwo_id,
23969 and add it to both comp-unit DIEs. */
23970 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23971 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23973 /* Add the base offset of the ranges table to the skeleton
23974 comp-unit DIE. */
23975 if (ranges_table_in_use)
23976 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23977 ranges_section_label);
23979 switch_to_section (debug_addr_section);
23980 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23981 output_addr_table ();
23984 /* Output the main compilation unit if non-empty or if .debug_macinfo
23985 or .debug_macro will be emitted. */
23986 output_comp_unit (comp_unit_die (), have_macinfo);
23988 if (dwarf_split_debug_info && info_section_emitted)
23989 output_skeleton_debug_sections (main_comp_unit_die);
23991 /* Output the abbreviation table. */
23992 if (abbrev_die_table_in_use != 1)
23994 switch_to_section (debug_abbrev_section);
23995 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23996 output_abbrev_section ();
23999 /* Output location list section if necessary. */
24000 if (have_location_lists)
24002 /* Output the location lists info. */
24003 switch_to_section (debug_loc_section);
24004 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24005 output_location_lists (comp_unit_die ());
24008 /* Output public names and types tables if necessary. */
24009 output_pubnames (pubname_table);
24010 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
24011 It shouldn't hurt to emit it always, since pure DWARF2 consumers
24012 simply won't look for the section. */
24013 output_pubnames (pubtype_table);
24015 /* Output the address range information if a CU (.debug_info section)
24016 was emitted. We output an empty table even if we had no functions
24017 to put in it. This because the consumer has no way to tell the
24018 difference between an empty table that we omitted and failure to
24019 generate a table that would have contained data. */
24020 if (info_section_emitted)
24022 unsigned long aranges_length = size_of_aranges ();
24024 switch_to_section (debug_aranges_section);
24025 output_aranges (aranges_length);
24028 /* Output ranges section if necessary. */
24029 if (ranges_table_in_use)
24031 switch_to_section (debug_ranges_section);
24032 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24033 output_ranges ();
24036 /* Have to end the macro section. */
24037 if (have_macinfo)
24039 switch_to_section (debug_macinfo_section);
24040 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24041 output_macinfo ();
24042 dw2_asm_output_data (1, 0, "End compilation unit");
24045 /* Output the source line correspondence table. We must do this
24046 even if there is no line information. Otherwise, on an empty
24047 translation unit, we will generate a present, but empty,
24048 .debug_info section. IRIX 6.5 `nm' will then complain when
24049 examining the file. This is done late so that any filenames
24050 used by the debug_info section are marked as 'used'. */
24051 switch_to_section (debug_line_section);
24052 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24053 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24054 output_line_info (false);
24056 if (dwarf_split_debug_info && info_section_emitted)
24058 switch_to_section (debug_skeleton_line_section);
24059 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24060 output_line_info (true);
24063 /* If we emitted any indirect strings, output the string table too. */
24064 if (debug_str_hash || skeleton_debug_str_hash)
24065 output_indirect_strings ();
24068 #include "gt-dwarf2out.h"