- Remove SVN merge marker.
[official-gcc.git] / gcc / dwarf2out.c
blobe4a3c075520bd3760c17e55c7ae2081e80e97132
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 /* enum for tracking thread-local variables whose address is really an offset
3218 relative to the TLS pointer, which will need link-time relocation, but will
3219 not need relocation by the DWARF consumer. */
3221 enum dtprel_bool
3223 dtprel_false = 0,
3224 dtprel_true = 1
3227 /* Return the operator to use for an address of a variable. For dtprel_true, we
3228 use DW_OP_const*. For regular variables, which need both link-time
3229 relocation and consumer-level relocation (e.g., to account for shared objects
3230 loaded at a random address), we use DW_OP_addr*. */
3232 static inline enum dwarf_location_atom
3233 dw_addr_op (enum dtprel_bool dtprel)
3235 if (dtprel == dtprel_true)
3236 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3237 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3238 else
3239 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3242 /* Return a pointer to a newly allocated address location description. If
3243 dwarf_split_debug_info is true, then record the address with the appropriate
3244 relocation. */
3245 static inline dw_loc_descr_ref
3246 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3248 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3250 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3251 ref->dw_loc_oprnd1.v.val_addr = addr;
3252 ref->dtprel = dtprel;
3253 if (dwarf_split_debug_info)
3254 ref->dw_loc_oprnd1.val_entry
3255 = add_addr_table_entry (addr,
3256 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3257 else
3258 ref->dw_loc_oprnd1.val_entry = NULL;
3260 return ref;
3263 /* Section names used to hold DWARF debugging information. */
3265 #ifndef DEBUG_INFO_SECTION
3266 #define DEBUG_INFO_SECTION ".debug_info"
3267 #endif
3268 #ifndef DEBUG_DWO_INFO_SECTION
3269 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3270 #endif
3271 #ifndef DEBUG_ABBREV_SECTION
3272 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3273 #endif
3274 #ifndef DEBUG_DWO_ABBREV_SECTION
3275 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3276 #endif
3277 #ifndef DEBUG_ARANGES_SECTION
3278 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3279 #endif
3280 #ifndef DEBUG_ADDR_SECTION
3281 #define DEBUG_ADDR_SECTION ".debug_addr"
3282 #endif
3283 #ifndef DEBUG_NORM_MACINFO_SECTION
3284 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3285 #endif
3286 #ifndef DEBUG_DWO_MACINFO_SECTION
3287 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3288 #endif
3289 #ifndef DEBUG_MACINFO_SECTION
3290 #define DEBUG_MACINFO_SECTION \
3291 (!dwarf_split_debug_info \
3292 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3293 #endif
3294 #ifndef DEBUG_NORM_MACRO_SECTION
3295 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3296 #endif
3297 #ifndef DEBUG_DWO_MACRO_SECTION
3298 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3299 #endif
3300 #ifndef DEBUG_MACRO_SECTION
3301 #define DEBUG_MACRO_SECTION \
3302 (!dwarf_split_debug_info \
3303 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3304 #endif
3305 #ifndef DEBUG_LINE_SECTION
3306 #define DEBUG_LINE_SECTION ".debug_line"
3307 #endif
3308 #ifndef DEBUG_DWO_LINE_SECTION
3309 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3310 #endif
3311 #ifndef DEBUG_LOC_SECTION
3312 #define DEBUG_LOC_SECTION ".debug_loc"
3313 #endif
3314 #ifndef DEBUG_DWO_LOC_SECTION
3315 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3316 #endif
3317 #ifndef DEBUG_PUBNAMES_SECTION
3318 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3319 #endif
3320 #ifndef DEBUG_PUBTYPES_SECTION
3321 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3322 #endif
3323 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3324 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3325 #ifndef DEBUG_STR_OFFSETS_SECTION
3326 #define DEBUG_STR_OFFSETS_SECTION \
3327 (!dwarf_split_debug_info \
3328 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3329 #endif
3330 #ifndef DEBUG_STR_DWO_SECTION
3331 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3332 #endif
3333 #ifndef DEBUG_STR_SECTION
3334 #define DEBUG_STR_SECTION ".debug_str"
3335 #endif
3336 #ifndef DEBUG_RANGES_SECTION
3337 #define DEBUG_RANGES_SECTION ".debug_ranges"
3338 #endif
3340 /* Standard ELF section names for compiled code and data. */
3341 #ifndef TEXT_SECTION_NAME
3342 #define TEXT_SECTION_NAME ".text"
3343 #endif
3345 /* Section flags for .debug_macinfo/.debug_macro section. */
3346 #define DEBUG_MACRO_SECTION_FLAGS \
3347 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3349 /* Section flags for .debug_str section. */
3350 #define DEBUG_STR_SECTION_FLAGS \
3351 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3352 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3353 : SECTION_DEBUG)
3355 /* Section flags for .debug_str.dwo section. */
3356 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3358 /* Labels we insert at beginning sections we can reference instead of
3359 the section names themselves. */
3361 #ifndef TEXT_SECTION_LABEL
3362 #define TEXT_SECTION_LABEL "Ltext"
3363 #endif
3364 #ifndef COLD_TEXT_SECTION_LABEL
3365 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3366 #endif
3367 #ifndef DEBUG_LINE_SECTION_LABEL
3368 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3369 #endif
3370 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3371 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3372 #endif
3373 #ifndef DEBUG_INFO_SECTION_LABEL
3374 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3375 #endif
3376 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3377 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3378 #endif
3379 #ifndef DEBUG_ABBREV_SECTION_LABEL
3380 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3381 #endif
3382 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3383 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3384 #endif
3385 #ifndef DEBUG_ADDR_SECTION_LABEL
3386 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3387 #endif
3388 #ifndef DEBUG_LOC_SECTION_LABEL
3389 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3390 #endif
3391 #ifndef DEBUG_RANGES_SECTION_LABEL
3392 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3393 #endif
3394 #ifndef DEBUG_MACINFO_SECTION_LABEL
3395 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3396 #endif
3397 #ifndef DEBUG_MACRO_SECTION_LABEL
3398 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3399 #endif
3400 #define SKELETON_COMP_DIE_ABBREV 1
3401 #define SKELETON_TYPE_DIE_ABBREV 2
3403 /* Definitions of defaults for formats and names of various special
3404 (artificial) labels which may be generated within this file (when the -g
3405 options is used and DWARF2_DEBUGGING_INFO is in effect.
3406 If necessary, these may be overridden from within the tm.h file, but
3407 typically, overriding these defaults is unnecessary. */
3409 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3410 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3411 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3412 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3413 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3414 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3415 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3416 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3417 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3418 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3419 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3420 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3421 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3422 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3424 #ifndef TEXT_END_LABEL
3425 #define TEXT_END_LABEL "Letext"
3426 #endif
3427 #ifndef COLD_END_LABEL
3428 #define COLD_END_LABEL "Letext_cold"
3429 #endif
3430 #ifndef BLOCK_BEGIN_LABEL
3431 #define BLOCK_BEGIN_LABEL "LBB"
3432 #endif
3433 #ifndef BLOCK_END_LABEL
3434 #define BLOCK_END_LABEL "LBE"
3435 #endif
3436 #ifndef LINE_CODE_LABEL
3437 #define LINE_CODE_LABEL "LM"
3438 #endif
3441 /* Return the root of the DIE's built for the current compilation unit. */
3442 static dw_die_ref
3443 comp_unit_die (void)
3445 if (!single_comp_unit_die)
3446 single_comp_unit_die = gen_compile_unit_die (NULL);
3447 return single_comp_unit_die;
3450 /* We allow a language front-end to designate a function that is to be
3451 called to "demangle" any name before it is put into a DIE. */
3453 static const char *(*demangle_name_func) (const char *);
3455 void
3456 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3458 demangle_name_func = func;
3461 /* Test if rtl node points to a pseudo register. */
3463 static inline int
3464 is_pseudo_reg (const_rtx rtl)
3466 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3467 || (GET_CODE (rtl) == SUBREG
3468 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3471 /* Return a reference to a type, with its const and volatile qualifiers
3472 removed. */
3474 static inline tree
3475 type_main_variant (tree type)
3477 type = TYPE_MAIN_VARIANT (type);
3479 /* ??? There really should be only one main variant among any group of
3480 variants of a given type (and all of the MAIN_VARIANT values for all
3481 members of the group should point to that one type) but sometimes the C
3482 front-end messes this up for array types, so we work around that bug
3483 here. */
3484 if (TREE_CODE (type) == ARRAY_TYPE)
3485 while (type != TYPE_MAIN_VARIANT (type))
3486 type = TYPE_MAIN_VARIANT (type);
3488 return type;
3491 /* Return nonzero if the given type node represents a tagged type. */
3493 static inline int
3494 is_tagged_type (const_tree type)
3496 enum tree_code code = TREE_CODE (type);
3498 return (code == RECORD_TYPE || code == UNION_TYPE
3499 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3502 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3504 static void
3505 get_ref_die_offset_label (char *label, dw_die_ref ref)
3507 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3510 /* Return die_offset of a DIE reference to a base type. */
3512 static unsigned long int
3513 get_base_type_offset (dw_die_ref ref)
3515 if (ref->die_offset)
3516 return ref->die_offset;
3517 if (comp_unit_die ()->die_abbrev)
3519 calc_base_type_die_sizes ();
3520 gcc_assert (ref->die_offset);
3522 return ref->die_offset;
3525 /* Return die_offset of a DIE reference other than base type. */
3527 static unsigned long int
3528 get_ref_die_offset (dw_die_ref ref)
3530 gcc_assert (ref->die_offset);
3531 return ref->die_offset;
3534 /* Convert a DIE tag into its string name. */
3536 static const char *
3537 dwarf_tag_name (unsigned int tag)
3539 const char *name = get_DW_TAG_name (tag);
3541 if (name != NULL)
3542 return name;
3544 return "DW_TAG_<unknown>";
3547 /* Convert a DWARF attribute code into its string name. */
3549 static const char *
3550 dwarf_attr_name (unsigned int attr)
3552 const char *name;
3554 switch (attr)
3556 #if VMS_DEBUGGING_INFO
3557 case DW_AT_HP_prologue:
3558 return "DW_AT_HP_prologue";
3559 #else
3560 case DW_AT_MIPS_loop_unroll_factor:
3561 return "DW_AT_MIPS_loop_unroll_factor";
3562 #endif
3564 #if VMS_DEBUGGING_INFO
3565 case DW_AT_HP_epilogue:
3566 return "DW_AT_HP_epilogue";
3567 #else
3568 case DW_AT_MIPS_stride:
3569 return "DW_AT_MIPS_stride";
3570 #endif
3573 name = get_DW_AT_name (attr);
3575 if (name != NULL)
3576 return name;
3578 return "DW_AT_<unknown>";
3581 /* Convert a DWARF value form code into its string name. */
3583 static const char *
3584 dwarf_form_name (unsigned int form)
3586 const char *name = get_DW_FORM_name (form);
3588 if (name != NULL)
3589 return name;
3591 return "DW_FORM_<unknown>";
3594 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3595 instance of an inlined instance of a decl which is local to an inline
3596 function, so we have to trace all of the way back through the origin chain
3597 to find out what sort of node actually served as the original seed for the
3598 given block. */
3600 static tree
3601 decl_ultimate_origin (const_tree decl)
3603 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3604 return NULL_TREE;
3606 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3607 nodes in the function to point to themselves; ignore that if
3608 we're trying to output the abstract instance of this function. */
3609 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3610 return NULL_TREE;
3612 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3613 most distant ancestor, this should never happen. */
3614 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3616 return DECL_ABSTRACT_ORIGIN (decl);
3619 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3620 of a virtual function may refer to a base class, so we check the 'this'
3621 parameter. */
3623 static tree
3624 decl_class_context (tree decl)
3626 tree context = NULL_TREE;
3628 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3629 context = DECL_CONTEXT (decl);
3630 else
3631 context = TYPE_MAIN_VARIANT
3632 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3634 if (context && !TYPE_P (context))
3635 context = NULL_TREE;
3637 return context;
3640 /* Add an attribute/value pair to a DIE. */
3642 static inline void
3643 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3645 /* Maybe this should be an assert? */
3646 if (die == NULL)
3647 return;
3649 vec_safe_reserve (die->die_attr, 1);
3650 vec_safe_push (die->die_attr, *attr);
3653 static inline enum dw_val_class
3654 AT_class (dw_attr_ref a)
3656 return a->dw_attr_val.val_class;
3659 /* Return the index for any attribute that will be referenced with a
3660 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3661 are stored in dw_attr_val.v.val_str for reference counting
3662 pruning. */
3664 static inline unsigned int
3665 AT_index (dw_attr_ref a)
3667 if (AT_class (a) == dw_val_class_str)
3668 return a->dw_attr_val.v.val_str->index;
3669 else if (a->dw_attr_val.val_entry != NULL)
3670 return a->dw_attr_val.val_entry->index;
3671 return NOT_INDEXED;
3674 /* Add a flag value attribute to a DIE. */
3676 static inline void
3677 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3679 dw_attr_node attr;
3681 attr.dw_attr = attr_kind;
3682 attr.dw_attr_val.val_class = dw_val_class_flag;
3683 attr.dw_attr_val.val_entry = NULL;
3684 attr.dw_attr_val.v.val_flag = flag;
3685 add_dwarf_attr (die, &attr);
3688 static inline unsigned
3689 AT_flag (dw_attr_ref a)
3691 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3692 return a->dw_attr_val.v.val_flag;
3695 /* Add a signed integer attribute value to a DIE. */
3697 static inline void
3698 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3700 dw_attr_node attr;
3702 attr.dw_attr = attr_kind;
3703 attr.dw_attr_val.val_class = dw_val_class_const;
3704 attr.dw_attr_val.val_entry = NULL;
3705 attr.dw_attr_val.v.val_int = int_val;
3706 add_dwarf_attr (die, &attr);
3709 static inline HOST_WIDE_INT
3710 AT_int (dw_attr_ref a)
3712 gcc_assert (a && AT_class (a) == dw_val_class_const);
3713 return a->dw_attr_val.v.val_int;
3716 /* Add an unsigned integer attribute value to a DIE. */
3718 static inline void
3719 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3720 unsigned HOST_WIDE_INT unsigned_val)
3722 dw_attr_node attr;
3724 attr.dw_attr = attr_kind;
3725 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3726 attr.dw_attr_val.val_entry = NULL;
3727 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3728 add_dwarf_attr (die, &attr);
3731 static inline unsigned HOST_WIDE_INT
3732 AT_unsigned (dw_attr_ref a)
3734 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3735 return a->dw_attr_val.v.val_unsigned;
3738 /* Add an unsigned double integer attribute value to a DIE. */
3740 static inline void
3741 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3742 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3744 dw_attr_node attr;
3746 attr.dw_attr = attr_kind;
3747 attr.dw_attr_val.val_class = dw_val_class_const_double;
3748 attr.dw_attr_val.val_entry = NULL;
3749 attr.dw_attr_val.v.val_double.high = high;
3750 attr.dw_attr_val.v.val_double.low = low;
3751 add_dwarf_attr (die, &attr);
3754 /* Add a floating point attribute value to a DIE and return it. */
3756 static inline void
3757 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3758 unsigned int length, unsigned int elt_size, unsigned char *array)
3760 dw_attr_node attr;
3762 attr.dw_attr = attr_kind;
3763 attr.dw_attr_val.val_class = dw_val_class_vec;
3764 attr.dw_attr_val.val_entry = NULL;
3765 attr.dw_attr_val.v.val_vec.length = length;
3766 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3767 attr.dw_attr_val.v.val_vec.array = array;
3768 add_dwarf_attr (die, &attr);
3771 /* Add an 8-byte data attribute value to a DIE. */
3773 static inline void
3774 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3775 unsigned char data8[8])
3777 dw_attr_node attr;
3779 attr.dw_attr = attr_kind;
3780 attr.dw_attr_val.val_class = dw_val_class_data8;
3781 attr.dw_attr_val.val_entry = NULL;
3782 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3783 add_dwarf_attr (die, &attr);
3786 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3787 dwarf_split_debug_info, address attributes in dies destined for the
3788 final executable have force_direct set to avoid using indexed
3789 references. */
3791 static inline void
3792 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3793 bool force_direct)
3795 dw_attr_node attr;
3796 char * lbl_id;
3798 lbl_id = xstrdup (lbl_low);
3799 attr.dw_attr = DW_AT_low_pc;
3800 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3801 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3802 if (dwarf_split_debug_info && !force_direct)
3803 attr.dw_attr_val.val_entry
3804 = add_addr_table_entry (lbl_id, ate_kind_label);
3805 else
3806 attr.dw_attr_val.val_entry = NULL;
3807 add_dwarf_attr (die, &attr);
3809 attr.dw_attr = DW_AT_high_pc;
3810 if (dwarf_version < 4)
3811 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3812 else
3813 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3814 lbl_id = xstrdup (lbl_high);
3815 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3816 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3817 && dwarf_split_debug_info && !force_direct)
3818 attr.dw_attr_val.val_entry
3819 = add_addr_table_entry (lbl_id, ate_kind_label);
3820 else
3821 attr.dw_attr_val.val_entry = NULL;
3822 add_dwarf_attr (die, &attr);
3825 /* Hash and equality functions for debug_str_hash. */
3827 static hashval_t
3828 debug_str_do_hash (const void *x)
3830 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3833 static int
3834 debug_str_eq (const void *x1, const void *x2)
3836 return strcmp ((((const struct indirect_string_node *)x1)->str),
3837 (const char *)x2) == 0;
3840 /* Add STR to the given string hash table. */
3842 static struct indirect_string_node *
3843 find_AT_string_in_table (const char *str, htab_t table)
3845 struct indirect_string_node *node;
3846 void **slot;
3848 slot = htab_find_slot_with_hash (table, str,
3849 htab_hash_string (str), INSERT);
3850 if (*slot == NULL)
3852 node = ggc_alloc_cleared_indirect_string_node ();
3853 node->str = ggc_strdup (str);
3854 *slot = node;
3856 else
3857 node = (struct indirect_string_node *) *slot;
3859 node->refcount++;
3860 return node;
3863 /* Add STR to the indirect string hash table. */
3865 static struct indirect_string_node *
3866 find_AT_string (const char *str)
3868 if (! debug_str_hash)
3869 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3870 debug_str_eq, NULL);
3872 return find_AT_string_in_table (str, debug_str_hash);
3875 /* Add a string attribute value to a DIE. */
3877 static inline void
3878 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3880 dw_attr_node attr;
3881 struct indirect_string_node *node;
3883 node = find_AT_string (str);
3885 attr.dw_attr = attr_kind;
3886 attr.dw_attr_val.val_class = dw_val_class_str;
3887 attr.dw_attr_val.val_entry = NULL;
3888 attr.dw_attr_val.v.val_str = node;
3889 add_dwarf_attr (die, &attr);
3892 static inline const char *
3893 AT_string (dw_attr_ref a)
3895 gcc_assert (a && AT_class (a) == dw_val_class_str);
3896 return a->dw_attr_val.v.val_str->str;
3899 /* Call this function directly to bypass AT_string_form's logic to put
3900 the string inline in the die. */
3902 static void
3903 set_indirect_string (struct indirect_string_node *node)
3905 char label[32];
3906 /* Already indirect is a no op. */
3907 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3909 gcc_assert (node->label);
3910 return;
3912 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3913 ++dw2_string_counter;
3914 node->label = xstrdup (label);
3916 if (!dwarf_split_debug_info)
3918 node->form = DW_FORM_strp;
3919 node->index = NOT_INDEXED;
3921 else
3923 node->form = DW_FORM_GNU_str_index;
3924 node->index = NO_INDEX_ASSIGNED;
3928 /* Find out whether a string should be output inline in DIE
3929 or out-of-line in .debug_str section. */
3931 static enum dwarf_form
3932 find_string_form (struct indirect_string_node *node)
3934 unsigned int len;
3936 if (node->form)
3937 return node->form;
3939 len = strlen (node->str) + 1;
3941 /* If the string is shorter or equal to the size of the reference, it is
3942 always better to put it inline. */
3943 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3944 return node->form = DW_FORM_string;
3946 /* If we cannot expect the linker to merge strings in .debug_str
3947 section, only put it into .debug_str if it is worth even in this
3948 single module. */
3949 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3950 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3951 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3952 return node->form = DW_FORM_string;
3954 set_indirect_string (node);
3956 return node->form;
3959 /* Find out whether the string referenced from the attribute should be
3960 output inline in DIE or out-of-line in .debug_str section. */
3962 static enum dwarf_form
3963 AT_string_form (dw_attr_ref a)
3965 gcc_assert (a && AT_class (a) == dw_val_class_str);
3966 return find_string_form (a->dw_attr_val.v.val_str);
3969 /* Add a DIE reference attribute value to a DIE. */
3971 static inline void
3972 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3974 dw_attr_node attr;
3976 #ifdef ENABLE_CHECKING
3977 gcc_assert (targ_die != NULL);
3978 #else
3979 /* With LTO we can end up trying to reference something we didn't create
3980 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3981 if (targ_die == NULL)
3982 return;
3983 #endif
3985 attr.dw_attr = attr_kind;
3986 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3987 attr.dw_attr_val.val_entry = NULL;
3988 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3989 attr.dw_attr_val.v.val_die_ref.external = 0;
3990 add_dwarf_attr (die, &attr);
3993 /* Change DIE reference REF to point to NEW_DIE instead. */
3995 static inline void
3996 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3998 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3999 ref->dw_attr_val.v.val_die_ref.die = new_die;
4000 ref->dw_attr_val.v.val_die_ref.external = 0;
4003 /* Add an AT_specification attribute to a DIE, and also make the back
4004 pointer from the specification to the definition. */
4006 static inline void
4007 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4009 add_AT_die_ref (die, DW_AT_specification, targ_die);
4010 gcc_assert (!targ_die->die_definition);
4011 targ_die->die_definition = die;
4014 static inline dw_die_ref
4015 AT_ref (dw_attr_ref a)
4017 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4018 return a->dw_attr_val.v.val_die_ref.die;
4021 static inline int
4022 AT_ref_external (dw_attr_ref a)
4024 if (a && AT_class (a) == dw_val_class_die_ref)
4025 return a->dw_attr_val.v.val_die_ref.external;
4027 return 0;
4030 static inline void
4031 set_AT_ref_external (dw_attr_ref a, int i)
4033 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4034 a->dw_attr_val.v.val_die_ref.external = i;
4037 /* Add an FDE reference attribute value to a DIE. */
4039 static inline void
4040 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4042 dw_attr_node attr;
4044 attr.dw_attr = attr_kind;
4045 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4046 attr.dw_attr_val.val_entry = NULL;
4047 attr.dw_attr_val.v.val_fde_index = targ_fde;
4048 add_dwarf_attr (die, &attr);
4051 /* Add a location description attribute value to a DIE. */
4053 static inline void
4054 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4056 dw_attr_node attr;
4058 attr.dw_attr = attr_kind;
4059 attr.dw_attr_val.val_class = dw_val_class_loc;
4060 attr.dw_attr_val.val_entry = NULL;
4061 attr.dw_attr_val.v.val_loc = loc;
4062 add_dwarf_attr (die, &attr);
4065 static inline dw_loc_descr_ref
4066 AT_loc (dw_attr_ref a)
4068 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4069 return a->dw_attr_val.v.val_loc;
4072 static inline void
4073 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4075 dw_attr_node attr;
4077 attr.dw_attr = attr_kind;
4078 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4079 attr.dw_attr_val.val_entry = NULL;
4080 attr.dw_attr_val.v.val_loc_list = loc_list;
4081 add_dwarf_attr (die, &attr);
4082 have_location_lists = true;
4085 static inline dw_loc_list_ref
4086 AT_loc_list (dw_attr_ref a)
4088 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4089 return a->dw_attr_val.v.val_loc_list;
4092 static inline dw_loc_list_ref *
4093 AT_loc_list_ptr (dw_attr_ref a)
4095 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4096 return &a->dw_attr_val.v.val_loc_list;
4099 /* Table of entries into the .debug_addr section. */
4101 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4103 /* Hash an address_table_entry. */
4105 static hashval_t
4106 addr_table_entry_do_hash (const void *x)
4108 const addr_table_entry *a = (const addr_table_entry *) x;
4109 switch (a->kind)
4111 case ate_kind_rtx:
4112 return iterative_hash_rtx (a->addr.rtl, 0);
4113 case ate_kind_rtx_dtprel:
4114 return iterative_hash_rtx (a->addr.rtl, 1);
4115 case ate_kind_label:
4116 return htab_hash_string (a->addr.label);
4117 default:
4118 gcc_unreachable ();
4122 /* Determine equality for two address_table_entries. */
4124 static int
4125 addr_table_entry_eq (const void *x1, const void *x2)
4127 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4128 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4130 if (a1->kind != a2->kind)
4131 return 0;
4132 switch (a1->kind)
4134 case ate_kind_rtx:
4135 case ate_kind_rtx_dtprel:
4136 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4137 case ate_kind_label:
4138 return strcmp (a1->addr.label, a2->addr.label) == 0;
4139 default:
4140 gcc_unreachable ();
4144 /* Initialize an addr_table_entry. */
4146 void
4147 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4149 e->kind = kind;
4150 switch (kind)
4152 case ate_kind_rtx:
4153 case ate_kind_rtx_dtprel:
4154 e->addr.rtl = (rtx) addr;
4155 break;
4156 case ate_kind_label:
4157 e->addr.label = (char *) addr;
4158 break;
4160 e->refcount = 0;
4161 e->index = NO_INDEX_ASSIGNED;
4164 /* Add attr to the address table entry to the table. Defer setting an
4165 index until output time. */
4167 static addr_table_entry *
4168 add_addr_table_entry (void *addr, enum ate_kind kind)
4170 addr_table_entry *node;
4171 addr_table_entry finder;
4172 void **slot;
4174 gcc_assert (dwarf_split_debug_info);
4175 if (! addr_index_table)
4176 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4177 addr_table_entry_eq, NULL);
4178 init_addr_table_entry (&finder, kind, addr);
4179 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4181 if (*slot == HTAB_EMPTY_ENTRY)
4183 node = ggc_alloc_cleared_addr_table_entry ();
4184 init_addr_table_entry (node, kind, addr);
4185 *slot = node;
4187 else
4188 node = (addr_table_entry *) *slot;
4190 node->refcount++;
4191 return node;
4194 /* Remove an entry from the addr table by decrementing its refcount.
4195 Strictly, decrementing the refcount would be enough, but the
4196 assertion that the entry is actually in the table has found
4197 bugs. */
4199 static void
4200 remove_addr_table_entry (addr_table_entry *entry)
4202 addr_table_entry *node;
4204 gcc_assert (dwarf_split_debug_info && addr_index_table);
4205 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4206 /* After an index is assigned, the table is frozen. */
4207 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4208 node->refcount--;
4211 /* Given a location list, remove all addresses it refers to from the
4212 address_table. */
4214 static void
4215 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4217 for (; descr; descr = descr->dw_loc_next)
4218 if (descr->dw_loc_oprnd1.val_entry != NULL)
4220 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4221 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4225 /* A helper function for dwarf2out_finish called through
4226 htab_traverse. Assign an addr_table_entry its index. All entries
4227 must be collected into the table when this function is called,
4228 because the indexing code relies on htab_traverse to traverse nodes
4229 in the same order for each run. */
4231 static int
4232 index_addr_table_entry (void **h, void *v)
4234 addr_table_entry *node = (addr_table_entry *) *h;
4235 unsigned int *index = (unsigned int *) v;
4237 /* Don't index unreferenced nodes. */
4238 if (node->refcount == 0)
4239 return 1;
4241 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4242 node->index = *index;
4243 *index += 1;
4245 return 1;
4248 /* Add an address constant attribute value to a DIE. When using
4249 dwarf_split_debug_info, address attributes in dies destined for the
4250 final executable should be direct references--setting the parameter
4251 force_direct ensures this behavior. */
4253 static inline void
4254 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4255 bool force_direct)
4257 dw_attr_node attr;
4259 attr.dw_attr = attr_kind;
4260 attr.dw_attr_val.val_class = dw_val_class_addr;
4261 attr.dw_attr_val.v.val_addr = addr;
4262 if (dwarf_split_debug_info && !force_direct)
4263 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4264 else
4265 attr.dw_attr_val.val_entry = NULL;
4266 add_dwarf_attr (die, &attr);
4269 /* Get the RTX from to an address DIE attribute. */
4271 static inline rtx
4272 AT_addr (dw_attr_ref a)
4274 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4275 return a->dw_attr_val.v.val_addr;
4278 /* Add a file attribute value to a DIE. */
4280 static inline void
4281 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4282 struct dwarf_file_data *fd)
4284 dw_attr_node attr;
4286 attr.dw_attr = attr_kind;
4287 attr.dw_attr_val.val_class = dw_val_class_file;
4288 attr.dw_attr_val.val_entry = NULL;
4289 attr.dw_attr_val.v.val_file = fd;
4290 add_dwarf_attr (die, &attr);
4293 /* Get the dwarf_file_data from a file DIE attribute. */
4295 static inline struct dwarf_file_data *
4296 AT_file (dw_attr_ref a)
4298 gcc_assert (a && AT_class (a) == dw_val_class_file);
4299 return a->dw_attr_val.v.val_file;
4302 /* Add a vms delta attribute value to a DIE. */
4304 static inline void
4305 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4306 const char *lbl1, const char *lbl2)
4308 dw_attr_node attr;
4310 attr.dw_attr = attr_kind;
4311 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4312 attr.dw_attr_val.val_entry = NULL;
4313 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4314 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4315 add_dwarf_attr (die, &attr);
4318 /* Add a label identifier attribute value to a DIE. */
4320 static inline void
4321 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4322 const char *lbl_id)
4324 dw_attr_node attr;
4326 attr.dw_attr = attr_kind;
4327 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4328 attr.dw_attr_val.val_entry = NULL;
4329 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4330 if (dwarf_split_debug_info)
4331 attr.dw_attr_val.val_entry
4332 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4333 ate_kind_label);
4334 add_dwarf_attr (die, &attr);
4337 /* Add a section offset attribute value to a DIE, an offset into the
4338 debug_line section. */
4340 static inline void
4341 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4342 const char *label)
4344 dw_attr_node attr;
4346 attr.dw_attr = attr_kind;
4347 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4348 attr.dw_attr_val.val_entry = NULL;
4349 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4350 add_dwarf_attr (die, &attr);
4353 /* Add a section offset attribute value to a DIE, an offset into the
4354 debug_macinfo section. */
4356 static inline void
4357 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4358 const char *label)
4360 dw_attr_node attr;
4362 attr.dw_attr = attr_kind;
4363 attr.dw_attr_val.val_class = dw_val_class_macptr;
4364 attr.dw_attr_val.val_entry = NULL;
4365 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4366 add_dwarf_attr (die, &attr);
4369 /* Add an offset attribute value to a DIE. */
4371 static inline void
4372 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4373 unsigned HOST_WIDE_INT offset)
4375 dw_attr_node attr;
4377 attr.dw_attr = attr_kind;
4378 attr.dw_attr_val.val_class = dw_val_class_offset;
4379 attr.dw_attr_val.val_entry = NULL;
4380 attr.dw_attr_val.v.val_offset = offset;
4381 add_dwarf_attr (die, &attr);
4384 /* Add a range_list attribute value to a DIE. When using
4385 dwarf_split_debug_info, address attributes in dies destined for the
4386 final executable should be direct references--setting the parameter
4387 force_direct ensures this behavior. */
4389 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4390 #define RELOCATED_OFFSET (NULL)
4392 static void
4393 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4394 long unsigned int offset, bool force_direct)
4396 dw_attr_node attr;
4398 attr.dw_attr = attr_kind;
4399 attr.dw_attr_val.val_class = dw_val_class_range_list;
4400 /* For the range_list attribute, use val_entry to store whether the
4401 offset should follow split-debug-info or normal semantics. This
4402 value is read in output_range_list_offset. */
4403 if (dwarf_split_debug_info && !force_direct)
4404 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4405 else
4406 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4407 attr.dw_attr_val.v.val_offset = offset;
4408 add_dwarf_attr (die, &attr);
4411 /* Return the start label of a delta attribute. */
4413 static inline const char *
4414 AT_vms_delta1 (dw_attr_ref a)
4416 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4417 return a->dw_attr_val.v.val_vms_delta.lbl1;
4420 /* Return the end label of a delta attribute. */
4422 static inline const char *
4423 AT_vms_delta2 (dw_attr_ref a)
4425 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4426 return a->dw_attr_val.v.val_vms_delta.lbl2;
4429 static inline const char *
4430 AT_lbl (dw_attr_ref a)
4432 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4433 || AT_class (a) == dw_val_class_lineptr
4434 || AT_class (a) == dw_val_class_macptr
4435 || AT_class (a) == dw_val_class_high_pc));
4436 return a->dw_attr_val.v.val_lbl_id;
4439 /* Get the attribute of type attr_kind. */
4441 static dw_attr_ref
4442 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4444 dw_attr_ref a;
4445 unsigned ix;
4446 dw_die_ref spec = NULL;
4448 if (! die)
4449 return NULL;
4451 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4452 if (a->dw_attr == attr_kind)
4453 return a;
4454 else if (a->dw_attr == DW_AT_specification
4455 || a->dw_attr == DW_AT_abstract_origin)
4456 spec = AT_ref (a);
4458 if (spec)
4459 return get_AT (spec, attr_kind);
4461 return NULL;
4464 /* Returns the parent of the declaration of DIE. */
4466 static dw_die_ref
4467 get_die_parent (dw_die_ref die)
4469 dw_die_ref t;
4471 if (!die)
4472 return NULL;
4474 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4475 || (t = get_AT_ref (die, DW_AT_specification)))
4476 die = t;
4478 return die->die_parent;
4481 /* Return the "low pc" attribute value, typically associated with a subprogram
4482 DIE. Return null if the "low pc" attribute is either not present, or if it
4483 cannot be represented as an assembler label identifier. */
4485 static inline const char *
4486 get_AT_low_pc (dw_die_ref die)
4488 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4490 return a ? AT_lbl (a) : NULL;
4493 /* Return the "high pc" attribute value, typically associated with a subprogram
4494 DIE. Return null if the "high pc" attribute is either not present, or if it
4495 cannot be represented as an assembler label identifier. */
4497 static inline const char *
4498 get_AT_hi_pc (dw_die_ref die)
4500 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4502 return a ? AT_lbl (a) : NULL;
4505 /* Return the value of the string attribute designated by ATTR_KIND, or
4506 NULL if it is not present. */
4508 static inline const char *
4509 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4511 dw_attr_ref a = get_AT (die, attr_kind);
4513 return a ? AT_string (a) : NULL;
4516 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4517 if it is not present. */
4519 static inline int
4520 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4522 dw_attr_ref a = get_AT (die, attr_kind);
4524 return a ? AT_flag (a) : 0;
4527 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4528 if it is not present. */
4530 static inline unsigned
4531 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4533 dw_attr_ref a = get_AT (die, attr_kind);
4535 return a ? AT_unsigned (a) : 0;
4538 static inline dw_die_ref
4539 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4541 dw_attr_ref a = get_AT (die, attr_kind);
4543 return a ? AT_ref (a) : NULL;
4546 static inline struct dwarf_file_data *
4547 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4549 dw_attr_ref a = get_AT (die, attr_kind);
4551 return a ? AT_file (a) : NULL;
4554 /* Return TRUE if the language is C++. */
4556 static inline bool
4557 is_cxx (void)
4559 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4561 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4564 /* Return TRUE if the language is Fortran. */
4566 static inline bool
4567 is_fortran (void)
4569 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4571 return (lang == DW_LANG_Fortran77
4572 || lang == DW_LANG_Fortran90
4573 || lang == DW_LANG_Fortran95);
4576 /* Return TRUE if the language is Ada. */
4578 static inline bool
4579 is_ada (void)
4581 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4583 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4586 /* Remove the specified attribute if present. */
4588 static void
4589 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4591 dw_attr_ref a;
4592 unsigned ix;
4594 if (! die)
4595 return;
4597 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4598 if (a->dw_attr == attr_kind)
4600 if (AT_class (a) == dw_val_class_str)
4601 if (a->dw_attr_val.v.val_str->refcount)
4602 a->dw_attr_val.v.val_str->refcount--;
4604 /* vec::ordered_remove should help reduce the number of abbrevs
4605 that are needed. */
4606 die->die_attr->ordered_remove (ix);
4607 return;
4611 /* Remove CHILD from its parent. PREV must have the property that
4612 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4614 static void
4615 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4617 gcc_assert (child->die_parent == prev->die_parent);
4618 gcc_assert (prev->die_sib == child);
4619 if (prev == child)
4621 gcc_assert (child->die_parent->die_child == child);
4622 prev = NULL;
4624 else
4625 prev->die_sib = child->die_sib;
4626 if (child->die_parent->die_child == child)
4627 child->die_parent->die_child = prev;
4630 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4631 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4633 static void
4634 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4636 dw_die_ref parent = old_child->die_parent;
4638 gcc_assert (parent == prev->die_parent);
4639 gcc_assert (prev->die_sib == old_child);
4641 new_child->die_parent = parent;
4642 if (prev == old_child)
4644 gcc_assert (parent->die_child == old_child);
4645 new_child->die_sib = new_child;
4647 else
4649 prev->die_sib = new_child;
4650 new_child->die_sib = old_child->die_sib;
4652 if (old_child->die_parent->die_child == old_child)
4653 old_child->die_parent->die_child = new_child;
4656 /* Move all children from OLD_PARENT to NEW_PARENT. */
4658 static void
4659 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4661 dw_die_ref c;
4662 new_parent->die_child = old_parent->die_child;
4663 old_parent->die_child = NULL;
4664 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4667 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4668 matches TAG. */
4670 static void
4671 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4673 dw_die_ref c;
4675 c = die->die_child;
4676 if (c) do {
4677 dw_die_ref prev = c;
4678 c = c->die_sib;
4679 while (c->die_tag == tag)
4681 remove_child_with_prev (c, prev);
4682 /* Might have removed every child. */
4683 if (c == c->die_sib)
4684 return;
4685 c = c->die_sib;
4687 } while (c != die->die_child);
4690 /* Add a CHILD_DIE as the last child of DIE. */
4692 static void
4693 add_child_die (dw_die_ref die, dw_die_ref child_die)
4695 /* FIXME this should probably be an assert. */
4696 if (! die || ! child_die)
4697 return;
4698 gcc_assert (die != child_die);
4700 child_die->die_parent = die;
4701 if (die->die_child)
4703 child_die->die_sib = die->die_child->die_sib;
4704 die->die_child->die_sib = child_die;
4706 else
4707 child_die->die_sib = child_die;
4708 die->die_child = child_die;
4711 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4712 is the specification, to the end of PARENT's list of children.
4713 This is done by removing and re-adding it. */
4715 static void
4716 splice_child_die (dw_die_ref parent, dw_die_ref child)
4718 dw_die_ref p;
4720 /* We want the declaration DIE from inside the class, not the
4721 specification DIE at toplevel. */
4722 if (child->die_parent != parent)
4724 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4726 if (tmp)
4727 child = tmp;
4730 gcc_assert (child->die_parent == parent
4731 || (child->die_parent
4732 == get_AT_ref (parent, DW_AT_specification)));
4734 for (p = child->die_parent->die_child; ; p = p->die_sib)
4735 if (p->die_sib == child)
4737 remove_child_with_prev (child, p);
4738 break;
4741 add_child_die (parent, child);
4744 /* Return a pointer to a newly created DIE node. */
4746 static inline dw_die_ref
4747 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4749 dw_die_ref die = ggc_alloc_cleared_die_node ();
4751 die->die_tag = tag_value;
4753 if (parent_die != NULL)
4754 add_child_die (parent_die, die);
4755 else
4757 limbo_die_node *limbo_node;
4759 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4760 limbo_node->die = die;
4761 limbo_node->created_for = t;
4762 limbo_node->next = limbo_die_list;
4763 limbo_die_list = limbo_node;
4766 return die;
4769 /* Return the DIE associated with the given type specifier. */
4771 static inline dw_die_ref
4772 lookup_type_die (tree type)
4774 return TYPE_SYMTAB_DIE (type);
4777 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4778 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4779 anonymous type instead the one of the naming typedef. */
4781 static inline dw_die_ref
4782 strip_naming_typedef (tree type, dw_die_ref type_die)
4784 if (type
4785 && TREE_CODE (type) == RECORD_TYPE
4786 && type_die
4787 && type_die->die_tag == DW_TAG_typedef
4788 && is_naming_typedef_decl (TYPE_NAME (type)))
4789 type_die = get_AT_ref (type_die, DW_AT_type);
4790 return type_die;
4793 /* Like lookup_type_die, but if type is an anonymous type named by a
4794 typedef[1], return the DIE of the anonymous type instead the one of
4795 the naming typedef. This is because in gen_typedef_die, we did
4796 equate the anonymous struct named by the typedef with the DIE of
4797 the naming typedef. So by default, lookup_type_die on an anonymous
4798 struct yields the DIE of the naming typedef.
4800 [1]: Read the comment of is_naming_typedef_decl to learn about what
4801 a naming typedef is. */
4803 static inline dw_die_ref
4804 lookup_type_die_strip_naming_typedef (tree type)
4806 dw_die_ref die = lookup_type_die (type);
4807 return strip_naming_typedef (type, die);
4810 /* Equate a DIE to a given type specifier. */
4812 static inline void
4813 equate_type_number_to_die (tree type, dw_die_ref type_die)
4815 TYPE_SYMTAB_DIE (type) = type_die;
4818 /* Returns a hash value for X (which really is a die_struct). */
4820 static hashval_t
4821 decl_die_table_hash (const void *x)
4823 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4826 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4828 static int
4829 decl_die_table_eq (const void *x, const void *y)
4831 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4834 /* Return the DIE associated with a given declaration. */
4836 static inline dw_die_ref
4837 lookup_decl_die (tree decl)
4839 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4842 /* Returns a hash value for X (which really is a var_loc_list). */
4844 static hashval_t
4845 decl_loc_table_hash (const void *x)
4847 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4850 /* Return nonzero if decl_id of var_loc_list X is the same as
4851 UID of decl *Y. */
4853 static int
4854 decl_loc_table_eq (const void *x, const void *y)
4856 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4859 /* Return the var_loc list associated with a given declaration. */
4861 static inline var_loc_list *
4862 lookup_decl_loc (const_tree decl)
4864 if (!decl_loc_table)
4865 return NULL;
4866 return (var_loc_list *)
4867 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4870 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4872 static hashval_t
4873 cached_dw_loc_list_table_hash (const void *x)
4875 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4878 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4879 UID of decl *Y. */
4881 static int
4882 cached_dw_loc_list_table_eq (const void *x, const void *y)
4884 return (((const cached_dw_loc_list *) x)->decl_id
4885 == DECL_UID ((const_tree) y));
4888 /* Equate a DIE to a particular declaration. */
4890 static void
4891 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4893 unsigned int decl_id = DECL_UID (decl);
4894 void **slot;
4896 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4897 *slot = decl_die;
4898 decl_die->decl_id = decl_id;
4901 /* Return how many bits covers PIECE EXPR_LIST. */
4903 static int
4904 decl_piece_bitsize (rtx piece)
4906 int ret = (int) GET_MODE (piece);
4907 if (ret)
4908 return ret;
4909 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4910 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4911 return INTVAL (XEXP (XEXP (piece, 0), 0));
4914 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4916 static rtx *
4917 decl_piece_varloc_ptr (rtx piece)
4919 if ((int) GET_MODE (piece))
4920 return &XEXP (piece, 0);
4921 else
4922 return &XEXP (XEXP (piece, 0), 1);
4925 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4926 Next is the chain of following piece nodes. */
4928 static rtx
4929 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4931 if (bitsize <= (int) MAX_MACHINE_MODE)
4932 return alloc_EXPR_LIST (bitsize, loc_note, next);
4933 else
4934 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4935 GEN_INT (bitsize),
4936 loc_note), next);
4939 /* Return rtx that should be stored into loc field for
4940 LOC_NOTE and BITPOS/BITSIZE. */
4942 static rtx
4943 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4944 HOST_WIDE_INT bitsize)
4946 if (bitsize != -1)
4948 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4949 if (bitpos != 0)
4950 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4952 return loc_note;
4955 /* This function either modifies location piece list *DEST in
4956 place (if SRC and INNER is NULL), or copies location piece list
4957 *SRC to *DEST while modifying it. Location BITPOS is modified
4958 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4959 not copied and if needed some padding around it is added.
4960 When modifying in place, DEST should point to EXPR_LIST where
4961 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4962 to the start of the whole list and INNER points to the EXPR_LIST
4963 where earlier pieces cover PIECE_BITPOS bits. */
4965 static void
4966 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4967 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4968 HOST_WIDE_INT bitsize, rtx loc_note)
4970 int diff;
4971 bool copy = inner != NULL;
4973 if (copy)
4975 /* First copy all nodes preceding the current bitpos. */
4976 while (src != inner)
4978 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4979 decl_piece_bitsize (*src), NULL_RTX);
4980 dest = &XEXP (*dest, 1);
4981 src = &XEXP (*src, 1);
4984 /* Add padding if needed. */
4985 if (bitpos != piece_bitpos)
4987 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4988 copy ? NULL_RTX : *dest);
4989 dest = &XEXP (*dest, 1);
4991 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4993 gcc_assert (!copy);
4994 /* A piece with correct bitpos and bitsize already exist,
4995 just update the location for it and return. */
4996 *decl_piece_varloc_ptr (*dest) = loc_note;
4997 return;
4999 /* Add the piece that changed. */
5000 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5001 dest = &XEXP (*dest, 1);
5002 /* Skip over pieces that overlap it. */
5003 diff = bitpos - piece_bitpos + bitsize;
5004 if (!copy)
5005 src = dest;
5006 while (diff > 0 && *src)
5008 rtx piece = *src;
5009 diff -= decl_piece_bitsize (piece);
5010 if (copy)
5011 src = &XEXP (piece, 1);
5012 else
5014 *src = XEXP (piece, 1);
5015 free_EXPR_LIST_node (piece);
5018 /* Add padding if needed. */
5019 if (diff < 0 && *src)
5021 if (!copy)
5022 dest = src;
5023 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5024 dest = &XEXP (*dest, 1);
5026 if (!copy)
5027 return;
5028 /* Finally copy all nodes following it. */
5029 while (*src)
5031 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5032 decl_piece_bitsize (*src), NULL_RTX);
5033 dest = &XEXP (*dest, 1);
5034 src = &XEXP (*src, 1);
5038 /* Add a variable location node to the linked list for DECL. */
5040 static struct var_loc_node *
5041 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5043 unsigned int decl_id;
5044 var_loc_list *temp;
5045 void **slot;
5046 struct var_loc_node *loc = NULL;
5047 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5049 if (TREE_CODE (decl) == VAR_DECL
5050 && DECL_HAS_DEBUG_EXPR_P (decl))
5052 tree realdecl = DECL_DEBUG_EXPR (decl);
5053 if (handled_component_p (realdecl)
5054 || (TREE_CODE (realdecl) == MEM_REF
5055 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5057 HOST_WIDE_INT maxsize;
5058 tree innerdecl;
5059 innerdecl
5060 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5061 if (!DECL_P (innerdecl)
5062 || DECL_IGNORED_P (innerdecl)
5063 || TREE_STATIC (innerdecl)
5064 || bitsize <= 0
5065 || bitpos + bitsize > 256
5066 || bitsize != maxsize)
5067 return NULL;
5068 decl = innerdecl;
5072 decl_id = DECL_UID (decl);
5073 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5074 if (*slot == NULL)
5076 temp = ggc_alloc_cleared_var_loc_list ();
5077 temp->decl_id = decl_id;
5078 *slot = temp;
5080 else
5081 temp = (var_loc_list *) *slot;
5083 /* For PARM_DECLs try to keep around the original incoming value,
5084 even if that means we'll emit a zero-range .debug_loc entry. */
5085 if (temp->last
5086 && temp->first == temp->last
5087 && TREE_CODE (decl) == PARM_DECL
5088 && NOTE_P (temp->first->loc)
5089 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5090 && DECL_INCOMING_RTL (decl)
5091 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5092 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5093 == GET_CODE (DECL_INCOMING_RTL (decl))
5094 && prev_real_insn (temp->first->loc) == NULL_RTX
5095 && (bitsize != -1
5096 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5097 NOTE_VAR_LOCATION_LOC (loc_note))
5098 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5099 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5101 loc = ggc_alloc_cleared_var_loc_node ();
5102 temp->first->next = loc;
5103 temp->last = loc;
5104 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5106 else if (temp->last)
5108 struct var_loc_node *last = temp->last, *unused = NULL;
5109 rtx *piece_loc = NULL, last_loc_note;
5110 int piece_bitpos = 0;
5111 if (last->next)
5113 last = last->next;
5114 gcc_assert (last->next == NULL);
5116 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5118 piece_loc = &last->loc;
5121 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5122 if (piece_bitpos + cur_bitsize > bitpos)
5123 break;
5124 piece_bitpos += cur_bitsize;
5125 piece_loc = &XEXP (*piece_loc, 1);
5127 while (*piece_loc);
5129 /* TEMP->LAST here is either pointer to the last but one or
5130 last element in the chained list, LAST is pointer to the
5131 last element. */
5132 if (label && strcmp (last->label, label) == 0)
5134 /* For SRA optimized variables if there weren't any real
5135 insns since last note, just modify the last node. */
5136 if (piece_loc != NULL)
5138 adjust_piece_list (piece_loc, NULL, NULL,
5139 bitpos, piece_bitpos, bitsize, loc_note);
5140 return NULL;
5142 /* If the last note doesn't cover any instructions, remove it. */
5143 if (temp->last != last)
5145 temp->last->next = NULL;
5146 unused = last;
5147 last = temp->last;
5148 gcc_assert (strcmp (last->label, label) != 0);
5150 else
5152 gcc_assert (temp->first == temp->last
5153 || (temp->first->next == temp->last
5154 && TREE_CODE (decl) == PARM_DECL));
5155 memset (temp->last, '\0', sizeof (*temp->last));
5156 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5157 return temp->last;
5160 if (bitsize == -1 && NOTE_P (last->loc))
5161 last_loc_note = last->loc;
5162 else if (piece_loc != NULL
5163 && *piece_loc != NULL_RTX
5164 && piece_bitpos == bitpos
5165 && decl_piece_bitsize (*piece_loc) == bitsize)
5166 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5167 else
5168 last_loc_note = NULL_RTX;
5169 /* If the current location is the same as the end of the list,
5170 and either both or neither of the locations is uninitialized,
5171 we have nothing to do. */
5172 if (last_loc_note == NULL_RTX
5173 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5174 NOTE_VAR_LOCATION_LOC (loc_note)))
5175 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5176 != NOTE_VAR_LOCATION_STATUS (loc_note))
5177 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5178 == VAR_INIT_STATUS_UNINITIALIZED)
5179 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5180 == VAR_INIT_STATUS_UNINITIALIZED))))
5182 /* Add LOC to the end of list and update LAST. If the last
5183 element of the list has been removed above, reuse its
5184 memory for the new node, otherwise allocate a new one. */
5185 if (unused)
5187 loc = unused;
5188 memset (loc, '\0', sizeof (*loc));
5190 else
5191 loc = ggc_alloc_cleared_var_loc_node ();
5192 if (bitsize == -1 || piece_loc == NULL)
5193 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5194 else
5195 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5196 bitpos, piece_bitpos, bitsize, loc_note);
5197 last->next = loc;
5198 /* Ensure TEMP->LAST will point either to the new last but one
5199 element of the chain, or to the last element in it. */
5200 if (last != temp->last)
5201 temp->last = last;
5203 else if (unused)
5204 ggc_free (unused);
5206 else
5208 loc = ggc_alloc_cleared_var_loc_node ();
5209 temp->first = loc;
5210 temp->last = loc;
5211 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5213 return loc;
5216 /* Keep track of the number of spaces used to indent the
5217 output of the debugging routines that print the structure of
5218 the DIE internal representation. */
5219 static int print_indent;
5221 /* Indent the line the number of spaces given by print_indent. */
5223 static inline void
5224 print_spaces (FILE *outfile)
5226 fprintf (outfile, "%*s", print_indent, "");
5229 /* Print a type signature in hex. */
5231 static inline void
5232 print_signature (FILE *outfile, char *sig)
5234 int i;
5236 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5237 fprintf (outfile, "%02x", sig[i] & 0xff);
5240 /* Print the information associated with a given DIE, and its children.
5241 This routine is a debugging aid only. */
5243 static void
5244 print_die (dw_die_ref die, FILE *outfile)
5246 dw_attr_ref a;
5247 dw_die_ref c;
5248 unsigned ix;
5250 print_spaces (outfile);
5251 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5252 die->die_offset, dwarf_tag_name (die->die_tag),
5253 (void*) die);
5254 print_spaces (outfile);
5255 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5256 fprintf (outfile, " offset: %ld", die->die_offset);
5257 fprintf (outfile, " mark: %d\n", die->die_mark);
5259 if (die->comdat_type_p)
5261 print_spaces (outfile);
5262 fprintf (outfile, " signature: ");
5263 print_signature (outfile, die->die_id.die_type_node->signature);
5264 fprintf (outfile, "\n");
5267 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5269 print_spaces (outfile);
5270 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5272 switch (AT_class (a))
5274 case dw_val_class_addr:
5275 fprintf (outfile, "address");
5276 break;
5277 case dw_val_class_offset:
5278 fprintf (outfile, "offset");
5279 break;
5280 case dw_val_class_loc:
5281 fprintf (outfile, "location descriptor");
5282 break;
5283 case dw_val_class_loc_list:
5284 fprintf (outfile, "location list -> label:%s",
5285 AT_loc_list (a)->ll_symbol);
5286 break;
5287 case dw_val_class_range_list:
5288 fprintf (outfile, "range list");
5289 break;
5290 case dw_val_class_const:
5291 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5292 break;
5293 case dw_val_class_unsigned_const:
5294 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5295 break;
5296 case dw_val_class_const_double:
5297 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5298 HOST_WIDE_INT_PRINT_UNSIGNED")",
5299 a->dw_attr_val.v.val_double.high,
5300 a->dw_attr_val.v.val_double.low);
5301 break;
5302 case dw_val_class_vec:
5303 fprintf (outfile, "floating-point or vector constant");
5304 break;
5305 case dw_val_class_flag:
5306 fprintf (outfile, "%u", AT_flag (a));
5307 break;
5308 case dw_val_class_die_ref:
5309 if (AT_ref (a) != NULL)
5311 if (AT_ref (a)->comdat_type_p)
5313 fprintf (outfile, "die -> signature: ");
5314 print_signature (outfile,
5315 AT_ref (a)->die_id.die_type_node->signature);
5317 else if (AT_ref (a)->die_id.die_symbol)
5318 fprintf (outfile, "die -> label: %s",
5319 AT_ref (a)->die_id.die_symbol);
5320 else
5321 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5322 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5324 else
5325 fprintf (outfile, "die -> <null>");
5326 break;
5327 case dw_val_class_vms_delta:
5328 fprintf (outfile, "delta: @slotcount(%s-%s)",
5329 AT_vms_delta2 (a), AT_vms_delta1 (a));
5330 break;
5331 case dw_val_class_lbl_id:
5332 case dw_val_class_lineptr:
5333 case dw_val_class_macptr:
5334 case dw_val_class_high_pc:
5335 fprintf (outfile, "label: %s", AT_lbl (a));
5336 break;
5337 case dw_val_class_str:
5338 if (AT_string (a) != NULL)
5339 fprintf (outfile, "\"%s\"", AT_string (a));
5340 else
5341 fprintf (outfile, "<null>");
5342 break;
5343 case dw_val_class_file:
5344 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5345 AT_file (a)->emitted_number);
5346 break;
5347 case dw_val_class_data8:
5349 int i;
5351 for (i = 0; i < 8; i++)
5352 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5353 break;
5355 default:
5356 break;
5359 fprintf (outfile, "\n");
5362 if (die->die_child != NULL)
5364 print_indent += 4;
5365 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5366 print_indent -= 4;
5368 if (print_indent == 0)
5369 fprintf (outfile, "\n");
5372 /* Print the information collected for a given DIE. */
5374 DEBUG_FUNCTION void
5375 debug_dwarf_die (dw_die_ref die)
5377 print_die (die, stderr);
5380 DEBUG_FUNCTION void
5381 debug (die_struct &ref)
5383 print_die (&ref, stderr);
5386 DEBUG_FUNCTION void
5387 debug (die_struct *ptr)
5389 if (ptr)
5390 debug (*ptr);
5391 else
5392 fprintf (stderr, "<nil>\n");
5396 /* Print all DWARF information collected for the compilation unit.
5397 This routine is a debugging aid only. */
5399 DEBUG_FUNCTION void
5400 debug_dwarf (void)
5402 print_indent = 0;
5403 print_die (comp_unit_die (), stderr);
5406 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5407 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5408 DIE that marks the start of the DIEs for this include file. */
5410 static dw_die_ref
5411 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5413 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5414 dw_die_ref new_unit = gen_compile_unit_die (filename);
5416 new_unit->die_sib = old_unit;
5417 return new_unit;
5420 /* Close an include-file CU and reopen the enclosing one. */
5422 static dw_die_ref
5423 pop_compile_unit (dw_die_ref old_unit)
5425 dw_die_ref new_unit = old_unit->die_sib;
5427 old_unit->die_sib = NULL;
5428 return new_unit;
5431 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5432 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5434 /* Calculate the checksum of a location expression. */
5436 static inline void
5437 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5439 int tem;
5441 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5442 CHECKSUM (tem);
5443 CHECKSUM (loc->dw_loc_oprnd1);
5444 CHECKSUM (loc->dw_loc_oprnd2);
5447 /* Calculate the checksum of an attribute. */
5449 static void
5450 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5452 dw_loc_descr_ref loc;
5453 rtx r;
5455 CHECKSUM (at->dw_attr);
5457 /* We don't care that this was compiled with a different compiler
5458 snapshot; if the output is the same, that's what matters. */
5459 if (at->dw_attr == DW_AT_producer)
5460 return;
5462 switch (AT_class (at))
5464 case dw_val_class_const:
5465 CHECKSUM (at->dw_attr_val.v.val_int);
5466 break;
5467 case dw_val_class_unsigned_const:
5468 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5469 break;
5470 case dw_val_class_const_double:
5471 CHECKSUM (at->dw_attr_val.v.val_double);
5472 break;
5473 case dw_val_class_vec:
5474 CHECKSUM (at->dw_attr_val.v.val_vec);
5475 break;
5476 case dw_val_class_flag:
5477 CHECKSUM (at->dw_attr_val.v.val_flag);
5478 break;
5479 case dw_val_class_str:
5480 CHECKSUM_STRING (AT_string (at));
5481 break;
5483 case dw_val_class_addr:
5484 r = AT_addr (at);
5485 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5486 CHECKSUM_STRING (XSTR (r, 0));
5487 break;
5489 case dw_val_class_offset:
5490 CHECKSUM (at->dw_attr_val.v.val_offset);
5491 break;
5493 case dw_val_class_loc:
5494 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5495 loc_checksum (loc, ctx);
5496 break;
5498 case dw_val_class_die_ref:
5499 die_checksum (AT_ref (at), ctx, mark);
5500 break;
5502 case dw_val_class_fde_ref:
5503 case dw_val_class_vms_delta:
5504 case dw_val_class_lbl_id:
5505 case dw_val_class_lineptr:
5506 case dw_val_class_macptr:
5507 case dw_val_class_high_pc:
5508 break;
5510 case dw_val_class_file:
5511 CHECKSUM_STRING (AT_file (at)->filename);
5512 break;
5514 case dw_val_class_data8:
5515 CHECKSUM (at->dw_attr_val.v.val_data8);
5516 break;
5518 default:
5519 break;
5523 /* Calculate the checksum of a DIE. */
5525 static void
5526 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5528 dw_die_ref c;
5529 dw_attr_ref a;
5530 unsigned ix;
5532 /* To avoid infinite recursion. */
5533 if (die->die_mark)
5535 CHECKSUM (die->die_mark);
5536 return;
5538 die->die_mark = ++(*mark);
5540 CHECKSUM (die->die_tag);
5542 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5543 attr_checksum (a, ctx, mark);
5545 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5548 #undef CHECKSUM
5549 #undef CHECKSUM_STRING
5551 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5552 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5553 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5554 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5555 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5556 #define CHECKSUM_ATTR(FOO) \
5557 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5559 /* Calculate the checksum of a number in signed LEB128 format. */
5561 static void
5562 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5564 unsigned char byte;
5565 bool more;
5567 while (1)
5569 byte = (value & 0x7f);
5570 value >>= 7;
5571 more = !((value == 0 && (byte & 0x40) == 0)
5572 || (value == -1 && (byte & 0x40) != 0));
5573 if (more)
5574 byte |= 0x80;
5575 CHECKSUM (byte);
5576 if (!more)
5577 break;
5581 /* Calculate the checksum of a number in unsigned LEB128 format. */
5583 static void
5584 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5586 while (1)
5588 unsigned char byte = (value & 0x7f);
5589 value >>= 7;
5590 if (value != 0)
5591 /* More bytes to follow. */
5592 byte |= 0x80;
5593 CHECKSUM (byte);
5594 if (value == 0)
5595 break;
5599 /* Checksum the context of the DIE. This adds the names of any
5600 surrounding namespaces or structures to the checksum. */
5602 static void
5603 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5605 const char *name;
5606 dw_die_ref spec;
5607 int tag = die->die_tag;
5609 if (tag != DW_TAG_namespace
5610 && tag != DW_TAG_structure_type
5611 && tag != DW_TAG_class_type)
5612 return;
5614 name = get_AT_string (die, DW_AT_name);
5616 spec = get_AT_ref (die, DW_AT_specification);
5617 if (spec != NULL)
5618 die = spec;
5620 if (die->die_parent != NULL)
5621 checksum_die_context (die->die_parent, ctx);
5623 CHECKSUM_ULEB128 ('C');
5624 CHECKSUM_ULEB128 (tag);
5625 if (name != NULL)
5626 CHECKSUM_STRING (name);
5629 /* Calculate the checksum of a location expression. */
5631 static inline void
5632 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5634 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5635 were emitted as a DW_FORM_sdata instead of a location expression. */
5636 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5638 CHECKSUM_ULEB128 (DW_FORM_sdata);
5639 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5640 return;
5643 /* Otherwise, just checksum the raw location expression. */
5644 while (loc != NULL)
5646 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5647 CHECKSUM (loc->dw_loc_oprnd1);
5648 CHECKSUM (loc->dw_loc_oprnd2);
5649 loc = loc->dw_loc_next;
5653 /* Calculate the checksum of an attribute. */
5655 static void
5656 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5657 struct md5_ctx *ctx, int *mark)
5659 dw_loc_descr_ref loc;
5660 rtx r;
5662 if (AT_class (at) == dw_val_class_die_ref)
5664 dw_die_ref target_die = AT_ref (at);
5666 /* For pointer and reference types, we checksum only the (qualified)
5667 name of the target type (if there is a name). For friend entries,
5668 we checksum only the (qualified) name of the target type or function.
5669 This allows the checksum to remain the same whether the target type
5670 is complete or not. */
5671 if ((at->dw_attr == DW_AT_type
5672 && (tag == DW_TAG_pointer_type
5673 || tag == DW_TAG_reference_type
5674 || tag == DW_TAG_rvalue_reference_type
5675 || tag == DW_TAG_ptr_to_member_type))
5676 || (at->dw_attr == DW_AT_friend
5677 && tag == DW_TAG_friend))
5679 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5681 if (name_attr != NULL)
5683 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5685 if (decl == NULL)
5686 decl = target_die;
5687 CHECKSUM_ULEB128 ('N');
5688 CHECKSUM_ULEB128 (at->dw_attr);
5689 if (decl->die_parent != NULL)
5690 checksum_die_context (decl->die_parent, ctx);
5691 CHECKSUM_ULEB128 ('E');
5692 CHECKSUM_STRING (AT_string (name_attr));
5693 return;
5697 /* For all other references to another DIE, we check to see if the
5698 target DIE has already been visited. If it has, we emit a
5699 backward reference; if not, we descend recursively. */
5700 if (target_die->die_mark > 0)
5702 CHECKSUM_ULEB128 ('R');
5703 CHECKSUM_ULEB128 (at->dw_attr);
5704 CHECKSUM_ULEB128 (target_die->die_mark);
5706 else
5708 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5710 if (decl == NULL)
5711 decl = target_die;
5712 target_die->die_mark = ++(*mark);
5713 CHECKSUM_ULEB128 ('T');
5714 CHECKSUM_ULEB128 (at->dw_attr);
5715 if (decl->die_parent != NULL)
5716 checksum_die_context (decl->die_parent, ctx);
5717 die_checksum_ordered (target_die, ctx, mark);
5719 return;
5722 CHECKSUM_ULEB128 ('A');
5723 CHECKSUM_ULEB128 (at->dw_attr);
5725 switch (AT_class (at))
5727 case dw_val_class_const:
5728 CHECKSUM_ULEB128 (DW_FORM_sdata);
5729 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5730 break;
5732 case dw_val_class_unsigned_const:
5733 CHECKSUM_ULEB128 (DW_FORM_sdata);
5734 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5735 break;
5737 case dw_val_class_const_double:
5738 CHECKSUM_ULEB128 (DW_FORM_block);
5739 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5740 CHECKSUM (at->dw_attr_val.v.val_double);
5741 break;
5743 case dw_val_class_vec:
5744 CHECKSUM_ULEB128 (DW_FORM_block);
5745 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5746 CHECKSUM (at->dw_attr_val.v.val_vec);
5747 break;
5749 case dw_val_class_flag:
5750 CHECKSUM_ULEB128 (DW_FORM_flag);
5751 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5752 break;
5754 case dw_val_class_str:
5755 CHECKSUM_ULEB128 (DW_FORM_string);
5756 CHECKSUM_STRING (AT_string (at));
5757 break;
5759 case dw_val_class_addr:
5760 r = AT_addr (at);
5761 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5762 CHECKSUM_ULEB128 (DW_FORM_string);
5763 CHECKSUM_STRING (XSTR (r, 0));
5764 break;
5766 case dw_val_class_offset:
5767 CHECKSUM_ULEB128 (DW_FORM_sdata);
5768 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5769 break;
5771 case dw_val_class_loc:
5772 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5773 loc_checksum_ordered (loc, ctx);
5774 break;
5776 case dw_val_class_fde_ref:
5777 case dw_val_class_lbl_id:
5778 case dw_val_class_lineptr:
5779 case dw_val_class_macptr:
5780 case dw_val_class_high_pc:
5781 break;
5783 case dw_val_class_file:
5784 CHECKSUM_ULEB128 (DW_FORM_string);
5785 CHECKSUM_STRING (AT_file (at)->filename);
5786 break;
5788 case dw_val_class_data8:
5789 CHECKSUM (at->dw_attr_val.v.val_data8);
5790 break;
5792 default:
5793 break;
5797 struct checksum_attributes
5799 dw_attr_ref at_name;
5800 dw_attr_ref at_type;
5801 dw_attr_ref at_friend;
5802 dw_attr_ref at_accessibility;
5803 dw_attr_ref at_address_class;
5804 dw_attr_ref at_allocated;
5805 dw_attr_ref at_artificial;
5806 dw_attr_ref at_associated;
5807 dw_attr_ref at_binary_scale;
5808 dw_attr_ref at_bit_offset;
5809 dw_attr_ref at_bit_size;
5810 dw_attr_ref at_bit_stride;
5811 dw_attr_ref at_byte_size;
5812 dw_attr_ref at_byte_stride;
5813 dw_attr_ref at_const_value;
5814 dw_attr_ref at_containing_type;
5815 dw_attr_ref at_count;
5816 dw_attr_ref at_data_location;
5817 dw_attr_ref at_data_member_location;
5818 dw_attr_ref at_decimal_scale;
5819 dw_attr_ref at_decimal_sign;
5820 dw_attr_ref at_default_value;
5821 dw_attr_ref at_digit_count;
5822 dw_attr_ref at_discr;
5823 dw_attr_ref at_discr_list;
5824 dw_attr_ref at_discr_value;
5825 dw_attr_ref at_encoding;
5826 dw_attr_ref at_endianity;
5827 dw_attr_ref at_explicit;
5828 dw_attr_ref at_is_optional;
5829 dw_attr_ref at_location;
5830 dw_attr_ref at_lower_bound;
5831 dw_attr_ref at_mutable;
5832 dw_attr_ref at_ordering;
5833 dw_attr_ref at_picture_string;
5834 dw_attr_ref at_prototyped;
5835 dw_attr_ref at_small;
5836 dw_attr_ref at_segment;
5837 dw_attr_ref at_string_length;
5838 dw_attr_ref at_threads_scaled;
5839 dw_attr_ref at_upper_bound;
5840 dw_attr_ref at_use_location;
5841 dw_attr_ref at_use_UTF8;
5842 dw_attr_ref at_variable_parameter;
5843 dw_attr_ref at_virtuality;
5844 dw_attr_ref at_visibility;
5845 dw_attr_ref at_vtable_elem_location;
5848 /* Collect the attributes that we will want to use for the checksum. */
5850 static void
5851 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5853 dw_attr_ref a;
5854 unsigned ix;
5856 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5858 switch (a->dw_attr)
5860 case DW_AT_name:
5861 attrs->at_name = a;
5862 break;
5863 case DW_AT_type:
5864 attrs->at_type = a;
5865 break;
5866 case DW_AT_friend:
5867 attrs->at_friend = a;
5868 break;
5869 case DW_AT_accessibility:
5870 attrs->at_accessibility = a;
5871 break;
5872 case DW_AT_address_class:
5873 attrs->at_address_class = a;
5874 break;
5875 case DW_AT_allocated:
5876 attrs->at_allocated = a;
5877 break;
5878 case DW_AT_artificial:
5879 attrs->at_artificial = a;
5880 break;
5881 case DW_AT_associated:
5882 attrs->at_associated = a;
5883 break;
5884 case DW_AT_binary_scale:
5885 attrs->at_binary_scale = a;
5886 break;
5887 case DW_AT_bit_offset:
5888 attrs->at_bit_offset = a;
5889 break;
5890 case DW_AT_bit_size:
5891 attrs->at_bit_size = a;
5892 break;
5893 case DW_AT_bit_stride:
5894 attrs->at_bit_stride = a;
5895 break;
5896 case DW_AT_byte_size:
5897 attrs->at_byte_size = a;
5898 break;
5899 case DW_AT_byte_stride:
5900 attrs->at_byte_stride = a;
5901 break;
5902 case DW_AT_const_value:
5903 attrs->at_const_value = a;
5904 break;
5905 case DW_AT_containing_type:
5906 attrs->at_containing_type = a;
5907 break;
5908 case DW_AT_count:
5909 attrs->at_count = a;
5910 break;
5911 case DW_AT_data_location:
5912 attrs->at_data_location = a;
5913 break;
5914 case DW_AT_data_member_location:
5915 attrs->at_data_member_location = a;
5916 break;
5917 case DW_AT_decimal_scale:
5918 attrs->at_decimal_scale = a;
5919 break;
5920 case DW_AT_decimal_sign:
5921 attrs->at_decimal_sign = a;
5922 break;
5923 case DW_AT_default_value:
5924 attrs->at_default_value = a;
5925 break;
5926 case DW_AT_digit_count:
5927 attrs->at_digit_count = a;
5928 break;
5929 case DW_AT_discr:
5930 attrs->at_discr = a;
5931 break;
5932 case DW_AT_discr_list:
5933 attrs->at_discr_list = a;
5934 break;
5935 case DW_AT_discr_value:
5936 attrs->at_discr_value = a;
5937 break;
5938 case DW_AT_encoding:
5939 attrs->at_encoding = a;
5940 break;
5941 case DW_AT_endianity:
5942 attrs->at_endianity = a;
5943 break;
5944 case DW_AT_explicit:
5945 attrs->at_explicit = a;
5946 break;
5947 case DW_AT_is_optional:
5948 attrs->at_is_optional = a;
5949 break;
5950 case DW_AT_location:
5951 attrs->at_location = a;
5952 break;
5953 case DW_AT_lower_bound:
5954 attrs->at_lower_bound = a;
5955 break;
5956 case DW_AT_mutable:
5957 attrs->at_mutable = a;
5958 break;
5959 case DW_AT_ordering:
5960 attrs->at_ordering = a;
5961 break;
5962 case DW_AT_picture_string:
5963 attrs->at_picture_string = a;
5964 break;
5965 case DW_AT_prototyped:
5966 attrs->at_prototyped = a;
5967 break;
5968 case DW_AT_small:
5969 attrs->at_small = a;
5970 break;
5971 case DW_AT_segment:
5972 attrs->at_segment = a;
5973 break;
5974 case DW_AT_string_length:
5975 attrs->at_string_length = a;
5976 break;
5977 case DW_AT_threads_scaled:
5978 attrs->at_threads_scaled = a;
5979 break;
5980 case DW_AT_upper_bound:
5981 attrs->at_upper_bound = a;
5982 break;
5983 case DW_AT_use_location:
5984 attrs->at_use_location = a;
5985 break;
5986 case DW_AT_use_UTF8:
5987 attrs->at_use_UTF8 = a;
5988 break;
5989 case DW_AT_variable_parameter:
5990 attrs->at_variable_parameter = a;
5991 break;
5992 case DW_AT_virtuality:
5993 attrs->at_virtuality = a;
5994 break;
5995 case DW_AT_visibility:
5996 attrs->at_visibility = a;
5997 break;
5998 case DW_AT_vtable_elem_location:
5999 attrs->at_vtable_elem_location = a;
6000 break;
6001 default:
6002 break;
6007 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6009 static void
6010 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6012 dw_die_ref c;
6013 dw_die_ref decl;
6014 struct checksum_attributes attrs;
6016 CHECKSUM_ULEB128 ('D');
6017 CHECKSUM_ULEB128 (die->die_tag);
6019 memset (&attrs, 0, sizeof (attrs));
6021 decl = get_AT_ref (die, DW_AT_specification);
6022 if (decl != NULL)
6023 collect_checksum_attributes (&attrs, decl);
6024 collect_checksum_attributes (&attrs, die);
6026 CHECKSUM_ATTR (attrs.at_name);
6027 CHECKSUM_ATTR (attrs.at_accessibility);
6028 CHECKSUM_ATTR (attrs.at_address_class);
6029 CHECKSUM_ATTR (attrs.at_allocated);
6030 CHECKSUM_ATTR (attrs.at_artificial);
6031 CHECKSUM_ATTR (attrs.at_associated);
6032 CHECKSUM_ATTR (attrs.at_binary_scale);
6033 CHECKSUM_ATTR (attrs.at_bit_offset);
6034 CHECKSUM_ATTR (attrs.at_bit_size);
6035 CHECKSUM_ATTR (attrs.at_bit_stride);
6036 CHECKSUM_ATTR (attrs.at_byte_size);
6037 CHECKSUM_ATTR (attrs.at_byte_stride);
6038 CHECKSUM_ATTR (attrs.at_const_value);
6039 CHECKSUM_ATTR (attrs.at_containing_type);
6040 CHECKSUM_ATTR (attrs.at_count);
6041 CHECKSUM_ATTR (attrs.at_data_location);
6042 CHECKSUM_ATTR (attrs.at_data_member_location);
6043 CHECKSUM_ATTR (attrs.at_decimal_scale);
6044 CHECKSUM_ATTR (attrs.at_decimal_sign);
6045 CHECKSUM_ATTR (attrs.at_default_value);
6046 CHECKSUM_ATTR (attrs.at_digit_count);
6047 CHECKSUM_ATTR (attrs.at_discr);
6048 CHECKSUM_ATTR (attrs.at_discr_list);
6049 CHECKSUM_ATTR (attrs.at_discr_value);
6050 CHECKSUM_ATTR (attrs.at_encoding);
6051 CHECKSUM_ATTR (attrs.at_endianity);
6052 CHECKSUM_ATTR (attrs.at_explicit);
6053 CHECKSUM_ATTR (attrs.at_is_optional);
6054 CHECKSUM_ATTR (attrs.at_location);
6055 CHECKSUM_ATTR (attrs.at_lower_bound);
6056 CHECKSUM_ATTR (attrs.at_mutable);
6057 CHECKSUM_ATTR (attrs.at_ordering);
6058 CHECKSUM_ATTR (attrs.at_picture_string);
6059 CHECKSUM_ATTR (attrs.at_prototyped);
6060 CHECKSUM_ATTR (attrs.at_small);
6061 CHECKSUM_ATTR (attrs.at_segment);
6062 CHECKSUM_ATTR (attrs.at_string_length);
6063 CHECKSUM_ATTR (attrs.at_threads_scaled);
6064 CHECKSUM_ATTR (attrs.at_upper_bound);
6065 CHECKSUM_ATTR (attrs.at_use_location);
6066 CHECKSUM_ATTR (attrs.at_use_UTF8);
6067 CHECKSUM_ATTR (attrs.at_variable_parameter);
6068 CHECKSUM_ATTR (attrs.at_virtuality);
6069 CHECKSUM_ATTR (attrs.at_visibility);
6070 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6071 CHECKSUM_ATTR (attrs.at_type);
6072 CHECKSUM_ATTR (attrs.at_friend);
6074 /* Checksum the child DIEs, except for nested types and member functions. */
6075 c = die->die_child;
6076 if (c) do {
6077 dw_attr_ref name_attr;
6079 c = c->die_sib;
6080 name_attr = get_AT (c, DW_AT_name);
6081 if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6082 && name_attr != NULL)
6084 CHECKSUM_ULEB128 ('S');
6085 CHECKSUM_ULEB128 (c->die_tag);
6086 CHECKSUM_STRING (AT_string (name_attr));
6088 else
6090 /* Mark this DIE so it gets processed when unmarking. */
6091 if (c->die_mark == 0)
6092 c->die_mark = -1;
6093 die_checksum_ordered (c, ctx, mark);
6095 } while (c != die->die_child);
6097 CHECKSUM_ULEB128 (0);
6100 #undef CHECKSUM
6101 #undef CHECKSUM_STRING
6102 #undef CHECKSUM_ATTR
6103 #undef CHECKSUM_LEB128
6104 #undef CHECKSUM_ULEB128
6106 /* Generate the type signature for DIE. This is computed by generating an
6107 MD5 checksum over the DIE's tag, its relevant attributes, and its
6108 children. Attributes that are references to other DIEs are processed
6109 by recursion, using the MARK field to prevent infinite recursion.
6110 If the DIE is nested inside a namespace or another type, we also
6111 need to include that context in the signature. The lower 64 bits
6112 of the resulting MD5 checksum comprise the signature. */
6114 static void
6115 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6117 int mark;
6118 const char *name;
6119 unsigned char checksum[16];
6120 struct md5_ctx ctx;
6121 dw_die_ref decl;
6122 dw_die_ref parent;
6124 name = get_AT_string (die, DW_AT_name);
6125 decl = get_AT_ref (die, DW_AT_specification);
6126 parent = get_die_parent (die);
6128 /* First, compute a signature for just the type name (and its surrounding
6129 context, if any. This is stored in the type unit DIE for link-time
6130 ODR (one-definition rule) checking. */
6132 if (is_cxx() && name != NULL)
6134 md5_init_ctx (&ctx);
6136 /* Checksum the names of surrounding namespaces and structures. */
6137 if (parent != NULL)
6138 checksum_die_context (parent, &ctx);
6140 md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6141 md5_process_bytes (name, strlen (name) + 1, &ctx);
6142 md5_finish_ctx (&ctx, checksum);
6144 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6147 /* Next, compute the complete type signature. */
6149 md5_init_ctx (&ctx);
6150 mark = 1;
6151 die->die_mark = mark;
6153 /* Checksum the names of surrounding namespaces and structures. */
6154 if (parent != NULL)
6155 checksum_die_context (parent, &ctx);
6157 /* Checksum the DIE and its children. */
6158 die_checksum_ordered (die, &ctx, &mark);
6159 unmark_all_dies (die);
6160 md5_finish_ctx (&ctx, checksum);
6162 /* Store the signature in the type node and link the type DIE and the
6163 type node together. */
6164 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6165 DWARF_TYPE_SIGNATURE_SIZE);
6166 die->comdat_type_p = true;
6167 die->die_id.die_type_node = type_node;
6168 type_node->type_die = die;
6170 /* If the DIE is a specification, link its declaration to the type node
6171 as well. */
6172 if (decl != NULL)
6174 decl->comdat_type_p = true;
6175 decl->die_id.die_type_node = type_node;
6179 /* Do the location expressions look same? */
6180 static inline int
6181 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6183 return loc1->dw_loc_opc == loc2->dw_loc_opc
6184 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6185 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6188 /* Do the values look the same? */
6189 static int
6190 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6192 dw_loc_descr_ref loc1, loc2;
6193 rtx r1, r2;
6195 if (v1->val_class != v2->val_class)
6196 return 0;
6198 switch (v1->val_class)
6200 case dw_val_class_const:
6201 return v1->v.val_int == v2->v.val_int;
6202 case dw_val_class_unsigned_const:
6203 return v1->v.val_unsigned == v2->v.val_unsigned;
6204 case dw_val_class_const_double:
6205 return v1->v.val_double.high == v2->v.val_double.high
6206 && v1->v.val_double.low == v2->v.val_double.low;
6207 case dw_val_class_vec:
6208 if (v1->v.val_vec.length != v2->v.val_vec.length
6209 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6210 return 0;
6211 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6212 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6213 return 0;
6214 return 1;
6215 case dw_val_class_flag:
6216 return v1->v.val_flag == v2->v.val_flag;
6217 case dw_val_class_str:
6218 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6220 case dw_val_class_addr:
6221 r1 = v1->v.val_addr;
6222 r2 = v2->v.val_addr;
6223 if (GET_CODE (r1) != GET_CODE (r2))
6224 return 0;
6225 return !rtx_equal_p (r1, r2);
6227 case dw_val_class_offset:
6228 return v1->v.val_offset == v2->v.val_offset;
6230 case dw_val_class_loc:
6231 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6232 loc1 && loc2;
6233 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6234 if (!same_loc_p (loc1, loc2, mark))
6235 return 0;
6236 return !loc1 && !loc2;
6238 case dw_val_class_die_ref:
6239 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6241 case dw_val_class_fde_ref:
6242 case dw_val_class_vms_delta:
6243 case dw_val_class_lbl_id:
6244 case dw_val_class_lineptr:
6245 case dw_val_class_macptr:
6246 case dw_val_class_high_pc:
6247 return 1;
6249 case dw_val_class_file:
6250 return v1->v.val_file == v2->v.val_file;
6252 case dw_val_class_data8:
6253 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6255 default:
6256 return 1;
6260 /* Do the attributes look the same? */
6262 static int
6263 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6265 if (at1->dw_attr != at2->dw_attr)
6266 return 0;
6268 /* We don't care that this was compiled with a different compiler
6269 snapshot; if the output is the same, that's what matters. */
6270 if (at1->dw_attr == DW_AT_producer)
6271 return 1;
6273 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6276 /* Do the dies look the same? */
6278 static int
6279 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6281 dw_die_ref c1, c2;
6282 dw_attr_ref a1;
6283 unsigned ix;
6285 /* To avoid infinite recursion. */
6286 if (die1->die_mark)
6287 return die1->die_mark == die2->die_mark;
6288 die1->die_mark = die2->die_mark = ++(*mark);
6290 if (die1->die_tag != die2->die_tag)
6291 return 0;
6293 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6294 return 0;
6296 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6297 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6298 return 0;
6300 c1 = die1->die_child;
6301 c2 = die2->die_child;
6302 if (! c1)
6304 if (c2)
6305 return 0;
6307 else
6308 for (;;)
6310 if (!same_die_p (c1, c2, mark))
6311 return 0;
6312 c1 = c1->die_sib;
6313 c2 = c2->die_sib;
6314 if (c1 == die1->die_child)
6316 if (c2 == die2->die_child)
6317 break;
6318 else
6319 return 0;
6323 return 1;
6326 /* Do the dies look the same? Wrapper around same_die_p. */
6328 static int
6329 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6331 int mark = 0;
6332 int ret = same_die_p (die1, die2, &mark);
6334 unmark_all_dies (die1);
6335 unmark_all_dies (die2);
6337 return ret;
6340 /* The prefix to attach to symbols on DIEs in the current comdat debug
6341 info section. */
6342 static const char *comdat_symbol_id;
6344 /* The index of the current symbol within the current comdat CU. */
6345 static unsigned int comdat_symbol_number;
6347 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6348 children, and set comdat_symbol_id accordingly. */
6350 static void
6351 compute_section_prefix (dw_die_ref unit_die)
6353 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6354 const char *base = die_name ? lbasename (die_name) : "anonymous";
6355 char *name = XALLOCAVEC (char, strlen (base) + 64);
6356 char *p;
6357 int i, mark;
6358 unsigned char checksum[16];
6359 struct md5_ctx ctx;
6361 /* Compute the checksum of the DIE, then append part of it as hex digits to
6362 the name filename of the unit. */
6364 md5_init_ctx (&ctx);
6365 mark = 0;
6366 die_checksum (unit_die, &ctx, &mark);
6367 unmark_all_dies (unit_die);
6368 md5_finish_ctx (&ctx, checksum);
6370 sprintf (name, "%s.", base);
6371 clean_symbol_name (name);
6373 p = name + strlen (name);
6374 for (i = 0; i < 4; i++)
6376 sprintf (p, "%.2x", checksum[i]);
6377 p += 2;
6380 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6381 comdat_symbol_number = 0;
6384 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6386 static int
6387 is_type_die (dw_die_ref die)
6389 switch (die->die_tag)
6391 case DW_TAG_array_type:
6392 case DW_TAG_class_type:
6393 case DW_TAG_interface_type:
6394 case DW_TAG_enumeration_type:
6395 case DW_TAG_pointer_type:
6396 case DW_TAG_reference_type:
6397 case DW_TAG_rvalue_reference_type:
6398 case DW_TAG_string_type:
6399 case DW_TAG_structure_type:
6400 case DW_TAG_subroutine_type:
6401 case DW_TAG_union_type:
6402 case DW_TAG_ptr_to_member_type:
6403 case DW_TAG_set_type:
6404 case DW_TAG_subrange_type:
6405 case DW_TAG_base_type:
6406 case DW_TAG_const_type:
6407 case DW_TAG_file_type:
6408 case DW_TAG_packed_type:
6409 case DW_TAG_volatile_type:
6410 case DW_TAG_typedef:
6411 return 1;
6412 default:
6413 return 0;
6417 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6418 Basically, we want to choose the bits that are likely to be shared between
6419 compilations (types) and leave out the bits that are specific to individual
6420 compilations (functions). */
6422 static int
6423 is_comdat_die (dw_die_ref c)
6425 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6426 we do for stabs. The advantage is a greater likelihood of sharing between
6427 objects that don't include headers in the same order (and therefore would
6428 put the base types in a different comdat). jason 8/28/00 */
6430 if (c->die_tag == DW_TAG_base_type)
6431 return 0;
6433 if (c->die_tag == DW_TAG_pointer_type
6434 || c->die_tag == DW_TAG_reference_type
6435 || c->die_tag == DW_TAG_rvalue_reference_type
6436 || c->die_tag == DW_TAG_const_type
6437 || c->die_tag == DW_TAG_volatile_type)
6439 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6441 return t ? is_comdat_die (t) : 0;
6444 return is_type_die (c);
6447 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6448 compilation unit. */
6450 static int
6451 is_symbol_die (dw_die_ref c)
6453 return (is_type_die (c)
6454 || is_declaration_die (c)
6455 || c->die_tag == DW_TAG_namespace
6456 || c->die_tag == DW_TAG_module);
6459 /* Returns true iff C is a compile-unit DIE. */
6461 static inline bool
6462 is_cu_die (dw_die_ref c)
6464 return c && c->die_tag == DW_TAG_compile_unit;
6467 /* Returns true iff C is a unit DIE of some sort. */
6469 static inline bool
6470 is_unit_die (dw_die_ref c)
6472 return c && (c->die_tag == DW_TAG_compile_unit
6473 || c->die_tag == DW_TAG_partial_unit
6474 || c->die_tag == DW_TAG_type_unit);
6477 /* Returns true iff C is a namespace DIE. */
6479 static inline bool
6480 is_namespace_die (dw_die_ref c)
6482 return c && c->die_tag == DW_TAG_namespace;
6485 /* Returns true iff C is a class or structure DIE. */
6487 static inline bool
6488 is_class_die (dw_die_ref c)
6490 return c && (c->die_tag == DW_TAG_class_type
6491 || c->die_tag == DW_TAG_structure_type);
6494 static char *
6495 gen_internal_sym (const char *prefix)
6497 char buf[256];
6499 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6500 return xstrdup (buf);
6503 /* Assign symbols to all worthy DIEs under DIE. */
6505 static void
6506 assign_symbol_names (dw_die_ref die)
6508 dw_die_ref c;
6510 if (is_symbol_die (die) && !die->comdat_type_p)
6512 if (comdat_symbol_id)
6514 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6516 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6517 comdat_symbol_id, comdat_symbol_number++);
6518 die->die_id.die_symbol = xstrdup (p);
6520 else
6521 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6524 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6527 struct cu_hash_table_entry
6529 dw_die_ref cu;
6530 unsigned min_comdat_num, max_comdat_num;
6531 struct cu_hash_table_entry *next;
6534 /* Helpers to manipulate hash table of CUs. */
6536 struct cu_hash_table_entry_hasher
6538 typedef cu_hash_table_entry value_type;
6539 typedef die_struct compare_type;
6540 static inline hashval_t hash (const value_type *);
6541 static inline bool equal (const value_type *, const compare_type *);
6542 static inline void remove (value_type *);
6545 inline hashval_t
6546 cu_hash_table_entry_hasher::hash (const value_type *entry)
6548 return htab_hash_string (entry->cu->die_id.die_symbol);
6551 inline bool
6552 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6553 const compare_type *entry2)
6555 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6558 inline void
6559 cu_hash_table_entry_hasher::remove (value_type *entry)
6561 struct cu_hash_table_entry *next;
6563 while (entry)
6565 next = entry->next;
6566 free (entry);
6567 entry = next;
6571 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6573 /* Check whether we have already seen this CU and set up SYM_NUM
6574 accordingly. */
6575 static int
6576 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6578 struct cu_hash_table_entry dummy;
6579 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6581 dummy.max_comdat_num = 0;
6583 slot = htable.find_slot_with_hash (cu,
6584 htab_hash_string (cu->die_id.die_symbol),
6585 INSERT);
6586 entry = *slot;
6588 for (; entry; last = entry, entry = entry->next)
6590 if (same_die_p_wrap (cu, entry->cu))
6591 break;
6594 if (entry)
6596 *sym_num = entry->min_comdat_num;
6597 return 1;
6600 entry = XCNEW (struct cu_hash_table_entry);
6601 entry->cu = cu;
6602 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6603 entry->next = *slot;
6604 *slot = entry;
6606 return 0;
6609 /* Record SYM_NUM to record of CU in HTABLE. */
6610 static void
6611 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6612 unsigned int sym_num)
6614 struct cu_hash_table_entry **slot, *entry;
6616 slot = htable.find_slot_with_hash (cu,
6617 htab_hash_string (cu->die_id.die_symbol),
6618 NO_INSERT);
6619 entry = *slot;
6621 entry->max_comdat_num = sym_num;
6624 /* Traverse the DIE (which is always comp_unit_die), and set up
6625 additional compilation units for each of the include files we see
6626 bracketed by BINCL/EINCL. */
6628 static void
6629 break_out_includes (dw_die_ref die)
6631 dw_die_ref c;
6632 dw_die_ref unit = NULL;
6633 limbo_die_node *node, **pnode;
6634 cu_hash_type cu_hash_table;
6636 c = die->die_child;
6637 if (c) do {
6638 dw_die_ref prev = c;
6639 c = c->die_sib;
6640 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6641 || (unit && is_comdat_die (c)))
6643 dw_die_ref next = c->die_sib;
6645 /* This DIE is for a secondary CU; remove it from the main one. */
6646 remove_child_with_prev (c, prev);
6648 if (c->die_tag == DW_TAG_GNU_BINCL)
6649 unit = push_new_compile_unit (unit, c);
6650 else if (c->die_tag == DW_TAG_GNU_EINCL)
6651 unit = pop_compile_unit (unit);
6652 else
6653 add_child_die (unit, c);
6654 c = next;
6655 if (c == die->die_child)
6656 break;
6658 } while (c != die->die_child);
6660 #if 0
6661 /* We can only use this in debugging, since the frontend doesn't check
6662 to make sure that we leave every include file we enter. */
6663 gcc_assert (!unit);
6664 #endif
6666 assign_symbol_names (die);
6667 cu_hash_table.create (10);
6668 for (node = limbo_die_list, pnode = &limbo_die_list;
6669 node;
6670 node = node->next)
6672 int is_dupl;
6674 compute_section_prefix (node->die);
6675 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6676 &comdat_symbol_number);
6677 assign_symbol_names (node->die);
6678 if (is_dupl)
6679 *pnode = node->next;
6680 else
6682 pnode = &node->next;
6683 record_comdat_symbol_number (node->die, cu_hash_table,
6684 comdat_symbol_number);
6687 cu_hash_table.dispose ();
6690 /* Return non-zero if this DIE is a declaration. */
6692 static int
6693 is_declaration_die (dw_die_ref die)
6695 dw_attr_ref a;
6696 unsigned ix;
6698 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6699 if (a->dw_attr == DW_AT_declaration)
6700 return 1;
6702 return 0;
6705 /* Return non-zero if this DIE is nested inside a subprogram. */
6707 static int
6708 is_nested_in_subprogram (dw_die_ref die)
6710 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6712 if (decl == NULL)
6713 decl = die;
6714 return local_scope_p (decl);
6717 /* Return non-zero if this DIE contains a defining declaration of a
6718 subprogram. */
6720 static int
6721 contains_subprogram_definition (dw_die_ref die)
6723 dw_die_ref c;
6725 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6726 return 1;
6727 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6728 return 0;
6731 /* Return non-zero if this is a type DIE that should be moved to a
6732 COMDAT .debug_types section. */
6734 static int
6735 should_move_die_to_comdat (dw_die_ref die)
6737 switch (die->die_tag)
6739 case DW_TAG_class_type:
6740 case DW_TAG_structure_type:
6741 case DW_TAG_enumeration_type:
6742 case DW_TAG_union_type:
6743 /* Don't move declarations, inlined instances, or types nested in a
6744 subprogram. */
6745 if (is_declaration_die (die)
6746 || get_AT (die, DW_AT_abstract_origin)
6747 || is_nested_in_subprogram (die))
6748 return 0;
6749 /* A type definition should never contain a subprogram definition. */
6750 gcc_assert (!contains_subprogram_definition (die));
6751 return 1;
6752 case DW_TAG_array_type:
6753 case DW_TAG_interface_type:
6754 case DW_TAG_pointer_type:
6755 case DW_TAG_reference_type:
6756 case DW_TAG_rvalue_reference_type:
6757 case DW_TAG_string_type:
6758 case DW_TAG_subroutine_type:
6759 case DW_TAG_ptr_to_member_type:
6760 case DW_TAG_set_type:
6761 case DW_TAG_subrange_type:
6762 case DW_TAG_base_type:
6763 case DW_TAG_const_type:
6764 case DW_TAG_file_type:
6765 case DW_TAG_packed_type:
6766 case DW_TAG_volatile_type:
6767 case DW_TAG_typedef:
6768 default:
6769 return 0;
6773 /* Make a clone of DIE. */
6775 static dw_die_ref
6776 clone_die (dw_die_ref die)
6778 dw_die_ref clone;
6779 dw_attr_ref a;
6780 unsigned ix;
6782 clone = ggc_alloc_cleared_die_node ();
6783 clone->die_tag = die->die_tag;
6785 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6786 add_dwarf_attr (clone, a);
6788 return clone;
6791 /* Make a clone of the tree rooted at DIE. */
6793 static dw_die_ref
6794 clone_tree (dw_die_ref die)
6796 dw_die_ref c;
6797 dw_die_ref clone = clone_die (die);
6799 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6801 return clone;
6804 /* Make a clone of DIE as a declaration. */
6806 static dw_die_ref
6807 clone_as_declaration (dw_die_ref die)
6809 dw_die_ref clone;
6810 dw_die_ref decl;
6811 dw_attr_ref a;
6812 unsigned ix;
6814 /* If the DIE is already a declaration, just clone it. */
6815 if (is_declaration_die (die))
6816 return clone_die (die);
6818 /* If the DIE is a specification, just clone its declaration DIE. */
6819 decl = get_AT_ref (die, DW_AT_specification);
6820 if (decl != NULL)
6822 clone = clone_die (decl);
6823 if (die->comdat_type_p)
6824 add_AT_die_ref (clone, DW_AT_signature, die);
6825 return clone;
6828 clone = ggc_alloc_cleared_die_node ();
6829 clone->die_tag = die->die_tag;
6831 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6833 /* We don't want to copy over all attributes.
6834 For example we don't want DW_AT_byte_size because otherwise we will no
6835 longer have a declaration and GDB will treat it as a definition. */
6837 switch (a->dw_attr)
6839 case DW_AT_artificial:
6840 case DW_AT_containing_type:
6841 case DW_AT_external:
6842 case DW_AT_name:
6843 case DW_AT_type:
6844 case DW_AT_virtuality:
6845 case DW_AT_linkage_name:
6846 case DW_AT_MIPS_linkage_name:
6847 add_dwarf_attr (clone, a);
6848 break;
6849 case DW_AT_byte_size:
6850 default:
6851 break;
6855 if (die->comdat_type_p)
6856 add_AT_die_ref (clone, DW_AT_signature, die);
6858 add_AT_flag (clone, DW_AT_declaration, 1);
6859 return clone;
6863 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6865 struct decl_table_entry
6867 dw_die_ref orig;
6868 dw_die_ref copy;
6871 /* Helpers to manipulate hash table of copied declarations. */
6873 /* Hashtable helpers. */
6875 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6877 typedef decl_table_entry value_type;
6878 typedef die_struct compare_type;
6879 static inline hashval_t hash (const value_type *);
6880 static inline bool equal (const value_type *, const compare_type *);
6883 inline hashval_t
6884 decl_table_entry_hasher::hash (const value_type *entry)
6886 return htab_hash_pointer (entry->orig);
6889 inline bool
6890 decl_table_entry_hasher::equal (const value_type *entry1,
6891 const compare_type *entry2)
6893 return entry1->orig == entry2;
6896 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6898 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6899 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6900 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6901 to check if the ancestor has already been copied into UNIT. */
6903 static dw_die_ref
6904 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6906 dw_die_ref parent = die->die_parent;
6907 dw_die_ref new_parent = unit;
6908 dw_die_ref copy;
6909 decl_table_entry **slot = NULL;
6910 struct decl_table_entry *entry = NULL;
6912 if (decl_table.is_created ())
6914 /* Check if the entry has already been copied to UNIT. */
6915 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
6916 INSERT);
6917 if (*slot != HTAB_EMPTY_ENTRY)
6919 entry = *slot;
6920 return entry->copy;
6923 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
6924 entry = XCNEW (struct decl_table_entry);
6925 entry->orig = die;
6926 entry->copy = NULL;
6927 *slot = entry;
6930 if (parent != NULL)
6932 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
6933 if (spec != NULL)
6934 parent = spec;
6935 if (!is_unit_die (parent))
6936 new_parent = copy_ancestor_tree (unit, parent, decl_table);
6939 copy = clone_as_declaration (die);
6940 add_child_die (new_parent, copy);
6942 if (decl_table.is_created ())
6944 /* Record the pointer to the copy. */
6945 entry->copy = copy;
6948 return copy;
6950 /* Copy the declaration context to the new type unit DIE. This includes
6951 any surrounding namespace or type declarations. If the DIE has an
6952 AT_specification attribute, it also includes attributes and children
6953 attached to the specification, and returns a pointer to the original
6954 parent of the declaration DIE. Returns NULL otherwise. */
6956 static dw_die_ref
6957 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6959 dw_die_ref decl;
6960 dw_die_ref new_decl;
6961 dw_die_ref orig_parent = NULL;
6963 decl = get_AT_ref (die, DW_AT_specification);
6964 if (decl == NULL)
6965 decl = die;
6966 else
6968 unsigned ix;
6969 dw_die_ref c;
6970 dw_attr_ref a;
6972 /* The original DIE will be changed to a declaration, and must
6973 be moved to be a child of the original declaration DIE. */
6974 orig_parent = decl->die_parent;
6976 /* Copy the type node pointer from the new DIE to the original
6977 declaration DIE so we can forward references later. */
6978 decl->comdat_type_p = true;
6979 decl->die_id.die_type_node = die->die_id.die_type_node;
6981 remove_AT (die, DW_AT_specification);
6983 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
6985 if (a->dw_attr != DW_AT_name
6986 && a->dw_attr != DW_AT_declaration
6987 && a->dw_attr != DW_AT_external)
6988 add_dwarf_attr (die, a);
6991 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6994 if (decl->die_parent != NULL
6995 && !is_unit_die (decl->die_parent))
6997 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
6998 if (new_decl != NULL)
7000 remove_AT (new_decl, DW_AT_signature);
7001 add_AT_specification (die, new_decl);
7005 return orig_parent;
7008 /* Generate the skeleton ancestor tree for the given NODE, then clone
7009 the DIE and add the clone into the tree. */
7011 static void
7012 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7014 if (node->new_die != NULL)
7015 return;
7017 node->new_die = clone_as_declaration (node->old_die);
7019 if (node->parent != NULL)
7021 generate_skeleton_ancestor_tree (node->parent);
7022 add_child_die (node->parent->new_die, node->new_die);
7026 /* Generate a skeleton tree of DIEs containing any declarations that are
7027 found in the original tree. We traverse the tree looking for declaration
7028 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7030 static void
7031 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7033 skeleton_chain_node node;
7034 dw_die_ref c;
7035 dw_die_ref first;
7036 dw_die_ref prev = NULL;
7037 dw_die_ref next = NULL;
7039 node.parent = parent;
7041 first = c = parent->old_die->die_child;
7042 if (c)
7043 next = c->die_sib;
7044 if (c) do {
7045 if (prev == NULL || prev->die_sib == c)
7046 prev = c;
7047 c = next;
7048 next = (c == first ? NULL : c->die_sib);
7049 node.old_die = c;
7050 node.new_die = NULL;
7051 if (is_declaration_die (c))
7053 /* Clone the existing DIE, move the original to the skeleton
7054 tree (which is in the main CU), and put the clone, with
7055 all the original's children, where the original came from. */
7056 dw_die_ref clone = clone_die (c);
7057 move_all_children (c, clone);
7059 replace_child (c, clone, prev);
7060 generate_skeleton_ancestor_tree (parent);
7061 add_child_die (parent->new_die, c);
7062 node.new_die = c;
7063 c = clone;
7065 generate_skeleton_bottom_up (&node);
7066 } while (next != NULL);
7069 /* Wrapper function for generate_skeleton_bottom_up. */
7071 static dw_die_ref
7072 generate_skeleton (dw_die_ref die)
7074 skeleton_chain_node node;
7076 node.old_die = die;
7077 node.new_die = NULL;
7078 node.parent = NULL;
7080 /* If this type definition is nested inside another type,
7081 always leave at least a declaration in its place. */
7082 if (die->die_parent != NULL && is_type_die (die->die_parent))
7083 node.new_die = clone_as_declaration (die);
7085 generate_skeleton_bottom_up (&node);
7086 return node.new_die;
7089 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7090 declaration. The original DIE is moved to a new compile unit so that
7091 existing references to it follow it to the new location. If any of the
7092 original DIE's descendants is a declaration, we need to replace the
7093 original DIE with a skeleton tree and move the declarations back into the
7094 skeleton tree. */
7096 static dw_die_ref
7097 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7098 dw_die_ref prev)
7100 dw_die_ref skeleton, orig_parent;
7102 /* Copy the declaration context to the type unit DIE. If the returned
7103 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7104 that DIE. */
7105 orig_parent = copy_declaration_context (unit, child);
7107 skeleton = generate_skeleton (child);
7108 if (skeleton == NULL)
7109 remove_child_with_prev (child, prev);
7110 else
7112 skeleton->comdat_type_p = true;
7113 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7115 /* If the original DIE was a specification, we need to put
7116 the skeleton under the parent DIE of the declaration.
7117 This leaves the original declaration in the tree, but
7118 it will be pruned later since there are no longer any
7119 references to it. */
7120 if (orig_parent != NULL)
7122 remove_child_with_prev (child, prev);
7123 add_child_die (orig_parent, skeleton);
7125 else
7126 replace_child (child, skeleton, prev);
7129 return skeleton;
7132 /* Traverse the DIE and set up additional .debug_types sections for each
7133 type worthy of being placed in a COMDAT section. */
7135 static void
7136 break_out_comdat_types (dw_die_ref die)
7138 dw_die_ref c;
7139 dw_die_ref first;
7140 dw_die_ref prev = NULL;
7141 dw_die_ref next = NULL;
7142 dw_die_ref unit = NULL;
7144 first = c = die->die_child;
7145 if (c)
7146 next = c->die_sib;
7147 if (c) do {
7148 if (prev == NULL || prev->die_sib == c)
7149 prev = c;
7150 c = next;
7151 next = (c == first ? NULL : c->die_sib);
7152 if (should_move_die_to_comdat (c))
7154 dw_die_ref replacement;
7155 comdat_type_node_ref type_node;
7157 /* Create a new type unit DIE as the root for the new tree, and
7158 add it to the list of comdat types. */
7159 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7160 add_AT_unsigned (unit, DW_AT_language,
7161 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7162 type_node = ggc_alloc_cleared_comdat_type_node ();
7163 type_node->root_die = unit;
7164 type_node->next = comdat_type_list;
7165 comdat_type_list = type_node;
7167 /* Generate the type signature. */
7168 generate_type_signature (c, type_node);
7170 /* Copy the declaration context, attributes, and children of the
7171 declaration into the new type unit DIE, then remove this DIE
7172 from the main CU (or replace it with a skeleton if necessary). */
7173 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7174 type_node->skeleton_die = replacement;
7176 /* Break out nested types into their own type units. */
7177 break_out_comdat_types (c);
7179 /* Add the DIE to the new compunit. */
7180 add_child_die (unit, c);
7182 if (replacement != NULL)
7183 c = replacement;
7185 else if (c->die_tag == DW_TAG_namespace
7186 || c->die_tag == DW_TAG_class_type
7187 || c->die_tag == DW_TAG_structure_type
7188 || c->die_tag == DW_TAG_union_type)
7190 /* Look for nested types that can be broken out. */
7191 break_out_comdat_types (c);
7193 } while (next != NULL);
7196 /* Like clone_tree, but additionally enter all the children into
7197 the hash table decl_table. */
7199 static dw_die_ref
7200 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7202 dw_die_ref c;
7203 dw_die_ref clone = clone_die (die);
7204 struct decl_table_entry *entry;
7205 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7206 htab_hash_pointer (die), INSERT);
7207 /* Assert that DIE isn't in the hash table yet. If it would be there
7208 before, the ancestors would be necessarily there as well, therefore
7209 clone_tree_hash wouldn't be called. */
7210 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7211 entry = XCNEW (struct decl_table_entry);
7212 entry->orig = die;
7213 entry->copy = clone;
7214 *slot = entry;
7216 FOR_EACH_CHILD (die, c,
7217 add_child_die (clone, clone_tree_hash (c, decl_table)));
7219 return clone;
7222 /* Walk the DIE and its children, looking for references to incomplete
7223 or trivial types that are unmarked (i.e., that are not in the current
7224 type_unit). */
7226 static void
7227 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7229 dw_die_ref c;
7230 dw_attr_ref a;
7231 unsigned ix;
7233 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7235 if (AT_class (a) == dw_val_class_die_ref)
7237 dw_die_ref targ = AT_ref (a);
7238 decl_table_entry **slot;
7239 struct decl_table_entry *entry;
7241 if (targ->die_mark != 0 || targ->comdat_type_p)
7242 continue;
7244 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7245 INSERT);
7247 if (*slot != HTAB_EMPTY_ENTRY)
7249 /* TARG has already been copied, so we just need to
7250 modify the reference to point to the copy. */
7251 entry = *slot;
7252 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7254 else
7256 dw_die_ref parent = unit;
7257 dw_die_ref copy = clone_die (targ);
7259 /* Record in DECL_TABLE that TARG has been copied.
7260 Need to do this now, before the recursive call,
7261 because DECL_TABLE may be expanded and SLOT
7262 would no longer be a valid pointer. */
7263 entry = XCNEW (struct decl_table_entry);
7264 entry->orig = targ;
7265 entry->copy = copy;
7266 *slot = entry;
7268 FOR_EACH_CHILD (targ, c,
7269 add_child_die (copy,
7270 clone_tree_hash (c, decl_table)));
7272 /* Make sure the cloned tree is marked as part of the
7273 type unit. */
7274 mark_dies (copy);
7276 /* If TARG has surrounding context, copy its ancestor tree
7277 into the new type unit. */
7278 if (targ->die_parent != NULL
7279 && !is_unit_die (targ->die_parent))
7280 parent = copy_ancestor_tree (unit, targ->die_parent,
7281 decl_table);
7283 add_child_die (parent, copy);
7284 a->dw_attr_val.v.val_die_ref.die = copy;
7286 /* Make sure the newly-copied DIE is walked. If it was
7287 installed in a previously-added context, it won't
7288 get visited otherwise. */
7289 if (parent != unit)
7291 /* Find the highest point of the newly-added tree,
7292 mark each node along the way, and walk from there. */
7293 parent->die_mark = 1;
7294 while (parent->die_parent
7295 && parent->die_parent->die_mark == 0)
7297 parent = parent->die_parent;
7298 parent->die_mark = 1;
7300 copy_decls_walk (unit, parent, decl_table);
7306 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7309 /* Copy declarations for "unworthy" types into the new comdat section.
7310 Incomplete types, modified types, and certain other types aren't broken
7311 out into comdat sections of their own, so they don't have a signature,
7312 and we need to copy the declaration into the same section so that we
7313 don't have an external reference. */
7315 static void
7316 copy_decls_for_unworthy_types (dw_die_ref unit)
7318 decl_hash_type decl_table;
7320 mark_dies (unit);
7321 decl_table.create (10);
7322 copy_decls_walk (unit, unit, decl_table);
7323 decl_table.dispose ();
7324 unmark_dies (unit);
7327 /* Traverse the DIE and add a sibling attribute if it may have the
7328 effect of speeding up access to siblings. To save some space,
7329 avoid generating sibling attributes for DIE's without children. */
7331 static void
7332 add_sibling_attributes (dw_die_ref die)
7334 dw_die_ref c;
7336 if (! die->die_child)
7337 return;
7339 if (die->die_parent && die != die->die_parent->die_child)
7340 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7342 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7345 /* Output all location lists for the DIE and its children. */
7347 static void
7348 output_location_lists (dw_die_ref die)
7350 dw_die_ref c;
7351 dw_attr_ref a;
7352 unsigned ix;
7354 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7355 if (AT_class (a) == dw_val_class_loc_list)
7356 output_loc_list (AT_loc_list (a));
7358 FOR_EACH_CHILD (die, c, output_location_lists (c));
7361 /* We want to limit the number of external references, because they are
7362 larger than local references: a relocation takes multiple words, and
7363 even a sig8 reference is always eight bytes, whereas a local reference
7364 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7365 So if we encounter multiple external references to the same type DIE, we
7366 make a local typedef stub for it and redirect all references there.
7368 This is the element of the hash table for keeping track of these
7369 references. */
7371 struct external_ref
7373 dw_die_ref type;
7374 dw_die_ref stub;
7375 unsigned n_refs;
7378 /* Hashtable helpers. */
7380 struct external_ref_hasher : typed_free_remove <external_ref>
7382 typedef external_ref value_type;
7383 typedef external_ref compare_type;
7384 static inline hashval_t hash (const value_type *);
7385 static inline bool equal (const value_type *, const compare_type *);
7388 inline hashval_t
7389 external_ref_hasher::hash (const value_type *r)
7391 return htab_hash_pointer (r->type);
7394 inline bool
7395 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7397 return r1->type == r2->type;
7400 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7402 /* Return a pointer to the external_ref for references to DIE. */
7404 static struct external_ref *
7405 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7407 struct external_ref ref, *ref_p;
7408 external_ref **slot;
7410 ref.type = die;
7411 slot = map.find_slot (&ref, INSERT);
7412 if (*slot != HTAB_EMPTY_ENTRY)
7413 return *slot;
7415 ref_p = XCNEW (struct external_ref);
7416 ref_p->type = die;
7417 *slot = ref_p;
7418 return ref_p;
7421 /* Subroutine of optimize_external_refs, below.
7423 If we see a type skeleton, record it as our stub. If we see external
7424 references, remember how many we've seen. */
7426 static void
7427 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7429 dw_die_ref c;
7430 dw_attr_ref a;
7431 unsigned ix;
7432 struct external_ref *ref_p;
7434 if (is_type_die (die)
7435 && (c = get_AT_ref (die, DW_AT_signature)))
7437 /* This is a local skeleton; use it for local references. */
7438 ref_p = lookup_external_ref (map, c);
7439 ref_p->stub = die;
7442 /* Scan the DIE references, and remember any that refer to DIEs from
7443 other CUs (i.e. those which are not marked). */
7444 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7445 if (AT_class (a) == dw_val_class_die_ref
7446 && (c = AT_ref (a))->die_mark == 0
7447 && is_type_die (c))
7449 ref_p = lookup_external_ref (map, c);
7450 ref_p->n_refs++;
7453 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7456 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7457 points to an external_ref, DATA is the CU we're processing. If we don't
7458 already have a local stub, and we have multiple refs, build a stub. */
7461 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7463 struct external_ref *ref_p = *slot;
7465 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7467 /* We have multiple references to this type, so build a small stub.
7468 Both of these forms are a bit dodgy from the perspective of the
7469 DWARF standard, since technically they should have names. */
7470 dw_die_ref cu = data;
7471 dw_die_ref type = ref_p->type;
7472 dw_die_ref stub = NULL;
7474 if (type->comdat_type_p)
7476 /* If we refer to this type via sig8, use AT_signature. */
7477 stub = new_die (type->die_tag, cu, NULL_TREE);
7478 add_AT_die_ref (stub, DW_AT_signature, type);
7480 else
7482 /* Otherwise, use a typedef with no name. */
7483 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7484 add_AT_die_ref (stub, DW_AT_type, type);
7487 stub->die_mark++;
7488 ref_p->stub = stub;
7490 return 1;
7493 /* DIE is a unit; look through all the DIE references to see if there are
7494 any external references to types, and if so, create local stubs for
7495 them which will be applied in build_abbrev_table. This is useful because
7496 references to local DIEs are smaller. */
7498 static external_ref_hash_type
7499 optimize_external_refs (dw_die_ref die)
7501 external_ref_hash_type map;
7502 map.create (10);
7503 optimize_external_refs_1 (die, map);
7504 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7505 return map;
7508 /* The format of each DIE (and its attribute value pairs) is encoded in an
7509 abbreviation table. This routine builds the abbreviation table and assigns
7510 a unique abbreviation id for each abbreviation entry. The children of each
7511 die are visited recursively. */
7513 static void
7514 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7516 unsigned long abbrev_id;
7517 unsigned int n_alloc;
7518 dw_die_ref c;
7519 dw_attr_ref a;
7520 unsigned ix;
7522 /* Scan the DIE references, and replace any that refer to
7523 DIEs from other CUs (i.e. those which are not marked) with
7524 the local stubs we built in optimize_external_refs. */
7525 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7526 if (AT_class (a) == dw_val_class_die_ref
7527 && (c = AT_ref (a))->die_mark == 0)
7529 struct external_ref *ref_p;
7530 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7532 ref_p = lookup_external_ref (extern_map, c);
7533 if (ref_p->stub && ref_p->stub != die)
7534 change_AT_die_ref (a, ref_p->stub);
7535 else
7536 /* We aren't changing this reference, so mark it external. */
7537 set_AT_ref_external (a, 1);
7540 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7542 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7543 dw_attr_ref die_a, abbrev_a;
7544 unsigned ix;
7545 bool ok = true;
7547 if (abbrev->die_tag != die->die_tag)
7548 continue;
7549 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7550 continue;
7552 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7553 continue;
7555 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7557 abbrev_a = &(*abbrev->die_attr)[ix];
7558 if ((abbrev_a->dw_attr != die_a->dw_attr)
7559 || (value_format (abbrev_a) != value_format (die_a)))
7561 ok = false;
7562 break;
7565 if (ok)
7566 break;
7569 if (abbrev_id >= abbrev_die_table_in_use)
7571 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7573 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7574 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7575 n_alloc);
7577 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7578 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7579 abbrev_die_table_allocated = n_alloc;
7582 ++abbrev_die_table_in_use;
7583 abbrev_die_table[abbrev_id] = die;
7586 die->die_abbrev = abbrev_id;
7587 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7590 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7592 static int
7593 constant_size (unsigned HOST_WIDE_INT value)
7595 int log;
7597 if (value == 0)
7598 log = 0;
7599 else
7600 log = floor_log2 (value);
7602 log = log / 8;
7603 log = 1 << (floor_log2 (log) + 1);
7605 return log;
7608 /* Return the size of a DIE as it is represented in the
7609 .debug_info section. */
7611 static unsigned long
7612 size_of_die (dw_die_ref die)
7614 unsigned long size = 0;
7615 dw_attr_ref a;
7616 unsigned ix;
7617 enum dwarf_form form;
7619 size += size_of_uleb128 (die->die_abbrev);
7620 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7622 switch (AT_class (a))
7624 case dw_val_class_addr:
7625 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7627 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7628 size += size_of_uleb128 (AT_index (a));
7630 else
7631 size += DWARF2_ADDR_SIZE;
7632 break;
7633 case dw_val_class_offset:
7634 size += DWARF_OFFSET_SIZE;
7635 break;
7636 case dw_val_class_loc:
7638 unsigned long lsize = size_of_locs (AT_loc (a));
7640 /* Block length. */
7641 if (dwarf_version >= 4)
7642 size += size_of_uleb128 (lsize);
7643 else
7644 size += constant_size (lsize);
7645 size += lsize;
7647 break;
7648 case dw_val_class_loc_list:
7649 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7651 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7652 size += size_of_uleb128 (AT_index (a));
7654 else
7655 size += DWARF_OFFSET_SIZE;
7656 break;
7657 case dw_val_class_range_list:
7658 size += DWARF_OFFSET_SIZE;
7659 break;
7660 case dw_val_class_const:
7661 size += size_of_sleb128 (AT_int (a));
7662 break;
7663 case dw_val_class_unsigned_const:
7665 int csize = constant_size (AT_unsigned (a));
7666 if (dwarf_version == 3
7667 && a->dw_attr == DW_AT_data_member_location
7668 && csize >= 4)
7669 size += size_of_uleb128 (AT_unsigned (a));
7670 else
7671 size += csize;
7673 break;
7674 case dw_val_class_const_double:
7675 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7676 if (HOST_BITS_PER_WIDE_INT >= 64)
7677 size++; /* block */
7678 break;
7679 case dw_val_class_vec:
7680 size += constant_size (a->dw_attr_val.v.val_vec.length
7681 * a->dw_attr_val.v.val_vec.elt_size)
7682 + a->dw_attr_val.v.val_vec.length
7683 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7684 break;
7685 case dw_val_class_flag:
7686 if (dwarf_version >= 4)
7687 /* Currently all add_AT_flag calls pass in 1 as last argument,
7688 so DW_FORM_flag_present can be used. If that ever changes,
7689 we'll need to use DW_FORM_flag and have some optimization
7690 in build_abbrev_table that will change those to
7691 DW_FORM_flag_present if it is set to 1 in all DIEs using
7692 the same abbrev entry. */
7693 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7694 else
7695 size += 1;
7696 break;
7697 case dw_val_class_die_ref:
7698 if (AT_ref_external (a))
7700 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7701 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7702 is sized by target address length, whereas in DWARF3
7703 it's always sized as an offset. */
7704 if (use_debug_types)
7705 size += DWARF_TYPE_SIGNATURE_SIZE;
7706 else if (dwarf_version == 2)
7707 size += DWARF2_ADDR_SIZE;
7708 else
7709 size += DWARF_OFFSET_SIZE;
7711 else
7712 size += DWARF_OFFSET_SIZE;
7713 break;
7714 case dw_val_class_fde_ref:
7715 size += DWARF_OFFSET_SIZE;
7716 break;
7717 case dw_val_class_lbl_id:
7718 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7720 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7721 size += size_of_uleb128 (AT_index (a));
7723 else
7724 size += DWARF2_ADDR_SIZE;
7725 break;
7726 case dw_val_class_lineptr:
7727 case dw_val_class_macptr:
7728 size += DWARF_OFFSET_SIZE;
7729 break;
7730 case dw_val_class_str:
7731 form = AT_string_form (a);
7732 if (form == DW_FORM_strp)
7733 size += DWARF_OFFSET_SIZE;
7734 else if (form == DW_FORM_GNU_str_index)
7735 size += size_of_uleb128 (AT_index (a));
7736 else
7737 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7738 break;
7739 case dw_val_class_file:
7740 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7741 break;
7742 case dw_val_class_data8:
7743 size += 8;
7744 break;
7745 case dw_val_class_vms_delta:
7746 size += DWARF_OFFSET_SIZE;
7747 break;
7748 case dw_val_class_high_pc:
7749 size += DWARF2_ADDR_SIZE;
7750 break;
7751 default:
7752 gcc_unreachable ();
7756 return size;
7759 /* Size the debugging information associated with a given DIE. Visits the
7760 DIE's children recursively. Updates the global variable next_die_offset, on
7761 each time through. Uses the current value of next_die_offset to update the
7762 die_offset field in each DIE. */
7764 static void
7765 calc_die_sizes (dw_die_ref die)
7767 dw_die_ref c;
7769 gcc_assert (die->die_offset == 0
7770 || (unsigned long int) die->die_offset == next_die_offset);
7771 die->die_offset = next_die_offset;
7772 next_die_offset += size_of_die (die);
7774 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7776 if (die->die_child != NULL)
7777 /* Count the null byte used to terminate sibling lists. */
7778 next_die_offset += 1;
7781 /* Size just the base type children at the start of the CU.
7782 This is needed because build_abbrev needs to size locs
7783 and sizing of type based stack ops needs to know die_offset
7784 values for the base types. */
7786 static void
7787 calc_base_type_die_sizes (void)
7789 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7790 unsigned int i;
7791 dw_die_ref base_type;
7792 #if ENABLE_ASSERT_CHECKING
7793 dw_die_ref prev = comp_unit_die ()->die_child;
7794 #endif
7796 die_offset += size_of_die (comp_unit_die ());
7797 for (i = 0; base_types.iterate (i, &base_type); i++)
7799 #if ENABLE_ASSERT_CHECKING
7800 gcc_assert (base_type->die_offset == 0
7801 && prev->die_sib == base_type
7802 && base_type->die_child == NULL
7803 && base_type->die_abbrev);
7804 prev = base_type;
7805 #endif
7806 base_type->die_offset = die_offset;
7807 die_offset += size_of_die (base_type);
7811 /* Set the marks for a die and its children. We do this so
7812 that we know whether or not a reference needs to use FORM_ref_addr; only
7813 DIEs in the same CU will be marked. We used to clear out the offset
7814 and use that as the flag, but ran into ordering problems. */
7816 static void
7817 mark_dies (dw_die_ref die)
7819 dw_die_ref c;
7821 gcc_assert (!die->die_mark);
7823 die->die_mark = 1;
7824 FOR_EACH_CHILD (die, c, mark_dies (c));
7827 /* Clear the marks for a die and its children. */
7829 static void
7830 unmark_dies (dw_die_ref die)
7832 dw_die_ref c;
7834 if (! use_debug_types)
7835 gcc_assert (die->die_mark);
7837 die->die_mark = 0;
7838 FOR_EACH_CHILD (die, c, unmark_dies (c));
7841 /* Clear the marks for a die, its children and referred dies. */
7843 static void
7844 unmark_all_dies (dw_die_ref die)
7846 dw_die_ref c;
7847 dw_attr_ref a;
7848 unsigned ix;
7850 if (!die->die_mark)
7851 return;
7852 die->die_mark = 0;
7854 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7856 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7857 if (AT_class (a) == dw_val_class_die_ref)
7858 unmark_all_dies (AT_ref (a));
7861 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7862 generated for the compilation unit. */
7864 static unsigned long
7865 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7867 unsigned long size;
7868 unsigned i;
7869 pubname_ref p;
7871 size = DWARF_PUBNAMES_HEADER_SIZE;
7872 FOR_EACH_VEC_ELT (*names, i, p)
7873 if (names != pubtype_table
7874 || p->die->die_offset != 0
7875 || !flag_eliminate_unused_debug_types)
7876 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7878 size += DWARF_OFFSET_SIZE;
7879 return size;
7882 /* Return the size of the information in the .debug_aranges section. */
7884 static unsigned long
7885 size_of_aranges (void)
7887 unsigned long size;
7889 size = DWARF_ARANGES_HEADER_SIZE;
7891 /* Count the address/length pair for this compilation unit. */
7892 if (text_section_used)
7893 size += 2 * DWARF2_ADDR_SIZE;
7894 if (cold_text_section_used)
7895 size += 2 * DWARF2_ADDR_SIZE;
7896 if (have_multiple_function_sections)
7898 unsigned fde_idx;
7899 dw_fde_ref fde;
7901 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7903 if (DECL_IGNORED_P (fde->decl))
7904 continue;
7905 if (!fde->in_std_section)
7906 size += 2 * DWARF2_ADDR_SIZE;
7907 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7908 size += 2 * DWARF2_ADDR_SIZE;
7912 /* Count the two zero words used to terminated the address range table. */
7913 size += 2 * DWARF2_ADDR_SIZE;
7914 return size;
7917 /* Select the encoding of an attribute value. */
7919 static enum dwarf_form
7920 value_format (dw_attr_ref a)
7922 switch (AT_class (a))
7924 case dw_val_class_addr:
7925 /* Only very few attributes allow DW_FORM_addr. */
7926 switch (a->dw_attr)
7928 case DW_AT_low_pc:
7929 case DW_AT_high_pc:
7930 case DW_AT_entry_pc:
7931 case DW_AT_trampoline:
7932 return (AT_index (a) == NOT_INDEXED
7933 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7934 default:
7935 break;
7937 switch (DWARF2_ADDR_SIZE)
7939 case 1:
7940 return DW_FORM_data1;
7941 case 2:
7942 return DW_FORM_data2;
7943 case 4:
7944 return DW_FORM_data4;
7945 case 8:
7946 return DW_FORM_data8;
7947 default:
7948 gcc_unreachable ();
7950 case dw_val_class_range_list:
7951 case dw_val_class_loc_list:
7952 if (dwarf_version >= 4)
7953 return DW_FORM_sec_offset;
7954 /* FALLTHRU */
7955 case dw_val_class_vms_delta:
7956 case dw_val_class_offset:
7957 switch (DWARF_OFFSET_SIZE)
7959 case 4:
7960 return DW_FORM_data4;
7961 case 8:
7962 return DW_FORM_data8;
7963 default:
7964 gcc_unreachable ();
7966 case dw_val_class_loc:
7967 if (dwarf_version >= 4)
7968 return DW_FORM_exprloc;
7969 switch (constant_size (size_of_locs (AT_loc (a))))
7971 case 1:
7972 return DW_FORM_block1;
7973 case 2:
7974 return DW_FORM_block2;
7975 case 4:
7976 return DW_FORM_block4;
7977 default:
7978 gcc_unreachable ();
7980 case dw_val_class_const:
7981 return DW_FORM_sdata;
7982 case dw_val_class_unsigned_const:
7983 switch (constant_size (AT_unsigned (a)))
7985 case 1:
7986 return DW_FORM_data1;
7987 case 2:
7988 return DW_FORM_data2;
7989 case 4:
7990 /* In DWARF3 DW_AT_data_member_location with
7991 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7992 constant, so we need to use DW_FORM_udata if we need
7993 a large constant. */
7994 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7995 return DW_FORM_udata;
7996 return DW_FORM_data4;
7997 case 8:
7998 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7999 return DW_FORM_udata;
8000 return DW_FORM_data8;
8001 default:
8002 gcc_unreachable ();
8004 case dw_val_class_const_double:
8005 switch (HOST_BITS_PER_WIDE_INT)
8007 case 8:
8008 return DW_FORM_data2;
8009 case 16:
8010 return DW_FORM_data4;
8011 case 32:
8012 return DW_FORM_data8;
8013 case 64:
8014 default:
8015 return DW_FORM_block1;
8017 case dw_val_class_vec:
8018 switch (constant_size (a->dw_attr_val.v.val_vec.length
8019 * a->dw_attr_val.v.val_vec.elt_size))
8021 case 1:
8022 return DW_FORM_block1;
8023 case 2:
8024 return DW_FORM_block2;
8025 case 4:
8026 return DW_FORM_block4;
8027 default:
8028 gcc_unreachable ();
8030 case dw_val_class_flag:
8031 if (dwarf_version >= 4)
8033 /* Currently all add_AT_flag calls pass in 1 as last argument,
8034 so DW_FORM_flag_present can be used. If that ever changes,
8035 we'll need to use DW_FORM_flag and have some optimization
8036 in build_abbrev_table that will change those to
8037 DW_FORM_flag_present if it is set to 1 in all DIEs using
8038 the same abbrev entry. */
8039 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8040 return DW_FORM_flag_present;
8042 return DW_FORM_flag;
8043 case dw_val_class_die_ref:
8044 if (AT_ref_external (a))
8045 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8046 else
8047 return DW_FORM_ref;
8048 case dw_val_class_fde_ref:
8049 return DW_FORM_data;
8050 case dw_val_class_lbl_id:
8051 return (AT_index (a) == NOT_INDEXED
8052 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8053 case dw_val_class_lineptr:
8054 case dw_val_class_macptr:
8055 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8056 case dw_val_class_str:
8057 return AT_string_form (a);
8058 case dw_val_class_file:
8059 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8061 case 1:
8062 return DW_FORM_data1;
8063 case 2:
8064 return DW_FORM_data2;
8065 case 4:
8066 return DW_FORM_data4;
8067 default:
8068 gcc_unreachable ();
8071 case dw_val_class_data8:
8072 return DW_FORM_data8;
8074 case dw_val_class_high_pc:
8075 switch (DWARF2_ADDR_SIZE)
8077 case 1:
8078 return DW_FORM_data1;
8079 case 2:
8080 return DW_FORM_data2;
8081 case 4:
8082 return DW_FORM_data4;
8083 case 8:
8084 return DW_FORM_data8;
8085 default:
8086 gcc_unreachable ();
8089 default:
8090 gcc_unreachable ();
8094 /* Output the encoding of an attribute value. */
8096 static void
8097 output_value_format (dw_attr_ref a)
8099 enum dwarf_form form = value_format (a);
8101 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8104 /* Given a die and id, produce the appropriate abbreviations. */
8106 static void
8107 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8109 unsigned ix;
8110 dw_attr_ref a_attr;
8112 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8113 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8114 dwarf_tag_name (abbrev->die_tag));
8116 if (abbrev->die_child != NULL)
8117 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8118 else
8119 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8121 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8123 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8124 dwarf_attr_name (a_attr->dw_attr));
8125 output_value_format (a_attr);
8128 dw2_asm_output_data (1, 0, NULL);
8129 dw2_asm_output_data (1, 0, NULL);
8133 /* Output the .debug_abbrev section which defines the DIE abbreviation
8134 table. */
8136 static void
8137 output_abbrev_section (void)
8139 unsigned long abbrev_id;
8141 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8142 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8144 /* Terminate the table. */
8145 dw2_asm_output_data (1, 0, NULL);
8148 /* Output a symbol we can use to refer to this DIE from another CU. */
8150 static inline void
8151 output_die_symbol (dw_die_ref die)
8153 const char *sym = die->die_id.die_symbol;
8155 gcc_assert (!die->comdat_type_p);
8157 if (sym == 0)
8158 return;
8160 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8161 /* We make these global, not weak; if the target doesn't support
8162 .linkonce, it doesn't support combining the sections, so debugging
8163 will break. */
8164 targetm.asm_out.globalize_label (asm_out_file, sym);
8166 ASM_OUTPUT_LABEL (asm_out_file, sym);
8169 /* Return a new location list, given the begin and end range, and the
8170 expression. */
8172 static inline dw_loc_list_ref
8173 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8174 const char *section)
8176 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8178 retlist->begin = begin;
8179 retlist->begin_entry = NULL;
8180 retlist->end = end;
8181 retlist->expr = expr;
8182 retlist->section = section;
8184 return retlist;
8187 /* Generate a new internal symbol for this location list node, if it
8188 hasn't got one yet. */
8190 static inline void
8191 gen_llsym (dw_loc_list_ref list)
8193 gcc_assert (!list->ll_symbol);
8194 list->ll_symbol = gen_internal_sym ("LLST");
8197 /* Output the location list given to us. */
8199 static void
8200 output_loc_list (dw_loc_list_ref list_head)
8202 dw_loc_list_ref curr = list_head;
8204 if (list_head->emitted)
8205 return;
8206 list_head->emitted = true;
8208 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8210 /* Walk the location list, and output each range + expression. */
8211 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8213 unsigned long size;
8214 /* Don't output an entry that starts and ends at the same address. */
8215 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8216 continue;
8217 size = size_of_locs (curr->expr);
8218 /* If the expression is too large, drop it on the floor. We could
8219 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8220 in the expression, but >= 64KB expressions for a single value
8221 in a single range are unlikely very useful. */
8222 if (size > 0xffff)
8223 continue;
8224 if (dwarf_split_debug_info)
8226 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8227 "Location list start/length entry (%s)",
8228 list_head->ll_symbol);
8229 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8230 "Location list range start index (%s)",
8231 curr->begin);
8232 /* The length field is 4 bytes. If we ever need to support
8233 an 8-byte length, we can add a new DW_LLE code or fall back
8234 to DW_LLE_GNU_start_end_entry. */
8235 dw2_asm_output_delta (4, curr->end, curr->begin,
8236 "Location list range length (%s)",
8237 list_head->ll_symbol);
8239 else if (!have_multiple_function_sections)
8241 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8242 "Location list begin address (%s)",
8243 list_head->ll_symbol);
8244 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8245 "Location list end address (%s)",
8246 list_head->ll_symbol);
8248 else
8250 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8251 "Location list begin address (%s)",
8252 list_head->ll_symbol);
8253 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8254 "Location list end address (%s)",
8255 list_head->ll_symbol);
8258 /* Output the block length for this list of location operations. */
8259 gcc_assert (size <= 0xffff);
8260 dw2_asm_output_data (2, size, "%s", "Location expression size");
8262 output_loc_sequence (curr->expr, -1);
8265 if (dwarf_split_debug_info)
8266 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8267 "Location list terminator (%s)",
8268 list_head->ll_symbol);
8269 else
8271 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8272 "Location list terminator begin (%s)",
8273 list_head->ll_symbol);
8274 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8275 "Location list terminator end (%s)",
8276 list_head->ll_symbol);
8280 /* Output a range_list offset into the debug_range section. Emit a
8281 relocated reference if val_entry is NULL, otherwise, emit an
8282 indirect reference. */
8284 static void
8285 output_range_list_offset (dw_attr_ref a)
8287 const char *name = dwarf_attr_name (a->dw_attr);
8289 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8291 char *p = strchr (ranges_section_label, '\0');
8292 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8293 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8294 debug_ranges_section, "%s", name);
8295 *p = '\0';
8297 else
8298 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8299 "%s (offset from %s)", name, ranges_section_label);
8302 /* Output the offset into the debug_loc section. */
8304 static void
8305 output_loc_list_offset (dw_attr_ref a)
8307 char *sym = AT_loc_list (a)->ll_symbol;
8309 gcc_assert (sym);
8310 if (dwarf_split_debug_info)
8311 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8312 "%s", dwarf_attr_name (a->dw_attr));
8313 else
8314 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8315 "%s", dwarf_attr_name (a->dw_attr));
8318 /* Output an attribute's index or value appropriately. */
8320 static void
8321 output_attr_index_or_value (dw_attr_ref a)
8323 const char *name = dwarf_attr_name (a->dw_attr);
8325 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8327 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8328 return;
8330 switch (AT_class (a))
8332 case dw_val_class_addr:
8333 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8334 break;
8335 case dw_val_class_high_pc:
8336 case dw_val_class_lbl_id:
8337 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8338 break;
8339 case dw_val_class_loc_list:
8340 output_loc_list_offset (a);
8341 break;
8342 default:
8343 gcc_unreachable ();
8347 /* Output a type signature. */
8349 static inline void
8350 output_signature (const char *sig, const char *name)
8352 int i;
8354 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8355 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8358 /* Output the DIE and its attributes. Called recursively to generate
8359 the definitions of each child DIE. */
8361 static void
8362 output_die (dw_die_ref die)
8364 dw_attr_ref a;
8365 dw_die_ref c;
8366 unsigned long size;
8367 unsigned ix;
8369 /* If someone in another CU might refer to us, set up a symbol for
8370 them to point to. */
8371 if (! die->comdat_type_p && die->die_id.die_symbol)
8372 output_die_symbol (die);
8374 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8375 (unsigned long)die->die_offset,
8376 dwarf_tag_name (die->die_tag));
8378 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8380 const char *name = dwarf_attr_name (a->dw_attr);
8382 switch (AT_class (a))
8384 case dw_val_class_addr:
8385 output_attr_index_or_value (a);
8386 break;
8388 case dw_val_class_offset:
8389 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8390 "%s", name);
8391 break;
8393 case dw_val_class_range_list:
8394 output_range_list_offset (a);
8395 break;
8397 case dw_val_class_loc:
8398 size = size_of_locs (AT_loc (a));
8400 /* Output the block length for this list of location operations. */
8401 if (dwarf_version >= 4)
8402 dw2_asm_output_data_uleb128 (size, "%s", name);
8403 else
8404 dw2_asm_output_data (constant_size (size), size, "%s", name);
8406 output_loc_sequence (AT_loc (a), -1);
8407 break;
8409 case dw_val_class_const:
8410 /* ??? It would be slightly more efficient to use a scheme like is
8411 used for unsigned constants below, but gdb 4.x does not sign
8412 extend. Gdb 5.x does sign extend. */
8413 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8414 break;
8416 case dw_val_class_unsigned_const:
8418 int csize = constant_size (AT_unsigned (a));
8419 if (dwarf_version == 3
8420 && a->dw_attr == DW_AT_data_member_location
8421 && csize >= 4)
8422 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8423 else
8424 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8426 break;
8428 case dw_val_class_const_double:
8430 unsigned HOST_WIDE_INT first, second;
8432 if (HOST_BITS_PER_WIDE_INT >= 64)
8433 dw2_asm_output_data (1,
8434 HOST_BITS_PER_DOUBLE_INT
8435 / HOST_BITS_PER_CHAR,
8436 NULL);
8438 if (WORDS_BIG_ENDIAN)
8440 first = a->dw_attr_val.v.val_double.high;
8441 second = a->dw_attr_val.v.val_double.low;
8443 else
8445 first = a->dw_attr_val.v.val_double.low;
8446 second = a->dw_attr_val.v.val_double.high;
8449 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8450 first, "%s", name);
8451 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8452 second, NULL);
8454 break;
8456 case dw_val_class_vec:
8458 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8459 unsigned int len = a->dw_attr_val.v.val_vec.length;
8460 unsigned int i;
8461 unsigned char *p;
8463 dw2_asm_output_data (constant_size (len * elt_size),
8464 len * elt_size, "%s", name);
8465 if (elt_size > sizeof (HOST_WIDE_INT))
8467 elt_size /= 2;
8468 len *= 2;
8470 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8471 i < len;
8472 i++, p += elt_size)
8473 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8474 "fp or vector constant word %u", i);
8475 break;
8478 case dw_val_class_flag:
8479 if (dwarf_version >= 4)
8481 /* Currently all add_AT_flag calls pass in 1 as last argument,
8482 so DW_FORM_flag_present can be used. If that ever changes,
8483 we'll need to use DW_FORM_flag and have some optimization
8484 in build_abbrev_table that will change those to
8485 DW_FORM_flag_present if it is set to 1 in all DIEs using
8486 the same abbrev entry. */
8487 gcc_assert (AT_flag (a) == 1);
8488 if (flag_debug_asm)
8489 fprintf (asm_out_file, "\t\t\t%s %s\n",
8490 ASM_COMMENT_START, name);
8491 break;
8493 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8494 break;
8496 case dw_val_class_loc_list:
8497 output_attr_index_or_value (a);
8498 break;
8500 case dw_val_class_die_ref:
8501 if (AT_ref_external (a))
8503 if (AT_ref (a)->comdat_type_p)
8505 comdat_type_node_ref type_node =
8506 AT_ref (a)->die_id.die_type_node;
8508 gcc_assert (type_node);
8509 output_signature (type_node->signature, name);
8511 else
8513 const char *sym = AT_ref (a)->die_id.die_symbol;
8514 int size;
8516 gcc_assert (sym);
8517 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8518 length, whereas in DWARF3 it's always sized as an
8519 offset. */
8520 if (dwarf_version == 2)
8521 size = DWARF2_ADDR_SIZE;
8522 else
8523 size = DWARF_OFFSET_SIZE;
8524 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8525 name);
8528 else
8530 gcc_assert (AT_ref (a)->die_offset);
8531 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8532 "%s", name);
8534 break;
8536 case dw_val_class_fde_ref:
8538 char l1[20];
8540 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8541 a->dw_attr_val.v.val_fde_index * 2);
8542 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8543 "%s", name);
8545 break;
8547 case dw_val_class_vms_delta:
8548 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8549 AT_vms_delta2 (a), AT_vms_delta1 (a),
8550 "%s", name);
8551 break;
8553 case dw_val_class_lbl_id:
8554 output_attr_index_or_value (a);
8555 break;
8557 case dw_val_class_lineptr:
8558 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8559 debug_line_section, "%s", name);
8560 break;
8562 case dw_val_class_macptr:
8563 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8564 debug_macinfo_section, "%s", name);
8565 break;
8567 case dw_val_class_str:
8568 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8569 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8570 a->dw_attr_val.v.val_str->label,
8571 debug_str_section,
8572 "%s: \"%s\"", name, AT_string (a));
8573 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8574 dw2_asm_output_data_uleb128 (AT_index (a),
8575 "%s: \"%s\"", name, AT_string (a));
8576 else
8577 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8578 break;
8580 case dw_val_class_file:
8582 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8584 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8585 a->dw_attr_val.v.val_file->filename);
8586 break;
8589 case dw_val_class_data8:
8591 int i;
8593 for (i = 0; i < 8; i++)
8594 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8595 i == 0 ? "%s" : NULL, name);
8596 break;
8599 case dw_val_class_high_pc:
8600 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8601 get_AT_low_pc (die), "DW_AT_high_pc");
8602 break;
8604 default:
8605 gcc_unreachable ();
8609 FOR_EACH_CHILD (die, c, output_die (c));
8611 /* Add null byte to terminate sibling list. */
8612 if (die->die_child != NULL)
8613 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8614 (unsigned long) die->die_offset);
8617 /* Output the compilation unit that appears at the beginning of the
8618 .debug_info section, and precedes the DIE descriptions. */
8620 static void
8621 output_compilation_unit_header (void)
8623 int ver = dwarf_version;
8625 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8626 dw2_asm_output_data (4, 0xffffffff,
8627 "Initial length escape value indicating 64-bit DWARF extension");
8628 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8629 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8630 "Length of Compilation Unit Info");
8631 dw2_asm_output_data (2, ver, "DWARF version number");
8632 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8633 debug_abbrev_section,
8634 "Offset Into Abbrev. Section");
8635 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8638 /* Output the compilation unit DIE and its children. */
8640 static void
8641 output_comp_unit (dw_die_ref die, int output_if_empty)
8643 const char *secname, *oldsym;
8644 char *tmp;
8645 external_ref_hash_type extern_map;
8647 /* Unless we are outputting main CU, we may throw away empty ones. */
8648 if (!output_if_empty && die->die_child == NULL)
8649 return;
8651 /* Even if there are no children of this DIE, we must output the information
8652 about the compilation unit. Otherwise, on an empty translation unit, we
8653 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8654 will then complain when examining the file. First mark all the DIEs in
8655 this CU so we know which get local refs. */
8656 mark_dies (die);
8658 extern_map = optimize_external_refs (die);
8660 build_abbrev_table (die, extern_map);
8662 extern_map.dispose ();
8664 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8665 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8666 calc_die_sizes (die);
8668 oldsym = die->die_id.die_symbol;
8669 if (oldsym)
8671 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8673 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8674 secname = tmp;
8675 die->die_id.die_symbol = NULL;
8676 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8678 else
8680 switch_to_section (debug_info_section);
8681 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8682 info_section_emitted = true;
8685 /* Output debugging information. */
8686 output_compilation_unit_header ();
8687 output_die (die);
8689 /* Leave the marks on the main CU, so we can check them in
8690 output_pubnames. */
8691 if (oldsym)
8693 unmark_dies (die);
8694 die->die_id.die_symbol = oldsym;
8698 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8699 and .debug_pubtypes. This is configured per-target, but can be
8700 overridden by the -gpubnames or -gno-pubnames options. */
8702 static inline bool
8703 want_pubnames (void)
8705 return (debug_generate_pub_sections != -1
8706 ? debug_generate_pub_sections
8707 : targetm.want_debug_pub_sections);
8710 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8712 static void
8713 add_AT_pubnames (dw_die_ref die)
8715 if (want_pubnames ())
8716 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8719 /* Add a string attribute value to a skeleton DIE. */
8721 static inline void
8722 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8723 const char *str)
8725 dw_attr_node attr;
8726 struct indirect_string_node *node;
8728 if (! skeleton_debug_str_hash)
8729 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8730 debug_str_eq, NULL);
8732 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8733 find_string_form (node);
8734 if (node->form == DW_FORM_GNU_str_index)
8735 node->form = DW_FORM_strp;
8737 attr.dw_attr = attr_kind;
8738 attr.dw_attr_val.val_class = dw_val_class_str;
8739 attr.dw_attr_val.val_entry = NULL;
8740 attr.dw_attr_val.v.val_str = node;
8741 add_dwarf_attr (die, &attr);
8744 /* Helper function to generate top-level dies for skeleton debug_info and
8745 debug_types. */
8747 static void
8748 add_top_level_skeleton_die_attrs (dw_die_ref die)
8750 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8751 const char *comp_dir = comp_dir_string ();
8753 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8754 if (comp_dir != NULL)
8755 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8756 add_AT_pubnames (die);
8757 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8760 /* Return the single type-unit die for skeleton type units. */
8762 static dw_die_ref
8763 get_skeleton_type_unit (void)
8765 /* For dwarf_split_debug_sections with use_type info, all type units in the
8766 skeleton sections have identical dies (but different headers). This
8767 single die will be output many times. */
8769 static dw_die_ref skeleton_type_unit = NULL;
8771 if (skeleton_type_unit == NULL)
8773 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8774 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8775 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8777 return skeleton_type_unit;
8780 /* Output skeleton debug sections that point to the dwo file. */
8782 static void
8783 output_skeleton_debug_sections (dw_die_ref comp_unit)
8785 /* These attributes will be found in the full debug_info section. */
8786 remove_AT (comp_unit, DW_AT_producer);
8787 remove_AT (comp_unit, DW_AT_language);
8789 switch_to_section (debug_skeleton_info_section);
8790 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8792 /* Produce the skeleton compilation-unit header. This one differs enough from
8793 a normal CU header that it's better not to call output_compilation_unit
8794 header. */
8795 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8796 dw2_asm_output_data (4, 0xffffffff,
8797 "Initial length escape value indicating 64-bit DWARF extension");
8799 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8800 DWARF_COMPILE_UNIT_HEADER_SIZE
8801 - DWARF_INITIAL_LENGTH_SIZE
8802 + size_of_die (comp_unit),
8803 "Length of Compilation Unit Info");
8804 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8805 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8806 debug_abbrev_section,
8807 "Offset Into Abbrev. Section");
8808 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8810 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8811 output_die (comp_unit);
8813 /* Build the skeleton debug_abbrev section. */
8814 switch_to_section (debug_skeleton_abbrev_section);
8815 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8817 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8818 if (use_debug_types)
8819 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8821 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8824 /* Output a comdat type unit DIE and its children. */
8826 static void
8827 output_comdat_type_unit (comdat_type_node *node)
8829 const char *secname;
8830 char *tmp;
8831 int i;
8832 #if defined (OBJECT_FORMAT_ELF)
8833 tree comdat_key;
8834 #endif
8835 external_ref_hash_type extern_map;
8837 /* First mark all the DIEs in this CU so we know which get local refs. */
8838 mark_dies (node->root_die);
8840 extern_map = optimize_external_refs (node->root_die);
8842 build_abbrev_table (node->root_die, extern_map);
8844 extern_map.dispose ();
8846 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8847 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8848 calc_die_sizes (node->root_die);
8850 #if defined (OBJECT_FORMAT_ELF)
8851 if (!dwarf_split_debug_info)
8852 secname = ".debug_types";
8853 else
8854 secname = ".debug_types.dwo";
8856 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8857 sprintf (tmp, "wt.");
8858 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8859 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8860 comdat_key = get_identifier (tmp);
8861 targetm.asm_out.named_section (secname,
8862 SECTION_DEBUG | SECTION_LINKONCE,
8863 comdat_key);
8864 #else
8865 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8866 sprintf (tmp, ".gnu.linkonce.wt.");
8867 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8868 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8869 secname = tmp;
8870 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8871 #endif
8873 /* Output debugging information. */
8874 output_compilation_unit_header ();
8875 output_signature (node->signature, "Type Signature");
8876 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8877 "Offset to Type DIE");
8878 output_die (node->root_die);
8880 unmark_dies (node->root_die);
8882 #if defined (OBJECT_FORMAT_ELF)
8883 if (dwarf_split_debug_info)
8885 /* Produce the skeleton type-unit header. */
8886 const char *secname = ".debug_types";
8888 targetm.asm_out.named_section (secname,
8889 SECTION_DEBUG | SECTION_LINKONCE,
8890 comdat_key);
8891 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8892 dw2_asm_output_data (4, 0xffffffff,
8893 "Initial length escape value indicating 64-bit DWARF extension");
8895 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8896 DWARF_COMPILE_UNIT_HEADER_SIZE
8897 - DWARF_INITIAL_LENGTH_SIZE
8898 + size_of_die (get_skeleton_type_unit ())
8899 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8900 "Length of Type Unit Info");
8901 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8902 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8903 debug_skeleton_abbrev_section_label,
8904 debug_abbrev_section,
8905 "Offset Into Abbrev. Section");
8906 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8907 output_signature (node->signature, "Type Signature");
8908 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8910 output_die (get_skeleton_type_unit ());
8912 #endif
8915 /* Return the DWARF2/3 pubname associated with a decl. */
8917 static const char *
8918 dwarf2_name (tree decl, int scope)
8920 if (DECL_NAMELESS (decl))
8921 return NULL;
8922 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8925 /* Add a new entry to .debug_pubnames if appropriate. */
8927 static void
8928 add_pubname_string (const char *str, dw_die_ref die)
8930 pubname_entry e;
8932 e.die = die;
8933 e.name = xstrdup (str);
8934 vec_safe_push (pubname_table, e);
8937 static void
8938 add_pubname (tree decl, dw_die_ref die)
8940 if (!want_pubnames ())
8941 return;
8943 /* Don't add items to the table when we expect that the consumer will have
8944 just read the enclosing die. For example, if the consumer is looking at a
8945 class_member, it will either be inside the class already, or will have just
8946 looked up the class to find the member. Either way, searching the class is
8947 faster than searching the index. */
8948 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8949 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8951 const char *name = dwarf2_name (decl, 1);
8953 if (name)
8954 add_pubname_string (name, die);
8958 /* Add an enumerator to the pubnames section. */
8960 static void
8961 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8963 pubname_entry e;
8965 gcc_assert (scope_name);
8966 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8967 e.die = die;
8968 vec_safe_push (pubname_table, e);
8971 /* Add a new entry to .debug_pubtypes if appropriate. */
8973 static void
8974 add_pubtype (tree decl, dw_die_ref die)
8976 pubname_entry e;
8978 if (!want_pubnames ())
8979 return;
8981 if ((TREE_PUBLIC (decl)
8982 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8983 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8985 tree scope = NULL;
8986 const char *scope_name = "";
8987 const char *sep = is_cxx () ? "::" : ".";
8988 const char *name;
8990 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8991 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8993 scope_name = lang_hooks.dwarf_name (scope, 1);
8994 if (scope_name != NULL && scope_name[0] != '\0')
8995 scope_name = concat (scope_name, sep, NULL);
8996 else
8997 scope_name = "";
9000 if (TYPE_P (decl))
9001 name = type_tag (decl);
9002 else
9003 name = lang_hooks.dwarf_name (decl, 1);
9005 /* If we don't have a name for the type, there's no point in adding
9006 it to the table. */
9007 if (name != NULL && name[0] != '\0')
9009 e.die = die;
9010 e.name = concat (scope_name, name, NULL);
9011 vec_safe_push (pubtype_table, e);
9014 /* Although it might be more consistent to add the pubinfo for the
9015 enumerators as their dies are created, they should only be added if the
9016 enum type meets the criteria above. So rather than re-check the parent
9017 enum type whenever an enumerator die is created, just output them all
9018 here. This isn't protected by the name conditional because anonymous
9019 enums don't have names. */
9020 if (die->die_tag == DW_TAG_enumeration_type)
9022 dw_die_ref c;
9024 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9029 /* Output the public names table used to speed up access to externally
9030 visible names; or the public types table used to find type definitions. */
9032 static void
9033 output_pubnames (vec<pubname_entry, va_gc> *names)
9035 unsigned i;
9036 unsigned long pubnames_length = size_of_pubnames (names);
9037 pubname_ref pub;
9039 if (!want_pubnames () || !info_section_emitted)
9040 return;
9041 if (names == pubname_table)
9042 switch_to_section (debug_pubnames_section);
9043 else
9044 switch_to_section (debug_pubtypes_section);
9045 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9046 dw2_asm_output_data (4, 0xffffffff,
9047 "Initial length escape value indicating 64-bit DWARF extension");
9048 if (names == pubname_table)
9049 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9050 "Length of Public Names Info");
9051 else
9052 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9053 "Length of Public Type Names Info");
9054 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
9055 dw2_asm_output_data (2, 2, "DWARF Version");
9056 if (dwarf_split_debug_info)
9057 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9058 debug_skeleton_info_section,
9059 "Offset of Compilation Unit Info");
9060 else
9061 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9062 debug_info_section,
9063 "Offset of Compilation Unit Info");
9064 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9065 "Compilation Unit Length");
9067 FOR_EACH_VEC_ELT (*names, i, pub)
9069 /* Enumerator names are part of the pubname table, but the parent
9070 DW_TAG_enumeration_type die may have been pruned. Don't output
9071 them if that is the case. */
9072 if (pub->die->die_tag == DW_TAG_enumerator &&
9073 (pub->die->die_parent == NULL
9074 || !pub->die->die_parent->die_perennial_p))
9075 continue;
9077 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9078 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9079 gcc_assert (pub->die->die_mark);
9081 if (names != pubtype_table
9082 || pub->die->die_offset != 0
9083 || !flag_eliminate_unused_debug_types)
9085 dw_offset die_offset = pub->die->die_offset;
9087 /* If we're putting types in their own .debug_types sections,
9088 the .debug_pubtypes table will still point to the compile
9089 unit (not the type unit), so we want to use the offset of
9090 the skeleton DIE (if there is one). */
9091 if (pub->die->comdat_type_p && names == pubtype_table)
9093 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9095 if (type_node != NULL)
9096 die_offset = (type_node->skeleton_die != NULL
9097 ? type_node->skeleton_die->die_offset
9098 : 0);
9101 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9103 dw2_asm_output_nstring (pub->name, -1, "external name");
9107 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9110 /* Output the information that goes into the .debug_aranges table.
9111 Namely, define the beginning and ending address range of the
9112 text section generated for this compilation unit. */
9114 static void
9115 output_aranges (unsigned long aranges_length)
9117 unsigned i;
9119 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9120 dw2_asm_output_data (4, 0xffffffff,
9121 "Initial length escape value indicating 64-bit DWARF extension");
9122 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9123 "Length of Address Ranges Info");
9124 /* Version number for aranges is still 2, even in DWARF3. */
9125 dw2_asm_output_data (2, 2, "DWARF Version");
9126 if (dwarf_split_debug_info)
9127 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9128 debug_skeleton_info_section,
9129 "Offset of Compilation Unit Info");
9130 else
9131 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9132 debug_info_section,
9133 "Offset of Compilation Unit Info");
9134 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9135 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9137 /* We need to align to twice the pointer size here. */
9138 if (DWARF_ARANGES_PAD_SIZE)
9140 /* Pad using a 2 byte words so that padding is correct for any
9141 pointer size. */
9142 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9143 2 * DWARF2_ADDR_SIZE);
9144 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9145 dw2_asm_output_data (2, 0, NULL);
9148 /* It is necessary not to output these entries if the sections were
9149 not used; if the sections were not used, the length will be 0 and
9150 the address may end up as 0 if the section is discarded by ld
9151 --gc-sections, leaving an invalid (0, 0) entry that can be
9152 confused with the terminator. */
9153 if (text_section_used)
9155 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9156 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9157 text_section_label, "Length");
9159 if (cold_text_section_used)
9161 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9162 "Address");
9163 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9164 cold_text_section_label, "Length");
9167 if (have_multiple_function_sections)
9169 unsigned fde_idx;
9170 dw_fde_ref fde;
9172 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9174 if (DECL_IGNORED_P (fde->decl))
9175 continue;
9176 if (!fde->in_std_section)
9178 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9179 "Address");
9180 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9181 fde->dw_fde_begin, "Length");
9183 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9185 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9186 "Address");
9187 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9188 fde->dw_fde_second_begin, "Length");
9193 /* Output the terminator words. */
9194 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9195 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9198 /* Add a new entry to .debug_ranges. Return the offset at which it
9199 was placed. */
9201 static unsigned int
9202 add_ranges_num (int num)
9204 unsigned int in_use = ranges_table_in_use;
9206 if (in_use == ranges_table_allocated)
9208 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9209 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9210 ranges_table_allocated);
9211 memset (ranges_table + ranges_table_in_use, 0,
9212 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9215 ranges_table[in_use].num = num;
9216 ranges_table_in_use = in_use + 1;
9218 return in_use * 2 * DWARF2_ADDR_SIZE;
9221 /* Add a new entry to .debug_ranges corresponding to a block, or a
9222 range terminator if BLOCK is NULL. */
9224 static unsigned int
9225 add_ranges (const_tree block)
9227 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9230 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9231 When using dwarf_split_debug_info, address attributes in dies destined
9232 for the final executable should be direct references--setting the
9233 parameter force_direct ensures this behavior. */
9235 static void
9236 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9237 bool *added, bool force_direct)
9239 unsigned int in_use = ranges_by_label_in_use;
9240 unsigned int offset;
9242 if (in_use == ranges_by_label_allocated)
9244 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9245 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9246 ranges_by_label,
9247 ranges_by_label_allocated);
9248 memset (ranges_by_label + ranges_by_label_in_use, 0,
9249 RANGES_TABLE_INCREMENT
9250 * sizeof (struct dw_ranges_by_label_struct));
9253 ranges_by_label[in_use].begin = begin;
9254 ranges_by_label[in_use].end = end;
9255 ranges_by_label_in_use = in_use + 1;
9257 offset = add_ranges_num (-(int)in_use - 1);
9258 if (!*added)
9260 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9261 *added = true;
9265 static void
9266 output_ranges (void)
9268 unsigned i;
9269 static const char *const start_fmt = "Offset %#x";
9270 const char *fmt = start_fmt;
9272 for (i = 0; i < ranges_table_in_use; i++)
9274 int block_num = ranges_table[i].num;
9276 if (block_num > 0)
9278 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9279 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9281 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9282 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9284 /* If all code is in the text section, then the compilation
9285 unit base address defaults to DW_AT_low_pc, which is the
9286 base of the text section. */
9287 if (!have_multiple_function_sections)
9289 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9290 text_section_label,
9291 fmt, i * 2 * DWARF2_ADDR_SIZE);
9292 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9293 text_section_label, NULL);
9296 /* Otherwise, the compilation unit base address is zero,
9297 which allows us to use absolute addresses, and not worry
9298 about whether the target supports cross-section
9299 arithmetic. */
9300 else
9302 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9303 fmt, i * 2 * DWARF2_ADDR_SIZE);
9304 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9307 fmt = NULL;
9310 /* Negative block_num stands for an index into ranges_by_label. */
9311 else if (block_num < 0)
9313 int lab_idx = - block_num - 1;
9315 if (!have_multiple_function_sections)
9317 gcc_unreachable ();
9318 #if 0
9319 /* If we ever use add_ranges_by_labels () for a single
9320 function section, all we have to do is to take out
9321 the #if 0 above. */
9322 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9323 ranges_by_label[lab_idx].begin,
9324 text_section_label,
9325 fmt, i * 2 * DWARF2_ADDR_SIZE);
9326 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9327 ranges_by_label[lab_idx].end,
9328 text_section_label, NULL);
9329 #endif
9331 else
9333 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9334 ranges_by_label[lab_idx].begin,
9335 fmt, i * 2 * DWARF2_ADDR_SIZE);
9336 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9337 ranges_by_label[lab_idx].end,
9338 NULL);
9341 else
9343 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9344 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9345 fmt = start_fmt;
9350 /* Data structure containing information about input files. */
9351 struct file_info
9353 const char *path; /* Complete file name. */
9354 const char *fname; /* File name part. */
9355 int length; /* Length of entire string. */
9356 struct dwarf_file_data * file_idx; /* Index in input file table. */
9357 int dir_idx; /* Index in directory table. */
9360 /* Data structure containing information about directories with source
9361 files. */
9362 struct dir_info
9364 const char *path; /* Path including directory name. */
9365 int length; /* Path length. */
9366 int prefix; /* Index of directory entry which is a prefix. */
9367 int count; /* Number of files in this directory. */
9368 int dir_idx; /* Index of directory used as base. */
9371 /* Callback function for file_info comparison. We sort by looking at
9372 the directories in the path. */
9374 static int
9375 file_info_cmp (const void *p1, const void *p2)
9377 const struct file_info *const s1 = (const struct file_info *) p1;
9378 const struct file_info *const s2 = (const struct file_info *) p2;
9379 const unsigned char *cp1;
9380 const unsigned char *cp2;
9382 /* Take care of file names without directories. We need to make sure that
9383 we return consistent values to qsort since some will get confused if
9384 we return the same value when identical operands are passed in opposite
9385 orders. So if neither has a directory, return 0 and otherwise return
9386 1 or -1 depending on which one has the directory. */
9387 if ((s1->path == s1->fname || s2->path == s2->fname))
9388 return (s2->path == s2->fname) - (s1->path == s1->fname);
9390 cp1 = (const unsigned char *) s1->path;
9391 cp2 = (const unsigned char *) s2->path;
9393 while (1)
9395 ++cp1;
9396 ++cp2;
9397 /* Reached the end of the first path? If so, handle like above. */
9398 if ((cp1 == (const unsigned char *) s1->fname)
9399 || (cp2 == (const unsigned char *) s2->fname))
9400 return ((cp2 == (const unsigned char *) s2->fname)
9401 - (cp1 == (const unsigned char *) s1->fname));
9403 /* Character of current path component the same? */
9404 else if (*cp1 != *cp2)
9405 return *cp1 - *cp2;
9409 struct file_name_acquire_data
9411 struct file_info *files;
9412 int used_files;
9413 int max_files;
9416 /* Traversal function for the hash table. */
9418 static int
9419 file_name_acquire (void ** slot, void *data)
9421 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9422 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9423 struct file_info *fi;
9424 const char *f;
9426 gcc_assert (fnad->max_files >= d->emitted_number);
9428 if (! d->emitted_number)
9429 return 1;
9431 gcc_assert (fnad->max_files != fnad->used_files);
9433 fi = fnad->files + fnad->used_files++;
9435 /* Skip all leading "./". */
9436 f = d->filename;
9437 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9438 f += 2;
9440 /* Create a new array entry. */
9441 fi->path = f;
9442 fi->length = strlen (f);
9443 fi->file_idx = d;
9445 /* Search for the file name part. */
9446 f = strrchr (f, DIR_SEPARATOR);
9447 #if defined (DIR_SEPARATOR_2)
9449 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9451 if (g != NULL)
9453 if (f == NULL || f < g)
9454 f = g;
9457 #endif
9459 fi->fname = f == NULL ? fi->path : f + 1;
9460 return 1;
9463 /* Output the directory table and the file name table. We try to minimize
9464 the total amount of memory needed. A heuristic is used to avoid large
9465 slowdowns with many input files. */
9467 static void
9468 output_file_names (void)
9470 struct file_name_acquire_data fnad;
9471 int numfiles;
9472 struct file_info *files;
9473 struct dir_info *dirs;
9474 int *saved;
9475 int *savehere;
9476 int *backmap;
9477 int ndirs;
9478 int idx_offset;
9479 int i;
9481 if (!last_emitted_file)
9483 dw2_asm_output_data (1, 0, "End directory table");
9484 dw2_asm_output_data (1, 0, "End file name table");
9485 return;
9488 numfiles = last_emitted_file->emitted_number;
9490 /* Allocate the various arrays we need. */
9491 files = XALLOCAVEC (struct file_info, numfiles);
9492 dirs = XALLOCAVEC (struct dir_info, numfiles);
9494 fnad.files = files;
9495 fnad.used_files = 0;
9496 fnad.max_files = numfiles;
9497 htab_traverse (file_table, file_name_acquire, &fnad);
9498 gcc_assert (fnad.used_files == fnad.max_files);
9500 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9502 /* Find all the different directories used. */
9503 dirs[0].path = files[0].path;
9504 dirs[0].length = files[0].fname - files[0].path;
9505 dirs[0].prefix = -1;
9506 dirs[0].count = 1;
9507 dirs[0].dir_idx = 0;
9508 files[0].dir_idx = 0;
9509 ndirs = 1;
9511 for (i = 1; i < numfiles; i++)
9512 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9513 && memcmp (dirs[ndirs - 1].path, files[i].path,
9514 dirs[ndirs - 1].length) == 0)
9516 /* Same directory as last entry. */
9517 files[i].dir_idx = ndirs - 1;
9518 ++dirs[ndirs - 1].count;
9520 else
9522 int j;
9524 /* This is a new directory. */
9525 dirs[ndirs].path = files[i].path;
9526 dirs[ndirs].length = files[i].fname - files[i].path;
9527 dirs[ndirs].count = 1;
9528 dirs[ndirs].dir_idx = ndirs;
9529 files[i].dir_idx = ndirs;
9531 /* Search for a prefix. */
9532 dirs[ndirs].prefix = -1;
9533 for (j = 0; j < ndirs; j++)
9534 if (dirs[j].length < dirs[ndirs].length
9535 && dirs[j].length > 1
9536 && (dirs[ndirs].prefix == -1
9537 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9538 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9539 dirs[ndirs].prefix = j;
9541 ++ndirs;
9544 /* Now to the actual work. We have to find a subset of the directories which
9545 allow expressing the file name using references to the directory table
9546 with the least amount of characters. We do not do an exhaustive search
9547 where we would have to check out every combination of every single
9548 possible prefix. Instead we use a heuristic which provides nearly optimal
9549 results in most cases and never is much off. */
9550 saved = XALLOCAVEC (int, ndirs);
9551 savehere = XALLOCAVEC (int, ndirs);
9553 memset (saved, '\0', ndirs * sizeof (saved[0]));
9554 for (i = 0; i < ndirs; i++)
9556 int j;
9557 int total;
9559 /* We can always save some space for the current directory. But this
9560 does not mean it will be enough to justify adding the directory. */
9561 savehere[i] = dirs[i].length;
9562 total = (savehere[i] - saved[i]) * dirs[i].count;
9564 for (j = i + 1; j < ndirs; j++)
9566 savehere[j] = 0;
9567 if (saved[j] < dirs[i].length)
9569 /* Determine whether the dirs[i] path is a prefix of the
9570 dirs[j] path. */
9571 int k;
9573 k = dirs[j].prefix;
9574 while (k != -1 && k != (int) i)
9575 k = dirs[k].prefix;
9577 if (k == (int) i)
9579 /* Yes it is. We can possibly save some memory by
9580 writing the filenames in dirs[j] relative to
9581 dirs[i]. */
9582 savehere[j] = dirs[i].length;
9583 total += (savehere[j] - saved[j]) * dirs[j].count;
9588 /* Check whether we can save enough to justify adding the dirs[i]
9589 directory. */
9590 if (total > dirs[i].length + 1)
9592 /* It's worthwhile adding. */
9593 for (j = i; j < ndirs; j++)
9594 if (savehere[j] > 0)
9596 /* Remember how much we saved for this directory so far. */
9597 saved[j] = savehere[j];
9599 /* Remember the prefix directory. */
9600 dirs[j].dir_idx = i;
9605 /* Emit the directory name table. */
9606 idx_offset = dirs[0].length > 0 ? 1 : 0;
9607 for (i = 1 - idx_offset; i < ndirs; i++)
9608 dw2_asm_output_nstring (dirs[i].path,
9609 dirs[i].length
9610 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9611 "Directory Entry: %#x", i + idx_offset);
9613 dw2_asm_output_data (1, 0, "End directory table");
9615 /* We have to emit them in the order of emitted_number since that's
9616 used in the debug info generation. To do this efficiently we
9617 generate a back-mapping of the indices first. */
9618 backmap = XALLOCAVEC (int, numfiles);
9619 for (i = 0; i < numfiles; i++)
9620 backmap[files[i].file_idx->emitted_number - 1] = i;
9622 /* Now write all the file names. */
9623 for (i = 0; i < numfiles; i++)
9625 int file_idx = backmap[i];
9626 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9628 #ifdef VMS_DEBUGGING_INFO
9629 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9631 /* Setting these fields can lead to debugger miscomparisons,
9632 but VMS Debug requires them to be set correctly. */
9634 int ver;
9635 long long cdt;
9636 long siz;
9637 int maxfilelen = strlen (files[file_idx].path)
9638 + dirs[dir_idx].length
9639 + MAX_VMS_VERSION_LEN + 1;
9640 char *filebuf = XALLOCAVEC (char, maxfilelen);
9642 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9643 snprintf (filebuf, maxfilelen, "%s;%d",
9644 files[file_idx].path + dirs[dir_idx].length, ver);
9646 dw2_asm_output_nstring
9647 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9649 /* Include directory index. */
9650 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9652 /* Modification time. */
9653 dw2_asm_output_data_uleb128
9654 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9655 ? cdt : 0,
9656 NULL);
9658 /* File length in bytes. */
9659 dw2_asm_output_data_uleb128
9660 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9661 ? siz : 0,
9662 NULL);
9663 #else
9664 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9665 "File Entry: %#x", (unsigned) i + 1);
9667 /* Include directory index. */
9668 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9670 /* Modification time. */
9671 dw2_asm_output_data_uleb128 (0, NULL);
9673 /* File length in bytes. */
9674 dw2_asm_output_data_uleb128 (0, NULL);
9675 #endif /* VMS_DEBUGGING_INFO */
9678 dw2_asm_output_data (1, 0, "End file name table");
9682 /* Output one line number table into the .debug_line section. */
9684 static void
9685 output_one_line_info_table (dw_line_info_table *table)
9687 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9688 unsigned int current_line = 1;
9689 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9690 dw_line_info_entry *ent;
9691 size_t i;
9693 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9695 switch (ent->opcode)
9697 case LI_set_address:
9698 /* ??? Unfortunately, we have little choice here currently, and
9699 must always use the most general form. GCC does not know the
9700 address delta itself, so we can't use DW_LNS_advance_pc. Many
9701 ports do have length attributes which will give an upper bound
9702 on the address range. We could perhaps use length attributes
9703 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9704 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9706 /* This can handle any delta. This takes
9707 4+DWARF2_ADDR_SIZE bytes. */
9708 dw2_asm_output_data (1, 0, "set address %s", line_label);
9709 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9710 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9711 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9712 break;
9714 case LI_set_line:
9715 if (ent->val == current_line)
9717 /* We still need to start a new row, so output a copy insn. */
9718 dw2_asm_output_data (1, DW_LNS_copy,
9719 "copy line %u", current_line);
9721 else
9723 int line_offset = ent->val - current_line;
9724 int line_delta = line_offset - DWARF_LINE_BASE;
9726 current_line = ent->val;
9727 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9729 /* This can handle deltas from -10 to 234, using the current
9730 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9731 This takes 1 byte. */
9732 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9733 "line %u", current_line);
9735 else
9737 /* This can handle any delta. This takes at least 4 bytes,
9738 depending on the value being encoded. */
9739 dw2_asm_output_data (1, DW_LNS_advance_line,
9740 "advance to line %u", current_line);
9741 dw2_asm_output_data_sleb128 (line_offset, NULL);
9742 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9745 break;
9747 case LI_set_file:
9748 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9749 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9750 break;
9752 case LI_set_column:
9753 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9754 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9755 break;
9757 case LI_negate_stmt:
9758 current_is_stmt = !current_is_stmt;
9759 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9760 "is_stmt %d", current_is_stmt);
9761 break;
9763 case LI_set_prologue_end:
9764 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9765 "set prologue end");
9766 break;
9768 case LI_set_epilogue_begin:
9769 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9770 "set epilogue begin");
9771 break;
9773 case LI_set_discriminator:
9774 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9775 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9776 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9777 dw2_asm_output_data_uleb128 (ent->val, NULL);
9778 break;
9782 /* Emit debug info for the address of the end of the table. */
9783 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9784 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9785 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9786 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9788 dw2_asm_output_data (1, 0, "end sequence");
9789 dw2_asm_output_data_uleb128 (1, NULL);
9790 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9793 /* Output the source line number correspondence information. This
9794 information goes into the .debug_line section. */
9796 static void
9797 output_line_info (bool prologue_only)
9799 char l1[20], l2[20], p1[20], p2[20];
9800 int ver = dwarf_version;
9801 bool saw_one = false;
9802 int opc;
9804 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9805 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9806 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9807 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9809 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9810 dw2_asm_output_data (4, 0xffffffff,
9811 "Initial length escape value indicating 64-bit DWARF extension");
9812 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9813 "Length of Source Line Info");
9814 ASM_OUTPUT_LABEL (asm_out_file, l1);
9816 dw2_asm_output_data (2, ver, "DWARF Version");
9817 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9818 ASM_OUTPUT_LABEL (asm_out_file, p1);
9820 /* Define the architecture-dependent minimum instruction length (in bytes).
9821 In this implementation of DWARF, this field is used for information
9822 purposes only. Since GCC generates assembly language, we have no
9823 a priori knowledge of how many instruction bytes are generated for each
9824 source line, and therefore can use only the DW_LNE_set_address and
9825 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9826 this as '1', which is "correct enough" for all architectures,
9827 and don't let the target override. */
9828 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9830 if (ver >= 4)
9831 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9832 "Maximum Operations Per Instruction");
9833 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9834 "Default is_stmt_start flag");
9835 dw2_asm_output_data (1, DWARF_LINE_BASE,
9836 "Line Base Value (Special Opcodes)");
9837 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9838 "Line Range Value (Special Opcodes)");
9839 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9840 "Special Opcode Base");
9842 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9844 int n_op_args;
9845 switch (opc)
9847 case DW_LNS_advance_pc:
9848 case DW_LNS_advance_line:
9849 case DW_LNS_set_file:
9850 case DW_LNS_set_column:
9851 case DW_LNS_fixed_advance_pc:
9852 case DW_LNS_set_isa:
9853 n_op_args = 1;
9854 break;
9855 default:
9856 n_op_args = 0;
9857 break;
9860 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9861 opc, n_op_args);
9864 /* Write out the information about the files we use. */
9865 output_file_names ();
9866 ASM_OUTPUT_LABEL (asm_out_file, p2);
9867 if (prologue_only)
9869 /* Output the marker for the end of the line number info. */
9870 ASM_OUTPUT_LABEL (asm_out_file, l2);
9871 return;
9874 if (separate_line_info)
9876 dw_line_info_table *table;
9877 size_t i;
9879 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9880 if (table->in_use)
9882 output_one_line_info_table (table);
9883 saw_one = true;
9886 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9888 output_one_line_info_table (cold_text_section_line_info);
9889 saw_one = true;
9892 /* ??? Some Darwin linkers crash on a .debug_line section with no
9893 sequences. Further, merely a DW_LNE_end_sequence entry is not
9894 sufficient -- the address column must also be initialized.
9895 Make sure to output at least one set_address/end_sequence pair,
9896 choosing .text since that section is always present. */
9897 if (text_section_line_info->in_use || !saw_one)
9898 output_one_line_info_table (text_section_line_info);
9900 /* Output the marker for the end of the line number info. */
9901 ASM_OUTPUT_LABEL (asm_out_file, l2);
9904 /* Given a pointer to a tree node for some base type, return a pointer to
9905 a DIE that describes the given type.
9907 This routine must only be called for GCC type nodes that correspond to
9908 Dwarf base (fundamental) types. */
9910 static dw_die_ref
9911 base_type_die (tree type)
9913 dw_die_ref base_type_result;
9914 enum dwarf_type encoding;
9916 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9917 return 0;
9919 /* If this is a subtype that should not be emitted as a subrange type,
9920 use the base type. See subrange_type_for_debug_p. */
9921 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9922 type = TREE_TYPE (type);
9924 switch (TREE_CODE (type))
9926 case INTEGER_TYPE:
9927 if ((dwarf_version >= 4 || !dwarf_strict)
9928 && TYPE_NAME (type)
9929 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9930 && DECL_IS_BUILTIN (TYPE_NAME (type))
9931 && DECL_NAME (TYPE_NAME (type)))
9933 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9934 if (strcmp (name, "char16_t") == 0
9935 || strcmp (name, "char32_t") == 0)
9937 encoding = DW_ATE_UTF;
9938 break;
9941 if (TYPE_STRING_FLAG (type))
9943 if (TYPE_UNSIGNED (type))
9944 encoding = DW_ATE_unsigned_char;
9945 else
9946 encoding = DW_ATE_signed_char;
9948 else if (TYPE_UNSIGNED (type))
9949 encoding = DW_ATE_unsigned;
9950 else
9951 encoding = DW_ATE_signed;
9952 break;
9954 case REAL_TYPE:
9955 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9957 if (dwarf_version >= 3 || !dwarf_strict)
9958 encoding = DW_ATE_decimal_float;
9959 else
9960 encoding = DW_ATE_lo_user;
9962 else
9963 encoding = DW_ATE_float;
9964 break;
9966 case FIXED_POINT_TYPE:
9967 if (!(dwarf_version >= 3 || !dwarf_strict))
9968 encoding = DW_ATE_lo_user;
9969 else if (TYPE_UNSIGNED (type))
9970 encoding = DW_ATE_unsigned_fixed;
9971 else
9972 encoding = DW_ATE_signed_fixed;
9973 break;
9975 /* Dwarf2 doesn't know anything about complex ints, so use
9976 a user defined type for it. */
9977 case COMPLEX_TYPE:
9978 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9979 encoding = DW_ATE_complex_float;
9980 else
9981 encoding = DW_ATE_lo_user;
9982 break;
9984 case BOOLEAN_TYPE:
9985 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
9986 encoding = DW_ATE_boolean;
9987 break;
9989 default:
9990 /* No other TREE_CODEs are Dwarf fundamental types. */
9991 gcc_unreachable ();
9994 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9996 add_AT_unsigned (base_type_result, DW_AT_byte_size,
9997 int_size_in_bytes (type));
9998 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9999 add_pubtype (type, base_type_result);
10001 return base_type_result;
10004 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10005 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10007 static inline int
10008 is_base_type (tree type)
10010 switch (TREE_CODE (type))
10012 case ERROR_MARK:
10013 case VOID_TYPE:
10014 case INTEGER_TYPE:
10015 case REAL_TYPE:
10016 case FIXED_POINT_TYPE:
10017 case COMPLEX_TYPE:
10018 case BOOLEAN_TYPE:
10019 return 1;
10021 case ARRAY_TYPE:
10022 case RECORD_TYPE:
10023 case UNION_TYPE:
10024 case QUAL_UNION_TYPE:
10025 case ENUMERAL_TYPE:
10026 case FUNCTION_TYPE:
10027 case METHOD_TYPE:
10028 case POINTER_TYPE:
10029 case REFERENCE_TYPE:
10030 case NULLPTR_TYPE:
10031 case OFFSET_TYPE:
10032 case LANG_TYPE:
10033 case VECTOR_TYPE:
10034 return 0;
10036 default:
10037 gcc_unreachable ();
10040 return 0;
10043 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10044 node, return the size in bits for the type if it is a constant, or else
10045 return the alignment for the type if the type's size is not constant, or
10046 else return BITS_PER_WORD if the type actually turns out to be an
10047 ERROR_MARK node. */
10049 static inline unsigned HOST_WIDE_INT
10050 simple_type_size_in_bits (const_tree type)
10052 if (TREE_CODE (type) == ERROR_MARK)
10053 return BITS_PER_WORD;
10054 else if (TYPE_SIZE (type) == NULL_TREE)
10055 return 0;
10056 else if (host_integerp (TYPE_SIZE (type), 1))
10057 return tree_low_cst (TYPE_SIZE (type), 1);
10058 else
10059 return TYPE_ALIGN (type);
10062 /* Similarly, but return a double_int instead of UHWI. */
10064 static inline double_int
10065 double_int_type_size_in_bits (const_tree type)
10067 if (TREE_CODE (type) == ERROR_MARK)
10068 return double_int::from_uhwi (BITS_PER_WORD);
10069 else if (TYPE_SIZE (type) == NULL_TREE)
10070 return double_int_zero;
10071 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10072 return tree_to_double_int (TYPE_SIZE (type));
10073 else
10074 return double_int::from_uhwi (TYPE_ALIGN (type));
10077 /* Given a pointer to a tree node for a subrange type, return a pointer
10078 to a DIE that describes the given type. */
10080 static dw_die_ref
10081 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10083 dw_die_ref subrange_die;
10084 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10086 if (context_die == NULL)
10087 context_die = comp_unit_die ();
10089 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10091 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10093 /* The size of the subrange type and its base type do not match,
10094 so we need to generate a size attribute for the subrange type. */
10095 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10098 if (low)
10099 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10100 if (high)
10101 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10103 return subrange_die;
10106 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10107 entry that chains various modifiers in front of the given type. */
10109 static dw_die_ref
10110 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10111 dw_die_ref context_die)
10113 enum tree_code code = TREE_CODE (type);
10114 dw_die_ref mod_type_die;
10115 dw_die_ref sub_die = NULL;
10116 tree item_type = NULL;
10117 tree qualified_type;
10118 tree name, low, high;
10119 dw_die_ref mod_scope;
10121 if (code == ERROR_MARK)
10122 return NULL;
10124 /* See if we already have the appropriately qualified variant of
10125 this type. */
10126 qualified_type
10127 = get_qualified_type (type,
10128 ((is_const_type ? TYPE_QUAL_CONST : 0)
10129 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10131 if (qualified_type == sizetype
10132 && TYPE_NAME (qualified_type)
10133 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10135 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10137 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10138 && TYPE_PRECISION (t)
10139 == TYPE_PRECISION (qualified_type)
10140 && TYPE_UNSIGNED (t)
10141 == TYPE_UNSIGNED (qualified_type));
10142 qualified_type = t;
10145 /* If we do, then we can just use its DIE, if it exists. */
10146 if (qualified_type)
10148 mod_type_die = lookup_type_die (qualified_type);
10149 if (mod_type_die)
10150 return mod_type_die;
10153 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10155 /* Handle C typedef types. */
10156 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10157 && !DECL_ARTIFICIAL (name))
10159 tree dtype = TREE_TYPE (name);
10161 if (qualified_type == dtype)
10163 /* For a named type, use the typedef. */
10164 gen_type_die (qualified_type, context_die);
10165 return lookup_type_die (qualified_type);
10167 else if (is_const_type < TYPE_READONLY (dtype)
10168 || is_volatile_type < TYPE_VOLATILE (dtype)
10169 || (is_const_type <= TYPE_READONLY (dtype)
10170 && is_volatile_type <= TYPE_VOLATILE (dtype)
10171 && DECL_ORIGINAL_TYPE (name) != type))
10172 /* cv-unqualified version of named type. Just use the unnamed
10173 type to which it refers. */
10174 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10175 is_const_type, is_volatile_type,
10176 context_die);
10177 /* Else cv-qualified version of named type; fall through. */
10180 mod_scope = scope_die_for (type, context_die);
10182 if (is_const_type
10183 /* If both is_const_type and is_volatile_type, prefer the path
10184 which leads to a qualified type. */
10185 && (!is_volatile_type
10186 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10187 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10189 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10190 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10192 else if (is_volatile_type)
10194 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10195 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10197 else if (code == POINTER_TYPE)
10199 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10200 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10201 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10202 item_type = TREE_TYPE (type);
10203 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10204 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10205 TYPE_ADDR_SPACE (item_type));
10207 else if (code == REFERENCE_TYPE)
10209 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10210 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10211 type);
10212 else
10213 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10214 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10215 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10216 item_type = TREE_TYPE (type);
10217 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10218 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10219 TYPE_ADDR_SPACE (item_type));
10221 else if (code == INTEGER_TYPE
10222 && TREE_TYPE (type) != NULL_TREE
10223 && subrange_type_for_debug_p (type, &low, &high))
10225 mod_type_die = subrange_type_die (type, low, high, context_die);
10226 item_type = TREE_TYPE (type);
10228 else if (is_base_type (type))
10229 mod_type_die = base_type_die (type);
10230 else
10232 gen_type_die (type, context_die);
10234 /* We have to get the type_main_variant here (and pass that to the
10235 `lookup_type_die' routine) because the ..._TYPE node we have
10236 might simply be a *copy* of some original type node (where the
10237 copy was created to help us keep track of typedef names) and
10238 that copy might have a different TYPE_UID from the original
10239 ..._TYPE node. */
10240 if (TREE_CODE (type) != VECTOR_TYPE)
10241 return lookup_type_die (type_main_variant (type));
10242 else
10243 /* Vectors have the debugging information in the type,
10244 not the main variant. */
10245 return lookup_type_die (type);
10248 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10249 don't output a DW_TAG_typedef, since there isn't one in the
10250 user's program; just attach a DW_AT_name to the type.
10251 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10252 if the base type already has the same name. */
10253 if (name
10254 && ((TREE_CODE (name) != TYPE_DECL
10255 && (qualified_type == TYPE_MAIN_VARIANT (type)
10256 || (!is_const_type && !is_volatile_type)))
10257 || (TREE_CODE (name) == TYPE_DECL
10258 && TREE_TYPE (name) == qualified_type
10259 && DECL_NAME (name))))
10261 if (TREE_CODE (name) == TYPE_DECL)
10262 /* Could just call add_name_and_src_coords_attributes here,
10263 but since this is a builtin type it doesn't have any
10264 useful source coordinates anyway. */
10265 name = DECL_NAME (name);
10266 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10268 /* This probably indicates a bug. */
10269 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10271 name = TYPE_NAME (type);
10272 if (name
10273 && TREE_CODE (name) == TYPE_DECL)
10274 name = DECL_NAME (name);
10275 add_name_attribute (mod_type_die,
10276 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10279 if (qualified_type)
10280 equate_type_number_to_die (qualified_type, mod_type_die);
10282 if (item_type)
10283 /* We must do this after the equate_type_number_to_die call, in case
10284 this is a recursive type. This ensures that the modified_type_die
10285 recursion will terminate even if the type is recursive. Recursive
10286 types are possible in Ada. */
10287 sub_die = modified_type_die (item_type,
10288 TYPE_READONLY (item_type),
10289 TYPE_VOLATILE (item_type),
10290 context_die);
10292 if (sub_die != NULL)
10293 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10295 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10296 if (TYPE_ARTIFICIAL (type))
10297 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10299 return mod_type_die;
10302 /* Generate DIEs for the generic parameters of T.
10303 T must be either a generic type or a generic function.
10304 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10306 static void
10307 gen_generic_params_dies (tree t)
10309 tree parms, args;
10310 int parms_num, i;
10311 dw_die_ref die = NULL;
10312 int non_default;
10314 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10315 return;
10317 if (TYPE_P (t))
10318 die = lookup_type_die (t);
10319 else if (DECL_P (t))
10320 die = lookup_decl_die (t);
10322 gcc_assert (die);
10324 parms = lang_hooks.get_innermost_generic_parms (t);
10325 if (!parms)
10326 /* T has no generic parameter. It means T is neither a generic type
10327 or function. End of story. */
10328 return;
10330 parms_num = TREE_VEC_LENGTH (parms);
10331 args = lang_hooks.get_innermost_generic_args (t);
10332 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10333 non_default = int_cst_value (TREE_CHAIN (args));
10334 else
10335 non_default = TREE_VEC_LENGTH (args);
10336 for (i = 0; i < parms_num; i++)
10338 tree parm, arg, arg_pack_elems;
10339 dw_die_ref parm_die;
10341 parm = TREE_VEC_ELT (parms, i);
10342 arg = TREE_VEC_ELT (args, i);
10343 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10344 gcc_assert (parm && TREE_VALUE (parm) && arg);
10346 if (parm && TREE_VALUE (parm) && arg)
10348 /* If PARM represents a template parameter pack,
10349 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10350 by DW_TAG_template_*_parameter DIEs for the argument
10351 pack elements of ARG. Note that ARG would then be
10352 an argument pack. */
10353 if (arg_pack_elems)
10354 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10355 arg_pack_elems,
10356 die);
10357 else
10358 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10359 true /* emit name */, die);
10360 if (i >= non_default)
10361 add_AT_flag (parm_die, DW_AT_default_value, 1);
10366 /* Create and return a DIE for PARM which should be
10367 the representation of a generic type parameter.
10368 For instance, in the C++ front end, PARM would be a template parameter.
10369 ARG is the argument to PARM.
10370 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10371 name of the PARM.
10372 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10373 as a child node. */
10375 static dw_die_ref
10376 generic_parameter_die (tree parm, tree arg,
10377 bool emit_name_p,
10378 dw_die_ref parent_die)
10380 dw_die_ref tmpl_die = NULL;
10381 const char *name = NULL;
10383 if (!parm || !DECL_NAME (parm) || !arg)
10384 return NULL;
10386 /* We support non-type generic parameters and arguments,
10387 type generic parameters and arguments, as well as
10388 generic generic parameters (a.k.a. template template parameters in C++)
10389 and arguments. */
10390 if (TREE_CODE (parm) == PARM_DECL)
10391 /* PARM is a nontype generic parameter */
10392 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10393 else if (TREE_CODE (parm) == TYPE_DECL)
10394 /* PARM is a type generic parameter. */
10395 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10396 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10397 /* PARM is a generic generic parameter.
10398 Its DIE is a GNU extension. It shall have a
10399 DW_AT_name attribute to represent the name of the template template
10400 parameter, and a DW_AT_GNU_template_name attribute to represent the
10401 name of the template template argument. */
10402 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10403 parent_die, parm);
10404 else
10405 gcc_unreachable ();
10407 if (tmpl_die)
10409 tree tmpl_type;
10411 /* If PARM is a generic parameter pack, it means we are
10412 emitting debug info for a template argument pack element.
10413 In other terms, ARG is a template argument pack element.
10414 In that case, we don't emit any DW_AT_name attribute for
10415 the die. */
10416 if (emit_name_p)
10418 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10419 gcc_assert (name);
10420 add_AT_string (tmpl_die, DW_AT_name, name);
10423 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10425 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10426 TMPL_DIE should have a child DW_AT_type attribute that is set
10427 to the type of the argument to PARM, which is ARG.
10428 If PARM is a type generic parameter, TMPL_DIE should have a
10429 child DW_AT_type that is set to ARG. */
10430 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10431 add_type_attribute (tmpl_die, tmpl_type, 0,
10432 TREE_THIS_VOLATILE (tmpl_type),
10433 parent_die);
10435 else
10437 /* So TMPL_DIE is a DIE representing a
10438 a generic generic template parameter, a.k.a template template
10439 parameter in C++ and arg is a template. */
10441 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10442 to the name of the argument. */
10443 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10444 if (name)
10445 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10448 if (TREE_CODE (parm) == PARM_DECL)
10449 /* So PARM is a non-type generic parameter.
10450 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10451 attribute of TMPL_DIE which value represents the value
10452 of ARG.
10453 We must be careful here:
10454 The value of ARG might reference some function decls.
10455 We might currently be emitting debug info for a generic
10456 type and types are emitted before function decls, we don't
10457 know if the function decls referenced by ARG will actually be
10458 emitted after cgraph computations.
10459 So must defer the generation of the DW_AT_const_value to
10460 after cgraph is ready. */
10461 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10464 return tmpl_die;
10467 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10468 PARM_PACK must be a template parameter pack. The returned DIE
10469 will be child DIE of PARENT_DIE. */
10471 static dw_die_ref
10472 template_parameter_pack_die (tree parm_pack,
10473 tree parm_pack_args,
10474 dw_die_ref parent_die)
10476 dw_die_ref die;
10477 int j;
10479 gcc_assert (parent_die && parm_pack);
10481 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10482 add_name_and_src_coords_attributes (die, parm_pack);
10483 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10484 generic_parameter_die (parm_pack,
10485 TREE_VEC_ELT (parm_pack_args, j),
10486 false /* Don't emit DW_AT_name */,
10487 die);
10488 return die;
10491 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10492 an enumerated type. */
10494 static inline int
10495 type_is_enum (const_tree type)
10497 return TREE_CODE (type) == ENUMERAL_TYPE;
10500 /* Return the DBX register number described by a given RTL node. */
10502 static unsigned int
10503 dbx_reg_number (const_rtx rtl)
10505 unsigned regno = REGNO (rtl);
10507 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10509 #ifdef LEAF_REG_REMAP
10510 if (crtl->uses_only_leaf_regs)
10512 int leaf_reg = LEAF_REG_REMAP (regno);
10513 if (leaf_reg != -1)
10514 regno = (unsigned) leaf_reg;
10516 #endif
10518 regno = DBX_REGISTER_NUMBER (regno);
10519 gcc_assert (regno != INVALID_REGNUM);
10520 return regno;
10523 /* Optionally add a DW_OP_piece term to a location description expression.
10524 DW_OP_piece is only added if the location description expression already
10525 doesn't end with DW_OP_piece. */
10527 static void
10528 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10530 dw_loc_descr_ref loc;
10532 if (*list_head != NULL)
10534 /* Find the end of the chain. */
10535 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10538 if (loc->dw_loc_opc != DW_OP_piece)
10539 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10543 /* Return a location descriptor that designates a machine register or
10544 zero if there is none. */
10546 static dw_loc_descr_ref
10547 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10549 rtx regs;
10551 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10552 return 0;
10554 /* We only use "frame base" when we're sure we're talking about the
10555 post-prologue local stack frame. We do this by *not* running
10556 register elimination until this point, and recognizing the special
10557 argument pointer and soft frame pointer rtx's.
10558 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10559 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10560 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10562 dw_loc_descr_ref result = NULL;
10564 if (dwarf_version >= 4 || !dwarf_strict)
10566 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10567 initialized);
10568 if (result)
10569 add_loc_descr (&result,
10570 new_loc_descr (DW_OP_stack_value, 0, 0));
10572 return result;
10575 regs = targetm.dwarf_register_span (rtl);
10577 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10578 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10579 else
10581 unsigned int dbx_regnum = dbx_reg_number (rtl);
10582 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10583 return 0;
10584 return one_reg_loc_descriptor (dbx_regnum, initialized);
10588 /* Return a location descriptor that designates a machine register for
10589 a given hard register number. */
10591 static dw_loc_descr_ref
10592 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10594 dw_loc_descr_ref reg_loc_descr;
10596 if (regno <= 31)
10597 reg_loc_descr
10598 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10599 else
10600 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10602 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10603 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10605 return reg_loc_descr;
10608 /* Given an RTL of a register, return a location descriptor that
10609 designates a value that spans more than one register. */
10611 static dw_loc_descr_ref
10612 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10613 enum var_init_status initialized)
10615 int size, i;
10616 dw_loc_descr_ref loc_result = NULL;
10618 /* Simple, contiguous registers. */
10619 if (regs == NULL_RTX)
10621 unsigned reg = REGNO (rtl);
10622 int nregs;
10624 #ifdef LEAF_REG_REMAP
10625 if (crtl->uses_only_leaf_regs)
10627 int leaf_reg = LEAF_REG_REMAP (reg);
10628 if (leaf_reg != -1)
10629 reg = (unsigned) leaf_reg;
10631 #endif
10633 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10634 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10636 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10638 loc_result = NULL;
10639 while (nregs--)
10641 dw_loc_descr_ref t;
10643 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10644 VAR_INIT_STATUS_INITIALIZED);
10645 add_loc_descr (&loc_result, t);
10646 add_loc_descr_op_piece (&loc_result, size);
10647 ++reg;
10649 return loc_result;
10652 /* Now onto stupid register sets in non contiguous locations. */
10654 gcc_assert (GET_CODE (regs) == PARALLEL);
10656 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10657 loc_result = NULL;
10659 for (i = 0; i < XVECLEN (regs, 0); ++i)
10661 dw_loc_descr_ref t;
10663 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10664 VAR_INIT_STATUS_INITIALIZED);
10665 add_loc_descr (&loc_result, t);
10666 add_loc_descr_op_piece (&loc_result, size);
10669 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10670 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10671 return loc_result;
10674 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10676 /* Return a location descriptor that designates a constant i,
10677 as a compound operation from constant (i >> shift), constant shift
10678 and DW_OP_shl. */
10680 static dw_loc_descr_ref
10681 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10683 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10684 add_loc_descr (&ret, int_loc_descriptor (shift));
10685 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10686 return ret;
10689 /* Return a location descriptor that designates a constant. */
10691 static dw_loc_descr_ref
10692 int_loc_descriptor (HOST_WIDE_INT i)
10694 enum dwarf_location_atom op;
10696 /* Pick the smallest representation of a constant, rather than just
10697 defaulting to the LEB encoding. */
10698 if (i >= 0)
10700 int clz = clz_hwi (i);
10701 int ctz = ctz_hwi (i);
10702 if (i <= 31)
10703 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10704 else if (i <= 0xff)
10705 op = DW_OP_const1u;
10706 else if (i <= 0xffff)
10707 op = DW_OP_const2u;
10708 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10709 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10710 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10711 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10712 while DW_OP_const4u is 5 bytes. */
10713 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10714 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10715 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10716 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10717 while DW_OP_const4u is 5 bytes. */
10718 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10719 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10720 op = DW_OP_const4u;
10721 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10722 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10723 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10724 while DW_OP_constu of constant >= 0x100000000 takes at least
10725 6 bytes. */
10726 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10727 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10728 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10729 >= HOST_BITS_PER_WIDE_INT)
10730 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10731 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10732 while DW_OP_constu takes in this case at least 6 bytes. */
10733 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10734 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10735 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10736 && size_of_uleb128 (i) > 6)
10737 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10738 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10739 else
10740 op = DW_OP_constu;
10742 else
10744 if (i >= -0x80)
10745 op = DW_OP_const1s;
10746 else if (i >= -0x8000)
10747 op = DW_OP_const2s;
10748 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10750 if (size_of_int_loc_descriptor (i) < 5)
10752 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10753 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10754 return ret;
10756 op = DW_OP_const4s;
10758 else
10760 if (size_of_int_loc_descriptor (i)
10761 < (unsigned long) 1 + size_of_sleb128 (i))
10763 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10764 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10765 return ret;
10767 op = DW_OP_consts;
10771 return new_loc_descr (op, i, 0);
10774 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10775 without actually allocating it. */
10777 static unsigned long
10778 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10780 return size_of_int_loc_descriptor (i >> shift)
10781 + size_of_int_loc_descriptor (shift)
10782 + 1;
10785 /* Return size_of_locs (int_loc_descriptor (i)) without
10786 actually allocating it. */
10788 static unsigned long
10789 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10791 unsigned long s;
10793 if (i >= 0)
10795 int clz, ctz;
10796 if (i <= 31)
10797 return 1;
10798 else if (i <= 0xff)
10799 return 2;
10800 else if (i <= 0xffff)
10801 return 3;
10802 clz = clz_hwi (i);
10803 ctz = ctz_hwi (i);
10804 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10805 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10806 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10807 - clz - 5);
10808 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10809 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10810 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10811 - clz - 8);
10812 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10813 return 5;
10814 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10815 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10816 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10817 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10818 - clz - 8);
10819 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10820 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10821 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10822 - clz - 16);
10823 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10824 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10825 && s > 6)
10826 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10827 - clz - 32);
10828 else
10829 return 1 + s;
10831 else
10833 if (i >= -0x80)
10834 return 2;
10835 else if (i >= -0x8000)
10836 return 3;
10837 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10839 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10841 s = size_of_int_loc_descriptor (-i) + 1;
10842 if (s < 5)
10843 return s;
10845 return 5;
10847 else
10849 unsigned long r = 1 + size_of_sleb128 (i);
10850 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10852 s = size_of_int_loc_descriptor (-i) + 1;
10853 if (s < r)
10854 return s;
10856 return r;
10861 /* Return loc description representing "address" of integer value.
10862 This can appear only as toplevel expression. */
10864 static dw_loc_descr_ref
10865 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10867 int litsize;
10868 dw_loc_descr_ref loc_result = NULL;
10870 if (!(dwarf_version >= 4 || !dwarf_strict))
10871 return NULL;
10873 litsize = size_of_int_loc_descriptor (i);
10874 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10875 is more compact. For DW_OP_stack_value we need:
10876 litsize + 1 (DW_OP_stack_value)
10877 and for DW_OP_implicit_value:
10878 1 (DW_OP_implicit_value) + 1 (length) + size. */
10879 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10881 loc_result = int_loc_descriptor (i);
10882 add_loc_descr (&loc_result,
10883 new_loc_descr (DW_OP_stack_value, 0, 0));
10884 return loc_result;
10887 loc_result = new_loc_descr (DW_OP_implicit_value,
10888 size, 0);
10889 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10890 loc_result->dw_loc_oprnd2.v.val_int = i;
10891 return loc_result;
10894 /* Return a location descriptor that designates a base+offset location. */
10896 static dw_loc_descr_ref
10897 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10898 enum var_init_status initialized)
10900 unsigned int regno;
10901 dw_loc_descr_ref result;
10902 dw_fde_ref fde = cfun->fde;
10904 /* We only use "frame base" when we're sure we're talking about the
10905 post-prologue local stack frame. We do this by *not* running
10906 register elimination until this point, and recognizing the special
10907 argument pointer and soft frame pointer rtx's. */
10908 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10910 rtx elim = (ira_use_lra_p
10911 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10912 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10914 if (elim != reg)
10916 if (GET_CODE (elim) == PLUS)
10918 offset += INTVAL (XEXP (elim, 1));
10919 elim = XEXP (elim, 0);
10921 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10922 && (elim == hard_frame_pointer_rtx
10923 || elim == stack_pointer_rtx))
10924 || elim == (frame_pointer_needed
10925 ? hard_frame_pointer_rtx
10926 : stack_pointer_rtx));
10928 /* If drap register is used to align stack, use frame
10929 pointer + offset to access stack variables. If stack
10930 is aligned without drap, use stack pointer + offset to
10931 access stack variables. */
10932 if (crtl->stack_realign_tried
10933 && reg == frame_pointer_rtx)
10935 int base_reg
10936 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10937 ? HARD_FRAME_POINTER_REGNUM
10938 : REGNO (elim));
10939 return new_reg_loc_descr (base_reg, offset);
10942 gcc_assert (frame_pointer_fb_offset_valid);
10943 offset += frame_pointer_fb_offset;
10944 return new_loc_descr (DW_OP_fbreg, offset, 0);
10948 regno = REGNO (reg);
10949 #ifdef LEAF_REG_REMAP
10950 if (crtl->uses_only_leaf_regs)
10952 int leaf_reg = LEAF_REG_REMAP (regno);
10953 if (leaf_reg != -1)
10954 regno = (unsigned) leaf_reg;
10956 #endif
10957 regno = DWARF_FRAME_REGNUM (regno);
10959 if (!optimize && fde
10960 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10962 /* Use cfa+offset to represent the location of arguments passed
10963 on the stack when drap is used to align stack.
10964 Only do this when not optimizing, for optimized code var-tracking
10965 is supposed to track where the arguments live and the register
10966 used as vdrap or drap in some spot might be used for something
10967 else in other part of the routine. */
10968 return new_loc_descr (DW_OP_fbreg, offset, 0);
10971 if (regno <= 31)
10972 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10973 offset, 0);
10974 else
10975 result = new_loc_descr (DW_OP_bregx, regno, offset);
10977 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10978 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10980 return result;
10983 /* Return true if this RTL expression describes a base+offset calculation. */
10985 static inline int
10986 is_based_loc (const_rtx rtl)
10988 return (GET_CODE (rtl) == PLUS
10989 && ((REG_P (XEXP (rtl, 0))
10990 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10991 && CONST_INT_P (XEXP (rtl, 1)))));
10994 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10995 failed. */
10997 static dw_loc_descr_ref
10998 tls_mem_loc_descriptor (rtx mem)
11000 tree base;
11001 dw_loc_descr_ref loc_result;
11003 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11004 return NULL;
11006 base = get_base_address (MEM_EXPR (mem));
11007 if (base == NULL
11008 || TREE_CODE (base) != VAR_DECL
11009 || !DECL_THREAD_LOCAL_P (base))
11010 return NULL;
11012 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11013 if (loc_result == NULL)
11014 return NULL;
11016 if (MEM_OFFSET (mem))
11017 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11019 return loc_result;
11022 /* Output debug info about reason why we failed to expand expression as dwarf
11023 expression. */
11025 static void
11026 expansion_failed (tree expr, rtx rtl, char const *reason)
11028 if (dump_file && (dump_flags & TDF_DETAILS))
11030 fprintf (dump_file, "Failed to expand as dwarf: ");
11031 if (expr)
11032 print_generic_expr (dump_file, expr, dump_flags);
11033 if (rtl)
11035 fprintf (dump_file, "\n");
11036 print_rtl (dump_file, rtl);
11038 fprintf (dump_file, "\nReason: %s\n", reason);
11042 /* Helper function for const_ok_for_output, called either directly
11043 or via for_each_rtx. */
11045 static int
11046 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11048 rtx rtl = *rtlp;
11050 if (GET_CODE (rtl) == UNSPEC)
11052 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11053 we can't express it in the debug info. */
11054 #ifdef ENABLE_CHECKING
11055 /* Don't complain about TLS UNSPECs, those are just too hard to
11056 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11057 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11058 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11059 if (XVECLEN (rtl, 0) == 0
11060 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11061 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11062 inform (current_function_decl
11063 ? DECL_SOURCE_LOCATION (current_function_decl)
11064 : UNKNOWN_LOCATION,
11065 #if NUM_UNSPEC_VALUES > 0
11066 "non-delegitimized UNSPEC %s (%d) found in variable location",
11067 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11068 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11069 XINT (rtl, 1));
11070 #else
11071 "non-delegitimized UNSPEC %d found in variable location",
11072 XINT (rtl, 1));
11073 #endif
11074 #endif
11075 expansion_failed (NULL_TREE, rtl,
11076 "UNSPEC hasn't been delegitimized.\n");
11077 return 1;
11080 if (targetm.const_not_ok_for_debug_p (rtl))
11082 expansion_failed (NULL_TREE, rtl,
11083 "Expression rejected for debug by the backend.\n");
11084 return 1;
11087 if (GET_CODE (rtl) != SYMBOL_REF)
11088 return 0;
11090 if (CONSTANT_POOL_ADDRESS_P (rtl))
11092 bool marked;
11093 get_pool_constant_mark (rtl, &marked);
11094 /* If all references to this pool constant were optimized away,
11095 it was not output and thus we can't represent it. */
11096 if (!marked)
11098 expansion_failed (NULL_TREE, rtl,
11099 "Constant was removed from constant pool.\n");
11100 return 1;
11104 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11105 return 1;
11107 /* Avoid references to external symbols in debug info, on several targets
11108 the linker might even refuse to link when linking a shared library,
11109 and in many other cases the relocations for .debug_info/.debug_loc are
11110 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11111 to be defined within the same shared library or executable are fine. */
11112 if (SYMBOL_REF_EXTERNAL_P (rtl))
11114 tree decl = SYMBOL_REF_DECL (rtl);
11116 if (decl == NULL || !targetm.binds_local_p (decl))
11118 expansion_failed (NULL_TREE, rtl,
11119 "Symbol not defined in current TU.\n");
11120 return 1;
11124 return 0;
11127 /* Return true if constant RTL can be emitted in DW_OP_addr or
11128 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11129 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11131 static bool
11132 const_ok_for_output (rtx rtl)
11134 if (GET_CODE (rtl) == SYMBOL_REF)
11135 return const_ok_for_output_1 (&rtl, NULL) == 0;
11137 if (GET_CODE (rtl) == CONST)
11138 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11140 return true;
11143 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11144 if possible, NULL otherwise. */
11146 static dw_die_ref
11147 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11149 dw_die_ref type_die;
11150 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11152 if (type == NULL)
11153 return NULL;
11154 switch (TREE_CODE (type))
11156 case INTEGER_TYPE:
11157 case REAL_TYPE:
11158 break;
11159 default:
11160 return NULL;
11162 type_die = lookup_type_die (type);
11163 if (!type_die)
11164 type_die = modified_type_die (type, false, false, comp_unit_die ());
11165 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11166 return NULL;
11167 return type_die;
11170 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11171 type matching MODE, or, if MODE is narrower than or as wide as
11172 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11173 possible. */
11175 static dw_loc_descr_ref
11176 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11178 enum machine_mode outer_mode = mode;
11179 dw_die_ref type_die;
11180 dw_loc_descr_ref cvt;
11182 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11184 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11185 return op;
11187 type_die = base_type_for_mode (outer_mode, 1);
11188 if (type_die == NULL)
11189 return NULL;
11190 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11191 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11192 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11193 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11194 add_loc_descr (&op, cvt);
11195 return op;
11198 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11200 static dw_loc_descr_ref
11201 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11202 dw_loc_descr_ref op1)
11204 dw_loc_descr_ref ret = op0;
11205 add_loc_descr (&ret, op1);
11206 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11207 if (STORE_FLAG_VALUE != 1)
11209 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11210 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11212 return ret;
11215 /* Return location descriptor for signed comparison OP RTL. */
11217 static dw_loc_descr_ref
11218 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11219 enum machine_mode mem_mode)
11221 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11222 dw_loc_descr_ref op0, op1;
11223 int shift;
11225 if (op_mode == VOIDmode)
11226 op_mode = GET_MODE (XEXP (rtl, 1));
11227 if (op_mode == VOIDmode)
11228 return NULL;
11230 if (dwarf_strict
11231 && (GET_MODE_CLASS (op_mode) != MODE_INT
11232 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11233 return NULL;
11235 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11236 VAR_INIT_STATUS_INITIALIZED);
11237 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11238 VAR_INIT_STATUS_INITIALIZED);
11240 if (op0 == NULL || op1 == NULL)
11241 return NULL;
11243 if (GET_MODE_CLASS (op_mode) != MODE_INT
11244 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11245 return compare_loc_descriptor (op, op0, op1);
11247 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11249 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11250 dw_loc_descr_ref cvt;
11252 if (type_die == NULL)
11253 return NULL;
11254 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11255 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11256 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11257 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11258 add_loc_descr (&op0, cvt);
11259 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11260 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11261 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11262 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11263 add_loc_descr (&op1, cvt);
11264 return compare_loc_descriptor (op, op0, op1);
11267 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11268 /* For eq/ne, if the operands are known to be zero-extended,
11269 there is no need to do the fancy shifting up. */
11270 if (op == DW_OP_eq || op == DW_OP_ne)
11272 dw_loc_descr_ref last0, last1;
11273 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11275 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11277 /* deref_size zero extends, and for constants we can check
11278 whether they are zero extended or not. */
11279 if (((last0->dw_loc_opc == DW_OP_deref_size
11280 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11281 || (CONST_INT_P (XEXP (rtl, 0))
11282 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11283 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11284 && ((last1->dw_loc_opc == DW_OP_deref_size
11285 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11286 || (CONST_INT_P (XEXP (rtl, 1))
11287 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11288 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11289 return compare_loc_descriptor (op, op0, op1);
11291 /* EQ/NE comparison against constant in narrower type than
11292 DWARF2_ADDR_SIZE can be performed either as
11293 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11294 DW_OP_{eq,ne}
11296 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11297 DW_OP_{eq,ne}. Pick whatever is shorter. */
11298 if (CONST_INT_P (XEXP (rtl, 1))
11299 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11300 && (size_of_int_loc_descriptor (shift) + 1
11301 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11302 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11303 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11304 & GET_MODE_MASK (op_mode))))
11306 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11307 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11308 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11309 & GET_MODE_MASK (op_mode));
11310 return compare_loc_descriptor (op, op0, op1);
11313 add_loc_descr (&op0, int_loc_descriptor (shift));
11314 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11315 if (CONST_INT_P (XEXP (rtl, 1)))
11316 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11317 else
11319 add_loc_descr (&op1, int_loc_descriptor (shift));
11320 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11322 return compare_loc_descriptor (op, op0, op1);
11325 /* Return location descriptor for unsigned comparison OP RTL. */
11327 static dw_loc_descr_ref
11328 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11329 enum machine_mode mem_mode)
11331 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11332 dw_loc_descr_ref op0, op1;
11334 if (op_mode == VOIDmode)
11335 op_mode = GET_MODE (XEXP (rtl, 1));
11336 if (op_mode == VOIDmode)
11337 return NULL;
11338 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11339 return NULL;
11341 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11342 return NULL;
11344 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11345 VAR_INIT_STATUS_INITIALIZED);
11346 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11347 VAR_INIT_STATUS_INITIALIZED);
11349 if (op0 == NULL || op1 == NULL)
11350 return NULL;
11352 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11354 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11355 dw_loc_descr_ref last0, last1;
11356 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11358 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11360 if (CONST_INT_P (XEXP (rtl, 0)))
11361 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11362 /* deref_size zero extends, so no need to mask it again. */
11363 else if (last0->dw_loc_opc != DW_OP_deref_size
11364 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11366 add_loc_descr (&op0, int_loc_descriptor (mask));
11367 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11369 if (CONST_INT_P (XEXP (rtl, 1)))
11370 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11371 /* deref_size zero extends, so no need to mask it again. */
11372 else if (last1->dw_loc_opc != DW_OP_deref_size
11373 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11375 add_loc_descr (&op1, int_loc_descriptor (mask));
11376 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11379 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11381 HOST_WIDE_INT bias = 1;
11382 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11383 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11384 if (CONST_INT_P (XEXP (rtl, 1)))
11385 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11386 + INTVAL (XEXP (rtl, 1)));
11387 else
11388 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11389 bias, 0));
11391 return compare_loc_descriptor (op, op0, op1);
11394 /* Return location descriptor for {U,S}{MIN,MAX}. */
11396 static dw_loc_descr_ref
11397 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11398 enum machine_mode mem_mode)
11400 enum dwarf_location_atom op;
11401 dw_loc_descr_ref op0, op1, ret;
11402 dw_loc_descr_ref bra_node, drop_node;
11404 if (dwarf_strict
11405 && (GET_MODE_CLASS (mode) != MODE_INT
11406 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11407 return NULL;
11409 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11410 VAR_INIT_STATUS_INITIALIZED);
11411 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11412 VAR_INIT_STATUS_INITIALIZED);
11414 if (op0 == NULL || op1 == NULL)
11415 return NULL;
11417 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11418 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11419 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11420 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11422 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11424 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11425 add_loc_descr (&op0, int_loc_descriptor (mask));
11426 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11427 add_loc_descr (&op1, int_loc_descriptor (mask));
11428 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11430 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11432 HOST_WIDE_INT bias = 1;
11433 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11434 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11435 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11438 else if (GET_MODE_CLASS (mode) == MODE_INT
11439 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11441 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11442 add_loc_descr (&op0, int_loc_descriptor (shift));
11443 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11444 add_loc_descr (&op1, int_loc_descriptor (shift));
11445 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11447 else if (GET_MODE_CLASS (mode) == MODE_INT
11448 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11450 dw_die_ref type_die = base_type_for_mode (mode, 0);
11451 dw_loc_descr_ref cvt;
11452 if (type_die == NULL)
11453 return NULL;
11454 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11455 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11456 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11457 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11458 add_loc_descr (&op0, cvt);
11459 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11460 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11461 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11462 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11463 add_loc_descr (&op1, cvt);
11466 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11467 op = DW_OP_lt;
11468 else
11469 op = DW_OP_gt;
11470 ret = op0;
11471 add_loc_descr (&ret, op1);
11472 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11473 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11474 add_loc_descr (&ret, bra_node);
11475 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11476 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11477 add_loc_descr (&ret, drop_node);
11478 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11479 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11480 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11481 && GET_MODE_CLASS (mode) == MODE_INT
11482 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11483 ret = convert_descriptor_to_mode (mode, ret);
11484 return ret;
11487 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11488 but after converting arguments to type_die, afterwards
11489 convert back to unsigned. */
11491 static dw_loc_descr_ref
11492 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11493 enum machine_mode mode, enum machine_mode mem_mode)
11495 dw_loc_descr_ref cvt, op0, op1;
11497 if (type_die == NULL)
11498 return NULL;
11499 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11500 VAR_INIT_STATUS_INITIALIZED);
11501 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11502 VAR_INIT_STATUS_INITIALIZED);
11503 if (op0 == NULL || op1 == NULL)
11504 return NULL;
11505 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11506 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11507 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11508 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11509 add_loc_descr (&op0, cvt);
11510 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11511 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11512 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11513 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11514 add_loc_descr (&op1, cvt);
11515 add_loc_descr (&op0, op1);
11516 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11517 return convert_descriptor_to_mode (mode, op0);
11520 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11521 const0 is DW_OP_lit0 or corresponding typed constant,
11522 const1 is DW_OP_lit1 or corresponding typed constant
11523 and constMSB is constant with just the MSB bit set
11524 for the mode):
11525 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11526 L1: const0 DW_OP_swap
11527 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11528 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11529 L3: DW_OP_drop
11530 L4: DW_OP_nop
11532 CTZ is similar:
11533 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11534 L1: const0 DW_OP_swap
11535 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11536 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11537 L3: DW_OP_drop
11538 L4: DW_OP_nop
11540 FFS is similar:
11541 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11542 L1: const1 DW_OP_swap
11543 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11544 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11545 L3: DW_OP_drop
11546 L4: DW_OP_nop */
11548 static dw_loc_descr_ref
11549 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11550 enum machine_mode mem_mode)
11552 dw_loc_descr_ref op0, ret, tmp;
11553 HOST_WIDE_INT valv;
11554 dw_loc_descr_ref l1jump, l1label;
11555 dw_loc_descr_ref l2jump, l2label;
11556 dw_loc_descr_ref l3jump, l3label;
11557 dw_loc_descr_ref l4jump, l4label;
11558 rtx msb;
11560 if (GET_MODE_CLASS (mode) != MODE_INT
11561 || GET_MODE (XEXP (rtl, 0)) != mode
11562 || (GET_CODE (rtl) == CLZ
11563 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11564 return NULL;
11566 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11567 VAR_INIT_STATUS_INITIALIZED);
11568 if (op0 == NULL)
11569 return NULL;
11570 ret = op0;
11571 if (GET_CODE (rtl) == CLZ)
11573 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11574 valv = GET_MODE_BITSIZE (mode);
11576 else if (GET_CODE (rtl) == FFS)
11577 valv = 0;
11578 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11579 valv = GET_MODE_BITSIZE (mode);
11580 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11581 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11582 add_loc_descr (&ret, l1jump);
11583 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11584 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11585 VAR_INIT_STATUS_INITIALIZED);
11586 if (tmp == NULL)
11587 return NULL;
11588 add_loc_descr (&ret, tmp);
11589 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11590 add_loc_descr (&ret, l4jump);
11591 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11592 ? const1_rtx : const0_rtx,
11593 mode, mem_mode,
11594 VAR_INIT_STATUS_INITIALIZED);
11595 if (l1label == NULL)
11596 return NULL;
11597 add_loc_descr (&ret, l1label);
11598 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11599 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11600 add_loc_descr (&ret, l2label);
11601 if (GET_CODE (rtl) != CLZ)
11602 msb = const1_rtx;
11603 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11604 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11605 << (GET_MODE_BITSIZE (mode) - 1));
11606 else
11607 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11608 << (GET_MODE_BITSIZE (mode)
11609 - HOST_BITS_PER_WIDE_INT - 1), mode);
11610 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11611 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11612 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11613 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11614 else
11615 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11616 VAR_INIT_STATUS_INITIALIZED);
11617 if (tmp == NULL)
11618 return NULL;
11619 add_loc_descr (&ret, tmp);
11620 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11621 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11622 add_loc_descr (&ret, l3jump);
11623 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11624 VAR_INIT_STATUS_INITIALIZED);
11625 if (tmp == NULL)
11626 return NULL;
11627 add_loc_descr (&ret, tmp);
11628 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11629 ? DW_OP_shl : DW_OP_shr, 0, 0));
11630 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11631 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11632 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11633 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11634 add_loc_descr (&ret, l2jump);
11635 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11636 add_loc_descr (&ret, l3label);
11637 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11638 add_loc_descr (&ret, l4label);
11639 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11640 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11641 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11642 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11643 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11644 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11645 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11646 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11647 return ret;
11650 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11651 const1 is DW_OP_lit1 or corresponding typed constant):
11652 const0 DW_OP_swap
11653 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11654 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11655 L2: DW_OP_drop
11657 PARITY is similar:
11658 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11659 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11660 L2: DW_OP_drop */
11662 static dw_loc_descr_ref
11663 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11664 enum machine_mode mem_mode)
11666 dw_loc_descr_ref op0, ret, tmp;
11667 dw_loc_descr_ref l1jump, l1label;
11668 dw_loc_descr_ref l2jump, l2label;
11670 if (GET_MODE_CLASS (mode) != MODE_INT
11671 || GET_MODE (XEXP (rtl, 0)) != mode)
11672 return NULL;
11674 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11675 VAR_INIT_STATUS_INITIALIZED);
11676 if (op0 == NULL)
11677 return NULL;
11678 ret = op0;
11679 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11680 VAR_INIT_STATUS_INITIALIZED);
11681 if (tmp == NULL)
11682 return NULL;
11683 add_loc_descr (&ret, tmp);
11684 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11685 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11686 add_loc_descr (&ret, l1label);
11687 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11688 add_loc_descr (&ret, l2jump);
11689 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11690 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11691 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11692 VAR_INIT_STATUS_INITIALIZED);
11693 if (tmp == NULL)
11694 return NULL;
11695 add_loc_descr (&ret, tmp);
11696 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11697 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11698 ? DW_OP_plus : DW_OP_xor, 0, 0));
11699 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11700 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11701 VAR_INIT_STATUS_INITIALIZED);
11702 add_loc_descr (&ret, tmp);
11703 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11704 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11705 add_loc_descr (&ret, l1jump);
11706 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11707 add_loc_descr (&ret, l2label);
11708 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11709 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11710 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11711 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11712 return ret;
11715 /* BSWAP (constS is initial shift count, either 56 or 24):
11716 constS const0
11717 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11718 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11719 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11720 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11721 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11723 static dw_loc_descr_ref
11724 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11725 enum machine_mode mem_mode)
11727 dw_loc_descr_ref op0, ret, tmp;
11728 dw_loc_descr_ref l1jump, l1label;
11729 dw_loc_descr_ref l2jump, l2label;
11731 if (GET_MODE_CLASS (mode) != MODE_INT
11732 || BITS_PER_UNIT != 8
11733 || (GET_MODE_BITSIZE (mode) != 32
11734 && GET_MODE_BITSIZE (mode) != 64))
11735 return NULL;
11737 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11738 VAR_INIT_STATUS_INITIALIZED);
11739 if (op0 == NULL)
11740 return NULL;
11742 ret = op0;
11743 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11744 mode, mem_mode,
11745 VAR_INIT_STATUS_INITIALIZED);
11746 if (tmp == NULL)
11747 return NULL;
11748 add_loc_descr (&ret, tmp);
11749 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11750 VAR_INIT_STATUS_INITIALIZED);
11751 if (tmp == NULL)
11752 return NULL;
11753 add_loc_descr (&ret, tmp);
11754 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11755 add_loc_descr (&ret, l1label);
11756 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11757 mode, mem_mode,
11758 VAR_INIT_STATUS_INITIALIZED);
11759 add_loc_descr (&ret, tmp);
11760 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11761 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11762 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11763 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11764 VAR_INIT_STATUS_INITIALIZED);
11765 if (tmp == NULL)
11766 return NULL;
11767 add_loc_descr (&ret, tmp);
11768 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11769 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11770 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11771 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11772 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11773 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11774 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11775 VAR_INIT_STATUS_INITIALIZED);
11776 add_loc_descr (&ret, tmp);
11777 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11778 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11779 add_loc_descr (&ret, l2jump);
11780 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11781 VAR_INIT_STATUS_INITIALIZED);
11782 add_loc_descr (&ret, tmp);
11783 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11784 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11785 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11786 add_loc_descr (&ret, l1jump);
11787 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11788 add_loc_descr (&ret, l2label);
11789 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11790 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11791 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11792 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11793 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11794 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11795 return ret;
11798 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11799 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11800 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11801 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11803 ROTATERT is similar:
11804 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11805 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11806 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11808 static dw_loc_descr_ref
11809 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11810 enum machine_mode mem_mode)
11812 rtx rtlop1 = XEXP (rtl, 1);
11813 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11814 int i;
11816 if (GET_MODE_CLASS (mode) != MODE_INT)
11817 return NULL;
11819 if (GET_MODE (rtlop1) != VOIDmode
11820 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11821 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11822 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11823 VAR_INIT_STATUS_INITIALIZED);
11824 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11825 VAR_INIT_STATUS_INITIALIZED);
11826 if (op0 == NULL || op1 == NULL)
11827 return NULL;
11828 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11829 for (i = 0; i < 2; i++)
11831 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11832 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11833 mode, mem_mode,
11834 VAR_INIT_STATUS_INITIALIZED);
11835 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11836 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11837 ? DW_OP_const4u
11838 : HOST_BITS_PER_WIDE_INT == 64
11839 ? DW_OP_const8u : DW_OP_constu,
11840 GET_MODE_MASK (mode), 0);
11841 else
11842 mask[i] = NULL;
11843 if (mask[i] == NULL)
11844 return NULL;
11845 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11847 ret = op0;
11848 add_loc_descr (&ret, op1);
11849 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11850 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11851 if (GET_CODE (rtl) == ROTATERT)
11853 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11854 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11855 GET_MODE_BITSIZE (mode), 0));
11857 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11858 if (mask[0] != NULL)
11859 add_loc_descr (&ret, mask[0]);
11860 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11861 if (mask[1] != NULL)
11863 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11864 add_loc_descr (&ret, mask[1]);
11865 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11867 if (GET_CODE (rtl) == ROTATE)
11869 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11870 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11871 GET_MODE_BITSIZE (mode), 0));
11873 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11874 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11875 return ret;
11878 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11879 for DEBUG_PARAMETER_REF RTL. */
11881 static dw_loc_descr_ref
11882 parameter_ref_descriptor (rtx rtl)
11884 dw_loc_descr_ref ret;
11885 dw_die_ref ref;
11887 if (dwarf_strict)
11888 return NULL;
11889 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11890 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11891 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11892 if (ref)
11894 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11895 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11896 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11898 else
11900 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11901 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11903 return ret;
11906 /* The following routine converts the RTL for a variable or parameter
11907 (resident in memory) into an equivalent Dwarf representation of a
11908 mechanism for getting the address of that same variable onto the top of a
11909 hypothetical "address evaluation" stack.
11911 When creating memory location descriptors, we are effectively transforming
11912 the RTL for a memory-resident object into its Dwarf postfix expression
11913 equivalent. This routine recursively descends an RTL tree, turning
11914 it into Dwarf postfix code as it goes.
11916 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11918 MEM_MODE is the mode of the memory reference, needed to handle some
11919 autoincrement addressing modes.
11921 Return 0 if we can't represent the location. */
11923 dw_loc_descr_ref
11924 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11925 enum machine_mode mem_mode,
11926 enum var_init_status initialized)
11928 dw_loc_descr_ref mem_loc_result = NULL;
11929 enum dwarf_location_atom op;
11930 dw_loc_descr_ref op0, op1;
11931 rtx inner = NULL_RTX;
11933 if (mode == VOIDmode)
11934 mode = GET_MODE (rtl);
11936 /* Note that for a dynamically sized array, the location we will generate a
11937 description of here will be the lowest numbered location which is
11938 actually within the array. That's *not* necessarily the same as the
11939 zeroth element of the array. */
11941 rtl = targetm.delegitimize_address (rtl);
11943 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11944 return NULL;
11946 switch (GET_CODE (rtl))
11948 case POST_INC:
11949 case POST_DEC:
11950 case POST_MODIFY:
11951 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11953 case SUBREG:
11954 /* The case of a subreg may arise when we have a local (register)
11955 variable or a formal (register) parameter which doesn't quite fill
11956 up an entire register. For now, just assume that it is
11957 legitimate to make the Dwarf info refer to the whole register which
11958 contains the given subreg. */
11959 if (!subreg_lowpart_p (rtl))
11960 break;
11961 inner = SUBREG_REG (rtl);
11962 case TRUNCATE:
11963 if (inner == NULL_RTX)
11964 inner = XEXP (rtl, 0);
11965 if (GET_MODE_CLASS (mode) == MODE_INT
11966 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11967 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11968 #ifdef POINTERS_EXTEND_UNSIGNED
11969 || (mode == Pmode && mem_mode != VOIDmode)
11970 #endif
11972 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
11974 mem_loc_result = mem_loc_descriptor (inner,
11975 GET_MODE (inner),
11976 mem_mode, initialized);
11977 break;
11979 if (dwarf_strict)
11980 break;
11981 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
11982 break;
11983 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
11984 && (GET_MODE_CLASS (mode) != MODE_INT
11985 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
11986 break;
11987 else
11989 dw_die_ref type_die;
11990 dw_loc_descr_ref cvt;
11992 mem_loc_result = mem_loc_descriptor (inner,
11993 GET_MODE (inner),
11994 mem_mode, initialized);
11995 if (mem_loc_result == NULL)
11996 break;
11997 type_die = base_type_for_mode (mode,
11998 GET_MODE_CLASS (mode) == MODE_INT);
11999 if (type_die == NULL)
12001 mem_loc_result = NULL;
12002 break;
12004 if (GET_MODE_SIZE (mode)
12005 != GET_MODE_SIZE (GET_MODE (inner)))
12006 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12007 else
12008 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12009 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12010 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12011 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12012 add_loc_descr (&mem_loc_result, cvt);
12014 break;
12016 case REG:
12017 if (GET_MODE_CLASS (mode) != MODE_INT
12018 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12019 && rtl != arg_pointer_rtx
12020 && rtl != frame_pointer_rtx
12021 #ifdef POINTERS_EXTEND_UNSIGNED
12022 && (mode != Pmode || mem_mode == VOIDmode)
12023 #endif
12026 dw_die_ref type_die;
12027 unsigned int dbx_regnum;
12029 if (dwarf_strict)
12030 break;
12031 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12032 break;
12033 type_die = base_type_for_mode (mode,
12034 GET_MODE_CLASS (mode) == MODE_INT);
12035 if (type_die == NULL)
12036 break;
12038 dbx_regnum = dbx_reg_number (rtl);
12039 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12040 break;
12041 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12042 dbx_regnum, 0);
12043 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12044 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12045 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12046 break;
12048 /* Whenever a register number forms a part of the description of the
12049 method for calculating the (dynamic) address of a memory resident
12050 object, DWARF rules require the register number be referred to as
12051 a "base register". This distinction is not based in any way upon
12052 what category of register the hardware believes the given register
12053 belongs to. This is strictly DWARF terminology we're dealing with
12054 here. Note that in cases where the location of a memory-resident
12055 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12056 OP_CONST (0)) the actual DWARF location descriptor that we generate
12057 may just be OP_BASEREG (basereg). This may look deceptively like
12058 the object in question was allocated to a register (rather than in
12059 memory) so DWARF consumers need to be aware of the subtle
12060 distinction between OP_REG and OP_BASEREG. */
12061 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12062 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12063 else if (stack_realign_drap
12064 && crtl->drap_reg
12065 && crtl->args.internal_arg_pointer == rtl
12066 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12068 /* If RTL is internal_arg_pointer, which has been optimized
12069 out, use DRAP instead. */
12070 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12071 VAR_INIT_STATUS_INITIALIZED);
12073 break;
12075 case SIGN_EXTEND:
12076 case ZERO_EXTEND:
12077 if (GET_MODE_CLASS (mode) != MODE_INT)
12078 break;
12079 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12080 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12081 if (op0 == 0)
12082 break;
12083 else if (GET_CODE (rtl) == ZERO_EXTEND
12084 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12085 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12086 < HOST_BITS_PER_WIDE_INT
12087 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12088 to expand zero extend as two shifts instead of
12089 masking. */
12090 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12092 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12093 mem_loc_result = op0;
12094 add_loc_descr (&mem_loc_result,
12095 int_loc_descriptor (GET_MODE_MASK (imode)));
12096 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12098 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12100 int shift = DWARF2_ADDR_SIZE
12101 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12102 shift *= BITS_PER_UNIT;
12103 if (GET_CODE (rtl) == SIGN_EXTEND)
12104 op = DW_OP_shra;
12105 else
12106 op = DW_OP_shr;
12107 mem_loc_result = op0;
12108 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12109 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12110 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12111 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12113 else if (!dwarf_strict)
12115 dw_die_ref type_die1, type_die2;
12116 dw_loc_descr_ref cvt;
12118 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12119 GET_CODE (rtl) == ZERO_EXTEND);
12120 if (type_die1 == NULL)
12121 break;
12122 type_die2 = base_type_for_mode (mode, 1);
12123 if (type_die2 == NULL)
12124 break;
12125 mem_loc_result = op0;
12126 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12127 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12128 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12129 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12130 add_loc_descr (&mem_loc_result, cvt);
12131 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12132 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12133 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12134 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12135 add_loc_descr (&mem_loc_result, cvt);
12137 break;
12139 case MEM:
12141 rtx new_rtl = avoid_constant_pool_reference (rtl);
12142 if (new_rtl != rtl)
12144 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12145 initialized);
12146 if (mem_loc_result != NULL)
12147 return mem_loc_result;
12150 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12151 get_address_mode (rtl), mode,
12152 VAR_INIT_STATUS_INITIALIZED);
12153 if (mem_loc_result == NULL)
12154 mem_loc_result = tls_mem_loc_descriptor (rtl);
12155 if (mem_loc_result != NULL)
12157 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12158 || GET_MODE_CLASS (mode) != MODE_INT)
12160 dw_die_ref type_die;
12161 dw_loc_descr_ref deref;
12163 if (dwarf_strict)
12164 return NULL;
12165 type_die
12166 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12167 if (type_die == NULL)
12168 return NULL;
12169 deref = new_loc_descr (DW_OP_GNU_deref_type,
12170 GET_MODE_SIZE (mode), 0);
12171 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12172 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12173 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12174 add_loc_descr (&mem_loc_result, deref);
12176 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12177 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12178 else
12179 add_loc_descr (&mem_loc_result,
12180 new_loc_descr (DW_OP_deref_size,
12181 GET_MODE_SIZE (mode), 0));
12183 break;
12185 case LO_SUM:
12186 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12188 case LABEL_REF:
12189 /* Some ports can transform a symbol ref into a label ref, because
12190 the symbol ref is too far away and has to be dumped into a constant
12191 pool. */
12192 case CONST:
12193 case SYMBOL_REF:
12194 if (GET_MODE_CLASS (mode) != MODE_INT
12195 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12196 #ifdef POINTERS_EXTEND_UNSIGNED
12197 && (mode != Pmode || mem_mode == VOIDmode)
12198 #endif
12200 break;
12201 if (GET_CODE (rtl) == SYMBOL_REF
12202 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12204 dw_loc_descr_ref temp;
12206 /* If this is not defined, we have no way to emit the data. */
12207 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12208 break;
12210 temp = new_addr_loc_descr (rtl, dtprel_true);
12212 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12213 add_loc_descr (&mem_loc_result, temp);
12215 break;
12218 if (!const_ok_for_output (rtl))
12219 break;
12221 symref:
12222 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12223 vec_safe_push (used_rtx_array, rtl);
12224 break;
12226 case CONCAT:
12227 case CONCATN:
12228 case VAR_LOCATION:
12229 case DEBUG_IMPLICIT_PTR:
12230 expansion_failed (NULL_TREE, rtl,
12231 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12232 return 0;
12234 case ENTRY_VALUE:
12235 if (dwarf_strict)
12236 return NULL;
12237 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12239 if (GET_MODE_CLASS (mode) != MODE_INT
12240 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12241 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12242 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12243 else
12245 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12246 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12247 return NULL;
12248 op0 = one_reg_loc_descriptor (dbx_regnum,
12249 VAR_INIT_STATUS_INITIALIZED);
12252 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12253 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12255 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12256 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12257 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12258 return NULL;
12260 else
12261 gcc_unreachable ();
12262 if (op0 == NULL)
12263 return NULL;
12264 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12265 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12266 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12267 break;
12269 case DEBUG_PARAMETER_REF:
12270 mem_loc_result = parameter_ref_descriptor (rtl);
12271 break;
12273 case PRE_MODIFY:
12274 /* Extract the PLUS expression nested inside and fall into
12275 PLUS code below. */
12276 rtl = XEXP (rtl, 1);
12277 goto plus;
12279 case PRE_INC:
12280 case PRE_DEC:
12281 /* Turn these into a PLUS expression and fall into the PLUS code
12282 below. */
12283 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12284 GEN_INT (GET_CODE (rtl) == PRE_INC
12285 ? GET_MODE_UNIT_SIZE (mem_mode)
12286 : -GET_MODE_UNIT_SIZE (mem_mode)));
12288 /* ... fall through ... */
12290 case PLUS:
12291 plus:
12292 if (is_based_loc (rtl)
12293 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12294 || XEXP (rtl, 0) == arg_pointer_rtx
12295 || XEXP (rtl, 0) == frame_pointer_rtx)
12296 && GET_MODE_CLASS (mode) == MODE_INT)
12297 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12298 INTVAL (XEXP (rtl, 1)),
12299 VAR_INIT_STATUS_INITIALIZED);
12300 else
12302 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12303 VAR_INIT_STATUS_INITIALIZED);
12304 if (mem_loc_result == 0)
12305 break;
12307 if (CONST_INT_P (XEXP (rtl, 1))
12308 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12309 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12310 else
12312 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12313 VAR_INIT_STATUS_INITIALIZED);
12314 if (op1 == 0)
12315 break;
12316 add_loc_descr (&mem_loc_result, op1);
12317 add_loc_descr (&mem_loc_result,
12318 new_loc_descr (DW_OP_plus, 0, 0));
12321 break;
12323 /* If a pseudo-reg is optimized away, it is possible for it to
12324 be replaced with a MEM containing a multiply or shift. */
12325 case MINUS:
12326 op = DW_OP_minus;
12327 goto do_binop;
12329 case MULT:
12330 op = DW_OP_mul;
12331 goto do_binop;
12333 case DIV:
12334 if (!dwarf_strict
12335 && GET_MODE_CLASS (mode) == MODE_INT
12336 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12338 mem_loc_result = typed_binop (DW_OP_div, rtl,
12339 base_type_for_mode (mode, 0),
12340 mode, mem_mode);
12341 break;
12343 op = DW_OP_div;
12344 goto do_binop;
12346 case UMOD:
12347 op = DW_OP_mod;
12348 goto do_binop;
12350 case ASHIFT:
12351 op = DW_OP_shl;
12352 goto do_shift;
12354 case ASHIFTRT:
12355 op = DW_OP_shra;
12356 goto do_shift;
12358 case LSHIFTRT:
12359 op = DW_OP_shr;
12360 goto do_shift;
12362 do_shift:
12363 if (GET_MODE_CLASS (mode) != MODE_INT)
12364 break;
12365 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12366 VAR_INIT_STATUS_INITIALIZED);
12368 rtx rtlop1 = XEXP (rtl, 1);
12369 if (GET_MODE (rtlop1) != VOIDmode
12370 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12371 < GET_MODE_BITSIZE (mode))
12372 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12373 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12374 VAR_INIT_STATUS_INITIALIZED);
12377 if (op0 == 0 || op1 == 0)
12378 break;
12380 mem_loc_result = op0;
12381 add_loc_descr (&mem_loc_result, op1);
12382 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12383 break;
12385 case AND:
12386 op = DW_OP_and;
12387 goto do_binop;
12389 case IOR:
12390 op = DW_OP_or;
12391 goto do_binop;
12393 case XOR:
12394 op = DW_OP_xor;
12395 goto do_binop;
12397 do_binop:
12398 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12399 VAR_INIT_STATUS_INITIALIZED);
12400 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12401 VAR_INIT_STATUS_INITIALIZED);
12403 if (op0 == 0 || op1 == 0)
12404 break;
12406 mem_loc_result = op0;
12407 add_loc_descr (&mem_loc_result, op1);
12408 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12409 break;
12411 case MOD:
12412 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12414 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12415 base_type_for_mode (mode, 0),
12416 mode, mem_mode);
12417 break;
12420 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12421 VAR_INIT_STATUS_INITIALIZED);
12422 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12423 VAR_INIT_STATUS_INITIALIZED);
12425 if (op0 == 0 || op1 == 0)
12426 break;
12428 mem_loc_result = op0;
12429 add_loc_descr (&mem_loc_result, op1);
12430 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12431 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12432 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12433 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12434 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12435 break;
12437 case UDIV:
12438 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12440 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12442 op = DW_OP_div;
12443 goto do_binop;
12445 mem_loc_result = typed_binop (DW_OP_div, rtl,
12446 base_type_for_mode (mode, 1),
12447 mode, mem_mode);
12449 break;
12451 case NOT:
12452 op = DW_OP_not;
12453 goto do_unop;
12455 case ABS:
12456 op = DW_OP_abs;
12457 goto do_unop;
12459 case NEG:
12460 op = DW_OP_neg;
12461 goto do_unop;
12463 do_unop:
12464 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12465 VAR_INIT_STATUS_INITIALIZED);
12467 if (op0 == 0)
12468 break;
12470 mem_loc_result = op0;
12471 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12472 break;
12474 case CONST_INT:
12475 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12476 #ifdef POINTERS_EXTEND_UNSIGNED
12477 || (mode == Pmode
12478 && mem_mode != VOIDmode
12479 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12480 #endif
12483 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12484 break;
12486 if (!dwarf_strict
12487 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12488 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12490 dw_die_ref type_die = base_type_for_mode (mode, 1);
12491 enum machine_mode amode;
12492 if (type_die == NULL)
12493 return NULL;
12494 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12495 MODE_INT, 0);
12496 if (INTVAL (rtl) >= 0
12497 && amode != BLKmode
12498 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12499 /* const DW_OP_GNU_convert <XXX> vs.
12500 DW_OP_GNU_const_type <XXX, 1, const>. */
12501 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12502 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12504 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12505 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12506 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12507 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12508 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12509 add_loc_descr (&mem_loc_result, op0);
12510 return mem_loc_result;
12512 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12513 INTVAL (rtl));
12514 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12515 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12516 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12517 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12518 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12519 else
12521 mem_loc_result->dw_loc_oprnd2.val_class
12522 = dw_val_class_const_double;
12523 mem_loc_result->dw_loc_oprnd2.v.val_double
12524 = double_int::from_shwi (INTVAL (rtl));
12527 break;
12529 case CONST_DOUBLE:
12530 if (!dwarf_strict)
12532 dw_die_ref type_die;
12534 /* Note that a CONST_DOUBLE rtx could represent either an integer
12535 or a floating-point constant. A CONST_DOUBLE is used whenever
12536 the constant requires more than one word in order to be
12537 adequately represented. We output CONST_DOUBLEs as blocks. */
12538 if (mode == VOIDmode
12539 || (GET_MODE (rtl) == VOIDmode
12540 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12541 break;
12542 type_die = base_type_for_mode (mode,
12543 GET_MODE_CLASS (mode) == MODE_INT);
12544 if (type_die == NULL)
12545 return NULL;
12546 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12547 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12548 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12549 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12550 if (SCALAR_FLOAT_MODE_P (mode))
12552 unsigned int length = GET_MODE_SIZE (mode);
12553 unsigned char *array
12554 = (unsigned char*) ggc_alloc_atomic (length);
12556 insert_float (rtl, array);
12557 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12558 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12559 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12560 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12562 else
12564 mem_loc_result->dw_loc_oprnd2.val_class
12565 = dw_val_class_const_double;
12566 mem_loc_result->dw_loc_oprnd2.v.val_double
12567 = rtx_to_double_int (rtl);
12570 break;
12572 case EQ:
12573 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12574 break;
12576 case GE:
12577 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12578 break;
12580 case GT:
12581 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12582 break;
12584 case LE:
12585 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12586 break;
12588 case LT:
12589 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12590 break;
12592 case NE:
12593 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12594 break;
12596 case GEU:
12597 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12598 break;
12600 case GTU:
12601 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12602 break;
12604 case LEU:
12605 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12606 break;
12608 case LTU:
12609 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12610 break;
12612 case UMIN:
12613 case UMAX:
12614 if (GET_MODE_CLASS (mode) != MODE_INT)
12615 break;
12616 /* FALLTHRU */
12617 case SMIN:
12618 case SMAX:
12619 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12620 break;
12622 case ZERO_EXTRACT:
12623 case SIGN_EXTRACT:
12624 if (CONST_INT_P (XEXP (rtl, 1))
12625 && CONST_INT_P (XEXP (rtl, 2))
12626 && ((unsigned) INTVAL (XEXP (rtl, 1))
12627 + (unsigned) INTVAL (XEXP (rtl, 2))
12628 <= GET_MODE_BITSIZE (mode))
12629 && GET_MODE_CLASS (mode) == MODE_INT
12630 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12631 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12633 int shift, size;
12634 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12635 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12636 if (op0 == 0)
12637 break;
12638 if (GET_CODE (rtl) == SIGN_EXTRACT)
12639 op = DW_OP_shra;
12640 else
12641 op = DW_OP_shr;
12642 mem_loc_result = op0;
12643 size = INTVAL (XEXP (rtl, 1));
12644 shift = INTVAL (XEXP (rtl, 2));
12645 if (BITS_BIG_ENDIAN)
12646 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12647 - shift - size;
12648 if (shift + size != (int) DWARF2_ADDR_SIZE)
12650 add_loc_descr (&mem_loc_result,
12651 int_loc_descriptor (DWARF2_ADDR_SIZE
12652 - shift - size));
12653 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12655 if (size != (int) DWARF2_ADDR_SIZE)
12657 add_loc_descr (&mem_loc_result,
12658 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12659 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12662 break;
12664 case IF_THEN_ELSE:
12666 dw_loc_descr_ref op2, bra_node, drop_node;
12667 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12668 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12669 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12670 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12671 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12672 VAR_INIT_STATUS_INITIALIZED);
12673 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12674 VAR_INIT_STATUS_INITIALIZED);
12675 if (op0 == NULL || op1 == NULL || op2 == NULL)
12676 break;
12678 mem_loc_result = op1;
12679 add_loc_descr (&mem_loc_result, op2);
12680 add_loc_descr (&mem_loc_result, op0);
12681 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12682 add_loc_descr (&mem_loc_result, bra_node);
12683 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12684 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12685 add_loc_descr (&mem_loc_result, drop_node);
12686 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12687 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12689 break;
12691 case FLOAT_EXTEND:
12692 case FLOAT_TRUNCATE:
12693 case FLOAT:
12694 case UNSIGNED_FLOAT:
12695 case FIX:
12696 case UNSIGNED_FIX:
12697 if (!dwarf_strict)
12699 dw_die_ref type_die;
12700 dw_loc_descr_ref cvt;
12702 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12703 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12704 if (op0 == NULL)
12705 break;
12706 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12707 && (GET_CODE (rtl) == FLOAT
12708 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12709 <= DWARF2_ADDR_SIZE))
12711 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12712 GET_CODE (rtl) == UNSIGNED_FLOAT);
12713 if (type_die == NULL)
12714 break;
12715 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12716 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12717 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12718 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12719 add_loc_descr (&op0, cvt);
12721 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12722 if (type_die == NULL)
12723 break;
12724 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12725 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12726 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12727 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12728 add_loc_descr (&op0, cvt);
12729 if (GET_MODE_CLASS (mode) == MODE_INT
12730 && (GET_CODE (rtl) == FIX
12731 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12733 op0 = convert_descriptor_to_mode (mode, op0);
12734 if (op0 == NULL)
12735 break;
12737 mem_loc_result = op0;
12739 break;
12741 case CLZ:
12742 case CTZ:
12743 case FFS:
12744 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12745 break;
12747 case POPCOUNT:
12748 case PARITY:
12749 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12750 break;
12752 case BSWAP:
12753 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12754 break;
12756 case ROTATE:
12757 case ROTATERT:
12758 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12759 break;
12761 case COMPARE:
12762 /* In theory, we could implement the above. */
12763 /* DWARF cannot represent the unsigned compare operations
12764 natively. */
12765 case SS_MULT:
12766 case US_MULT:
12767 case SS_DIV:
12768 case US_DIV:
12769 case SS_PLUS:
12770 case US_PLUS:
12771 case SS_MINUS:
12772 case US_MINUS:
12773 case SS_NEG:
12774 case US_NEG:
12775 case SS_ABS:
12776 case SS_ASHIFT:
12777 case US_ASHIFT:
12778 case SS_TRUNCATE:
12779 case US_TRUNCATE:
12780 case UNORDERED:
12781 case ORDERED:
12782 case UNEQ:
12783 case UNGE:
12784 case UNGT:
12785 case UNLE:
12786 case UNLT:
12787 case LTGT:
12788 case FRACT_CONVERT:
12789 case UNSIGNED_FRACT_CONVERT:
12790 case SAT_FRACT:
12791 case UNSIGNED_SAT_FRACT:
12792 case SQRT:
12793 case ASM_OPERANDS:
12794 case VEC_MERGE:
12795 case VEC_SELECT:
12796 case VEC_CONCAT:
12797 case VEC_DUPLICATE:
12798 case UNSPEC:
12799 case HIGH:
12800 case FMA:
12801 case STRICT_LOW_PART:
12802 case CONST_VECTOR:
12803 case CONST_FIXED:
12804 case CLRSB:
12805 case CLOBBER:
12806 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12807 can't express it in the debug info. This can happen e.g. with some
12808 TLS UNSPECs. */
12809 break;
12811 case CONST_STRING:
12812 resolve_one_addr (&rtl, NULL);
12813 goto symref;
12815 default:
12816 #ifdef ENABLE_CHECKING
12817 print_rtl (stderr, rtl);
12818 gcc_unreachable ();
12819 #else
12820 break;
12821 #endif
12824 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12825 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12827 return mem_loc_result;
12830 /* Return a descriptor that describes the concatenation of two locations.
12831 This is typically a complex variable. */
12833 static dw_loc_descr_ref
12834 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12836 dw_loc_descr_ref cc_loc_result = NULL;
12837 dw_loc_descr_ref x0_ref
12838 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12839 dw_loc_descr_ref x1_ref
12840 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12842 if (x0_ref == 0 || x1_ref == 0)
12843 return 0;
12845 cc_loc_result = x0_ref;
12846 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12848 add_loc_descr (&cc_loc_result, x1_ref);
12849 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12851 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12852 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12854 return cc_loc_result;
12857 /* Return a descriptor that describes the concatenation of N
12858 locations. */
12860 static dw_loc_descr_ref
12861 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12863 unsigned int i;
12864 dw_loc_descr_ref cc_loc_result = NULL;
12865 unsigned int n = XVECLEN (concatn, 0);
12867 for (i = 0; i < n; ++i)
12869 dw_loc_descr_ref ref;
12870 rtx x = XVECEXP (concatn, 0, i);
12872 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12873 if (ref == NULL)
12874 return NULL;
12876 add_loc_descr (&cc_loc_result, ref);
12877 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12880 if (cc_loc_result && 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 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12887 for DEBUG_IMPLICIT_PTR RTL. */
12889 static dw_loc_descr_ref
12890 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12892 dw_loc_descr_ref ret;
12893 dw_die_ref ref;
12895 if (dwarf_strict)
12896 return NULL;
12897 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12898 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12899 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12900 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12901 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12902 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12903 if (ref)
12905 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12906 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12907 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12909 else
12911 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12912 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12914 return ret;
12917 /* Output a proper Dwarf location descriptor for a variable or parameter
12918 which is either allocated in a register or in a memory location. For a
12919 register, we just generate an OP_REG and the register number. For a
12920 memory location we provide a Dwarf postfix expression describing how to
12921 generate the (dynamic) address of the object onto the address stack.
12923 MODE is mode of the decl if this loc_descriptor is going to be used in
12924 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12925 allowed, VOIDmode otherwise.
12927 If we don't know how to describe it, return 0. */
12929 static dw_loc_descr_ref
12930 loc_descriptor (rtx rtl, enum machine_mode mode,
12931 enum var_init_status initialized)
12933 dw_loc_descr_ref loc_result = NULL;
12935 switch (GET_CODE (rtl))
12937 case SUBREG:
12938 /* The case of a subreg may arise when we have a local (register)
12939 variable or a formal (register) parameter which doesn't quite fill
12940 up an entire register. For now, just assume that it is
12941 legitimate to make the Dwarf info refer to the whole register which
12942 contains the given subreg. */
12943 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12944 loc_result = loc_descriptor (SUBREG_REG (rtl),
12945 GET_MODE (SUBREG_REG (rtl)), initialized);
12946 else
12947 goto do_default;
12948 break;
12950 case REG:
12951 loc_result = reg_loc_descriptor (rtl, initialized);
12952 break;
12954 case MEM:
12955 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12956 GET_MODE (rtl), initialized);
12957 if (loc_result == NULL)
12958 loc_result = tls_mem_loc_descriptor (rtl);
12959 if (loc_result == NULL)
12961 rtx new_rtl = avoid_constant_pool_reference (rtl);
12962 if (new_rtl != rtl)
12963 loc_result = loc_descriptor (new_rtl, mode, initialized);
12965 break;
12967 case CONCAT:
12968 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12969 initialized);
12970 break;
12972 case CONCATN:
12973 loc_result = concatn_loc_descriptor (rtl, initialized);
12974 break;
12976 case VAR_LOCATION:
12977 /* Single part. */
12978 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12980 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12981 if (GET_CODE (loc) == EXPR_LIST)
12982 loc = XEXP (loc, 0);
12983 loc_result = loc_descriptor (loc, mode, initialized);
12984 break;
12987 rtl = XEXP (rtl, 1);
12988 /* FALLTHRU */
12990 case PARALLEL:
12992 rtvec par_elems = XVEC (rtl, 0);
12993 int num_elem = GET_NUM_ELEM (par_elems);
12994 enum machine_mode mode;
12995 int i;
12997 /* Create the first one, so we have something to add to. */
12998 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12999 VOIDmode, initialized);
13000 if (loc_result == NULL)
13001 return NULL;
13002 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13003 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13004 for (i = 1; i < num_elem; i++)
13006 dw_loc_descr_ref temp;
13008 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13009 VOIDmode, initialized);
13010 if (temp == NULL)
13011 return NULL;
13012 add_loc_descr (&loc_result, temp);
13013 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13014 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13017 break;
13019 case CONST_INT:
13020 if (mode != VOIDmode && mode != BLKmode)
13021 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13022 INTVAL (rtl));
13023 break;
13025 case CONST_DOUBLE:
13026 if (mode == VOIDmode)
13027 mode = GET_MODE (rtl);
13029 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13031 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13033 /* Note that a CONST_DOUBLE rtx could represent either an integer
13034 or a floating-point constant. A CONST_DOUBLE is used whenever
13035 the constant requires more than one word in order to be
13036 adequately represented. We output CONST_DOUBLEs as blocks. */
13037 loc_result = new_loc_descr (DW_OP_implicit_value,
13038 GET_MODE_SIZE (mode), 0);
13039 if (SCALAR_FLOAT_MODE_P (mode))
13041 unsigned int length = GET_MODE_SIZE (mode);
13042 unsigned char *array
13043 = (unsigned char*) ggc_alloc_atomic (length);
13045 insert_float (rtl, array);
13046 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13047 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13048 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13049 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13051 else
13053 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13054 loc_result->dw_loc_oprnd2.v.val_double
13055 = rtx_to_double_int (rtl);
13058 break;
13060 case CONST_VECTOR:
13061 if (mode == VOIDmode)
13062 mode = GET_MODE (rtl);
13064 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13066 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13067 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13068 unsigned char *array = (unsigned char *)
13069 ggc_alloc_atomic (length * elt_size);
13070 unsigned int i;
13071 unsigned char *p;
13073 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13074 switch (GET_MODE_CLASS (mode))
13076 case MODE_VECTOR_INT:
13077 for (i = 0, p = array; i < length; i++, p += elt_size)
13079 rtx elt = CONST_VECTOR_ELT (rtl, i);
13080 double_int val = rtx_to_double_int (elt);
13082 if (elt_size <= sizeof (HOST_WIDE_INT))
13083 insert_int (val.to_shwi (), elt_size, p);
13084 else
13086 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13087 insert_double (val, p);
13090 break;
13092 case MODE_VECTOR_FLOAT:
13093 for (i = 0, p = array; i < length; i++, p += elt_size)
13095 rtx elt = CONST_VECTOR_ELT (rtl, i);
13096 insert_float (elt, p);
13098 break;
13100 default:
13101 gcc_unreachable ();
13104 loc_result = new_loc_descr (DW_OP_implicit_value,
13105 length * elt_size, 0);
13106 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13107 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13108 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13109 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13111 break;
13113 case CONST:
13114 if (mode == VOIDmode
13115 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13116 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13117 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13119 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13120 break;
13122 /* FALLTHROUGH */
13123 case SYMBOL_REF:
13124 if (!const_ok_for_output (rtl))
13125 break;
13126 case LABEL_REF:
13127 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13128 && (dwarf_version >= 4 || !dwarf_strict))
13130 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13131 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13132 vec_safe_push (used_rtx_array, rtl);
13134 break;
13136 case DEBUG_IMPLICIT_PTR:
13137 loc_result = implicit_ptr_descriptor (rtl, 0);
13138 break;
13140 case PLUS:
13141 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13142 && CONST_INT_P (XEXP (rtl, 1)))
13144 loc_result
13145 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13146 break;
13148 /* FALLTHRU */
13149 do_default:
13150 default:
13151 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13152 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13153 && dwarf_version >= 4)
13154 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13156 /* Value expression. */
13157 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13158 if (loc_result)
13159 add_loc_descr (&loc_result,
13160 new_loc_descr (DW_OP_stack_value, 0, 0));
13162 break;
13165 return loc_result;
13168 /* We need to figure out what section we should use as the base for the
13169 address ranges where a given location is valid.
13170 1. If this particular DECL has a section associated with it, use that.
13171 2. If this function has a section associated with it, use that.
13172 3. Otherwise, use the text section.
13173 XXX: If you split a variable across multiple sections, we won't notice. */
13175 static const char *
13176 secname_for_decl (const_tree decl)
13178 const char *secname;
13180 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13182 tree sectree = DECL_SECTION_NAME (decl);
13183 secname = TREE_STRING_POINTER (sectree);
13185 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13187 tree sectree = DECL_SECTION_NAME (current_function_decl);
13188 secname = TREE_STRING_POINTER (sectree);
13190 else if (cfun && in_cold_section_p)
13191 secname = crtl->subsections.cold_section_label;
13192 else
13193 secname = text_section_label;
13195 return secname;
13198 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13200 static bool
13201 decl_by_reference_p (tree decl)
13203 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13204 || TREE_CODE (decl) == VAR_DECL)
13205 && DECL_BY_REFERENCE (decl));
13208 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13209 for VARLOC. */
13211 static dw_loc_descr_ref
13212 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13213 enum var_init_status initialized)
13215 int have_address = 0;
13216 dw_loc_descr_ref descr;
13217 enum machine_mode mode;
13219 if (want_address != 2)
13221 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13222 /* Single part. */
13223 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13225 varloc = PAT_VAR_LOCATION_LOC (varloc);
13226 if (GET_CODE (varloc) == EXPR_LIST)
13227 varloc = XEXP (varloc, 0);
13228 mode = GET_MODE (varloc);
13229 if (MEM_P (varloc))
13231 rtx addr = XEXP (varloc, 0);
13232 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13233 mode, initialized);
13234 if (descr)
13235 have_address = 1;
13236 else
13238 rtx x = avoid_constant_pool_reference (varloc);
13239 if (x != varloc)
13240 descr = mem_loc_descriptor (x, mode, VOIDmode,
13241 initialized);
13244 else
13245 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13247 else
13248 return 0;
13250 else
13252 if (GET_CODE (varloc) == VAR_LOCATION)
13253 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13254 else
13255 mode = DECL_MODE (loc);
13256 descr = loc_descriptor (varloc, mode, initialized);
13257 have_address = 1;
13260 if (!descr)
13261 return 0;
13263 if (want_address == 2 && !have_address
13264 && (dwarf_version >= 4 || !dwarf_strict))
13266 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13268 expansion_failed (loc, NULL_RTX,
13269 "DWARF address size mismatch");
13270 return 0;
13272 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13273 have_address = 1;
13275 /* Show if we can't fill the request for an address. */
13276 if (want_address && !have_address)
13278 expansion_failed (loc, NULL_RTX,
13279 "Want address and only have value");
13280 return 0;
13283 /* If we've got an address and don't want one, dereference. */
13284 if (!want_address && have_address)
13286 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13287 enum dwarf_location_atom op;
13289 if (size > DWARF2_ADDR_SIZE || size == -1)
13291 expansion_failed (loc, NULL_RTX,
13292 "DWARF address size mismatch");
13293 return 0;
13295 else if (size == DWARF2_ADDR_SIZE)
13296 op = DW_OP_deref;
13297 else
13298 op = DW_OP_deref_size;
13300 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13303 return descr;
13306 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13307 if it is not possible. */
13309 static dw_loc_descr_ref
13310 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13312 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13313 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13314 else if (dwarf_version >= 3 || !dwarf_strict)
13315 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13316 else
13317 return NULL;
13320 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13321 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13323 static dw_loc_descr_ref
13324 dw_sra_loc_expr (tree decl, rtx loc)
13326 rtx p;
13327 unsigned int padsize = 0;
13328 dw_loc_descr_ref descr, *descr_tail;
13329 unsigned HOST_WIDE_INT decl_size;
13330 rtx varloc;
13331 enum var_init_status initialized;
13333 if (DECL_SIZE (decl) == NULL
13334 || !host_integerp (DECL_SIZE (decl), 1))
13335 return NULL;
13337 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13338 descr = NULL;
13339 descr_tail = &descr;
13341 for (p = loc; p; p = XEXP (p, 1))
13343 unsigned int bitsize = decl_piece_bitsize (p);
13344 rtx loc_note = *decl_piece_varloc_ptr (p);
13345 dw_loc_descr_ref cur_descr;
13346 dw_loc_descr_ref *tail, last = NULL;
13347 unsigned int opsize = 0;
13349 if (loc_note == NULL_RTX
13350 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13352 padsize += bitsize;
13353 continue;
13355 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13356 varloc = NOTE_VAR_LOCATION (loc_note);
13357 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13358 if (cur_descr == NULL)
13360 padsize += bitsize;
13361 continue;
13364 /* Check that cur_descr either doesn't use
13365 DW_OP_*piece operations, or their sum is equal
13366 to bitsize. Otherwise we can't embed it. */
13367 for (tail = &cur_descr; *tail != NULL;
13368 tail = &(*tail)->dw_loc_next)
13369 if ((*tail)->dw_loc_opc == DW_OP_piece)
13371 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13372 * BITS_PER_UNIT;
13373 last = *tail;
13375 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13377 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13378 last = *tail;
13381 if (last != NULL && opsize != bitsize)
13383 padsize += bitsize;
13384 continue;
13387 /* If there is a hole, add DW_OP_*piece after empty DWARF
13388 expression, which means that those bits are optimized out. */
13389 if (padsize)
13391 if (padsize > decl_size)
13392 return NULL;
13393 decl_size -= padsize;
13394 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13395 if (*descr_tail == NULL)
13396 return NULL;
13397 descr_tail = &(*descr_tail)->dw_loc_next;
13398 padsize = 0;
13400 *descr_tail = cur_descr;
13401 descr_tail = tail;
13402 if (bitsize > decl_size)
13403 return NULL;
13404 decl_size -= bitsize;
13405 if (last == NULL)
13407 HOST_WIDE_INT offset = 0;
13408 if (GET_CODE (varloc) == VAR_LOCATION
13409 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13411 varloc = PAT_VAR_LOCATION_LOC (varloc);
13412 if (GET_CODE (varloc) == EXPR_LIST)
13413 varloc = XEXP (varloc, 0);
13417 if (GET_CODE (varloc) == CONST
13418 || GET_CODE (varloc) == SIGN_EXTEND
13419 || GET_CODE (varloc) == ZERO_EXTEND)
13420 varloc = XEXP (varloc, 0);
13421 else if (GET_CODE (varloc) == SUBREG)
13422 varloc = SUBREG_REG (varloc);
13423 else
13424 break;
13426 while (1);
13427 /* DW_OP_bit_size offset should be zero for register
13428 or implicit location descriptions and empty location
13429 descriptions, but for memory addresses needs big endian
13430 adjustment. */
13431 if (MEM_P (varloc))
13433 unsigned HOST_WIDE_INT memsize
13434 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13435 if (memsize != bitsize)
13437 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13438 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13439 return NULL;
13440 if (memsize < bitsize)
13441 return NULL;
13442 if (BITS_BIG_ENDIAN)
13443 offset = memsize - bitsize;
13447 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13448 if (*descr_tail == NULL)
13449 return NULL;
13450 descr_tail = &(*descr_tail)->dw_loc_next;
13454 /* If there were any non-empty expressions, add padding till the end of
13455 the decl. */
13456 if (descr != NULL && decl_size != 0)
13458 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13459 if (*descr_tail == NULL)
13460 return NULL;
13462 return descr;
13465 /* Return the dwarf representation of the location list LOC_LIST of
13466 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13467 function. */
13469 static dw_loc_list_ref
13470 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13472 const char *endname, *secname;
13473 rtx varloc;
13474 enum var_init_status initialized;
13475 struct var_loc_node *node;
13476 dw_loc_descr_ref descr;
13477 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13478 dw_loc_list_ref list = NULL;
13479 dw_loc_list_ref *listp = &list;
13481 /* Now that we know what section we are using for a base,
13482 actually construct the list of locations.
13483 The first location information is what is passed to the
13484 function that creates the location list, and the remaining
13485 locations just get added on to that list.
13486 Note that we only know the start address for a location
13487 (IE location changes), so to build the range, we use
13488 the range [current location start, next location start].
13489 This means we have to special case the last node, and generate
13490 a range of [last location start, end of function label]. */
13492 secname = secname_for_decl (decl);
13494 for (node = loc_list->first; node; node = node->next)
13495 if (GET_CODE (node->loc) == EXPR_LIST
13496 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13498 if (GET_CODE (node->loc) == EXPR_LIST)
13500 /* This requires DW_OP_{,bit_}piece, which is not usable
13501 inside DWARF expressions. */
13502 if (want_address != 2)
13503 continue;
13504 descr = dw_sra_loc_expr (decl, node->loc);
13505 if (descr == NULL)
13506 continue;
13508 else
13510 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13511 varloc = NOTE_VAR_LOCATION (node->loc);
13512 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13514 if (descr)
13516 bool range_across_switch = false;
13517 /* If section switch happens in between node->label
13518 and node->next->label (or end of function) and
13519 we can't emit it as a single entry list,
13520 emit two ranges, first one ending at the end
13521 of first partition and second one starting at the
13522 beginning of second partition. */
13523 if (node == loc_list->last_before_switch
13524 && (node != loc_list->first || loc_list->first->next)
13525 && current_function_decl)
13527 endname = cfun->fde->dw_fde_end;
13528 range_across_switch = true;
13530 /* The variable has a location between NODE->LABEL and
13531 NODE->NEXT->LABEL. */
13532 else if (node->next)
13533 endname = node->next->label;
13534 /* If the variable has a location at the last label
13535 it keeps its location until the end of function. */
13536 else if (!current_function_decl)
13537 endname = text_end_label;
13538 else
13540 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13541 current_function_funcdef_no);
13542 endname = ggc_strdup (label_id);
13545 *listp = new_loc_list (descr, node->label, endname, secname);
13546 if (TREE_CODE (decl) == PARM_DECL
13547 && node == loc_list->first
13548 && NOTE_P (node->loc)
13549 && strcmp (node->label, endname) == 0)
13550 (*listp)->force = true;
13551 listp = &(*listp)->dw_loc_next;
13553 if (range_across_switch)
13555 if (GET_CODE (node->loc) == EXPR_LIST)
13556 descr = dw_sra_loc_expr (decl, node->loc);
13557 else
13559 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13560 varloc = NOTE_VAR_LOCATION (node->loc);
13561 descr = dw_loc_list_1 (decl, varloc, want_address,
13562 initialized);
13564 gcc_assert (descr);
13565 /* The variable has a location between NODE->LABEL and
13566 NODE->NEXT->LABEL. */
13567 if (node->next)
13568 endname = node->next->label;
13569 else
13570 endname = cfun->fde->dw_fde_second_end;
13571 *listp = new_loc_list (descr,
13572 cfun->fde->dw_fde_second_begin,
13573 endname, secname);
13574 listp = &(*listp)->dw_loc_next;
13579 /* Try to avoid the overhead of a location list emitting a location
13580 expression instead, but only if we didn't have more than one
13581 location entry in the first place. If some entries were not
13582 representable, we don't want to pretend a single entry that was
13583 applies to the entire scope in which the variable is
13584 available. */
13585 if (list && loc_list->first->next)
13586 gen_llsym (list);
13588 return list;
13591 /* Return if the loc_list has only single element and thus can be represented
13592 as location description. */
13594 static bool
13595 single_element_loc_list_p (dw_loc_list_ref list)
13597 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13598 return !list->ll_symbol;
13601 /* To each location in list LIST add loc descr REF. */
13603 static void
13604 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13606 dw_loc_descr_ref copy;
13607 add_loc_descr (&list->expr, ref);
13608 list = list->dw_loc_next;
13609 while (list)
13611 copy = ggc_alloc_dw_loc_descr_node ();
13612 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13613 add_loc_descr (&list->expr, copy);
13614 while (copy->dw_loc_next)
13616 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13617 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13618 copy->dw_loc_next = new_copy;
13619 copy = new_copy;
13621 list = list->dw_loc_next;
13625 /* Given two lists RET and LIST
13626 produce location list that is result of adding expression in LIST
13627 to expression in RET on each position in program.
13628 Might be destructive on both RET and LIST.
13630 TODO: We handle only simple cases of RET or LIST having at most one
13631 element. General case would inolve sorting the lists in program order
13632 and merging them that will need some additional work.
13633 Adding that will improve quality of debug info especially for SRA-ed
13634 structures. */
13636 static void
13637 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13639 if (!list)
13640 return;
13641 if (!*ret)
13643 *ret = list;
13644 return;
13646 if (!list->dw_loc_next)
13648 add_loc_descr_to_each (*ret, list->expr);
13649 return;
13651 if (!(*ret)->dw_loc_next)
13653 add_loc_descr_to_each (list, (*ret)->expr);
13654 *ret = list;
13655 return;
13657 expansion_failed (NULL_TREE, NULL_RTX,
13658 "Don't know how to merge two non-trivial"
13659 " location lists.\n");
13660 *ret = NULL;
13661 return;
13664 /* LOC is constant expression. Try a luck, look it up in constant
13665 pool and return its loc_descr of its address. */
13667 static dw_loc_descr_ref
13668 cst_pool_loc_descr (tree loc)
13670 /* Get an RTL for this, if something has been emitted. */
13671 rtx rtl = lookup_constant_def (loc);
13673 if (!rtl || !MEM_P (rtl))
13675 gcc_assert (!rtl);
13676 return 0;
13678 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13680 /* TODO: We might get more coverage if we was actually delaying expansion
13681 of all expressions till end of compilation when constant pools are fully
13682 populated. */
13683 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13685 expansion_failed (loc, NULL_RTX,
13686 "CST value in contant pool but not marked.");
13687 return 0;
13689 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13690 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13693 /* Return dw_loc_list representing address of addr_expr LOC
13694 by looking for inner INDIRECT_REF expression and turning
13695 it into simple arithmetics. */
13697 static dw_loc_list_ref
13698 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13700 tree obj, offset;
13701 HOST_WIDE_INT bitsize, bitpos, bytepos;
13702 enum machine_mode mode;
13703 int unsignedp, volatilep = 0;
13704 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13706 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13707 &bitsize, &bitpos, &offset, &mode,
13708 &unsignedp, &volatilep, false);
13709 STRIP_NOPS (obj);
13710 if (bitpos % BITS_PER_UNIT)
13712 expansion_failed (loc, NULL_RTX, "bitfield access");
13713 return 0;
13715 if (!INDIRECT_REF_P (obj))
13717 expansion_failed (obj,
13718 NULL_RTX, "no indirect ref in inner refrence");
13719 return 0;
13721 if (!offset && !bitpos)
13722 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13723 else if (toplev
13724 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13725 && (dwarf_version >= 4 || !dwarf_strict))
13727 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13728 if (!list_ret)
13729 return 0;
13730 if (offset)
13732 /* Variable offset. */
13733 list_ret1 = loc_list_from_tree (offset, 0);
13734 if (list_ret1 == 0)
13735 return 0;
13736 add_loc_list (&list_ret, list_ret1);
13737 if (!list_ret)
13738 return 0;
13739 add_loc_descr_to_each (list_ret,
13740 new_loc_descr (DW_OP_plus, 0, 0));
13742 bytepos = bitpos / BITS_PER_UNIT;
13743 if (bytepos > 0)
13744 add_loc_descr_to_each (list_ret,
13745 new_loc_descr (DW_OP_plus_uconst,
13746 bytepos, 0));
13747 else if (bytepos < 0)
13748 loc_list_plus_const (list_ret, bytepos);
13749 add_loc_descr_to_each (list_ret,
13750 new_loc_descr (DW_OP_stack_value, 0, 0));
13752 return list_ret;
13756 /* Generate Dwarf location list representing LOC.
13757 If WANT_ADDRESS is false, expression computing LOC will be computed
13758 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13759 if WANT_ADDRESS is 2, expression computing address useable in location
13760 will be returned (i.e. DW_OP_reg can be used
13761 to refer to register values). */
13763 static dw_loc_list_ref
13764 loc_list_from_tree (tree loc, int want_address)
13766 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13767 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13768 int have_address = 0;
13769 enum dwarf_location_atom op;
13771 /* ??? Most of the time we do not take proper care for sign/zero
13772 extending the values properly. Hopefully this won't be a real
13773 problem... */
13775 switch (TREE_CODE (loc))
13777 case ERROR_MARK:
13778 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13779 return 0;
13781 case PLACEHOLDER_EXPR:
13782 /* This case involves extracting fields from an object to determine the
13783 position of other fields. We don't try to encode this here. The
13784 only user of this is Ada, which encodes the needed information using
13785 the names of types. */
13786 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13787 return 0;
13789 case CALL_EXPR:
13790 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13791 /* There are no opcodes for these operations. */
13792 return 0;
13794 case PREINCREMENT_EXPR:
13795 case PREDECREMENT_EXPR:
13796 case POSTINCREMENT_EXPR:
13797 case POSTDECREMENT_EXPR:
13798 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13799 /* There are no opcodes for these operations. */
13800 return 0;
13802 case ADDR_EXPR:
13803 /* If we already want an address, see if there is INDIRECT_REF inside
13804 e.g. for &this->field. */
13805 if (want_address)
13807 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13808 (loc, want_address == 2);
13809 if (list_ret)
13810 have_address = 1;
13811 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13812 && (ret = cst_pool_loc_descr (loc)))
13813 have_address = 1;
13815 /* Otherwise, process the argument and look for the address. */
13816 if (!list_ret && !ret)
13817 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13818 else
13820 if (want_address)
13821 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13822 return NULL;
13824 break;
13826 case VAR_DECL:
13827 if (DECL_THREAD_LOCAL_P (loc))
13829 rtx rtl;
13830 enum dwarf_location_atom tls_op;
13831 enum dtprel_bool dtprel = dtprel_false;
13833 if (targetm.have_tls)
13835 /* If this is not defined, we have no way to emit the
13836 data. */
13837 if (!targetm.asm_out.output_dwarf_dtprel)
13838 return 0;
13840 /* The way DW_OP_GNU_push_tls_address is specified, we
13841 can only look up addresses of objects in the current
13842 module. We used DW_OP_addr as first op, but that's
13843 wrong, because DW_OP_addr is relocated by the debug
13844 info consumer, while DW_OP_GNU_push_tls_address
13845 operand shouldn't be. */
13846 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13847 return 0;
13848 dtprel = dtprel_true;
13849 tls_op = DW_OP_GNU_push_tls_address;
13851 else
13853 if (!targetm.emutls.debug_form_tls_address
13854 || !(dwarf_version >= 3 || !dwarf_strict))
13855 return 0;
13856 /* We stuffed the control variable into the DECL_VALUE_EXPR
13857 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13858 no longer appear in gimple code. We used the control
13859 variable in specific so that we could pick it up here. */
13860 loc = DECL_VALUE_EXPR (loc);
13861 tls_op = DW_OP_form_tls_address;
13864 rtl = rtl_for_decl_location (loc);
13865 if (rtl == NULL_RTX)
13866 return 0;
13868 if (!MEM_P (rtl))
13869 return 0;
13870 rtl = XEXP (rtl, 0);
13871 if (! CONSTANT_P (rtl))
13872 return 0;
13874 ret = new_addr_loc_descr (rtl, dtprel);
13875 ret1 = new_loc_descr (tls_op, 0, 0);
13876 add_loc_descr (&ret, ret1);
13878 have_address = 1;
13879 break;
13881 /* FALLTHRU */
13883 case PARM_DECL:
13884 case RESULT_DECL:
13885 if (DECL_HAS_VALUE_EXPR_P (loc))
13886 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13887 want_address);
13888 /* FALLTHRU */
13890 case FUNCTION_DECL:
13892 rtx rtl;
13893 var_loc_list *loc_list = lookup_decl_loc (loc);
13895 if (loc_list && loc_list->first)
13897 list_ret = dw_loc_list (loc_list, loc, want_address);
13898 have_address = want_address != 0;
13899 break;
13901 rtl = rtl_for_decl_location (loc);
13902 if (rtl == NULL_RTX)
13904 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13905 return 0;
13907 else if (CONST_INT_P (rtl))
13909 HOST_WIDE_INT val = INTVAL (rtl);
13910 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13911 val &= GET_MODE_MASK (DECL_MODE (loc));
13912 ret = int_loc_descriptor (val);
13914 else if (GET_CODE (rtl) == CONST_STRING)
13916 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13917 return 0;
13919 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13920 ret = new_addr_loc_descr (rtl, dtprel_false);
13921 else
13923 enum machine_mode mode, mem_mode;
13925 /* Certain constructs can only be represented at top-level. */
13926 if (want_address == 2)
13928 ret = loc_descriptor (rtl, VOIDmode,
13929 VAR_INIT_STATUS_INITIALIZED);
13930 have_address = 1;
13932 else
13934 mode = GET_MODE (rtl);
13935 mem_mode = VOIDmode;
13936 if (MEM_P (rtl))
13938 mem_mode = mode;
13939 mode = get_address_mode (rtl);
13940 rtl = XEXP (rtl, 0);
13941 have_address = 1;
13943 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13944 VAR_INIT_STATUS_INITIALIZED);
13946 if (!ret)
13947 expansion_failed (loc, rtl,
13948 "failed to produce loc descriptor for rtl");
13951 break;
13953 case MEM_REF:
13954 /* ??? FIXME. */
13955 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13956 return 0;
13957 /* Fallthru. */
13958 case INDIRECT_REF:
13959 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13960 have_address = 1;
13961 break;
13963 case COMPOUND_EXPR:
13964 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13966 CASE_CONVERT:
13967 case VIEW_CONVERT_EXPR:
13968 case SAVE_EXPR:
13969 case MODIFY_EXPR:
13970 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13972 case COMPONENT_REF:
13973 case BIT_FIELD_REF:
13974 case ARRAY_REF:
13975 case ARRAY_RANGE_REF:
13976 case REALPART_EXPR:
13977 case IMAGPART_EXPR:
13979 tree obj, offset;
13980 HOST_WIDE_INT bitsize, bitpos, bytepos;
13981 enum machine_mode mode;
13982 int unsignedp, volatilep = 0;
13984 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13985 &unsignedp, &volatilep, false);
13987 gcc_assert (obj != loc);
13989 list_ret = loc_list_from_tree (obj,
13990 want_address == 2
13991 && !bitpos && !offset ? 2 : 1);
13992 /* TODO: We can extract value of the small expression via shifting even
13993 for nonzero bitpos. */
13994 if (list_ret == 0)
13995 return 0;
13996 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13998 expansion_failed (loc, NULL_RTX,
13999 "bitfield access");
14000 return 0;
14003 if (offset != NULL_TREE)
14005 /* Variable offset. */
14006 list_ret1 = loc_list_from_tree (offset, 0);
14007 if (list_ret1 == 0)
14008 return 0;
14009 add_loc_list (&list_ret, list_ret1);
14010 if (!list_ret)
14011 return 0;
14012 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14015 bytepos = bitpos / BITS_PER_UNIT;
14016 if (bytepos > 0)
14017 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14018 else if (bytepos < 0)
14019 loc_list_plus_const (list_ret, bytepos);
14021 have_address = 1;
14022 break;
14025 case INTEGER_CST:
14026 if ((want_address || !host_integerp (loc, 0))
14027 && (ret = cst_pool_loc_descr (loc)))
14028 have_address = 1;
14029 else if (want_address == 2
14030 && host_integerp (loc, 0)
14031 && (ret = address_of_int_loc_descriptor
14032 (int_size_in_bytes (TREE_TYPE (loc)),
14033 tree_low_cst (loc, 0))))
14034 have_address = 1;
14035 else if (host_integerp (loc, 0))
14036 ret = int_loc_descriptor (tree_low_cst (loc, 0));
14037 else
14039 expansion_failed (loc, NULL_RTX,
14040 "Integer operand is not host integer");
14041 return 0;
14043 break;
14045 case CONSTRUCTOR:
14046 case REAL_CST:
14047 case STRING_CST:
14048 case COMPLEX_CST:
14049 if ((ret = cst_pool_loc_descr (loc)))
14050 have_address = 1;
14051 else
14052 /* We can construct small constants here using int_loc_descriptor. */
14053 expansion_failed (loc, NULL_RTX,
14054 "constructor or constant not in constant pool");
14055 break;
14057 case TRUTH_AND_EXPR:
14058 case TRUTH_ANDIF_EXPR:
14059 case BIT_AND_EXPR:
14060 op = DW_OP_and;
14061 goto do_binop;
14063 case TRUTH_XOR_EXPR:
14064 case BIT_XOR_EXPR:
14065 op = DW_OP_xor;
14066 goto do_binop;
14068 case TRUTH_OR_EXPR:
14069 case TRUTH_ORIF_EXPR:
14070 case BIT_IOR_EXPR:
14071 op = DW_OP_or;
14072 goto do_binop;
14074 case FLOOR_DIV_EXPR:
14075 case CEIL_DIV_EXPR:
14076 case ROUND_DIV_EXPR:
14077 case TRUNC_DIV_EXPR:
14078 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14079 return 0;
14080 op = DW_OP_div;
14081 goto do_binop;
14083 case MINUS_EXPR:
14084 op = DW_OP_minus;
14085 goto do_binop;
14087 case FLOOR_MOD_EXPR:
14088 case CEIL_MOD_EXPR:
14089 case ROUND_MOD_EXPR:
14090 case TRUNC_MOD_EXPR:
14091 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14093 op = DW_OP_mod;
14094 goto do_binop;
14096 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14097 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14098 if (list_ret == 0 || list_ret1 == 0)
14099 return 0;
14101 add_loc_list (&list_ret, list_ret1);
14102 if (list_ret == 0)
14103 return 0;
14104 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14105 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14106 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14107 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14108 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14109 break;
14111 case MULT_EXPR:
14112 op = DW_OP_mul;
14113 goto do_binop;
14115 case LSHIFT_EXPR:
14116 op = DW_OP_shl;
14117 goto do_binop;
14119 case RSHIFT_EXPR:
14120 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14121 goto do_binop;
14123 case POINTER_PLUS_EXPR:
14124 case PLUS_EXPR:
14125 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14127 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14128 if (list_ret == 0)
14129 return 0;
14131 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14132 break;
14135 op = DW_OP_plus;
14136 goto do_binop;
14138 case LE_EXPR:
14139 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14140 return 0;
14142 op = DW_OP_le;
14143 goto do_binop;
14145 case GE_EXPR:
14146 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14147 return 0;
14149 op = DW_OP_ge;
14150 goto do_binop;
14152 case LT_EXPR:
14153 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14154 return 0;
14156 op = DW_OP_lt;
14157 goto do_binop;
14159 case GT_EXPR:
14160 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14161 return 0;
14163 op = DW_OP_gt;
14164 goto do_binop;
14166 case EQ_EXPR:
14167 op = DW_OP_eq;
14168 goto do_binop;
14170 case NE_EXPR:
14171 op = DW_OP_ne;
14172 goto do_binop;
14174 do_binop:
14175 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14176 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14177 if (list_ret == 0 || list_ret1 == 0)
14178 return 0;
14180 add_loc_list (&list_ret, list_ret1);
14181 if (list_ret == 0)
14182 return 0;
14183 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14184 break;
14186 case TRUTH_NOT_EXPR:
14187 case BIT_NOT_EXPR:
14188 op = DW_OP_not;
14189 goto do_unop;
14191 case ABS_EXPR:
14192 op = DW_OP_abs;
14193 goto do_unop;
14195 case NEGATE_EXPR:
14196 op = DW_OP_neg;
14197 goto do_unop;
14199 do_unop:
14200 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14201 if (list_ret == 0)
14202 return 0;
14204 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14205 break;
14207 case MIN_EXPR:
14208 case MAX_EXPR:
14210 const enum tree_code code =
14211 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14213 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14214 build2 (code, integer_type_node,
14215 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14216 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14219 /* ... fall through ... */
14221 case COND_EXPR:
14223 dw_loc_descr_ref lhs
14224 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14225 dw_loc_list_ref rhs
14226 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14227 dw_loc_descr_ref bra_node, jump_node, tmp;
14229 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14230 if (list_ret == 0 || lhs == 0 || rhs == 0)
14231 return 0;
14233 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14234 add_loc_descr_to_each (list_ret, bra_node);
14236 add_loc_list (&list_ret, rhs);
14237 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14238 add_loc_descr_to_each (list_ret, jump_node);
14240 add_loc_descr_to_each (list_ret, lhs);
14241 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14242 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14244 /* ??? Need a node to point the skip at. Use a nop. */
14245 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14246 add_loc_descr_to_each (list_ret, tmp);
14247 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14248 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14250 break;
14252 case FIX_TRUNC_EXPR:
14253 return 0;
14255 default:
14256 /* Leave front-end specific codes as simply unknown. This comes
14257 up, for instance, with the C STMT_EXPR. */
14258 if ((unsigned int) TREE_CODE (loc)
14259 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14261 expansion_failed (loc, NULL_RTX,
14262 "language specific tree node");
14263 return 0;
14266 #ifdef ENABLE_CHECKING
14267 /* Otherwise this is a generic code; we should just lists all of
14268 these explicitly. We forgot one. */
14269 gcc_unreachable ();
14270 #else
14271 /* In a release build, we want to degrade gracefully: better to
14272 generate incomplete debugging information than to crash. */
14273 return NULL;
14274 #endif
14277 if (!ret && !list_ret)
14278 return 0;
14280 if (want_address == 2 && !have_address
14281 && (dwarf_version >= 4 || !dwarf_strict))
14283 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14285 expansion_failed (loc, NULL_RTX,
14286 "DWARF address size mismatch");
14287 return 0;
14289 if (ret)
14290 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14291 else
14292 add_loc_descr_to_each (list_ret,
14293 new_loc_descr (DW_OP_stack_value, 0, 0));
14294 have_address = 1;
14296 /* Show if we can't fill the request for an address. */
14297 if (want_address && !have_address)
14299 expansion_failed (loc, NULL_RTX,
14300 "Want address and only have value");
14301 return 0;
14304 gcc_assert (!ret || !list_ret);
14306 /* If we've got an address and don't want one, dereference. */
14307 if (!want_address && have_address)
14309 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14311 if (size > DWARF2_ADDR_SIZE || size == -1)
14313 expansion_failed (loc, NULL_RTX,
14314 "DWARF address size mismatch");
14315 return 0;
14317 else if (size == DWARF2_ADDR_SIZE)
14318 op = DW_OP_deref;
14319 else
14320 op = DW_OP_deref_size;
14322 if (ret)
14323 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14324 else
14325 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14327 if (ret)
14328 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14330 return list_ret;
14333 /* Same as above but return only single location expression. */
14334 static dw_loc_descr_ref
14335 loc_descriptor_from_tree (tree loc, int want_address)
14337 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14338 if (!ret)
14339 return NULL;
14340 if (ret->dw_loc_next)
14342 expansion_failed (loc, NULL_RTX,
14343 "Location list where only loc descriptor needed");
14344 return NULL;
14346 return ret->expr;
14349 /* Given a value, round it up to the lowest multiple of `boundary'
14350 which is not less than the value itself. */
14352 static inline HOST_WIDE_INT
14353 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14355 return (((value + boundary - 1) / boundary) * boundary);
14358 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14359 pointer to the declared type for the relevant field variable, or return
14360 `integer_type_node' if the given node turns out to be an
14361 ERROR_MARK node. */
14363 static inline tree
14364 field_type (const_tree decl)
14366 tree type;
14368 if (TREE_CODE (decl) == ERROR_MARK)
14369 return integer_type_node;
14371 type = DECL_BIT_FIELD_TYPE (decl);
14372 if (type == NULL_TREE)
14373 type = TREE_TYPE (decl);
14375 return type;
14378 /* Given a pointer to a tree node, return the alignment in bits for
14379 it, or else return BITS_PER_WORD if the node actually turns out to
14380 be an ERROR_MARK node. */
14382 static inline unsigned
14383 simple_type_align_in_bits (const_tree type)
14385 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14388 static inline unsigned
14389 simple_decl_align_in_bits (const_tree decl)
14391 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14394 /* Return the result of rounding T up to ALIGN. */
14396 static inline double_int
14397 round_up_to_align (double_int t, unsigned int align)
14399 double_int alignd = double_int::from_uhwi (align);
14400 t += alignd;
14401 t += double_int_minus_one;
14402 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14403 t *= alignd;
14404 return t;
14407 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14408 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14409 or return 0 if we are unable to determine what that offset is, either
14410 because the argument turns out to be a pointer to an ERROR_MARK node, or
14411 because the offset is actually variable. (We can't handle the latter case
14412 just yet). */
14414 static HOST_WIDE_INT
14415 field_byte_offset (const_tree decl)
14417 double_int object_offset_in_bits;
14418 double_int object_offset_in_bytes;
14419 double_int bitpos_int;
14421 if (TREE_CODE (decl) == ERROR_MARK)
14422 return 0;
14424 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14426 /* We cannot yet cope with fields whose positions are variable, so
14427 for now, when we see such things, we simply return 0. Someday, we may
14428 be able to handle such cases, but it will be damn difficult. */
14429 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14430 return 0;
14432 bitpos_int = tree_to_double_int (bit_position (decl));
14434 #ifdef PCC_BITFIELD_TYPE_MATTERS
14435 if (PCC_BITFIELD_TYPE_MATTERS)
14437 tree type;
14438 tree field_size_tree;
14439 double_int deepest_bitpos;
14440 double_int field_size_in_bits;
14441 unsigned int type_align_in_bits;
14442 unsigned int decl_align_in_bits;
14443 double_int type_size_in_bits;
14445 type = field_type (decl);
14446 type_size_in_bits = double_int_type_size_in_bits (type);
14447 type_align_in_bits = simple_type_align_in_bits (type);
14449 field_size_tree = DECL_SIZE (decl);
14451 /* The size could be unspecified if there was an error, or for
14452 a flexible array member. */
14453 if (!field_size_tree)
14454 field_size_tree = bitsize_zero_node;
14456 /* If the size of the field is not constant, use the type size. */
14457 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14458 field_size_in_bits = tree_to_double_int (field_size_tree);
14459 else
14460 field_size_in_bits = type_size_in_bits;
14462 decl_align_in_bits = simple_decl_align_in_bits (decl);
14464 /* The GCC front-end doesn't make any attempt to keep track of the
14465 starting bit offset (relative to the start of the containing
14466 structure type) of the hypothetical "containing object" for a
14467 bit-field. Thus, when computing the byte offset value for the
14468 start of the "containing object" of a bit-field, we must deduce
14469 this information on our own. This can be rather tricky to do in
14470 some cases. For example, handling the following structure type
14471 definition when compiling for an i386/i486 target (which only
14472 aligns long long's to 32-bit boundaries) can be very tricky:
14474 struct S { int field1; long long field2:31; };
14476 Fortunately, there is a simple rule-of-thumb which can be used
14477 in such cases. When compiling for an i386/i486, GCC will
14478 allocate 8 bytes for the structure shown above. It decides to
14479 do this based upon one simple rule for bit-field allocation.
14480 GCC allocates each "containing object" for each bit-field at
14481 the first (i.e. lowest addressed) legitimate alignment boundary
14482 (based upon the required minimum alignment for the declared
14483 type of the field) which it can possibly use, subject to the
14484 condition that there is still enough available space remaining
14485 in the containing object (when allocated at the selected point)
14486 to fully accommodate all of the bits of the bit-field itself.
14488 This simple rule makes it obvious why GCC allocates 8 bytes for
14489 each object of the structure type shown above. When looking
14490 for a place to allocate the "containing object" for `field2',
14491 the compiler simply tries to allocate a 64-bit "containing
14492 object" at each successive 32-bit boundary (starting at zero)
14493 until it finds a place to allocate that 64- bit field such that
14494 at least 31 contiguous (and previously unallocated) bits remain
14495 within that selected 64 bit field. (As it turns out, for the
14496 example above, the compiler finds it is OK to allocate the
14497 "containing object" 64-bit field at bit-offset zero within the
14498 structure type.)
14500 Here we attempt to work backwards from the limited set of facts
14501 we're given, and we try to deduce from those facts, where GCC
14502 must have believed that the containing object started (within
14503 the structure type). The value we deduce is then used (by the
14504 callers of this routine) to generate DW_AT_location and
14505 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14506 the case of DW_AT_location, regular fields as well). */
14508 /* Figure out the bit-distance from the start of the structure to
14509 the "deepest" bit of the bit-field. */
14510 deepest_bitpos = bitpos_int + field_size_in_bits;
14512 /* This is the tricky part. Use some fancy footwork to deduce
14513 where the lowest addressed bit of the containing object must
14514 be. */
14515 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14517 /* Round up to type_align by default. This works best for
14518 bitfields. */
14519 object_offset_in_bits
14520 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14522 if (object_offset_in_bits.ugt (bitpos_int))
14524 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14526 /* Round up to decl_align instead. */
14527 object_offset_in_bits
14528 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14531 else
14532 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14533 object_offset_in_bits = bitpos_int;
14535 object_offset_in_bytes
14536 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14537 true, TRUNC_DIV_EXPR);
14538 return object_offset_in_bytes.to_shwi ();
14541 /* The following routines define various Dwarf attributes and any data
14542 associated with them. */
14544 /* Add a location description attribute value to a DIE.
14546 This emits location attributes suitable for whole variables and
14547 whole parameters. Note that the location attributes for struct fields are
14548 generated by the routine `data_member_location_attribute' below. */
14550 static inline void
14551 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14552 dw_loc_list_ref descr)
14554 if (descr == 0)
14555 return;
14556 if (single_element_loc_list_p (descr))
14557 add_AT_loc (die, attr_kind, descr->expr);
14558 else
14559 add_AT_loc_list (die, attr_kind, descr);
14562 /* Add DW_AT_accessibility attribute to DIE if needed. */
14564 static void
14565 add_accessibility_attribute (dw_die_ref die, tree decl)
14567 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14568 children, otherwise the default is DW_ACCESS_public. In DWARF2
14569 the default has always been DW_ACCESS_public. */
14570 if (TREE_PROTECTED (decl))
14571 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14572 else if (TREE_PRIVATE (decl))
14574 if (dwarf_version == 2
14575 || die->die_parent == NULL
14576 || die->die_parent->die_tag != DW_TAG_class_type)
14577 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14579 else if (dwarf_version > 2
14580 && die->die_parent
14581 && die->die_parent->die_tag == DW_TAG_class_type)
14582 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14585 /* Attach the specialized form of location attribute used for data members of
14586 struct and union types. In the special case of a FIELD_DECL node which
14587 represents a bit-field, the "offset" part of this special location
14588 descriptor must indicate the distance in bytes from the lowest-addressed
14589 byte of the containing struct or union type to the lowest-addressed byte of
14590 the "containing object" for the bit-field. (See the `field_byte_offset'
14591 function above).
14593 For any given bit-field, the "containing object" is a hypothetical object
14594 (of some integral or enum type) within which the given bit-field lives. The
14595 type of this hypothetical "containing object" is always the same as the
14596 declared type of the individual bit-field itself (for GCC anyway... the
14597 DWARF spec doesn't actually mandate this). Note that it is the size (in
14598 bytes) of the hypothetical "containing object" which will be given in the
14599 DW_AT_byte_size attribute for this bit-field. (See the
14600 `byte_size_attribute' function below.) It is also used when calculating the
14601 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14602 function below.) */
14604 static void
14605 add_data_member_location_attribute (dw_die_ref die, tree decl)
14607 HOST_WIDE_INT offset;
14608 dw_loc_descr_ref loc_descr = 0;
14610 if (TREE_CODE (decl) == TREE_BINFO)
14612 /* We're working on the TAG_inheritance for a base class. */
14613 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14615 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14616 aren't at a fixed offset from all (sub)objects of the same
14617 type. We need to extract the appropriate offset from our
14618 vtable. The following dwarf expression means
14620 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14622 This is specific to the V3 ABI, of course. */
14624 dw_loc_descr_ref tmp;
14626 /* Make a copy of the object address. */
14627 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14628 add_loc_descr (&loc_descr, tmp);
14630 /* Extract the vtable address. */
14631 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14632 add_loc_descr (&loc_descr, tmp);
14634 /* Calculate the address of the offset. */
14635 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14636 gcc_assert (offset < 0);
14638 tmp = int_loc_descriptor (-offset);
14639 add_loc_descr (&loc_descr, tmp);
14640 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14641 add_loc_descr (&loc_descr, tmp);
14643 /* Extract the offset. */
14644 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14645 add_loc_descr (&loc_descr, tmp);
14647 /* Add it to the object address. */
14648 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14649 add_loc_descr (&loc_descr, tmp);
14651 else
14652 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14654 else
14655 offset = field_byte_offset (decl);
14657 if (! loc_descr)
14659 if (dwarf_version > 2)
14661 /* Don't need to output a location expression, just the constant. */
14662 if (offset < 0)
14663 add_AT_int (die, DW_AT_data_member_location, offset);
14664 else
14665 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14666 return;
14668 else
14670 enum dwarf_location_atom op;
14672 /* The DWARF2 standard says that we should assume that the structure
14673 address is already on the stack, so we can specify a structure
14674 field address by using DW_OP_plus_uconst. */
14675 op = DW_OP_plus_uconst;
14676 loc_descr = new_loc_descr (op, offset, 0);
14680 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14683 /* Writes integer values to dw_vec_const array. */
14685 static void
14686 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14688 while (size != 0)
14690 *dest++ = val & 0xff;
14691 val >>= 8;
14692 --size;
14696 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14698 static HOST_WIDE_INT
14699 extract_int (const unsigned char *src, unsigned int size)
14701 HOST_WIDE_INT val = 0;
14703 src += size;
14704 while (size != 0)
14706 val <<= 8;
14707 val |= *--src & 0xff;
14708 --size;
14710 return val;
14713 /* Writes double_int values to dw_vec_const array. */
14715 static void
14716 insert_double (double_int val, unsigned char *dest)
14718 unsigned char *p0 = dest;
14719 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14721 if (WORDS_BIG_ENDIAN)
14723 p0 = p1;
14724 p1 = dest;
14727 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14728 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14731 /* Writes floating point values to dw_vec_const array. */
14733 static void
14734 insert_float (const_rtx rtl, unsigned char *array)
14736 REAL_VALUE_TYPE rv;
14737 long val[4];
14738 int i;
14740 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14741 real_to_target (val, &rv, GET_MODE (rtl));
14743 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14744 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14746 insert_int (val[i], 4, array);
14747 array += 4;
14751 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14752 does not have a "location" either in memory or in a register. These
14753 things can arise in GNU C when a constant is passed as an actual parameter
14754 to an inlined function. They can also arise in C++ where declared
14755 constants do not necessarily get memory "homes". */
14757 static bool
14758 add_const_value_attribute (dw_die_ref die, rtx rtl)
14760 switch (GET_CODE (rtl))
14762 case CONST_INT:
14764 HOST_WIDE_INT val = INTVAL (rtl);
14766 if (val < 0)
14767 add_AT_int (die, DW_AT_const_value, val);
14768 else
14769 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14771 return true;
14773 case CONST_DOUBLE:
14774 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14775 floating-point constant. A CONST_DOUBLE is used whenever the
14776 constant requires more than one word in order to be adequately
14777 represented. */
14779 enum machine_mode mode = GET_MODE (rtl);
14781 if (SCALAR_FLOAT_MODE_P (mode))
14783 unsigned int length = GET_MODE_SIZE (mode);
14784 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14786 insert_float (rtl, array);
14787 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14789 else
14790 add_AT_double (die, DW_AT_const_value,
14791 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14793 return true;
14795 case CONST_VECTOR:
14797 enum machine_mode mode = GET_MODE (rtl);
14798 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14799 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14800 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14801 (length * elt_size);
14802 unsigned int i;
14803 unsigned char *p;
14805 switch (GET_MODE_CLASS (mode))
14807 case MODE_VECTOR_INT:
14808 for (i = 0, p = array; i < length; i++, p += elt_size)
14810 rtx elt = CONST_VECTOR_ELT (rtl, i);
14811 double_int val = rtx_to_double_int (elt);
14813 if (elt_size <= sizeof (HOST_WIDE_INT))
14814 insert_int (val.to_shwi (), elt_size, p);
14815 else
14817 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14818 insert_double (val, p);
14821 break;
14823 case MODE_VECTOR_FLOAT:
14824 for (i = 0, p = array; i < length; i++, p += elt_size)
14826 rtx elt = CONST_VECTOR_ELT (rtl, i);
14827 insert_float (elt, p);
14829 break;
14831 default:
14832 gcc_unreachable ();
14835 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14837 return true;
14839 case CONST_STRING:
14840 if (dwarf_version >= 4 || !dwarf_strict)
14842 dw_loc_descr_ref loc_result;
14843 resolve_one_addr (&rtl, NULL);
14844 rtl_addr:
14845 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14846 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14847 add_AT_loc (die, DW_AT_location, loc_result);
14848 vec_safe_push (used_rtx_array, rtl);
14849 return true;
14851 return false;
14853 case CONST:
14854 if (CONSTANT_P (XEXP (rtl, 0)))
14855 return add_const_value_attribute (die, XEXP (rtl, 0));
14856 /* FALLTHROUGH */
14857 case SYMBOL_REF:
14858 if (!const_ok_for_output (rtl))
14859 return false;
14860 case LABEL_REF:
14861 if (dwarf_version >= 4 || !dwarf_strict)
14862 goto rtl_addr;
14863 return false;
14865 case PLUS:
14866 /* In cases where an inlined instance of an inline function is passed
14867 the address of an `auto' variable (which is local to the caller) we
14868 can get a situation where the DECL_RTL of the artificial local
14869 variable (for the inlining) which acts as a stand-in for the
14870 corresponding formal parameter (of the inline function) will look
14871 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14872 exactly a compile-time constant expression, but it isn't the address
14873 of the (artificial) local variable either. Rather, it represents the
14874 *value* which the artificial local variable always has during its
14875 lifetime. We currently have no way to represent such quasi-constant
14876 values in Dwarf, so for now we just punt and generate nothing. */
14877 return false;
14879 case HIGH:
14880 case CONST_FIXED:
14881 return false;
14883 case MEM:
14884 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14885 && MEM_READONLY_P (rtl)
14886 && GET_MODE (rtl) == BLKmode)
14888 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14889 return true;
14891 return false;
14893 default:
14894 /* No other kinds of rtx should be possible here. */
14895 gcc_unreachable ();
14897 return false;
14900 /* Determine whether the evaluation of EXPR references any variables
14901 or functions which aren't otherwise used (and therefore may not be
14902 output). */
14903 static tree
14904 reference_to_unused (tree * tp, int * walk_subtrees,
14905 void * data ATTRIBUTE_UNUSED)
14907 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14908 *walk_subtrees = 0;
14910 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14911 && ! TREE_ASM_WRITTEN (*tp))
14912 return *tp;
14913 /* ??? The C++ FE emits debug information for using decls, so
14914 putting gcc_unreachable here falls over. See PR31899. For now
14915 be conservative. */
14916 else if (!cgraph_global_info_ready
14917 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14918 return *tp;
14919 else if (TREE_CODE (*tp) == VAR_DECL)
14921 struct varpool_node *node = varpool_get_node (*tp);
14922 if (!node || !node->analyzed)
14923 return *tp;
14925 else if (TREE_CODE (*tp) == FUNCTION_DECL
14926 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14928 /* The call graph machinery must have finished analyzing,
14929 optimizing and gimplifying the CU by now.
14930 So if *TP has no call graph node associated
14931 to it, it means *TP will not be emitted. */
14932 if (!cgraph_get_node (*tp))
14933 return *tp;
14935 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14936 return *tp;
14938 return NULL_TREE;
14941 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14942 for use in a later add_const_value_attribute call. */
14944 static rtx
14945 rtl_for_decl_init (tree init, tree type)
14947 rtx rtl = NULL_RTX;
14949 STRIP_NOPS (init);
14951 /* If a variable is initialized with a string constant without embedded
14952 zeros, build CONST_STRING. */
14953 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14955 tree enttype = TREE_TYPE (type);
14956 tree domain = TYPE_DOMAIN (type);
14957 enum machine_mode mode = TYPE_MODE (enttype);
14959 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14960 && domain
14961 && integer_zerop (TYPE_MIN_VALUE (domain))
14962 && compare_tree_int (TYPE_MAX_VALUE (domain),
14963 TREE_STRING_LENGTH (init) - 1) == 0
14964 && ((size_t) TREE_STRING_LENGTH (init)
14965 == strlen (TREE_STRING_POINTER (init)) + 1))
14967 rtl = gen_rtx_CONST_STRING (VOIDmode,
14968 ggc_strdup (TREE_STRING_POINTER (init)));
14969 rtl = gen_rtx_MEM (BLKmode, rtl);
14970 MEM_READONLY_P (rtl) = 1;
14973 /* Other aggregates, and complex values, could be represented using
14974 CONCAT: FIXME! */
14975 else if (AGGREGATE_TYPE_P (type)
14976 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14977 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14978 || TREE_CODE (type) == COMPLEX_TYPE)
14980 /* Vectors only work if their mode is supported by the target.
14981 FIXME: generic vectors ought to work too. */
14982 else if (TREE_CODE (type) == VECTOR_TYPE
14983 && !VECTOR_MODE_P (TYPE_MODE (type)))
14985 /* If the initializer is something that we know will expand into an
14986 immediate RTL constant, expand it now. We must be careful not to
14987 reference variables which won't be output. */
14988 else if (initializer_constant_valid_p (init, type)
14989 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14991 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14992 possible. */
14993 if (TREE_CODE (type) == VECTOR_TYPE)
14994 switch (TREE_CODE (init))
14996 case VECTOR_CST:
14997 break;
14998 case CONSTRUCTOR:
14999 if (TREE_CONSTANT (init))
15001 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15002 bool constant_p = true;
15003 tree value;
15004 unsigned HOST_WIDE_INT ix;
15006 /* Even when ctor is constant, it might contain non-*_CST
15007 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15008 belong into VECTOR_CST nodes. */
15009 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15010 if (!CONSTANT_CLASS_P (value))
15012 constant_p = false;
15013 break;
15016 if (constant_p)
15018 init = build_vector_from_ctor (type, elts);
15019 break;
15022 /* FALLTHRU */
15024 default:
15025 return NULL;
15028 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15030 /* If expand_expr returns a MEM, it wasn't immediate. */
15031 gcc_assert (!rtl || !MEM_P (rtl));
15034 return rtl;
15037 /* Generate RTL for the variable DECL to represent its location. */
15039 static rtx
15040 rtl_for_decl_location (tree decl)
15042 rtx rtl;
15044 /* Here we have to decide where we are going to say the parameter "lives"
15045 (as far as the debugger is concerned). We only have a couple of
15046 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15048 DECL_RTL normally indicates where the parameter lives during most of the
15049 activation of the function. If optimization is enabled however, this
15050 could be either NULL or else a pseudo-reg. Both of those cases indicate
15051 that the parameter doesn't really live anywhere (as far as the code
15052 generation parts of GCC are concerned) during most of the function's
15053 activation. That will happen (for example) if the parameter is never
15054 referenced within the function.
15056 We could just generate a location descriptor here for all non-NULL
15057 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15058 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15059 where DECL_RTL is NULL or is a pseudo-reg.
15061 Note however that we can only get away with using DECL_INCOMING_RTL as
15062 a backup substitute for DECL_RTL in certain limited cases. In cases
15063 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15064 we can be sure that the parameter was passed using the same type as it is
15065 declared to have within the function, and that its DECL_INCOMING_RTL
15066 points us to a place where a value of that type is passed.
15068 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15069 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15070 because in these cases DECL_INCOMING_RTL points us to a value of some
15071 type which is *different* from the type of the parameter itself. Thus,
15072 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15073 such cases, the debugger would end up (for example) trying to fetch a
15074 `float' from a place which actually contains the first part of a
15075 `double'. That would lead to really incorrect and confusing
15076 output at debug-time.
15078 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15079 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15080 are a couple of exceptions however. On little-endian machines we can
15081 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15082 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15083 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15084 when (on a little-endian machine) a non-prototyped function has a
15085 parameter declared to be of type `short' or `char'. In such cases,
15086 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15087 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15088 passed `int' value. If the debugger then uses that address to fetch
15089 a `short' or a `char' (on a little-endian machine) the result will be
15090 the correct data, so we allow for such exceptional cases below.
15092 Note that our goal here is to describe the place where the given formal
15093 parameter lives during most of the function's activation (i.e. between the
15094 end of the prologue and the start of the epilogue). We'll do that as best
15095 as we can. Note however that if the given formal parameter is modified
15096 sometime during the execution of the function, then a stack backtrace (at
15097 debug-time) will show the function as having been called with the *new*
15098 value rather than the value which was originally passed in. This happens
15099 rarely enough that it is not a major problem, but it *is* a problem, and
15100 I'd like to fix it.
15102 A future version of dwarf2out.c may generate two additional attributes for
15103 any given DW_TAG_formal_parameter DIE which will describe the "passed
15104 type" and the "passed location" for the given formal parameter in addition
15105 to the attributes we now generate to indicate the "declared type" and the
15106 "active location" for each parameter. This additional set of attributes
15107 could be used by debuggers for stack backtraces. Separately, note that
15108 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15109 This happens (for example) for inlined-instances of inline function formal
15110 parameters which are never referenced. This really shouldn't be
15111 happening. All PARM_DECL nodes should get valid non-NULL
15112 DECL_INCOMING_RTL values. FIXME. */
15114 /* Use DECL_RTL as the "location" unless we find something better. */
15115 rtl = DECL_RTL_IF_SET (decl);
15117 /* When generating abstract instances, ignore everything except
15118 constants, symbols living in memory, and symbols living in
15119 fixed registers. */
15120 if (! reload_completed)
15122 if (rtl
15123 && (CONSTANT_P (rtl)
15124 || (MEM_P (rtl)
15125 && CONSTANT_P (XEXP (rtl, 0)))
15126 || (REG_P (rtl)
15127 && TREE_CODE (decl) == VAR_DECL
15128 && TREE_STATIC (decl))))
15130 rtl = targetm.delegitimize_address (rtl);
15131 return rtl;
15133 rtl = NULL_RTX;
15135 else if (TREE_CODE (decl) == PARM_DECL)
15137 if (rtl == NULL_RTX
15138 || is_pseudo_reg (rtl)
15139 || (MEM_P (rtl)
15140 && is_pseudo_reg (XEXP (rtl, 0))
15141 && DECL_INCOMING_RTL (decl)
15142 && MEM_P (DECL_INCOMING_RTL (decl))
15143 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15145 tree declared_type = TREE_TYPE (decl);
15146 tree passed_type = DECL_ARG_TYPE (decl);
15147 enum machine_mode dmode = TYPE_MODE (declared_type);
15148 enum machine_mode pmode = TYPE_MODE (passed_type);
15150 /* This decl represents a formal parameter which was optimized out.
15151 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15152 all cases where (rtl == NULL_RTX) just below. */
15153 if (dmode == pmode)
15154 rtl = DECL_INCOMING_RTL (decl);
15155 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15156 && SCALAR_INT_MODE_P (dmode)
15157 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15158 && DECL_INCOMING_RTL (decl))
15160 rtx inc = DECL_INCOMING_RTL (decl);
15161 if (REG_P (inc))
15162 rtl = inc;
15163 else if (MEM_P (inc))
15165 if (BYTES_BIG_ENDIAN)
15166 rtl = adjust_address_nv (inc, dmode,
15167 GET_MODE_SIZE (pmode)
15168 - GET_MODE_SIZE (dmode));
15169 else
15170 rtl = inc;
15175 /* If the parm was passed in registers, but lives on the stack, then
15176 make a big endian correction if the mode of the type of the
15177 parameter is not the same as the mode of the rtl. */
15178 /* ??? This is the same series of checks that are made in dbxout.c before
15179 we reach the big endian correction code there. It isn't clear if all
15180 of these checks are necessary here, but keeping them all is the safe
15181 thing to do. */
15182 else if (MEM_P (rtl)
15183 && XEXP (rtl, 0) != const0_rtx
15184 && ! CONSTANT_P (XEXP (rtl, 0))
15185 /* Not passed in memory. */
15186 && !MEM_P (DECL_INCOMING_RTL (decl))
15187 /* Not passed by invisible reference. */
15188 && (!REG_P (XEXP (rtl, 0))
15189 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15190 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15191 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15192 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15193 #endif
15195 /* Big endian correction check. */
15196 && BYTES_BIG_ENDIAN
15197 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15198 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15199 < UNITS_PER_WORD))
15201 enum machine_mode addr_mode = get_address_mode (rtl);
15202 int offset = (UNITS_PER_WORD
15203 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15205 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15206 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15209 else if (TREE_CODE (decl) == VAR_DECL
15210 && rtl
15211 && MEM_P (rtl)
15212 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15213 && BYTES_BIG_ENDIAN)
15215 enum machine_mode addr_mode = get_address_mode (rtl);
15216 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15217 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15219 /* If a variable is declared "register" yet is smaller than
15220 a register, then if we store the variable to memory, it
15221 looks like we're storing a register-sized value, when in
15222 fact we are not. We need to adjust the offset of the
15223 storage location to reflect the actual value's bytes,
15224 else gdb will not be able to display it. */
15225 if (rsize > dsize)
15226 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15227 plus_constant (addr_mode, XEXP (rtl, 0),
15228 rsize - dsize));
15231 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15232 and will have been substituted directly into all expressions that use it.
15233 C does not have such a concept, but C++ and other languages do. */
15234 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15235 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15237 if (rtl)
15238 rtl = targetm.delegitimize_address (rtl);
15240 /* If we don't look past the constant pool, we risk emitting a
15241 reference to a constant pool entry that isn't referenced from
15242 code, and thus is not emitted. */
15243 if (rtl)
15244 rtl = avoid_constant_pool_reference (rtl);
15246 /* Try harder to get a rtl. If this symbol ends up not being emitted
15247 in the current CU, resolve_addr will remove the expression referencing
15248 it. */
15249 if (rtl == NULL_RTX
15250 && TREE_CODE (decl) == VAR_DECL
15251 && !DECL_EXTERNAL (decl)
15252 && TREE_STATIC (decl)
15253 && DECL_NAME (decl)
15254 && !DECL_HARD_REGISTER (decl)
15255 && DECL_MODE (decl) != VOIDmode)
15257 rtl = make_decl_rtl_for_debug (decl);
15258 if (!MEM_P (rtl)
15259 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15260 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15261 rtl = NULL_RTX;
15264 return rtl;
15267 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15268 returned. If so, the decl for the COMMON block is returned, and the
15269 value is the offset into the common block for the symbol. */
15271 static tree
15272 fortran_common (tree decl, HOST_WIDE_INT *value)
15274 tree val_expr, cvar;
15275 enum machine_mode mode;
15276 HOST_WIDE_INT bitsize, bitpos;
15277 tree offset;
15278 int unsignedp, volatilep = 0;
15280 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15281 it does not have a value (the offset into the common area), or if it
15282 is thread local (as opposed to global) then it isn't common, and shouldn't
15283 be handled as such. */
15284 if (TREE_CODE (decl) != VAR_DECL
15285 || !TREE_STATIC (decl)
15286 || !DECL_HAS_VALUE_EXPR_P (decl)
15287 || !is_fortran ())
15288 return NULL_TREE;
15290 val_expr = DECL_VALUE_EXPR (decl);
15291 if (TREE_CODE (val_expr) != COMPONENT_REF)
15292 return NULL_TREE;
15294 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15295 &mode, &unsignedp, &volatilep, true);
15297 if (cvar == NULL_TREE
15298 || TREE_CODE (cvar) != VAR_DECL
15299 || DECL_ARTIFICIAL (cvar)
15300 || !TREE_PUBLIC (cvar))
15301 return NULL_TREE;
15303 *value = 0;
15304 if (offset != NULL)
15306 if (!host_integerp (offset, 0))
15307 return NULL_TREE;
15308 *value = tree_low_cst (offset, 0);
15310 if (bitpos != 0)
15311 *value += bitpos / BITS_PER_UNIT;
15313 return cvar;
15316 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15317 data attribute for a variable or a parameter. We generate the
15318 DW_AT_const_value attribute only in those cases where the given variable
15319 or parameter does not have a true "location" either in memory or in a
15320 register. This can happen (for example) when a constant is passed as an
15321 actual argument in a call to an inline function. (It's possible that
15322 these things can crop up in other ways also.) Note that one type of
15323 constant value which can be passed into an inlined function is a constant
15324 pointer. This can happen for example if an actual argument in an inlined
15325 function call evaluates to a compile-time constant address.
15327 CACHE_P is true if it is worth caching the location list for DECL,
15328 so that future calls can reuse it rather than regenerate it from scratch.
15329 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15330 since we will need to refer to them each time the function is inlined. */
15332 static bool
15333 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15334 enum dwarf_attribute attr)
15336 rtx rtl;
15337 dw_loc_list_ref list;
15338 var_loc_list *loc_list;
15339 cached_dw_loc_list *cache;
15340 void **slot;
15342 if (TREE_CODE (decl) == ERROR_MARK)
15343 return false;
15345 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15346 || TREE_CODE (decl) == RESULT_DECL);
15348 /* Try to get some constant RTL for this decl, and use that as the value of
15349 the location. */
15351 rtl = rtl_for_decl_location (decl);
15352 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15353 && add_const_value_attribute (die, rtl))
15354 return true;
15356 /* See if we have single element location list that is equivalent to
15357 a constant value. That way we are better to use add_const_value_attribute
15358 rather than expanding constant value equivalent. */
15359 loc_list = lookup_decl_loc (decl);
15360 if (loc_list
15361 && loc_list->first
15362 && loc_list->first->next == NULL
15363 && NOTE_P (loc_list->first->loc)
15364 && NOTE_VAR_LOCATION (loc_list->first->loc)
15365 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15367 struct var_loc_node *node;
15369 node = loc_list->first;
15370 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15371 if (GET_CODE (rtl) == EXPR_LIST)
15372 rtl = XEXP (rtl, 0);
15373 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15374 && add_const_value_attribute (die, rtl))
15375 return true;
15377 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15378 list several times. See if we've already cached the contents. */
15379 list = NULL;
15380 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15381 cache_p = false;
15382 if (cache_p)
15384 cache = (cached_dw_loc_list *)
15385 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15386 if (cache)
15387 list = cache->loc_list;
15389 if (list == NULL)
15391 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15392 /* It is usually worth caching this result if the decl is from
15393 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15394 if (cache_p && list && list->dw_loc_next)
15396 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15397 DECL_UID (decl), INSERT);
15398 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15399 cache->decl_id = DECL_UID (decl);
15400 cache->loc_list = list;
15401 *slot = cache;
15404 if (list)
15406 add_AT_location_description (die, attr, list);
15407 return true;
15409 /* None of that worked, so it must not really have a location;
15410 try adding a constant value attribute from the DECL_INITIAL. */
15411 return tree_add_const_value_attribute_for_decl (die, decl);
15414 /* Add VARIABLE and DIE into deferred locations list. */
15416 static void
15417 defer_location (tree variable, dw_die_ref die)
15419 deferred_locations entry;
15420 entry.variable = variable;
15421 entry.die = die;
15422 vec_safe_push (deferred_locations_list, entry);
15425 /* Helper function for tree_add_const_value_attribute. Natively encode
15426 initializer INIT into an array. Return true if successful. */
15428 static bool
15429 native_encode_initializer (tree init, unsigned char *array, int size)
15431 tree type;
15433 if (init == NULL_TREE)
15434 return false;
15436 STRIP_NOPS (init);
15437 switch (TREE_CODE (init))
15439 case STRING_CST:
15440 type = TREE_TYPE (init);
15441 if (TREE_CODE (type) == ARRAY_TYPE)
15443 tree enttype = TREE_TYPE (type);
15444 enum machine_mode mode = TYPE_MODE (enttype);
15446 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15447 return false;
15448 if (int_size_in_bytes (type) != size)
15449 return false;
15450 if (size > TREE_STRING_LENGTH (init))
15452 memcpy (array, TREE_STRING_POINTER (init),
15453 TREE_STRING_LENGTH (init));
15454 memset (array + TREE_STRING_LENGTH (init),
15455 '\0', size - TREE_STRING_LENGTH (init));
15457 else
15458 memcpy (array, TREE_STRING_POINTER (init), size);
15459 return true;
15461 return false;
15462 case CONSTRUCTOR:
15463 type = TREE_TYPE (init);
15464 if (int_size_in_bytes (type) != size)
15465 return false;
15466 if (TREE_CODE (type) == ARRAY_TYPE)
15468 HOST_WIDE_INT min_index;
15469 unsigned HOST_WIDE_INT cnt;
15470 int curpos = 0, fieldsize;
15471 constructor_elt *ce;
15473 if (TYPE_DOMAIN (type) == NULL_TREE
15474 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15475 return false;
15477 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15478 if (fieldsize <= 0)
15479 return false;
15481 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15482 memset (array, '\0', size);
15483 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15485 tree val = ce->value;
15486 tree index = ce->index;
15487 int pos = curpos;
15488 if (index && TREE_CODE (index) == RANGE_EXPR)
15489 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15490 * fieldsize;
15491 else if (index)
15492 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15494 if (val)
15496 STRIP_NOPS (val);
15497 if (!native_encode_initializer (val, array + pos, fieldsize))
15498 return false;
15500 curpos = pos + fieldsize;
15501 if (index && TREE_CODE (index) == RANGE_EXPR)
15503 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15504 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15505 while (count-- > 0)
15507 if (val)
15508 memcpy (array + curpos, array + pos, fieldsize);
15509 curpos += fieldsize;
15512 gcc_assert (curpos <= size);
15514 return true;
15516 else if (TREE_CODE (type) == RECORD_TYPE
15517 || TREE_CODE (type) == UNION_TYPE)
15519 tree field = NULL_TREE;
15520 unsigned HOST_WIDE_INT cnt;
15521 constructor_elt *ce;
15523 if (int_size_in_bytes (type) != size)
15524 return false;
15526 if (TREE_CODE (type) == RECORD_TYPE)
15527 field = TYPE_FIELDS (type);
15529 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15531 tree val = ce->value;
15532 int pos, fieldsize;
15534 if (ce->index != 0)
15535 field = ce->index;
15537 if (val)
15538 STRIP_NOPS (val);
15540 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15541 return false;
15543 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15544 && TYPE_DOMAIN (TREE_TYPE (field))
15545 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15546 return false;
15547 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15548 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15549 return false;
15550 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15551 pos = int_byte_position (field);
15552 gcc_assert (pos + fieldsize <= size);
15553 if (val
15554 && !native_encode_initializer (val, array + pos, fieldsize))
15555 return false;
15557 return true;
15559 return false;
15560 case VIEW_CONVERT_EXPR:
15561 case NON_LVALUE_EXPR:
15562 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15563 default:
15564 return native_encode_expr (init, array, size) == size;
15568 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15569 attribute is the const value T. */
15571 static bool
15572 tree_add_const_value_attribute (dw_die_ref die, tree t)
15574 tree init;
15575 tree type = TREE_TYPE (t);
15576 rtx rtl;
15578 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15579 return false;
15581 init = t;
15582 gcc_assert (!DECL_P (init));
15584 rtl = rtl_for_decl_init (init, type);
15585 if (rtl)
15586 return add_const_value_attribute (die, rtl);
15587 /* If the host and target are sane, try harder. */
15588 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15589 && initializer_constant_valid_p (init, type))
15591 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15592 if (size > 0 && (int) size == size)
15594 unsigned char *array = (unsigned char *)
15595 ggc_alloc_cleared_atomic (size);
15597 if (native_encode_initializer (init, array, size))
15599 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15600 return true;
15602 ggc_free (array);
15605 return false;
15608 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15609 attribute is the const value of T, where T is an integral constant
15610 variable with static storage duration
15611 (so it can't be a PARM_DECL or a RESULT_DECL). */
15613 static bool
15614 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15617 if (!decl
15618 || (TREE_CODE (decl) != VAR_DECL
15619 && TREE_CODE (decl) != CONST_DECL)
15620 || (TREE_CODE (decl) == VAR_DECL
15621 && !TREE_STATIC (decl)))
15622 return false;
15624 if (TREE_READONLY (decl)
15625 && ! TREE_THIS_VOLATILE (decl)
15626 && DECL_INITIAL (decl))
15627 /* OK */;
15628 else
15629 return false;
15631 /* Don't add DW_AT_const_value if abstract origin already has one. */
15632 if (get_AT (var_die, DW_AT_const_value))
15633 return false;
15635 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15638 /* Convert the CFI instructions for the current function into a
15639 location list. This is used for DW_AT_frame_base when we targeting
15640 a dwarf2 consumer that does not support the dwarf3
15641 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15642 expressions. */
15644 static dw_loc_list_ref
15645 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15647 int ix;
15648 dw_fde_ref fde;
15649 dw_loc_list_ref list, *list_tail;
15650 dw_cfi_ref cfi;
15651 dw_cfa_location last_cfa, next_cfa;
15652 const char *start_label, *last_label, *section;
15653 dw_cfa_location remember;
15655 fde = cfun->fde;
15656 gcc_assert (fde != NULL);
15658 section = secname_for_decl (current_function_decl);
15659 list_tail = &list;
15660 list = NULL;
15662 memset (&next_cfa, 0, sizeof (next_cfa));
15663 next_cfa.reg = INVALID_REGNUM;
15664 remember = next_cfa;
15666 start_label = fde->dw_fde_begin;
15668 /* ??? Bald assumption that the CIE opcode list does not contain
15669 advance opcodes. */
15670 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15671 lookup_cfa_1 (cfi, &next_cfa, &remember);
15673 last_cfa = next_cfa;
15674 last_label = start_label;
15676 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15678 /* If the first partition contained no CFI adjustments, the
15679 CIE opcodes apply to the whole first partition. */
15680 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15681 fde->dw_fde_begin, fde->dw_fde_end, section);
15682 list_tail =&(*list_tail)->dw_loc_next;
15683 start_label = last_label = fde->dw_fde_second_begin;
15686 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15688 switch (cfi->dw_cfi_opc)
15690 case DW_CFA_set_loc:
15691 case DW_CFA_advance_loc1:
15692 case DW_CFA_advance_loc2:
15693 case DW_CFA_advance_loc4:
15694 if (!cfa_equal_p (&last_cfa, &next_cfa))
15696 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15697 start_label, last_label, section);
15699 list_tail = &(*list_tail)->dw_loc_next;
15700 last_cfa = next_cfa;
15701 start_label = last_label;
15703 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15704 break;
15706 case DW_CFA_advance_loc:
15707 /* The encoding is complex enough that we should never emit this. */
15708 gcc_unreachable ();
15710 default:
15711 lookup_cfa_1 (cfi, &next_cfa, &remember);
15712 break;
15714 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15716 if (!cfa_equal_p (&last_cfa, &next_cfa))
15718 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15719 start_label, last_label, section);
15721 list_tail = &(*list_tail)->dw_loc_next;
15722 last_cfa = next_cfa;
15723 start_label = last_label;
15725 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15726 start_label, fde->dw_fde_end, section);
15727 list_tail = &(*list_tail)->dw_loc_next;
15728 start_label = last_label = fde->dw_fde_second_begin;
15732 if (!cfa_equal_p (&last_cfa, &next_cfa))
15734 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15735 start_label, last_label, section);
15736 list_tail = &(*list_tail)->dw_loc_next;
15737 start_label = last_label;
15740 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15741 start_label,
15742 fde->dw_fde_second_begin
15743 ? fde->dw_fde_second_end : fde->dw_fde_end,
15744 section);
15746 if (list && list->dw_loc_next)
15747 gen_llsym (list);
15749 return list;
15752 /* Compute a displacement from the "steady-state frame pointer" to the
15753 frame base (often the same as the CFA), and store it in
15754 frame_pointer_fb_offset. OFFSET is added to the displacement
15755 before the latter is negated. */
15757 static void
15758 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15760 rtx reg, elim;
15762 #ifdef FRAME_POINTER_CFA_OFFSET
15763 reg = frame_pointer_rtx;
15764 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15765 #else
15766 reg = arg_pointer_rtx;
15767 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15768 #endif
15770 elim = (ira_use_lra_p
15771 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15772 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15773 if (GET_CODE (elim) == PLUS)
15775 offset += INTVAL (XEXP (elim, 1));
15776 elim = XEXP (elim, 0);
15779 frame_pointer_fb_offset = -offset;
15781 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15782 in which to eliminate. This is because it's stack pointer isn't
15783 directly accessible as a register within the ISA. To work around
15784 this, assume that while we cannot provide a proper value for
15785 frame_pointer_fb_offset, we won't need one either. */
15786 frame_pointer_fb_offset_valid
15787 = ((SUPPORTS_STACK_ALIGNMENT
15788 && (elim == hard_frame_pointer_rtx
15789 || elim == stack_pointer_rtx))
15790 || elim == (frame_pointer_needed
15791 ? hard_frame_pointer_rtx
15792 : stack_pointer_rtx));
15795 /* Generate a DW_AT_name attribute given some string value to be included as
15796 the value of the attribute. */
15798 static void
15799 add_name_attribute (dw_die_ref die, const char *name_string)
15801 if (name_string != NULL && *name_string != 0)
15803 if (demangle_name_func)
15804 name_string = (*demangle_name_func) (name_string);
15806 add_AT_string (die, DW_AT_name, name_string);
15810 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15811 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15812 of TYPE accordingly.
15814 ??? This is a temporary measure until after we're able to generate
15815 regular DWARF for the complex Ada type system. */
15817 static void
15818 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15819 dw_die_ref context_die)
15821 tree dtype;
15822 dw_die_ref dtype_die;
15824 if (!lang_hooks.types.descriptive_type)
15825 return;
15827 dtype = lang_hooks.types.descriptive_type (type);
15828 if (!dtype)
15829 return;
15831 dtype_die = lookup_type_die (dtype);
15832 if (!dtype_die)
15834 gen_type_die (dtype, context_die);
15835 dtype_die = lookup_type_die (dtype);
15836 gcc_assert (dtype_die);
15839 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15842 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
15844 static const char *
15845 comp_dir_string (void)
15847 const char *wd;
15848 char *wd1;
15849 static const char *cached_wd = NULL;
15851 if (cached_wd != NULL)
15852 return cached_wd;
15854 wd = get_src_pwd ();
15855 if (wd == NULL)
15856 return NULL;
15858 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15860 int wdlen;
15862 wdlen = strlen (wd);
15863 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15864 strcpy (wd1, wd);
15865 wd1 [wdlen] = DIR_SEPARATOR;
15866 wd1 [wdlen + 1] = 0;
15867 wd = wd1;
15870 cached_wd = remap_debug_filename (wd);
15871 return cached_wd;
15874 /* Generate a DW_AT_comp_dir attribute for DIE. */
15876 static void
15877 add_comp_dir_attribute (dw_die_ref die)
15879 const char * wd = comp_dir_string ();
15880 if (wd != NULL)
15881 add_AT_string (die, DW_AT_comp_dir, wd);
15884 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15885 default. */
15887 static int
15888 lower_bound_default (void)
15890 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15892 case DW_LANG_C:
15893 case DW_LANG_C89:
15894 case DW_LANG_C99:
15895 case DW_LANG_C_plus_plus:
15896 case DW_LANG_ObjC:
15897 case DW_LANG_ObjC_plus_plus:
15898 case DW_LANG_Java:
15899 return 0;
15900 case DW_LANG_Fortran77:
15901 case DW_LANG_Fortran90:
15902 case DW_LANG_Fortran95:
15903 return 1;
15904 case DW_LANG_UPC:
15905 case DW_LANG_D:
15906 case DW_LANG_Python:
15907 return dwarf_version >= 4 ? 0 : -1;
15908 case DW_LANG_Ada95:
15909 case DW_LANG_Ada83:
15910 case DW_LANG_Cobol74:
15911 case DW_LANG_Cobol85:
15912 case DW_LANG_Pascal83:
15913 case DW_LANG_Modula2:
15914 case DW_LANG_PLI:
15915 return dwarf_version >= 4 ? 1 : -1;
15916 default:
15917 return -1;
15921 /* Given a tree node describing an array bound (either lower or upper) output
15922 a representation for that bound. */
15924 static void
15925 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15927 switch (TREE_CODE (bound))
15929 case ERROR_MARK:
15930 return;
15932 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15933 case INTEGER_CST:
15935 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15936 int dflt;
15938 /* Use the default if possible. */
15939 if (bound_attr == DW_AT_lower_bound
15940 && host_integerp (bound, 0)
15941 && (dflt = lower_bound_default ()) != -1
15942 && tree_low_cst (bound, 0) == dflt)
15945 /* Otherwise represent the bound as an unsigned value with the
15946 precision of its type. The precision and signedness of the
15947 type will be necessary to re-interpret it unambiguously. */
15948 else if (prec < HOST_BITS_PER_WIDE_INT)
15950 unsigned HOST_WIDE_INT mask
15951 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15952 add_AT_unsigned (subrange_die, bound_attr,
15953 TREE_INT_CST_LOW (bound) & mask);
15955 else if (prec == HOST_BITS_PER_WIDE_INT
15956 || TREE_INT_CST_HIGH (bound) == 0)
15957 add_AT_unsigned (subrange_die, bound_attr,
15958 TREE_INT_CST_LOW (bound));
15959 else
15960 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15961 TREE_INT_CST_LOW (bound));
15963 break;
15965 CASE_CONVERT:
15966 case VIEW_CONVERT_EXPR:
15967 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15968 break;
15970 case SAVE_EXPR:
15971 break;
15973 case VAR_DECL:
15974 case PARM_DECL:
15975 case RESULT_DECL:
15977 dw_die_ref decl_die = lookup_decl_die (bound);
15979 /* ??? Can this happen, or should the variable have been bound
15980 first? Probably it can, since I imagine that we try to create
15981 the types of parameters in the order in which they exist in
15982 the list, and won't have created a forward reference to a
15983 later parameter. */
15984 if (decl_die != NULL)
15986 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15987 break;
15990 /* FALLTHRU */
15992 default:
15994 /* Otherwise try to create a stack operation procedure to
15995 evaluate the value of the array bound. */
15997 dw_die_ref ctx, decl_die;
15998 dw_loc_list_ref list;
16000 list = loc_list_from_tree (bound, 2);
16001 if (list == NULL || single_element_loc_list_p (list))
16003 /* If DW_AT_*bound is not a reference nor constant, it is
16004 a DWARF expression rather than location description.
16005 For that loc_list_from_tree (bound, 0) is needed.
16006 If that fails to give a single element list,
16007 fall back to outputting this as a reference anyway. */
16008 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16009 if (list2 && single_element_loc_list_p (list2))
16011 add_AT_loc (subrange_die, bound_attr, list2->expr);
16012 break;
16015 if (list == NULL)
16016 break;
16018 if (current_function_decl == 0)
16019 ctx = comp_unit_die ();
16020 else
16021 ctx = lookup_decl_die (current_function_decl);
16023 decl_die = new_die (DW_TAG_variable, ctx, bound);
16024 add_AT_flag (decl_die, DW_AT_artificial, 1);
16025 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16026 add_AT_location_description (decl_die, DW_AT_location, list);
16027 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16028 break;
16033 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16034 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16035 Note that the block of subscript information for an array type also
16036 includes information about the element type of the given array type. */
16038 static void
16039 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16041 unsigned dimension_number;
16042 tree lower, upper;
16043 dw_die_ref subrange_die;
16045 for (dimension_number = 0;
16046 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16047 type = TREE_TYPE (type), dimension_number++)
16049 tree domain = TYPE_DOMAIN (type);
16051 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16052 break;
16054 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16055 and (in GNU C only) variable bounds. Handle all three forms
16056 here. */
16057 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16058 if (domain)
16060 /* We have an array type with specified bounds. */
16061 lower = TYPE_MIN_VALUE (domain);
16062 upper = TYPE_MAX_VALUE (domain);
16064 /* Define the index type. */
16065 if (TREE_TYPE (domain))
16067 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16068 TREE_TYPE field. We can't emit debug info for this
16069 because it is an unnamed integral type. */
16070 if (TREE_CODE (domain) == INTEGER_TYPE
16071 && TYPE_NAME (domain) == NULL_TREE
16072 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16073 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16075 else
16076 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16077 type_die);
16080 /* ??? If upper is NULL, the array has unspecified length,
16081 but it does have a lower bound. This happens with Fortran
16082 dimension arr(N:*)
16083 Since the debugger is definitely going to need to know N
16084 to produce useful results, go ahead and output the lower
16085 bound solo, and hope the debugger can cope. */
16087 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16088 if (upper)
16089 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16092 /* Otherwise we have an array type with an unspecified length. The
16093 DWARF-2 spec does not say how to handle this; let's just leave out the
16094 bounds. */
16098 static void
16099 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16101 dw_die_ref decl_die;
16102 unsigned size;
16104 switch (TREE_CODE (tree_node))
16106 case ERROR_MARK:
16107 size = 0;
16108 break;
16109 case ENUMERAL_TYPE:
16110 case RECORD_TYPE:
16111 case UNION_TYPE:
16112 case QUAL_UNION_TYPE:
16113 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16114 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16116 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16117 return;
16119 size = int_size_in_bytes (tree_node);
16120 break;
16121 case FIELD_DECL:
16122 /* For a data member of a struct or union, the DW_AT_byte_size is
16123 generally given as the number of bytes normally allocated for an
16124 object of the *declared* type of the member itself. This is true
16125 even for bit-fields. */
16126 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16127 break;
16128 default:
16129 gcc_unreachable ();
16132 /* Note that `size' might be -1 when we get to this point. If it is, that
16133 indicates that the byte size of the entity in question is variable. We
16134 have no good way of expressing this fact in Dwarf at the present time,
16135 so just let the -1 pass on through. */
16136 add_AT_unsigned (die, DW_AT_byte_size, size);
16139 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16140 which specifies the distance in bits from the highest order bit of the
16141 "containing object" for the bit-field to the highest order bit of the
16142 bit-field itself.
16144 For any given bit-field, the "containing object" is a hypothetical object
16145 (of some integral or enum type) within which the given bit-field lives. The
16146 type of this hypothetical "containing object" is always the same as the
16147 declared type of the individual bit-field itself. The determination of the
16148 exact location of the "containing object" for a bit-field is rather
16149 complicated. It's handled by the `field_byte_offset' function (above).
16151 Note that it is the size (in bytes) of the hypothetical "containing object"
16152 which will be given in the DW_AT_byte_size attribute for this bit-field.
16153 (See `byte_size_attribute' above). */
16155 static inline void
16156 add_bit_offset_attribute (dw_die_ref die, tree decl)
16158 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16159 tree type = DECL_BIT_FIELD_TYPE (decl);
16160 HOST_WIDE_INT bitpos_int;
16161 HOST_WIDE_INT highest_order_object_bit_offset;
16162 HOST_WIDE_INT highest_order_field_bit_offset;
16163 HOST_WIDE_INT bit_offset;
16165 /* Must be a field and a bit field. */
16166 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16168 /* We can't yet handle bit-fields whose offsets are variable, so if we
16169 encounter such things, just return without generating any attribute
16170 whatsoever. Likewise for variable or too large size. */
16171 if (! host_integerp (bit_position (decl), 0)
16172 || ! host_integerp (DECL_SIZE (decl), 1))
16173 return;
16175 bitpos_int = int_bit_position (decl);
16177 /* Note that the bit offset is always the distance (in bits) from the
16178 highest-order bit of the "containing object" to the highest-order bit of
16179 the bit-field itself. Since the "high-order end" of any object or field
16180 is different on big-endian and little-endian machines, the computation
16181 below must take account of these differences. */
16182 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16183 highest_order_field_bit_offset = bitpos_int;
16185 if (! BYTES_BIG_ENDIAN)
16187 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16188 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16191 bit_offset
16192 = (! BYTES_BIG_ENDIAN
16193 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16194 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16196 if (bit_offset < 0)
16197 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16198 else
16199 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16202 /* For a FIELD_DECL node which represents a bit field, output an attribute
16203 which specifies the length in bits of the given field. */
16205 static inline void
16206 add_bit_size_attribute (dw_die_ref die, tree decl)
16208 /* Must be a field and a bit field. */
16209 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16210 && DECL_BIT_FIELD_TYPE (decl));
16212 if (host_integerp (DECL_SIZE (decl), 1))
16213 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16216 /* If the compiled language is ANSI C, then add a 'prototyped'
16217 attribute, if arg types are given for the parameters of a function. */
16219 static inline void
16220 add_prototyped_attribute (dw_die_ref die, tree func_type)
16222 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16223 && prototype_p (func_type))
16224 add_AT_flag (die, DW_AT_prototyped, 1);
16227 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16228 by looking in either the type declaration or object declaration
16229 equate table. */
16231 static inline dw_die_ref
16232 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16234 dw_die_ref origin_die = NULL;
16236 if (TREE_CODE (origin) != FUNCTION_DECL)
16238 /* We may have gotten separated from the block for the inlined
16239 function, if we're in an exception handler or some such; make
16240 sure that the abstract function has been written out.
16242 Doing this for nested functions is wrong, however; functions are
16243 distinct units, and our context might not even be inline. */
16244 tree fn = origin;
16246 if (TYPE_P (fn))
16247 fn = TYPE_STUB_DECL (fn);
16249 fn = decl_function_context (fn);
16250 if (fn)
16251 dwarf2out_abstract_function (fn);
16254 if (DECL_P (origin))
16255 origin_die = lookup_decl_die (origin);
16256 else if (TYPE_P (origin))
16257 origin_die = lookup_type_die (origin);
16259 /* XXX: Functions that are never lowered don't always have correct block
16260 trees (in the case of java, they simply have no block tree, in some other
16261 languages). For these functions, there is nothing we can really do to
16262 output correct debug info for inlined functions in all cases. Rather
16263 than die, we'll just produce deficient debug info now, in that we will
16264 have variables without a proper abstract origin. In the future, when all
16265 functions are lowered, we should re-add a gcc_assert (origin_die)
16266 here. */
16268 if (origin_die)
16269 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16270 return origin_die;
16273 /* We do not currently support the pure_virtual attribute. */
16275 static inline void
16276 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16278 if (DECL_VINDEX (func_decl))
16280 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16282 if (host_integerp (DECL_VINDEX (func_decl), 0))
16283 add_AT_loc (die, DW_AT_vtable_elem_location,
16284 new_loc_descr (DW_OP_constu,
16285 tree_low_cst (DECL_VINDEX (func_decl), 0),
16286 0));
16288 /* GNU extension: Record what type this method came from originally. */
16289 if (debug_info_level > DINFO_LEVEL_TERSE
16290 && DECL_CONTEXT (func_decl))
16291 add_AT_die_ref (die, DW_AT_containing_type,
16292 lookup_type_die (DECL_CONTEXT (func_decl)));
16296 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16297 given decl. This used to be a vendor extension until after DWARF 4
16298 standardized it. */
16300 static void
16301 add_linkage_attr (dw_die_ref die, tree decl)
16303 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16305 /* Mimic what assemble_name_raw does with a leading '*'. */
16306 if (name[0] == '*')
16307 name = &name[1];
16309 if (dwarf_version >= 4)
16310 add_AT_string (die, DW_AT_linkage_name, name);
16311 else
16312 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16315 /* Add source coordinate attributes for the given decl. */
16317 static void
16318 add_src_coords_attributes (dw_die_ref die, tree decl)
16320 expanded_location s;
16322 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16323 return;
16324 s = expand_location (DECL_SOURCE_LOCATION (decl));
16325 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16326 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16329 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16331 static void
16332 add_linkage_name (dw_die_ref die, tree decl)
16334 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16335 && TREE_PUBLIC (decl)
16336 && !DECL_ABSTRACT (decl)
16337 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16338 && die->die_tag != DW_TAG_member)
16340 /* Defer until we have an assembler name set. */
16341 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16343 limbo_die_node *asm_name;
16345 asm_name = ggc_alloc_cleared_limbo_die_node ();
16346 asm_name->die = die;
16347 asm_name->created_for = decl;
16348 asm_name->next = deferred_asm_name;
16349 deferred_asm_name = asm_name;
16351 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16352 add_linkage_attr (die, decl);
16356 /* Add a DW_AT_name attribute and source coordinate attribute for the
16357 given decl, but only if it actually has a name. */
16359 static void
16360 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16362 tree decl_name;
16364 decl_name = DECL_NAME (decl);
16365 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16367 const char *name = dwarf2_name (decl, 0);
16368 if (name)
16369 add_name_attribute (die, name);
16370 if (! DECL_ARTIFICIAL (decl))
16371 add_src_coords_attributes (die, decl);
16373 add_linkage_name (die, decl);
16376 #ifdef VMS_DEBUGGING_INFO
16377 /* Get the function's name, as described by its RTL. This may be different
16378 from the DECL_NAME name used in the source file. */
16379 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16381 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16382 XEXP (DECL_RTL (decl), 0), false);
16383 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16385 #endif /* VMS_DEBUGGING_INFO */
16388 #ifdef VMS_DEBUGGING_INFO
16389 /* Output the debug main pointer die for VMS */
16391 void
16392 dwarf2out_vms_debug_main_pointer (void)
16394 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16395 dw_die_ref die;
16397 /* Allocate the VMS debug main subprogram die. */
16398 die = ggc_alloc_cleared_die_node ();
16399 die->die_tag = DW_TAG_subprogram;
16400 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16401 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16402 current_function_funcdef_no);
16403 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16405 /* Make it the first child of comp_unit_die (). */
16406 die->die_parent = comp_unit_die ();
16407 if (comp_unit_die ()->die_child)
16409 die->die_sib = comp_unit_die ()->die_child->die_sib;
16410 comp_unit_die ()->die_child->die_sib = die;
16412 else
16414 die->die_sib = die;
16415 comp_unit_die ()->die_child = die;
16418 #endif /* VMS_DEBUGGING_INFO */
16420 /* Push a new declaration scope. */
16422 static void
16423 push_decl_scope (tree scope)
16425 vec_safe_push (decl_scope_table, scope);
16428 /* Pop a declaration scope. */
16430 static inline void
16431 pop_decl_scope (void)
16433 decl_scope_table->pop ();
16436 /* walk_tree helper function for uses_local_type, below. */
16438 static tree
16439 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16441 if (!TYPE_P (*tp))
16442 *walk_subtrees = 0;
16443 else
16445 tree name = TYPE_NAME (*tp);
16446 if (name && DECL_P (name) && decl_function_context (name))
16447 return *tp;
16449 return NULL_TREE;
16452 /* If TYPE involves a function-local type (including a local typedef to a
16453 non-local type), returns that type; otherwise returns NULL_TREE. */
16455 static tree
16456 uses_local_type (tree type)
16458 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16459 return used;
16462 /* Return the DIE for the scope that immediately contains this type.
16463 Non-named types that do not involve a function-local type get global
16464 scope. Named types nested in namespaces or other types get their
16465 containing scope. All other types (i.e. function-local named types) get
16466 the current active scope. */
16468 static dw_die_ref
16469 scope_die_for (tree t, dw_die_ref context_die)
16471 dw_die_ref scope_die = NULL;
16472 tree containing_scope;
16474 /* Non-types always go in the current scope. */
16475 gcc_assert (TYPE_P (t));
16477 /* Use the scope of the typedef, rather than the scope of the type
16478 it refers to. */
16479 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16480 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16481 else
16482 containing_scope = TYPE_CONTEXT (t);
16484 /* Use the containing namespace if there is one. */
16485 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16487 if (context_die == lookup_decl_die (containing_scope))
16488 /* OK */;
16489 else if (debug_info_level > DINFO_LEVEL_TERSE)
16490 context_die = get_context_die (containing_scope);
16491 else
16492 containing_scope = NULL_TREE;
16495 /* Ignore function type "scopes" from the C frontend. They mean that
16496 a tagged type is local to a parmlist of a function declarator, but
16497 that isn't useful to DWARF. */
16498 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16499 containing_scope = NULL_TREE;
16501 if (SCOPE_FILE_SCOPE_P (containing_scope))
16503 /* If T uses a local type keep it local as well, to avoid references
16504 to function-local DIEs from outside the function. */
16505 if (current_function_decl && uses_local_type (t))
16506 scope_die = context_die;
16507 else
16508 scope_die = comp_unit_die ();
16510 else if (TYPE_P (containing_scope))
16512 /* For types, we can just look up the appropriate DIE. */
16513 if (debug_info_level > DINFO_LEVEL_TERSE)
16514 scope_die = get_context_die (containing_scope);
16515 else
16517 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16518 if (scope_die == NULL)
16519 scope_die = comp_unit_die ();
16522 else
16523 scope_die = context_die;
16525 return scope_die;
16528 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16530 static inline int
16531 local_scope_p (dw_die_ref context_die)
16533 for (; context_die; context_die = context_die->die_parent)
16534 if (context_die->die_tag == DW_TAG_inlined_subroutine
16535 || context_die->die_tag == DW_TAG_subprogram)
16536 return 1;
16538 return 0;
16541 /* Returns nonzero if CONTEXT_DIE is a class. */
16543 static inline int
16544 class_scope_p (dw_die_ref context_die)
16546 return (context_die
16547 && (context_die->die_tag == DW_TAG_structure_type
16548 || context_die->die_tag == DW_TAG_class_type
16549 || context_die->die_tag == DW_TAG_interface_type
16550 || context_die->die_tag == DW_TAG_union_type));
16553 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16554 whether or not to treat a DIE in this context as a declaration. */
16556 static inline int
16557 class_or_namespace_scope_p (dw_die_ref context_die)
16559 return (class_scope_p (context_die)
16560 || (context_die && context_die->die_tag == DW_TAG_namespace));
16563 /* Many forms of DIEs require a "type description" attribute. This
16564 routine locates the proper "type descriptor" die for the type given
16565 by 'type', and adds a DW_AT_type attribute below the given die. */
16567 static void
16568 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16569 int decl_volatile, dw_die_ref context_die)
16571 enum tree_code code = TREE_CODE (type);
16572 dw_die_ref type_die = NULL;
16574 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16575 or fixed-point type, use the inner type. This is because we have no
16576 support for unnamed types in base_type_die. This can happen if this is
16577 an Ada subrange type. Correct solution is emit a subrange type die. */
16578 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16579 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16580 type = TREE_TYPE (type), code = TREE_CODE (type);
16582 if (code == ERROR_MARK
16583 /* Handle a special case. For functions whose return type is void, we
16584 generate *no* type attribute. (Note that no object may have type
16585 `void', so this only applies to function return types). */
16586 || code == VOID_TYPE)
16587 return;
16589 type_die = modified_type_die (type,
16590 decl_const || TYPE_READONLY (type),
16591 decl_volatile || TYPE_VOLATILE (type),
16592 context_die);
16594 if (type_die != NULL)
16595 add_AT_die_ref (object_die, DW_AT_type, type_die);
16598 /* Given an object die, add the calling convention attribute for the
16599 function call type. */
16600 static void
16601 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16603 enum dwarf_calling_convention value = DW_CC_normal;
16605 value = ((enum dwarf_calling_convention)
16606 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16608 if (is_fortran ()
16609 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16611 /* DWARF 2 doesn't provide a way to identify a program's source-level
16612 entry point. DW_AT_calling_convention attributes are only meant
16613 to describe functions' calling conventions. However, lacking a
16614 better way to signal the Fortran main program, we used this for
16615 a long time, following existing custom. Now, DWARF 4 has
16616 DW_AT_main_subprogram, which we add below, but some tools still
16617 rely on the old way, which we thus keep. */
16618 value = DW_CC_program;
16620 if (dwarf_version >= 4 || !dwarf_strict)
16621 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16624 /* Only add the attribute if the backend requests it, and
16625 is not DW_CC_normal. */
16626 if (value && (value != DW_CC_normal))
16627 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16630 /* Given a tree pointer to a struct, class, union, or enum type node, return
16631 a pointer to the (string) tag name for the given type, or zero if the type
16632 was declared without a tag. */
16634 static const char *
16635 type_tag (const_tree type)
16637 const char *name = 0;
16639 if (TYPE_NAME (type) != 0)
16641 tree t = 0;
16643 /* Find the IDENTIFIER_NODE for the type name. */
16644 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16645 && !TYPE_NAMELESS (type))
16646 t = TYPE_NAME (type);
16648 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16649 a TYPE_DECL node, regardless of whether or not a `typedef' was
16650 involved. */
16651 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16652 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16654 /* We want to be extra verbose. Don't call dwarf_name if
16655 DECL_NAME isn't set. The default hook for decl_printable_name
16656 doesn't like that, and in this context it's correct to return
16657 0, instead of "<anonymous>" or the like. */
16658 if (DECL_NAME (TYPE_NAME (type))
16659 && !DECL_NAMELESS (TYPE_NAME (type)))
16660 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16663 /* Now get the name as a string, or invent one. */
16664 if (!name && t != 0)
16665 name = IDENTIFIER_POINTER (t);
16668 return (name == 0 || *name == '\0') ? 0 : name;
16671 /* Return the type associated with a data member, make a special check
16672 for bit field types. */
16674 static inline tree
16675 member_declared_type (const_tree member)
16677 return (DECL_BIT_FIELD_TYPE (member)
16678 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16681 /* Get the decl's label, as described by its RTL. This may be different
16682 from the DECL_NAME name used in the source file. */
16684 #if 0
16685 static const char *
16686 decl_start_label (tree decl)
16688 rtx x;
16689 const char *fnname;
16691 x = DECL_RTL (decl);
16692 gcc_assert (MEM_P (x));
16694 x = XEXP (x, 0);
16695 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16697 fnname = XSTR (x, 0);
16698 return fnname;
16700 #endif
16702 /* These routines generate the internal representation of the DIE's for
16703 the compilation unit. Debugging information is collected by walking
16704 the declaration trees passed in from dwarf2out_decl(). */
16706 static void
16707 gen_array_type_die (tree type, dw_die_ref context_die)
16709 dw_die_ref scope_die = scope_die_for (type, context_die);
16710 dw_die_ref array_die;
16712 /* GNU compilers represent multidimensional array types as sequences of one
16713 dimensional array types whose element types are themselves array types.
16714 We sometimes squish that down to a single array_type DIE with multiple
16715 subscripts in the Dwarf debugging info. The draft Dwarf specification
16716 say that we are allowed to do this kind of compression in C, because
16717 there is no difference between an array of arrays and a multidimensional
16718 array. We don't do this for Ada to remain as close as possible to the
16719 actual representation, which is especially important against the language
16720 flexibilty wrt arrays of variable size. */
16722 bool collapse_nested_arrays = !is_ada ();
16723 tree element_type;
16725 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16726 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16727 if (TYPE_STRING_FLAG (type)
16728 && TREE_CODE (type) == ARRAY_TYPE
16729 && is_fortran ()
16730 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16732 HOST_WIDE_INT size;
16734 array_die = new_die (DW_TAG_string_type, scope_die, type);
16735 add_name_attribute (array_die, type_tag (type));
16736 equate_type_number_to_die (type, array_die);
16737 size = int_size_in_bytes (type);
16738 if (size >= 0)
16739 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16740 else if (TYPE_DOMAIN (type) != NULL_TREE
16741 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16742 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16744 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16745 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16747 size = int_size_in_bytes (TREE_TYPE (szdecl));
16748 if (loc && size > 0)
16750 add_AT_location_description (array_die, DW_AT_string_length, loc);
16751 if (size != DWARF2_ADDR_SIZE)
16752 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16755 return;
16758 array_die = new_die (DW_TAG_array_type, scope_die, type);
16759 add_name_attribute (array_die, type_tag (type));
16760 equate_type_number_to_die (type, array_die);
16762 if (TREE_CODE (type) == VECTOR_TYPE)
16763 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16765 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16766 if (is_fortran ()
16767 && TREE_CODE (type) == ARRAY_TYPE
16768 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16769 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16770 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16772 #if 0
16773 /* We default the array ordering. SDB will probably do
16774 the right things even if DW_AT_ordering is not present. It's not even
16775 an issue until we start to get into multidimensional arrays anyway. If
16776 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16777 then we'll have to put the DW_AT_ordering attribute back in. (But if
16778 and when we find out that we need to put these in, we will only do so
16779 for multidimensional arrays. */
16780 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16781 #endif
16783 if (TREE_CODE (type) == VECTOR_TYPE)
16785 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16786 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16787 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16788 add_bound_info (subrange_die, DW_AT_upper_bound,
16789 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16791 else
16792 add_subscript_info (array_die, type, collapse_nested_arrays);
16794 /* Add representation of the type of the elements of this array type and
16795 emit the corresponding DIE if we haven't done it already. */
16796 element_type = TREE_TYPE (type);
16797 if (collapse_nested_arrays)
16798 while (TREE_CODE (element_type) == ARRAY_TYPE)
16800 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16801 break;
16802 element_type = TREE_TYPE (element_type);
16805 add_type_attribute (array_die, element_type, 0, 0, context_die);
16807 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16808 if (TYPE_ARTIFICIAL (type))
16809 add_AT_flag (array_die, DW_AT_artificial, 1);
16811 if (get_AT (array_die, DW_AT_name))
16812 add_pubtype (type, array_die);
16815 static dw_loc_descr_ref
16816 descr_info_loc (tree val, tree base_decl)
16818 HOST_WIDE_INT size;
16819 dw_loc_descr_ref loc, loc2;
16820 enum dwarf_location_atom op;
16822 if (val == base_decl)
16823 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16825 switch (TREE_CODE (val))
16827 CASE_CONVERT:
16828 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16829 case VAR_DECL:
16830 return loc_descriptor_from_tree (val, 0);
16831 case INTEGER_CST:
16832 if (host_integerp (val, 0))
16833 return int_loc_descriptor (tree_low_cst (val, 0));
16834 break;
16835 case INDIRECT_REF:
16836 size = int_size_in_bytes (TREE_TYPE (val));
16837 if (size < 0)
16838 break;
16839 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16840 if (!loc)
16841 break;
16842 if (size == DWARF2_ADDR_SIZE)
16843 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16844 else
16845 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16846 return loc;
16847 case POINTER_PLUS_EXPR:
16848 case PLUS_EXPR:
16849 if (host_integerp (TREE_OPERAND (val, 1), 1)
16850 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16851 < 16384)
16853 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16854 if (!loc)
16855 break;
16856 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16858 else
16860 op = DW_OP_plus;
16861 do_binop:
16862 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16863 if (!loc)
16864 break;
16865 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16866 if (!loc2)
16867 break;
16868 add_loc_descr (&loc, loc2);
16869 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16871 return loc;
16872 case MINUS_EXPR:
16873 op = DW_OP_minus;
16874 goto do_binop;
16875 case MULT_EXPR:
16876 op = DW_OP_mul;
16877 goto do_binop;
16878 case EQ_EXPR:
16879 op = DW_OP_eq;
16880 goto do_binop;
16881 case NE_EXPR:
16882 op = DW_OP_ne;
16883 goto do_binop;
16884 default:
16885 break;
16887 return NULL;
16890 static void
16891 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16892 tree val, tree base_decl)
16894 dw_loc_descr_ref loc;
16896 if (host_integerp (val, 0))
16898 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16899 return;
16902 loc = descr_info_loc (val, base_decl);
16903 if (!loc)
16904 return;
16906 add_AT_loc (die, attr, loc);
16909 /* This routine generates DIE for array with hidden descriptor, details
16910 are filled into *info by a langhook. */
16912 static void
16913 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16914 dw_die_ref context_die)
16916 dw_die_ref scope_die = scope_die_for (type, context_die);
16917 dw_die_ref array_die;
16918 int dim;
16920 array_die = new_die (DW_TAG_array_type, scope_die, type);
16921 add_name_attribute (array_die, type_tag (type));
16922 equate_type_number_to_die (type, array_die);
16924 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16925 if (is_fortran ()
16926 && info->ndimensions >= 2)
16927 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16929 if (info->data_location)
16930 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16931 info->base_decl);
16932 if (info->associated)
16933 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16934 info->base_decl);
16935 if (info->allocated)
16936 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16937 info->base_decl);
16939 for (dim = 0; dim < info->ndimensions; dim++)
16941 dw_die_ref subrange_die
16942 = new_die (DW_TAG_subrange_type, array_die, NULL);
16944 if (info->dimen[dim].lower_bound)
16946 /* If it is the default value, omit it. */
16947 int dflt;
16949 if (host_integerp (info->dimen[dim].lower_bound, 0)
16950 && (dflt = lower_bound_default ()) != -1
16951 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16953 else
16954 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16955 info->dimen[dim].lower_bound,
16956 info->base_decl);
16958 if (info->dimen[dim].upper_bound)
16959 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16960 info->dimen[dim].upper_bound,
16961 info->base_decl);
16962 if (info->dimen[dim].stride)
16963 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16964 info->dimen[dim].stride,
16965 info->base_decl);
16968 gen_type_die (info->element_type, context_die);
16969 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16971 if (get_AT (array_die, DW_AT_name))
16972 add_pubtype (type, array_die);
16975 #if 0
16976 static void
16977 gen_entry_point_die (tree decl, dw_die_ref context_die)
16979 tree origin = decl_ultimate_origin (decl);
16980 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16982 if (origin != NULL)
16983 add_abstract_origin_attribute (decl_die, origin);
16984 else
16986 add_name_and_src_coords_attributes (decl_die, decl);
16987 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16988 0, 0, context_die);
16991 if (DECL_ABSTRACT (decl))
16992 equate_decl_number_to_die (decl, decl_die);
16993 else
16994 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16996 #endif
16998 /* Walk through the list of incomplete types again, trying once more to
16999 emit full debugging info for them. */
17001 static void
17002 retry_incomplete_types (void)
17004 int i;
17006 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17007 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17008 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17011 /* Determine what tag to use for a record type. */
17013 static enum dwarf_tag
17014 record_type_tag (tree type)
17016 if (! lang_hooks.types.classify_record)
17017 return DW_TAG_structure_type;
17019 switch (lang_hooks.types.classify_record (type))
17021 case RECORD_IS_STRUCT:
17022 return DW_TAG_structure_type;
17024 case RECORD_IS_CLASS:
17025 return DW_TAG_class_type;
17027 case RECORD_IS_INTERFACE:
17028 if (dwarf_version >= 3 || !dwarf_strict)
17029 return DW_TAG_interface_type;
17030 return DW_TAG_structure_type;
17032 default:
17033 gcc_unreachable ();
17037 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17038 include all of the information about the enumeration values also. Each
17039 enumerated type name/value is listed as a child of the enumerated type
17040 DIE. */
17042 static dw_die_ref
17043 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17045 dw_die_ref type_die = lookup_type_die (type);
17047 if (type_die == NULL)
17049 type_die = new_die (DW_TAG_enumeration_type,
17050 scope_die_for (type, context_die), type);
17051 equate_type_number_to_die (type, type_die);
17052 add_name_attribute (type_die, type_tag (type));
17053 if (dwarf_version >= 4 || !dwarf_strict)
17055 if (ENUM_IS_SCOPED (type))
17056 add_AT_flag (type_die, DW_AT_enum_class, 1);
17057 if (ENUM_IS_OPAQUE (type))
17058 add_AT_flag (type_die, DW_AT_declaration, 1);
17061 else if (! TYPE_SIZE (type))
17062 return type_die;
17063 else
17064 remove_AT (type_die, DW_AT_declaration);
17066 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17067 given enum type is incomplete, do not generate the DW_AT_byte_size
17068 attribute or the DW_AT_element_list attribute. */
17069 if (TYPE_SIZE (type))
17071 tree link;
17073 TREE_ASM_WRITTEN (type) = 1;
17074 add_byte_size_attribute (type_die, type);
17075 if (TYPE_STUB_DECL (type) != NULL_TREE)
17077 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17078 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17081 /* If the first reference to this type was as the return type of an
17082 inline function, then it may not have a parent. Fix this now. */
17083 if (type_die->die_parent == NULL)
17084 add_child_die (scope_die_for (type, context_die), type_die);
17086 for (link = TYPE_VALUES (type);
17087 link != NULL; link = TREE_CHAIN (link))
17089 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17090 tree value = TREE_VALUE (link);
17092 add_name_attribute (enum_die,
17093 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17095 if (TREE_CODE (value) == CONST_DECL)
17096 value = DECL_INITIAL (value);
17098 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value)))
17099 && (simple_type_size_in_bits (TREE_TYPE (value))
17100 <= HOST_BITS_PER_WIDE_INT || host_integerp (value, 0)))
17101 /* DWARF2 does not provide a way of indicating whether or
17102 not enumeration constants are signed or unsigned. GDB
17103 always assumes the values are signed, so we output all
17104 values as if they were signed. That means that
17105 enumeration constants with very large unsigned values
17106 will appear to have negative values in the debugger.
17108 TODO: the above comment is wrong, DWARF2 does provide
17109 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17110 This should be re-worked to use correct signed/unsigned
17111 int/double tags for all cases, instead of always treating as
17112 signed. */
17113 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17114 else
17115 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17116 that here. */
17117 add_AT_double (enum_die, DW_AT_const_value,
17118 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17121 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17122 if (TYPE_ARTIFICIAL (type))
17123 add_AT_flag (type_die, DW_AT_artificial, 1);
17125 else
17126 add_AT_flag (type_die, DW_AT_declaration, 1);
17128 add_pubtype (type, type_die);
17130 return type_die;
17133 /* Generate a DIE to represent either a real live formal parameter decl or to
17134 represent just the type of some formal parameter position in some function
17135 type.
17137 Note that this routine is a bit unusual because its argument may be a
17138 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17139 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17140 node. If it's the former then this function is being called to output a
17141 DIE to represent a formal parameter object (or some inlining thereof). If
17142 it's the latter, then this function is only being called to output a
17143 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17144 argument type of some subprogram type.
17145 If EMIT_NAME_P is true, name and source coordinate attributes
17146 are emitted. */
17148 static dw_die_ref
17149 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17150 dw_die_ref context_die)
17152 tree node_or_origin = node ? node : origin;
17153 tree ultimate_origin;
17154 dw_die_ref parm_die
17155 = new_die (DW_TAG_formal_parameter, context_die, node);
17157 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17159 case tcc_declaration:
17160 ultimate_origin = decl_ultimate_origin (node_or_origin);
17161 if (node || ultimate_origin)
17162 origin = ultimate_origin;
17163 if (origin != NULL)
17164 add_abstract_origin_attribute (parm_die, origin);
17165 else if (emit_name_p)
17166 add_name_and_src_coords_attributes (parm_die, node);
17167 if (origin == NULL
17168 || (! DECL_ABSTRACT (node_or_origin)
17169 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17170 decl_function_context
17171 (node_or_origin))))
17173 tree type = TREE_TYPE (node_or_origin);
17174 if (decl_by_reference_p (node_or_origin))
17175 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17176 context_die);
17177 else
17178 add_type_attribute (parm_die, type,
17179 TREE_READONLY (node_or_origin),
17180 TREE_THIS_VOLATILE (node_or_origin),
17181 context_die);
17183 if (origin == NULL && DECL_ARTIFICIAL (node))
17184 add_AT_flag (parm_die, DW_AT_artificial, 1);
17186 if (node && node != origin)
17187 equate_decl_number_to_die (node, parm_die);
17188 if (! DECL_ABSTRACT (node_or_origin))
17189 add_location_or_const_value_attribute (parm_die, node_or_origin,
17190 node == NULL, DW_AT_location);
17192 break;
17194 case tcc_type:
17195 /* We were called with some kind of a ..._TYPE node. */
17196 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17197 break;
17199 default:
17200 gcc_unreachable ();
17203 return parm_die;
17206 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17207 children DW_TAG_formal_parameter DIEs representing the arguments of the
17208 parameter pack.
17210 PARM_PACK must be a function parameter pack.
17211 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17212 must point to the subsequent arguments of the function PACK_ARG belongs to.
17213 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17214 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17215 following the last one for which a DIE was generated. */
17217 static dw_die_ref
17218 gen_formal_parameter_pack_die (tree parm_pack,
17219 tree pack_arg,
17220 dw_die_ref subr_die,
17221 tree *next_arg)
17223 tree arg;
17224 dw_die_ref parm_pack_die;
17226 gcc_assert (parm_pack
17227 && lang_hooks.function_parameter_pack_p (parm_pack)
17228 && subr_die);
17230 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17231 add_src_coords_attributes (parm_pack_die, parm_pack);
17233 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17235 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17236 parm_pack))
17237 break;
17238 gen_formal_parameter_die (arg, NULL,
17239 false /* Don't emit name attribute. */,
17240 parm_pack_die);
17242 if (next_arg)
17243 *next_arg = arg;
17244 return parm_pack_die;
17247 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17248 at the end of an (ANSI prototyped) formal parameters list. */
17250 static void
17251 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17253 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17256 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17257 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17258 parameters as specified in some function type specification (except for
17259 those which appear as part of a function *definition*). */
17261 static void
17262 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17264 tree link;
17265 tree formal_type = NULL;
17266 tree first_parm_type;
17267 tree arg;
17269 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17271 arg = DECL_ARGUMENTS (function_or_method_type);
17272 function_or_method_type = TREE_TYPE (function_or_method_type);
17274 else
17275 arg = NULL_TREE;
17277 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17279 /* Make our first pass over the list of formal parameter types and output a
17280 DW_TAG_formal_parameter DIE for each one. */
17281 for (link = first_parm_type; link; )
17283 dw_die_ref parm_die;
17285 formal_type = TREE_VALUE (link);
17286 if (formal_type == void_type_node)
17287 break;
17289 /* Output a (nameless) DIE to represent the formal parameter itself. */
17290 parm_die = gen_formal_parameter_die (formal_type, NULL,
17291 true /* Emit name attribute. */,
17292 context_die);
17293 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17294 && link == first_parm_type)
17296 add_AT_flag (parm_die, DW_AT_artificial, 1);
17297 if (dwarf_version >= 3 || !dwarf_strict)
17298 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17300 else if (arg && DECL_ARTIFICIAL (arg))
17301 add_AT_flag (parm_die, DW_AT_artificial, 1);
17303 link = TREE_CHAIN (link);
17304 if (arg)
17305 arg = DECL_CHAIN (arg);
17308 /* If this function type has an ellipsis, add a
17309 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17310 if (formal_type != void_type_node)
17311 gen_unspecified_parameters_die (function_or_method_type, context_die);
17313 /* Make our second (and final) pass over the list of formal parameter types
17314 and output DIEs to represent those types (as necessary). */
17315 for (link = TYPE_ARG_TYPES (function_or_method_type);
17316 link && TREE_VALUE (link);
17317 link = TREE_CHAIN (link))
17318 gen_type_die (TREE_VALUE (link), context_die);
17321 /* We want to generate the DIE for TYPE so that we can generate the
17322 die for MEMBER, which has been defined; we will need to refer back
17323 to the member declaration nested within TYPE. If we're trying to
17324 generate minimal debug info for TYPE, processing TYPE won't do the
17325 trick; we need to attach the member declaration by hand. */
17327 static void
17328 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17330 gen_type_die (type, context_die);
17332 /* If we're trying to avoid duplicate debug info, we may not have
17333 emitted the member decl for this function. Emit it now. */
17334 if (TYPE_STUB_DECL (type)
17335 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17336 && ! lookup_decl_die (member))
17338 dw_die_ref type_die;
17339 gcc_assert (!decl_ultimate_origin (member));
17341 push_decl_scope (type);
17342 type_die = lookup_type_die_strip_naming_typedef (type);
17343 if (TREE_CODE (member) == FUNCTION_DECL)
17344 gen_subprogram_die (member, type_die);
17345 else if (TREE_CODE (member) == FIELD_DECL)
17347 /* Ignore the nameless fields that are used to skip bits but handle
17348 C++ anonymous unions and structs. */
17349 if (DECL_NAME (member) != NULL_TREE
17350 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17351 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17353 gen_type_die (member_declared_type (member), type_die);
17354 gen_field_die (member, type_die);
17357 else
17358 gen_variable_die (member, NULL_TREE, type_die);
17360 pop_decl_scope ();
17364 /* Forward declare these functions, because they are mutually recursive
17365 with their set_block_* pairing functions. */
17366 static void set_decl_origin_self (tree);
17367 static void set_decl_abstract_flags (tree, int);
17369 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17370 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17371 that it points to the node itself, thus indicating that the node is its
17372 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17373 the given node is NULL, recursively descend the decl/block tree which
17374 it is the root of, and for each other ..._DECL or BLOCK node contained
17375 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17376 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17377 values to point to themselves. */
17379 static void
17380 set_block_origin_self (tree stmt)
17382 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17384 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17387 tree local_decl;
17389 for (local_decl = BLOCK_VARS (stmt);
17390 local_decl != NULL_TREE;
17391 local_decl = DECL_CHAIN (local_decl))
17392 if (! DECL_EXTERNAL (local_decl))
17393 set_decl_origin_self (local_decl); /* Potential recursion. */
17397 tree subblock;
17399 for (subblock = BLOCK_SUBBLOCKS (stmt);
17400 subblock != NULL_TREE;
17401 subblock = BLOCK_CHAIN (subblock))
17402 set_block_origin_self (subblock); /* Recurse. */
17407 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17408 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17409 node to so that it points to the node itself, thus indicating that the
17410 node represents its own (abstract) origin. Additionally, if the
17411 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17412 the decl/block tree of which the given node is the root of, and for
17413 each other ..._DECL or BLOCK node contained therein whose
17414 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17415 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17416 point to themselves. */
17418 static void
17419 set_decl_origin_self (tree decl)
17421 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17423 DECL_ABSTRACT_ORIGIN (decl) = decl;
17424 if (TREE_CODE (decl) == FUNCTION_DECL)
17426 tree arg;
17428 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17429 DECL_ABSTRACT_ORIGIN (arg) = arg;
17430 if (DECL_INITIAL (decl) != NULL_TREE
17431 && DECL_INITIAL (decl) != error_mark_node)
17432 set_block_origin_self (DECL_INITIAL (decl));
17437 /* Given a pointer to some BLOCK node, and a boolean value to set the
17438 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17439 the given block, and for all local decls and all local sub-blocks
17440 (recursively) which are contained therein. */
17442 static void
17443 set_block_abstract_flags (tree stmt, int setting)
17445 tree local_decl;
17446 tree subblock;
17447 unsigned int i;
17449 BLOCK_ABSTRACT (stmt) = setting;
17451 for (local_decl = BLOCK_VARS (stmt);
17452 local_decl != NULL_TREE;
17453 local_decl = DECL_CHAIN (local_decl))
17454 if (! DECL_EXTERNAL (local_decl))
17455 set_decl_abstract_flags (local_decl, setting);
17457 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17459 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17460 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17461 || TREE_CODE (local_decl) == PARM_DECL)
17462 set_decl_abstract_flags (local_decl, setting);
17465 for (subblock = BLOCK_SUBBLOCKS (stmt);
17466 subblock != NULL_TREE;
17467 subblock = BLOCK_CHAIN (subblock))
17468 set_block_abstract_flags (subblock, setting);
17471 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17472 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17473 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17474 set the abstract flags for all of the parameters, local vars, local
17475 blocks and sub-blocks (recursively) to the same setting. */
17477 static void
17478 set_decl_abstract_flags (tree decl, int setting)
17480 DECL_ABSTRACT (decl) = setting;
17481 if (TREE_CODE (decl) == FUNCTION_DECL)
17483 tree arg;
17485 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17486 DECL_ABSTRACT (arg) = setting;
17487 if (DECL_INITIAL (decl) != NULL_TREE
17488 && DECL_INITIAL (decl) != error_mark_node)
17489 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17493 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17494 may later generate inlined and/or out-of-line instances of. */
17496 static void
17497 dwarf2out_abstract_function (tree decl)
17499 dw_die_ref old_die;
17500 tree save_fn;
17501 tree context;
17502 int was_abstract;
17503 htab_t old_decl_loc_table;
17504 htab_t old_cached_dw_loc_list_table;
17505 int old_call_site_count, old_tail_call_site_count;
17506 struct call_arg_loc_node *old_call_arg_locations;
17508 /* Make sure we have the actual abstract inline, not a clone. */
17509 decl = DECL_ORIGIN (decl);
17511 old_die = lookup_decl_die (decl);
17512 if (old_die && get_AT (old_die, DW_AT_inline))
17513 /* We've already generated the abstract instance. */
17514 return;
17516 /* We can be called while recursively when seeing block defining inlined subroutine
17517 DIE. Be sure to not clobber the outer location table nor use it or we would
17518 get locations in abstract instantces. */
17519 old_decl_loc_table = decl_loc_table;
17520 decl_loc_table = NULL;
17521 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17522 cached_dw_loc_list_table = NULL;
17523 old_call_arg_locations = call_arg_locations;
17524 call_arg_locations = NULL;
17525 old_call_site_count = call_site_count;
17526 call_site_count = -1;
17527 old_tail_call_site_count = tail_call_site_count;
17528 tail_call_site_count = -1;
17530 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17531 we don't get confused by DECL_ABSTRACT. */
17532 if (debug_info_level > DINFO_LEVEL_TERSE)
17534 context = decl_class_context (decl);
17535 if (context)
17536 gen_type_die_for_member
17537 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17540 /* Pretend we've just finished compiling this function. */
17541 save_fn = current_function_decl;
17542 current_function_decl = decl;
17544 was_abstract = DECL_ABSTRACT (decl);
17545 set_decl_abstract_flags (decl, 1);
17546 dwarf2out_decl (decl);
17547 if (! was_abstract)
17548 set_decl_abstract_flags (decl, 0);
17550 current_function_decl = save_fn;
17551 decl_loc_table = old_decl_loc_table;
17552 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17553 call_arg_locations = old_call_arg_locations;
17554 call_site_count = old_call_site_count;
17555 tail_call_site_count = old_tail_call_site_count;
17558 /* Helper function of premark_used_types() which gets called through
17559 htab_traverse.
17561 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17562 marked as unused by prune_unused_types. */
17564 static int
17565 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17567 tree type;
17568 dw_die_ref die;
17570 type = (tree) *slot;
17571 die = lookup_type_die (type);
17572 if (die != NULL)
17573 die->die_perennial_p = 1;
17574 return 1;
17577 /* Helper function of premark_types_used_by_global_vars which gets called
17578 through htab_traverse.
17580 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17581 marked as unused by prune_unused_types. The DIE of the type is marked
17582 only if the global variable using the type will actually be emitted. */
17584 static int
17585 premark_types_used_by_global_vars_helper (void **slot,
17586 void *data ATTRIBUTE_UNUSED)
17588 struct types_used_by_vars_entry *entry;
17589 dw_die_ref die;
17591 entry = (struct types_used_by_vars_entry *) *slot;
17592 gcc_assert (entry->type != NULL
17593 && entry->var_decl != NULL);
17594 die = lookup_type_die (entry->type);
17595 if (die)
17597 /* Ask cgraph if the global variable really is to be emitted.
17598 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17599 struct varpool_node *node = varpool_get_node (entry->var_decl);
17600 if (node && node->analyzed)
17602 die->die_perennial_p = 1;
17603 /* Keep the parent DIEs as well. */
17604 while ((die = die->die_parent) && die->die_perennial_p == 0)
17605 die->die_perennial_p = 1;
17608 return 1;
17611 /* Mark all members of used_types_hash as perennial. */
17613 static void
17614 premark_used_types (struct function *fun)
17616 if (fun && fun->used_types_hash)
17617 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17620 /* Mark all members of types_used_by_vars_entry as perennial. */
17622 static void
17623 premark_types_used_by_global_vars (void)
17625 if (types_used_by_vars_hash)
17626 htab_traverse (types_used_by_vars_hash,
17627 premark_types_used_by_global_vars_helper, NULL);
17630 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17631 for CA_LOC call arg loc node. */
17633 static dw_die_ref
17634 gen_call_site_die (tree decl, dw_die_ref subr_die,
17635 struct call_arg_loc_node *ca_loc)
17637 dw_die_ref stmt_die = NULL, die;
17638 tree block = ca_loc->block;
17640 while (block
17641 && block != DECL_INITIAL (decl)
17642 && TREE_CODE (block) == BLOCK)
17644 if (block_map.length () > BLOCK_NUMBER (block))
17645 stmt_die = block_map[BLOCK_NUMBER (block)];
17646 if (stmt_die)
17647 break;
17648 block = BLOCK_SUPERCONTEXT (block);
17650 if (stmt_die == NULL)
17651 stmt_die = subr_die;
17652 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17653 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17654 if (ca_loc->tail_call_p)
17655 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17656 if (ca_loc->symbol_ref)
17658 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17659 if (tdie)
17660 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17661 else
17662 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17664 return die;
17667 /* Generate a DIE to represent a declared function (either file-scope or
17668 block-local). */
17670 static void
17671 gen_subprogram_die (tree decl, dw_die_ref context_die)
17673 tree origin = decl_ultimate_origin (decl);
17674 dw_die_ref subr_die;
17675 tree outer_scope;
17676 dw_die_ref old_die = lookup_decl_die (decl);
17677 int declaration = (current_function_decl != decl
17678 || class_or_namespace_scope_p (context_die));
17680 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17682 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17683 started to generate the abstract instance of an inline, decided to output
17684 its containing class, and proceeded to emit the declaration of the inline
17685 from the member list for the class. If so, DECLARATION takes priority;
17686 we'll get back to the abstract instance when done with the class. */
17688 /* The class-scope declaration DIE must be the primary DIE. */
17689 if (origin && declaration && class_or_namespace_scope_p (context_die))
17691 origin = NULL;
17692 gcc_assert (!old_die);
17695 /* Now that the C++ front end lazily declares artificial member fns, we
17696 might need to retrofit the declaration into its class. */
17697 if (!declaration && !origin && !old_die
17698 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17699 && !class_or_namespace_scope_p (context_die)
17700 && debug_info_level > DINFO_LEVEL_TERSE)
17701 old_die = force_decl_die (decl);
17703 if (origin != NULL)
17705 gcc_assert (!declaration || local_scope_p (context_die));
17707 /* Fixup die_parent for the abstract instance of a nested
17708 inline function. */
17709 if (old_die && old_die->die_parent == NULL)
17710 add_child_die (context_die, old_die);
17712 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17713 add_abstract_origin_attribute (subr_die, origin);
17714 /* This is where the actual code for a cloned function is.
17715 Let's emit linkage name attribute for it. This helps
17716 debuggers to e.g, set breakpoints into
17717 constructors/destructors when the user asks "break
17718 K::K". */
17719 add_linkage_name (subr_die, decl);
17721 else if (old_die)
17723 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17724 struct dwarf_file_data * file_index = lookup_filename (s.file);
17726 if (!get_AT_flag (old_die, DW_AT_declaration)
17727 /* We can have a normal definition following an inline one in the
17728 case of redefinition of GNU C extern inlines.
17729 It seems reasonable to use AT_specification in this case. */
17730 && !get_AT (old_die, DW_AT_inline))
17732 /* Detect and ignore this case, where we are trying to output
17733 something we have already output. */
17734 return;
17737 /* If the definition comes from the same place as the declaration,
17738 maybe use the old DIE. We always want the DIE for this function
17739 that has the *_pc attributes to be under comp_unit_die so the
17740 debugger can find it. We also need to do this for abstract
17741 instances of inlines, since the spec requires the out-of-line copy
17742 to have the same parent. For local class methods, this doesn't
17743 apply; we just use the old DIE. */
17744 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17745 && (DECL_ARTIFICIAL (decl)
17746 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17747 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17748 == (unsigned) s.line))))
17750 subr_die = old_die;
17752 /* Clear out the declaration attribute and the formal parameters.
17753 Do not remove all children, because it is possible that this
17754 declaration die was forced using force_decl_die(). In such
17755 cases die that forced declaration die (e.g. TAG_imported_module)
17756 is one of the children that we do not want to remove. */
17757 remove_AT (subr_die, DW_AT_declaration);
17758 remove_AT (subr_die, DW_AT_object_pointer);
17759 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17761 else
17763 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17764 add_AT_specification (subr_die, old_die);
17765 add_pubname (decl, subr_die);
17766 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17767 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17768 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17769 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17772 else
17774 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17776 if (TREE_PUBLIC (decl))
17777 add_AT_flag (subr_die, DW_AT_external, 1);
17779 add_name_and_src_coords_attributes (subr_die, decl);
17780 add_pubname (decl, subr_die);
17781 if (debug_info_level > DINFO_LEVEL_TERSE)
17783 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17784 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17785 0, 0, context_die);
17788 add_pure_or_virtual_attribute (subr_die, decl);
17789 if (DECL_ARTIFICIAL (decl))
17790 add_AT_flag (subr_die, DW_AT_artificial, 1);
17792 add_accessibility_attribute (subr_die, decl);
17795 if (declaration)
17797 if (!old_die || !get_AT (old_die, DW_AT_inline))
17799 add_AT_flag (subr_die, DW_AT_declaration, 1);
17801 /* If this is an explicit function declaration then generate
17802 a DW_AT_explicit attribute. */
17803 if (lang_hooks.decls.function_decl_explicit_p (decl)
17804 && (dwarf_version >= 3 || !dwarf_strict))
17805 add_AT_flag (subr_die, DW_AT_explicit, 1);
17807 /* The first time we see a member function, it is in the context of
17808 the class to which it belongs. We make sure of this by emitting
17809 the class first. The next time is the definition, which is
17810 handled above. The two may come from the same source text.
17812 Note that force_decl_die() forces function declaration die. It is
17813 later reused to represent definition. */
17814 equate_decl_number_to_die (decl, subr_die);
17817 else if (DECL_ABSTRACT (decl))
17819 if (DECL_DECLARED_INLINE_P (decl))
17821 if (cgraph_function_possibly_inlined_p (decl))
17822 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17823 else
17824 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17826 else
17828 if (cgraph_function_possibly_inlined_p (decl))
17829 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17830 else
17831 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17834 if (DECL_DECLARED_INLINE_P (decl)
17835 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17836 add_AT_flag (subr_die, DW_AT_artificial, 1);
17838 equate_decl_number_to_die (decl, subr_die);
17840 else if (!DECL_EXTERNAL (decl))
17842 HOST_WIDE_INT cfa_fb_offset;
17843 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17845 if (!old_die || !get_AT (old_die, DW_AT_inline))
17846 equate_decl_number_to_die (decl, subr_die);
17848 gcc_checking_assert (fun);
17849 if (!flag_reorder_blocks_and_partition)
17851 dw_fde_ref fde = fun->fde;
17852 if (fde->dw_fde_begin)
17854 /* We have already generated the labels. */
17855 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17856 fde->dw_fde_end, false);
17858 else
17860 /* Create start/end labels and add the range. */
17861 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17862 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17863 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17864 current_function_funcdef_no);
17865 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17866 current_function_funcdef_no);
17867 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17868 false);
17871 #if VMS_DEBUGGING_INFO
17872 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17873 Section 2.3 Prologue and Epilogue Attributes:
17874 When a breakpoint is set on entry to a function, it is generally
17875 desirable for execution to be suspended, not on the very first
17876 instruction of the function, but rather at a point after the
17877 function's frame has been set up, after any language defined local
17878 declaration processing has been completed, and before execution of
17879 the first statement of the function begins. Debuggers generally
17880 cannot properly determine where this point is. Similarly for a
17881 breakpoint set on exit from a function. The prologue and epilogue
17882 attributes allow a compiler to communicate the location(s) to use. */
17885 if (fde->dw_fde_vms_end_prologue)
17886 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17887 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17889 if (fde->dw_fde_vms_begin_epilogue)
17890 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17891 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17893 #endif
17896 else
17898 /* Generate pubnames entries for the split function code ranges. */
17899 dw_fde_ref fde = fun->fde;
17901 if (fde->dw_fde_second_begin)
17903 if (dwarf_version >= 3 || !dwarf_strict)
17905 /* We should use ranges for non-contiguous code section
17906 addresses. Use the actual code range for the initial
17907 section, since the HOT/COLD labels might precede an
17908 alignment offset. */
17909 bool range_list_added = false;
17910 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17911 fde->dw_fde_end, &range_list_added,
17912 false);
17913 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17914 fde->dw_fde_second_end,
17915 &range_list_added, false);
17916 if (range_list_added)
17917 add_ranges (NULL);
17919 else
17921 /* There is no real support in DW2 for this .. so we make
17922 a work-around. First, emit the pub name for the segment
17923 containing the function label. Then make and emit a
17924 simplified subprogram DIE for the second segment with the
17925 name pre-fixed by __hot/cold_sect_of_. We use the same
17926 linkage name for the second die so that gdb will find both
17927 sections when given "b foo". */
17928 const char *name = NULL;
17929 tree decl_name = DECL_NAME (decl);
17930 dw_die_ref seg_die;
17932 /* Do the 'primary' section. */
17933 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17934 fde->dw_fde_end, false);
17936 /* Build a minimal DIE for the secondary section. */
17937 seg_die = new_die (DW_TAG_subprogram,
17938 subr_die->die_parent, decl);
17940 if (TREE_PUBLIC (decl))
17941 add_AT_flag (seg_die, DW_AT_external, 1);
17943 if (decl_name != NULL
17944 && IDENTIFIER_POINTER (decl_name) != NULL)
17946 name = dwarf2_name (decl, 1);
17947 if (! DECL_ARTIFICIAL (decl))
17948 add_src_coords_attributes (seg_die, decl);
17950 add_linkage_name (seg_die, decl);
17952 gcc_assert (name != NULL);
17953 add_pure_or_virtual_attribute (seg_die, decl);
17954 if (DECL_ARTIFICIAL (decl))
17955 add_AT_flag (seg_die, DW_AT_artificial, 1);
17957 name = concat ("__second_sect_of_", name, NULL);
17958 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17959 fde->dw_fde_second_end, false);
17960 add_name_attribute (seg_die, name);
17961 if (want_pubnames ())
17962 add_pubname_string (name, seg_die);
17965 else
17966 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17967 false);
17970 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17972 /* We define the "frame base" as the function's CFA. This is more
17973 convenient for several reasons: (1) It's stable across the prologue
17974 and epilogue, which makes it better than just a frame pointer,
17975 (2) With dwarf3, there exists a one-byte encoding that allows us
17976 to reference the .debug_frame data by proxy, but failing that,
17977 (3) We can at least reuse the code inspection and interpretation
17978 code that determines the CFA position at various points in the
17979 function. */
17980 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17982 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17983 add_AT_loc (subr_die, DW_AT_frame_base, op);
17985 else
17987 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17988 if (list->dw_loc_next)
17989 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17990 else
17991 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17994 /* Compute a displacement from the "steady-state frame pointer" to
17995 the CFA. The former is what all stack slots and argument slots
17996 will reference in the rtl; the latter is what we've told the
17997 debugger about. We'll need to adjust all frame_base references
17998 by this displacement. */
17999 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18001 if (fun->static_chain_decl)
18002 add_AT_location_description (subr_die, DW_AT_static_link,
18003 loc_list_from_tree (fun->static_chain_decl, 2));
18006 /* Generate child dies for template paramaters. */
18007 if (debug_info_level > DINFO_LEVEL_TERSE)
18008 gen_generic_params_dies (decl);
18010 /* Now output descriptions of the arguments for this function. This gets
18011 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18012 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18013 `...' at the end of the formal parameter list. In order to find out if
18014 there was a trailing ellipsis or not, we must instead look at the type
18015 associated with the FUNCTION_DECL. This will be a node of type
18016 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18017 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18018 an ellipsis at the end. */
18020 /* In the case where we are describing a mere function declaration, all we
18021 need to do here (and all we *can* do here) is to describe the *types* of
18022 its formal parameters. */
18023 if (debug_info_level <= DINFO_LEVEL_TERSE)
18025 else if (declaration)
18026 gen_formal_types_die (decl, subr_die);
18027 else
18029 /* Generate DIEs to represent all known formal parameters. */
18030 tree parm = DECL_ARGUMENTS (decl);
18031 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18032 tree generic_decl_parm = generic_decl
18033 ? DECL_ARGUMENTS (generic_decl)
18034 : NULL;
18036 /* Now we want to walk the list of parameters of the function and
18037 emit their relevant DIEs.
18039 We consider the case of DECL being an instance of a generic function
18040 as well as it being a normal function.
18042 If DECL is an instance of a generic function we walk the
18043 parameters of the generic function declaration _and_ the parameters of
18044 DECL itself. This is useful because we want to emit specific DIEs for
18045 function parameter packs and those are declared as part of the
18046 generic function declaration. In that particular case,
18047 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18048 That DIE has children DIEs representing the set of arguments
18049 of the pack. Note that the set of pack arguments can be empty.
18050 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18051 children DIE.
18053 Otherwise, we just consider the parameters of DECL. */
18054 while (generic_decl_parm || parm)
18056 if (generic_decl_parm
18057 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18058 gen_formal_parameter_pack_die (generic_decl_parm,
18059 parm, subr_die,
18060 &parm);
18061 else if (parm)
18063 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18065 if (parm == DECL_ARGUMENTS (decl)
18066 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18067 && parm_die
18068 && (dwarf_version >= 3 || !dwarf_strict))
18069 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18071 parm = DECL_CHAIN (parm);
18074 if (generic_decl_parm)
18075 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18078 /* Decide whether we need an unspecified_parameters DIE at the end.
18079 There are 2 more cases to do this for: 1) the ansi ... declaration -
18080 this is detectable when the end of the arg list is not a
18081 void_type_node 2) an unprototyped function declaration (not a
18082 definition). This just means that we have no info about the
18083 parameters at all. */
18084 if (prototype_p (TREE_TYPE (decl)))
18086 /* This is the prototyped case, check for.... */
18087 if (stdarg_p (TREE_TYPE (decl)))
18088 gen_unspecified_parameters_die (decl, subr_die);
18090 else if (DECL_INITIAL (decl) == NULL_TREE)
18091 gen_unspecified_parameters_die (decl, subr_die);
18094 /* Output Dwarf info for all of the stuff within the body of the function
18095 (if it has one - it may be just a declaration). */
18096 outer_scope = DECL_INITIAL (decl);
18098 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18099 a function. This BLOCK actually represents the outermost binding contour
18100 for the function, i.e. the contour in which the function's formal
18101 parameters and labels get declared. Curiously, it appears that the front
18102 end doesn't actually put the PARM_DECL nodes for the current function onto
18103 the BLOCK_VARS list for this outer scope, but are strung off of the
18104 DECL_ARGUMENTS list for the function instead.
18106 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18107 the LABEL_DECL nodes for the function however, and we output DWARF info
18108 for those in decls_for_scope. Just within the `outer_scope' there will be
18109 a BLOCK node representing the function's outermost pair of curly braces,
18110 and any blocks used for the base and member initializers of a C++
18111 constructor function. */
18112 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18114 int call_site_note_count = 0;
18115 int tail_call_site_note_count = 0;
18117 /* Emit a DW_TAG_variable DIE for a named return value. */
18118 if (DECL_NAME (DECL_RESULT (decl)))
18119 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18121 current_function_has_inlines = 0;
18122 decls_for_scope (outer_scope, subr_die, 0);
18124 if (call_arg_locations && !dwarf_strict)
18126 struct call_arg_loc_node *ca_loc;
18127 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18129 dw_die_ref die = NULL;
18130 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18131 rtx arg, next_arg;
18133 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18134 arg; arg = next_arg)
18136 dw_loc_descr_ref reg, val;
18137 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18138 dw_die_ref cdie, tdie = NULL;
18140 next_arg = XEXP (arg, 1);
18141 if (REG_P (XEXP (XEXP (arg, 0), 0))
18142 && next_arg
18143 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18144 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18145 && REGNO (XEXP (XEXP (arg, 0), 0))
18146 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18147 next_arg = XEXP (next_arg, 1);
18148 if (mode == VOIDmode)
18150 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18151 if (mode == VOIDmode)
18152 mode = GET_MODE (XEXP (arg, 0));
18154 if (mode == VOIDmode || mode == BLKmode)
18155 continue;
18156 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18158 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18159 tloc = XEXP (XEXP (arg, 0), 1);
18160 continue;
18162 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18163 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18165 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18166 tlocc = XEXP (XEXP (arg, 0), 1);
18167 continue;
18169 reg = NULL;
18170 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18171 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18172 VAR_INIT_STATUS_INITIALIZED);
18173 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18175 rtx mem = XEXP (XEXP (arg, 0), 0);
18176 reg = mem_loc_descriptor (XEXP (mem, 0),
18177 get_address_mode (mem),
18178 GET_MODE (mem),
18179 VAR_INIT_STATUS_INITIALIZED);
18181 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18182 == DEBUG_PARAMETER_REF)
18184 tree tdecl
18185 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18186 tdie = lookup_decl_die (tdecl);
18187 if (tdie == NULL)
18188 continue;
18190 else
18191 continue;
18192 if (reg == NULL
18193 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18194 != DEBUG_PARAMETER_REF)
18195 continue;
18196 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18197 VOIDmode,
18198 VAR_INIT_STATUS_INITIALIZED);
18199 if (val == NULL)
18200 continue;
18201 if (die == NULL)
18202 die = gen_call_site_die (decl, subr_die, ca_loc);
18203 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18204 NULL_TREE);
18205 if (reg != NULL)
18206 add_AT_loc (cdie, DW_AT_location, reg);
18207 else if (tdie != NULL)
18208 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18209 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18210 if (next_arg != XEXP (arg, 1))
18212 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18213 if (mode == VOIDmode)
18214 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18215 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18216 0), 1),
18217 mode, VOIDmode,
18218 VAR_INIT_STATUS_INITIALIZED);
18219 if (val != NULL)
18220 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18223 if (die == NULL
18224 && (ca_loc->symbol_ref || tloc))
18225 die = gen_call_site_die (decl, subr_die, ca_loc);
18226 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18228 dw_loc_descr_ref tval = NULL;
18230 if (tloc != NULL_RTX)
18231 tval = mem_loc_descriptor (tloc,
18232 GET_MODE (tloc) == VOIDmode
18233 ? Pmode : GET_MODE (tloc),
18234 VOIDmode,
18235 VAR_INIT_STATUS_INITIALIZED);
18236 if (tval)
18237 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18238 else if (tlocc != NULL_RTX)
18240 tval = mem_loc_descriptor (tlocc,
18241 GET_MODE (tlocc) == VOIDmode
18242 ? Pmode : GET_MODE (tlocc),
18243 VOIDmode,
18244 VAR_INIT_STATUS_INITIALIZED);
18245 if (tval)
18246 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18247 tval);
18250 if (die != NULL)
18252 call_site_note_count++;
18253 if (ca_loc->tail_call_p)
18254 tail_call_site_note_count++;
18258 call_arg_locations = NULL;
18259 call_arg_loc_last = NULL;
18260 if (tail_call_site_count >= 0
18261 && tail_call_site_count == tail_call_site_note_count
18262 && !dwarf_strict)
18264 if (call_site_count >= 0
18265 && call_site_count == call_site_note_count)
18266 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18267 else
18268 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18270 call_site_count = -1;
18271 tail_call_site_count = -1;
18273 /* Add the calling convention attribute if requested. */
18274 add_calling_convention_attribute (subr_die, decl);
18278 /* Returns a hash value for X (which really is a die_struct). */
18280 static hashval_t
18281 common_block_die_table_hash (const void *x)
18283 const_dw_die_ref d = (const_dw_die_ref) x;
18284 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18287 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18288 as decl_id and die_parent of die_struct Y. */
18290 static int
18291 common_block_die_table_eq (const void *x, const void *y)
18293 const_dw_die_ref d = (const_dw_die_ref) x;
18294 const_dw_die_ref e = (const_dw_die_ref) y;
18295 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18298 /* Generate a DIE to represent a declared data object.
18299 Either DECL or ORIGIN must be non-null. */
18301 static void
18302 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18304 HOST_WIDE_INT off = 0;
18305 tree com_decl;
18306 tree decl_or_origin = decl ? decl : origin;
18307 tree ultimate_origin;
18308 dw_die_ref var_die;
18309 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18310 dw_die_ref origin_die;
18311 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18312 || class_or_namespace_scope_p (context_die));
18313 bool specialization_p = false;
18315 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18316 if (decl || ultimate_origin)
18317 origin = ultimate_origin;
18318 com_decl = fortran_common (decl_or_origin, &off);
18320 /* Symbol in common gets emitted as a child of the common block, in the form
18321 of a data member. */
18322 if (com_decl)
18324 dw_die_ref com_die;
18325 dw_loc_list_ref loc;
18326 die_node com_die_arg;
18328 var_die = lookup_decl_die (decl_or_origin);
18329 if (var_die)
18331 if (get_AT (var_die, DW_AT_location) == NULL)
18333 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18334 if (loc)
18336 if (off)
18338 /* Optimize the common case. */
18339 if (single_element_loc_list_p (loc)
18340 && loc->expr->dw_loc_opc == DW_OP_addr
18341 && loc->expr->dw_loc_next == NULL
18342 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18343 == SYMBOL_REF)
18345 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18346 loc->expr->dw_loc_oprnd1.v.val_addr
18347 = plus_constant (GET_MODE (x), x , off);
18349 else
18350 loc_list_plus_const (loc, off);
18352 add_AT_location_description (var_die, DW_AT_location, loc);
18353 remove_AT (var_die, DW_AT_declaration);
18356 return;
18359 if (common_block_die_table == NULL)
18360 common_block_die_table
18361 = htab_create_ggc (10, common_block_die_table_hash,
18362 common_block_die_table_eq, NULL);
18364 com_die_arg.decl_id = DECL_UID (com_decl);
18365 com_die_arg.die_parent = context_die;
18366 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18367 loc = loc_list_from_tree (com_decl, 2);
18368 if (com_die == NULL)
18370 const char *cnam
18371 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18372 void **slot;
18374 com_die = new_die (DW_TAG_common_block, context_die, decl);
18375 add_name_and_src_coords_attributes (com_die, com_decl);
18376 if (loc)
18378 add_AT_location_description (com_die, DW_AT_location, loc);
18379 /* Avoid sharing the same loc descriptor between
18380 DW_TAG_common_block and DW_TAG_variable. */
18381 loc = loc_list_from_tree (com_decl, 2);
18383 else if (DECL_EXTERNAL (decl))
18384 add_AT_flag (com_die, DW_AT_declaration, 1);
18385 if (want_pubnames ())
18386 add_pubname_string (cnam, com_die); /* ??? needed? */
18387 com_die->decl_id = DECL_UID (com_decl);
18388 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18389 *slot = (void *) com_die;
18391 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18393 add_AT_location_description (com_die, DW_AT_location, loc);
18394 loc = loc_list_from_tree (com_decl, 2);
18395 remove_AT (com_die, DW_AT_declaration);
18397 var_die = new_die (DW_TAG_variable, com_die, decl);
18398 add_name_and_src_coords_attributes (var_die, decl);
18399 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18400 TREE_THIS_VOLATILE (decl), context_die);
18401 add_AT_flag (var_die, DW_AT_external, 1);
18402 if (loc)
18404 if (off)
18406 /* Optimize the common case. */
18407 if (single_element_loc_list_p (loc)
18408 && loc->expr->dw_loc_opc == DW_OP_addr
18409 && loc->expr->dw_loc_next == NULL
18410 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18412 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18413 loc->expr->dw_loc_oprnd1.v.val_addr
18414 = plus_constant (GET_MODE (x), x, off);
18416 else
18417 loc_list_plus_const (loc, off);
18419 add_AT_location_description (var_die, DW_AT_location, loc);
18421 else if (DECL_EXTERNAL (decl))
18422 add_AT_flag (var_die, DW_AT_declaration, 1);
18423 equate_decl_number_to_die (decl, var_die);
18424 return;
18427 /* If the compiler emitted a definition for the DECL declaration
18428 and if we already emitted a DIE for it, don't emit a second
18429 DIE for it again. Allow re-declarations of DECLs that are
18430 inside functions, though. */
18431 if (old_die && declaration && !local_scope_p (context_die))
18432 return;
18434 /* For static data members, the declaration in the class is supposed
18435 to have DW_TAG_member tag; the specification should still be
18436 DW_TAG_variable referencing the DW_TAG_member DIE. */
18437 if (declaration && class_scope_p (context_die))
18438 var_die = new_die (DW_TAG_member, context_die, decl);
18439 else
18440 var_die = new_die (DW_TAG_variable, context_die, decl);
18442 origin_die = NULL;
18443 if (origin != NULL)
18444 origin_die = add_abstract_origin_attribute (var_die, origin);
18446 /* Loop unrolling can create multiple blocks that refer to the same
18447 static variable, so we must test for the DW_AT_declaration flag.
18449 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18450 copy decls and set the DECL_ABSTRACT flag on them instead of
18451 sharing them.
18453 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18455 ??? The declare_in_namespace support causes us to get two DIEs for one
18456 variable, both of which are declarations. We want to avoid considering
18457 one to be a specification, so we must test that this DIE is not a
18458 declaration. */
18459 else if (old_die && TREE_STATIC (decl) && ! declaration
18460 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18462 /* This is a definition of a C++ class level static. */
18463 add_AT_specification (var_die, old_die);
18464 specialization_p = true;
18465 if (DECL_NAME (decl))
18467 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18468 struct dwarf_file_data * file_index = lookup_filename (s.file);
18470 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18471 add_AT_file (var_die, DW_AT_decl_file, file_index);
18473 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18474 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18476 if (old_die->die_tag == DW_TAG_member)
18477 add_linkage_name (var_die, decl);
18480 else
18481 add_name_and_src_coords_attributes (var_die, decl);
18483 if ((origin == NULL && !specialization_p)
18484 || (origin != NULL
18485 && !DECL_ABSTRACT (decl_or_origin)
18486 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18487 decl_function_context
18488 (decl_or_origin))))
18490 tree type = TREE_TYPE (decl_or_origin);
18492 if (decl_by_reference_p (decl_or_origin))
18493 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18494 else
18495 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18496 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18499 if (origin == NULL && !specialization_p)
18501 if (TREE_PUBLIC (decl))
18502 add_AT_flag (var_die, DW_AT_external, 1);
18504 if (DECL_ARTIFICIAL (decl))
18505 add_AT_flag (var_die, DW_AT_artificial, 1);
18507 add_accessibility_attribute (var_die, decl);
18510 if (declaration)
18511 add_AT_flag (var_die, DW_AT_declaration, 1);
18513 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18514 equate_decl_number_to_die (decl, var_die);
18516 if (! declaration
18517 && (! DECL_ABSTRACT (decl_or_origin)
18518 /* Local static vars are shared between all clones/inlines,
18519 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18520 already set. */
18521 || (TREE_CODE (decl_or_origin) == VAR_DECL
18522 && TREE_STATIC (decl_or_origin)
18523 && DECL_RTL_SET_P (decl_or_origin)))
18524 /* When abstract origin already has DW_AT_location attribute, no need
18525 to add it again. */
18526 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18528 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18529 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18530 defer_location (decl_or_origin, var_die);
18531 else
18532 add_location_or_const_value_attribute (var_die, decl_or_origin,
18533 decl == NULL, DW_AT_location);
18534 add_pubname (decl_or_origin, var_die);
18536 else
18537 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18540 /* Generate a DIE to represent a named constant. */
18542 static void
18543 gen_const_die (tree decl, dw_die_ref context_die)
18545 dw_die_ref const_die;
18546 tree type = TREE_TYPE (decl);
18548 const_die = new_die (DW_TAG_constant, context_die, decl);
18549 add_name_and_src_coords_attributes (const_die, decl);
18550 add_type_attribute (const_die, type, 1, 0, context_die);
18551 if (TREE_PUBLIC (decl))
18552 add_AT_flag (const_die, DW_AT_external, 1);
18553 if (DECL_ARTIFICIAL (decl))
18554 add_AT_flag (const_die, DW_AT_artificial, 1);
18555 tree_add_const_value_attribute_for_decl (const_die, decl);
18558 /* Generate a DIE to represent a label identifier. */
18560 static void
18561 gen_label_die (tree decl, dw_die_ref context_die)
18563 tree origin = decl_ultimate_origin (decl);
18564 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18565 rtx insn;
18566 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18568 if (origin != NULL)
18569 add_abstract_origin_attribute (lbl_die, origin);
18570 else
18571 add_name_and_src_coords_attributes (lbl_die, decl);
18573 if (DECL_ABSTRACT (decl))
18574 equate_decl_number_to_die (decl, lbl_die);
18575 else
18577 insn = DECL_RTL_IF_SET (decl);
18579 /* Deleted labels are programmer specified labels which have been
18580 eliminated because of various optimizations. We still emit them
18581 here so that it is possible to put breakpoints on them. */
18582 if (insn
18583 && (LABEL_P (insn)
18584 || ((NOTE_P (insn)
18585 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18587 /* When optimization is enabled (via -O) some parts of the compiler
18588 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18589 represent source-level labels which were explicitly declared by
18590 the user. This really shouldn't be happening though, so catch
18591 it if it ever does happen. */
18592 gcc_assert (!INSN_DELETED_P (insn));
18594 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18595 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18597 else if (insn
18598 && NOTE_P (insn)
18599 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18600 && CODE_LABEL_NUMBER (insn) != -1)
18602 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18603 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18608 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18609 attributes to the DIE for a block STMT, to describe where the inlined
18610 function was called from. This is similar to add_src_coords_attributes. */
18612 static inline void
18613 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18615 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18617 if (dwarf_version >= 3 || !dwarf_strict)
18619 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18620 add_AT_unsigned (die, DW_AT_call_line, s.line);
18625 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18626 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18628 static inline void
18629 add_high_low_attributes (tree stmt, dw_die_ref die)
18631 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18633 if (BLOCK_FRAGMENT_CHAIN (stmt)
18634 && (dwarf_version >= 3 || !dwarf_strict))
18636 tree chain, superblock = NULL_TREE;
18637 dw_die_ref pdie;
18638 dw_attr_ref attr = NULL;
18640 if (inlined_function_outer_scope_p (stmt))
18642 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18643 BLOCK_NUMBER (stmt));
18644 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18647 /* Optimize duplicate .debug_ranges lists or even tails of
18648 lists. If this BLOCK has same ranges as its supercontext,
18649 lookup DW_AT_ranges attribute in the supercontext (and
18650 recursively so), verify that the ranges_table contains the
18651 right values and use it instead of adding a new .debug_range. */
18652 for (chain = stmt, pdie = die;
18653 BLOCK_SAME_RANGE (chain);
18654 chain = BLOCK_SUPERCONTEXT (chain))
18656 dw_attr_ref new_attr;
18658 pdie = pdie->die_parent;
18659 if (pdie == NULL)
18660 break;
18661 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18662 break;
18663 new_attr = get_AT (pdie, DW_AT_ranges);
18664 if (new_attr == NULL
18665 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18666 break;
18667 attr = new_attr;
18668 superblock = BLOCK_SUPERCONTEXT (chain);
18670 if (attr != NULL
18671 && (ranges_table[attr->dw_attr_val.v.val_offset
18672 / 2 / DWARF2_ADDR_SIZE].num
18673 == BLOCK_NUMBER (superblock))
18674 && BLOCK_FRAGMENT_CHAIN (superblock))
18676 unsigned long off = attr->dw_attr_val.v.val_offset
18677 / 2 / DWARF2_ADDR_SIZE;
18678 unsigned long supercnt = 0, thiscnt = 0;
18679 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18680 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18682 ++supercnt;
18683 gcc_checking_assert (ranges_table[off + supercnt].num
18684 == BLOCK_NUMBER (chain));
18686 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18687 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18688 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18689 ++thiscnt;
18690 gcc_assert (supercnt >= thiscnt);
18691 add_AT_range_list (die, DW_AT_ranges,
18692 ((off + supercnt - thiscnt)
18693 * 2 * DWARF2_ADDR_SIZE),
18694 false);
18695 return;
18698 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18700 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18703 add_ranges (chain);
18704 chain = BLOCK_FRAGMENT_CHAIN (chain);
18706 while (chain);
18707 add_ranges (NULL);
18709 else
18711 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18712 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18713 BLOCK_NUMBER (stmt));
18714 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18715 BLOCK_NUMBER (stmt));
18716 add_AT_low_high_pc (die, label, label_high, false);
18720 /* Generate a DIE for a lexical block. */
18722 static void
18723 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18725 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18727 if (call_arg_locations)
18729 if (block_map.length () <= BLOCK_NUMBER (stmt))
18730 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18731 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18734 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18735 add_high_low_attributes (stmt, stmt_die);
18737 decls_for_scope (stmt, stmt_die, depth);
18740 /* Generate a DIE for an inlined subprogram. */
18742 static void
18743 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18745 tree decl;
18747 /* The instance of function that is effectively being inlined shall not
18748 be abstract. */
18749 gcc_assert (! BLOCK_ABSTRACT (stmt));
18751 decl = block_ultimate_origin (stmt);
18753 /* Emit info for the abstract instance first, if we haven't yet. We
18754 must emit this even if the block is abstract, otherwise when we
18755 emit the block below (or elsewhere), we may end up trying to emit
18756 a die whose origin die hasn't been emitted, and crashing. */
18757 dwarf2out_abstract_function (decl);
18759 if (! BLOCK_ABSTRACT (stmt))
18761 dw_die_ref subr_die
18762 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18764 if (call_arg_locations)
18766 if (block_map.length () <= BLOCK_NUMBER (stmt))
18767 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18768 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18770 add_abstract_origin_attribute (subr_die, decl);
18771 if (TREE_ASM_WRITTEN (stmt))
18772 add_high_low_attributes (stmt, subr_die);
18773 add_call_src_coords_attributes (stmt, subr_die);
18775 decls_for_scope (stmt, subr_die, depth);
18776 current_function_has_inlines = 1;
18780 /* Generate a DIE for a field in a record, or structure. */
18782 static void
18783 gen_field_die (tree decl, dw_die_ref context_die)
18785 dw_die_ref decl_die;
18787 if (TREE_TYPE (decl) == error_mark_node)
18788 return;
18790 decl_die = new_die (DW_TAG_member, context_die, decl);
18791 add_name_and_src_coords_attributes (decl_die, decl);
18792 add_type_attribute (decl_die, member_declared_type (decl),
18793 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18794 context_die);
18796 if (DECL_BIT_FIELD_TYPE (decl))
18798 add_byte_size_attribute (decl_die, decl);
18799 add_bit_size_attribute (decl_die, decl);
18800 add_bit_offset_attribute (decl_die, decl);
18803 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18804 add_data_member_location_attribute (decl_die, decl);
18806 if (DECL_ARTIFICIAL (decl))
18807 add_AT_flag (decl_die, DW_AT_artificial, 1);
18809 add_accessibility_attribute (decl_die, decl);
18811 /* Equate decl number to die, so that we can look up this decl later on. */
18812 equate_decl_number_to_die (decl, decl_die);
18815 #if 0
18816 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18817 Use modified_type_die instead.
18818 We keep this code here just in case these types of DIEs may be needed to
18819 represent certain things in other languages (e.g. Pascal) someday. */
18821 static void
18822 gen_pointer_type_die (tree type, dw_die_ref context_die)
18824 dw_die_ref ptr_die
18825 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18827 equate_type_number_to_die (type, ptr_die);
18828 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18829 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18832 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18833 Use modified_type_die instead.
18834 We keep this code here just in case these types of DIEs may be needed to
18835 represent certain things in other languages (e.g. Pascal) someday. */
18837 static void
18838 gen_reference_type_die (tree type, dw_die_ref context_die)
18840 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18842 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18843 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18844 else
18845 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18847 equate_type_number_to_die (type, ref_die);
18848 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18849 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18851 #endif
18853 /* Generate a DIE for a pointer to a member type. */
18855 static void
18856 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18858 dw_die_ref ptr_die
18859 = new_die (DW_TAG_ptr_to_member_type,
18860 scope_die_for (type, context_die), type);
18862 equate_type_number_to_die (type, ptr_die);
18863 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18864 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18865 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18868 typedef const char *dchar_p; /* For DEF_VEC_P. */
18870 static char *producer_string;
18872 /* Return a heap allocated producer string including command line options
18873 if -grecord-gcc-switches. */
18875 static char *
18876 gen_producer_string (void)
18878 size_t j;
18879 vec<dchar_p> switches = vNULL;
18880 const char *language_string = lang_hooks.name;
18881 char *producer, *tail;
18882 const char *p;
18883 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18884 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18886 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18887 switch (save_decoded_options[j].opt_index)
18889 case OPT_o:
18890 case OPT_d:
18891 case OPT_dumpbase:
18892 case OPT_dumpdir:
18893 case OPT_auxbase:
18894 case OPT_auxbase_strip:
18895 case OPT_quiet:
18896 case OPT_version:
18897 case OPT_v:
18898 case OPT_w:
18899 case OPT_L:
18900 case OPT_D:
18901 case OPT_I:
18902 case OPT_U:
18903 case OPT_SPECIAL_unknown:
18904 case OPT_SPECIAL_ignore:
18905 case OPT_SPECIAL_program_name:
18906 case OPT_SPECIAL_input_file:
18907 case OPT_grecord_gcc_switches:
18908 case OPT_gno_record_gcc_switches:
18909 case OPT__output_pch_:
18910 case OPT_fdiagnostics_show_location_:
18911 case OPT_fdiagnostics_show_option:
18912 case OPT_fdiagnostics_show_caret:
18913 case OPT_fdiagnostics_color_:
18914 case OPT_fverbose_asm:
18915 case OPT____:
18916 case OPT__sysroot_:
18917 case OPT_nostdinc:
18918 case OPT_nostdinc__:
18919 /* Ignore these. */
18920 continue;
18921 default:
18922 if (cl_options[save_decoded_options[j].opt_index].flags
18923 & CL_NO_DWARF_RECORD)
18924 continue;
18925 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18926 == '-');
18927 switch (save_decoded_options[j].canonical_option[0][1])
18929 case 'M':
18930 case 'i':
18931 case 'W':
18932 continue;
18933 case 'f':
18934 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18935 "dump", 4) == 0)
18936 continue;
18937 break;
18938 default:
18939 break;
18941 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18942 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18943 break;
18946 producer = XNEWVEC (char, plen + 1 + len + 1);
18947 tail = producer;
18948 sprintf (tail, "%s %s", language_string, version_string);
18949 tail += plen;
18951 FOR_EACH_VEC_ELT (switches, j, p)
18953 len = strlen (p);
18954 *tail = ' ';
18955 memcpy (tail + 1, p, len);
18956 tail += len + 1;
18959 *tail = '\0';
18960 switches.release ();
18961 return producer;
18964 /* Generate the DIE for the compilation unit. */
18966 static dw_die_ref
18967 gen_compile_unit_die (const char *filename)
18969 dw_die_ref die;
18970 const char *language_string = lang_hooks.name;
18971 int language;
18973 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18975 if (filename)
18977 add_name_attribute (die, filename);
18978 /* Don't add cwd for <built-in>. */
18979 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18980 add_comp_dir_attribute (die);
18983 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
18985 /* If our producer is LTO try to figure out a common language to use
18986 from the global list of translation units. */
18987 if (strcmp (language_string, "GNU GIMPLE") == 0)
18989 unsigned i;
18990 tree t;
18991 const char *common_lang = NULL;
18993 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
18995 if (!TRANSLATION_UNIT_LANGUAGE (t))
18996 continue;
18997 if (!common_lang)
18998 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18999 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19001 else if (strncmp (common_lang, "GNU C", 5) == 0
19002 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19003 /* Mixing C and C++ is ok, use C++ in that case. */
19004 common_lang = "GNU C++";
19005 else
19007 /* Fall back to C. */
19008 common_lang = NULL;
19009 break;
19013 if (common_lang)
19014 language_string = common_lang;
19017 language = DW_LANG_C89;
19018 if (strcmp (language_string, "GNU C++") == 0)
19019 language = DW_LANG_C_plus_plus;
19020 else if (strcmp (language_string, "GNU F77") == 0)
19021 language = DW_LANG_Fortran77;
19022 else if (strcmp (language_string, "GNU Pascal") == 0)
19023 language = DW_LANG_Pascal83;
19024 else if (dwarf_version >= 3 || !dwarf_strict)
19026 if (strcmp (language_string, "GNU Ada") == 0)
19027 language = DW_LANG_Ada95;
19028 else if (strcmp (language_string, "GNU Fortran") == 0)
19029 language = DW_LANG_Fortran95;
19030 else if (strcmp (language_string, "GNU Java") == 0)
19031 language = DW_LANG_Java;
19032 else if (strcmp (language_string, "GNU Objective-C") == 0)
19033 language = DW_LANG_ObjC;
19034 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19035 language = DW_LANG_ObjC_plus_plus;
19036 else if (dwarf_version >= 5 || !dwarf_strict)
19038 if (strcmp (language_string, "GNU Go") == 0)
19039 language = DW_LANG_Go;
19042 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19043 else if (strcmp (language_string, "GNU Fortran") == 0)
19044 language = DW_LANG_Fortran90;
19046 add_AT_unsigned (die, DW_AT_language, language);
19048 switch (language)
19050 case DW_LANG_Fortran77:
19051 case DW_LANG_Fortran90:
19052 case DW_LANG_Fortran95:
19053 /* Fortran has case insensitive identifiers and the front-end
19054 lowercases everything. */
19055 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19056 break;
19057 default:
19058 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19059 break;
19061 return die;
19064 /* Generate the DIE for a base class. */
19066 static void
19067 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19069 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19071 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19072 add_data_member_location_attribute (die, binfo);
19074 if (BINFO_VIRTUAL_P (binfo))
19075 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19077 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19078 children, otherwise the default is DW_ACCESS_public. In DWARF2
19079 the default has always been DW_ACCESS_private. */
19080 if (access == access_public_node)
19082 if (dwarf_version == 2
19083 || context_die->die_tag == DW_TAG_class_type)
19084 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19086 else if (access == access_protected_node)
19087 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19088 else if (dwarf_version > 2
19089 && context_die->die_tag != DW_TAG_class_type)
19090 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19093 /* Generate a DIE for a class member. */
19095 static void
19096 gen_member_die (tree type, dw_die_ref context_die)
19098 tree member;
19099 tree binfo = TYPE_BINFO (type);
19100 dw_die_ref child;
19102 /* If this is not an incomplete type, output descriptions of each of its
19103 members. Note that as we output the DIEs necessary to represent the
19104 members of this record or union type, we will also be trying to output
19105 DIEs to represent the *types* of those members. However the `type'
19106 function (above) will specifically avoid generating type DIEs for member
19107 types *within* the list of member DIEs for this (containing) type except
19108 for those types (of members) which are explicitly marked as also being
19109 members of this (containing) type themselves. The g++ front- end can
19110 force any given type to be treated as a member of some other (containing)
19111 type by setting the TYPE_CONTEXT of the given (member) type to point to
19112 the TREE node representing the appropriate (containing) type. */
19114 /* First output info about the base classes. */
19115 if (binfo)
19117 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19118 int i;
19119 tree base;
19121 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19122 gen_inheritance_die (base,
19123 (accesses ? (*accesses)[i] : access_public_node),
19124 context_die);
19127 /* Now output info about the data members and type members. */
19128 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19130 /* If we thought we were generating minimal debug info for TYPE
19131 and then changed our minds, some of the member declarations
19132 may have already been defined. Don't define them again, but
19133 do put them in the right order. */
19135 child = lookup_decl_die (member);
19136 if (child)
19137 splice_child_die (context_die, child);
19138 else
19139 gen_decl_die (member, NULL, context_die);
19142 /* Now output info about the function members (if any). */
19143 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19145 /* Don't include clones in the member list. */
19146 if (DECL_ABSTRACT_ORIGIN (member))
19147 continue;
19149 child = lookup_decl_die (member);
19150 if (child)
19151 splice_child_die (context_die, child);
19152 else
19153 gen_decl_die (member, NULL, context_die);
19157 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19158 is set, we pretend that the type was never defined, so we only get the
19159 member DIEs needed by later specification DIEs. */
19161 static void
19162 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19163 enum debug_info_usage usage)
19165 dw_die_ref type_die = lookup_type_die (type);
19166 dw_die_ref scope_die = 0;
19167 int nested = 0;
19168 int complete = (TYPE_SIZE (type)
19169 && (! TYPE_STUB_DECL (type)
19170 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19171 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19172 complete = complete && should_emit_struct_debug (type, usage);
19174 if (type_die && ! complete)
19175 return;
19177 if (TYPE_CONTEXT (type) != NULL_TREE
19178 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19179 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19180 nested = 1;
19182 scope_die = scope_die_for (type, context_die);
19184 /* Generate child dies for template paramaters. */
19185 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19186 schedule_generic_params_dies_gen (type);
19188 if (! type_die || (nested && is_cu_die (scope_die)))
19189 /* First occurrence of type or toplevel definition of nested class. */
19191 dw_die_ref old_die = type_die;
19193 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19194 ? record_type_tag (type) : DW_TAG_union_type,
19195 scope_die, type);
19196 equate_type_number_to_die (type, type_die);
19197 if (old_die)
19198 add_AT_specification (type_die, old_die);
19199 else
19200 add_name_attribute (type_die, type_tag (type));
19202 else
19203 remove_AT (type_die, DW_AT_declaration);
19205 /* If this type has been completed, then give it a byte_size attribute and
19206 then give a list of members. */
19207 if (complete && !ns_decl)
19209 /* Prevent infinite recursion in cases where the type of some member of
19210 this type is expressed in terms of this type itself. */
19211 TREE_ASM_WRITTEN (type) = 1;
19212 add_byte_size_attribute (type_die, type);
19213 if (TYPE_STUB_DECL (type) != NULL_TREE)
19215 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19216 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19219 /* If the first reference to this type was as the return type of an
19220 inline function, then it may not have a parent. Fix this now. */
19221 if (type_die->die_parent == NULL)
19222 add_child_die (scope_die, type_die);
19224 push_decl_scope (type);
19225 gen_member_die (type, type_die);
19226 pop_decl_scope ();
19228 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19229 if (TYPE_ARTIFICIAL (type))
19230 add_AT_flag (type_die, DW_AT_artificial, 1);
19232 /* GNU extension: Record what type our vtable lives in. */
19233 if (TYPE_VFIELD (type))
19235 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19237 gen_type_die (vtype, context_die);
19238 add_AT_die_ref (type_die, DW_AT_containing_type,
19239 lookup_type_die (vtype));
19242 else
19244 add_AT_flag (type_die, DW_AT_declaration, 1);
19246 /* We don't need to do this for function-local types. */
19247 if (TYPE_STUB_DECL (type)
19248 && ! decl_function_context (TYPE_STUB_DECL (type)))
19249 vec_safe_push (incomplete_types, type);
19252 if (get_AT (type_die, DW_AT_name))
19253 add_pubtype (type, type_die);
19256 /* Generate a DIE for a subroutine _type_. */
19258 static void
19259 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19261 tree return_type = TREE_TYPE (type);
19262 dw_die_ref subr_die
19263 = new_die (DW_TAG_subroutine_type,
19264 scope_die_for (type, context_die), type);
19266 equate_type_number_to_die (type, subr_die);
19267 add_prototyped_attribute (subr_die, type);
19268 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19269 gen_formal_types_die (type, subr_die);
19271 if (get_AT (subr_die, DW_AT_name))
19272 add_pubtype (type, subr_die);
19275 /* Generate a DIE for a type definition. */
19277 static void
19278 gen_typedef_die (tree decl, dw_die_ref context_die)
19280 dw_die_ref type_die;
19281 tree origin;
19283 if (TREE_ASM_WRITTEN (decl))
19284 return;
19286 TREE_ASM_WRITTEN (decl) = 1;
19287 type_die = new_die (DW_TAG_typedef, context_die, decl);
19288 origin = decl_ultimate_origin (decl);
19289 if (origin != NULL)
19290 add_abstract_origin_attribute (type_die, origin);
19291 else
19293 tree type;
19295 add_name_and_src_coords_attributes (type_die, decl);
19296 if (DECL_ORIGINAL_TYPE (decl))
19298 type = DECL_ORIGINAL_TYPE (decl);
19300 gcc_assert (type != TREE_TYPE (decl));
19301 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19303 else
19305 type = TREE_TYPE (decl);
19307 if (is_naming_typedef_decl (TYPE_NAME (type)))
19309 /* Here, we are in the case of decl being a typedef naming
19310 an anonymous type, e.g:
19311 typedef struct {...} foo;
19312 In that case TREE_TYPE (decl) is not a typedef variant
19313 type and TYPE_NAME of the anonymous type is set to the
19314 TYPE_DECL of the typedef. This construct is emitted by
19315 the C++ FE.
19317 TYPE is the anonymous struct named by the typedef
19318 DECL. As we need the DW_AT_type attribute of the
19319 DW_TAG_typedef to point to the DIE of TYPE, let's
19320 generate that DIE right away. add_type_attribute
19321 called below will then pick (via lookup_type_die) that
19322 anonymous struct DIE. */
19323 if (!TREE_ASM_WRITTEN (type))
19324 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19326 /* This is a GNU Extension. We are adding a
19327 DW_AT_linkage_name attribute to the DIE of the
19328 anonymous struct TYPE. The value of that attribute
19329 is the name of the typedef decl naming the anonymous
19330 struct. This greatly eases the work of consumers of
19331 this debug info. */
19332 add_linkage_attr (lookup_type_die (type), decl);
19336 add_type_attribute (type_die, type, TREE_READONLY (decl),
19337 TREE_THIS_VOLATILE (decl), context_die);
19339 if (is_naming_typedef_decl (decl))
19340 /* We want that all subsequent calls to lookup_type_die with
19341 TYPE in argument yield the DW_TAG_typedef we have just
19342 created. */
19343 equate_type_number_to_die (type, type_die);
19345 add_accessibility_attribute (type_die, decl);
19348 if (DECL_ABSTRACT (decl))
19349 equate_decl_number_to_die (decl, type_die);
19351 if (get_AT (type_die, DW_AT_name))
19352 add_pubtype (decl, type_die);
19355 /* Generate a DIE for a struct, class, enum or union type. */
19357 static void
19358 gen_tagged_type_die (tree type,
19359 dw_die_ref context_die,
19360 enum debug_info_usage usage)
19362 int need_pop;
19364 if (type == NULL_TREE
19365 || !is_tagged_type (type))
19366 return;
19368 /* If this is a nested type whose containing class hasn't been written
19369 out yet, writing it out will cover this one, too. This does not apply
19370 to instantiations of member class templates; they need to be added to
19371 the containing class as they are generated. FIXME: This hurts the
19372 idea of combining type decls from multiple TUs, since we can't predict
19373 what set of template instantiations we'll get. */
19374 if (TYPE_CONTEXT (type)
19375 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19376 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19378 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19380 if (TREE_ASM_WRITTEN (type))
19381 return;
19383 /* If that failed, attach ourselves to the stub. */
19384 push_decl_scope (TYPE_CONTEXT (type));
19385 context_die = lookup_type_die (TYPE_CONTEXT (type));
19386 need_pop = 1;
19388 else if (TYPE_CONTEXT (type) != NULL_TREE
19389 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19391 /* If this type is local to a function that hasn't been written
19392 out yet, use a NULL context for now; it will be fixed up in
19393 decls_for_scope. */
19394 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19395 /* A declaration DIE doesn't count; nested types need to go in the
19396 specification. */
19397 if (context_die && is_declaration_die (context_die))
19398 context_die = NULL;
19399 need_pop = 0;
19401 else
19403 context_die = declare_in_namespace (type, context_die);
19404 need_pop = 0;
19407 if (TREE_CODE (type) == ENUMERAL_TYPE)
19409 /* This might have been written out by the call to
19410 declare_in_namespace. */
19411 if (!TREE_ASM_WRITTEN (type))
19412 gen_enumeration_type_die (type, context_die);
19414 else
19415 gen_struct_or_union_type_die (type, context_die, usage);
19417 if (need_pop)
19418 pop_decl_scope ();
19420 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19421 it up if it is ever completed. gen_*_type_die will set it for us
19422 when appropriate. */
19425 /* Generate a type description DIE. */
19427 static void
19428 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19429 enum debug_info_usage usage)
19431 struct array_descr_info info;
19433 if (type == NULL_TREE || type == error_mark_node)
19434 return;
19436 if (TYPE_NAME (type) != NULL_TREE
19437 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19438 && is_redundant_typedef (TYPE_NAME (type))
19439 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19440 /* The DECL of this type is a typedef we don't want to emit debug
19441 info for but we want debug info for its underlying typedef.
19442 This can happen for e.g, the injected-class-name of a C++
19443 type. */
19444 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19446 /* If TYPE is a typedef type variant, let's generate debug info
19447 for the parent typedef which TYPE is a type of. */
19448 if (typedef_variant_p (type))
19450 if (TREE_ASM_WRITTEN (type))
19451 return;
19453 /* Prevent broken recursion; we can't hand off to the same type. */
19454 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19456 /* Give typedefs the right scope. */
19457 context_die = scope_die_for (type, context_die);
19459 TREE_ASM_WRITTEN (type) = 1;
19461 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19462 return;
19465 /* If type is an anonymous tagged type named by a typedef, let's
19466 generate debug info for the typedef. */
19467 if (is_naming_typedef_decl (TYPE_NAME (type)))
19469 /* Use the DIE of the containing namespace as the parent DIE of
19470 the type description DIE we want to generate. */
19471 if (DECL_CONTEXT (TYPE_NAME (type))
19472 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19473 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19475 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19476 return;
19479 /* If this is an array type with hidden descriptor, handle it first. */
19480 if (!TREE_ASM_WRITTEN (type)
19481 && lang_hooks.types.get_array_descr_info
19482 && lang_hooks.types.get_array_descr_info (type, &info)
19483 && (dwarf_version >= 3 || !dwarf_strict))
19485 gen_descr_array_type_die (type, &info, context_die);
19486 TREE_ASM_WRITTEN (type) = 1;
19487 return;
19490 /* We are going to output a DIE to represent the unqualified version
19491 of this type (i.e. without any const or volatile qualifiers) so
19492 get the main variant (i.e. the unqualified version) of this type
19493 now. (Vectors are special because the debugging info is in the
19494 cloned type itself). */
19495 if (TREE_CODE (type) != VECTOR_TYPE)
19496 type = type_main_variant (type);
19498 if (TREE_ASM_WRITTEN (type))
19499 return;
19501 switch (TREE_CODE (type))
19503 case ERROR_MARK:
19504 break;
19506 case POINTER_TYPE:
19507 case REFERENCE_TYPE:
19508 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19509 ensures that the gen_type_die recursion will terminate even if the
19510 type is recursive. Recursive types are possible in Ada. */
19511 /* ??? We could perhaps do this for all types before the switch
19512 statement. */
19513 TREE_ASM_WRITTEN (type) = 1;
19515 /* For these types, all that is required is that we output a DIE (or a
19516 set of DIEs) to represent the "basis" type. */
19517 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19518 DINFO_USAGE_IND_USE);
19519 break;
19521 case OFFSET_TYPE:
19522 /* This code is used for C++ pointer-to-data-member types.
19523 Output a description of the relevant class type. */
19524 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19525 DINFO_USAGE_IND_USE);
19527 /* Output a description of the type of the object pointed to. */
19528 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19529 DINFO_USAGE_IND_USE);
19531 /* Now output a DIE to represent this pointer-to-data-member type
19532 itself. */
19533 gen_ptr_to_mbr_type_die (type, context_die);
19534 break;
19536 case FUNCTION_TYPE:
19537 /* Force out return type (in case it wasn't forced out already). */
19538 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19539 DINFO_USAGE_DIR_USE);
19540 gen_subroutine_type_die (type, context_die);
19541 break;
19543 case METHOD_TYPE:
19544 /* Force out return type (in case it wasn't forced out already). */
19545 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19546 DINFO_USAGE_DIR_USE);
19547 gen_subroutine_type_die (type, context_die);
19548 break;
19550 case ARRAY_TYPE:
19551 gen_array_type_die (type, context_die);
19552 break;
19554 case VECTOR_TYPE:
19555 gen_array_type_die (type, context_die);
19556 break;
19558 case ENUMERAL_TYPE:
19559 case RECORD_TYPE:
19560 case UNION_TYPE:
19561 case QUAL_UNION_TYPE:
19562 gen_tagged_type_die (type, context_die, usage);
19563 return;
19565 case VOID_TYPE:
19566 case INTEGER_TYPE:
19567 case REAL_TYPE:
19568 case FIXED_POINT_TYPE:
19569 case COMPLEX_TYPE:
19570 case BOOLEAN_TYPE:
19571 /* No DIEs needed for fundamental types. */
19572 break;
19574 case NULLPTR_TYPE:
19575 case LANG_TYPE:
19576 /* Just use DW_TAG_unspecified_type. */
19578 dw_die_ref type_die = lookup_type_die (type);
19579 if (type_die == NULL)
19581 tree name = TYPE_NAME (type);
19582 if (TREE_CODE (name) == TYPE_DECL)
19583 name = DECL_NAME (name);
19584 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19585 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19586 equate_type_number_to_die (type, type_die);
19589 break;
19591 default:
19592 gcc_unreachable ();
19595 TREE_ASM_WRITTEN (type) = 1;
19598 static void
19599 gen_type_die (tree type, dw_die_ref context_die)
19601 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19604 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19605 things which are local to the given block. */
19607 static void
19608 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19610 int must_output_die = 0;
19611 bool inlined_func;
19613 /* Ignore blocks that are NULL. */
19614 if (stmt == NULL_TREE)
19615 return;
19617 inlined_func = inlined_function_outer_scope_p (stmt);
19619 /* If the block is one fragment of a non-contiguous block, do not
19620 process the variables, since they will have been done by the
19621 origin block. Do process subblocks. */
19622 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19624 tree sub;
19626 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19627 gen_block_die (sub, context_die, depth + 1);
19629 return;
19632 /* Determine if we need to output any Dwarf DIEs at all to represent this
19633 block. */
19634 if (inlined_func)
19635 /* The outer scopes for inlinings *must* always be represented. We
19636 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19637 must_output_die = 1;
19638 else
19640 /* Determine if this block directly contains any "significant"
19641 local declarations which we will need to output DIEs for. */
19642 if (debug_info_level > DINFO_LEVEL_TERSE)
19643 /* We are not in terse mode so *any* local declaration counts
19644 as being a "significant" one. */
19645 must_output_die = ((BLOCK_VARS (stmt) != NULL
19646 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19647 && (TREE_USED (stmt)
19648 || TREE_ASM_WRITTEN (stmt)
19649 || BLOCK_ABSTRACT (stmt)));
19650 else if ((TREE_USED (stmt)
19651 || TREE_ASM_WRITTEN (stmt)
19652 || BLOCK_ABSTRACT (stmt))
19653 && !dwarf2out_ignore_block (stmt))
19654 must_output_die = 1;
19657 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19658 DIE for any block which contains no significant local declarations at
19659 all. Rather, in such cases we just call `decls_for_scope' so that any
19660 needed Dwarf info for any sub-blocks will get properly generated. Note
19661 that in terse mode, our definition of what constitutes a "significant"
19662 local declaration gets restricted to include only inlined function
19663 instances and local (nested) function definitions. */
19664 if (must_output_die)
19666 if (inlined_func)
19668 /* If STMT block is abstract, that means we have been called
19669 indirectly from dwarf2out_abstract_function.
19670 That function rightfully marks the descendent blocks (of
19671 the abstract function it is dealing with) as being abstract,
19672 precisely to prevent us from emitting any
19673 DW_TAG_inlined_subroutine DIE as a descendent
19674 of an abstract function instance. So in that case, we should
19675 not call gen_inlined_subroutine_die.
19677 Later though, when cgraph asks dwarf2out to emit info
19678 for the concrete instance of the function decl into which
19679 the concrete instance of STMT got inlined, the later will lead
19680 to the generation of a DW_TAG_inlined_subroutine DIE. */
19681 if (! BLOCK_ABSTRACT (stmt))
19682 gen_inlined_subroutine_die (stmt, context_die, depth);
19684 else
19685 gen_lexical_block_die (stmt, context_die, depth);
19687 else
19688 decls_for_scope (stmt, context_die, depth);
19691 /* Process variable DECL (or variable with origin ORIGIN) within
19692 block STMT and add it to CONTEXT_DIE. */
19693 static void
19694 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19696 dw_die_ref die;
19697 tree decl_or_origin = decl ? decl : origin;
19699 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19700 die = lookup_decl_die (decl_or_origin);
19701 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19702 && TYPE_DECL_IS_STUB (decl_or_origin))
19703 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19704 else
19705 die = NULL;
19707 if (die != NULL && die->die_parent == NULL)
19708 add_child_die (context_die, die);
19709 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19710 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19711 stmt, context_die);
19712 else
19713 gen_decl_die (decl, origin, context_die);
19716 /* Generate all of the decls declared within a given scope and (recursively)
19717 all of its sub-blocks. */
19719 static void
19720 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19722 tree decl;
19723 unsigned int i;
19724 tree subblocks;
19726 /* Ignore NULL blocks. */
19727 if (stmt == NULL_TREE)
19728 return;
19730 /* Output the DIEs to represent all of the data objects and typedefs
19731 declared directly within this block but not within any nested
19732 sub-blocks. Also, nested function and tag DIEs have been
19733 generated with a parent of NULL; fix that up now. */
19734 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19735 process_scope_var (stmt, decl, NULL_TREE, context_die);
19736 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19737 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19738 context_die);
19740 /* If we're at -g1, we're not interested in subblocks. */
19741 if (debug_info_level <= DINFO_LEVEL_TERSE)
19742 return;
19744 /* Output the DIEs to represent all sub-blocks (and the items declared
19745 therein) of this block. */
19746 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19747 subblocks != NULL;
19748 subblocks = BLOCK_CHAIN (subblocks))
19749 gen_block_die (subblocks, context_die, depth + 1);
19752 /* Is this a typedef we can avoid emitting? */
19754 static inline int
19755 is_redundant_typedef (const_tree decl)
19757 if (TYPE_DECL_IS_STUB (decl))
19758 return 1;
19760 if (DECL_ARTIFICIAL (decl)
19761 && DECL_CONTEXT (decl)
19762 && is_tagged_type (DECL_CONTEXT (decl))
19763 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19764 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19765 /* Also ignore the artificial member typedef for the class name. */
19766 return 1;
19768 return 0;
19771 /* Return TRUE if TYPE is a typedef that names a type for linkage
19772 purposes. This kind of typedefs is produced by the C++ FE for
19773 constructs like:
19775 typedef struct {...} foo;
19777 In that case, there is no typedef variant type produced for foo.
19778 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19779 struct type. */
19781 static bool
19782 is_naming_typedef_decl (const_tree decl)
19784 if (decl == NULL_TREE
19785 || TREE_CODE (decl) != TYPE_DECL
19786 || !is_tagged_type (TREE_TYPE (decl))
19787 || DECL_IS_BUILTIN (decl)
19788 || is_redundant_typedef (decl)
19789 /* It looks like Ada produces TYPE_DECLs that are very similar
19790 to C++ naming typedefs but that have different
19791 semantics. Let's be specific to c++ for now. */
19792 || !is_cxx ())
19793 return FALSE;
19795 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19796 && TYPE_NAME (TREE_TYPE (decl)) == decl
19797 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19798 != TYPE_NAME (TREE_TYPE (decl))));
19801 /* Returns the DIE for a context. */
19803 static inline dw_die_ref
19804 get_context_die (tree context)
19806 if (context)
19808 /* Find die that represents this context. */
19809 if (TYPE_P (context))
19811 context = TYPE_MAIN_VARIANT (context);
19812 return strip_naming_typedef (context, force_type_die (context));
19814 else
19815 return force_decl_die (context);
19817 return comp_unit_die ();
19820 /* Returns the DIE for decl. A DIE will always be returned. */
19822 static dw_die_ref
19823 force_decl_die (tree decl)
19825 dw_die_ref decl_die;
19826 unsigned saved_external_flag;
19827 tree save_fn = NULL_TREE;
19828 decl_die = lookup_decl_die (decl);
19829 if (!decl_die)
19831 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19833 decl_die = lookup_decl_die (decl);
19834 if (decl_die)
19835 return decl_die;
19837 switch (TREE_CODE (decl))
19839 case FUNCTION_DECL:
19840 /* Clear current_function_decl, so that gen_subprogram_die thinks
19841 that this is a declaration. At this point, we just want to force
19842 declaration die. */
19843 save_fn = current_function_decl;
19844 current_function_decl = NULL_TREE;
19845 gen_subprogram_die (decl, context_die);
19846 current_function_decl = save_fn;
19847 break;
19849 case VAR_DECL:
19850 /* Set external flag to force declaration die. Restore it after
19851 gen_decl_die() call. */
19852 saved_external_flag = DECL_EXTERNAL (decl);
19853 DECL_EXTERNAL (decl) = 1;
19854 gen_decl_die (decl, NULL, context_die);
19855 DECL_EXTERNAL (decl) = saved_external_flag;
19856 break;
19858 case NAMESPACE_DECL:
19859 if (dwarf_version >= 3 || !dwarf_strict)
19860 dwarf2out_decl (decl);
19861 else
19862 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19863 decl_die = comp_unit_die ();
19864 break;
19866 case TRANSLATION_UNIT_DECL:
19867 decl_die = comp_unit_die ();
19868 break;
19870 default:
19871 gcc_unreachable ();
19874 /* We should be able to find the DIE now. */
19875 if (!decl_die)
19876 decl_die = lookup_decl_die (decl);
19877 gcc_assert (decl_die);
19880 return decl_die;
19883 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19884 always returned. */
19886 static dw_die_ref
19887 force_type_die (tree type)
19889 dw_die_ref type_die;
19891 type_die = lookup_type_die (type);
19892 if (!type_die)
19894 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19896 type_die = modified_type_die (type, TYPE_READONLY (type),
19897 TYPE_VOLATILE (type), context_die);
19898 gcc_assert (type_die);
19900 return type_die;
19903 /* Force out any required namespaces to be able to output DECL,
19904 and return the new context_die for it, if it's changed. */
19906 static dw_die_ref
19907 setup_namespace_context (tree thing, dw_die_ref context_die)
19909 tree context = (DECL_P (thing)
19910 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19911 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19912 /* Force out the namespace. */
19913 context_die = force_decl_die (context);
19915 return context_die;
19918 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19919 type) within its namespace, if appropriate.
19921 For compatibility with older debuggers, namespace DIEs only contain
19922 declarations; all definitions are emitted at CU scope. */
19924 static dw_die_ref
19925 declare_in_namespace (tree thing, dw_die_ref context_die)
19927 dw_die_ref ns_context;
19929 if (debug_info_level <= DINFO_LEVEL_TERSE)
19930 return context_die;
19932 /* If this decl is from an inlined function, then don't try to emit it in its
19933 namespace, as we will get confused. It would have already been emitted
19934 when the abstract instance of the inline function was emitted anyways. */
19935 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19936 return context_die;
19938 ns_context = setup_namespace_context (thing, context_die);
19940 if (ns_context != context_die)
19942 if (is_fortran ())
19943 return ns_context;
19944 if (DECL_P (thing))
19945 gen_decl_die (thing, NULL, ns_context);
19946 else
19947 gen_type_die (thing, ns_context);
19949 return context_die;
19952 /* Generate a DIE for a namespace or namespace alias. */
19954 static void
19955 gen_namespace_die (tree decl, dw_die_ref context_die)
19957 dw_die_ref namespace_die;
19959 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19960 they are an alias of. */
19961 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19963 /* Output a real namespace or module. */
19964 context_die = setup_namespace_context (decl, comp_unit_die ());
19965 namespace_die = new_die (is_fortran ()
19966 ? DW_TAG_module : DW_TAG_namespace,
19967 context_die, decl);
19968 /* For Fortran modules defined in different CU don't add src coords. */
19969 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19971 const char *name = dwarf2_name (decl, 0);
19972 if (name)
19973 add_name_attribute (namespace_die, name);
19975 else
19976 add_name_and_src_coords_attributes (namespace_die, decl);
19977 if (DECL_EXTERNAL (decl))
19978 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19979 equate_decl_number_to_die (decl, namespace_die);
19981 else
19983 /* Output a namespace alias. */
19985 /* Force out the namespace we are an alias of, if necessary. */
19986 dw_die_ref origin_die
19987 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19989 if (DECL_FILE_SCOPE_P (decl)
19990 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19991 context_die = setup_namespace_context (decl, comp_unit_die ());
19992 /* Now create the namespace alias DIE. */
19993 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19994 add_name_and_src_coords_attributes (namespace_die, decl);
19995 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19996 equate_decl_number_to_die (decl, namespace_die);
19998 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19999 if (want_pubnames ())
20000 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20003 /* Generate Dwarf debug information for a decl described by DECL.
20004 The return value is currently only meaningful for PARM_DECLs,
20005 for all other decls it returns NULL. */
20007 static dw_die_ref
20008 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20010 tree decl_or_origin = decl ? decl : origin;
20011 tree class_origin = NULL, ultimate_origin;
20013 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20014 return NULL;
20016 switch (TREE_CODE (decl_or_origin))
20018 case ERROR_MARK:
20019 break;
20021 case CONST_DECL:
20022 if (!is_fortran () && !is_ada ())
20024 /* The individual enumerators of an enum type get output when we output
20025 the Dwarf representation of the relevant enum type itself. */
20026 break;
20029 /* Emit its type. */
20030 gen_type_die (TREE_TYPE (decl), context_die);
20032 /* And its containing namespace. */
20033 context_die = declare_in_namespace (decl, context_die);
20035 gen_const_die (decl, context_die);
20036 break;
20038 case FUNCTION_DECL:
20039 /* Don't output any DIEs to represent mere function declarations,
20040 unless they are class members or explicit block externs. */
20041 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20042 && DECL_FILE_SCOPE_P (decl_or_origin)
20043 && (current_function_decl == NULL_TREE
20044 || DECL_ARTIFICIAL (decl_or_origin)))
20045 break;
20047 #if 0
20048 /* FIXME */
20049 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20050 on local redeclarations of global functions. That seems broken. */
20051 if (current_function_decl != decl)
20052 /* This is only a declaration. */;
20053 #endif
20055 /* If we're emitting a clone, emit info for the abstract instance. */
20056 if (origin || DECL_ORIGIN (decl) != decl)
20057 dwarf2out_abstract_function (origin
20058 ? DECL_ORIGIN (origin)
20059 : DECL_ABSTRACT_ORIGIN (decl));
20061 /* If we're emitting an out-of-line copy of an inline function,
20062 emit info for the abstract instance and set up to refer to it. */
20063 else if (cgraph_function_possibly_inlined_p (decl)
20064 && ! DECL_ABSTRACT (decl)
20065 && ! class_or_namespace_scope_p (context_die)
20066 /* dwarf2out_abstract_function won't emit a die if this is just
20067 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20068 that case, because that works only if we have a die. */
20069 && DECL_INITIAL (decl) != NULL_TREE)
20071 dwarf2out_abstract_function (decl);
20072 set_decl_origin_self (decl);
20075 /* Otherwise we're emitting the primary DIE for this decl. */
20076 else if (debug_info_level > DINFO_LEVEL_TERSE)
20078 /* Before we describe the FUNCTION_DECL itself, make sure that we
20079 have its containing type. */
20080 if (!origin)
20081 origin = decl_class_context (decl);
20082 if (origin != NULL_TREE)
20083 gen_type_die (origin, context_die);
20085 /* And its return type. */
20086 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20088 /* And its virtual context. */
20089 if (DECL_VINDEX (decl) != NULL_TREE)
20090 gen_type_die (DECL_CONTEXT (decl), context_die);
20092 /* Make sure we have a member DIE for decl. */
20093 if (origin != NULL_TREE)
20094 gen_type_die_for_member (origin, decl, context_die);
20096 /* And its containing namespace. */
20097 context_die = declare_in_namespace (decl, context_die);
20100 /* Now output a DIE to represent the function itself. */
20101 if (decl)
20102 gen_subprogram_die (decl, context_die);
20103 break;
20105 case TYPE_DECL:
20106 /* If we are in terse mode, don't generate any DIEs to represent any
20107 actual typedefs. */
20108 if (debug_info_level <= DINFO_LEVEL_TERSE)
20109 break;
20111 /* In the special case of a TYPE_DECL node representing the declaration
20112 of some type tag, if the given TYPE_DECL is marked as having been
20113 instantiated from some other (original) TYPE_DECL node (e.g. one which
20114 was generated within the original definition of an inline function) we
20115 used to generate a special (abbreviated) DW_TAG_structure_type,
20116 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20117 should be actually referencing those DIEs, as variable DIEs with that
20118 type would be emitted already in the abstract origin, so it was always
20119 removed during unused type prunning. Don't add anything in this
20120 case. */
20121 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20122 break;
20124 if (is_redundant_typedef (decl))
20125 gen_type_die (TREE_TYPE (decl), context_die);
20126 else
20127 /* Output a DIE to represent the typedef itself. */
20128 gen_typedef_die (decl, context_die);
20129 break;
20131 case LABEL_DECL:
20132 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20133 gen_label_die (decl, context_die);
20134 break;
20136 case VAR_DECL:
20137 case RESULT_DECL:
20138 /* If we are in terse mode, don't generate any DIEs to represent any
20139 variable declarations or definitions. */
20140 if (debug_info_level <= DINFO_LEVEL_TERSE)
20141 break;
20143 /* Output any DIEs that are needed to specify the type of this data
20144 object. */
20145 if (decl_by_reference_p (decl_or_origin))
20146 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20147 else
20148 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20150 /* And its containing type. */
20151 class_origin = decl_class_context (decl_or_origin);
20152 if (class_origin != NULL_TREE)
20153 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20155 /* And its containing namespace. */
20156 context_die = declare_in_namespace (decl_or_origin, context_die);
20158 /* Now output the DIE to represent the data object itself. This gets
20159 complicated because of the possibility that the VAR_DECL really
20160 represents an inlined instance of a formal parameter for an inline
20161 function. */
20162 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20163 if (ultimate_origin != NULL_TREE
20164 && TREE_CODE (ultimate_origin) == PARM_DECL)
20165 gen_formal_parameter_die (decl, origin,
20166 true /* Emit name attribute. */,
20167 context_die);
20168 else
20169 gen_variable_die (decl, origin, context_die);
20170 break;
20172 case FIELD_DECL:
20173 /* Ignore the nameless fields that are used to skip bits but handle C++
20174 anonymous unions and structs. */
20175 if (DECL_NAME (decl) != NULL_TREE
20176 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20177 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20179 gen_type_die (member_declared_type (decl), context_die);
20180 gen_field_die (decl, context_die);
20182 break;
20184 case PARM_DECL:
20185 if (DECL_BY_REFERENCE (decl_or_origin))
20186 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20187 else
20188 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20189 return gen_formal_parameter_die (decl, origin,
20190 true /* Emit name attribute. */,
20191 context_die);
20193 case NAMESPACE_DECL:
20194 case IMPORTED_DECL:
20195 if (dwarf_version >= 3 || !dwarf_strict)
20196 gen_namespace_die (decl, context_die);
20197 break;
20199 default:
20200 /* Probably some frontend-internal decl. Assume we don't care. */
20201 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20202 break;
20205 return NULL;
20208 /* Output debug information for global decl DECL. Called from toplev.c after
20209 compilation proper has finished. */
20211 static void
20212 dwarf2out_global_decl (tree decl)
20214 /* Output DWARF2 information for file-scope tentative data object
20215 declarations, file-scope (extern) function declarations (which
20216 had no corresponding body) and file-scope tagged type declarations
20217 and definitions which have not yet been forced out. */
20218 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20219 dwarf2out_decl (decl);
20222 /* Output debug information for type decl DECL. Called from toplev.c
20223 and from language front ends (to record built-in types). */
20224 static void
20225 dwarf2out_type_decl (tree decl, int local)
20227 if (!local)
20228 dwarf2out_decl (decl);
20231 /* Output debug information for imported module or decl DECL.
20232 NAME is non-NULL name in the lexical block if the decl has been renamed.
20233 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20234 that DECL belongs to.
20235 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20236 static void
20237 dwarf2out_imported_module_or_decl_1 (tree decl,
20238 tree name,
20239 tree lexical_block,
20240 dw_die_ref lexical_block_die)
20242 expanded_location xloc;
20243 dw_die_ref imported_die = NULL;
20244 dw_die_ref at_import_die;
20246 if (TREE_CODE (decl) == IMPORTED_DECL)
20248 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20249 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20250 gcc_assert (decl);
20252 else
20253 xloc = expand_location (input_location);
20255 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20257 at_import_die = force_type_die (TREE_TYPE (decl));
20258 /* For namespace N { typedef void T; } using N::T; base_type_die
20259 returns NULL, but DW_TAG_imported_declaration requires
20260 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20261 if (!at_import_die)
20263 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20264 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20265 at_import_die = lookup_type_die (TREE_TYPE (decl));
20266 gcc_assert (at_import_die);
20269 else
20271 at_import_die = lookup_decl_die (decl);
20272 if (!at_import_die)
20274 /* If we're trying to avoid duplicate debug info, we may not have
20275 emitted the member decl for this field. Emit it now. */
20276 if (TREE_CODE (decl) == FIELD_DECL)
20278 tree type = DECL_CONTEXT (decl);
20280 if (TYPE_CONTEXT (type)
20281 && TYPE_P (TYPE_CONTEXT (type))
20282 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20283 DINFO_USAGE_DIR_USE))
20284 return;
20285 gen_type_die_for_member (type, decl,
20286 get_context_die (TYPE_CONTEXT (type)));
20288 at_import_die = force_decl_die (decl);
20292 if (TREE_CODE (decl) == NAMESPACE_DECL)
20294 if (dwarf_version >= 3 || !dwarf_strict)
20295 imported_die = new_die (DW_TAG_imported_module,
20296 lexical_block_die,
20297 lexical_block);
20298 else
20299 return;
20301 else
20302 imported_die = new_die (DW_TAG_imported_declaration,
20303 lexical_block_die,
20304 lexical_block);
20306 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20307 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20308 if (name)
20309 add_AT_string (imported_die, DW_AT_name,
20310 IDENTIFIER_POINTER (name));
20311 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20314 /* Output debug information for imported module or decl DECL.
20315 NAME is non-NULL name in context if the decl has been renamed.
20316 CHILD is true if decl is one of the renamed decls as part of
20317 importing whole module. */
20319 static void
20320 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20321 bool child)
20323 /* dw_die_ref at_import_die; */
20324 dw_die_ref scope_die;
20326 if (debug_info_level <= DINFO_LEVEL_TERSE)
20327 return;
20329 gcc_assert (decl);
20331 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20332 We need decl DIE for reference and scope die. First, get DIE for the decl
20333 itself. */
20335 /* Get the scope die for decl context. Use comp_unit_die for global module
20336 or decl. If die is not found for non globals, force new die. */
20337 if (context
20338 && TYPE_P (context)
20339 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20340 return;
20342 if (!(dwarf_version >= 3 || !dwarf_strict))
20343 return;
20345 scope_die = get_context_die (context);
20347 if (child)
20349 gcc_assert (scope_die->die_child);
20350 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20351 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20352 scope_die = scope_die->die_child;
20355 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20356 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20360 /* Write the debugging output for DECL. */
20362 void
20363 dwarf2out_decl (tree decl)
20365 dw_die_ref context_die = comp_unit_die ();
20367 switch (TREE_CODE (decl))
20369 case ERROR_MARK:
20370 return;
20372 case FUNCTION_DECL:
20373 /* What we would really like to do here is to filter out all mere
20374 file-scope declarations of file-scope functions which are never
20375 referenced later within this translation unit (and keep all of ones
20376 that *are* referenced later on) but we aren't clairvoyant, so we have
20377 no idea which functions will be referenced in the future (i.e. later
20378 on within the current translation unit). So here we just ignore all
20379 file-scope function declarations which are not also definitions. If
20380 and when the debugger needs to know something about these functions,
20381 it will have to hunt around and find the DWARF information associated
20382 with the definition of the function.
20384 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20385 nodes represent definitions and which ones represent mere
20386 declarations. We have to check DECL_INITIAL instead. That's because
20387 the C front-end supports some weird semantics for "extern inline"
20388 function definitions. These can get inlined within the current
20389 translation unit (and thus, we need to generate Dwarf info for their
20390 abstract instances so that the Dwarf info for the concrete inlined
20391 instances can have something to refer to) but the compiler never
20392 generates any out-of-lines instances of such things (despite the fact
20393 that they *are* definitions).
20395 The important point is that the C front-end marks these "extern
20396 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20397 them anyway. Note that the C++ front-end also plays some similar games
20398 for inline function definitions appearing within include files which
20399 also contain `#pragma interface' pragmas.
20401 If we are called from dwarf2out_abstract_function output a DIE
20402 anyway. We can end up here this way with early inlining and LTO
20403 where the inlined function is output in a different LTRANS unit
20404 or not at all. */
20405 if (DECL_INITIAL (decl) == NULL_TREE
20406 && ! DECL_ABSTRACT (decl))
20407 return;
20409 /* If we're a nested function, initially use a parent of NULL; if we're
20410 a plain function, this will be fixed up in decls_for_scope. If
20411 we're a method, it will be ignored, since we already have a DIE. */
20412 if (decl_function_context (decl)
20413 /* But if we're in terse mode, we don't care about scope. */
20414 && debug_info_level > DINFO_LEVEL_TERSE)
20415 context_die = NULL;
20416 break;
20418 case VAR_DECL:
20419 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20420 declaration and if the declaration was never even referenced from
20421 within this entire compilation unit. We suppress these DIEs in
20422 order to save space in the .debug section (by eliminating entries
20423 which are probably useless). Note that we must not suppress
20424 block-local extern declarations (whether used or not) because that
20425 would screw-up the debugger's name lookup mechanism and cause it to
20426 miss things which really ought to be in scope at a given point. */
20427 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20428 return;
20430 /* For local statics lookup proper context die. */
20431 if (TREE_STATIC (decl)
20432 && DECL_CONTEXT (decl)
20433 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20434 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20436 /* If we are in terse mode, don't generate any DIEs to represent any
20437 variable declarations or definitions. */
20438 if (debug_info_level <= DINFO_LEVEL_TERSE)
20439 return;
20440 break;
20442 case CONST_DECL:
20443 if (debug_info_level <= DINFO_LEVEL_TERSE)
20444 return;
20445 if (!is_fortran () && !is_ada ())
20446 return;
20447 if (TREE_STATIC (decl) && decl_function_context (decl))
20448 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20449 break;
20451 case NAMESPACE_DECL:
20452 case IMPORTED_DECL:
20453 if (debug_info_level <= DINFO_LEVEL_TERSE)
20454 return;
20455 if (lookup_decl_die (decl) != NULL)
20456 return;
20457 break;
20459 case TYPE_DECL:
20460 /* Don't emit stubs for types unless they are needed by other DIEs. */
20461 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20462 return;
20464 /* Don't bother trying to generate any DIEs to represent any of the
20465 normal built-in types for the language we are compiling. */
20466 if (DECL_IS_BUILTIN (decl))
20467 return;
20469 /* If we are in terse mode, don't generate any DIEs for types. */
20470 if (debug_info_level <= DINFO_LEVEL_TERSE)
20471 return;
20473 /* If we're a function-scope tag, initially use a parent of NULL;
20474 this will be fixed up in decls_for_scope. */
20475 if (decl_function_context (decl))
20476 context_die = NULL;
20478 break;
20480 default:
20481 return;
20484 gen_decl_die (decl, NULL, context_die);
20487 /* Write the debugging output for DECL. */
20489 static void
20490 dwarf2out_function_decl (tree decl)
20492 dwarf2out_decl (decl);
20493 call_arg_locations = NULL;
20494 call_arg_loc_last = NULL;
20495 call_site_count = -1;
20496 tail_call_site_count = -1;
20497 block_map.release ();
20498 htab_empty (decl_loc_table);
20499 htab_empty (cached_dw_loc_list_table);
20502 /* Output a marker (i.e. a label) for the beginning of the generated code for
20503 a lexical block. */
20505 static void
20506 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20507 unsigned int blocknum)
20509 switch_to_section (current_function_section ());
20510 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20513 /* Output a marker (i.e. a label) for the end of the generated code for a
20514 lexical block. */
20516 static void
20517 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20519 switch_to_section (current_function_section ());
20520 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20523 /* Returns nonzero if it is appropriate not to emit any debugging
20524 information for BLOCK, because it doesn't contain any instructions.
20526 Don't allow this for blocks with nested functions or local classes
20527 as we would end up with orphans, and in the presence of scheduling
20528 we may end up calling them anyway. */
20530 static bool
20531 dwarf2out_ignore_block (const_tree block)
20533 tree decl;
20534 unsigned int i;
20536 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20537 if (TREE_CODE (decl) == FUNCTION_DECL
20538 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20539 return 0;
20540 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20542 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20543 if (TREE_CODE (decl) == FUNCTION_DECL
20544 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20545 return 0;
20548 return 1;
20551 /* Hash table routines for file_hash. */
20553 static int
20554 file_table_eq (const void *p1_p, const void *p2_p)
20556 const struct dwarf_file_data *const p1 =
20557 (const struct dwarf_file_data *) p1_p;
20558 const char *const p2 = (const char *) p2_p;
20559 return filename_cmp (p1->filename, p2) == 0;
20562 static hashval_t
20563 file_table_hash (const void *p_p)
20565 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20566 return htab_hash_string (p->filename);
20569 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20570 dwarf2out.c) and return its "index". The index of each (known) filename is
20571 just a unique number which is associated with only that one filename. We
20572 need such numbers for the sake of generating labels (in the .debug_sfnames
20573 section) and references to those files numbers (in the .debug_srcinfo
20574 and.debug_macinfo sections). If the filename given as an argument is not
20575 found in our current list, add it to the list and assign it the next
20576 available unique index number. In order to speed up searches, we remember
20577 the index of the filename was looked up last. This handles the majority of
20578 all searches. */
20580 static struct dwarf_file_data *
20581 lookup_filename (const char *file_name)
20583 void ** slot;
20584 struct dwarf_file_data * created;
20586 /* Check to see if the file name that was searched on the previous
20587 call matches this file name. If so, return the index. */
20588 if (file_table_last_lookup
20589 && (file_name == file_table_last_lookup->filename
20590 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20591 return file_table_last_lookup;
20593 /* Didn't match the previous lookup, search the table. */
20594 slot = htab_find_slot_with_hash (file_table, file_name,
20595 htab_hash_string (file_name), INSERT);
20596 if (*slot)
20597 return (struct dwarf_file_data *) *slot;
20599 created = ggc_alloc_dwarf_file_data ();
20600 created->filename = file_name;
20601 created->emitted_number = 0;
20602 *slot = created;
20603 return created;
20606 /* If the assembler will construct the file table, then translate the compiler
20607 internal file table number into the assembler file table number, and emit
20608 a .file directive if we haven't already emitted one yet. The file table
20609 numbers are different because we prune debug info for unused variables and
20610 types, which may include filenames. */
20612 static int
20613 maybe_emit_file (struct dwarf_file_data * fd)
20615 if (! fd->emitted_number)
20617 if (last_emitted_file)
20618 fd->emitted_number = last_emitted_file->emitted_number + 1;
20619 else
20620 fd->emitted_number = 1;
20621 last_emitted_file = fd;
20623 if (DWARF2_ASM_LINE_DEBUG_INFO)
20625 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20626 output_quoted_string (asm_out_file,
20627 remap_debug_filename (fd->filename));
20628 fputc ('\n', asm_out_file);
20632 return fd->emitted_number;
20635 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20636 That generation should happen after function debug info has been
20637 generated. The value of the attribute is the constant value of ARG. */
20639 static void
20640 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20642 die_arg_entry entry;
20644 if (!die || !arg)
20645 return;
20647 if (!tmpl_value_parm_die_table)
20648 vec_alloc (tmpl_value_parm_die_table, 32);
20650 entry.die = die;
20651 entry.arg = arg;
20652 vec_safe_push (tmpl_value_parm_die_table, entry);
20655 /* Return TRUE if T is an instance of generic type, FALSE
20656 otherwise. */
20658 static bool
20659 generic_type_p (tree t)
20661 if (t == NULL_TREE || !TYPE_P (t))
20662 return false;
20663 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20666 /* Schedule the generation of the generic parameter dies for the
20667 instance of generic type T. The proper generation itself is later
20668 done by gen_scheduled_generic_parms_dies. */
20670 static void
20671 schedule_generic_params_dies_gen (tree t)
20673 if (!generic_type_p (t))
20674 return;
20676 if (!generic_type_instances)
20677 vec_alloc (generic_type_instances, 256);
20679 vec_safe_push (generic_type_instances, t);
20682 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20683 by append_entry_to_tmpl_value_parm_die_table. This function must
20684 be called after function DIEs have been generated. */
20686 static void
20687 gen_remaining_tmpl_value_param_die_attribute (void)
20689 if (tmpl_value_parm_die_table)
20691 unsigned i;
20692 die_arg_entry *e;
20694 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20695 tree_add_const_value_attribute (e->die, e->arg);
20699 /* Generate generic parameters DIEs for instances of generic types
20700 that have been previously scheduled by
20701 schedule_generic_params_dies_gen. This function must be called
20702 after all the types of the CU have been laid out. */
20704 static void
20705 gen_scheduled_generic_parms_dies (void)
20707 unsigned i;
20708 tree t;
20710 if (!generic_type_instances)
20711 return;
20713 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20714 if (COMPLETE_TYPE_P (t))
20715 gen_generic_params_dies (t);
20719 /* Replace DW_AT_name for the decl with name. */
20721 static void
20722 dwarf2out_set_name (tree decl, tree name)
20724 dw_die_ref die;
20725 dw_attr_ref attr;
20726 const char *dname;
20728 die = TYPE_SYMTAB_DIE (decl);
20729 if (!die)
20730 return;
20732 dname = dwarf2_name (name, 0);
20733 if (!dname)
20734 return;
20736 attr = get_AT (die, DW_AT_name);
20737 if (attr)
20739 struct indirect_string_node *node;
20741 node = find_AT_string (dname);
20742 /* replace the string. */
20743 attr->dw_attr_val.v.val_str = node;
20746 else
20747 add_name_attribute (die, dname);
20750 /* True if before or during processing of the first function being emitted. */
20751 static bool in_first_function_p = true;
20752 /* True if loc_note during dwarf2out_var_location call might still be
20753 before first real instruction at address equal to .Ltext0. */
20754 static bool maybe_at_text_label_p = true;
20755 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20756 static unsigned int first_loclabel_num_not_at_text_label;
20758 /* Called by the final INSN scan whenever we see a var location. We
20759 use it to drop labels in the right places, and throw the location in
20760 our lookup table. */
20762 static void
20763 dwarf2out_var_location (rtx loc_note)
20765 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20766 struct var_loc_node *newloc;
20767 rtx next_real, next_note;
20768 static const char *last_label;
20769 static const char *last_postcall_label;
20770 static bool last_in_cold_section_p;
20771 static rtx expected_next_loc_note;
20772 tree decl;
20773 bool var_loc_p;
20775 if (!NOTE_P (loc_note))
20777 if (CALL_P (loc_note))
20779 call_site_count++;
20780 if (SIBLING_CALL_P (loc_note))
20781 tail_call_site_count++;
20783 return;
20786 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20787 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20788 return;
20790 /* Optimize processing a large consecutive sequence of location
20791 notes so we don't spend too much time in next_real_insn. If the
20792 next insn is another location note, remember the next_real_insn
20793 calculation for next time. */
20794 next_real = cached_next_real_insn;
20795 if (next_real)
20797 if (expected_next_loc_note != loc_note)
20798 next_real = NULL_RTX;
20801 next_note = NEXT_INSN (loc_note);
20802 if (! next_note
20803 || INSN_DELETED_P (next_note)
20804 || ! NOTE_P (next_note)
20805 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20806 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20807 next_note = NULL_RTX;
20809 if (! next_real)
20810 next_real = next_real_insn (loc_note);
20812 if (next_note)
20814 expected_next_loc_note = next_note;
20815 cached_next_real_insn = next_real;
20817 else
20818 cached_next_real_insn = NULL_RTX;
20820 /* If there are no instructions which would be affected by this note,
20821 don't do anything. */
20822 if (var_loc_p
20823 && next_real == NULL_RTX
20824 && !NOTE_DURING_CALL_P (loc_note))
20825 return;
20827 if (next_real == NULL_RTX)
20828 next_real = get_last_insn ();
20830 /* If there were any real insns between note we processed last time
20831 and this note (or if it is the first note), clear
20832 last_{,postcall_}label so that they are not reused this time. */
20833 if (last_var_location_insn == NULL_RTX
20834 || last_var_location_insn != next_real
20835 || last_in_cold_section_p != in_cold_section_p)
20837 last_label = NULL;
20838 last_postcall_label = NULL;
20841 if (var_loc_p)
20843 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20844 newloc = add_var_loc_to_decl (decl, loc_note,
20845 NOTE_DURING_CALL_P (loc_note)
20846 ? last_postcall_label : last_label);
20847 if (newloc == NULL)
20848 return;
20850 else
20852 decl = NULL_TREE;
20853 newloc = NULL;
20856 /* If there were no real insns between note we processed last time
20857 and this note, use the label we emitted last time. Otherwise
20858 create a new label and emit it. */
20859 if (last_label == NULL)
20861 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20862 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20863 loclabel_num++;
20864 last_label = ggc_strdup (loclabel);
20865 /* See if loclabel might be equal to .Ltext0. If yes,
20866 bump first_loclabel_num_not_at_text_label. */
20867 if (!have_multiple_function_sections
20868 && in_first_function_p
20869 && maybe_at_text_label_p)
20871 static rtx last_start;
20872 rtx insn;
20873 for (insn = loc_note; insn; insn = previous_insn (insn))
20874 if (insn == last_start)
20875 break;
20876 else if (!NONDEBUG_INSN_P (insn))
20877 continue;
20878 else
20880 rtx body = PATTERN (insn);
20881 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
20882 continue;
20883 /* Inline asm could occupy zero bytes. */
20884 else if (GET_CODE (body) == ASM_INPUT
20885 || asm_noperands (body) >= 0)
20886 continue;
20887 #ifdef HAVE_attr_length
20888 else if (get_attr_min_length (insn) == 0)
20889 continue;
20890 #endif
20891 else
20893 /* Assume insn has non-zero length. */
20894 maybe_at_text_label_p = false;
20895 break;
20898 if (maybe_at_text_label_p)
20900 last_start = loc_note;
20901 first_loclabel_num_not_at_text_label = loclabel_num;
20906 if (!var_loc_p)
20908 struct call_arg_loc_node *ca_loc
20909 = ggc_alloc_cleared_call_arg_loc_node ();
20910 rtx prev = prev_real_insn (loc_note), x;
20911 ca_loc->call_arg_loc_note = loc_note;
20912 ca_loc->next = NULL;
20913 ca_loc->label = last_label;
20914 gcc_assert (prev
20915 && (CALL_P (prev)
20916 || (NONJUMP_INSN_P (prev)
20917 && GET_CODE (PATTERN (prev)) == SEQUENCE
20918 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20919 if (!CALL_P (prev))
20920 prev = XVECEXP (PATTERN (prev), 0, 0);
20921 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20922 x = get_call_rtx_from (PATTERN (prev));
20923 if (x)
20925 x = XEXP (XEXP (x, 0), 0);
20926 if (GET_CODE (x) == SYMBOL_REF
20927 && SYMBOL_REF_DECL (x)
20928 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20929 ca_loc->symbol_ref = x;
20931 ca_loc->block = insn_scope (prev);
20932 if (call_arg_locations)
20933 call_arg_loc_last->next = ca_loc;
20934 else
20935 call_arg_locations = ca_loc;
20936 call_arg_loc_last = ca_loc;
20938 else if (!NOTE_DURING_CALL_P (loc_note))
20939 newloc->label = last_label;
20940 else
20942 if (!last_postcall_label)
20944 sprintf (loclabel, "%s-1", last_label);
20945 last_postcall_label = ggc_strdup (loclabel);
20947 newloc->label = last_postcall_label;
20950 last_var_location_insn = next_real;
20951 last_in_cold_section_p = in_cold_section_p;
20954 /* Note in one location list that text section has changed. */
20956 static int
20957 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20959 var_loc_list *list = (var_loc_list *) *slot;
20960 if (list->first)
20961 list->last_before_switch
20962 = list->last->next ? list->last->next : list->last;
20963 return 1;
20966 /* Note in all location lists that text section has changed. */
20968 static void
20969 var_location_switch_text_section (void)
20971 if (decl_loc_table == NULL)
20972 return;
20974 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20977 /* Create a new line number table. */
20979 static dw_line_info_table *
20980 new_line_info_table (void)
20982 dw_line_info_table *table;
20984 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20985 table->file_num = 1;
20986 table->line_num = 1;
20987 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20989 return table;
20992 /* Lookup the "current" table into which we emit line info, so
20993 that we don't have to do it for every source line. */
20995 static void
20996 set_cur_line_info_table (section *sec)
20998 dw_line_info_table *table;
21000 if (sec == text_section)
21001 table = text_section_line_info;
21002 else if (sec == cold_text_section)
21004 table = cold_text_section_line_info;
21005 if (!table)
21007 cold_text_section_line_info = table = new_line_info_table ();
21008 table->end_label = cold_end_label;
21011 else
21013 const char *end_label;
21015 if (flag_reorder_blocks_and_partition)
21017 if (in_cold_section_p)
21018 end_label = crtl->subsections.cold_section_end_label;
21019 else
21020 end_label = crtl->subsections.hot_section_end_label;
21022 else
21024 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21025 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21026 current_function_funcdef_no);
21027 end_label = ggc_strdup (label);
21030 table = new_line_info_table ();
21031 table->end_label = end_label;
21033 vec_safe_push (separate_line_info, table);
21036 if (DWARF2_ASM_LINE_DEBUG_INFO)
21037 table->is_stmt = (cur_line_info_table
21038 ? cur_line_info_table->is_stmt
21039 : DWARF_LINE_DEFAULT_IS_STMT_START);
21040 cur_line_info_table = table;
21044 /* We need to reset the locations at the beginning of each
21045 function. We can't do this in the end_function hook, because the
21046 declarations that use the locations won't have been output when
21047 that hook is called. Also compute have_multiple_function_sections here. */
21049 static void
21050 dwarf2out_begin_function (tree fun)
21052 section *sec = function_section (fun);
21054 if (sec != text_section)
21055 have_multiple_function_sections = true;
21057 if (flag_reorder_blocks_and_partition && !cold_text_section)
21059 gcc_assert (current_function_decl == fun);
21060 cold_text_section = unlikely_text_section ();
21061 switch_to_section (cold_text_section);
21062 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21063 switch_to_section (sec);
21066 dwarf2out_note_section_used ();
21067 call_site_count = 0;
21068 tail_call_site_count = 0;
21070 set_cur_line_info_table (sec);
21073 /* Helper function of dwarf2out_end_function, called only after emitting
21074 the very first function into assembly. Check if some .debug_loc range
21075 might end with a .LVL* label that could be equal to .Ltext0.
21076 In that case we must force using absolute addresses in .debug_loc ranges,
21077 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21078 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21079 list terminator.
21080 Set have_multiple_function_sections to true in that case and
21081 terminate htab traversal. */
21083 static int
21084 find_empty_loc_ranges_at_text_label (void **slot, void *)
21086 var_loc_list *entry;
21087 struct var_loc_node *node;
21089 entry = (var_loc_list *) *slot;
21090 node = entry->first;
21091 if (node && node->next && node->next->label)
21093 unsigned int i;
21094 const char *label = node->next->label;
21095 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21097 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21099 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21100 if (strcmp (label, loclabel) == 0)
21102 have_multiple_function_sections = true;
21103 return 0;
21107 return 1;
21110 /* Hook called after emitting a function into assembly.
21111 This does something only for the very first function emitted. */
21113 static void
21114 dwarf2out_end_function (unsigned int)
21116 if (in_first_function_p
21117 && !have_multiple_function_sections
21118 && first_loclabel_num_not_at_text_label
21119 && decl_loc_table)
21120 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21121 NULL);
21122 in_first_function_p = false;
21123 maybe_at_text_label_p = false;
21126 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21128 static void
21129 push_dw_line_info_entry (dw_line_info_table *table,
21130 enum dw_line_info_opcode opcode, unsigned int val)
21132 dw_line_info_entry e;
21133 e.opcode = opcode;
21134 e.val = val;
21135 vec_safe_push (table->entries, e);
21138 /* Output a label to mark the beginning of a source code line entry
21139 and record information relating to this source line, in
21140 'line_info_table' for later output of the .debug_line section. */
21141 /* ??? The discriminator parameter ought to be unsigned. */
21143 static void
21144 dwarf2out_source_line (unsigned int line, const char *filename,
21145 int discriminator, bool is_stmt)
21147 unsigned int file_num;
21148 dw_line_info_table *table;
21150 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21151 return;
21153 /* The discriminator column was added in dwarf4. Simplify the below
21154 by simply removing it if we're not supposed to output it. */
21155 if (dwarf_version < 4 && dwarf_strict)
21156 discriminator = 0;
21158 table = cur_line_info_table;
21159 file_num = maybe_emit_file (lookup_filename (filename));
21161 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21162 the debugger has used the second (possibly duplicate) line number
21163 at the beginning of the function to mark the end of the prologue.
21164 We could eliminate any other duplicates within the function. For
21165 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21166 that second line number entry. */
21167 /* Recall that this end-of-prologue indication is *not* the same thing
21168 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21169 to which the hook corresponds, follows the last insn that was
21170 emitted by gen_prologue. What we need is to precede the first insn
21171 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21172 insn that corresponds to something the user wrote. These may be
21173 very different locations once scheduling is enabled. */
21175 if (0 && file_num == table->file_num
21176 && line == table->line_num
21177 && discriminator == table->discrim_num
21178 && is_stmt == table->is_stmt)
21179 return;
21181 switch_to_section (current_function_section ());
21183 /* If requested, emit something human-readable. */
21184 if (flag_debug_asm)
21185 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21187 if (DWARF2_ASM_LINE_DEBUG_INFO)
21189 /* Emit the .loc directive understood by GNU as. */
21190 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21191 file_num, line, is_stmt, discriminator */
21192 fputs ("\t.loc ", asm_out_file);
21193 fprint_ul (asm_out_file, file_num);
21194 putc (' ', asm_out_file);
21195 fprint_ul (asm_out_file, line);
21196 putc (' ', asm_out_file);
21197 putc ('0', asm_out_file);
21199 if (is_stmt != table->is_stmt)
21201 fputs (" is_stmt ", asm_out_file);
21202 putc (is_stmt ? '1' : '0', asm_out_file);
21204 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21206 gcc_assert (discriminator > 0);
21207 fputs (" discriminator ", asm_out_file);
21208 fprint_ul (asm_out_file, (unsigned long) discriminator);
21210 putc ('\n', asm_out_file);
21212 else
21214 unsigned int label_num = ++line_info_label_num;
21216 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21218 push_dw_line_info_entry (table, LI_set_address, label_num);
21219 if (file_num != table->file_num)
21220 push_dw_line_info_entry (table, LI_set_file, file_num);
21221 if (discriminator != table->discrim_num)
21222 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21223 if (is_stmt != table->is_stmt)
21224 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21225 push_dw_line_info_entry (table, LI_set_line, line);
21228 table->file_num = file_num;
21229 table->line_num = line;
21230 table->discrim_num = discriminator;
21231 table->is_stmt = is_stmt;
21232 table->in_use = true;
21235 /* Record the beginning of a new source file. */
21237 static void
21238 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21240 if (flag_eliminate_dwarf2_dups)
21242 /* Record the beginning of the file for break_out_includes. */
21243 dw_die_ref bincl_die;
21245 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21246 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21249 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21251 macinfo_entry e;
21252 e.code = DW_MACINFO_start_file;
21253 e.lineno = lineno;
21254 e.info = ggc_strdup (filename);
21255 vec_safe_push (macinfo_table, e);
21259 /* Record the end of a source file. */
21261 static void
21262 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21264 if (flag_eliminate_dwarf2_dups)
21265 /* Record the end of the file for break_out_includes. */
21266 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21268 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21270 macinfo_entry e;
21271 e.code = DW_MACINFO_end_file;
21272 e.lineno = lineno;
21273 e.info = NULL;
21274 vec_safe_push (macinfo_table, e);
21278 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21279 the tail part of the directive line, i.e. the part which is past the
21280 initial whitespace, #, whitespace, directive-name, whitespace part. */
21282 static void
21283 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21284 const char *buffer ATTRIBUTE_UNUSED)
21286 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21288 macinfo_entry e;
21289 /* Insert a dummy first entry to be able to optimize the whole
21290 predefined macro block using DW_MACRO_GNU_transparent_include. */
21291 if (macinfo_table->is_empty () && lineno <= 1)
21293 e.code = 0;
21294 e.lineno = 0;
21295 e.info = NULL;
21296 vec_safe_push (macinfo_table, e);
21298 e.code = DW_MACINFO_define;
21299 e.lineno = lineno;
21300 e.info = ggc_strdup (buffer);
21301 vec_safe_push (macinfo_table, e);
21305 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21306 the tail part of the directive line, i.e. the part which is past the
21307 initial whitespace, #, whitespace, directive-name, whitespace part. */
21309 static void
21310 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21311 const char *buffer ATTRIBUTE_UNUSED)
21313 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21315 macinfo_entry e;
21316 /* Insert a dummy first entry to be able to optimize the whole
21317 predefined macro block using DW_MACRO_GNU_transparent_include. */
21318 if (macinfo_table->is_empty () && lineno <= 1)
21320 e.code = 0;
21321 e.lineno = 0;
21322 e.info = NULL;
21323 vec_safe_push (macinfo_table, e);
21325 e.code = DW_MACINFO_undef;
21326 e.lineno = lineno;
21327 e.info = ggc_strdup (buffer);
21328 vec_safe_push (macinfo_table, e);
21332 /* Helpers to manipulate hash table of CUs. */
21334 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21336 typedef macinfo_entry value_type;
21337 typedef macinfo_entry compare_type;
21338 static inline hashval_t hash (const value_type *);
21339 static inline bool equal (const value_type *, const compare_type *);
21342 inline hashval_t
21343 macinfo_entry_hasher::hash (const value_type *entry)
21345 return htab_hash_string (entry->info);
21348 inline bool
21349 macinfo_entry_hasher::equal (const value_type *entry1,
21350 const compare_type *entry2)
21352 return !strcmp (entry1->info, entry2->info);
21355 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21357 /* Output a single .debug_macinfo entry. */
21359 static void
21360 output_macinfo_op (macinfo_entry *ref)
21362 int file_num;
21363 size_t len;
21364 struct indirect_string_node *node;
21365 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21366 struct dwarf_file_data *fd;
21368 switch (ref->code)
21370 case DW_MACINFO_start_file:
21371 fd = lookup_filename (ref->info);
21372 file_num = maybe_emit_file (fd);
21373 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21374 dw2_asm_output_data_uleb128 (ref->lineno,
21375 "Included from line number %lu",
21376 (unsigned long) ref->lineno);
21377 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21378 break;
21379 case DW_MACINFO_end_file:
21380 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21381 break;
21382 case DW_MACINFO_define:
21383 case DW_MACINFO_undef:
21384 len = strlen (ref->info) + 1;
21385 if (!dwarf_strict
21386 && len > DWARF_OFFSET_SIZE
21387 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21388 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21390 ref->code = ref->code == DW_MACINFO_define
21391 ? DW_MACRO_GNU_define_indirect
21392 : DW_MACRO_GNU_undef_indirect;
21393 output_macinfo_op (ref);
21394 return;
21396 dw2_asm_output_data (1, ref->code,
21397 ref->code == DW_MACINFO_define
21398 ? "Define macro" : "Undefine macro");
21399 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21400 (unsigned long) ref->lineno);
21401 dw2_asm_output_nstring (ref->info, -1, "The macro");
21402 break;
21403 case DW_MACRO_GNU_define_indirect:
21404 case DW_MACRO_GNU_undef_indirect:
21405 node = find_AT_string (ref->info);
21406 gcc_assert (node
21407 && ((node->form == DW_FORM_strp)
21408 || (node->form == DW_FORM_GNU_str_index)));
21409 dw2_asm_output_data (1, ref->code,
21410 ref->code == DW_MACRO_GNU_define_indirect
21411 ? "Define macro indirect"
21412 : "Undefine macro indirect");
21413 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21414 (unsigned long) ref->lineno);
21415 if (node->form == DW_FORM_strp)
21416 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21417 debug_str_section, "The macro: \"%s\"",
21418 ref->info);
21419 else
21420 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21421 ref->info);
21422 break;
21423 case DW_MACRO_GNU_transparent_include:
21424 dw2_asm_output_data (1, ref->code, "Transparent include");
21425 ASM_GENERATE_INTERNAL_LABEL (label,
21426 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21427 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21428 break;
21429 default:
21430 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21431 ASM_COMMENT_START, (unsigned long) ref->code);
21432 break;
21436 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21437 other compilation unit .debug_macinfo sections. IDX is the first
21438 index of a define/undef, return the number of ops that should be
21439 emitted in a comdat .debug_macinfo section and emit
21440 a DW_MACRO_GNU_transparent_include entry referencing it.
21441 If the define/undef entry should be emitted normally, return 0. */
21443 static unsigned
21444 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21445 macinfo_hash_type *macinfo_htab)
21447 macinfo_entry *first, *second, *cur, *inc;
21448 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21449 unsigned char checksum[16];
21450 struct md5_ctx ctx;
21451 char *grp_name, *tail;
21452 const char *base;
21453 unsigned int i, count, encoded_filename_len, linebuf_len;
21454 macinfo_entry **slot;
21456 first = &(*macinfo_table)[idx];
21457 second = &(*macinfo_table)[idx + 1];
21459 /* Optimize only if there are at least two consecutive define/undef ops,
21460 and either all of them are before first DW_MACINFO_start_file
21461 with lineno {0,1} (i.e. predefined macro block), or all of them are
21462 in some included header file. */
21463 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21464 return 0;
21465 if (vec_safe_is_empty (files))
21467 if (first->lineno > 1 || second->lineno > 1)
21468 return 0;
21470 else if (first->lineno == 0)
21471 return 0;
21473 /* Find the last define/undef entry that can be grouped together
21474 with first and at the same time compute md5 checksum of their
21475 codes, linenumbers and strings. */
21476 md5_init_ctx (&ctx);
21477 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21478 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21479 break;
21480 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21481 break;
21482 else
21484 unsigned char code = cur->code;
21485 md5_process_bytes (&code, 1, &ctx);
21486 checksum_uleb128 (cur->lineno, &ctx);
21487 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21489 md5_finish_ctx (&ctx, checksum);
21490 count = i - idx;
21492 /* From the containing include filename (if any) pick up just
21493 usable characters from its basename. */
21494 if (vec_safe_is_empty (files))
21495 base = "";
21496 else
21497 base = lbasename (files->last ().info);
21498 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21499 if (ISIDNUM (base[i]) || base[i] == '.')
21500 encoded_filename_len++;
21501 /* Count . at the end. */
21502 if (encoded_filename_len)
21503 encoded_filename_len++;
21505 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21506 linebuf_len = strlen (linebuf);
21508 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21509 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21510 + 16 * 2 + 1);
21511 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21512 tail = grp_name + 4;
21513 if (encoded_filename_len)
21515 for (i = 0; base[i]; i++)
21516 if (ISIDNUM (base[i]) || base[i] == '.')
21517 *tail++ = base[i];
21518 *tail++ = '.';
21520 memcpy (tail, linebuf, linebuf_len);
21521 tail += linebuf_len;
21522 *tail++ = '.';
21523 for (i = 0; i < 16; i++)
21524 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21526 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21527 in the empty vector entry before the first define/undef. */
21528 inc = &(*macinfo_table)[idx - 1];
21529 inc->code = DW_MACRO_GNU_transparent_include;
21530 inc->lineno = 0;
21531 inc->info = ggc_strdup (grp_name);
21532 if (!macinfo_htab->is_created ())
21533 macinfo_htab->create (10);
21534 /* Avoid emitting duplicates. */
21535 slot = macinfo_htab->find_slot (inc, INSERT);
21536 if (*slot != NULL)
21538 inc->code = 0;
21539 inc->info = NULL;
21540 /* If such an entry has been used before, just emit
21541 a DW_MACRO_GNU_transparent_include op. */
21542 inc = *slot;
21543 output_macinfo_op (inc);
21544 /* And clear all macinfo_entry in the range to avoid emitting them
21545 in the second pass. */
21546 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21548 cur->code = 0;
21549 cur->info = NULL;
21552 else
21554 *slot = inc;
21555 inc->lineno = macinfo_htab->elements ();
21556 output_macinfo_op (inc);
21558 return count;
21561 /* Save any strings needed by the macinfo table in the debug str
21562 table. All strings must be collected into the table by the time
21563 index_string is called. */
21565 static void
21566 save_macinfo_strings (void)
21568 unsigned len;
21569 unsigned i;
21570 macinfo_entry *ref;
21572 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21574 switch (ref->code)
21576 /* Match the logic in output_macinfo_op to decide on
21577 indirect strings. */
21578 case DW_MACINFO_define:
21579 case DW_MACINFO_undef:
21580 len = strlen (ref->info) + 1;
21581 if (!dwarf_strict
21582 && len > DWARF_OFFSET_SIZE
21583 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21584 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21585 set_indirect_string (find_AT_string (ref->info));
21586 break;
21587 case DW_MACRO_GNU_define_indirect:
21588 case DW_MACRO_GNU_undef_indirect:
21589 set_indirect_string (find_AT_string (ref->info));
21590 break;
21591 default:
21592 break;
21597 /* Output macinfo section(s). */
21599 static void
21600 output_macinfo (void)
21602 unsigned i;
21603 unsigned long length = vec_safe_length (macinfo_table);
21604 macinfo_entry *ref;
21605 vec<macinfo_entry, va_gc> *files = NULL;
21606 macinfo_hash_type macinfo_htab;
21608 if (! length)
21609 return;
21611 /* output_macinfo* uses these interchangeably. */
21612 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21613 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21614 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21615 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21617 /* For .debug_macro emit the section header. */
21618 if (!dwarf_strict)
21620 dw2_asm_output_data (2, 4, "DWARF macro version number");
21621 if (DWARF_OFFSET_SIZE == 8)
21622 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21623 else
21624 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21625 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21626 (!dwarf_split_debug_info ? debug_line_section_label
21627 : debug_skeleton_line_section_label),
21628 debug_line_section, NULL);
21631 /* In the first loop, it emits the primary .debug_macinfo section
21632 and after each emitted op the macinfo_entry is cleared.
21633 If a longer range of define/undef ops can be optimized using
21634 DW_MACRO_GNU_transparent_include, the
21635 DW_MACRO_GNU_transparent_include op is emitted and kept in
21636 the vector before the first define/undef in the range and the
21637 whole range of define/undef ops is not emitted and kept. */
21638 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21640 switch (ref->code)
21642 case DW_MACINFO_start_file:
21643 vec_safe_push (files, *ref);
21644 break;
21645 case DW_MACINFO_end_file:
21646 if (!vec_safe_is_empty (files))
21647 files->pop ();
21648 break;
21649 case DW_MACINFO_define:
21650 case DW_MACINFO_undef:
21651 if (!dwarf_strict
21652 && HAVE_COMDAT_GROUP
21653 && vec_safe_length (files) != 1
21654 && i > 0
21655 && i + 1 < length
21656 && (*macinfo_table)[i - 1].code == 0)
21658 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21659 if (count)
21661 i += count - 1;
21662 continue;
21665 break;
21666 case 0:
21667 /* A dummy entry may be inserted at the beginning to be able
21668 to optimize the whole block of predefined macros. */
21669 if (i == 0)
21670 continue;
21671 default:
21672 break;
21674 output_macinfo_op (ref);
21675 ref->info = NULL;
21676 ref->code = 0;
21679 if (!macinfo_htab.is_created ())
21680 return;
21682 macinfo_htab.dispose ();
21684 /* If any DW_MACRO_GNU_transparent_include were used, on those
21685 DW_MACRO_GNU_transparent_include entries terminate the
21686 current chain and switch to a new comdat .debug_macinfo
21687 section and emit the define/undef entries within it. */
21688 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21689 switch (ref->code)
21691 case 0:
21692 continue;
21693 case DW_MACRO_GNU_transparent_include:
21695 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21696 tree comdat_key = get_identifier (ref->info);
21697 /* Terminate the previous .debug_macinfo section. */
21698 dw2_asm_output_data (1, 0, "End compilation unit");
21699 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21700 SECTION_DEBUG
21701 | SECTION_LINKONCE,
21702 comdat_key);
21703 ASM_GENERATE_INTERNAL_LABEL (label,
21704 DEBUG_MACRO_SECTION_LABEL,
21705 ref->lineno);
21706 ASM_OUTPUT_LABEL (asm_out_file, label);
21707 ref->code = 0;
21708 ref->info = NULL;
21709 dw2_asm_output_data (2, 4, "DWARF macro version number");
21710 if (DWARF_OFFSET_SIZE == 8)
21711 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21712 else
21713 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21715 break;
21716 case DW_MACINFO_define:
21717 case DW_MACINFO_undef:
21718 output_macinfo_op (ref);
21719 ref->code = 0;
21720 ref->info = NULL;
21721 break;
21722 default:
21723 gcc_unreachable ();
21727 /* Set up for Dwarf output at the start of compilation. */
21729 static void
21730 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21732 /* Allocate the file_table. */
21733 file_table = htab_create_ggc (50, file_table_hash,
21734 file_table_eq, NULL);
21736 /* Allocate the decl_die_table. */
21737 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21738 decl_die_table_eq, NULL);
21740 /* Allocate the decl_loc_table. */
21741 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21742 decl_loc_table_eq, NULL);
21744 /* Allocate the cached_dw_loc_list_table. */
21745 cached_dw_loc_list_table
21746 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21747 cached_dw_loc_list_table_eq, NULL);
21749 /* Allocate the initial hunk of the decl_scope_table. */
21750 vec_alloc (decl_scope_table, 256);
21752 /* Allocate the initial hunk of the abbrev_die_table. */
21753 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21754 (ABBREV_DIE_TABLE_INCREMENT);
21755 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21756 /* Zero-th entry is allocated, but unused. */
21757 abbrev_die_table_in_use = 1;
21759 /* Allocate the pubtypes and pubnames vectors. */
21760 vec_alloc (pubname_table, 32);
21761 vec_alloc (pubtype_table, 32);
21763 vec_alloc (incomplete_types, 64);
21765 vec_alloc (used_rtx_array, 32);
21767 if (!dwarf_split_debug_info)
21769 debug_info_section = get_section (DEBUG_INFO_SECTION,
21770 SECTION_DEBUG, NULL);
21771 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21772 SECTION_DEBUG, NULL);
21773 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21774 SECTION_DEBUG, NULL);
21776 else
21778 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21779 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21780 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21781 SECTION_DEBUG | SECTION_EXCLUDE,
21782 NULL);
21783 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21784 SECTION_DEBUG, NULL);
21785 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21786 SECTION_DEBUG, NULL);
21787 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21788 SECTION_DEBUG, NULL);
21789 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21790 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21792 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21793 the main .o, but the skeleton_line goes into the split off dwo. */
21794 debug_skeleton_line_section
21795 = get_section (DEBUG_DWO_LINE_SECTION,
21796 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21797 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21798 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21799 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21800 SECTION_DEBUG | SECTION_EXCLUDE,
21801 NULL);
21802 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21803 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21804 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21805 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21806 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
21807 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
21809 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21810 SECTION_DEBUG, NULL);
21811 debug_macinfo_section = get_section (dwarf_strict
21812 ? DEBUG_MACINFO_SECTION
21813 : DEBUG_MACRO_SECTION,
21814 DEBUG_MACRO_SECTION_FLAGS, NULL);
21815 debug_line_section = get_section (DEBUG_LINE_SECTION,
21816 SECTION_DEBUG, NULL);
21817 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21818 SECTION_DEBUG, NULL);
21819 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21820 SECTION_DEBUG, NULL);
21821 debug_str_section = get_section (DEBUG_STR_SECTION,
21822 DEBUG_STR_SECTION_FLAGS, NULL);
21823 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21824 SECTION_DEBUG, NULL);
21825 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21826 SECTION_DEBUG, NULL);
21828 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21829 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21830 DEBUG_ABBREV_SECTION_LABEL, 0);
21831 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21832 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21833 COLD_TEXT_SECTION_LABEL, 0);
21834 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21836 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21837 DEBUG_INFO_SECTION_LABEL, 0);
21838 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21839 DEBUG_LINE_SECTION_LABEL, 0);
21840 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21841 DEBUG_RANGES_SECTION_LABEL, 0);
21842 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21843 DEBUG_ADDR_SECTION_LABEL, 0);
21844 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21845 dwarf_strict
21846 ? DEBUG_MACINFO_SECTION_LABEL
21847 : DEBUG_MACRO_SECTION_LABEL, 0);
21848 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21850 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21851 vec_alloc (macinfo_table, 64);
21853 switch_to_section (text_section);
21854 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21856 /* Make sure the line number table for .text always exists. */
21857 text_section_line_info = new_line_info_table ();
21858 text_section_line_info->end_label = text_end_label;
21861 /* Called before compile () starts outputtting functions, variables
21862 and toplevel asms into assembly. */
21864 static void
21865 dwarf2out_assembly_start (void)
21867 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21868 && dwarf2out_do_cfi_asm ()
21869 && (!(flag_unwind_tables || flag_exceptions)
21870 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21871 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21874 /* A helper function for dwarf2out_finish called through
21875 htab_traverse. Assign a string its index. All strings must be
21876 collected into the table by the time index_string is called,
21877 because the indexing code relies on htab_traverse to traverse nodes
21878 in the same order for each run. */
21880 static int
21881 index_string (void **h, void *v)
21883 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21884 unsigned int *index = (unsigned int *) v;
21886 find_string_form (node);
21887 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21889 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21890 node->index = *index;
21891 *index += 1;
21893 return 1;
21896 /* A helper function for output_indirect_strings called through
21897 htab_traverse. Output the offset to a string and update the
21898 current offset. */
21900 static int
21901 output_index_string_offset (void **h, void *v)
21903 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21904 unsigned int *offset = (unsigned int *) v;
21906 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21908 /* Assert that this node has been assigned an index. */
21909 gcc_assert (node->index != NO_INDEX_ASSIGNED
21910 && node->index != NOT_INDEXED);
21911 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21912 "indexed string 0x%x: %s", node->index, node->str);
21913 *offset += strlen (node->str) + 1;
21915 return 1;
21918 /* A helper function for dwarf2out_finish called through
21919 htab_traverse. Output the indexed string. */
21921 static int
21922 output_index_string (void **h, void *v)
21924 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21925 unsigned int *cur_idx = (unsigned int *) v;
21927 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21929 /* Assert that the strings are output in the same order as their
21930 indexes were assigned. */
21931 gcc_assert (*cur_idx == node->index);
21932 assemble_string (node->str, strlen (node->str) + 1);
21933 *cur_idx += 1;
21935 return 1;
21938 /* A helper function for dwarf2out_finish called through
21939 htab_traverse. Emit one queued .debug_str string. */
21941 static int
21942 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21944 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21946 node->form = find_string_form (node);
21947 if (node->form == DW_FORM_strp && node->refcount > 0)
21949 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21950 assemble_string (node->str, strlen (node->str) + 1);
21953 return 1;
21956 /* Output the indexed string table. */
21958 static void
21959 output_indirect_strings (void)
21961 switch_to_section (debug_str_section);
21962 if (!dwarf_split_debug_info)
21963 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21964 else
21966 unsigned int offset = 0;
21967 unsigned int cur_idx = 0;
21969 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
21971 switch_to_section (debug_str_offsets_section);
21972 htab_traverse_noresize (debug_str_hash,
21973 output_index_string_offset,
21974 &offset);
21975 switch_to_section (debug_str_dwo_section);
21976 htab_traverse_noresize (debug_str_hash,
21977 output_index_string,
21978 &cur_idx);
21982 /* Callback for htab_traverse to assign an index to an entry in the
21983 table, and to write that entry to the .debug_addr section. */
21985 static int
21986 output_addr_table_entry (void **slot, void *data)
21988 addr_table_entry *entry = (addr_table_entry *) *slot;
21989 unsigned int *cur_index = (unsigned int *)data;
21991 if (entry->refcount == 0)
21993 gcc_assert (entry->index == NO_INDEX_ASSIGNED
21994 || entry->index == NOT_INDEXED);
21995 return 1;
21998 gcc_assert (entry->index == *cur_index);
21999 (*cur_index)++;
22001 switch (entry->kind)
22003 case ate_kind_rtx:
22004 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22005 "0x%x", entry->index);
22006 break;
22007 case ate_kind_rtx_dtprel:
22008 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22009 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22010 DWARF2_ADDR_SIZE,
22011 entry->addr.rtl);
22012 fputc ('\n', asm_out_file);
22013 break;
22014 case ate_kind_label:
22015 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22016 "0x%x", entry->index);
22017 break;
22018 default:
22019 gcc_unreachable ();
22021 return 1;
22024 /* Produce the .debug_addr section. */
22026 static void
22027 output_addr_table (void)
22029 unsigned int index = 0;
22030 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22031 return;
22033 switch_to_section (debug_addr_section);
22034 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22037 #if ENABLE_ASSERT_CHECKING
22038 /* Verify that all marks are clear. */
22040 static void
22041 verify_marks_clear (dw_die_ref die)
22043 dw_die_ref c;
22045 gcc_assert (! die->die_mark);
22046 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22048 #endif /* ENABLE_ASSERT_CHECKING */
22050 /* Clear the marks for a die and its children.
22051 Be cool if the mark isn't set. */
22053 static void
22054 prune_unmark_dies (dw_die_ref die)
22056 dw_die_ref c;
22058 if (die->die_mark)
22059 die->die_mark = 0;
22060 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22063 /* Given DIE that we're marking as used, find any other dies
22064 it references as attributes and mark them as used. */
22066 static void
22067 prune_unused_types_walk_attribs (dw_die_ref die)
22069 dw_attr_ref a;
22070 unsigned ix;
22072 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22074 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22076 /* A reference to another DIE.
22077 Make sure that it will get emitted.
22078 If it was broken out into a comdat group, don't follow it. */
22079 if (! AT_ref (a)->comdat_type_p
22080 || a->dw_attr == DW_AT_specification)
22081 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22083 /* Set the string's refcount to 0 so that prune_unused_types_mark
22084 accounts properly for it. */
22085 if (AT_class (a) == dw_val_class_str)
22086 a->dw_attr_val.v.val_str->refcount = 0;
22090 /* Mark the generic parameters and arguments children DIEs of DIE. */
22092 static void
22093 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22095 dw_die_ref c;
22097 if (die == NULL || die->die_child == NULL)
22098 return;
22099 c = die->die_child;
22102 switch (c->die_tag)
22104 case DW_TAG_template_type_param:
22105 case DW_TAG_template_value_param:
22106 case DW_TAG_GNU_template_template_param:
22107 case DW_TAG_GNU_template_parameter_pack:
22108 prune_unused_types_mark (c, 1);
22109 break;
22110 default:
22111 break;
22113 c = c->die_sib;
22114 } while (c && c != die->die_child);
22117 /* Mark DIE as being used. If DOKIDS is true, then walk down
22118 to DIE's children. */
22120 static void
22121 prune_unused_types_mark (dw_die_ref die, int dokids)
22123 dw_die_ref c;
22125 if (die->die_mark == 0)
22127 /* We haven't done this node yet. Mark it as used. */
22128 die->die_mark = 1;
22129 /* If this is the DIE of a generic type instantiation,
22130 mark the children DIEs that describe its generic parms and
22131 args. */
22132 prune_unused_types_mark_generic_parms_dies (die);
22134 /* We also have to mark its parents as used.
22135 (But we don't want to mark our parent's kids due to this,
22136 unless it is a class.) */
22137 if (die->die_parent)
22138 prune_unused_types_mark (die->die_parent,
22139 class_scope_p (die->die_parent));
22141 /* Mark any referenced nodes. */
22142 prune_unused_types_walk_attribs (die);
22144 /* If this node is a specification,
22145 also mark the definition, if it exists. */
22146 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22147 prune_unused_types_mark (die->die_definition, 1);
22150 if (dokids && die->die_mark != 2)
22152 /* We need to walk the children, but haven't done so yet.
22153 Remember that we've walked the kids. */
22154 die->die_mark = 2;
22156 /* If this is an array type, we need to make sure our
22157 kids get marked, even if they're types. If we're
22158 breaking out types into comdat sections, do this
22159 for all type definitions. */
22160 if (die->die_tag == DW_TAG_array_type
22161 || (use_debug_types
22162 && is_type_die (die) && ! is_declaration_die (die)))
22163 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22164 else
22165 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22169 /* For local classes, look if any static member functions were emitted
22170 and if so, mark them. */
22172 static void
22173 prune_unused_types_walk_local_classes (dw_die_ref die)
22175 dw_die_ref c;
22177 if (die->die_mark == 2)
22178 return;
22180 switch (die->die_tag)
22182 case DW_TAG_structure_type:
22183 case DW_TAG_union_type:
22184 case DW_TAG_class_type:
22185 break;
22187 case DW_TAG_subprogram:
22188 if (!get_AT_flag (die, DW_AT_declaration)
22189 || die->die_definition != NULL)
22190 prune_unused_types_mark (die, 1);
22191 return;
22193 default:
22194 return;
22197 /* Mark children. */
22198 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22201 /* Walk the tree DIE and mark types that we actually use. */
22203 static void
22204 prune_unused_types_walk (dw_die_ref die)
22206 dw_die_ref c;
22208 /* Don't do anything if this node is already marked and
22209 children have been marked as well. */
22210 if (die->die_mark == 2)
22211 return;
22213 switch (die->die_tag)
22215 case DW_TAG_structure_type:
22216 case DW_TAG_union_type:
22217 case DW_TAG_class_type:
22218 if (die->die_perennial_p)
22219 break;
22221 for (c = die->die_parent; c; c = c->die_parent)
22222 if (c->die_tag == DW_TAG_subprogram)
22223 break;
22225 /* Finding used static member functions inside of classes
22226 is needed just for local classes, because for other classes
22227 static member function DIEs with DW_AT_specification
22228 are emitted outside of the DW_TAG_*_type. If we ever change
22229 it, we'd need to call this even for non-local classes. */
22230 if (c)
22231 prune_unused_types_walk_local_classes (die);
22233 /* It's a type node --- don't mark it. */
22234 return;
22236 case DW_TAG_const_type:
22237 case DW_TAG_packed_type:
22238 case DW_TAG_pointer_type:
22239 case DW_TAG_reference_type:
22240 case DW_TAG_rvalue_reference_type:
22241 case DW_TAG_volatile_type:
22242 case DW_TAG_typedef:
22243 case DW_TAG_array_type:
22244 case DW_TAG_interface_type:
22245 case DW_TAG_friend:
22246 case DW_TAG_variant_part:
22247 case DW_TAG_enumeration_type:
22248 case DW_TAG_subroutine_type:
22249 case DW_TAG_string_type:
22250 case DW_TAG_set_type:
22251 case DW_TAG_subrange_type:
22252 case DW_TAG_ptr_to_member_type:
22253 case DW_TAG_file_type:
22254 if (die->die_perennial_p)
22255 break;
22257 /* It's a type node --- don't mark it. */
22258 return;
22260 default:
22261 /* Mark everything else. */
22262 break;
22265 if (die->die_mark == 0)
22267 die->die_mark = 1;
22269 /* Now, mark any dies referenced from here. */
22270 prune_unused_types_walk_attribs (die);
22273 die->die_mark = 2;
22275 /* Mark children. */
22276 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22279 /* Increment the string counts on strings referred to from DIE's
22280 attributes. */
22282 static void
22283 prune_unused_types_update_strings (dw_die_ref die)
22285 dw_attr_ref a;
22286 unsigned ix;
22288 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22289 if (AT_class (a) == dw_val_class_str)
22291 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22292 s->refcount++;
22293 /* Avoid unnecessarily putting strings that are used less than
22294 twice in the hash table. */
22295 if (s->refcount
22296 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22298 void ** slot;
22299 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22300 htab_hash_string (s->str),
22301 INSERT);
22302 gcc_assert (*slot == NULL);
22303 *slot = s;
22308 /* Remove from the tree DIE any dies that aren't marked. */
22310 static void
22311 prune_unused_types_prune (dw_die_ref die)
22313 dw_die_ref c;
22315 gcc_assert (die->die_mark);
22316 prune_unused_types_update_strings (die);
22318 if (! die->die_child)
22319 return;
22321 c = die->die_child;
22322 do {
22323 dw_die_ref prev = c;
22324 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22325 if (c == die->die_child)
22327 /* No marked children between 'prev' and the end of the list. */
22328 if (prev == c)
22329 /* No marked children at all. */
22330 die->die_child = NULL;
22331 else
22333 prev->die_sib = c->die_sib;
22334 die->die_child = prev;
22336 return;
22339 if (c != prev->die_sib)
22340 prev->die_sib = c;
22341 prune_unused_types_prune (c);
22342 } while (c != die->die_child);
22345 /* Remove dies representing declarations that we never use. */
22347 static void
22348 prune_unused_types (void)
22350 unsigned int i;
22351 limbo_die_node *node;
22352 comdat_type_node *ctnode;
22353 pubname_ref pub;
22354 dw_die_ref base_type;
22356 #if ENABLE_ASSERT_CHECKING
22357 /* All the marks should already be clear. */
22358 verify_marks_clear (comp_unit_die ());
22359 for (node = limbo_die_list; node; node = node->next)
22360 verify_marks_clear (node->die);
22361 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22362 verify_marks_clear (ctnode->root_die);
22363 #endif /* ENABLE_ASSERT_CHECKING */
22365 /* Mark types that are used in global variables. */
22366 premark_types_used_by_global_vars ();
22368 /* Set the mark on nodes that are actually used. */
22369 prune_unused_types_walk (comp_unit_die ());
22370 for (node = limbo_die_list; node; node = node->next)
22371 prune_unused_types_walk (node->die);
22372 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22374 prune_unused_types_walk (ctnode->root_die);
22375 prune_unused_types_mark (ctnode->type_die, 1);
22378 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22379 are unusual in that they are pubnames that are the children of pubtypes.
22380 They should only be marked via their parent DW_TAG_enumeration_type die,
22381 not as roots in themselves. */
22382 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22383 if (pub->die->die_tag != DW_TAG_enumerator)
22384 prune_unused_types_mark (pub->die, 1);
22385 for (i = 0; base_types.iterate (i, &base_type); i++)
22386 prune_unused_types_mark (base_type, 1);
22388 if (debug_str_hash)
22389 htab_empty (debug_str_hash);
22390 if (skeleton_debug_str_hash)
22391 htab_empty (skeleton_debug_str_hash);
22392 prune_unused_types_prune (comp_unit_die ());
22393 for (node = limbo_die_list; node; node = node->next)
22394 prune_unused_types_prune (node->die);
22395 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22396 prune_unused_types_prune (ctnode->root_die);
22398 /* Leave the marks clear. */
22399 prune_unmark_dies (comp_unit_die ());
22400 for (node = limbo_die_list; node; node = node->next)
22401 prune_unmark_dies (node->die);
22402 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22403 prune_unmark_dies (ctnode->root_die);
22406 /* Set the parameter to true if there are any relative pathnames in
22407 the file table. */
22408 static int
22409 file_table_relative_p (void ** slot, void *param)
22411 bool *p = (bool *) param;
22412 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22413 if (!IS_ABSOLUTE_PATH (d->filename))
22415 *p = true;
22416 return 0;
22418 return 1;
22421 /* Helpers to manipulate hash table of comdat type units. */
22423 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22425 typedef comdat_type_node value_type;
22426 typedef comdat_type_node compare_type;
22427 static inline hashval_t hash (const value_type *);
22428 static inline bool equal (const value_type *, const compare_type *);
22431 inline hashval_t
22432 comdat_type_hasher::hash (const value_type *type_node)
22434 hashval_t h;
22435 memcpy (&h, type_node->signature, sizeof (h));
22436 return h;
22439 inline bool
22440 comdat_type_hasher::equal (const value_type *type_node_1,
22441 const compare_type *type_node_2)
22443 return (! memcmp (type_node_1->signature, type_node_2->signature,
22444 DWARF_TYPE_SIGNATURE_SIZE));
22447 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22448 to the location it would have been added, should we know its
22449 DECL_ASSEMBLER_NAME when we added other attributes. This will
22450 probably improve compactness of debug info, removing equivalent
22451 abbrevs, and hide any differences caused by deferring the
22452 computation of the assembler name, triggered by e.g. PCH. */
22454 static inline void
22455 move_linkage_attr (dw_die_ref die)
22457 unsigned ix = vec_safe_length (die->die_attr);
22458 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22460 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22461 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22463 while (--ix > 0)
22465 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22467 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22468 break;
22471 if (ix != vec_safe_length (die->die_attr) - 1)
22473 die->die_attr->pop ();
22474 die->die_attr->quick_insert (ix, linkage);
22478 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22479 referenced from typed stack ops and count how often they are used. */
22481 static void
22482 mark_base_types (dw_loc_descr_ref loc)
22484 dw_die_ref base_type = NULL;
22486 for (; loc; loc = loc->dw_loc_next)
22488 switch (loc->dw_loc_opc)
22490 case DW_OP_GNU_regval_type:
22491 case DW_OP_GNU_deref_type:
22492 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22493 break;
22494 case DW_OP_GNU_convert:
22495 case DW_OP_GNU_reinterpret:
22496 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22497 continue;
22498 /* FALLTHRU */
22499 case DW_OP_GNU_const_type:
22500 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22501 break;
22502 case DW_OP_GNU_entry_value:
22503 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22504 continue;
22505 default:
22506 continue;
22508 gcc_assert (base_type->die_parent == comp_unit_die ());
22509 if (base_type->die_mark)
22510 base_type->die_mark++;
22511 else
22513 base_types.safe_push (base_type);
22514 base_type->die_mark = 1;
22519 /* Comparison function for sorting marked base types. */
22521 static int
22522 base_type_cmp (const void *x, const void *y)
22524 dw_die_ref dx = *(const dw_die_ref *) x;
22525 dw_die_ref dy = *(const dw_die_ref *) y;
22526 unsigned int byte_size1, byte_size2;
22527 unsigned int encoding1, encoding2;
22528 if (dx->die_mark > dy->die_mark)
22529 return -1;
22530 if (dx->die_mark < dy->die_mark)
22531 return 1;
22532 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22533 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22534 if (byte_size1 < byte_size2)
22535 return 1;
22536 if (byte_size1 > byte_size2)
22537 return -1;
22538 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22539 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22540 if (encoding1 < encoding2)
22541 return 1;
22542 if (encoding1 > encoding2)
22543 return -1;
22544 return 0;
22547 /* Move base types marked by mark_base_types as early as possible
22548 in the CU, sorted by decreasing usage count both to make the
22549 uleb128 references as small as possible and to make sure they
22550 will have die_offset already computed by calc_die_sizes when
22551 sizes of typed stack loc ops is computed. */
22553 static void
22554 move_marked_base_types (void)
22556 unsigned int i;
22557 dw_die_ref base_type, die, c;
22559 if (base_types.is_empty ())
22560 return;
22562 /* Sort by decreasing usage count, they will be added again in that
22563 order later on. */
22564 base_types.qsort (base_type_cmp);
22565 die = comp_unit_die ();
22566 c = die->die_child;
22569 dw_die_ref prev = c;
22570 c = c->die_sib;
22571 while (c->die_mark)
22573 remove_child_with_prev (c, prev);
22574 /* As base types got marked, there must be at least
22575 one node other than DW_TAG_base_type. */
22576 gcc_assert (c != c->die_sib);
22577 c = c->die_sib;
22580 while (c != die->die_child);
22581 gcc_assert (die->die_child);
22582 c = die->die_child;
22583 for (i = 0; base_types.iterate (i, &base_type); i++)
22585 base_type->die_mark = 0;
22586 base_type->die_sib = c->die_sib;
22587 c->die_sib = base_type;
22588 c = base_type;
22592 /* Helper function for resolve_addr, attempt to resolve
22593 one CONST_STRING, return non-zero if not successful. Similarly verify that
22594 SYMBOL_REFs refer to variables emitted in the current CU. */
22596 static int
22597 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22599 rtx rtl = *addr;
22601 if (GET_CODE (rtl) == CONST_STRING)
22603 size_t len = strlen (XSTR (rtl, 0)) + 1;
22604 tree t = build_string (len, XSTR (rtl, 0));
22605 tree tlen = size_int (len - 1);
22606 TREE_TYPE (t)
22607 = build_array_type (char_type_node, build_index_type (tlen));
22608 rtl = lookup_constant_def (t);
22609 if (!rtl || !MEM_P (rtl))
22610 return 1;
22611 rtl = XEXP (rtl, 0);
22612 if (GET_CODE (rtl) == SYMBOL_REF
22613 && SYMBOL_REF_DECL (rtl)
22614 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22615 return 1;
22616 vec_safe_push (used_rtx_array, rtl);
22617 *addr = rtl;
22618 return 0;
22621 if (GET_CODE (rtl) == SYMBOL_REF
22622 && SYMBOL_REF_DECL (rtl))
22624 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22626 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22627 return 1;
22629 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22630 return 1;
22633 if (GET_CODE (rtl) == CONST
22634 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22635 return 1;
22637 return 0;
22640 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22641 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22642 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22644 static rtx
22645 string_cst_pool_decl (tree t)
22647 rtx rtl = output_constant_def (t, 1);
22648 unsigned char *array;
22649 dw_loc_descr_ref l;
22650 tree decl;
22651 size_t len;
22652 dw_die_ref ref;
22654 if (!rtl || !MEM_P (rtl))
22655 return NULL_RTX;
22656 rtl = XEXP (rtl, 0);
22657 if (GET_CODE (rtl) != SYMBOL_REF
22658 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22659 return NULL_RTX;
22661 decl = SYMBOL_REF_DECL (rtl);
22662 if (!lookup_decl_die (decl))
22664 len = TREE_STRING_LENGTH (t);
22665 vec_safe_push (used_rtx_array, rtl);
22666 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22667 array = (unsigned char *) ggc_alloc_atomic (len);
22668 memcpy (array, TREE_STRING_POINTER (t), len);
22669 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22670 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22671 l->dw_loc_oprnd2.v.val_vec.length = len;
22672 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22673 l->dw_loc_oprnd2.v.val_vec.array = array;
22674 add_AT_loc (ref, DW_AT_location, l);
22675 equate_decl_number_to_die (decl, ref);
22677 return rtl;
22680 /* Helper function of resolve_addr_in_expr. LOC is
22681 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22682 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22683 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22684 with DW_OP_GNU_implicit_pointer if possible
22685 and return true, if unsuccesful, return false. */
22687 static bool
22688 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22690 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22691 HOST_WIDE_INT offset = 0;
22692 dw_die_ref ref = NULL;
22693 tree decl;
22695 if (GET_CODE (rtl) == CONST
22696 && GET_CODE (XEXP (rtl, 0)) == PLUS
22697 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22699 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22700 rtl = XEXP (XEXP (rtl, 0), 0);
22702 if (GET_CODE (rtl) == CONST_STRING)
22704 size_t len = strlen (XSTR (rtl, 0)) + 1;
22705 tree t = build_string (len, XSTR (rtl, 0));
22706 tree tlen = size_int (len - 1);
22708 TREE_TYPE (t)
22709 = build_array_type (char_type_node, build_index_type (tlen));
22710 rtl = string_cst_pool_decl (t);
22711 if (!rtl)
22712 return false;
22714 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22716 decl = SYMBOL_REF_DECL (rtl);
22717 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22719 ref = lookup_decl_die (decl);
22720 if (ref && (get_AT (ref, DW_AT_location)
22721 || get_AT (ref, DW_AT_const_value)))
22723 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22724 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22725 loc->dw_loc_oprnd1.val_entry = NULL;
22726 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22727 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22728 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22729 loc->dw_loc_oprnd2.v.val_int = offset;
22730 return true;
22734 return false;
22737 /* Helper function for resolve_addr, handle one location
22738 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22739 the location list couldn't be resolved. */
22741 static bool
22742 resolve_addr_in_expr (dw_loc_descr_ref loc)
22744 dw_loc_descr_ref keep = NULL;
22745 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22746 switch (loc->dw_loc_opc)
22748 case DW_OP_addr:
22749 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22751 if ((prev == NULL
22752 || prev->dw_loc_opc == DW_OP_piece
22753 || prev->dw_loc_opc == DW_OP_bit_piece)
22754 && loc->dw_loc_next
22755 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22756 && !dwarf_strict
22757 && optimize_one_addr_into_implicit_ptr (loc))
22758 break;
22759 return false;
22761 break;
22762 case DW_OP_GNU_addr_index:
22763 case DW_OP_GNU_const_index:
22764 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22765 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22766 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22767 NULL))
22768 return false;
22769 break;
22770 case DW_OP_const4u:
22771 case DW_OP_const8u:
22772 if (loc->dtprel
22773 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22774 return false;
22775 break;
22776 case DW_OP_plus_uconst:
22777 if (size_of_loc_descr (loc)
22778 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22780 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22782 dw_loc_descr_ref repl
22783 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22784 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22785 add_loc_descr (&repl, loc->dw_loc_next);
22786 *loc = *repl;
22788 break;
22789 case DW_OP_implicit_value:
22790 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22791 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22792 return false;
22793 break;
22794 case DW_OP_GNU_implicit_pointer:
22795 case DW_OP_GNU_parameter_ref:
22796 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22798 dw_die_ref ref
22799 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22800 if (ref == NULL)
22801 return false;
22802 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22803 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22804 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22806 break;
22807 case DW_OP_GNU_const_type:
22808 case DW_OP_GNU_regval_type:
22809 case DW_OP_GNU_deref_type:
22810 case DW_OP_GNU_convert:
22811 case DW_OP_GNU_reinterpret:
22812 while (loc->dw_loc_next
22813 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22815 dw_die_ref base1, base2;
22816 unsigned enc1, enc2, size1, size2;
22817 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22818 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22819 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22820 else if (loc->dw_loc_oprnd1.val_class
22821 == dw_val_class_unsigned_const)
22822 break;
22823 else
22824 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22825 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22826 == dw_val_class_unsigned_const)
22827 break;
22828 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22829 gcc_assert (base1->die_tag == DW_TAG_base_type
22830 && base2->die_tag == DW_TAG_base_type);
22831 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22832 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22833 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22834 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22835 if (size1 == size2
22836 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22837 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22838 && loc != keep)
22839 || enc1 == enc2))
22841 /* Optimize away next DW_OP_GNU_convert after
22842 adjusting LOC's base type die reference. */
22843 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22844 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22845 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22846 else
22847 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22848 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22849 continue;
22851 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22852 point typed stack entry. */
22853 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22854 keep = loc->dw_loc_next;
22855 break;
22857 break;
22858 default:
22859 break;
22861 return true;
22864 /* Helper function of resolve_addr. DIE had DW_AT_location of
22865 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
22866 and DW_OP_addr couldn't be resolved. resolve_addr has already
22867 removed the DW_AT_location attribute. This function attempts to
22868 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
22869 to it or DW_AT_const_value attribute, if possible. */
22871 static void
22872 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
22874 if (TREE_CODE (decl) != VAR_DECL
22875 || lookup_decl_die (decl) != die
22876 || DECL_EXTERNAL (decl)
22877 || !TREE_STATIC (decl)
22878 || DECL_INITIAL (decl) == NULL_TREE
22879 || DECL_P (DECL_INITIAL (decl))
22880 || get_AT (die, DW_AT_const_value))
22881 return;
22883 tree init = DECL_INITIAL (decl);
22884 HOST_WIDE_INT offset = 0;
22885 /* For variables that have been optimized away and thus
22886 don't have a memory location, see if we can emit
22887 DW_AT_const_value instead. */
22888 if (tree_add_const_value_attribute (die, init))
22889 return;
22890 if (dwarf_strict)
22891 return;
22892 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
22893 and ADDR_EXPR refers to a decl that has DW_AT_location or
22894 DW_AT_const_value (but isn't addressable, otherwise
22895 resolving the original DW_OP_addr wouldn't fail), see if
22896 we can add DW_OP_GNU_implicit_pointer. */
22897 STRIP_NOPS (init);
22898 if (TREE_CODE (init) == POINTER_PLUS_EXPR
22899 && host_integerp (TREE_OPERAND (init, 1), 0))
22901 offset = tree_low_cst (TREE_OPERAND (init, 1), 0);
22902 init = TREE_OPERAND (init, 0);
22903 STRIP_NOPS (init);
22905 if (TREE_CODE (init) != ADDR_EXPR)
22906 return;
22907 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
22908 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
22909 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
22910 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
22911 && TREE_OPERAND (init, 0) != decl))
22913 dw_die_ref ref;
22914 dw_loc_descr_ref l;
22916 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
22918 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
22919 if (!rtl)
22920 return;
22921 decl = SYMBOL_REF_DECL (rtl);
22923 else
22924 decl = TREE_OPERAND (init, 0);
22925 ref = lookup_decl_die (decl);
22926 if (ref == NULL
22927 || (!get_AT (ref, DW_AT_location)
22928 && !get_AT (ref, DW_AT_const_value)))
22929 return;
22930 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
22931 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22932 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
22933 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
22934 add_AT_loc (die, DW_AT_location, l);
22938 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22939 an address in .rodata section if the string literal is emitted there,
22940 or remove the containing location list or replace DW_AT_const_value
22941 with DW_AT_location and empty location expression, if it isn't found
22942 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22943 to something that has been emitted in the current CU. */
22945 static void
22946 resolve_addr (dw_die_ref die)
22948 dw_die_ref c;
22949 dw_attr_ref a;
22950 dw_loc_list_ref *curr, *start, loc;
22951 unsigned ix;
22953 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22954 switch (AT_class (a))
22956 case dw_val_class_loc_list:
22957 start = curr = AT_loc_list_ptr (a);
22958 loc = *curr;
22959 gcc_assert (loc);
22960 /* The same list can be referenced more than once. See if we have
22961 already recorded the result from a previous pass. */
22962 if (loc->replaced)
22963 *curr = loc->dw_loc_next;
22964 else if (!loc->resolved_addr)
22966 /* As things stand, we do not expect or allow one die to
22967 reference a suffix of another die's location list chain.
22968 References must be identical or completely separate.
22969 There is therefore no need to cache the result of this
22970 pass on any list other than the first; doing so
22971 would lead to unnecessary writes. */
22972 while (*curr)
22974 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22975 if (!resolve_addr_in_expr ((*curr)->expr))
22977 dw_loc_list_ref next = (*curr)->dw_loc_next;
22978 dw_loc_descr_ref l = (*curr)->expr;
22980 if (next && (*curr)->ll_symbol)
22982 gcc_assert (!next->ll_symbol);
22983 next->ll_symbol = (*curr)->ll_symbol;
22985 if (dwarf_split_debug_info)
22986 remove_loc_list_addr_table_entries (l);
22987 *curr = next;
22989 else
22991 mark_base_types ((*curr)->expr);
22992 curr = &(*curr)->dw_loc_next;
22995 if (loc == *start)
22996 loc->resolved_addr = 1;
22997 else
22999 loc->replaced = 1;
23000 loc->dw_loc_next = *start;
23003 if (!*start)
23005 remove_AT (die, a->dw_attr);
23006 ix--;
23008 break;
23009 case dw_val_class_loc:
23011 dw_loc_descr_ref l = AT_loc (a);
23012 /* For -gdwarf-2 don't attempt to optimize
23013 DW_AT_data_member_location containing
23014 DW_OP_plus_uconst - older consumers might
23015 rely on it being that op instead of a more complex,
23016 but shorter, location description. */
23017 if ((dwarf_version > 2
23018 || a->dw_attr != DW_AT_data_member_location
23019 || l == NULL
23020 || l->dw_loc_opc != DW_OP_plus_uconst
23021 || l->dw_loc_next != NULL)
23022 && !resolve_addr_in_expr (l))
23024 if (dwarf_split_debug_info)
23025 remove_loc_list_addr_table_entries (l);
23026 if (l != NULL
23027 && l->dw_loc_next == NULL
23028 && l->dw_loc_opc == DW_OP_addr
23029 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23030 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23031 && a->dw_attr == DW_AT_location)
23033 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23034 remove_AT (die, a->dw_attr);
23035 ix--;
23036 optimize_location_into_implicit_ptr (die, decl);
23037 break;
23039 remove_AT (die, a->dw_attr);
23040 ix--;
23042 else
23043 mark_base_types (l);
23045 break;
23046 case dw_val_class_addr:
23047 if (a->dw_attr == DW_AT_const_value
23048 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23050 if (AT_index (a) != NOT_INDEXED)
23051 remove_addr_table_entry (a->dw_attr_val.val_entry);
23052 remove_AT (die, a->dw_attr);
23053 ix--;
23055 if (die->die_tag == DW_TAG_GNU_call_site
23056 && a->dw_attr == DW_AT_abstract_origin)
23058 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23059 dw_die_ref tdie = lookup_decl_die (tdecl);
23060 if (tdie == NULL
23061 && DECL_EXTERNAL (tdecl)
23062 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23064 force_decl_die (tdecl);
23065 tdie = lookup_decl_die (tdecl);
23067 if (tdie)
23069 a->dw_attr_val.val_class = dw_val_class_die_ref;
23070 a->dw_attr_val.v.val_die_ref.die = tdie;
23071 a->dw_attr_val.v.val_die_ref.external = 0;
23073 else
23075 if (AT_index (a) != NOT_INDEXED)
23076 remove_addr_table_entry (a->dw_attr_val.val_entry);
23077 remove_AT (die, a->dw_attr);
23078 ix--;
23081 break;
23082 default:
23083 break;
23086 FOR_EACH_CHILD (die, c, resolve_addr (c));
23089 /* Helper routines for optimize_location_lists.
23090 This pass tries to share identical local lists in .debug_loc
23091 section. */
23093 /* Iteratively hash operands of LOC opcode. */
23095 static inline hashval_t
23096 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23098 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23099 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23101 switch (loc->dw_loc_opc)
23103 case DW_OP_const4u:
23104 case DW_OP_const8u:
23105 if (loc->dtprel)
23106 goto hash_addr;
23107 /* FALLTHRU */
23108 case DW_OP_const1u:
23109 case DW_OP_const1s:
23110 case DW_OP_const2u:
23111 case DW_OP_const2s:
23112 case DW_OP_const4s:
23113 case DW_OP_const8s:
23114 case DW_OP_constu:
23115 case DW_OP_consts:
23116 case DW_OP_pick:
23117 case DW_OP_plus_uconst:
23118 case DW_OP_breg0:
23119 case DW_OP_breg1:
23120 case DW_OP_breg2:
23121 case DW_OP_breg3:
23122 case DW_OP_breg4:
23123 case DW_OP_breg5:
23124 case DW_OP_breg6:
23125 case DW_OP_breg7:
23126 case DW_OP_breg8:
23127 case DW_OP_breg9:
23128 case DW_OP_breg10:
23129 case DW_OP_breg11:
23130 case DW_OP_breg12:
23131 case DW_OP_breg13:
23132 case DW_OP_breg14:
23133 case DW_OP_breg15:
23134 case DW_OP_breg16:
23135 case DW_OP_breg17:
23136 case DW_OP_breg18:
23137 case DW_OP_breg19:
23138 case DW_OP_breg20:
23139 case DW_OP_breg21:
23140 case DW_OP_breg22:
23141 case DW_OP_breg23:
23142 case DW_OP_breg24:
23143 case DW_OP_breg25:
23144 case DW_OP_breg26:
23145 case DW_OP_breg27:
23146 case DW_OP_breg28:
23147 case DW_OP_breg29:
23148 case DW_OP_breg30:
23149 case DW_OP_breg31:
23150 case DW_OP_regx:
23151 case DW_OP_fbreg:
23152 case DW_OP_piece:
23153 case DW_OP_deref_size:
23154 case DW_OP_xderef_size:
23155 hash = iterative_hash_object (val1->v.val_int, hash);
23156 break;
23157 case DW_OP_skip:
23158 case DW_OP_bra:
23160 int offset;
23162 gcc_assert (val1->val_class == dw_val_class_loc);
23163 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23164 hash = iterative_hash_object (offset, hash);
23166 break;
23167 case DW_OP_implicit_value:
23168 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23169 switch (val2->val_class)
23171 case dw_val_class_const:
23172 hash = iterative_hash_object (val2->v.val_int, hash);
23173 break;
23174 case dw_val_class_vec:
23176 unsigned int elt_size = val2->v.val_vec.elt_size;
23177 unsigned int len = val2->v.val_vec.length;
23179 hash = iterative_hash_object (elt_size, hash);
23180 hash = iterative_hash_object (len, hash);
23181 hash = iterative_hash (val2->v.val_vec.array,
23182 len * elt_size, hash);
23184 break;
23185 case dw_val_class_const_double:
23186 hash = iterative_hash_object (val2->v.val_double.low, hash);
23187 hash = iterative_hash_object (val2->v.val_double.high, hash);
23188 break;
23189 case dw_val_class_addr:
23190 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23191 break;
23192 default:
23193 gcc_unreachable ();
23195 break;
23196 case DW_OP_bregx:
23197 case DW_OP_bit_piece:
23198 hash = iterative_hash_object (val1->v.val_int, hash);
23199 hash = iterative_hash_object (val2->v.val_int, hash);
23200 break;
23201 case DW_OP_addr:
23202 hash_addr:
23203 if (loc->dtprel)
23205 unsigned char dtprel = 0xd1;
23206 hash = iterative_hash_object (dtprel, hash);
23208 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23209 break;
23210 case DW_OP_GNU_addr_index:
23211 case DW_OP_GNU_const_index:
23213 if (loc->dtprel)
23215 unsigned char dtprel = 0xd1;
23216 hash = iterative_hash_object (dtprel, hash);
23218 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23220 break;
23221 case DW_OP_GNU_implicit_pointer:
23222 hash = iterative_hash_object (val2->v.val_int, hash);
23223 break;
23224 case DW_OP_GNU_entry_value:
23225 hash = hash_loc_operands (val1->v.val_loc, hash);
23226 break;
23227 case DW_OP_GNU_regval_type:
23228 case DW_OP_GNU_deref_type:
23230 unsigned int byte_size
23231 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23232 unsigned int encoding
23233 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23234 hash = iterative_hash_object (val1->v.val_int, hash);
23235 hash = iterative_hash_object (byte_size, hash);
23236 hash = iterative_hash_object (encoding, hash);
23238 break;
23239 case DW_OP_GNU_convert:
23240 case DW_OP_GNU_reinterpret:
23241 if (val1->val_class == dw_val_class_unsigned_const)
23243 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23244 break;
23246 /* FALLTHRU */
23247 case DW_OP_GNU_const_type:
23249 unsigned int byte_size
23250 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23251 unsigned int encoding
23252 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23253 hash = iterative_hash_object (byte_size, hash);
23254 hash = iterative_hash_object (encoding, hash);
23255 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23256 break;
23257 hash = iterative_hash_object (val2->val_class, hash);
23258 switch (val2->val_class)
23260 case dw_val_class_const:
23261 hash = iterative_hash_object (val2->v.val_int, hash);
23262 break;
23263 case dw_val_class_vec:
23265 unsigned int elt_size = val2->v.val_vec.elt_size;
23266 unsigned int len = val2->v.val_vec.length;
23268 hash = iterative_hash_object (elt_size, hash);
23269 hash = iterative_hash_object (len, hash);
23270 hash = iterative_hash (val2->v.val_vec.array,
23271 len * elt_size, hash);
23273 break;
23274 case dw_val_class_const_double:
23275 hash = iterative_hash_object (val2->v.val_double.low, hash);
23276 hash = iterative_hash_object (val2->v.val_double.high, hash);
23277 break;
23278 default:
23279 gcc_unreachable ();
23282 break;
23284 default:
23285 /* Other codes have no operands. */
23286 break;
23288 return hash;
23291 /* Iteratively hash the whole DWARF location expression LOC. */
23293 static inline hashval_t
23294 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23296 dw_loc_descr_ref l;
23297 bool sizes_computed = false;
23298 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23299 size_of_locs (loc);
23301 for (l = loc; l != NULL; l = l->dw_loc_next)
23303 enum dwarf_location_atom opc = l->dw_loc_opc;
23304 hash = iterative_hash_object (opc, hash);
23305 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23307 size_of_locs (loc);
23308 sizes_computed = true;
23310 hash = hash_loc_operands (l, hash);
23312 return hash;
23315 /* Compute hash of the whole location list LIST_HEAD. */
23317 static inline void
23318 hash_loc_list (dw_loc_list_ref list_head)
23320 dw_loc_list_ref curr = list_head;
23321 hashval_t hash = 0;
23323 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23325 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23326 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23327 if (curr->section)
23328 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23329 hash);
23330 hash = hash_locs (curr->expr, hash);
23332 list_head->hash = hash;
23335 /* Return true if X and Y opcodes have the same operands. */
23337 static inline bool
23338 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23340 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23341 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23342 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23343 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23345 switch (x->dw_loc_opc)
23347 case DW_OP_const4u:
23348 case DW_OP_const8u:
23349 if (x->dtprel)
23350 goto hash_addr;
23351 /* FALLTHRU */
23352 case DW_OP_const1u:
23353 case DW_OP_const1s:
23354 case DW_OP_const2u:
23355 case DW_OP_const2s:
23356 case DW_OP_const4s:
23357 case DW_OP_const8s:
23358 case DW_OP_constu:
23359 case DW_OP_consts:
23360 case DW_OP_pick:
23361 case DW_OP_plus_uconst:
23362 case DW_OP_breg0:
23363 case DW_OP_breg1:
23364 case DW_OP_breg2:
23365 case DW_OP_breg3:
23366 case DW_OP_breg4:
23367 case DW_OP_breg5:
23368 case DW_OP_breg6:
23369 case DW_OP_breg7:
23370 case DW_OP_breg8:
23371 case DW_OP_breg9:
23372 case DW_OP_breg10:
23373 case DW_OP_breg11:
23374 case DW_OP_breg12:
23375 case DW_OP_breg13:
23376 case DW_OP_breg14:
23377 case DW_OP_breg15:
23378 case DW_OP_breg16:
23379 case DW_OP_breg17:
23380 case DW_OP_breg18:
23381 case DW_OP_breg19:
23382 case DW_OP_breg20:
23383 case DW_OP_breg21:
23384 case DW_OP_breg22:
23385 case DW_OP_breg23:
23386 case DW_OP_breg24:
23387 case DW_OP_breg25:
23388 case DW_OP_breg26:
23389 case DW_OP_breg27:
23390 case DW_OP_breg28:
23391 case DW_OP_breg29:
23392 case DW_OP_breg30:
23393 case DW_OP_breg31:
23394 case DW_OP_regx:
23395 case DW_OP_fbreg:
23396 case DW_OP_piece:
23397 case DW_OP_deref_size:
23398 case DW_OP_xderef_size:
23399 return valx1->v.val_int == valy1->v.val_int;
23400 case DW_OP_skip:
23401 case DW_OP_bra:
23402 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23403 can cause irrelevant differences in dw_loc_addr. */
23404 gcc_assert (valx1->val_class == dw_val_class_loc
23405 && valy1->val_class == dw_val_class_loc
23406 && (dwarf_split_debug_info
23407 || x->dw_loc_addr == y->dw_loc_addr));
23408 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23409 case DW_OP_implicit_value:
23410 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23411 || valx2->val_class != valy2->val_class)
23412 return false;
23413 switch (valx2->val_class)
23415 case dw_val_class_const:
23416 return valx2->v.val_int == valy2->v.val_int;
23417 case dw_val_class_vec:
23418 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23419 && valx2->v.val_vec.length == valy2->v.val_vec.length
23420 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23421 valx2->v.val_vec.elt_size
23422 * valx2->v.val_vec.length) == 0;
23423 case dw_val_class_const_double:
23424 return valx2->v.val_double.low == valy2->v.val_double.low
23425 && valx2->v.val_double.high == valy2->v.val_double.high;
23426 case dw_val_class_addr:
23427 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23428 default:
23429 gcc_unreachable ();
23431 case DW_OP_bregx:
23432 case DW_OP_bit_piece:
23433 return valx1->v.val_int == valy1->v.val_int
23434 && valx2->v.val_int == valy2->v.val_int;
23435 case DW_OP_addr:
23436 hash_addr:
23437 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23438 case DW_OP_GNU_addr_index:
23439 case DW_OP_GNU_const_index:
23441 rtx ax1 = valx1->val_entry->addr.rtl;
23442 rtx ay1 = valy1->val_entry->addr.rtl;
23443 return rtx_equal_p (ax1, ay1);
23445 case DW_OP_GNU_implicit_pointer:
23446 return valx1->val_class == dw_val_class_die_ref
23447 && valx1->val_class == valy1->val_class
23448 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23449 && valx2->v.val_int == valy2->v.val_int;
23450 case DW_OP_GNU_entry_value:
23451 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23452 case DW_OP_GNU_const_type:
23453 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23454 || valx2->val_class != valy2->val_class)
23455 return false;
23456 switch (valx2->val_class)
23458 case dw_val_class_const:
23459 return valx2->v.val_int == valy2->v.val_int;
23460 case dw_val_class_vec:
23461 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23462 && valx2->v.val_vec.length == valy2->v.val_vec.length
23463 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23464 valx2->v.val_vec.elt_size
23465 * valx2->v.val_vec.length) == 0;
23466 case dw_val_class_const_double:
23467 return valx2->v.val_double.low == valy2->v.val_double.low
23468 && valx2->v.val_double.high == valy2->v.val_double.high;
23469 default:
23470 gcc_unreachable ();
23472 case DW_OP_GNU_regval_type:
23473 case DW_OP_GNU_deref_type:
23474 return valx1->v.val_int == valy1->v.val_int
23475 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23476 case DW_OP_GNU_convert:
23477 case DW_OP_GNU_reinterpret:
23478 if (valx1->val_class != valy1->val_class)
23479 return false;
23480 if (valx1->val_class == dw_val_class_unsigned_const)
23481 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23482 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23483 case DW_OP_GNU_parameter_ref:
23484 return valx1->val_class == dw_val_class_die_ref
23485 && valx1->val_class == valy1->val_class
23486 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23487 default:
23488 /* Other codes have no operands. */
23489 return true;
23493 /* Return true if DWARF location expressions X and Y are the same. */
23495 static inline bool
23496 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23498 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23499 if (x->dw_loc_opc != y->dw_loc_opc
23500 || x->dtprel != y->dtprel
23501 || !compare_loc_operands (x, y))
23502 break;
23503 return x == NULL && y == NULL;
23506 /* Hashtable helpers. */
23508 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23510 typedef dw_loc_list_struct value_type;
23511 typedef dw_loc_list_struct compare_type;
23512 static inline hashval_t hash (const value_type *);
23513 static inline bool equal (const value_type *, const compare_type *);
23516 /* Return precomputed hash of location list X. */
23518 inline hashval_t
23519 loc_list_hasher::hash (const value_type *x)
23521 return x->hash;
23524 /* Return true if location lists A and B are the same. */
23526 inline bool
23527 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23529 if (a == b)
23530 return 1;
23531 if (a->hash != b->hash)
23532 return 0;
23533 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23534 if (strcmp (a->begin, b->begin) != 0
23535 || strcmp (a->end, b->end) != 0
23536 || (a->section == NULL) != (b->section == NULL)
23537 || (a->section && strcmp (a->section, b->section) != 0)
23538 || !compare_locs (a->expr, b->expr))
23539 break;
23540 return a == NULL && b == NULL;
23543 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23546 /* Recursively optimize location lists referenced from DIE
23547 children and share them whenever possible. */
23549 static void
23550 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23552 dw_die_ref c;
23553 dw_attr_ref a;
23554 unsigned ix;
23555 dw_loc_list_struct **slot;
23557 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23558 if (AT_class (a) == dw_val_class_loc_list)
23560 dw_loc_list_ref list = AT_loc_list (a);
23561 /* TODO: perform some optimizations here, before hashing
23562 it and storing into the hash table. */
23563 hash_loc_list (list);
23564 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23565 if (*slot == NULL)
23566 *slot = list;
23567 else
23568 a->dw_attr_val.v.val_loc_list = *slot;
23571 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23575 /* Recursively assign each location list a unique index into the debug_addr
23576 section. */
23578 static void
23579 index_location_lists (dw_die_ref die)
23581 dw_die_ref c;
23582 dw_attr_ref a;
23583 unsigned ix;
23585 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23586 if (AT_class (a) == dw_val_class_loc_list)
23588 dw_loc_list_ref list = AT_loc_list (a);
23589 dw_loc_list_ref curr;
23590 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23592 /* Don't index an entry that has already been indexed
23593 or won't be output. */
23594 if (curr->begin_entry != NULL
23595 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23596 continue;
23598 curr->begin_entry
23599 = add_addr_table_entry (xstrdup (curr->begin),
23600 ate_kind_label);
23604 FOR_EACH_CHILD (die, c, index_location_lists (c));
23607 /* Optimize location lists referenced from DIE
23608 children and share them whenever possible. */
23610 static void
23611 optimize_location_lists (dw_die_ref die)
23613 loc_list_hash_type htab;
23614 htab.create (500);
23615 optimize_location_lists_1 (die, htab);
23616 htab.dispose ();
23619 /* Output stuff that dwarf requires at the end of every file,
23620 and generate the DWARF-2 debugging info. */
23622 static void
23623 dwarf2out_finish (const char *filename)
23625 limbo_die_node *node, *next_node;
23626 comdat_type_node *ctnode;
23627 hash_table <comdat_type_hasher> comdat_type_table;
23628 unsigned int i;
23629 dw_die_ref main_comp_unit_die;
23631 /* PCH might result in DW_AT_producer string being restored from the
23632 header compilation, so always fill it with empty string initially
23633 and overwrite only here. */
23634 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23635 producer_string = gen_producer_string ();
23636 producer->dw_attr_val.v.val_str->refcount--;
23637 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23639 gen_scheduled_generic_parms_dies ();
23640 gen_remaining_tmpl_value_param_die_attribute ();
23642 /* Add the name for the main input file now. We delayed this from
23643 dwarf2out_init to avoid complications with PCH. */
23644 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23645 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23646 add_comp_dir_attribute (comp_unit_die ());
23647 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23649 bool p = false;
23650 htab_traverse (file_table, file_table_relative_p, &p);
23651 if (p)
23652 add_comp_dir_attribute (comp_unit_die ());
23655 if (deferred_locations_list)
23656 for (i = 0; i < deferred_locations_list->length (); i++)
23658 add_location_or_const_value_attribute (
23659 (*deferred_locations_list)[i].die,
23660 (*deferred_locations_list)[i].variable,
23661 false,
23662 DW_AT_location);
23665 /* Traverse the limbo die list, and add parent/child links. The only
23666 dies without parents that should be here are concrete instances of
23667 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23668 For concrete instances, we can get the parent die from the abstract
23669 instance. */
23670 for (node = limbo_die_list; node; node = next_node)
23672 dw_die_ref die = node->die;
23673 next_node = node->next;
23675 if (die->die_parent == NULL)
23677 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23679 if (origin && origin->die_parent)
23680 add_child_die (origin->die_parent, die);
23681 else if (is_cu_die (die))
23683 else if (seen_error ())
23684 /* It's OK to be confused by errors in the input. */
23685 add_child_die (comp_unit_die (), die);
23686 else
23688 /* In certain situations, the lexical block containing a
23689 nested function can be optimized away, which results
23690 in the nested function die being orphaned. Likewise
23691 with the return type of that nested function. Force
23692 this to be a child of the containing function.
23694 It may happen that even the containing function got fully
23695 inlined and optimized out. In that case we are lost and
23696 assign the empty child. This should not be big issue as
23697 the function is likely unreachable too. */
23698 gcc_assert (node->created_for);
23700 if (DECL_P (node->created_for))
23701 origin = get_context_die (DECL_CONTEXT (node->created_for));
23702 else if (TYPE_P (node->created_for))
23703 origin = scope_die_for (node->created_for, comp_unit_die ());
23704 else
23705 origin = comp_unit_die ();
23707 add_child_die (origin, die);
23712 limbo_die_list = NULL;
23714 #if ENABLE_ASSERT_CHECKING
23716 dw_die_ref die = comp_unit_die (), c;
23717 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23719 #endif
23720 resolve_addr (comp_unit_die ());
23721 move_marked_base_types ();
23723 for (node = deferred_asm_name; node; node = node->next)
23725 tree decl = node->created_for;
23726 /* When generating LTO bytecode we can not generate new assembler
23727 names at this point and all important decls got theirs via
23728 free-lang-data. */
23729 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23730 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23732 add_linkage_attr (node->die, decl);
23733 move_linkage_attr (node->die);
23737 deferred_asm_name = NULL;
23739 /* Walk through the list of incomplete types again, trying once more to
23740 emit full debugging info for them. */
23741 retry_incomplete_types ();
23743 if (flag_eliminate_unused_debug_types)
23744 prune_unused_types ();
23746 /* Generate separate COMDAT sections for type DIEs. */
23747 if (use_debug_types)
23749 break_out_comdat_types (comp_unit_die ());
23751 /* Each new type_unit DIE was added to the limbo die list when created.
23752 Since these have all been added to comdat_type_list, clear the
23753 limbo die list. */
23754 limbo_die_list = NULL;
23756 /* For each new comdat type unit, copy declarations for incomplete
23757 types to make the new unit self-contained (i.e., no direct
23758 references to the main compile unit). */
23759 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23760 copy_decls_for_unworthy_types (ctnode->root_die);
23761 copy_decls_for_unworthy_types (comp_unit_die ());
23763 /* In the process of copying declarations from one unit to another,
23764 we may have left some declarations behind that are no longer
23765 referenced. Prune them. */
23766 prune_unused_types ();
23769 /* Generate separate CUs for each of the include files we've seen.
23770 They will go into limbo_die_list. */
23771 if (flag_eliminate_dwarf2_dups)
23772 break_out_includes (comp_unit_die ());
23774 /* Traverse the DIE's and add add sibling attributes to those DIE's
23775 that have children. */
23776 add_sibling_attributes (comp_unit_die ());
23777 for (node = limbo_die_list; node; node = node->next)
23778 add_sibling_attributes (node->die);
23779 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23780 add_sibling_attributes (ctnode->root_die);
23782 /* When splitting DWARF info, we put some attributes in the
23783 skeleton compile_unit DIE that remains in the .o, while
23784 most attributes go in the DWO compile_unit_die. */
23785 if (dwarf_split_debug_info)
23786 main_comp_unit_die = gen_compile_unit_die (NULL);
23787 else
23788 main_comp_unit_die = comp_unit_die ();
23790 /* Output a terminator label for the .text section. */
23791 switch_to_section (text_section);
23792 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23793 if (cold_text_section)
23795 switch_to_section (cold_text_section);
23796 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23799 /* We can only use the low/high_pc attributes if all of the code was
23800 in .text. */
23801 if (!have_multiple_function_sections
23802 || (dwarf_version < 3 && dwarf_strict))
23804 /* Don't add if the CU has no associated code. */
23805 if (text_section_used)
23806 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23807 text_end_label, true);
23809 else
23811 unsigned fde_idx;
23812 dw_fde_ref fde;
23813 bool range_list_added = false;
23815 if (text_section_used)
23816 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23817 text_end_label, &range_list_added, true);
23818 if (cold_text_section_used)
23819 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23820 cold_end_label, &range_list_added, true);
23822 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23824 if (DECL_IGNORED_P (fde->decl))
23825 continue;
23826 if (!fde->in_std_section)
23827 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23828 fde->dw_fde_end, &range_list_added,
23829 true);
23830 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23831 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23832 fde->dw_fde_second_end, &range_list_added,
23833 true);
23836 if (range_list_added)
23838 /* We need to give .debug_loc and .debug_ranges an appropriate
23839 "base address". Use zero so that these addresses become
23840 absolute. Historically, we've emitted the unexpected
23841 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23842 Emit both to give time for other tools to adapt. */
23843 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23844 if (! dwarf_strict && dwarf_version < 4)
23845 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23847 add_ranges (NULL);
23851 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23852 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23853 debug_line_section_label);
23855 if (have_macinfo)
23856 add_AT_macptr (comp_unit_die (),
23857 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23858 macinfo_section_label);
23860 if (dwarf_split_debug_info && addr_index_table != NULL)
23862 /* optimize_location_lists calculates the size of the lists,
23863 so index them first, and assign indices to the entries.
23864 Although optimize_location_lists will remove entries from
23865 the table, it only does so for duplicates, and therefore
23866 only reduces ref_counts to 1. */
23867 unsigned int index = 0;
23868 index_location_lists (comp_unit_die ());
23869 htab_traverse_noresize (addr_index_table,
23870 index_addr_table_entry, &index);
23872 if (have_location_lists)
23873 optimize_location_lists (comp_unit_die ());
23875 save_macinfo_strings ();
23877 if (dwarf_split_debug_info)
23879 unsigned int index = 0;
23881 /* Add attributes common to skeleton compile_units and
23882 type_units. Because these attributes include strings, it
23883 must be done before freezing the string table. Top-level
23884 skeleton die attrs are added when the skeleton type unit is
23885 created, so ensure it is created by this point. */
23886 add_top_level_skeleton_die_attrs (main_comp_unit_die);
23887 (void) get_skeleton_type_unit ();
23888 htab_traverse_noresize (debug_str_hash, index_string, &index);
23891 /* Output all of the compilation units. We put the main one last so that
23892 the offsets are available to output_pubnames. */
23893 for (node = limbo_die_list; node; node = node->next)
23894 output_comp_unit (node->die, 0);
23896 comdat_type_table.create (100);
23897 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23899 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
23901 /* Don't output duplicate types. */
23902 if (*slot != HTAB_EMPTY_ENTRY)
23903 continue;
23905 /* Add a pointer to the line table for the main compilation unit
23906 so that the debugger can make sense of DW_AT_decl_file
23907 attributes. */
23908 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23909 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23910 (!dwarf_split_debug_info
23911 ? debug_line_section_label
23912 : debug_skeleton_line_section_label));
23914 output_comdat_type_unit (ctnode);
23915 *slot = ctnode;
23917 comdat_type_table.dispose ();
23919 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23920 both the main_cu and all skeleton TUs. Making this call unconditional
23921 would end up either adding a second copy of the AT_pubnames attribute, or
23922 requiring a special case in add_top_level_skeleton_die_attrs. */
23923 if (!dwarf_split_debug_info)
23924 add_AT_pubnames (comp_unit_die ());
23926 if (dwarf_split_debug_info)
23928 int mark;
23929 unsigned char checksum[16];
23930 struct md5_ctx ctx;
23932 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23933 md5_init_ctx (&ctx);
23934 mark = 0;
23935 die_checksum (comp_unit_die (), &ctx, &mark);
23936 unmark_all_dies (comp_unit_die ());
23937 md5_finish_ctx (&ctx, checksum);
23939 /* Use the first 8 bytes of the checksum as the dwo_id,
23940 and add it to both comp-unit DIEs. */
23941 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23942 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23944 /* Add the base offset of the ranges table to the skeleton
23945 comp-unit DIE. */
23946 if (ranges_table_in_use)
23947 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23948 ranges_section_label);
23950 switch_to_section (debug_addr_section);
23951 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23952 output_addr_table ();
23955 /* Output the main compilation unit if non-empty or if .debug_macinfo
23956 or .debug_macro will be emitted. */
23957 output_comp_unit (comp_unit_die (), have_macinfo);
23959 if (dwarf_split_debug_info && info_section_emitted)
23960 output_skeleton_debug_sections (main_comp_unit_die);
23962 /* Output the abbreviation table. */
23963 if (abbrev_die_table_in_use != 1)
23965 switch_to_section (debug_abbrev_section);
23966 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23967 output_abbrev_section ();
23970 /* Output location list section if necessary. */
23971 if (have_location_lists)
23973 /* Output the location lists info. */
23974 switch_to_section (debug_loc_section);
23975 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23976 output_location_lists (comp_unit_die ());
23979 /* Output public names and types tables if necessary. */
23980 output_pubnames (pubname_table);
23981 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23982 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23983 simply won't look for the section. */
23984 output_pubnames (pubtype_table);
23986 /* Output the address range information if a CU (.debug_info section)
23987 was emitted. We output an empty table even if we had no functions
23988 to put in it. This because the consumer has no way to tell the
23989 difference between an empty table that we omitted and failure to
23990 generate a table that would have contained data. */
23991 if (info_section_emitted)
23993 unsigned long aranges_length = size_of_aranges ();
23995 switch_to_section (debug_aranges_section);
23996 output_aranges (aranges_length);
23999 /* Output ranges section if necessary. */
24000 if (ranges_table_in_use)
24002 switch_to_section (debug_ranges_section);
24003 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24004 output_ranges ();
24007 /* Have to end the macro section. */
24008 if (have_macinfo)
24010 switch_to_section (debug_macinfo_section);
24011 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24012 output_macinfo ();
24013 dw2_asm_output_data (1, 0, "End compilation unit");
24016 /* Output the source line correspondence table. We must do this
24017 even if there is no line information. Otherwise, on an empty
24018 translation unit, we will generate a present, but empty,
24019 .debug_info section. IRIX 6.5 `nm' will then complain when
24020 examining the file. This is done late so that any filenames
24021 used by the debug_info section are marked as 'used'. */
24022 switch_to_section (debug_line_section);
24023 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24024 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24025 output_line_info (false);
24027 if (dwarf_split_debug_info && info_section_emitted)
24029 switch_to_section (debug_skeleton_line_section);
24030 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24031 output_line_info (true);
24034 /* If we emitted any indirect strings, output the string table too. */
24035 if (debug_str_hash || skeleton_debug_str_hash)
24036 output_indirect_strings ();
24039 #include "gt-dwarf2out.h"