[AArch64] Rewrite vca<ge, gt, le, lt> Neon patterns in C.
[official-gcc.git] / gcc / dwarf2out.c
blobde69cc8d7a71089bf105e2158e7e5fb97775604f
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 nregs, size, i;
10616 unsigned reg;
10617 dw_loc_descr_ref loc_result = NULL;
10619 reg = REGNO (rtl);
10620 #ifdef LEAF_REG_REMAP
10621 if (crtl->uses_only_leaf_regs)
10623 int leaf_reg = LEAF_REG_REMAP (reg);
10624 if (leaf_reg != -1)
10625 reg = (unsigned) leaf_reg;
10627 #endif
10628 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10629 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10631 /* Simple, contiguous registers. */
10632 if (regs == NULL_RTX)
10634 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10636 loc_result = NULL;
10637 while (nregs--)
10639 dw_loc_descr_ref t;
10641 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10642 VAR_INIT_STATUS_INITIALIZED);
10643 add_loc_descr (&loc_result, t);
10644 add_loc_descr_op_piece (&loc_result, size);
10645 ++reg;
10647 return loc_result;
10650 /* Now onto stupid register sets in non contiguous locations. */
10652 gcc_assert (GET_CODE (regs) == PARALLEL);
10654 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10655 loc_result = NULL;
10657 for (i = 0; i < XVECLEN (regs, 0); ++i)
10659 dw_loc_descr_ref t;
10661 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10662 VAR_INIT_STATUS_INITIALIZED);
10663 add_loc_descr (&loc_result, t);
10664 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10665 add_loc_descr_op_piece (&loc_result, size);
10668 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10669 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10670 return loc_result;
10673 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10675 /* Return a location descriptor that designates a constant i,
10676 as a compound operation from constant (i >> shift), constant shift
10677 and DW_OP_shl. */
10679 static dw_loc_descr_ref
10680 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10682 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10683 add_loc_descr (&ret, int_loc_descriptor (shift));
10684 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10685 return ret;
10688 /* Return a location descriptor that designates a constant. */
10690 static dw_loc_descr_ref
10691 int_loc_descriptor (HOST_WIDE_INT i)
10693 enum dwarf_location_atom op;
10695 /* Pick the smallest representation of a constant, rather than just
10696 defaulting to the LEB encoding. */
10697 if (i >= 0)
10699 int clz = clz_hwi (i);
10700 int ctz = ctz_hwi (i);
10701 if (i <= 31)
10702 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10703 else if (i <= 0xff)
10704 op = DW_OP_const1u;
10705 else if (i <= 0xffff)
10706 op = DW_OP_const2u;
10707 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10708 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10709 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10710 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10711 while DW_OP_const4u is 5 bytes. */
10712 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10713 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10714 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10715 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10716 while DW_OP_const4u is 5 bytes. */
10717 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10718 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10719 op = DW_OP_const4u;
10720 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10721 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10722 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10723 while DW_OP_constu of constant >= 0x100000000 takes at least
10724 6 bytes. */
10725 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10726 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10727 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10728 >= HOST_BITS_PER_WIDE_INT)
10729 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10730 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10731 while DW_OP_constu takes in this case at least 6 bytes. */
10732 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10733 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10734 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10735 && size_of_uleb128 (i) > 6)
10736 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10737 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10738 else
10739 op = DW_OP_constu;
10741 else
10743 if (i >= -0x80)
10744 op = DW_OP_const1s;
10745 else if (i >= -0x8000)
10746 op = DW_OP_const2s;
10747 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10749 if (size_of_int_loc_descriptor (i) < 5)
10751 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10752 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10753 return ret;
10755 op = DW_OP_const4s;
10757 else
10759 if (size_of_int_loc_descriptor (i)
10760 < (unsigned long) 1 + size_of_sleb128 (i))
10762 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10763 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10764 return ret;
10766 op = DW_OP_consts;
10770 return new_loc_descr (op, i, 0);
10773 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10774 without actually allocating it. */
10776 static unsigned long
10777 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10779 return size_of_int_loc_descriptor (i >> shift)
10780 + size_of_int_loc_descriptor (shift)
10781 + 1;
10784 /* Return size_of_locs (int_loc_descriptor (i)) without
10785 actually allocating it. */
10787 static unsigned long
10788 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10790 unsigned long s;
10792 if (i >= 0)
10794 int clz, ctz;
10795 if (i <= 31)
10796 return 1;
10797 else if (i <= 0xff)
10798 return 2;
10799 else if (i <= 0xffff)
10800 return 3;
10801 clz = clz_hwi (i);
10802 ctz = ctz_hwi (i);
10803 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10804 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10805 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10806 - clz - 5);
10807 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10808 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10809 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10810 - clz - 8);
10811 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10812 return 5;
10813 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10814 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10815 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10816 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10817 - clz - 8);
10818 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10819 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10820 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10821 - clz - 16);
10822 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10823 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10824 && s > 6)
10825 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10826 - clz - 32);
10827 else
10828 return 1 + s;
10830 else
10832 if (i >= -0x80)
10833 return 2;
10834 else if (i >= -0x8000)
10835 return 3;
10836 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10838 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10840 s = size_of_int_loc_descriptor (-i) + 1;
10841 if (s < 5)
10842 return s;
10844 return 5;
10846 else
10848 unsigned long r = 1 + size_of_sleb128 (i);
10849 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10851 s = size_of_int_loc_descriptor (-i) + 1;
10852 if (s < r)
10853 return s;
10855 return r;
10860 /* Return loc description representing "address" of integer value.
10861 This can appear only as toplevel expression. */
10863 static dw_loc_descr_ref
10864 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10866 int litsize;
10867 dw_loc_descr_ref loc_result = NULL;
10869 if (!(dwarf_version >= 4 || !dwarf_strict))
10870 return NULL;
10872 litsize = size_of_int_loc_descriptor (i);
10873 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10874 is more compact. For DW_OP_stack_value we need:
10875 litsize + 1 (DW_OP_stack_value)
10876 and for DW_OP_implicit_value:
10877 1 (DW_OP_implicit_value) + 1 (length) + size. */
10878 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10880 loc_result = int_loc_descriptor (i);
10881 add_loc_descr (&loc_result,
10882 new_loc_descr (DW_OP_stack_value, 0, 0));
10883 return loc_result;
10886 loc_result = new_loc_descr (DW_OP_implicit_value,
10887 size, 0);
10888 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10889 loc_result->dw_loc_oprnd2.v.val_int = i;
10890 return loc_result;
10893 /* Return a location descriptor that designates a base+offset location. */
10895 static dw_loc_descr_ref
10896 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10897 enum var_init_status initialized)
10899 unsigned int regno;
10900 dw_loc_descr_ref result;
10901 dw_fde_ref fde = cfun->fde;
10903 /* We only use "frame base" when we're sure we're talking about the
10904 post-prologue local stack frame. We do this by *not* running
10905 register elimination until this point, and recognizing the special
10906 argument pointer and soft frame pointer rtx's. */
10907 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10909 rtx elim = (ira_use_lra_p
10910 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10911 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10913 if (elim != reg)
10915 if (GET_CODE (elim) == PLUS)
10917 offset += INTVAL (XEXP (elim, 1));
10918 elim = XEXP (elim, 0);
10920 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10921 && (elim == hard_frame_pointer_rtx
10922 || elim == stack_pointer_rtx))
10923 || elim == (frame_pointer_needed
10924 ? hard_frame_pointer_rtx
10925 : stack_pointer_rtx));
10927 /* If drap register is used to align stack, use frame
10928 pointer + offset to access stack variables. If stack
10929 is aligned without drap, use stack pointer + offset to
10930 access stack variables. */
10931 if (crtl->stack_realign_tried
10932 && reg == frame_pointer_rtx)
10934 int base_reg
10935 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10936 ? HARD_FRAME_POINTER_REGNUM
10937 : REGNO (elim));
10938 return new_reg_loc_descr (base_reg, offset);
10941 gcc_assert (frame_pointer_fb_offset_valid);
10942 offset += frame_pointer_fb_offset;
10943 return new_loc_descr (DW_OP_fbreg, offset, 0);
10947 regno = REGNO (reg);
10948 #ifdef LEAF_REG_REMAP
10949 if (crtl->uses_only_leaf_regs)
10951 int leaf_reg = LEAF_REG_REMAP (regno);
10952 if (leaf_reg != -1)
10953 regno = (unsigned) leaf_reg;
10955 #endif
10956 regno = DWARF_FRAME_REGNUM (regno);
10958 if (!optimize && fde
10959 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10961 /* Use cfa+offset to represent the location of arguments passed
10962 on the stack when drap is used to align stack.
10963 Only do this when not optimizing, for optimized code var-tracking
10964 is supposed to track where the arguments live and the register
10965 used as vdrap or drap in some spot might be used for something
10966 else in other part of the routine. */
10967 return new_loc_descr (DW_OP_fbreg, offset, 0);
10970 if (regno <= 31)
10971 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10972 offset, 0);
10973 else
10974 result = new_loc_descr (DW_OP_bregx, regno, offset);
10976 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10977 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10979 return result;
10982 /* Return true if this RTL expression describes a base+offset calculation. */
10984 static inline int
10985 is_based_loc (const_rtx rtl)
10987 return (GET_CODE (rtl) == PLUS
10988 && ((REG_P (XEXP (rtl, 0))
10989 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10990 && CONST_INT_P (XEXP (rtl, 1)))));
10993 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10994 failed. */
10996 static dw_loc_descr_ref
10997 tls_mem_loc_descriptor (rtx mem)
10999 tree base;
11000 dw_loc_descr_ref loc_result;
11002 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11003 return NULL;
11005 base = get_base_address (MEM_EXPR (mem));
11006 if (base == NULL
11007 || TREE_CODE (base) != VAR_DECL
11008 || !DECL_THREAD_LOCAL_P (base))
11009 return NULL;
11011 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11012 if (loc_result == NULL)
11013 return NULL;
11015 if (MEM_OFFSET (mem))
11016 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11018 return loc_result;
11021 /* Output debug info about reason why we failed to expand expression as dwarf
11022 expression. */
11024 static void
11025 expansion_failed (tree expr, rtx rtl, char const *reason)
11027 if (dump_file && (dump_flags & TDF_DETAILS))
11029 fprintf (dump_file, "Failed to expand as dwarf: ");
11030 if (expr)
11031 print_generic_expr (dump_file, expr, dump_flags);
11032 if (rtl)
11034 fprintf (dump_file, "\n");
11035 print_rtl (dump_file, rtl);
11037 fprintf (dump_file, "\nReason: %s\n", reason);
11041 /* Helper function for const_ok_for_output, called either directly
11042 or via for_each_rtx. */
11044 static int
11045 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11047 rtx rtl = *rtlp;
11049 if (GET_CODE (rtl) == UNSPEC)
11051 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11052 we can't express it in the debug info. */
11053 #ifdef ENABLE_CHECKING
11054 /* Don't complain about TLS UNSPECs, those are just too hard to
11055 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11056 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11057 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11058 if (XVECLEN (rtl, 0) == 0
11059 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11060 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11061 inform (current_function_decl
11062 ? DECL_SOURCE_LOCATION (current_function_decl)
11063 : UNKNOWN_LOCATION,
11064 #if NUM_UNSPEC_VALUES > 0
11065 "non-delegitimized UNSPEC %s (%d) found in variable location",
11066 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11067 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11068 XINT (rtl, 1));
11069 #else
11070 "non-delegitimized UNSPEC %d found in variable location",
11071 XINT (rtl, 1));
11072 #endif
11073 #endif
11074 expansion_failed (NULL_TREE, rtl,
11075 "UNSPEC hasn't been delegitimized.\n");
11076 return 1;
11079 if (targetm.const_not_ok_for_debug_p (rtl))
11081 expansion_failed (NULL_TREE, rtl,
11082 "Expression rejected for debug by the backend.\n");
11083 return 1;
11086 if (GET_CODE (rtl) != SYMBOL_REF)
11087 return 0;
11089 if (CONSTANT_POOL_ADDRESS_P (rtl))
11091 bool marked;
11092 get_pool_constant_mark (rtl, &marked);
11093 /* If all references to this pool constant were optimized away,
11094 it was not output and thus we can't represent it. */
11095 if (!marked)
11097 expansion_failed (NULL_TREE, rtl,
11098 "Constant was removed from constant pool.\n");
11099 return 1;
11103 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11104 return 1;
11106 /* Avoid references to external symbols in debug info, on several targets
11107 the linker might even refuse to link when linking a shared library,
11108 and in many other cases the relocations for .debug_info/.debug_loc are
11109 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11110 to be defined within the same shared library or executable are fine. */
11111 if (SYMBOL_REF_EXTERNAL_P (rtl))
11113 tree decl = SYMBOL_REF_DECL (rtl);
11115 if (decl == NULL || !targetm.binds_local_p (decl))
11117 expansion_failed (NULL_TREE, rtl,
11118 "Symbol not defined in current TU.\n");
11119 return 1;
11123 return 0;
11126 /* Return true if constant RTL can be emitted in DW_OP_addr or
11127 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11128 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11130 static bool
11131 const_ok_for_output (rtx rtl)
11133 if (GET_CODE (rtl) == SYMBOL_REF)
11134 return const_ok_for_output_1 (&rtl, NULL) == 0;
11136 if (GET_CODE (rtl) == CONST)
11137 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11139 return true;
11142 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11143 if possible, NULL otherwise. */
11145 static dw_die_ref
11146 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11148 dw_die_ref type_die;
11149 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11151 if (type == NULL)
11152 return NULL;
11153 switch (TREE_CODE (type))
11155 case INTEGER_TYPE:
11156 case REAL_TYPE:
11157 break;
11158 default:
11159 return NULL;
11161 type_die = lookup_type_die (type);
11162 if (!type_die)
11163 type_die = modified_type_die (type, false, false, comp_unit_die ());
11164 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11165 return NULL;
11166 return type_die;
11169 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11170 type matching MODE, or, if MODE is narrower than or as wide as
11171 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11172 possible. */
11174 static dw_loc_descr_ref
11175 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11177 enum machine_mode outer_mode = mode;
11178 dw_die_ref type_die;
11179 dw_loc_descr_ref cvt;
11181 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11183 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11184 return op;
11186 type_die = base_type_for_mode (outer_mode, 1);
11187 if (type_die == NULL)
11188 return NULL;
11189 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11190 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11191 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11192 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11193 add_loc_descr (&op, cvt);
11194 return op;
11197 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11199 static dw_loc_descr_ref
11200 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11201 dw_loc_descr_ref op1)
11203 dw_loc_descr_ref ret = op0;
11204 add_loc_descr (&ret, op1);
11205 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11206 if (STORE_FLAG_VALUE != 1)
11208 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11209 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11211 return ret;
11214 /* Return location descriptor for signed comparison OP RTL. */
11216 static dw_loc_descr_ref
11217 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11218 enum machine_mode mem_mode)
11220 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11221 dw_loc_descr_ref op0, op1;
11222 int shift;
11224 if (op_mode == VOIDmode)
11225 op_mode = GET_MODE (XEXP (rtl, 1));
11226 if (op_mode == VOIDmode)
11227 return NULL;
11229 if (dwarf_strict
11230 && (GET_MODE_CLASS (op_mode) != MODE_INT
11231 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11232 return NULL;
11234 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11235 VAR_INIT_STATUS_INITIALIZED);
11236 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11237 VAR_INIT_STATUS_INITIALIZED);
11239 if (op0 == NULL || op1 == NULL)
11240 return NULL;
11242 if (GET_MODE_CLASS (op_mode) != MODE_INT
11243 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11244 return compare_loc_descriptor (op, op0, op1);
11246 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11248 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11249 dw_loc_descr_ref cvt;
11251 if (type_die == NULL)
11252 return NULL;
11253 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11254 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11255 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11256 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11257 add_loc_descr (&op0, cvt);
11258 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11259 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11260 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11261 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11262 add_loc_descr (&op1, cvt);
11263 return compare_loc_descriptor (op, op0, op1);
11266 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11267 /* For eq/ne, if the operands are known to be zero-extended,
11268 there is no need to do the fancy shifting up. */
11269 if (op == DW_OP_eq || op == DW_OP_ne)
11271 dw_loc_descr_ref last0, last1;
11272 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11274 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11276 /* deref_size zero extends, and for constants we can check
11277 whether they are zero extended or not. */
11278 if (((last0->dw_loc_opc == DW_OP_deref_size
11279 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11280 || (CONST_INT_P (XEXP (rtl, 0))
11281 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11282 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11283 && ((last1->dw_loc_opc == DW_OP_deref_size
11284 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11285 || (CONST_INT_P (XEXP (rtl, 1))
11286 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11287 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11288 return compare_loc_descriptor (op, op0, op1);
11290 /* EQ/NE comparison against constant in narrower type than
11291 DWARF2_ADDR_SIZE can be performed either as
11292 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11293 DW_OP_{eq,ne}
11295 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11296 DW_OP_{eq,ne}. Pick whatever is shorter. */
11297 if (CONST_INT_P (XEXP (rtl, 1))
11298 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11299 && (size_of_int_loc_descriptor (shift) + 1
11300 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11301 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11302 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11303 & GET_MODE_MASK (op_mode))))
11305 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11306 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11307 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11308 & GET_MODE_MASK (op_mode));
11309 return compare_loc_descriptor (op, op0, op1);
11312 add_loc_descr (&op0, int_loc_descriptor (shift));
11313 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11314 if (CONST_INT_P (XEXP (rtl, 1)))
11315 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11316 else
11318 add_loc_descr (&op1, int_loc_descriptor (shift));
11319 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11321 return compare_loc_descriptor (op, op0, op1);
11324 /* Return location descriptor for unsigned comparison OP RTL. */
11326 static dw_loc_descr_ref
11327 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11328 enum machine_mode mem_mode)
11330 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11331 dw_loc_descr_ref op0, op1;
11333 if (op_mode == VOIDmode)
11334 op_mode = GET_MODE (XEXP (rtl, 1));
11335 if (op_mode == VOIDmode)
11336 return NULL;
11337 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11338 return NULL;
11340 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11341 return NULL;
11343 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11344 VAR_INIT_STATUS_INITIALIZED);
11345 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11346 VAR_INIT_STATUS_INITIALIZED);
11348 if (op0 == NULL || op1 == NULL)
11349 return NULL;
11351 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11353 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11354 dw_loc_descr_ref last0, last1;
11355 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11357 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11359 if (CONST_INT_P (XEXP (rtl, 0)))
11360 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11361 /* deref_size zero extends, so no need to mask it again. */
11362 else if (last0->dw_loc_opc != DW_OP_deref_size
11363 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11365 add_loc_descr (&op0, int_loc_descriptor (mask));
11366 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11368 if (CONST_INT_P (XEXP (rtl, 1)))
11369 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11370 /* deref_size zero extends, so no need to mask it again. */
11371 else if (last1->dw_loc_opc != DW_OP_deref_size
11372 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11374 add_loc_descr (&op1, int_loc_descriptor (mask));
11375 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11378 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11380 HOST_WIDE_INT bias = 1;
11381 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11382 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11383 if (CONST_INT_P (XEXP (rtl, 1)))
11384 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11385 + INTVAL (XEXP (rtl, 1)));
11386 else
11387 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11388 bias, 0));
11390 return compare_loc_descriptor (op, op0, op1);
11393 /* Return location descriptor for {U,S}{MIN,MAX}. */
11395 static dw_loc_descr_ref
11396 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11397 enum machine_mode mem_mode)
11399 enum dwarf_location_atom op;
11400 dw_loc_descr_ref op0, op1, ret;
11401 dw_loc_descr_ref bra_node, drop_node;
11403 if (dwarf_strict
11404 && (GET_MODE_CLASS (mode) != MODE_INT
11405 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11406 return NULL;
11408 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11409 VAR_INIT_STATUS_INITIALIZED);
11410 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11411 VAR_INIT_STATUS_INITIALIZED);
11413 if (op0 == NULL || op1 == NULL)
11414 return NULL;
11416 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11417 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11418 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11419 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11421 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11423 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11424 add_loc_descr (&op0, int_loc_descriptor (mask));
11425 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11426 add_loc_descr (&op1, int_loc_descriptor (mask));
11427 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11429 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11431 HOST_WIDE_INT bias = 1;
11432 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11433 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11434 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11437 else if (GET_MODE_CLASS (mode) == MODE_INT
11438 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11440 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11441 add_loc_descr (&op0, int_loc_descriptor (shift));
11442 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11443 add_loc_descr (&op1, int_loc_descriptor (shift));
11444 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11446 else if (GET_MODE_CLASS (mode) == MODE_INT
11447 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11449 dw_die_ref type_die = base_type_for_mode (mode, 0);
11450 dw_loc_descr_ref cvt;
11451 if (type_die == NULL)
11452 return NULL;
11453 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11454 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11455 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11456 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11457 add_loc_descr (&op0, cvt);
11458 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11459 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11460 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11461 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11462 add_loc_descr (&op1, cvt);
11465 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11466 op = DW_OP_lt;
11467 else
11468 op = DW_OP_gt;
11469 ret = op0;
11470 add_loc_descr (&ret, op1);
11471 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11472 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11473 add_loc_descr (&ret, bra_node);
11474 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11475 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11476 add_loc_descr (&ret, drop_node);
11477 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11478 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11479 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11480 && GET_MODE_CLASS (mode) == MODE_INT
11481 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11482 ret = convert_descriptor_to_mode (mode, ret);
11483 return ret;
11486 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11487 but after converting arguments to type_die, afterwards
11488 convert back to unsigned. */
11490 static dw_loc_descr_ref
11491 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11492 enum machine_mode mode, enum machine_mode mem_mode)
11494 dw_loc_descr_ref cvt, op0, op1;
11496 if (type_die == NULL)
11497 return NULL;
11498 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11499 VAR_INIT_STATUS_INITIALIZED);
11500 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11501 VAR_INIT_STATUS_INITIALIZED);
11502 if (op0 == NULL || op1 == NULL)
11503 return NULL;
11504 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11505 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11506 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11507 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11508 add_loc_descr (&op0, cvt);
11509 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11510 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11511 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11512 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11513 add_loc_descr (&op1, cvt);
11514 add_loc_descr (&op0, op1);
11515 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11516 return convert_descriptor_to_mode (mode, op0);
11519 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11520 const0 is DW_OP_lit0 or corresponding typed constant,
11521 const1 is DW_OP_lit1 or corresponding typed constant
11522 and constMSB is constant with just the MSB bit set
11523 for the mode):
11524 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11525 L1: const0 DW_OP_swap
11526 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11527 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11528 L3: DW_OP_drop
11529 L4: DW_OP_nop
11531 CTZ is similar:
11532 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11533 L1: const0 DW_OP_swap
11534 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11535 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11536 L3: DW_OP_drop
11537 L4: DW_OP_nop
11539 FFS is similar:
11540 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11541 L1: const1 DW_OP_swap
11542 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11543 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11544 L3: DW_OP_drop
11545 L4: DW_OP_nop */
11547 static dw_loc_descr_ref
11548 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11549 enum machine_mode mem_mode)
11551 dw_loc_descr_ref op0, ret, tmp;
11552 HOST_WIDE_INT valv;
11553 dw_loc_descr_ref l1jump, l1label;
11554 dw_loc_descr_ref l2jump, l2label;
11555 dw_loc_descr_ref l3jump, l3label;
11556 dw_loc_descr_ref l4jump, l4label;
11557 rtx msb;
11559 if (GET_MODE_CLASS (mode) != MODE_INT
11560 || GET_MODE (XEXP (rtl, 0)) != mode
11561 || (GET_CODE (rtl) == CLZ
11562 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11563 return NULL;
11565 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11566 VAR_INIT_STATUS_INITIALIZED);
11567 if (op0 == NULL)
11568 return NULL;
11569 ret = op0;
11570 if (GET_CODE (rtl) == CLZ)
11572 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11573 valv = GET_MODE_BITSIZE (mode);
11575 else if (GET_CODE (rtl) == FFS)
11576 valv = 0;
11577 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11578 valv = GET_MODE_BITSIZE (mode);
11579 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11580 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11581 add_loc_descr (&ret, l1jump);
11582 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11583 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11584 VAR_INIT_STATUS_INITIALIZED);
11585 if (tmp == NULL)
11586 return NULL;
11587 add_loc_descr (&ret, tmp);
11588 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11589 add_loc_descr (&ret, l4jump);
11590 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11591 ? const1_rtx : const0_rtx,
11592 mode, mem_mode,
11593 VAR_INIT_STATUS_INITIALIZED);
11594 if (l1label == NULL)
11595 return NULL;
11596 add_loc_descr (&ret, l1label);
11597 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11598 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11599 add_loc_descr (&ret, l2label);
11600 if (GET_CODE (rtl) != CLZ)
11601 msb = const1_rtx;
11602 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11603 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11604 << (GET_MODE_BITSIZE (mode) - 1));
11605 else
11606 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11607 << (GET_MODE_BITSIZE (mode)
11608 - HOST_BITS_PER_WIDE_INT - 1), mode);
11609 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11610 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11611 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11612 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11613 else
11614 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11615 VAR_INIT_STATUS_INITIALIZED);
11616 if (tmp == NULL)
11617 return NULL;
11618 add_loc_descr (&ret, tmp);
11619 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11620 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11621 add_loc_descr (&ret, l3jump);
11622 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11623 VAR_INIT_STATUS_INITIALIZED);
11624 if (tmp == NULL)
11625 return NULL;
11626 add_loc_descr (&ret, tmp);
11627 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11628 ? DW_OP_shl : DW_OP_shr, 0, 0));
11629 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11630 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11631 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11632 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11633 add_loc_descr (&ret, l2jump);
11634 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11635 add_loc_descr (&ret, l3label);
11636 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11637 add_loc_descr (&ret, l4label);
11638 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11639 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11640 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11641 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11642 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11643 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11644 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11645 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11646 return ret;
11649 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11650 const1 is DW_OP_lit1 or corresponding typed constant):
11651 const0 DW_OP_swap
11652 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11653 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11654 L2: DW_OP_drop
11656 PARITY is similar:
11657 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11658 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11659 L2: DW_OP_drop */
11661 static dw_loc_descr_ref
11662 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11663 enum machine_mode mem_mode)
11665 dw_loc_descr_ref op0, ret, tmp;
11666 dw_loc_descr_ref l1jump, l1label;
11667 dw_loc_descr_ref l2jump, l2label;
11669 if (GET_MODE_CLASS (mode) != MODE_INT
11670 || GET_MODE (XEXP (rtl, 0)) != mode)
11671 return NULL;
11673 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11674 VAR_INIT_STATUS_INITIALIZED);
11675 if (op0 == NULL)
11676 return NULL;
11677 ret = op0;
11678 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11679 VAR_INIT_STATUS_INITIALIZED);
11680 if (tmp == NULL)
11681 return NULL;
11682 add_loc_descr (&ret, tmp);
11683 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11684 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11685 add_loc_descr (&ret, l1label);
11686 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11687 add_loc_descr (&ret, l2jump);
11688 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11689 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11690 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11691 VAR_INIT_STATUS_INITIALIZED);
11692 if (tmp == NULL)
11693 return NULL;
11694 add_loc_descr (&ret, tmp);
11695 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11696 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11697 ? DW_OP_plus : DW_OP_xor, 0, 0));
11698 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11699 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11700 VAR_INIT_STATUS_INITIALIZED);
11701 add_loc_descr (&ret, tmp);
11702 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11703 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11704 add_loc_descr (&ret, l1jump);
11705 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11706 add_loc_descr (&ret, l2label);
11707 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11708 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11709 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11710 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11711 return ret;
11714 /* BSWAP (constS is initial shift count, either 56 or 24):
11715 constS const0
11716 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11717 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11718 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11719 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11720 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11722 static dw_loc_descr_ref
11723 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11724 enum machine_mode mem_mode)
11726 dw_loc_descr_ref op0, ret, tmp;
11727 dw_loc_descr_ref l1jump, l1label;
11728 dw_loc_descr_ref l2jump, l2label;
11730 if (GET_MODE_CLASS (mode) != MODE_INT
11731 || BITS_PER_UNIT != 8
11732 || (GET_MODE_BITSIZE (mode) != 32
11733 && GET_MODE_BITSIZE (mode) != 64))
11734 return NULL;
11736 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11737 VAR_INIT_STATUS_INITIALIZED);
11738 if (op0 == NULL)
11739 return NULL;
11741 ret = op0;
11742 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11743 mode, mem_mode,
11744 VAR_INIT_STATUS_INITIALIZED);
11745 if (tmp == NULL)
11746 return NULL;
11747 add_loc_descr (&ret, tmp);
11748 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11749 VAR_INIT_STATUS_INITIALIZED);
11750 if (tmp == NULL)
11751 return NULL;
11752 add_loc_descr (&ret, tmp);
11753 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11754 add_loc_descr (&ret, l1label);
11755 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11756 mode, mem_mode,
11757 VAR_INIT_STATUS_INITIALIZED);
11758 add_loc_descr (&ret, tmp);
11759 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11760 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11761 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11762 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11763 VAR_INIT_STATUS_INITIALIZED);
11764 if (tmp == NULL)
11765 return NULL;
11766 add_loc_descr (&ret, tmp);
11767 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11768 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11769 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11770 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11771 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11772 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11773 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11774 VAR_INIT_STATUS_INITIALIZED);
11775 add_loc_descr (&ret, tmp);
11776 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11777 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11778 add_loc_descr (&ret, l2jump);
11779 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11780 VAR_INIT_STATUS_INITIALIZED);
11781 add_loc_descr (&ret, tmp);
11782 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11783 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11784 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11785 add_loc_descr (&ret, l1jump);
11786 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11787 add_loc_descr (&ret, l2label);
11788 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11789 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11790 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11791 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11792 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11793 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11794 return ret;
11797 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11798 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11799 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11800 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11802 ROTATERT is similar:
11803 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11804 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11805 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11807 static dw_loc_descr_ref
11808 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11809 enum machine_mode mem_mode)
11811 rtx rtlop1 = XEXP (rtl, 1);
11812 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11813 int i;
11815 if (GET_MODE_CLASS (mode) != MODE_INT)
11816 return NULL;
11818 if (GET_MODE (rtlop1) != VOIDmode
11819 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11820 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11821 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11822 VAR_INIT_STATUS_INITIALIZED);
11823 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11824 VAR_INIT_STATUS_INITIALIZED);
11825 if (op0 == NULL || op1 == NULL)
11826 return NULL;
11827 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11828 for (i = 0; i < 2; i++)
11830 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11831 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11832 mode, mem_mode,
11833 VAR_INIT_STATUS_INITIALIZED);
11834 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11835 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11836 ? DW_OP_const4u
11837 : HOST_BITS_PER_WIDE_INT == 64
11838 ? DW_OP_const8u : DW_OP_constu,
11839 GET_MODE_MASK (mode), 0);
11840 else
11841 mask[i] = NULL;
11842 if (mask[i] == NULL)
11843 return NULL;
11844 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11846 ret = op0;
11847 add_loc_descr (&ret, op1);
11848 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11849 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11850 if (GET_CODE (rtl) == ROTATERT)
11852 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11853 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11854 GET_MODE_BITSIZE (mode), 0));
11856 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11857 if (mask[0] != NULL)
11858 add_loc_descr (&ret, mask[0]);
11859 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11860 if (mask[1] != NULL)
11862 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11863 add_loc_descr (&ret, mask[1]);
11864 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11866 if (GET_CODE (rtl) == ROTATE)
11868 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11869 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11870 GET_MODE_BITSIZE (mode), 0));
11872 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11873 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11874 return ret;
11877 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11878 for DEBUG_PARAMETER_REF RTL. */
11880 static dw_loc_descr_ref
11881 parameter_ref_descriptor (rtx rtl)
11883 dw_loc_descr_ref ret;
11884 dw_die_ref ref;
11886 if (dwarf_strict)
11887 return NULL;
11888 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11889 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11890 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11891 if (ref)
11893 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11894 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11895 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11897 else
11899 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11900 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11902 return ret;
11905 /* The following routine converts the RTL for a variable or parameter
11906 (resident in memory) into an equivalent Dwarf representation of a
11907 mechanism for getting the address of that same variable onto the top of a
11908 hypothetical "address evaluation" stack.
11910 When creating memory location descriptors, we are effectively transforming
11911 the RTL for a memory-resident object into its Dwarf postfix expression
11912 equivalent. This routine recursively descends an RTL tree, turning
11913 it into Dwarf postfix code as it goes.
11915 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11917 MEM_MODE is the mode of the memory reference, needed to handle some
11918 autoincrement addressing modes.
11920 Return 0 if we can't represent the location. */
11922 dw_loc_descr_ref
11923 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11924 enum machine_mode mem_mode,
11925 enum var_init_status initialized)
11927 dw_loc_descr_ref mem_loc_result = NULL;
11928 enum dwarf_location_atom op;
11929 dw_loc_descr_ref op0, op1;
11930 rtx inner = NULL_RTX;
11932 if (mode == VOIDmode)
11933 mode = GET_MODE (rtl);
11935 /* Note that for a dynamically sized array, the location we will generate a
11936 description of here will be the lowest numbered location which is
11937 actually within the array. That's *not* necessarily the same as the
11938 zeroth element of the array. */
11940 rtl = targetm.delegitimize_address (rtl);
11942 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11943 return NULL;
11945 switch (GET_CODE (rtl))
11947 case POST_INC:
11948 case POST_DEC:
11949 case POST_MODIFY:
11950 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11952 case SUBREG:
11953 /* The case of a subreg may arise when we have a local (register)
11954 variable or a formal (register) parameter which doesn't quite fill
11955 up an entire register. For now, just assume that it is
11956 legitimate to make the Dwarf info refer to the whole register which
11957 contains the given subreg. */
11958 if (!subreg_lowpart_p (rtl))
11959 break;
11960 inner = SUBREG_REG (rtl);
11961 case TRUNCATE:
11962 if (inner == NULL_RTX)
11963 inner = XEXP (rtl, 0);
11964 if (GET_MODE_CLASS (mode) == MODE_INT
11965 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11966 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11967 #ifdef POINTERS_EXTEND_UNSIGNED
11968 || (mode == Pmode && mem_mode != VOIDmode)
11969 #endif
11971 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
11973 mem_loc_result = mem_loc_descriptor (inner,
11974 GET_MODE (inner),
11975 mem_mode, initialized);
11976 break;
11978 if (dwarf_strict)
11979 break;
11980 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
11981 break;
11982 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
11983 && (GET_MODE_CLASS (mode) != MODE_INT
11984 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
11985 break;
11986 else
11988 dw_die_ref type_die;
11989 dw_loc_descr_ref cvt;
11991 mem_loc_result = mem_loc_descriptor (inner,
11992 GET_MODE (inner),
11993 mem_mode, initialized);
11994 if (mem_loc_result == NULL)
11995 break;
11996 type_die = base_type_for_mode (mode,
11997 GET_MODE_CLASS (mode) == MODE_INT);
11998 if (type_die == NULL)
12000 mem_loc_result = NULL;
12001 break;
12003 if (GET_MODE_SIZE (mode)
12004 != GET_MODE_SIZE (GET_MODE (inner)))
12005 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12006 else
12007 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12008 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12009 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12010 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12011 add_loc_descr (&mem_loc_result, cvt);
12013 break;
12015 case REG:
12016 if (GET_MODE_CLASS (mode) != MODE_INT
12017 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12018 && rtl != arg_pointer_rtx
12019 && rtl != frame_pointer_rtx
12020 #ifdef POINTERS_EXTEND_UNSIGNED
12021 && (mode != Pmode || mem_mode == VOIDmode)
12022 #endif
12025 dw_die_ref type_die;
12026 unsigned int dbx_regnum;
12028 if (dwarf_strict)
12029 break;
12030 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12031 break;
12032 type_die = base_type_for_mode (mode,
12033 GET_MODE_CLASS (mode) == MODE_INT);
12034 if (type_die == NULL)
12035 break;
12037 dbx_regnum = dbx_reg_number (rtl);
12038 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12039 break;
12040 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12041 dbx_regnum, 0);
12042 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12043 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12044 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12045 break;
12047 /* Whenever a register number forms a part of the description of the
12048 method for calculating the (dynamic) address of a memory resident
12049 object, DWARF rules require the register number be referred to as
12050 a "base register". This distinction is not based in any way upon
12051 what category of register the hardware believes the given register
12052 belongs to. This is strictly DWARF terminology we're dealing with
12053 here. Note that in cases where the location of a memory-resident
12054 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12055 OP_CONST (0)) the actual DWARF location descriptor that we generate
12056 may just be OP_BASEREG (basereg). This may look deceptively like
12057 the object in question was allocated to a register (rather than in
12058 memory) so DWARF consumers need to be aware of the subtle
12059 distinction between OP_REG and OP_BASEREG. */
12060 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12061 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12062 else if (stack_realign_drap
12063 && crtl->drap_reg
12064 && crtl->args.internal_arg_pointer == rtl
12065 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12067 /* If RTL is internal_arg_pointer, which has been optimized
12068 out, use DRAP instead. */
12069 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12070 VAR_INIT_STATUS_INITIALIZED);
12072 break;
12074 case SIGN_EXTEND:
12075 case ZERO_EXTEND:
12076 if (GET_MODE_CLASS (mode) != MODE_INT)
12077 break;
12078 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12079 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12080 if (op0 == 0)
12081 break;
12082 else if (GET_CODE (rtl) == ZERO_EXTEND
12083 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12084 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12085 < HOST_BITS_PER_WIDE_INT
12086 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12087 to expand zero extend as two shifts instead of
12088 masking. */
12089 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12091 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12092 mem_loc_result = op0;
12093 add_loc_descr (&mem_loc_result,
12094 int_loc_descriptor (GET_MODE_MASK (imode)));
12095 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12097 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12099 int shift = DWARF2_ADDR_SIZE
12100 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12101 shift *= BITS_PER_UNIT;
12102 if (GET_CODE (rtl) == SIGN_EXTEND)
12103 op = DW_OP_shra;
12104 else
12105 op = DW_OP_shr;
12106 mem_loc_result = op0;
12107 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12108 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12109 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12110 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12112 else if (!dwarf_strict)
12114 dw_die_ref type_die1, type_die2;
12115 dw_loc_descr_ref cvt;
12117 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12118 GET_CODE (rtl) == ZERO_EXTEND);
12119 if (type_die1 == NULL)
12120 break;
12121 type_die2 = base_type_for_mode (mode, 1);
12122 if (type_die2 == NULL)
12123 break;
12124 mem_loc_result = op0;
12125 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12126 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12127 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12128 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12129 add_loc_descr (&mem_loc_result, cvt);
12130 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12131 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12132 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12133 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12134 add_loc_descr (&mem_loc_result, cvt);
12136 break;
12138 case MEM:
12140 rtx new_rtl = avoid_constant_pool_reference (rtl);
12141 if (new_rtl != rtl)
12143 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12144 initialized);
12145 if (mem_loc_result != NULL)
12146 return mem_loc_result;
12149 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12150 get_address_mode (rtl), mode,
12151 VAR_INIT_STATUS_INITIALIZED);
12152 if (mem_loc_result == NULL)
12153 mem_loc_result = tls_mem_loc_descriptor (rtl);
12154 if (mem_loc_result != NULL)
12156 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12157 || GET_MODE_CLASS (mode) != MODE_INT)
12159 dw_die_ref type_die;
12160 dw_loc_descr_ref deref;
12162 if (dwarf_strict)
12163 return NULL;
12164 type_die
12165 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12166 if (type_die == NULL)
12167 return NULL;
12168 deref = new_loc_descr (DW_OP_GNU_deref_type,
12169 GET_MODE_SIZE (mode), 0);
12170 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12171 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12172 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12173 add_loc_descr (&mem_loc_result, deref);
12175 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12176 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12177 else
12178 add_loc_descr (&mem_loc_result,
12179 new_loc_descr (DW_OP_deref_size,
12180 GET_MODE_SIZE (mode), 0));
12182 break;
12184 case LO_SUM:
12185 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12187 case LABEL_REF:
12188 /* Some ports can transform a symbol ref into a label ref, because
12189 the symbol ref is too far away and has to be dumped into a constant
12190 pool. */
12191 case CONST:
12192 case SYMBOL_REF:
12193 if (GET_MODE_CLASS (mode) != MODE_INT
12194 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12195 #ifdef POINTERS_EXTEND_UNSIGNED
12196 && (mode != Pmode || mem_mode == VOIDmode)
12197 #endif
12199 break;
12200 if (GET_CODE (rtl) == SYMBOL_REF
12201 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12203 dw_loc_descr_ref temp;
12205 /* If this is not defined, we have no way to emit the data. */
12206 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12207 break;
12209 temp = new_addr_loc_descr (rtl, dtprel_true);
12211 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12212 add_loc_descr (&mem_loc_result, temp);
12214 break;
12217 if (!const_ok_for_output (rtl))
12218 break;
12220 symref:
12221 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12222 vec_safe_push (used_rtx_array, rtl);
12223 break;
12225 case CONCAT:
12226 case CONCATN:
12227 case VAR_LOCATION:
12228 case DEBUG_IMPLICIT_PTR:
12229 expansion_failed (NULL_TREE, rtl,
12230 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12231 return 0;
12233 case ENTRY_VALUE:
12234 if (dwarf_strict)
12235 return NULL;
12236 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12238 if (GET_MODE_CLASS (mode) != MODE_INT
12239 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12240 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12241 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12242 else
12244 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12245 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12246 return NULL;
12247 op0 = one_reg_loc_descriptor (dbx_regnum,
12248 VAR_INIT_STATUS_INITIALIZED);
12251 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12252 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12254 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12255 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12256 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12257 return NULL;
12259 else
12260 gcc_unreachable ();
12261 if (op0 == NULL)
12262 return NULL;
12263 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12264 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12265 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12266 break;
12268 case DEBUG_PARAMETER_REF:
12269 mem_loc_result = parameter_ref_descriptor (rtl);
12270 break;
12272 case PRE_MODIFY:
12273 /* Extract the PLUS expression nested inside and fall into
12274 PLUS code below. */
12275 rtl = XEXP (rtl, 1);
12276 goto plus;
12278 case PRE_INC:
12279 case PRE_DEC:
12280 /* Turn these into a PLUS expression and fall into the PLUS code
12281 below. */
12282 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12283 GEN_INT (GET_CODE (rtl) == PRE_INC
12284 ? GET_MODE_UNIT_SIZE (mem_mode)
12285 : -GET_MODE_UNIT_SIZE (mem_mode)));
12287 /* ... fall through ... */
12289 case PLUS:
12290 plus:
12291 if (is_based_loc (rtl)
12292 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12293 || XEXP (rtl, 0) == arg_pointer_rtx
12294 || XEXP (rtl, 0) == frame_pointer_rtx)
12295 && GET_MODE_CLASS (mode) == MODE_INT)
12296 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12297 INTVAL (XEXP (rtl, 1)),
12298 VAR_INIT_STATUS_INITIALIZED);
12299 else
12301 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12302 VAR_INIT_STATUS_INITIALIZED);
12303 if (mem_loc_result == 0)
12304 break;
12306 if (CONST_INT_P (XEXP (rtl, 1))
12307 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12308 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12309 else
12311 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12312 VAR_INIT_STATUS_INITIALIZED);
12313 if (op1 == 0)
12314 break;
12315 add_loc_descr (&mem_loc_result, op1);
12316 add_loc_descr (&mem_loc_result,
12317 new_loc_descr (DW_OP_plus, 0, 0));
12320 break;
12322 /* If a pseudo-reg is optimized away, it is possible for it to
12323 be replaced with a MEM containing a multiply or shift. */
12324 case MINUS:
12325 op = DW_OP_minus;
12326 goto do_binop;
12328 case MULT:
12329 op = DW_OP_mul;
12330 goto do_binop;
12332 case DIV:
12333 if (!dwarf_strict
12334 && GET_MODE_CLASS (mode) == MODE_INT
12335 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12337 mem_loc_result = typed_binop (DW_OP_div, rtl,
12338 base_type_for_mode (mode, 0),
12339 mode, mem_mode);
12340 break;
12342 op = DW_OP_div;
12343 goto do_binop;
12345 case UMOD:
12346 op = DW_OP_mod;
12347 goto do_binop;
12349 case ASHIFT:
12350 op = DW_OP_shl;
12351 goto do_shift;
12353 case ASHIFTRT:
12354 op = DW_OP_shra;
12355 goto do_shift;
12357 case LSHIFTRT:
12358 op = DW_OP_shr;
12359 goto do_shift;
12361 do_shift:
12362 if (GET_MODE_CLASS (mode) != MODE_INT)
12363 break;
12364 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12365 VAR_INIT_STATUS_INITIALIZED);
12367 rtx rtlop1 = XEXP (rtl, 1);
12368 if (GET_MODE (rtlop1) != VOIDmode
12369 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12370 < GET_MODE_BITSIZE (mode))
12371 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12372 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12373 VAR_INIT_STATUS_INITIALIZED);
12376 if (op0 == 0 || op1 == 0)
12377 break;
12379 mem_loc_result = op0;
12380 add_loc_descr (&mem_loc_result, op1);
12381 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12382 break;
12384 case AND:
12385 op = DW_OP_and;
12386 goto do_binop;
12388 case IOR:
12389 op = DW_OP_or;
12390 goto do_binop;
12392 case XOR:
12393 op = DW_OP_xor;
12394 goto do_binop;
12396 do_binop:
12397 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12398 VAR_INIT_STATUS_INITIALIZED);
12399 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12400 VAR_INIT_STATUS_INITIALIZED);
12402 if (op0 == 0 || op1 == 0)
12403 break;
12405 mem_loc_result = op0;
12406 add_loc_descr (&mem_loc_result, op1);
12407 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12408 break;
12410 case MOD:
12411 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12413 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12414 base_type_for_mode (mode, 0),
12415 mode, mem_mode);
12416 break;
12419 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12420 VAR_INIT_STATUS_INITIALIZED);
12421 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12422 VAR_INIT_STATUS_INITIALIZED);
12424 if (op0 == 0 || op1 == 0)
12425 break;
12427 mem_loc_result = op0;
12428 add_loc_descr (&mem_loc_result, op1);
12429 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
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_div, 0, 0));
12432 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12433 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12434 break;
12436 case UDIV:
12437 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12439 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12441 op = DW_OP_div;
12442 goto do_binop;
12444 mem_loc_result = typed_binop (DW_OP_div, rtl,
12445 base_type_for_mode (mode, 1),
12446 mode, mem_mode);
12448 break;
12450 case NOT:
12451 op = DW_OP_not;
12452 goto do_unop;
12454 case ABS:
12455 op = DW_OP_abs;
12456 goto do_unop;
12458 case NEG:
12459 op = DW_OP_neg;
12460 goto do_unop;
12462 do_unop:
12463 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12464 VAR_INIT_STATUS_INITIALIZED);
12466 if (op0 == 0)
12467 break;
12469 mem_loc_result = op0;
12470 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12471 break;
12473 case CONST_INT:
12474 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12475 #ifdef POINTERS_EXTEND_UNSIGNED
12476 || (mode == Pmode
12477 && mem_mode != VOIDmode
12478 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12479 #endif
12482 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12483 break;
12485 if (!dwarf_strict
12486 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12487 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12489 dw_die_ref type_die = base_type_for_mode (mode, 1);
12490 enum machine_mode amode;
12491 if (type_die == NULL)
12492 return NULL;
12493 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12494 MODE_INT, 0);
12495 if (INTVAL (rtl) >= 0
12496 && amode != BLKmode
12497 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12498 /* const DW_OP_GNU_convert <XXX> vs.
12499 DW_OP_GNU_const_type <XXX, 1, const>. */
12500 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12501 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12503 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12504 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12505 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12506 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12507 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12508 add_loc_descr (&mem_loc_result, op0);
12509 return mem_loc_result;
12511 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12512 INTVAL (rtl));
12513 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12514 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12515 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12516 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12517 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12518 else
12520 mem_loc_result->dw_loc_oprnd2.val_class
12521 = dw_val_class_const_double;
12522 mem_loc_result->dw_loc_oprnd2.v.val_double
12523 = double_int::from_shwi (INTVAL (rtl));
12526 break;
12528 case CONST_DOUBLE:
12529 if (!dwarf_strict)
12531 dw_die_ref type_die;
12533 /* Note that a CONST_DOUBLE rtx could represent either an integer
12534 or a floating-point constant. A CONST_DOUBLE is used whenever
12535 the constant requires more than one word in order to be
12536 adequately represented. We output CONST_DOUBLEs as blocks. */
12537 if (mode == VOIDmode
12538 || (GET_MODE (rtl) == VOIDmode
12539 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12540 break;
12541 type_die = base_type_for_mode (mode,
12542 GET_MODE_CLASS (mode) == MODE_INT);
12543 if (type_die == NULL)
12544 return NULL;
12545 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12546 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12547 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12548 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12549 if (SCALAR_FLOAT_MODE_P (mode))
12551 unsigned int length = GET_MODE_SIZE (mode);
12552 unsigned char *array
12553 = (unsigned char*) ggc_alloc_atomic (length);
12555 insert_float (rtl, array);
12556 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12557 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12558 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12559 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12561 else
12563 mem_loc_result->dw_loc_oprnd2.val_class
12564 = dw_val_class_const_double;
12565 mem_loc_result->dw_loc_oprnd2.v.val_double
12566 = rtx_to_double_int (rtl);
12569 break;
12571 case EQ:
12572 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12573 break;
12575 case GE:
12576 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12577 break;
12579 case GT:
12580 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12581 break;
12583 case LE:
12584 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12585 break;
12587 case LT:
12588 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12589 break;
12591 case NE:
12592 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12593 break;
12595 case GEU:
12596 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12597 break;
12599 case GTU:
12600 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12601 break;
12603 case LEU:
12604 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12605 break;
12607 case LTU:
12608 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12609 break;
12611 case UMIN:
12612 case UMAX:
12613 if (GET_MODE_CLASS (mode) != MODE_INT)
12614 break;
12615 /* FALLTHRU */
12616 case SMIN:
12617 case SMAX:
12618 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12619 break;
12621 case ZERO_EXTRACT:
12622 case SIGN_EXTRACT:
12623 if (CONST_INT_P (XEXP (rtl, 1))
12624 && CONST_INT_P (XEXP (rtl, 2))
12625 && ((unsigned) INTVAL (XEXP (rtl, 1))
12626 + (unsigned) INTVAL (XEXP (rtl, 2))
12627 <= GET_MODE_BITSIZE (mode))
12628 && GET_MODE_CLASS (mode) == MODE_INT
12629 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12630 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12632 int shift, size;
12633 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12634 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12635 if (op0 == 0)
12636 break;
12637 if (GET_CODE (rtl) == SIGN_EXTRACT)
12638 op = DW_OP_shra;
12639 else
12640 op = DW_OP_shr;
12641 mem_loc_result = op0;
12642 size = INTVAL (XEXP (rtl, 1));
12643 shift = INTVAL (XEXP (rtl, 2));
12644 if (BITS_BIG_ENDIAN)
12645 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12646 - shift - size;
12647 if (shift + size != (int) DWARF2_ADDR_SIZE)
12649 add_loc_descr (&mem_loc_result,
12650 int_loc_descriptor (DWARF2_ADDR_SIZE
12651 - shift - size));
12652 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12654 if (size != (int) DWARF2_ADDR_SIZE)
12656 add_loc_descr (&mem_loc_result,
12657 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12658 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12661 break;
12663 case IF_THEN_ELSE:
12665 dw_loc_descr_ref op2, bra_node, drop_node;
12666 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12667 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12668 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12669 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12670 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12671 VAR_INIT_STATUS_INITIALIZED);
12672 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12673 VAR_INIT_STATUS_INITIALIZED);
12674 if (op0 == NULL || op1 == NULL || op2 == NULL)
12675 break;
12677 mem_loc_result = op1;
12678 add_loc_descr (&mem_loc_result, op2);
12679 add_loc_descr (&mem_loc_result, op0);
12680 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12681 add_loc_descr (&mem_loc_result, bra_node);
12682 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12683 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12684 add_loc_descr (&mem_loc_result, drop_node);
12685 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12686 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12688 break;
12690 case FLOAT_EXTEND:
12691 case FLOAT_TRUNCATE:
12692 case FLOAT:
12693 case UNSIGNED_FLOAT:
12694 case FIX:
12695 case UNSIGNED_FIX:
12696 if (!dwarf_strict)
12698 dw_die_ref type_die;
12699 dw_loc_descr_ref cvt;
12701 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12702 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12703 if (op0 == NULL)
12704 break;
12705 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12706 && (GET_CODE (rtl) == FLOAT
12707 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12708 <= DWARF2_ADDR_SIZE))
12710 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12711 GET_CODE (rtl) == UNSIGNED_FLOAT);
12712 if (type_die == NULL)
12713 break;
12714 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12715 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12716 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12717 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12718 add_loc_descr (&op0, cvt);
12720 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12721 if (type_die == NULL)
12722 break;
12723 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12724 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12725 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12726 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12727 add_loc_descr (&op0, cvt);
12728 if (GET_MODE_CLASS (mode) == MODE_INT
12729 && (GET_CODE (rtl) == FIX
12730 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12732 op0 = convert_descriptor_to_mode (mode, op0);
12733 if (op0 == NULL)
12734 break;
12736 mem_loc_result = op0;
12738 break;
12740 case CLZ:
12741 case CTZ:
12742 case FFS:
12743 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12744 break;
12746 case POPCOUNT:
12747 case PARITY:
12748 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12749 break;
12751 case BSWAP:
12752 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12753 break;
12755 case ROTATE:
12756 case ROTATERT:
12757 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12758 break;
12760 case COMPARE:
12761 /* In theory, we could implement the above. */
12762 /* DWARF cannot represent the unsigned compare operations
12763 natively. */
12764 case SS_MULT:
12765 case US_MULT:
12766 case SS_DIV:
12767 case US_DIV:
12768 case SS_PLUS:
12769 case US_PLUS:
12770 case SS_MINUS:
12771 case US_MINUS:
12772 case SS_NEG:
12773 case US_NEG:
12774 case SS_ABS:
12775 case SS_ASHIFT:
12776 case US_ASHIFT:
12777 case SS_TRUNCATE:
12778 case US_TRUNCATE:
12779 case UNORDERED:
12780 case ORDERED:
12781 case UNEQ:
12782 case UNGE:
12783 case UNGT:
12784 case UNLE:
12785 case UNLT:
12786 case LTGT:
12787 case FRACT_CONVERT:
12788 case UNSIGNED_FRACT_CONVERT:
12789 case SAT_FRACT:
12790 case UNSIGNED_SAT_FRACT:
12791 case SQRT:
12792 case ASM_OPERANDS:
12793 case VEC_MERGE:
12794 case VEC_SELECT:
12795 case VEC_CONCAT:
12796 case VEC_DUPLICATE:
12797 case UNSPEC:
12798 case HIGH:
12799 case FMA:
12800 case STRICT_LOW_PART:
12801 case CONST_VECTOR:
12802 case CONST_FIXED:
12803 case CLRSB:
12804 case CLOBBER:
12805 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12806 can't express it in the debug info. This can happen e.g. with some
12807 TLS UNSPECs. */
12808 break;
12810 case CONST_STRING:
12811 resolve_one_addr (&rtl, NULL);
12812 goto symref;
12814 default:
12815 #ifdef ENABLE_CHECKING
12816 print_rtl (stderr, rtl);
12817 gcc_unreachable ();
12818 #else
12819 break;
12820 #endif
12823 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12824 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12826 return mem_loc_result;
12829 /* Return a descriptor that describes the concatenation of two locations.
12830 This is typically a complex variable. */
12832 static dw_loc_descr_ref
12833 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12835 dw_loc_descr_ref cc_loc_result = NULL;
12836 dw_loc_descr_ref x0_ref
12837 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12838 dw_loc_descr_ref x1_ref
12839 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12841 if (x0_ref == 0 || x1_ref == 0)
12842 return 0;
12844 cc_loc_result = x0_ref;
12845 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12847 add_loc_descr (&cc_loc_result, x1_ref);
12848 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12850 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12851 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12853 return cc_loc_result;
12856 /* Return a descriptor that describes the concatenation of N
12857 locations. */
12859 static dw_loc_descr_ref
12860 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12862 unsigned int i;
12863 dw_loc_descr_ref cc_loc_result = NULL;
12864 unsigned int n = XVECLEN (concatn, 0);
12866 for (i = 0; i < n; ++i)
12868 dw_loc_descr_ref ref;
12869 rtx x = XVECEXP (concatn, 0, i);
12871 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12872 if (ref == NULL)
12873 return NULL;
12875 add_loc_descr (&cc_loc_result, ref);
12876 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12879 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12880 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12882 return cc_loc_result;
12885 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12886 for DEBUG_IMPLICIT_PTR RTL. */
12888 static dw_loc_descr_ref
12889 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12891 dw_loc_descr_ref ret;
12892 dw_die_ref ref;
12894 if (dwarf_strict)
12895 return NULL;
12896 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12897 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12898 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12899 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12900 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12901 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12902 if (ref)
12904 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12905 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12906 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12908 else
12910 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12911 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12913 return ret;
12916 /* Output a proper Dwarf location descriptor for a variable or parameter
12917 which is either allocated in a register or in a memory location. For a
12918 register, we just generate an OP_REG and the register number. For a
12919 memory location we provide a Dwarf postfix expression describing how to
12920 generate the (dynamic) address of the object onto the address stack.
12922 MODE is mode of the decl if this loc_descriptor is going to be used in
12923 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12924 allowed, VOIDmode otherwise.
12926 If we don't know how to describe it, return 0. */
12928 static dw_loc_descr_ref
12929 loc_descriptor (rtx rtl, enum machine_mode mode,
12930 enum var_init_status initialized)
12932 dw_loc_descr_ref loc_result = NULL;
12934 switch (GET_CODE (rtl))
12936 case SUBREG:
12937 /* The case of a subreg may arise when we have a local (register)
12938 variable or a formal (register) parameter which doesn't quite fill
12939 up an entire register. For now, just assume that it is
12940 legitimate to make the Dwarf info refer to the whole register which
12941 contains the given subreg. */
12942 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12943 loc_result = loc_descriptor (SUBREG_REG (rtl),
12944 GET_MODE (SUBREG_REG (rtl)), initialized);
12945 else
12946 goto do_default;
12947 break;
12949 case REG:
12950 loc_result = reg_loc_descriptor (rtl, initialized);
12951 break;
12953 case MEM:
12954 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12955 GET_MODE (rtl), initialized);
12956 if (loc_result == NULL)
12957 loc_result = tls_mem_loc_descriptor (rtl);
12958 if (loc_result == NULL)
12960 rtx new_rtl = avoid_constant_pool_reference (rtl);
12961 if (new_rtl != rtl)
12962 loc_result = loc_descriptor (new_rtl, mode, initialized);
12964 break;
12966 case CONCAT:
12967 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12968 initialized);
12969 break;
12971 case CONCATN:
12972 loc_result = concatn_loc_descriptor (rtl, initialized);
12973 break;
12975 case VAR_LOCATION:
12976 /* Single part. */
12977 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12979 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12980 if (GET_CODE (loc) == EXPR_LIST)
12981 loc = XEXP (loc, 0);
12982 loc_result = loc_descriptor (loc, mode, initialized);
12983 break;
12986 rtl = XEXP (rtl, 1);
12987 /* FALLTHRU */
12989 case PARALLEL:
12991 rtvec par_elems = XVEC (rtl, 0);
12992 int num_elem = GET_NUM_ELEM (par_elems);
12993 enum machine_mode mode;
12994 int i;
12996 /* Create the first one, so we have something to add to. */
12997 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12998 VOIDmode, initialized);
12999 if (loc_result == NULL)
13000 return NULL;
13001 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13002 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13003 for (i = 1; i < num_elem; i++)
13005 dw_loc_descr_ref temp;
13007 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13008 VOIDmode, initialized);
13009 if (temp == NULL)
13010 return NULL;
13011 add_loc_descr (&loc_result, temp);
13012 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13013 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13016 break;
13018 case CONST_INT:
13019 if (mode != VOIDmode && mode != BLKmode)
13020 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13021 INTVAL (rtl));
13022 break;
13024 case CONST_DOUBLE:
13025 if (mode == VOIDmode)
13026 mode = GET_MODE (rtl);
13028 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13030 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13032 /* Note that a CONST_DOUBLE rtx could represent either an integer
13033 or a floating-point constant. A CONST_DOUBLE is used whenever
13034 the constant requires more than one word in order to be
13035 adequately represented. We output CONST_DOUBLEs as blocks. */
13036 loc_result = new_loc_descr (DW_OP_implicit_value,
13037 GET_MODE_SIZE (mode), 0);
13038 if (SCALAR_FLOAT_MODE_P (mode))
13040 unsigned int length = GET_MODE_SIZE (mode);
13041 unsigned char *array
13042 = (unsigned char*) ggc_alloc_atomic (length);
13044 insert_float (rtl, array);
13045 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13046 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13047 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13048 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13050 else
13052 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13053 loc_result->dw_loc_oprnd2.v.val_double
13054 = rtx_to_double_int (rtl);
13057 break;
13059 case CONST_VECTOR:
13060 if (mode == VOIDmode)
13061 mode = GET_MODE (rtl);
13063 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13065 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13066 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13067 unsigned char *array = (unsigned char *)
13068 ggc_alloc_atomic (length * elt_size);
13069 unsigned int i;
13070 unsigned char *p;
13072 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13073 switch (GET_MODE_CLASS (mode))
13075 case MODE_VECTOR_INT:
13076 for (i = 0, p = array; i < length; i++, p += elt_size)
13078 rtx elt = CONST_VECTOR_ELT (rtl, i);
13079 double_int val = rtx_to_double_int (elt);
13081 if (elt_size <= sizeof (HOST_WIDE_INT))
13082 insert_int (val.to_shwi (), elt_size, p);
13083 else
13085 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13086 insert_double (val, p);
13089 break;
13091 case MODE_VECTOR_FLOAT:
13092 for (i = 0, p = array; i < length; i++, p += elt_size)
13094 rtx elt = CONST_VECTOR_ELT (rtl, i);
13095 insert_float (elt, p);
13097 break;
13099 default:
13100 gcc_unreachable ();
13103 loc_result = new_loc_descr (DW_OP_implicit_value,
13104 length * elt_size, 0);
13105 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13106 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13107 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13108 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13110 break;
13112 case CONST:
13113 if (mode == VOIDmode
13114 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13115 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13116 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13118 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13119 break;
13121 /* FALLTHROUGH */
13122 case SYMBOL_REF:
13123 if (!const_ok_for_output (rtl))
13124 break;
13125 case LABEL_REF:
13126 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13127 && (dwarf_version >= 4 || !dwarf_strict))
13129 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13130 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13131 vec_safe_push (used_rtx_array, rtl);
13133 break;
13135 case DEBUG_IMPLICIT_PTR:
13136 loc_result = implicit_ptr_descriptor (rtl, 0);
13137 break;
13139 case PLUS:
13140 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13141 && CONST_INT_P (XEXP (rtl, 1)))
13143 loc_result
13144 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13145 break;
13147 /* FALLTHRU */
13148 do_default:
13149 default:
13150 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13151 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13152 && dwarf_version >= 4)
13153 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13155 /* Value expression. */
13156 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13157 if (loc_result)
13158 add_loc_descr (&loc_result,
13159 new_loc_descr (DW_OP_stack_value, 0, 0));
13161 break;
13164 return loc_result;
13167 /* We need to figure out what section we should use as the base for the
13168 address ranges where a given location is valid.
13169 1. If this particular DECL has a section associated with it, use that.
13170 2. If this function has a section associated with it, use that.
13171 3. Otherwise, use the text section.
13172 XXX: If you split a variable across multiple sections, we won't notice. */
13174 static const char *
13175 secname_for_decl (const_tree decl)
13177 const char *secname;
13179 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13181 tree sectree = DECL_SECTION_NAME (decl);
13182 secname = TREE_STRING_POINTER (sectree);
13184 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13186 tree sectree = DECL_SECTION_NAME (current_function_decl);
13187 secname = TREE_STRING_POINTER (sectree);
13189 else if (cfun && in_cold_section_p)
13190 secname = crtl->subsections.cold_section_label;
13191 else
13192 secname = text_section_label;
13194 return secname;
13197 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13199 static bool
13200 decl_by_reference_p (tree decl)
13202 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13203 || TREE_CODE (decl) == VAR_DECL)
13204 && DECL_BY_REFERENCE (decl));
13207 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13208 for VARLOC. */
13210 static dw_loc_descr_ref
13211 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13212 enum var_init_status initialized)
13214 int have_address = 0;
13215 dw_loc_descr_ref descr;
13216 enum machine_mode mode;
13218 if (want_address != 2)
13220 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13221 /* Single part. */
13222 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13224 varloc = PAT_VAR_LOCATION_LOC (varloc);
13225 if (GET_CODE (varloc) == EXPR_LIST)
13226 varloc = XEXP (varloc, 0);
13227 mode = GET_MODE (varloc);
13228 if (MEM_P (varloc))
13230 rtx addr = XEXP (varloc, 0);
13231 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13232 mode, initialized);
13233 if (descr)
13234 have_address = 1;
13235 else
13237 rtx x = avoid_constant_pool_reference (varloc);
13238 if (x != varloc)
13239 descr = mem_loc_descriptor (x, mode, VOIDmode,
13240 initialized);
13243 else
13244 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13246 else
13247 return 0;
13249 else
13251 if (GET_CODE (varloc) == VAR_LOCATION)
13252 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13253 else
13254 mode = DECL_MODE (loc);
13255 descr = loc_descriptor (varloc, mode, initialized);
13256 have_address = 1;
13259 if (!descr)
13260 return 0;
13262 if (want_address == 2 && !have_address
13263 && (dwarf_version >= 4 || !dwarf_strict))
13265 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13267 expansion_failed (loc, NULL_RTX,
13268 "DWARF address size mismatch");
13269 return 0;
13271 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13272 have_address = 1;
13274 /* Show if we can't fill the request for an address. */
13275 if (want_address && !have_address)
13277 expansion_failed (loc, NULL_RTX,
13278 "Want address and only have value");
13279 return 0;
13282 /* If we've got an address and don't want one, dereference. */
13283 if (!want_address && have_address)
13285 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13286 enum dwarf_location_atom op;
13288 if (size > DWARF2_ADDR_SIZE || size == -1)
13290 expansion_failed (loc, NULL_RTX,
13291 "DWARF address size mismatch");
13292 return 0;
13294 else if (size == DWARF2_ADDR_SIZE)
13295 op = DW_OP_deref;
13296 else
13297 op = DW_OP_deref_size;
13299 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13302 return descr;
13305 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13306 if it is not possible. */
13308 static dw_loc_descr_ref
13309 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13311 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13312 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13313 else if (dwarf_version >= 3 || !dwarf_strict)
13314 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13315 else
13316 return NULL;
13319 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13320 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13322 static dw_loc_descr_ref
13323 dw_sra_loc_expr (tree decl, rtx loc)
13325 rtx p;
13326 unsigned int padsize = 0;
13327 dw_loc_descr_ref descr, *descr_tail;
13328 unsigned HOST_WIDE_INT decl_size;
13329 rtx varloc;
13330 enum var_init_status initialized;
13332 if (DECL_SIZE (decl) == NULL
13333 || !host_integerp (DECL_SIZE (decl), 1))
13334 return NULL;
13336 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13337 descr = NULL;
13338 descr_tail = &descr;
13340 for (p = loc; p; p = XEXP (p, 1))
13342 unsigned int bitsize = decl_piece_bitsize (p);
13343 rtx loc_note = *decl_piece_varloc_ptr (p);
13344 dw_loc_descr_ref cur_descr;
13345 dw_loc_descr_ref *tail, last = NULL;
13346 unsigned int opsize = 0;
13348 if (loc_note == NULL_RTX
13349 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13351 padsize += bitsize;
13352 continue;
13354 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13355 varloc = NOTE_VAR_LOCATION (loc_note);
13356 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13357 if (cur_descr == NULL)
13359 padsize += bitsize;
13360 continue;
13363 /* Check that cur_descr either doesn't use
13364 DW_OP_*piece operations, or their sum is equal
13365 to bitsize. Otherwise we can't embed it. */
13366 for (tail = &cur_descr; *tail != NULL;
13367 tail = &(*tail)->dw_loc_next)
13368 if ((*tail)->dw_loc_opc == DW_OP_piece)
13370 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13371 * BITS_PER_UNIT;
13372 last = *tail;
13374 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13376 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13377 last = *tail;
13380 if (last != NULL && opsize != bitsize)
13382 padsize += bitsize;
13383 continue;
13386 /* If there is a hole, add DW_OP_*piece after empty DWARF
13387 expression, which means that those bits are optimized out. */
13388 if (padsize)
13390 if (padsize > decl_size)
13391 return NULL;
13392 decl_size -= padsize;
13393 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13394 if (*descr_tail == NULL)
13395 return NULL;
13396 descr_tail = &(*descr_tail)->dw_loc_next;
13397 padsize = 0;
13399 *descr_tail = cur_descr;
13400 descr_tail = tail;
13401 if (bitsize > decl_size)
13402 return NULL;
13403 decl_size -= bitsize;
13404 if (last == NULL)
13406 HOST_WIDE_INT offset = 0;
13407 if (GET_CODE (varloc) == VAR_LOCATION
13408 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13410 varloc = PAT_VAR_LOCATION_LOC (varloc);
13411 if (GET_CODE (varloc) == EXPR_LIST)
13412 varloc = XEXP (varloc, 0);
13416 if (GET_CODE (varloc) == CONST
13417 || GET_CODE (varloc) == SIGN_EXTEND
13418 || GET_CODE (varloc) == ZERO_EXTEND)
13419 varloc = XEXP (varloc, 0);
13420 else if (GET_CODE (varloc) == SUBREG)
13421 varloc = SUBREG_REG (varloc);
13422 else
13423 break;
13425 while (1);
13426 /* DW_OP_bit_size offset should be zero for register
13427 or implicit location descriptions and empty location
13428 descriptions, but for memory addresses needs big endian
13429 adjustment. */
13430 if (MEM_P (varloc))
13432 unsigned HOST_WIDE_INT memsize
13433 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13434 if (memsize != bitsize)
13436 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13437 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13438 return NULL;
13439 if (memsize < bitsize)
13440 return NULL;
13441 if (BITS_BIG_ENDIAN)
13442 offset = memsize - bitsize;
13446 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13447 if (*descr_tail == NULL)
13448 return NULL;
13449 descr_tail = &(*descr_tail)->dw_loc_next;
13453 /* If there were any non-empty expressions, add padding till the end of
13454 the decl. */
13455 if (descr != NULL && decl_size != 0)
13457 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13458 if (*descr_tail == NULL)
13459 return NULL;
13461 return descr;
13464 /* Return the dwarf representation of the location list LOC_LIST of
13465 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13466 function. */
13468 static dw_loc_list_ref
13469 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13471 const char *endname, *secname;
13472 rtx varloc;
13473 enum var_init_status initialized;
13474 struct var_loc_node *node;
13475 dw_loc_descr_ref descr;
13476 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13477 dw_loc_list_ref list = NULL;
13478 dw_loc_list_ref *listp = &list;
13480 /* Now that we know what section we are using for a base,
13481 actually construct the list of locations.
13482 The first location information is what is passed to the
13483 function that creates the location list, and the remaining
13484 locations just get added on to that list.
13485 Note that we only know the start address for a location
13486 (IE location changes), so to build the range, we use
13487 the range [current location start, next location start].
13488 This means we have to special case the last node, and generate
13489 a range of [last location start, end of function label]. */
13491 secname = secname_for_decl (decl);
13493 for (node = loc_list->first; node; node = node->next)
13494 if (GET_CODE (node->loc) == EXPR_LIST
13495 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13497 if (GET_CODE (node->loc) == EXPR_LIST)
13499 /* This requires DW_OP_{,bit_}piece, which is not usable
13500 inside DWARF expressions. */
13501 if (want_address != 2)
13502 continue;
13503 descr = dw_sra_loc_expr (decl, node->loc);
13504 if (descr == NULL)
13505 continue;
13507 else
13509 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13510 varloc = NOTE_VAR_LOCATION (node->loc);
13511 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13513 if (descr)
13515 bool range_across_switch = false;
13516 /* If section switch happens in between node->label
13517 and node->next->label (or end of function) and
13518 we can't emit it as a single entry list,
13519 emit two ranges, first one ending at the end
13520 of first partition and second one starting at the
13521 beginning of second partition. */
13522 if (node == loc_list->last_before_switch
13523 && (node != loc_list->first || loc_list->first->next)
13524 && current_function_decl)
13526 endname = cfun->fde->dw_fde_end;
13527 range_across_switch = true;
13529 /* The variable has a location between NODE->LABEL and
13530 NODE->NEXT->LABEL. */
13531 else if (node->next)
13532 endname = node->next->label;
13533 /* If the variable has a location at the last label
13534 it keeps its location until the end of function. */
13535 else if (!current_function_decl)
13536 endname = text_end_label;
13537 else
13539 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13540 current_function_funcdef_no);
13541 endname = ggc_strdup (label_id);
13544 *listp = new_loc_list (descr, node->label, endname, secname);
13545 if (TREE_CODE (decl) == PARM_DECL
13546 && node == loc_list->first
13547 && NOTE_P (node->loc)
13548 && strcmp (node->label, endname) == 0)
13549 (*listp)->force = true;
13550 listp = &(*listp)->dw_loc_next;
13552 if (range_across_switch)
13554 if (GET_CODE (node->loc) == EXPR_LIST)
13555 descr = dw_sra_loc_expr (decl, node->loc);
13556 else
13558 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13559 varloc = NOTE_VAR_LOCATION (node->loc);
13560 descr = dw_loc_list_1 (decl, varloc, want_address,
13561 initialized);
13563 gcc_assert (descr);
13564 /* The variable has a location between NODE->LABEL and
13565 NODE->NEXT->LABEL. */
13566 if (node->next)
13567 endname = node->next->label;
13568 else
13569 endname = cfun->fde->dw_fde_second_end;
13570 *listp = new_loc_list (descr,
13571 cfun->fde->dw_fde_second_begin,
13572 endname, secname);
13573 listp = &(*listp)->dw_loc_next;
13578 /* Try to avoid the overhead of a location list emitting a location
13579 expression instead, but only if we didn't have more than one
13580 location entry in the first place. If some entries were not
13581 representable, we don't want to pretend a single entry that was
13582 applies to the entire scope in which the variable is
13583 available. */
13584 if (list && loc_list->first->next)
13585 gen_llsym (list);
13587 return list;
13590 /* Return if the loc_list has only single element and thus can be represented
13591 as location description. */
13593 static bool
13594 single_element_loc_list_p (dw_loc_list_ref list)
13596 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13597 return !list->ll_symbol;
13600 /* To each location in list LIST add loc descr REF. */
13602 static void
13603 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13605 dw_loc_descr_ref copy;
13606 add_loc_descr (&list->expr, ref);
13607 list = list->dw_loc_next;
13608 while (list)
13610 copy = ggc_alloc_dw_loc_descr_node ();
13611 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13612 add_loc_descr (&list->expr, copy);
13613 while (copy->dw_loc_next)
13615 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13616 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13617 copy->dw_loc_next = new_copy;
13618 copy = new_copy;
13620 list = list->dw_loc_next;
13624 /* Given two lists RET and LIST
13625 produce location list that is result of adding expression in LIST
13626 to expression in RET on each position in program.
13627 Might be destructive on both RET and LIST.
13629 TODO: We handle only simple cases of RET or LIST having at most one
13630 element. General case would inolve sorting the lists in program order
13631 and merging them that will need some additional work.
13632 Adding that will improve quality of debug info especially for SRA-ed
13633 structures. */
13635 static void
13636 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13638 if (!list)
13639 return;
13640 if (!*ret)
13642 *ret = list;
13643 return;
13645 if (!list->dw_loc_next)
13647 add_loc_descr_to_each (*ret, list->expr);
13648 return;
13650 if (!(*ret)->dw_loc_next)
13652 add_loc_descr_to_each (list, (*ret)->expr);
13653 *ret = list;
13654 return;
13656 expansion_failed (NULL_TREE, NULL_RTX,
13657 "Don't know how to merge two non-trivial"
13658 " location lists.\n");
13659 *ret = NULL;
13660 return;
13663 /* LOC is constant expression. Try a luck, look it up in constant
13664 pool and return its loc_descr of its address. */
13666 static dw_loc_descr_ref
13667 cst_pool_loc_descr (tree loc)
13669 /* Get an RTL for this, if something has been emitted. */
13670 rtx rtl = lookup_constant_def (loc);
13672 if (!rtl || !MEM_P (rtl))
13674 gcc_assert (!rtl);
13675 return 0;
13677 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13679 /* TODO: We might get more coverage if we was actually delaying expansion
13680 of all expressions till end of compilation when constant pools are fully
13681 populated. */
13682 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13684 expansion_failed (loc, NULL_RTX,
13685 "CST value in contant pool but not marked.");
13686 return 0;
13688 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13689 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13692 /* Return dw_loc_list representing address of addr_expr LOC
13693 by looking for inner INDIRECT_REF expression and turning
13694 it into simple arithmetics. */
13696 static dw_loc_list_ref
13697 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13699 tree obj, offset;
13700 HOST_WIDE_INT bitsize, bitpos, bytepos;
13701 enum machine_mode mode;
13702 int unsignedp, volatilep = 0;
13703 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13705 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13706 &bitsize, &bitpos, &offset, &mode,
13707 &unsignedp, &volatilep, false);
13708 STRIP_NOPS (obj);
13709 if (bitpos % BITS_PER_UNIT)
13711 expansion_failed (loc, NULL_RTX, "bitfield access");
13712 return 0;
13714 if (!INDIRECT_REF_P (obj))
13716 expansion_failed (obj,
13717 NULL_RTX, "no indirect ref in inner refrence");
13718 return 0;
13720 if (!offset && !bitpos)
13721 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13722 else if (toplev
13723 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13724 && (dwarf_version >= 4 || !dwarf_strict))
13726 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13727 if (!list_ret)
13728 return 0;
13729 if (offset)
13731 /* Variable offset. */
13732 list_ret1 = loc_list_from_tree (offset, 0);
13733 if (list_ret1 == 0)
13734 return 0;
13735 add_loc_list (&list_ret, list_ret1);
13736 if (!list_ret)
13737 return 0;
13738 add_loc_descr_to_each (list_ret,
13739 new_loc_descr (DW_OP_plus, 0, 0));
13741 bytepos = bitpos / BITS_PER_UNIT;
13742 if (bytepos > 0)
13743 add_loc_descr_to_each (list_ret,
13744 new_loc_descr (DW_OP_plus_uconst,
13745 bytepos, 0));
13746 else if (bytepos < 0)
13747 loc_list_plus_const (list_ret, bytepos);
13748 add_loc_descr_to_each (list_ret,
13749 new_loc_descr (DW_OP_stack_value, 0, 0));
13751 return list_ret;
13755 /* Generate Dwarf location list representing LOC.
13756 If WANT_ADDRESS is false, expression computing LOC will be computed
13757 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13758 if WANT_ADDRESS is 2, expression computing address useable in location
13759 will be returned (i.e. DW_OP_reg can be used
13760 to refer to register values). */
13762 static dw_loc_list_ref
13763 loc_list_from_tree (tree loc, int want_address)
13765 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13766 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13767 int have_address = 0;
13768 enum dwarf_location_atom op;
13770 /* ??? Most of the time we do not take proper care for sign/zero
13771 extending the values properly. Hopefully this won't be a real
13772 problem... */
13774 switch (TREE_CODE (loc))
13776 case ERROR_MARK:
13777 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13778 return 0;
13780 case PLACEHOLDER_EXPR:
13781 /* This case involves extracting fields from an object to determine the
13782 position of other fields. We don't try to encode this here. The
13783 only user of this is Ada, which encodes the needed information using
13784 the names of types. */
13785 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13786 return 0;
13788 case CALL_EXPR:
13789 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13790 /* There are no opcodes for these operations. */
13791 return 0;
13793 case PREINCREMENT_EXPR:
13794 case PREDECREMENT_EXPR:
13795 case POSTINCREMENT_EXPR:
13796 case POSTDECREMENT_EXPR:
13797 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13798 /* There are no opcodes for these operations. */
13799 return 0;
13801 case ADDR_EXPR:
13802 /* If we already want an address, see if there is INDIRECT_REF inside
13803 e.g. for &this->field. */
13804 if (want_address)
13806 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13807 (loc, want_address == 2);
13808 if (list_ret)
13809 have_address = 1;
13810 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13811 && (ret = cst_pool_loc_descr (loc)))
13812 have_address = 1;
13814 /* Otherwise, process the argument and look for the address. */
13815 if (!list_ret && !ret)
13816 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13817 else
13819 if (want_address)
13820 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13821 return NULL;
13823 break;
13825 case VAR_DECL:
13826 if (DECL_THREAD_LOCAL_P (loc))
13828 rtx rtl;
13829 enum dwarf_location_atom tls_op;
13830 enum dtprel_bool dtprel = dtprel_false;
13832 if (targetm.have_tls)
13834 /* If this is not defined, we have no way to emit the
13835 data. */
13836 if (!targetm.asm_out.output_dwarf_dtprel)
13837 return 0;
13839 /* The way DW_OP_GNU_push_tls_address is specified, we
13840 can only look up addresses of objects in the current
13841 module. We used DW_OP_addr as first op, but that's
13842 wrong, because DW_OP_addr is relocated by the debug
13843 info consumer, while DW_OP_GNU_push_tls_address
13844 operand shouldn't be. */
13845 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13846 return 0;
13847 dtprel = dtprel_true;
13848 tls_op = DW_OP_GNU_push_tls_address;
13850 else
13852 if (!targetm.emutls.debug_form_tls_address
13853 || !(dwarf_version >= 3 || !dwarf_strict))
13854 return 0;
13855 /* We stuffed the control variable into the DECL_VALUE_EXPR
13856 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13857 no longer appear in gimple code. We used the control
13858 variable in specific so that we could pick it up here. */
13859 loc = DECL_VALUE_EXPR (loc);
13860 tls_op = DW_OP_form_tls_address;
13863 rtl = rtl_for_decl_location (loc);
13864 if (rtl == NULL_RTX)
13865 return 0;
13867 if (!MEM_P (rtl))
13868 return 0;
13869 rtl = XEXP (rtl, 0);
13870 if (! CONSTANT_P (rtl))
13871 return 0;
13873 ret = new_addr_loc_descr (rtl, dtprel);
13874 ret1 = new_loc_descr (tls_op, 0, 0);
13875 add_loc_descr (&ret, ret1);
13877 have_address = 1;
13878 break;
13880 /* FALLTHRU */
13882 case PARM_DECL:
13883 case RESULT_DECL:
13884 if (DECL_HAS_VALUE_EXPR_P (loc))
13885 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13886 want_address);
13887 /* FALLTHRU */
13889 case FUNCTION_DECL:
13891 rtx rtl;
13892 var_loc_list *loc_list = lookup_decl_loc (loc);
13894 if (loc_list && loc_list->first)
13896 list_ret = dw_loc_list (loc_list, loc, want_address);
13897 have_address = want_address != 0;
13898 break;
13900 rtl = rtl_for_decl_location (loc);
13901 if (rtl == NULL_RTX)
13903 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13904 return 0;
13906 else if (CONST_INT_P (rtl))
13908 HOST_WIDE_INT val = INTVAL (rtl);
13909 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13910 val &= GET_MODE_MASK (DECL_MODE (loc));
13911 ret = int_loc_descriptor (val);
13913 else if (GET_CODE (rtl) == CONST_STRING)
13915 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13916 return 0;
13918 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13919 ret = new_addr_loc_descr (rtl, dtprel_false);
13920 else
13922 enum machine_mode mode, mem_mode;
13924 /* Certain constructs can only be represented at top-level. */
13925 if (want_address == 2)
13927 ret = loc_descriptor (rtl, VOIDmode,
13928 VAR_INIT_STATUS_INITIALIZED);
13929 have_address = 1;
13931 else
13933 mode = GET_MODE (rtl);
13934 mem_mode = VOIDmode;
13935 if (MEM_P (rtl))
13937 mem_mode = mode;
13938 mode = get_address_mode (rtl);
13939 rtl = XEXP (rtl, 0);
13940 have_address = 1;
13942 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13943 VAR_INIT_STATUS_INITIALIZED);
13945 if (!ret)
13946 expansion_failed (loc, rtl,
13947 "failed to produce loc descriptor for rtl");
13950 break;
13952 case MEM_REF:
13953 /* ??? FIXME. */
13954 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13955 return 0;
13956 /* Fallthru. */
13957 case INDIRECT_REF:
13958 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13959 have_address = 1;
13960 break;
13962 case COMPOUND_EXPR:
13963 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13965 CASE_CONVERT:
13966 case VIEW_CONVERT_EXPR:
13967 case SAVE_EXPR:
13968 case MODIFY_EXPR:
13969 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13971 case COMPONENT_REF:
13972 case BIT_FIELD_REF:
13973 case ARRAY_REF:
13974 case ARRAY_RANGE_REF:
13975 case REALPART_EXPR:
13976 case IMAGPART_EXPR:
13978 tree obj, offset;
13979 HOST_WIDE_INT bitsize, bitpos, bytepos;
13980 enum machine_mode mode;
13981 int unsignedp, volatilep = 0;
13983 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13984 &unsignedp, &volatilep, false);
13986 gcc_assert (obj != loc);
13988 list_ret = loc_list_from_tree (obj,
13989 want_address == 2
13990 && !bitpos && !offset ? 2 : 1);
13991 /* TODO: We can extract value of the small expression via shifting even
13992 for nonzero bitpos. */
13993 if (list_ret == 0)
13994 return 0;
13995 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13997 expansion_failed (loc, NULL_RTX,
13998 "bitfield access");
13999 return 0;
14002 if (offset != NULL_TREE)
14004 /* Variable offset. */
14005 list_ret1 = loc_list_from_tree (offset, 0);
14006 if (list_ret1 == 0)
14007 return 0;
14008 add_loc_list (&list_ret, list_ret1);
14009 if (!list_ret)
14010 return 0;
14011 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14014 bytepos = bitpos / BITS_PER_UNIT;
14015 if (bytepos > 0)
14016 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14017 else if (bytepos < 0)
14018 loc_list_plus_const (list_ret, bytepos);
14020 have_address = 1;
14021 break;
14024 case INTEGER_CST:
14025 if ((want_address || !host_integerp (loc, 0))
14026 && (ret = cst_pool_loc_descr (loc)))
14027 have_address = 1;
14028 else if (want_address == 2
14029 && host_integerp (loc, 0)
14030 && (ret = address_of_int_loc_descriptor
14031 (int_size_in_bytes (TREE_TYPE (loc)),
14032 tree_low_cst (loc, 0))))
14033 have_address = 1;
14034 else if (host_integerp (loc, 0))
14035 ret = int_loc_descriptor (tree_low_cst (loc, 0));
14036 else
14038 expansion_failed (loc, NULL_RTX,
14039 "Integer operand is not host integer");
14040 return 0;
14042 break;
14044 case CONSTRUCTOR:
14045 case REAL_CST:
14046 case STRING_CST:
14047 case COMPLEX_CST:
14048 if ((ret = cst_pool_loc_descr (loc)))
14049 have_address = 1;
14050 else
14051 /* We can construct small constants here using int_loc_descriptor. */
14052 expansion_failed (loc, NULL_RTX,
14053 "constructor or constant not in constant pool");
14054 break;
14056 case TRUTH_AND_EXPR:
14057 case TRUTH_ANDIF_EXPR:
14058 case BIT_AND_EXPR:
14059 op = DW_OP_and;
14060 goto do_binop;
14062 case TRUTH_XOR_EXPR:
14063 case BIT_XOR_EXPR:
14064 op = DW_OP_xor;
14065 goto do_binop;
14067 case TRUTH_OR_EXPR:
14068 case TRUTH_ORIF_EXPR:
14069 case BIT_IOR_EXPR:
14070 op = DW_OP_or;
14071 goto do_binop;
14073 case FLOOR_DIV_EXPR:
14074 case CEIL_DIV_EXPR:
14075 case ROUND_DIV_EXPR:
14076 case TRUNC_DIV_EXPR:
14077 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14078 return 0;
14079 op = DW_OP_div;
14080 goto do_binop;
14082 case MINUS_EXPR:
14083 op = DW_OP_minus;
14084 goto do_binop;
14086 case FLOOR_MOD_EXPR:
14087 case CEIL_MOD_EXPR:
14088 case ROUND_MOD_EXPR:
14089 case TRUNC_MOD_EXPR:
14090 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14092 op = DW_OP_mod;
14093 goto do_binop;
14095 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14096 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14097 if (list_ret == 0 || list_ret1 == 0)
14098 return 0;
14100 add_loc_list (&list_ret, list_ret1);
14101 if (list_ret == 0)
14102 return 0;
14103 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 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_div, 0, 0));
14106 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14107 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14108 break;
14110 case MULT_EXPR:
14111 op = DW_OP_mul;
14112 goto do_binop;
14114 case LSHIFT_EXPR:
14115 op = DW_OP_shl;
14116 goto do_binop;
14118 case RSHIFT_EXPR:
14119 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14120 goto do_binop;
14122 case POINTER_PLUS_EXPR:
14123 case PLUS_EXPR:
14124 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14126 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14127 if (list_ret == 0)
14128 return 0;
14130 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14131 break;
14134 op = DW_OP_plus;
14135 goto do_binop;
14137 case LE_EXPR:
14138 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14139 return 0;
14141 op = DW_OP_le;
14142 goto do_binop;
14144 case GE_EXPR:
14145 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14146 return 0;
14148 op = DW_OP_ge;
14149 goto do_binop;
14151 case LT_EXPR:
14152 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14153 return 0;
14155 op = DW_OP_lt;
14156 goto do_binop;
14158 case GT_EXPR:
14159 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14160 return 0;
14162 op = DW_OP_gt;
14163 goto do_binop;
14165 case EQ_EXPR:
14166 op = DW_OP_eq;
14167 goto do_binop;
14169 case NE_EXPR:
14170 op = DW_OP_ne;
14171 goto do_binop;
14173 do_binop:
14174 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14175 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14176 if (list_ret == 0 || list_ret1 == 0)
14177 return 0;
14179 add_loc_list (&list_ret, list_ret1);
14180 if (list_ret == 0)
14181 return 0;
14182 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14183 break;
14185 case TRUTH_NOT_EXPR:
14186 case BIT_NOT_EXPR:
14187 op = DW_OP_not;
14188 goto do_unop;
14190 case ABS_EXPR:
14191 op = DW_OP_abs;
14192 goto do_unop;
14194 case NEGATE_EXPR:
14195 op = DW_OP_neg;
14196 goto do_unop;
14198 do_unop:
14199 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14200 if (list_ret == 0)
14201 return 0;
14203 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14204 break;
14206 case MIN_EXPR:
14207 case MAX_EXPR:
14209 const enum tree_code code =
14210 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14212 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14213 build2 (code, integer_type_node,
14214 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14215 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14218 /* ... fall through ... */
14220 case COND_EXPR:
14222 dw_loc_descr_ref lhs
14223 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14224 dw_loc_list_ref rhs
14225 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14226 dw_loc_descr_ref bra_node, jump_node, tmp;
14228 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14229 if (list_ret == 0 || lhs == 0 || rhs == 0)
14230 return 0;
14232 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14233 add_loc_descr_to_each (list_ret, bra_node);
14235 add_loc_list (&list_ret, rhs);
14236 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14237 add_loc_descr_to_each (list_ret, jump_node);
14239 add_loc_descr_to_each (list_ret, lhs);
14240 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14241 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14243 /* ??? Need a node to point the skip at. Use a nop. */
14244 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14245 add_loc_descr_to_each (list_ret, tmp);
14246 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14247 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14249 break;
14251 case FIX_TRUNC_EXPR:
14252 return 0;
14254 default:
14255 /* Leave front-end specific codes as simply unknown. This comes
14256 up, for instance, with the C STMT_EXPR. */
14257 if ((unsigned int) TREE_CODE (loc)
14258 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14260 expansion_failed (loc, NULL_RTX,
14261 "language specific tree node");
14262 return 0;
14265 #ifdef ENABLE_CHECKING
14266 /* Otherwise this is a generic code; we should just lists all of
14267 these explicitly. We forgot one. */
14268 gcc_unreachable ();
14269 #else
14270 /* In a release build, we want to degrade gracefully: better to
14271 generate incomplete debugging information than to crash. */
14272 return NULL;
14273 #endif
14276 if (!ret && !list_ret)
14277 return 0;
14279 if (want_address == 2 && !have_address
14280 && (dwarf_version >= 4 || !dwarf_strict))
14282 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14284 expansion_failed (loc, NULL_RTX,
14285 "DWARF address size mismatch");
14286 return 0;
14288 if (ret)
14289 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14290 else
14291 add_loc_descr_to_each (list_ret,
14292 new_loc_descr (DW_OP_stack_value, 0, 0));
14293 have_address = 1;
14295 /* Show if we can't fill the request for an address. */
14296 if (want_address && !have_address)
14298 expansion_failed (loc, NULL_RTX,
14299 "Want address and only have value");
14300 return 0;
14303 gcc_assert (!ret || !list_ret);
14305 /* If we've got an address and don't want one, dereference. */
14306 if (!want_address && have_address)
14308 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14310 if (size > DWARF2_ADDR_SIZE || size == -1)
14312 expansion_failed (loc, NULL_RTX,
14313 "DWARF address size mismatch");
14314 return 0;
14316 else if (size == DWARF2_ADDR_SIZE)
14317 op = DW_OP_deref;
14318 else
14319 op = DW_OP_deref_size;
14321 if (ret)
14322 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14323 else
14324 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14326 if (ret)
14327 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14329 return list_ret;
14332 /* Same as above but return only single location expression. */
14333 static dw_loc_descr_ref
14334 loc_descriptor_from_tree (tree loc, int want_address)
14336 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14337 if (!ret)
14338 return NULL;
14339 if (ret->dw_loc_next)
14341 expansion_failed (loc, NULL_RTX,
14342 "Location list where only loc descriptor needed");
14343 return NULL;
14345 return ret->expr;
14348 /* Given a value, round it up to the lowest multiple of `boundary'
14349 which is not less than the value itself. */
14351 static inline HOST_WIDE_INT
14352 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14354 return (((value + boundary - 1) / boundary) * boundary);
14357 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14358 pointer to the declared type for the relevant field variable, or return
14359 `integer_type_node' if the given node turns out to be an
14360 ERROR_MARK node. */
14362 static inline tree
14363 field_type (const_tree decl)
14365 tree type;
14367 if (TREE_CODE (decl) == ERROR_MARK)
14368 return integer_type_node;
14370 type = DECL_BIT_FIELD_TYPE (decl);
14371 if (type == NULL_TREE)
14372 type = TREE_TYPE (decl);
14374 return type;
14377 /* Given a pointer to a tree node, return the alignment in bits for
14378 it, or else return BITS_PER_WORD if the node actually turns out to
14379 be an ERROR_MARK node. */
14381 static inline unsigned
14382 simple_type_align_in_bits (const_tree type)
14384 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14387 static inline unsigned
14388 simple_decl_align_in_bits (const_tree decl)
14390 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14393 /* Return the result of rounding T up to ALIGN. */
14395 static inline double_int
14396 round_up_to_align (double_int t, unsigned int align)
14398 double_int alignd = double_int::from_uhwi (align);
14399 t += alignd;
14400 t += double_int_minus_one;
14401 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14402 t *= alignd;
14403 return t;
14406 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14407 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14408 or return 0 if we are unable to determine what that offset is, either
14409 because the argument turns out to be a pointer to an ERROR_MARK node, or
14410 because the offset is actually variable. (We can't handle the latter case
14411 just yet). */
14413 static HOST_WIDE_INT
14414 field_byte_offset (const_tree decl)
14416 double_int object_offset_in_bits;
14417 double_int object_offset_in_bytes;
14418 double_int bitpos_int;
14420 if (TREE_CODE (decl) == ERROR_MARK)
14421 return 0;
14423 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14425 /* We cannot yet cope with fields whose positions are variable, so
14426 for now, when we see such things, we simply return 0. Someday, we may
14427 be able to handle such cases, but it will be damn difficult. */
14428 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14429 return 0;
14431 bitpos_int = tree_to_double_int (bit_position (decl));
14433 #ifdef PCC_BITFIELD_TYPE_MATTERS
14434 if (PCC_BITFIELD_TYPE_MATTERS)
14436 tree type;
14437 tree field_size_tree;
14438 double_int deepest_bitpos;
14439 double_int field_size_in_bits;
14440 unsigned int type_align_in_bits;
14441 unsigned int decl_align_in_bits;
14442 double_int type_size_in_bits;
14444 type = field_type (decl);
14445 type_size_in_bits = double_int_type_size_in_bits (type);
14446 type_align_in_bits = simple_type_align_in_bits (type);
14448 field_size_tree = DECL_SIZE (decl);
14450 /* The size could be unspecified if there was an error, or for
14451 a flexible array member. */
14452 if (!field_size_tree)
14453 field_size_tree = bitsize_zero_node;
14455 /* If the size of the field is not constant, use the type size. */
14456 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14457 field_size_in_bits = tree_to_double_int (field_size_tree);
14458 else
14459 field_size_in_bits = type_size_in_bits;
14461 decl_align_in_bits = simple_decl_align_in_bits (decl);
14463 /* The GCC front-end doesn't make any attempt to keep track of the
14464 starting bit offset (relative to the start of the containing
14465 structure type) of the hypothetical "containing object" for a
14466 bit-field. Thus, when computing the byte offset value for the
14467 start of the "containing object" of a bit-field, we must deduce
14468 this information on our own. This can be rather tricky to do in
14469 some cases. For example, handling the following structure type
14470 definition when compiling for an i386/i486 target (which only
14471 aligns long long's to 32-bit boundaries) can be very tricky:
14473 struct S { int field1; long long field2:31; };
14475 Fortunately, there is a simple rule-of-thumb which can be used
14476 in such cases. When compiling for an i386/i486, GCC will
14477 allocate 8 bytes for the structure shown above. It decides to
14478 do this based upon one simple rule for bit-field allocation.
14479 GCC allocates each "containing object" for each bit-field at
14480 the first (i.e. lowest addressed) legitimate alignment boundary
14481 (based upon the required minimum alignment for the declared
14482 type of the field) which it can possibly use, subject to the
14483 condition that there is still enough available space remaining
14484 in the containing object (when allocated at the selected point)
14485 to fully accommodate all of the bits of the bit-field itself.
14487 This simple rule makes it obvious why GCC allocates 8 bytes for
14488 each object of the structure type shown above. When looking
14489 for a place to allocate the "containing object" for `field2',
14490 the compiler simply tries to allocate a 64-bit "containing
14491 object" at each successive 32-bit boundary (starting at zero)
14492 until it finds a place to allocate that 64- bit field such that
14493 at least 31 contiguous (and previously unallocated) bits remain
14494 within that selected 64 bit field. (As it turns out, for the
14495 example above, the compiler finds it is OK to allocate the
14496 "containing object" 64-bit field at bit-offset zero within the
14497 structure type.)
14499 Here we attempt to work backwards from the limited set of facts
14500 we're given, and we try to deduce from those facts, where GCC
14501 must have believed that the containing object started (within
14502 the structure type). The value we deduce is then used (by the
14503 callers of this routine) to generate DW_AT_location and
14504 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14505 the case of DW_AT_location, regular fields as well). */
14507 /* Figure out the bit-distance from the start of the structure to
14508 the "deepest" bit of the bit-field. */
14509 deepest_bitpos = bitpos_int + field_size_in_bits;
14511 /* This is the tricky part. Use some fancy footwork to deduce
14512 where the lowest addressed bit of the containing object must
14513 be. */
14514 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14516 /* Round up to type_align by default. This works best for
14517 bitfields. */
14518 object_offset_in_bits
14519 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14521 if (object_offset_in_bits.ugt (bitpos_int))
14523 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14525 /* Round up to decl_align instead. */
14526 object_offset_in_bits
14527 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14530 else
14531 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14532 object_offset_in_bits = bitpos_int;
14534 object_offset_in_bytes
14535 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14536 true, TRUNC_DIV_EXPR);
14537 return object_offset_in_bytes.to_shwi ();
14540 /* The following routines define various Dwarf attributes and any data
14541 associated with them. */
14543 /* Add a location description attribute value to a DIE.
14545 This emits location attributes suitable for whole variables and
14546 whole parameters. Note that the location attributes for struct fields are
14547 generated by the routine `data_member_location_attribute' below. */
14549 static inline void
14550 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14551 dw_loc_list_ref descr)
14553 if (descr == 0)
14554 return;
14555 if (single_element_loc_list_p (descr))
14556 add_AT_loc (die, attr_kind, descr->expr);
14557 else
14558 add_AT_loc_list (die, attr_kind, descr);
14561 /* Add DW_AT_accessibility attribute to DIE if needed. */
14563 static void
14564 add_accessibility_attribute (dw_die_ref die, tree decl)
14566 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14567 children, otherwise the default is DW_ACCESS_public. In DWARF2
14568 the default has always been DW_ACCESS_public. */
14569 if (TREE_PROTECTED (decl))
14570 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14571 else if (TREE_PRIVATE (decl))
14573 if (dwarf_version == 2
14574 || die->die_parent == NULL
14575 || die->die_parent->die_tag != DW_TAG_class_type)
14576 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14578 else if (dwarf_version > 2
14579 && die->die_parent
14580 && die->die_parent->die_tag == DW_TAG_class_type)
14581 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14584 /* Attach the specialized form of location attribute used for data members of
14585 struct and union types. In the special case of a FIELD_DECL node which
14586 represents a bit-field, the "offset" part of this special location
14587 descriptor must indicate the distance in bytes from the lowest-addressed
14588 byte of the containing struct or union type to the lowest-addressed byte of
14589 the "containing object" for the bit-field. (See the `field_byte_offset'
14590 function above).
14592 For any given bit-field, the "containing object" is a hypothetical object
14593 (of some integral or enum type) within which the given bit-field lives. The
14594 type of this hypothetical "containing object" is always the same as the
14595 declared type of the individual bit-field itself (for GCC anyway... the
14596 DWARF spec doesn't actually mandate this). Note that it is the size (in
14597 bytes) of the hypothetical "containing object" which will be given in the
14598 DW_AT_byte_size attribute for this bit-field. (See the
14599 `byte_size_attribute' function below.) It is also used when calculating the
14600 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14601 function below.) */
14603 static void
14604 add_data_member_location_attribute (dw_die_ref die, tree decl)
14606 HOST_WIDE_INT offset;
14607 dw_loc_descr_ref loc_descr = 0;
14609 if (TREE_CODE (decl) == TREE_BINFO)
14611 /* We're working on the TAG_inheritance for a base class. */
14612 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14614 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14615 aren't at a fixed offset from all (sub)objects of the same
14616 type. We need to extract the appropriate offset from our
14617 vtable. The following dwarf expression means
14619 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14621 This is specific to the V3 ABI, of course. */
14623 dw_loc_descr_ref tmp;
14625 /* Make a copy of the object address. */
14626 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14627 add_loc_descr (&loc_descr, tmp);
14629 /* Extract the vtable address. */
14630 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14631 add_loc_descr (&loc_descr, tmp);
14633 /* Calculate the address of the offset. */
14634 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14635 gcc_assert (offset < 0);
14637 tmp = int_loc_descriptor (-offset);
14638 add_loc_descr (&loc_descr, tmp);
14639 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14640 add_loc_descr (&loc_descr, tmp);
14642 /* Extract the offset. */
14643 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14644 add_loc_descr (&loc_descr, tmp);
14646 /* Add it to the object address. */
14647 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14648 add_loc_descr (&loc_descr, tmp);
14650 else
14651 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14653 else
14654 offset = field_byte_offset (decl);
14656 if (! loc_descr)
14658 if (dwarf_version > 2)
14660 /* Don't need to output a location expression, just the constant. */
14661 if (offset < 0)
14662 add_AT_int (die, DW_AT_data_member_location, offset);
14663 else
14664 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14665 return;
14667 else
14669 enum dwarf_location_atom op;
14671 /* The DWARF2 standard says that we should assume that the structure
14672 address is already on the stack, so we can specify a structure
14673 field address by using DW_OP_plus_uconst. */
14674 op = DW_OP_plus_uconst;
14675 loc_descr = new_loc_descr (op, offset, 0);
14679 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14682 /* Writes integer values to dw_vec_const array. */
14684 static void
14685 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14687 while (size != 0)
14689 *dest++ = val & 0xff;
14690 val >>= 8;
14691 --size;
14695 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14697 static HOST_WIDE_INT
14698 extract_int (const unsigned char *src, unsigned int size)
14700 HOST_WIDE_INT val = 0;
14702 src += size;
14703 while (size != 0)
14705 val <<= 8;
14706 val |= *--src & 0xff;
14707 --size;
14709 return val;
14712 /* Writes double_int values to dw_vec_const array. */
14714 static void
14715 insert_double (double_int val, unsigned char *dest)
14717 unsigned char *p0 = dest;
14718 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14720 if (WORDS_BIG_ENDIAN)
14722 p0 = p1;
14723 p1 = dest;
14726 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14727 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14730 /* Writes floating point values to dw_vec_const array. */
14732 static void
14733 insert_float (const_rtx rtl, unsigned char *array)
14735 REAL_VALUE_TYPE rv;
14736 long val[4];
14737 int i;
14739 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14740 real_to_target (val, &rv, GET_MODE (rtl));
14742 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14743 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14745 insert_int (val[i], 4, array);
14746 array += 4;
14750 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14751 does not have a "location" either in memory or in a register. These
14752 things can arise in GNU C when a constant is passed as an actual parameter
14753 to an inlined function. They can also arise in C++ where declared
14754 constants do not necessarily get memory "homes". */
14756 static bool
14757 add_const_value_attribute (dw_die_ref die, rtx rtl)
14759 switch (GET_CODE (rtl))
14761 case CONST_INT:
14763 HOST_WIDE_INT val = INTVAL (rtl);
14765 if (val < 0)
14766 add_AT_int (die, DW_AT_const_value, val);
14767 else
14768 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14770 return true;
14772 case CONST_DOUBLE:
14773 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14774 floating-point constant. A CONST_DOUBLE is used whenever the
14775 constant requires more than one word in order to be adequately
14776 represented. */
14778 enum machine_mode mode = GET_MODE (rtl);
14780 if (SCALAR_FLOAT_MODE_P (mode))
14782 unsigned int length = GET_MODE_SIZE (mode);
14783 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14785 insert_float (rtl, array);
14786 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14788 else
14789 add_AT_double (die, DW_AT_const_value,
14790 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14792 return true;
14794 case CONST_VECTOR:
14796 enum machine_mode mode = GET_MODE (rtl);
14797 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14798 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14799 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14800 (length * elt_size);
14801 unsigned int i;
14802 unsigned char *p;
14804 switch (GET_MODE_CLASS (mode))
14806 case MODE_VECTOR_INT:
14807 for (i = 0, p = array; i < length; i++, p += elt_size)
14809 rtx elt = CONST_VECTOR_ELT (rtl, i);
14810 double_int val = rtx_to_double_int (elt);
14812 if (elt_size <= sizeof (HOST_WIDE_INT))
14813 insert_int (val.to_shwi (), elt_size, p);
14814 else
14816 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14817 insert_double (val, p);
14820 break;
14822 case MODE_VECTOR_FLOAT:
14823 for (i = 0, p = array; i < length; i++, p += elt_size)
14825 rtx elt = CONST_VECTOR_ELT (rtl, i);
14826 insert_float (elt, p);
14828 break;
14830 default:
14831 gcc_unreachable ();
14834 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14836 return true;
14838 case CONST_STRING:
14839 if (dwarf_version >= 4 || !dwarf_strict)
14841 dw_loc_descr_ref loc_result;
14842 resolve_one_addr (&rtl, NULL);
14843 rtl_addr:
14844 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14845 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14846 add_AT_loc (die, DW_AT_location, loc_result);
14847 vec_safe_push (used_rtx_array, rtl);
14848 return true;
14850 return false;
14852 case CONST:
14853 if (CONSTANT_P (XEXP (rtl, 0)))
14854 return add_const_value_attribute (die, XEXP (rtl, 0));
14855 /* FALLTHROUGH */
14856 case SYMBOL_REF:
14857 if (!const_ok_for_output (rtl))
14858 return false;
14859 case LABEL_REF:
14860 if (dwarf_version >= 4 || !dwarf_strict)
14861 goto rtl_addr;
14862 return false;
14864 case PLUS:
14865 /* In cases where an inlined instance of an inline function is passed
14866 the address of an `auto' variable (which is local to the caller) we
14867 can get a situation where the DECL_RTL of the artificial local
14868 variable (for the inlining) which acts as a stand-in for the
14869 corresponding formal parameter (of the inline function) will look
14870 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14871 exactly a compile-time constant expression, but it isn't the address
14872 of the (artificial) local variable either. Rather, it represents the
14873 *value* which the artificial local variable always has during its
14874 lifetime. We currently have no way to represent such quasi-constant
14875 values in Dwarf, so for now we just punt and generate nothing. */
14876 return false;
14878 case HIGH:
14879 case CONST_FIXED:
14880 return false;
14882 case MEM:
14883 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14884 && MEM_READONLY_P (rtl)
14885 && GET_MODE (rtl) == BLKmode)
14887 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14888 return true;
14890 return false;
14892 default:
14893 /* No other kinds of rtx should be possible here. */
14894 gcc_unreachable ();
14896 return false;
14899 /* Determine whether the evaluation of EXPR references any variables
14900 or functions which aren't otherwise used (and therefore may not be
14901 output). */
14902 static tree
14903 reference_to_unused (tree * tp, int * walk_subtrees,
14904 void * data ATTRIBUTE_UNUSED)
14906 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14907 *walk_subtrees = 0;
14909 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14910 && ! TREE_ASM_WRITTEN (*tp))
14911 return *tp;
14912 /* ??? The C++ FE emits debug information for using decls, so
14913 putting gcc_unreachable here falls over. See PR31899. For now
14914 be conservative. */
14915 else if (!cgraph_global_info_ready
14916 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14917 return *tp;
14918 else if (TREE_CODE (*tp) == VAR_DECL)
14920 struct varpool_node *node = varpool_get_node (*tp);
14921 if (!node || !node->analyzed)
14922 return *tp;
14924 else if (TREE_CODE (*tp) == FUNCTION_DECL
14925 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14927 /* The call graph machinery must have finished analyzing,
14928 optimizing and gimplifying the CU by now.
14929 So if *TP has no call graph node associated
14930 to it, it means *TP will not be emitted. */
14931 if (!cgraph_get_node (*tp))
14932 return *tp;
14934 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14935 return *tp;
14937 return NULL_TREE;
14940 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14941 for use in a later add_const_value_attribute call. */
14943 static rtx
14944 rtl_for_decl_init (tree init, tree type)
14946 rtx rtl = NULL_RTX;
14948 STRIP_NOPS (init);
14950 /* If a variable is initialized with a string constant without embedded
14951 zeros, build CONST_STRING. */
14952 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14954 tree enttype = TREE_TYPE (type);
14955 tree domain = TYPE_DOMAIN (type);
14956 enum machine_mode mode = TYPE_MODE (enttype);
14958 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14959 && domain
14960 && integer_zerop (TYPE_MIN_VALUE (domain))
14961 && compare_tree_int (TYPE_MAX_VALUE (domain),
14962 TREE_STRING_LENGTH (init) - 1) == 0
14963 && ((size_t) TREE_STRING_LENGTH (init)
14964 == strlen (TREE_STRING_POINTER (init)) + 1))
14966 rtl = gen_rtx_CONST_STRING (VOIDmode,
14967 ggc_strdup (TREE_STRING_POINTER (init)));
14968 rtl = gen_rtx_MEM (BLKmode, rtl);
14969 MEM_READONLY_P (rtl) = 1;
14972 /* Other aggregates, and complex values, could be represented using
14973 CONCAT: FIXME! */
14974 else if (AGGREGATE_TYPE_P (type)
14975 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14976 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14977 || TREE_CODE (type) == COMPLEX_TYPE)
14979 /* Vectors only work if their mode is supported by the target.
14980 FIXME: generic vectors ought to work too. */
14981 else if (TREE_CODE (type) == VECTOR_TYPE
14982 && !VECTOR_MODE_P (TYPE_MODE (type)))
14984 /* If the initializer is something that we know will expand into an
14985 immediate RTL constant, expand it now. We must be careful not to
14986 reference variables which won't be output. */
14987 else if (initializer_constant_valid_p (init, type)
14988 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14990 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14991 possible. */
14992 if (TREE_CODE (type) == VECTOR_TYPE)
14993 switch (TREE_CODE (init))
14995 case VECTOR_CST:
14996 break;
14997 case CONSTRUCTOR:
14998 if (TREE_CONSTANT (init))
15000 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15001 bool constant_p = true;
15002 tree value;
15003 unsigned HOST_WIDE_INT ix;
15005 /* Even when ctor is constant, it might contain non-*_CST
15006 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15007 belong into VECTOR_CST nodes. */
15008 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15009 if (!CONSTANT_CLASS_P (value))
15011 constant_p = false;
15012 break;
15015 if (constant_p)
15017 init = build_vector_from_ctor (type, elts);
15018 break;
15021 /* FALLTHRU */
15023 default:
15024 return NULL;
15027 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15029 /* If expand_expr returns a MEM, it wasn't immediate. */
15030 gcc_assert (!rtl || !MEM_P (rtl));
15033 return rtl;
15036 /* Generate RTL for the variable DECL to represent its location. */
15038 static rtx
15039 rtl_for_decl_location (tree decl)
15041 rtx rtl;
15043 /* Here we have to decide where we are going to say the parameter "lives"
15044 (as far as the debugger is concerned). We only have a couple of
15045 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15047 DECL_RTL normally indicates where the parameter lives during most of the
15048 activation of the function. If optimization is enabled however, this
15049 could be either NULL or else a pseudo-reg. Both of those cases indicate
15050 that the parameter doesn't really live anywhere (as far as the code
15051 generation parts of GCC are concerned) during most of the function's
15052 activation. That will happen (for example) if the parameter is never
15053 referenced within the function.
15055 We could just generate a location descriptor here for all non-NULL
15056 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15057 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15058 where DECL_RTL is NULL or is a pseudo-reg.
15060 Note however that we can only get away with using DECL_INCOMING_RTL as
15061 a backup substitute for DECL_RTL in certain limited cases. In cases
15062 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15063 we can be sure that the parameter was passed using the same type as it is
15064 declared to have within the function, and that its DECL_INCOMING_RTL
15065 points us to a place where a value of that type is passed.
15067 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15068 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15069 because in these cases DECL_INCOMING_RTL points us to a value of some
15070 type which is *different* from the type of the parameter itself. Thus,
15071 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15072 such cases, the debugger would end up (for example) trying to fetch a
15073 `float' from a place which actually contains the first part of a
15074 `double'. That would lead to really incorrect and confusing
15075 output at debug-time.
15077 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15078 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15079 are a couple of exceptions however. On little-endian machines we can
15080 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15081 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15082 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15083 when (on a little-endian machine) a non-prototyped function has a
15084 parameter declared to be of type `short' or `char'. In such cases,
15085 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15086 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15087 passed `int' value. If the debugger then uses that address to fetch
15088 a `short' or a `char' (on a little-endian machine) the result will be
15089 the correct data, so we allow for such exceptional cases below.
15091 Note that our goal here is to describe the place where the given formal
15092 parameter lives during most of the function's activation (i.e. between the
15093 end of the prologue and the start of the epilogue). We'll do that as best
15094 as we can. Note however that if the given formal parameter is modified
15095 sometime during the execution of the function, then a stack backtrace (at
15096 debug-time) will show the function as having been called with the *new*
15097 value rather than the value which was originally passed in. This happens
15098 rarely enough that it is not a major problem, but it *is* a problem, and
15099 I'd like to fix it.
15101 A future version of dwarf2out.c may generate two additional attributes for
15102 any given DW_TAG_formal_parameter DIE which will describe the "passed
15103 type" and the "passed location" for the given formal parameter in addition
15104 to the attributes we now generate to indicate the "declared type" and the
15105 "active location" for each parameter. This additional set of attributes
15106 could be used by debuggers for stack backtraces. Separately, note that
15107 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15108 This happens (for example) for inlined-instances of inline function formal
15109 parameters which are never referenced. This really shouldn't be
15110 happening. All PARM_DECL nodes should get valid non-NULL
15111 DECL_INCOMING_RTL values. FIXME. */
15113 /* Use DECL_RTL as the "location" unless we find something better. */
15114 rtl = DECL_RTL_IF_SET (decl);
15116 /* When generating abstract instances, ignore everything except
15117 constants, symbols living in memory, and symbols living in
15118 fixed registers. */
15119 if (! reload_completed)
15121 if (rtl
15122 && (CONSTANT_P (rtl)
15123 || (MEM_P (rtl)
15124 && CONSTANT_P (XEXP (rtl, 0)))
15125 || (REG_P (rtl)
15126 && TREE_CODE (decl) == VAR_DECL
15127 && TREE_STATIC (decl))))
15129 rtl = targetm.delegitimize_address (rtl);
15130 return rtl;
15132 rtl = NULL_RTX;
15134 else if (TREE_CODE (decl) == PARM_DECL)
15136 if (rtl == NULL_RTX
15137 || is_pseudo_reg (rtl)
15138 || (MEM_P (rtl)
15139 && is_pseudo_reg (XEXP (rtl, 0))
15140 && DECL_INCOMING_RTL (decl)
15141 && MEM_P (DECL_INCOMING_RTL (decl))
15142 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15144 tree declared_type = TREE_TYPE (decl);
15145 tree passed_type = DECL_ARG_TYPE (decl);
15146 enum machine_mode dmode = TYPE_MODE (declared_type);
15147 enum machine_mode pmode = TYPE_MODE (passed_type);
15149 /* This decl represents a formal parameter which was optimized out.
15150 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15151 all cases where (rtl == NULL_RTX) just below. */
15152 if (dmode == pmode)
15153 rtl = DECL_INCOMING_RTL (decl);
15154 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15155 && SCALAR_INT_MODE_P (dmode)
15156 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15157 && DECL_INCOMING_RTL (decl))
15159 rtx inc = DECL_INCOMING_RTL (decl);
15160 if (REG_P (inc))
15161 rtl = inc;
15162 else if (MEM_P (inc))
15164 if (BYTES_BIG_ENDIAN)
15165 rtl = adjust_address_nv (inc, dmode,
15166 GET_MODE_SIZE (pmode)
15167 - GET_MODE_SIZE (dmode));
15168 else
15169 rtl = inc;
15174 /* If the parm was passed in registers, but lives on the stack, then
15175 make a big endian correction if the mode of the type of the
15176 parameter is not the same as the mode of the rtl. */
15177 /* ??? This is the same series of checks that are made in dbxout.c before
15178 we reach the big endian correction code there. It isn't clear if all
15179 of these checks are necessary here, but keeping them all is the safe
15180 thing to do. */
15181 else if (MEM_P (rtl)
15182 && XEXP (rtl, 0) != const0_rtx
15183 && ! CONSTANT_P (XEXP (rtl, 0))
15184 /* Not passed in memory. */
15185 && !MEM_P (DECL_INCOMING_RTL (decl))
15186 /* Not passed by invisible reference. */
15187 && (!REG_P (XEXP (rtl, 0))
15188 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15189 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15190 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15191 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15192 #endif
15194 /* Big endian correction check. */
15195 && BYTES_BIG_ENDIAN
15196 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15197 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15198 < UNITS_PER_WORD))
15200 enum machine_mode addr_mode = get_address_mode (rtl);
15201 int offset = (UNITS_PER_WORD
15202 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15204 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15205 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15208 else if (TREE_CODE (decl) == VAR_DECL
15209 && rtl
15210 && MEM_P (rtl)
15211 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15212 && BYTES_BIG_ENDIAN)
15214 enum machine_mode addr_mode = get_address_mode (rtl);
15215 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15216 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15218 /* If a variable is declared "register" yet is smaller than
15219 a register, then if we store the variable to memory, it
15220 looks like we're storing a register-sized value, when in
15221 fact we are not. We need to adjust the offset of the
15222 storage location to reflect the actual value's bytes,
15223 else gdb will not be able to display it. */
15224 if (rsize > dsize)
15225 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15226 plus_constant (addr_mode, XEXP (rtl, 0),
15227 rsize - dsize));
15230 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15231 and will have been substituted directly into all expressions that use it.
15232 C does not have such a concept, but C++ and other languages do. */
15233 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15234 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15236 if (rtl)
15237 rtl = targetm.delegitimize_address (rtl);
15239 /* If we don't look past the constant pool, we risk emitting a
15240 reference to a constant pool entry that isn't referenced from
15241 code, and thus is not emitted. */
15242 if (rtl)
15243 rtl = avoid_constant_pool_reference (rtl);
15245 /* Try harder to get a rtl. If this symbol ends up not being emitted
15246 in the current CU, resolve_addr will remove the expression referencing
15247 it. */
15248 if (rtl == NULL_RTX
15249 && TREE_CODE (decl) == VAR_DECL
15250 && !DECL_EXTERNAL (decl)
15251 && TREE_STATIC (decl)
15252 && DECL_NAME (decl)
15253 && !DECL_HARD_REGISTER (decl)
15254 && DECL_MODE (decl) != VOIDmode)
15256 rtl = make_decl_rtl_for_debug (decl);
15257 if (!MEM_P (rtl)
15258 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15259 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15260 rtl = NULL_RTX;
15263 return rtl;
15266 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15267 returned. If so, the decl for the COMMON block is returned, and the
15268 value is the offset into the common block for the symbol. */
15270 static tree
15271 fortran_common (tree decl, HOST_WIDE_INT *value)
15273 tree val_expr, cvar;
15274 enum machine_mode mode;
15275 HOST_WIDE_INT bitsize, bitpos;
15276 tree offset;
15277 int unsignedp, volatilep = 0;
15279 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15280 it does not have a value (the offset into the common area), or if it
15281 is thread local (as opposed to global) then it isn't common, and shouldn't
15282 be handled as such. */
15283 if (TREE_CODE (decl) != VAR_DECL
15284 || !TREE_STATIC (decl)
15285 || !DECL_HAS_VALUE_EXPR_P (decl)
15286 || !is_fortran ())
15287 return NULL_TREE;
15289 val_expr = DECL_VALUE_EXPR (decl);
15290 if (TREE_CODE (val_expr) != COMPONENT_REF)
15291 return NULL_TREE;
15293 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15294 &mode, &unsignedp, &volatilep, true);
15296 if (cvar == NULL_TREE
15297 || TREE_CODE (cvar) != VAR_DECL
15298 || DECL_ARTIFICIAL (cvar)
15299 || !TREE_PUBLIC (cvar))
15300 return NULL_TREE;
15302 *value = 0;
15303 if (offset != NULL)
15305 if (!host_integerp (offset, 0))
15306 return NULL_TREE;
15307 *value = tree_low_cst (offset, 0);
15309 if (bitpos != 0)
15310 *value += bitpos / BITS_PER_UNIT;
15312 return cvar;
15315 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15316 data attribute for a variable or a parameter. We generate the
15317 DW_AT_const_value attribute only in those cases where the given variable
15318 or parameter does not have a true "location" either in memory or in a
15319 register. This can happen (for example) when a constant is passed as an
15320 actual argument in a call to an inline function. (It's possible that
15321 these things can crop up in other ways also.) Note that one type of
15322 constant value which can be passed into an inlined function is a constant
15323 pointer. This can happen for example if an actual argument in an inlined
15324 function call evaluates to a compile-time constant address.
15326 CACHE_P is true if it is worth caching the location list for DECL,
15327 so that future calls can reuse it rather than regenerate it from scratch.
15328 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15329 since we will need to refer to them each time the function is inlined. */
15331 static bool
15332 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15333 enum dwarf_attribute attr)
15335 rtx rtl;
15336 dw_loc_list_ref list;
15337 var_loc_list *loc_list;
15338 cached_dw_loc_list *cache;
15339 void **slot;
15341 if (TREE_CODE (decl) == ERROR_MARK)
15342 return false;
15344 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15345 || TREE_CODE (decl) == RESULT_DECL);
15347 /* Try to get some constant RTL for this decl, and use that as the value of
15348 the location. */
15350 rtl = rtl_for_decl_location (decl);
15351 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15352 && add_const_value_attribute (die, rtl))
15353 return true;
15355 /* See if we have single element location list that is equivalent to
15356 a constant value. That way we are better to use add_const_value_attribute
15357 rather than expanding constant value equivalent. */
15358 loc_list = lookup_decl_loc (decl);
15359 if (loc_list
15360 && loc_list->first
15361 && loc_list->first->next == NULL
15362 && NOTE_P (loc_list->first->loc)
15363 && NOTE_VAR_LOCATION (loc_list->first->loc)
15364 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15366 struct var_loc_node *node;
15368 node = loc_list->first;
15369 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15370 if (GET_CODE (rtl) == EXPR_LIST)
15371 rtl = XEXP (rtl, 0);
15372 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15373 && add_const_value_attribute (die, rtl))
15374 return true;
15376 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15377 list several times. See if we've already cached the contents. */
15378 list = NULL;
15379 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15380 cache_p = false;
15381 if (cache_p)
15383 cache = (cached_dw_loc_list *)
15384 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15385 if (cache)
15386 list = cache->loc_list;
15388 if (list == NULL)
15390 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15391 /* It is usually worth caching this result if the decl is from
15392 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15393 if (cache_p && list && list->dw_loc_next)
15395 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15396 DECL_UID (decl), INSERT);
15397 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15398 cache->decl_id = DECL_UID (decl);
15399 cache->loc_list = list;
15400 *slot = cache;
15403 if (list)
15405 add_AT_location_description (die, attr, list);
15406 return true;
15408 /* None of that worked, so it must not really have a location;
15409 try adding a constant value attribute from the DECL_INITIAL. */
15410 return tree_add_const_value_attribute_for_decl (die, decl);
15413 /* Add VARIABLE and DIE into deferred locations list. */
15415 static void
15416 defer_location (tree variable, dw_die_ref die)
15418 deferred_locations entry;
15419 entry.variable = variable;
15420 entry.die = die;
15421 vec_safe_push (deferred_locations_list, entry);
15424 /* Helper function for tree_add_const_value_attribute. Natively encode
15425 initializer INIT into an array. Return true if successful. */
15427 static bool
15428 native_encode_initializer (tree init, unsigned char *array, int size)
15430 tree type;
15432 if (init == NULL_TREE)
15433 return false;
15435 STRIP_NOPS (init);
15436 switch (TREE_CODE (init))
15438 case STRING_CST:
15439 type = TREE_TYPE (init);
15440 if (TREE_CODE (type) == ARRAY_TYPE)
15442 tree enttype = TREE_TYPE (type);
15443 enum machine_mode mode = TYPE_MODE (enttype);
15445 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15446 return false;
15447 if (int_size_in_bytes (type) != size)
15448 return false;
15449 if (size > TREE_STRING_LENGTH (init))
15451 memcpy (array, TREE_STRING_POINTER (init),
15452 TREE_STRING_LENGTH (init));
15453 memset (array + TREE_STRING_LENGTH (init),
15454 '\0', size - TREE_STRING_LENGTH (init));
15456 else
15457 memcpy (array, TREE_STRING_POINTER (init), size);
15458 return true;
15460 return false;
15461 case CONSTRUCTOR:
15462 type = TREE_TYPE (init);
15463 if (int_size_in_bytes (type) != size)
15464 return false;
15465 if (TREE_CODE (type) == ARRAY_TYPE)
15467 HOST_WIDE_INT min_index;
15468 unsigned HOST_WIDE_INT cnt;
15469 int curpos = 0, fieldsize;
15470 constructor_elt *ce;
15472 if (TYPE_DOMAIN (type) == NULL_TREE
15473 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15474 return false;
15476 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15477 if (fieldsize <= 0)
15478 return false;
15480 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15481 memset (array, '\0', size);
15482 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15484 tree val = ce->value;
15485 tree index = ce->index;
15486 int pos = curpos;
15487 if (index && TREE_CODE (index) == RANGE_EXPR)
15488 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15489 * fieldsize;
15490 else if (index)
15491 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15493 if (val)
15495 STRIP_NOPS (val);
15496 if (!native_encode_initializer (val, array + pos, fieldsize))
15497 return false;
15499 curpos = pos + fieldsize;
15500 if (index && TREE_CODE (index) == RANGE_EXPR)
15502 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15503 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15504 while (count-- > 0)
15506 if (val)
15507 memcpy (array + curpos, array + pos, fieldsize);
15508 curpos += fieldsize;
15511 gcc_assert (curpos <= size);
15513 return true;
15515 else if (TREE_CODE (type) == RECORD_TYPE
15516 || TREE_CODE (type) == UNION_TYPE)
15518 tree field = NULL_TREE;
15519 unsigned HOST_WIDE_INT cnt;
15520 constructor_elt *ce;
15522 if (int_size_in_bytes (type) != size)
15523 return false;
15525 if (TREE_CODE (type) == RECORD_TYPE)
15526 field = TYPE_FIELDS (type);
15528 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15530 tree val = ce->value;
15531 int pos, fieldsize;
15533 if (ce->index != 0)
15534 field = ce->index;
15536 if (val)
15537 STRIP_NOPS (val);
15539 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15540 return false;
15542 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15543 && TYPE_DOMAIN (TREE_TYPE (field))
15544 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15545 return false;
15546 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15547 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15548 return false;
15549 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15550 pos = int_byte_position (field);
15551 gcc_assert (pos + fieldsize <= size);
15552 if (val
15553 && !native_encode_initializer (val, array + pos, fieldsize))
15554 return false;
15556 return true;
15558 return false;
15559 case VIEW_CONVERT_EXPR:
15560 case NON_LVALUE_EXPR:
15561 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15562 default:
15563 return native_encode_expr (init, array, size) == size;
15567 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15568 attribute is the const value T. */
15570 static bool
15571 tree_add_const_value_attribute (dw_die_ref die, tree t)
15573 tree init;
15574 tree type = TREE_TYPE (t);
15575 rtx rtl;
15577 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15578 return false;
15580 init = t;
15581 gcc_assert (!DECL_P (init));
15583 rtl = rtl_for_decl_init (init, type);
15584 if (rtl)
15585 return add_const_value_attribute (die, rtl);
15586 /* If the host and target are sane, try harder. */
15587 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15588 && initializer_constant_valid_p (init, type))
15590 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15591 if (size > 0 && (int) size == size)
15593 unsigned char *array = (unsigned char *)
15594 ggc_alloc_cleared_atomic (size);
15596 if (native_encode_initializer (init, array, size))
15598 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15599 return true;
15601 ggc_free (array);
15604 return false;
15607 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15608 attribute is the const value of T, where T is an integral constant
15609 variable with static storage duration
15610 (so it can't be a PARM_DECL or a RESULT_DECL). */
15612 static bool
15613 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15616 if (!decl
15617 || (TREE_CODE (decl) != VAR_DECL
15618 && TREE_CODE (decl) != CONST_DECL)
15619 || (TREE_CODE (decl) == VAR_DECL
15620 && !TREE_STATIC (decl)))
15621 return false;
15623 if (TREE_READONLY (decl)
15624 && ! TREE_THIS_VOLATILE (decl)
15625 && DECL_INITIAL (decl))
15626 /* OK */;
15627 else
15628 return false;
15630 /* Don't add DW_AT_const_value if abstract origin already has one. */
15631 if (get_AT (var_die, DW_AT_const_value))
15632 return false;
15634 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15637 /* Convert the CFI instructions for the current function into a
15638 location list. This is used for DW_AT_frame_base when we targeting
15639 a dwarf2 consumer that does not support the dwarf3
15640 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15641 expressions. */
15643 static dw_loc_list_ref
15644 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15646 int ix;
15647 dw_fde_ref fde;
15648 dw_loc_list_ref list, *list_tail;
15649 dw_cfi_ref cfi;
15650 dw_cfa_location last_cfa, next_cfa;
15651 const char *start_label, *last_label, *section;
15652 dw_cfa_location remember;
15654 fde = cfun->fde;
15655 gcc_assert (fde != NULL);
15657 section = secname_for_decl (current_function_decl);
15658 list_tail = &list;
15659 list = NULL;
15661 memset (&next_cfa, 0, sizeof (next_cfa));
15662 next_cfa.reg = INVALID_REGNUM;
15663 remember = next_cfa;
15665 start_label = fde->dw_fde_begin;
15667 /* ??? Bald assumption that the CIE opcode list does not contain
15668 advance opcodes. */
15669 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15670 lookup_cfa_1 (cfi, &next_cfa, &remember);
15672 last_cfa = next_cfa;
15673 last_label = start_label;
15675 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15677 /* If the first partition contained no CFI adjustments, the
15678 CIE opcodes apply to the whole first partition. */
15679 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15680 fde->dw_fde_begin, fde->dw_fde_end, section);
15681 list_tail =&(*list_tail)->dw_loc_next;
15682 start_label = last_label = fde->dw_fde_second_begin;
15685 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15687 switch (cfi->dw_cfi_opc)
15689 case DW_CFA_set_loc:
15690 case DW_CFA_advance_loc1:
15691 case DW_CFA_advance_loc2:
15692 case DW_CFA_advance_loc4:
15693 if (!cfa_equal_p (&last_cfa, &next_cfa))
15695 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15696 start_label, last_label, section);
15698 list_tail = &(*list_tail)->dw_loc_next;
15699 last_cfa = next_cfa;
15700 start_label = last_label;
15702 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15703 break;
15705 case DW_CFA_advance_loc:
15706 /* The encoding is complex enough that we should never emit this. */
15707 gcc_unreachable ();
15709 default:
15710 lookup_cfa_1 (cfi, &next_cfa, &remember);
15711 break;
15713 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15715 if (!cfa_equal_p (&last_cfa, &next_cfa))
15717 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15718 start_label, last_label, section);
15720 list_tail = &(*list_tail)->dw_loc_next;
15721 last_cfa = next_cfa;
15722 start_label = last_label;
15724 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15725 start_label, fde->dw_fde_end, section);
15726 list_tail = &(*list_tail)->dw_loc_next;
15727 start_label = last_label = fde->dw_fde_second_begin;
15731 if (!cfa_equal_p (&last_cfa, &next_cfa))
15733 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15734 start_label, last_label, section);
15735 list_tail = &(*list_tail)->dw_loc_next;
15736 start_label = last_label;
15739 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15740 start_label,
15741 fde->dw_fde_second_begin
15742 ? fde->dw_fde_second_end : fde->dw_fde_end,
15743 section);
15745 if (list && list->dw_loc_next)
15746 gen_llsym (list);
15748 return list;
15751 /* Compute a displacement from the "steady-state frame pointer" to the
15752 frame base (often the same as the CFA), and store it in
15753 frame_pointer_fb_offset. OFFSET is added to the displacement
15754 before the latter is negated. */
15756 static void
15757 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15759 rtx reg, elim;
15761 #ifdef FRAME_POINTER_CFA_OFFSET
15762 reg = frame_pointer_rtx;
15763 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15764 #else
15765 reg = arg_pointer_rtx;
15766 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15767 #endif
15769 elim = (ira_use_lra_p
15770 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15771 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15772 if (GET_CODE (elim) == PLUS)
15774 offset += INTVAL (XEXP (elim, 1));
15775 elim = XEXP (elim, 0);
15778 frame_pointer_fb_offset = -offset;
15780 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15781 in which to eliminate. This is because it's stack pointer isn't
15782 directly accessible as a register within the ISA. To work around
15783 this, assume that while we cannot provide a proper value for
15784 frame_pointer_fb_offset, we won't need one either. */
15785 frame_pointer_fb_offset_valid
15786 = ((SUPPORTS_STACK_ALIGNMENT
15787 && (elim == hard_frame_pointer_rtx
15788 || elim == stack_pointer_rtx))
15789 || elim == (frame_pointer_needed
15790 ? hard_frame_pointer_rtx
15791 : stack_pointer_rtx));
15794 /* Generate a DW_AT_name attribute given some string value to be included as
15795 the value of the attribute. */
15797 static void
15798 add_name_attribute (dw_die_ref die, const char *name_string)
15800 if (name_string != NULL && *name_string != 0)
15802 if (demangle_name_func)
15803 name_string = (*demangle_name_func) (name_string);
15805 add_AT_string (die, DW_AT_name, name_string);
15809 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15810 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15811 of TYPE accordingly.
15813 ??? This is a temporary measure until after we're able to generate
15814 regular DWARF for the complex Ada type system. */
15816 static void
15817 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15818 dw_die_ref context_die)
15820 tree dtype;
15821 dw_die_ref dtype_die;
15823 if (!lang_hooks.types.descriptive_type)
15824 return;
15826 dtype = lang_hooks.types.descriptive_type (type);
15827 if (!dtype)
15828 return;
15830 dtype_die = lookup_type_die (dtype);
15831 if (!dtype_die)
15833 gen_type_die (dtype, context_die);
15834 dtype_die = lookup_type_die (dtype);
15835 gcc_assert (dtype_die);
15838 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15841 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
15843 static const char *
15844 comp_dir_string (void)
15846 const char *wd;
15847 char *wd1;
15848 static const char *cached_wd = NULL;
15850 if (cached_wd != NULL)
15851 return cached_wd;
15853 wd = get_src_pwd ();
15854 if (wd == NULL)
15855 return NULL;
15857 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15859 int wdlen;
15861 wdlen = strlen (wd);
15862 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15863 strcpy (wd1, wd);
15864 wd1 [wdlen] = DIR_SEPARATOR;
15865 wd1 [wdlen + 1] = 0;
15866 wd = wd1;
15869 cached_wd = remap_debug_filename (wd);
15870 return cached_wd;
15873 /* Generate a DW_AT_comp_dir attribute for DIE. */
15875 static void
15876 add_comp_dir_attribute (dw_die_ref die)
15878 const char * wd = comp_dir_string ();
15879 if (wd != NULL)
15880 add_AT_string (die, DW_AT_comp_dir, wd);
15883 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15884 default. */
15886 static int
15887 lower_bound_default (void)
15889 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15891 case DW_LANG_C:
15892 case DW_LANG_C89:
15893 case DW_LANG_C99:
15894 case DW_LANG_C_plus_plus:
15895 case DW_LANG_ObjC:
15896 case DW_LANG_ObjC_plus_plus:
15897 case DW_LANG_Java:
15898 return 0;
15899 case DW_LANG_Fortran77:
15900 case DW_LANG_Fortran90:
15901 case DW_LANG_Fortran95:
15902 return 1;
15903 case DW_LANG_UPC:
15904 case DW_LANG_D:
15905 case DW_LANG_Python:
15906 return dwarf_version >= 4 ? 0 : -1;
15907 case DW_LANG_Ada95:
15908 case DW_LANG_Ada83:
15909 case DW_LANG_Cobol74:
15910 case DW_LANG_Cobol85:
15911 case DW_LANG_Pascal83:
15912 case DW_LANG_Modula2:
15913 case DW_LANG_PLI:
15914 return dwarf_version >= 4 ? 1 : -1;
15915 default:
15916 return -1;
15920 /* Given a tree node describing an array bound (either lower or upper) output
15921 a representation for that bound. */
15923 static void
15924 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15926 switch (TREE_CODE (bound))
15928 case ERROR_MARK:
15929 return;
15931 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15932 case INTEGER_CST:
15934 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15935 int dflt;
15937 /* Use the default if possible. */
15938 if (bound_attr == DW_AT_lower_bound
15939 && host_integerp (bound, 0)
15940 && (dflt = lower_bound_default ()) != -1
15941 && tree_low_cst (bound, 0) == dflt)
15944 /* Otherwise represent the bound as an unsigned value with the
15945 precision of its type. The precision and signedness of the
15946 type will be necessary to re-interpret it unambiguously. */
15947 else if (prec < HOST_BITS_PER_WIDE_INT)
15949 unsigned HOST_WIDE_INT mask
15950 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15951 add_AT_unsigned (subrange_die, bound_attr,
15952 TREE_INT_CST_LOW (bound) & mask);
15954 else if (prec == HOST_BITS_PER_WIDE_INT
15955 || TREE_INT_CST_HIGH (bound) == 0)
15956 add_AT_unsigned (subrange_die, bound_attr,
15957 TREE_INT_CST_LOW (bound));
15958 else
15959 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15960 TREE_INT_CST_LOW (bound));
15962 break;
15964 CASE_CONVERT:
15965 case VIEW_CONVERT_EXPR:
15966 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15967 break;
15969 case SAVE_EXPR:
15970 break;
15972 case VAR_DECL:
15973 case PARM_DECL:
15974 case RESULT_DECL:
15976 dw_die_ref decl_die = lookup_decl_die (bound);
15978 /* ??? Can this happen, or should the variable have been bound
15979 first? Probably it can, since I imagine that we try to create
15980 the types of parameters in the order in which they exist in
15981 the list, and won't have created a forward reference to a
15982 later parameter. */
15983 if (decl_die != NULL)
15985 add_AT_die_ref (subrange_die, bound_attr, decl_die);
15986 break;
15989 /* FALLTHRU */
15991 default:
15993 /* Otherwise try to create a stack operation procedure to
15994 evaluate the value of the array bound. */
15996 dw_die_ref ctx, decl_die;
15997 dw_loc_list_ref list;
15999 list = loc_list_from_tree (bound, 2);
16000 if (list == NULL || single_element_loc_list_p (list))
16002 /* If DW_AT_*bound is not a reference nor constant, it is
16003 a DWARF expression rather than location description.
16004 For that loc_list_from_tree (bound, 0) is needed.
16005 If that fails to give a single element list,
16006 fall back to outputting this as a reference anyway. */
16007 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16008 if (list2 && single_element_loc_list_p (list2))
16010 add_AT_loc (subrange_die, bound_attr, list2->expr);
16011 break;
16014 if (list == NULL)
16015 break;
16017 if (current_function_decl == 0)
16018 ctx = comp_unit_die ();
16019 else
16020 ctx = lookup_decl_die (current_function_decl);
16022 decl_die = new_die (DW_TAG_variable, ctx, bound);
16023 add_AT_flag (decl_die, DW_AT_artificial, 1);
16024 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16025 add_AT_location_description (decl_die, DW_AT_location, list);
16026 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16027 break;
16032 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16033 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16034 Note that the block of subscript information for an array type also
16035 includes information about the element type of the given array type. */
16037 static void
16038 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16040 unsigned dimension_number;
16041 tree lower, upper;
16042 dw_die_ref subrange_die;
16044 for (dimension_number = 0;
16045 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16046 type = TREE_TYPE (type), dimension_number++)
16048 tree domain = TYPE_DOMAIN (type);
16050 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16051 break;
16053 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16054 and (in GNU C only) variable bounds. Handle all three forms
16055 here. */
16056 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16057 if (domain)
16059 /* We have an array type with specified bounds. */
16060 lower = TYPE_MIN_VALUE (domain);
16061 upper = TYPE_MAX_VALUE (domain);
16063 /* Define the index type. */
16064 if (TREE_TYPE (domain))
16066 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16067 TREE_TYPE field. We can't emit debug info for this
16068 because it is an unnamed integral type. */
16069 if (TREE_CODE (domain) == INTEGER_TYPE
16070 && TYPE_NAME (domain) == NULL_TREE
16071 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16072 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16074 else
16075 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16076 type_die);
16079 /* ??? If upper is NULL, the array has unspecified length,
16080 but it does have a lower bound. This happens with Fortran
16081 dimension arr(N:*)
16082 Since the debugger is definitely going to need to know N
16083 to produce useful results, go ahead and output the lower
16084 bound solo, and hope the debugger can cope. */
16086 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16087 if (upper)
16088 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16091 /* Otherwise we have an array type with an unspecified length. The
16092 DWARF-2 spec does not say how to handle this; let's just leave out the
16093 bounds. */
16097 static void
16098 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16100 dw_die_ref decl_die;
16101 unsigned size;
16103 switch (TREE_CODE (tree_node))
16105 case ERROR_MARK:
16106 size = 0;
16107 break;
16108 case ENUMERAL_TYPE:
16109 case RECORD_TYPE:
16110 case UNION_TYPE:
16111 case QUAL_UNION_TYPE:
16112 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16113 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16115 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16116 return;
16118 size = int_size_in_bytes (tree_node);
16119 break;
16120 case FIELD_DECL:
16121 /* For a data member of a struct or union, the DW_AT_byte_size is
16122 generally given as the number of bytes normally allocated for an
16123 object of the *declared* type of the member itself. This is true
16124 even for bit-fields. */
16125 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16126 break;
16127 default:
16128 gcc_unreachable ();
16131 /* Note that `size' might be -1 when we get to this point. If it is, that
16132 indicates that the byte size of the entity in question is variable. We
16133 have no good way of expressing this fact in Dwarf at the present time,
16134 so just let the -1 pass on through. */
16135 add_AT_unsigned (die, DW_AT_byte_size, size);
16138 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16139 which specifies the distance in bits from the highest order bit of the
16140 "containing object" for the bit-field to the highest order bit of the
16141 bit-field itself.
16143 For any given bit-field, the "containing object" is a hypothetical object
16144 (of some integral or enum type) within which the given bit-field lives. The
16145 type of this hypothetical "containing object" is always the same as the
16146 declared type of the individual bit-field itself. The determination of the
16147 exact location of the "containing object" for a bit-field is rather
16148 complicated. It's handled by the `field_byte_offset' function (above).
16150 Note that it is the size (in bytes) of the hypothetical "containing object"
16151 which will be given in the DW_AT_byte_size attribute for this bit-field.
16152 (See `byte_size_attribute' above). */
16154 static inline void
16155 add_bit_offset_attribute (dw_die_ref die, tree decl)
16157 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16158 tree type = DECL_BIT_FIELD_TYPE (decl);
16159 HOST_WIDE_INT bitpos_int;
16160 HOST_WIDE_INT highest_order_object_bit_offset;
16161 HOST_WIDE_INT highest_order_field_bit_offset;
16162 HOST_WIDE_INT bit_offset;
16164 /* Must be a field and a bit field. */
16165 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16167 /* We can't yet handle bit-fields whose offsets are variable, so if we
16168 encounter such things, just return without generating any attribute
16169 whatsoever. Likewise for variable or too large size. */
16170 if (! host_integerp (bit_position (decl), 0)
16171 || ! host_integerp (DECL_SIZE (decl), 1))
16172 return;
16174 bitpos_int = int_bit_position (decl);
16176 /* Note that the bit offset is always the distance (in bits) from the
16177 highest-order bit of the "containing object" to the highest-order bit of
16178 the bit-field itself. Since the "high-order end" of any object or field
16179 is different on big-endian and little-endian machines, the computation
16180 below must take account of these differences. */
16181 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16182 highest_order_field_bit_offset = bitpos_int;
16184 if (! BYTES_BIG_ENDIAN)
16186 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16187 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16190 bit_offset
16191 = (! BYTES_BIG_ENDIAN
16192 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16193 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16195 if (bit_offset < 0)
16196 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16197 else
16198 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16201 /* For a FIELD_DECL node which represents a bit field, output an attribute
16202 which specifies the length in bits of the given field. */
16204 static inline void
16205 add_bit_size_attribute (dw_die_ref die, tree decl)
16207 /* Must be a field and a bit field. */
16208 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16209 && DECL_BIT_FIELD_TYPE (decl));
16211 if (host_integerp (DECL_SIZE (decl), 1))
16212 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16215 /* If the compiled language is ANSI C, then add a 'prototyped'
16216 attribute, if arg types are given for the parameters of a function. */
16218 static inline void
16219 add_prototyped_attribute (dw_die_ref die, tree func_type)
16221 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16222 && prototype_p (func_type))
16223 add_AT_flag (die, DW_AT_prototyped, 1);
16226 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16227 by looking in either the type declaration or object declaration
16228 equate table. */
16230 static inline dw_die_ref
16231 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16233 dw_die_ref origin_die = NULL;
16235 if (TREE_CODE (origin) != FUNCTION_DECL)
16237 /* We may have gotten separated from the block for the inlined
16238 function, if we're in an exception handler or some such; make
16239 sure that the abstract function has been written out.
16241 Doing this for nested functions is wrong, however; functions are
16242 distinct units, and our context might not even be inline. */
16243 tree fn = origin;
16245 if (TYPE_P (fn))
16246 fn = TYPE_STUB_DECL (fn);
16248 fn = decl_function_context (fn);
16249 if (fn)
16250 dwarf2out_abstract_function (fn);
16253 if (DECL_P (origin))
16254 origin_die = lookup_decl_die (origin);
16255 else if (TYPE_P (origin))
16256 origin_die = lookup_type_die (origin);
16258 /* XXX: Functions that are never lowered don't always have correct block
16259 trees (in the case of java, they simply have no block tree, in some other
16260 languages). For these functions, there is nothing we can really do to
16261 output correct debug info for inlined functions in all cases. Rather
16262 than die, we'll just produce deficient debug info now, in that we will
16263 have variables without a proper abstract origin. In the future, when all
16264 functions are lowered, we should re-add a gcc_assert (origin_die)
16265 here. */
16267 if (origin_die)
16268 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16269 return origin_die;
16272 /* We do not currently support the pure_virtual attribute. */
16274 static inline void
16275 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16277 if (DECL_VINDEX (func_decl))
16279 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16281 if (host_integerp (DECL_VINDEX (func_decl), 0))
16282 add_AT_loc (die, DW_AT_vtable_elem_location,
16283 new_loc_descr (DW_OP_constu,
16284 tree_low_cst (DECL_VINDEX (func_decl), 0),
16285 0));
16287 /* GNU extension: Record what type this method came from originally. */
16288 if (debug_info_level > DINFO_LEVEL_TERSE
16289 && DECL_CONTEXT (func_decl))
16290 add_AT_die_ref (die, DW_AT_containing_type,
16291 lookup_type_die (DECL_CONTEXT (func_decl)));
16295 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16296 given decl. This used to be a vendor extension until after DWARF 4
16297 standardized it. */
16299 static void
16300 add_linkage_attr (dw_die_ref die, tree decl)
16302 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16304 /* Mimic what assemble_name_raw does with a leading '*'. */
16305 if (name[0] == '*')
16306 name = &name[1];
16308 if (dwarf_version >= 4)
16309 add_AT_string (die, DW_AT_linkage_name, name);
16310 else
16311 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16314 /* Add source coordinate attributes for the given decl. */
16316 static void
16317 add_src_coords_attributes (dw_die_ref die, tree decl)
16319 expanded_location s;
16321 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16322 return;
16323 s = expand_location (DECL_SOURCE_LOCATION (decl));
16324 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16325 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16328 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16330 static void
16331 add_linkage_name (dw_die_ref die, tree decl)
16333 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16334 && TREE_PUBLIC (decl)
16335 && !DECL_ABSTRACT (decl)
16336 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16337 && die->die_tag != DW_TAG_member)
16339 /* Defer until we have an assembler name set. */
16340 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16342 limbo_die_node *asm_name;
16344 asm_name = ggc_alloc_cleared_limbo_die_node ();
16345 asm_name->die = die;
16346 asm_name->created_for = decl;
16347 asm_name->next = deferred_asm_name;
16348 deferred_asm_name = asm_name;
16350 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16351 add_linkage_attr (die, decl);
16355 /* Add a DW_AT_name attribute and source coordinate attribute for the
16356 given decl, but only if it actually has a name. */
16358 static void
16359 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16361 tree decl_name;
16363 decl_name = DECL_NAME (decl);
16364 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16366 const char *name = dwarf2_name (decl, 0);
16367 if (name)
16368 add_name_attribute (die, name);
16369 if (! DECL_ARTIFICIAL (decl))
16370 add_src_coords_attributes (die, decl);
16372 add_linkage_name (die, decl);
16375 #ifdef VMS_DEBUGGING_INFO
16376 /* Get the function's name, as described by its RTL. This may be different
16377 from the DECL_NAME name used in the source file. */
16378 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16380 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16381 XEXP (DECL_RTL (decl), 0), false);
16382 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16384 #endif /* VMS_DEBUGGING_INFO */
16387 #ifdef VMS_DEBUGGING_INFO
16388 /* Output the debug main pointer die for VMS */
16390 void
16391 dwarf2out_vms_debug_main_pointer (void)
16393 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16394 dw_die_ref die;
16396 /* Allocate the VMS debug main subprogram die. */
16397 die = ggc_alloc_cleared_die_node ();
16398 die->die_tag = DW_TAG_subprogram;
16399 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16400 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16401 current_function_funcdef_no);
16402 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16404 /* Make it the first child of comp_unit_die (). */
16405 die->die_parent = comp_unit_die ();
16406 if (comp_unit_die ()->die_child)
16408 die->die_sib = comp_unit_die ()->die_child->die_sib;
16409 comp_unit_die ()->die_child->die_sib = die;
16411 else
16413 die->die_sib = die;
16414 comp_unit_die ()->die_child = die;
16417 #endif /* VMS_DEBUGGING_INFO */
16419 /* Push a new declaration scope. */
16421 static void
16422 push_decl_scope (tree scope)
16424 vec_safe_push (decl_scope_table, scope);
16427 /* Pop a declaration scope. */
16429 static inline void
16430 pop_decl_scope (void)
16432 decl_scope_table->pop ();
16435 /* walk_tree helper function for uses_local_type, below. */
16437 static tree
16438 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16440 if (!TYPE_P (*tp))
16441 *walk_subtrees = 0;
16442 else
16444 tree name = TYPE_NAME (*tp);
16445 if (name && DECL_P (name) && decl_function_context (name))
16446 return *tp;
16448 return NULL_TREE;
16451 /* If TYPE involves a function-local type (including a local typedef to a
16452 non-local type), returns that type; otherwise returns NULL_TREE. */
16454 static tree
16455 uses_local_type (tree type)
16457 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16458 return used;
16461 /* Return the DIE for the scope that immediately contains this type.
16462 Non-named types that do not involve a function-local type get global
16463 scope. Named types nested in namespaces or other types get their
16464 containing scope. All other types (i.e. function-local named types) get
16465 the current active scope. */
16467 static dw_die_ref
16468 scope_die_for (tree t, dw_die_ref context_die)
16470 dw_die_ref scope_die = NULL;
16471 tree containing_scope;
16473 /* Non-types always go in the current scope. */
16474 gcc_assert (TYPE_P (t));
16476 /* Use the scope of the typedef, rather than the scope of the type
16477 it refers to. */
16478 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16479 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16480 else
16481 containing_scope = TYPE_CONTEXT (t);
16483 /* Use the containing namespace if there is one. */
16484 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16486 if (context_die == lookup_decl_die (containing_scope))
16487 /* OK */;
16488 else if (debug_info_level > DINFO_LEVEL_TERSE)
16489 context_die = get_context_die (containing_scope);
16490 else
16491 containing_scope = NULL_TREE;
16494 /* Ignore function type "scopes" from the C frontend. They mean that
16495 a tagged type is local to a parmlist of a function declarator, but
16496 that isn't useful to DWARF. */
16497 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16498 containing_scope = NULL_TREE;
16500 if (SCOPE_FILE_SCOPE_P (containing_scope))
16502 /* If T uses a local type keep it local as well, to avoid references
16503 to function-local DIEs from outside the function. */
16504 if (current_function_decl && uses_local_type (t))
16505 scope_die = context_die;
16506 else
16507 scope_die = comp_unit_die ();
16509 else if (TYPE_P (containing_scope))
16511 /* For types, we can just look up the appropriate DIE. */
16512 if (debug_info_level > DINFO_LEVEL_TERSE)
16513 scope_die = get_context_die (containing_scope);
16514 else
16516 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16517 if (scope_die == NULL)
16518 scope_die = comp_unit_die ();
16521 else
16522 scope_die = context_die;
16524 return scope_die;
16527 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16529 static inline int
16530 local_scope_p (dw_die_ref context_die)
16532 for (; context_die; context_die = context_die->die_parent)
16533 if (context_die->die_tag == DW_TAG_inlined_subroutine
16534 || context_die->die_tag == DW_TAG_subprogram)
16535 return 1;
16537 return 0;
16540 /* Returns nonzero if CONTEXT_DIE is a class. */
16542 static inline int
16543 class_scope_p (dw_die_ref context_die)
16545 return (context_die
16546 && (context_die->die_tag == DW_TAG_structure_type
16547 || context_die->die_tag == DW_TAG_class_type
16548 || context_die->die_tag == DW_TAG_interface_type
16549 || context_die->die_tag == DW_TAG_union_type));
16552 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16553 whether or not to treat a DIE in this context as a declaration. */
16555 static inline int
16556 class_or_namespace_scope_p (dw_die_ref context_die)
16558 return (class_scope_p (context_die)
16559 || (context_die && context_die->die_tag == DW_TAG_namespace));
16562 /* Many forms of DIEs require a "type description" attribute. This
16563 routine locates the proper "type descriptor" die for the type given
16564 by 'type', and adds a DW_AT_type attribute below the given die. */
16566 static void
16567 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16568 int decl_volatile, dw_die_ref context_die)
16570 enum tree_code code = TREE_CODE (type);
16571 dw_die_ref type_die = NULL;
16573 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16574 or fixed-point type, use the inner type. This is because we have no
16575 support for unnamed types in base_type_die. This can happen if this is
16576 an Ada subrange type. Correct solution is emit a subrange type die. */
16577 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16578 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16579 type = TREE_TYPE (type), code = TREE_CODE (type);
16581 if (code == ERROR_MARK
16582 /* Handle a special case. For functions whose return type is void, we
16583 generate *no* type attribute. (Note that no object may have type
16584 `void', so this only applies to function return types). */
16585 || code == VOID_TYPE)
16586 return;
16588 type_die = modified_type_die (type,
16589 decl_const || TYPE_READONLY (type),
16590 decl_volatile || TYPE_VOLATILE (type),
16591 context_die);
16593 if (type_die != NULL)
16594 add_AT_die_ref (object_die, DW_AT_type, type_die);
16597 /* Given an object die, add the calling convention attribute for the
16598 function call type. */
16599 static void
16600 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16602 enum dwarf_calling_convention value = DW_CC_normal;
16604 value = ((enum dwarf_calling_convention)
16605 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16607 if (is_fortran ()
16608 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16610 /* DWARF 2 doesn't provide a way to identify a program's source-level
16611 entry point. DW_AT_calling_convention attributes are only meant
16612 to describe functions' calling conventions. However, lacking a
16613 better way to signal the Fortran main program, we used this for
16614 a long time, following existing custom. Now, DWARF 4 has
16615 DW_AT_main_subprogram, which we add below, but some tools still
16616 rely on the old way, which we thus keep. */
16617 value = DW_CC_program;
16619 if (dwarf_version >= 4 || !dwarf_strict)
16620 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16623 /* Only add the attribute if the backend requests it, and
16624 is not DW_CC_normal. */
16625 if (value && (value != DW_CC_normal))
16626 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16629 /* Given a tree pointer to a struct, class, union, or enum type node, return
16630 a pointer to the (string) tag name for the given type, or zero if the type
16631 was declared without a tag. */
16633 static const char *
16634 type_tag (const_tree type)
16636 const char *name = 0;
16638 if (TYPE_NAME (type) != 0)
16640 tree t = 0;
16642 /* Find the IDENTIFIER_NODE for the type name. */
16643 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16644 && !TYPE_NAMELESS (type))
16645 t = TYPE_NAME (type);
16647 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16648 a TYPE_DECL node, regardless of whether or not a `typedef' was
16649 involved. */
16650 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16651 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16653 /* We want to be extra verbose. Don't call dwarf_name if
16654 DECL_NAME isn't set. The default hook for decl_printable_name
16655 doesn't like that, and in this context it's correct to return
16656 0, instead of "<anonymous>" or the like. */
16657 if (DECL_NAME (TYPE_NAME (type))
16658 && !DECL_NAMELESS (TYPE_NAME (type)))
16659 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16662 /* Now get the name as a string, or invent one. */
16663 if (!name && t != 0)
16664 name = IDENTIFIER_POINTER (t);
16667 return (name == 0 || *name == '\0') ? 0 : name;
16670 /* Return the type associated with a data member, make a special check
16671 for bit field types. */
16673 static inline tree
16674 member_declared_type (const_tree member)
16676 return (DECL_BIT_FIELD_TYPE (member)
16677 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16680 /* Get the decl's label, as described by its RTL. This may be different
16681 from the DECL_NAME name used in the source file. */
16683 #if 0
16684 static const char *
16685 decl_start_label (tree decl)
16687 rtx x;
16688 const char *fnname;
16690 x = DECL_RTL (decl);
16691 gcc_assert (MEM_P (x));
16693 x = XEXP (x, 0);
16694 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16696 fnname = XSTR (x, 0);
16697 return fnname;
16699 #endif
16701 /* These routines generate the internal representation of the DIE's for
16702 the compilation unit. Debugging information is collected by walking
16703 the declaration trees passed in from dwarf2out_decl(). */
16705 static void
16706 gen_array_type_die (tree type, dw_die_ref context_die)
16708 dw_die_ref scope_die = scope_die_for (type, context_die);
16709 dw_die_ref array_die;
16711 /* GNU compilers represent multidimensional array types as sequences of one
16712 dimensional array types whose element types are themselves array types.
16713 We sometimes squish that down to a single array_type DIE with multiple
16714 subscripts in the Dwarf debugging info. The draft Dwarf specification
16715 say that we are allowed to do this kind of compression in C, because
16716 there is no difference between an array of arrays and a multidimensional
16717 array. We don't do this for Ada to remain as close as possible to the
16718 actual representation, which is especially important against the language
16719 flexibilty wrt arrays of variable size. */
16721 bool collapse_nested_arrays = !is_ada ();
16722 tree element_type;
16724 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16725 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16726 if (TYPE_STRING_FLAG (type)
16727 && TREE_CODE (type) == ARRAY_TYPE
16728 && is_fortran ()
16729 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16731 HOST_WIDE_INT size;
16733 array_die = new_die (DW_TAG_string_type, scope_die, type);
16734 add_name_attribute (array_die, type_tag (type));
16735 equate_type_number_to_die (type, array_die);
16736 size = int_size_in_bytes (type);
16737 if (size >= 0)
16738 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16739 else if (TYPE_DOMAIN (type) != NULL_TREE
16740 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16741 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16743 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16744 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16746 size = int_size_in_bytes (TREE_TYPE (szdecl));
16747 if (loc && size > 0)
16749 add_AT_location_description (array_die, DW_AT_string_length, loc);
16750 if (size != DWARF2_ADDR_SIZE)
16751 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16754 return;
16757 array_die = new_die (DW_TAG_array_type, scope_die, type);
16758 add_name_attribute (array_die, type_tag (type));
16759 equate_type_number_to_die (type, array_die);
16761 if (TREE_CODE (type) == VECTOR_TYPE)
16762 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16764 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16765 if (is_fortran ()
16766 && TREE_CODE (type) == ARRAY_TYPE
16767 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16768 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16769 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16771 #if 0
16772 /* We default the array ordering. SDB will probably do
16773 the right things even if DW_AT_ordering is not present. It's not even
16774 an issue until we start to get into multidimensional arrays anyway. If
16775 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16776 then we'll have to put the DW_AT_ordering attribute back in. (But if
16777 and when we find out that we need to put these in, we will only do so
16778 for multidimensional arrays. */
16779 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16780 #endif
16782 if (TREE_CODE (type) == VECTOR_TYPE)
16784 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16785 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16786 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16787 add_bound_info (subrange_die, DW_AT_upper_bound,
16788 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16790 else
16791 add_subscript_info (array_die, type, collapse_nested_arrays);
16793 /* Add representation of the type of the elements of this array type and
16794 emit the corresponding DIE if we haven't done it already. */
16795 element_type = TREE_TYPE (type);
16796 if (collapse_nested_arrays)
16797 while (TREE_CODE (element_type) == ARRAY_TYPE)
16799 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16800 break;
16801 element_type = TREE_TYPE (element_type);
16804 add_type_attribute (array_die, element_type, 0, 0, context_die);
16806 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16807 if (TYPE_ARTIFICIAL (type))
16808 add_AT_flag (array_die, DW_AT_artificial, 1);
16810 if (get_AT (array_die, DW_AT_name))
16811 add_pubtype (type, array_die);
16814 static dw_loc_descr_ref
16815 descr_info_loc (tree val, tree base_decl)
16817 HOST_WIDE_INT size;
16818 dw_loc_descr_ref loc, loc2;
16819 enum dwarf_location_atom op;
16821 if (val == base_decl)
16822 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16824 switch (TREE_CODE (val))
16826 CASE_CONVERT:
16827 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16828 case VAR_DECL:
16829 return loc_descriptor_from_tree (val, 0);
16830 case INTEGER_CST:
16831 if (host_integerp (val, 0))
16832 return int_loc_descriptor (tree_low_cst (val, 0));
16833 break;
16834 case INDIRECT_REF:
16835 size = int_size_in_bytes (TREE_TYPE (val));
16836 if (size < 0)
16837 break;
16838 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16839 if (!loc)
16840 break;
16841 if (size == DWARF2_ADDR_SIZE)
16842 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16843 else
16844 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16845 return loc;
16846 case POINTER_PLUS_EXPR:
16847 case PLUS_EXPR:
16848 if (host_integerp (TREE_OPERAND (val, 1), 1)
16849 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16850 < 16384)
16852 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16853 if (!loc)
16854 break;
16855 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16857 else
16859 op = DW_OP_plus;
16860 do_binop:
16861 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16862 if (!loc)
16863 break;
16864 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16865 if (!loc2)
16866 break;
16867 add_loc_descr (&loc, loc2);
16868 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16870 return loc;
16871 case MINUS_EXPR:
16872 op = DW_OP_minus;
16873 goto do_binop;
16874 case MULT_EXPR:
16875 op = DW_OP_mul;
16876 goto do_binop;
16877 case EQ_EXPR:
16878 op = DW_OP_eq;
16879 goto do_binop;
16880 case NE_EXPR:
16881 op = DW_OP_ne;
16882 goto do_binop;
16883 default:
16884 break;
16886 return NULL;
16889 static void
16890 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16891 tree val, tree base_decl)
16893 dw_loc_descr_ref loc;
16895 if (host_integerp (val, 0))
16897 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16898 return;
16901 loc = descr_info_loc (val, base_decl);
16902 if (!loc)
16903 return;
16905 add_AT_loc (die, attr, loc);
16908 /* This routine generates DIE for array with hidden descriptor, details
16909 are filled into *info by a langhook. */
16911 static void
16912 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16913 dw_die_ref context_die)
16915 dw_die_ref scope_die = scope_die_for (type, context_die);
16916 dw_die_ref array_die;
16917 int dim;
16919 array_die = new_die (DW_TAG_array_type, scope_die, type);
16920 add_name_attribute (array_die, type_tag (type));
16921 equate_type_number_to_die (type, array_die);
16923 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16924 if (is_fortran ()
16925 && info->ndimensions >= 2)
16926 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16928 if (info->data_location)
16929 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16930 info->base_decl);
16931 if (info->associated)
16932 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16933 info->base_decl);
16934 if (info->allocated)
16935 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16936 info->base_decl);
16938 for (dim = 0; dim < info->ndimensions; dim++)
16940 dw_die_ref subrange_die
16941 = new_die (DW_TAG_subrange_type, array_die, NULL);
16943 if (info->dimen[dim].lower_bound)
16945 /* If it is the default value, omit it. */
16946 int dflt;
16948 if (host_integerp (info->dimen[dim].lower_bound, 0)
16949 && (dflt = lower_bound_default ()) != -1
16950 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16952 else
16953 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16954 info->dimen[dim].lower_bound,
16955 info->base_decl);
16957 if (info->dimen[dim].upper_bound)
16958 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16959 info->dimen[dim].upper_bound,
16960 info->base_decl);
16961 if (info->dimen[dim].stride)
16962 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16963 info->dimen[dim].stride,
16964 info->base_decl);
16967 gen_type_die (info->element_type, context_die);
16968 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16970 if (get_AT (array_die, DW_AT_name))
16971 add_pubtype (type, array_die);
16974 #if 0
16975 static void
16976 gen_entry_point_die (tree decl, dw_die_ref context_die)
16978 tree origin = decl_ultimate_origin (decl);
16979 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16981 if (origin != NULL)
16982 add_abstract_origin_attribute (decl_die, origin);
16983 else
16985 add_name_and_src_coords_attributes (decl_die, decl);
16986 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16987 0, 0, context_die);
16990 if (DECL_ABSTRACT (decl))
16991 equate_decl_number_to_die (decl, decl_die);
16992 else
16993 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16995 #endif
16997 /* Walk through the list of incomplete types again, trying once more to
16998 emit full debugging info for them. */
17000 static void
17001 retry_incomplete_types (void)
17003 int i;
17005 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17006 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17007 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17010 /* Determine what tag to use for a record type. */
17012 static enum dwarf_tag
17013 record_type_tag (tree type)
17015 if (! lang_hooks.types.classify_record)
17016 return DW_TAG_structure_type;
17018 switch (lang_hooks.types.classify_record (type))
17020 case RECORD_IS_STRUCT:
17021 return DW_TAG_structure_type;
17023 case RECORD_IS_CLASS:
17024 return DW_TAG_class_type;
17026 case RECORD_IS_INTERFACE:
17027 if (dwarf_version >= 3 || !dwarf_strict)
17028 return DW_TAG_interface_type;
17029 return DW_TAG_structure_type;
17031 default:
17032 gcc_unreachable ();
17036 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17037 include all of the information about the enumeration values also. Each
17038 enumerated type name/value is listed as a child of the enumerated type
17039 DIE. */
17041 static dw_die_ref
17042 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17044 dw_die_ref type_die = lookup_type_die (type);
17046 if (type_die == NULL)
17048 type_die = new_die (DW_TAG_enumeration_type,
17049 scope_die_for (type, context_die), type);
17050 equate_type_number_to_die (type, type_die);
17051 add_name_attribute (type_die, type_tag (type));
17052 if (dwarf_version >= 4 || !dwarf_strict)
17054 if (ENUM_IS_SCOPED (type))
17055 add_AT_flag (type_die, DW_AT_enum_class, 1);
17056 if (ENUM_IS_OPAQUE (type))
17057 add_AT_flag (type_die, DW_AT_declaration, 1);
17060 else if (! TYPE_SIZE (type))
17061 return type_die;
17062 else
17063 remove_AT (type_die, DW_AT_declaration);
17065 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17066 given enum type is incomplete, do not generate the DW_AT_byte_size
17067 attribute or the DW_AT_element_list attribute. */
17068 if (TYPE_SIZE (type))
17070 tree link;
17072 TREE_ASM_WRITTEN (type) = 1;
17073 add_byte_size_attribute (type_die, type);
17074 if (TYPE_STUB_DECL (type) != NULL_TREE)
17076 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17077 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17080 /* If the first reference to this type was as the return type of an
17081 inline function, then it may not have a parent. Fix this now. */
17082 if (type_die->die_parent == NULL)
17083 add_child_die (scope_die_for (type, context_die), type_die);
17085 for (link = TYPE_VALUES (type);
17086 link != NULL; link = TREE_CHAIN (link))
17088 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17089 tree value = TREE_VALUE (link);
17091 add_name_attribute (enum_die,
17092 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17094 if (TREE_CODE (value) == CONST_DECL)
17095 value = DECL_INITIAL (value);
17097 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value)))
17098 && (simple_type_size_in_bits (TREE_TYPE (value))
17099 <= HOST_BITS_PER_WIDE_INT || host_integerp (value, 0)))
17100 /* DWARF2 does not provide a way of indicating whether or
17101 not enumeration constants are signed or unsigned. GDB
17102 always assumes the values are signed, so we output all
17103 values as if they were signed. That means that
17104 enumeration constants with very large unsigned values
17105 will appear to have negative values in the debugger.
17107 TODO: the above comment is wrong, DWARF2 does provide
17108 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17109 This should be re-worked to use correct signed/unsigned
17110 int/double tags for all cases, instead of always treating as
17111 signed. */
17112 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17113 else
17114 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17115 that here. */
17116 add_AT_double (enum_die, DW_AT_const_value,
17117 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17120 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17121 if (TYPE_ARTIFICIAL (type))
17122 add_AT_flag (type_die, DW_AT_artificial, 1);
17124 else
17125 add_AT_flag (type_die, DW_AT_declaration, 1);
17127 add_pubtype (type, type_die);
17129 return type_die;
17132 /* Generate a DIE to represent either a real live formal parameter decl or to
17133 represent just the type of some formal parameter position in some function
17134 type.
17136 Note that this routine is a bit unusual because its argument may be a
17137 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17138 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17139 node. If it's the former then this function is being called to output a
17140 DIE to represent a formal parameter object (or some inlining thereof). If
17141 it's the latter, then this function is only being called to output a
17142 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17143 argument type of some subprogram type.
17144 If EMIT_NAME_P is true, name and source coordinate attributes
17145 are emitted. */
17147 static dw_die_ref
17148 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17149 dw_die_ref context_die)
17151 tree node_or_origin = node ? node : origin;
17152 tree ultimate_origin;
17153 dw_die_ref parm_die
17154 = new_die (DW_TAG_formal_parameter, context_die, node);
17156 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17158 case tcc_declaration:
17159 ultimate_origin = decl_ultimate_origin (node_or_origin);
17160 if (node || ultimate_origin)
17161 origin = ultimate_origin;
17162 if (origin != NULL)
17163 add_abstract_origin_attribute (parm_die, origin);
17164 else if (emit_name_p)
17165 add_name_and_src_coords_attributes (parm_die, node);
17166 if (origin == NULL
17167 || (! DECL_ABSTRACT (node_or_origin)
17168 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17169 decl_function_context
17170 (node_or_origin))))
17172 tree type = TREE_TYPE (node_or_origin);
17173 if (decl_by_reference_p (node_or_origin))
17174 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17175 context_die);
17176 else
17177 add_type_attribute (parm_die, type,
17178 TREE_READONLY (node_or_origin),
17179 TREE_THIS_VOLATILE (node_or_origin),
17180 context_die);
17182 if (origin == NULL && DECL_ARTIFICIAL (node))
17183 add_AT_flag (parm_die, DW_AT_artificial, 1);
17185 if (node && node != origin)
17186 equate_decl_number_to_die (node, parm_die);
17187 if (! DECL_ABSTRACT (node_or_origin))
17188 add_location_or_const_value_attribute (parm_die, node_or_origin,
17189 node == NULL, DW_AT_location);
17191 break;
17193 case tcc_type:
17194 /* We were called with some kind of a ..._TYPE node. */
17195 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17196 break;
17198 default:
17199 gcc_unreachable ();
17202 return parm_die;
17205 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17206 children DW_TAG_formal_parameter DIEs representing the arguments of the
17207 parameter pack.
17209 PARM_PACK must be a function parameter pack.
17210 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17211 must point to the subsequent arguments of the function PACK_ARG belongs to.
17212 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17213 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17214 following the last one for which a DIE was generated. */
17216 static dw_die_ref
17217 gen_formal_parameter_pack_die (tree parm_pack,
17218 tree pack_arg,
17219 dw_die_ref subr_die,
17220 tree *next_arg)
17222 tree arg;
17223 dw_die_ref parm_pack_die;
17225 gcc_assert (parm_pack
17226 && lang_hooks.function_parameter_pack_p (parm_pack)
17227 && subr_die);
17229 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17230 add_src_coords_attributes (parm_pack_die, parm_pack);
17232 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17234 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17235 parm_pack))
17236 break;
17237 gen_formal_parameter_die (arg, NULL,
17238 false /* Don't emit name attribute. */,
17239 parm_pack_die);
17241 if (next_arg)
17242 *next_arg = arg;
17243 return parm_pack_die;
17246 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17247 at the end of an (ANSI prototyped) formal parameters list. */
17249 static void
17250 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17252 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17255 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17256 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17257 parameters as specified in some function type specification (except for
17258 those which appear as part of a function *definition*). */
17260 static void
17261 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17263 tree link;
17264 tree formal_type = NULL;
17265 tree first_parm_type;
17266 tree arg;
17268 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17270 arg = DECL_ARGUMENTS (function_or_method_type);
17271 function_or_method_type = TREE_TYPE (function_or_method_type);
17273 else
17274 arg = NULL_TREE;
17276 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17278 /* Make our first pass over the list of formal parameter types and output a
17279 DW_TAG_formal_parameter DIE for each one. */
17280 for (link = first_parm_type; link; )
17282 dw_die_ref parm_die;
17284 formal_type = TREE_VALUE (link);
17285 if (formal_type == void_type_node)
17286 break;
17288 /* Output a (nameless) DIE to represent the formal parameter itself. */
17289 parm_die = gen_formal_parameter_die (formal_type, NULL,
17290 true /* Emit name attribute. */,
17291 context_die);
17292 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17293 && link == first_parm_type)
17295 add_AT_flag (parm_die, DW_AT_artificial, 1);
17296 if (dwarf_version >= 3 || !dwarf_strict)
17297 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17299 else if (arg && DECL_ARTIFICIAL (arg))
17300 add_AT_flag (parm_die, DW_AT_artificial, 1);
17302 link = TREE_CHAIN (link);
17303 if (arg)
17304 arg = DECL_CHAIN (arg);
17307 /* If this function type has an ellipsis, add a
17308 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17309 if (formal_type != void_type_node)
17310 gen_unspecified_parameters_die (function_or_method_type, context_die);
17312 /* Make our second (and final) pass over the list of formal parameter types
17313 and output DIEs to represent those types (as necessary). */
17314 for (link = TYPE_ARG_TYPES (function_or_method_type);
17315 link && TREE_VALUE (link);
17316 link = TREE_CHAIN (link))
17317 gen_type_die (TREE_VALUE (link), context_die);
17320 /* We want to generate the DIE for TYPE so that we can generate the
17321 die for MEMBER, which has been defined; we will need to refer back
17322 to the member declaration nested within TYPE. If we're trying to
17323 generate minimal debug info for TYPE, processing TYPE won't do the
17324 trick; we need to attach the member declaration by hand. */
17326 static void
17327 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17329 gen_type_die (type, context_die);
17331 /* If we're trying to avoid duplicate debug info, we may not have
17332 emitted the member decl for this function. Emit it now. */
17333 if (TYPE_STUB_DECL (type)
17334 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17335 && ! lookup_decl_die (member))
17337 dw_die_ref type_die;
17338 gcc_assert (!decl_ultimate_origin (member));
17340 push_decl_scope (type);
17341 type_die = lookup_type_die_strip_naming_typedef (type);
17342 if (TREE_CODE (member) == FUNCTION_DECL)
17343 gen_subprogram_die (member, type_die);
17344 else if (TREE_CODE (member) == FIELD_DECL)
17346 /* Ignore the nameless fields that are used to skip bits but handle
17347 C++ anonymous unions and structs. */
17348 if (DECL_NAME (member) != NULL_TREE
17349 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17350 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17352 gen_type_die (member_declared_type (member), type_die);
17353 gen_field_die (member, type_die);
17356 else
17357 gen_variable_die (member, NULL_TREE, type_die);
17359 pop_decl_scope ();
17363 /* Forward declare these functions, because they are mutually recursive
17364 with their set_block_* pairing functions. */
17365 static void set_decl_origin_self (tree);
17366 static void set_decl_abstract_flags (tree, int);
17368 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17369 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17370 that it points to the node itself, thus indicating that the node is its
17371 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17372 the given node is NULL, recursively descend the decl/block tree which
17373 it is the root of, and for each other ..._DECL or BLOCK node contained
17374 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17375 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17376 values to point to themselves. */
17378 static void
17379 set_block_origin_self (tree stmt)
17381 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17383 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17386 tree local_decl;
17388 for (local_decl = BLOCK_VARS (stmt);
17389 local_decl != NULL_TREE;
17390 local_decl = DECL_CHAIN (local_decl))
17391 if (! DECL_EXTERNAL (local_decl))
17392 set_decl_origin_self (local_decl); /* Potential recursion. */
17396 tree subblock;
17398 for (subblock = BLOCK_SUBBLOCKS (stmt);
17399 subblock != NULL_TREE;
17400 subblock = BLOCK_CHAIN (subblock))
17401 set_block_origin_self (subblock); /* Recurse. */
17406 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17407 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17408 node to so that it points to the node itself, thus indicating that the
17409 node represents its own (abstract) origin. Additionally, if the
17410 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17411 the decl/block tree of which the given node is the root of, and for
17412 each other ..._DECL or BLOCK node contained therein whose
17413 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17414 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17415 point to themselves. */
17417 static void
17418 set_decl_origin_self (tree decl)
17420 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17422 DECL_ABSTRACT_ORIGIN (decl) = decl;
17423 if (TREE_CODE (decl) == FUNCTION_DECL)
17425 tree arg;
17427 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17428 DECL_ABSTRACT_ORIGIN (arg) = arg;
17429 if (DECL_INITIAL (decl) != NULL_TREE
17430 && DECL_INITIAL (decl) != error_mark_node)
17431 set_block_origin_self (DECL_INITIAL (decl));
17436 /* Given a pointer to some BLOCK node, and a boolean value to set the
17437 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17438 the given block, and for all local decls and all local sub-blocks
17439 (recursively) which are contained therein. */
17441 static void
17442 set_block_abstract_flags (tree stmt, int setting)
17444 tree local_decl;
17445 tree subblock;
17446 unsigned int i;
17448 BLOCK_ABSTRACT (stmt) = setting;
17450 for (local_decl = BLOCK_VARS (stmt);
17451 local_decl != NULL_TREE;
17452 local_decl = DECL_CHAIN (local_decl))
17453 if (! DECL_EXTERNAL (local_decl))
17454 set_decl_abstract_flags (local_decl, setting);
17456 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17458 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17459 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17460 || TREE_CODE (local_decl) == PARM_DECL)
17461 set_decl_abstract_flags (local_decl, setting);
17464 for (subblock = BLOCK_SUBBLOCKS (stmt);
17465 subblock != NULL_TREE;
17466 subblock = BLOCK_CHAIN (subblock))
17467 set_block_abstract_flags (subblock, setting);
17470 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17471 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17472 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17473 set the abstract flags for all of the parameters, local vars, local
17474 blocks and sub-blocks (recursively) to the same setting. */
17476 static void
17477 set_decl_abstract_flags (tree decl, int setting)
17479 DECL_ABSTRACT (decl) = setting;
17480 if (TREE_CODE (decl) == FUNCTION_DECL)
17482 tree arg;
17484 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17485 DECL_ABSTRACT (arg) = setting;
17486 if (DECL_INITIAL (decl) != NULL_TREE
17487 && DECL_INITIAL (decl) != error_mark_node)
17488 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17492 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17493 may later generate inlined and/or out-of-line instances of. */
17495 static void
17496 dwarf2out_abstract_function (tree decl)
17498 dw_die_ref old_die;
17499 tree save_fn;
17500 tree context;
17501 int was_abstract;
17502 htab_t old_decl_loc_table;
17503 htab_t old_cached_dw_loc_list_table;
17504 int old_call_site_count, old_tail_call_site_count;
17505 struct call_arg_loc_node *old_call_arg_locations;
17507 /* Make sure we have the actual abstract inline, not a clone. */
17508 decl = DECL_ORIGIN (decl);
17510 old_die = lookup_decl_die (decl);
17511 if (old_die && get_AT (old_die, DW_AT_inline))
17512 /* We've already generated the abstract instance. */
17513 return;
17515 /* We can be called while recursively when seeing block defining inlined subroutine
17516 DIE. Be sure to not clobber the outer location table nor use it or we would
17517 get locations in abstract instantces. */
17518 old_decl_loc_table = decl_loc_table;
17519 decl_loc_table = NULL;
17520 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17521 cached_dw_loc_list_table = NULL;
17522 old_call_arg_locations = call_arg_locations;
17523 call_arg_locations = NULL;
17524 old_call_site_count = call_site_count;
17525 call_site_count = -1;
17526 old_tail_call_site_count = tail_call_site_count;
17527 tail_call_site_count = -1;
17529 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17530 we don't get confused by DECL_ABSTRACT. */
17531 if (debug_info_level > DINFO_LEVEL_TERSE)
17533 context = decl_class_context (decl);
17534 if (context)
17535 gen_type_die_for_member
17536 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17539 /* Pretend we've just finished compiling this function. */
17540 save_fn = current_function_decl;
17541 current_function_decl = decl;
17543 was_abstract = DECL_ABSTRACT (decl);
17544 set_decl_abstract_flags (decl, 1);
17545 dwarf2out_decl (decl);
17546 if (! was_abstract)
17547 set_decl_abstract_flags (decl, 0);
17549 current_function_decl = save_fn;
17550 decl_loc_table = old_decl_loc_table;
17551 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17552 call_arg_locations = old_call_arg_locations;
17553 call_site_count = old_call_site_count;
17554 tail_call_site_count = old_tail_call_site_count;
17557 /* Helper function of premark_used_types() which gets called through
17558 htab_traverse.
17560 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17561 marked as unused by prune_unused_types. */
17563 static int
17564 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17566 tree type;
17567 dw_die_ref die;
17569 type = (tree) *slot;
17570 die = lookup_type_die (type);
17571 if (die != NULL)
17572 die->die_perennial_p = 1;
17573 return 1;
17576 /* Helper function of premark_types_used_by_global_vars which gets called
17577 through htab_traverse.
17579 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17580 marked as unused by prune_unused_types. The DIE of the type is marked
17581 only if the global variable using the type will actually be emitted. */
17583 static int
17584 premark_types_used_by_global_vars_helper (void **slot,
17585 void *data ATTRIBUTE_UNUSED)
17587 struct types_used_by_vars_entry *entry;
17588 dw_die_ref die;
17590 entry = (struct types_used_by_vars_entry *) *slot;
17591 gcc_assert (entry->type != NULL
17592 && entry->var_decl != NULL);
17593 die = lookup_type_die (entry->type);
17594 if (die)
17596 /* Ask cgraph if the global variable really is to be emitted.
17597 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17598 struct varpool_node *node = varpool_get_node (entry->var_decl);
17599 if (node && node->analyzed)
17601 die->die_perennial_p = 1;
17602 /* Keep the parent DIEs as well. */
17603 while ((die = die->die_parent) && die->die_perennial_p == 0)
17604 die->die_perennial_p = 1;
17607 return 1;
17610 /* Mark all members of used_types_hash as perennial. */
17612 static void
17613 premark_used_types (struct function *fun)
17615 if (fun && fun->used_types_hash)
17616 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17619 /* Mark all members of types_used_by_vars_entry as perennial. */
17621 static void
17622 premark_types_used_by_global_vars (void)
17624 if (types_used_by_vars_hash)
17625 htab_traverse (types_used_by_vars_hash,
17626 premark_types_used_by_global_vars_helper, NULL);
17629 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17630 for CA_LOC call arg loc node. */
17632 static dw_die_ref
17633 gen_call_site_die (tree decl, dw_die_ref subr_die,
17634 struct call_arg_loc_node *ca_loc)
17636 dw_die_ref stmt_die = NULL, die;
17637 tree block = ca_loc->block;
17639 while (block
17640 && block != DECL_INITIAL (decl)
17641 && TREE_CODE (block) == BLOCK)
17643 if (block_map.length () > BLOCK_NUMBER (block))
17644 stmt_die = block_map[BLOCK_NUMBER (block)];
17645 if (stmt_die)
17646 break;
17647 block = BLOCK_SUPERCONTEXT (block);
17649 if (stmt_die == NULL)
17650 stmt_die = subr_die;
17651 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17652 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17653 if (ca_loc->tail_call_p)
17654 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17655 if (ca_loc->symbol_ref)
17657 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17658 if (tdie)
17659 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17660 else
17661 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17663 return die;
17666 /* Generate a DIE to represent a declared function (either file-scope or
17667 block-local). */
17669 static void
17670 gen_subprogram_die (tree decl, dw_die_ref context_die)
17672 tree origin = decl_ultimate_origin (decl);
17673 dw_die_ref subr_die;
17674 tree outer_scope;
17675 dw_die_ref old_die = lookup_decl_die (decl);
17676 int declaration = (current_function_decl != decl
17677 || class_or_namespace_scope_p (context_die));
17679 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17681 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17682 started to generate the abstract instance of an inline, decided to output
17683 its containing class, and proceeded to emit the declaration of the inline
17684 from the member list for the class. If so, DECLARATION takes priority;
17685 we'll get back to the abstract instance when done with the class. */
17687 /* The class-scope declaration DIE must be the primary DIE. */
17688 if (origin && declaration && class_or_namespace_scope_p (context_die))
17690 origin = NULL;
17691 gcc_assert (!old_die);
17694 /* Now that the C++ front end lazily declares artificial member fns, we
17695 might need to retrofit the declaration into its class. */
17696 if (!declaration && !origin && !old_die
17697 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17698 && !class_or_namespace_scope_p (context_die)
17699 && debug_info_level > DINFO_LEVEL_TERSE)
17700 old_die = force_decl_die (decl);
17702 if (origin != NULL)
17704 gcc_assert (!declaration || local_scope_p (context_die));
17706 /* Fixup die_parent for the abstract instance of a nested
17707 inline function. */
17708 if (old_die && old_die->die_parent == NULL)
17709 add_child_die (context_die, old_die);
17711 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17712 add_abstract_origin_attribute (subr_die, origin);
17713 /* This is where the actual code for a cloned function is.
17714 Let's emit linkage name attribute for it. This helps
17715 debuggers to e.g, set breakpoints into
17716 constructors/destructors when the user asks "break
17717 K::K". */
17718 add_linkage_name (subr_die, decl);
17720 else if (old_die)
17722 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17723 struct dwarf_file_data * file_index = lookup_filename (s.file);
17725 if (!get_AT_flag (old_die, DW_AT_declaration)
17726 /* We can have a normal definition following an inline one in the
17727 case of redefinition of GNU C extern inlines.
17728 It seems reasonable to use AT_specification in this case. */
17729 && !get_AT (old_die, DW_AT_inline))
17731 /* Detect and ignore this case, where we are trying to output
17732 something we have already output. */
17733 return;
17736 /* If the definition comes from the same place as the declaration,
17737 maybe use the old DIE. We always want the DIE for this function
17738 that has the *_pc attributes to be under comp_unit_die so the
17739 debugger can find it. We also need to do this for abstract
17740 instances of inlines, since the spec requires the out-of-line copy
17741 to have the same parent. For local class methods, this doesn't
17742 apply; we just use the old DIE. */
17743 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17744 && (DECL_ARTIFICIAL (decl)
17745 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17746 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17747 == (unsigned) s.line))))
17749 subr_die = old_die;
17751 /* Clear out the declaration attribute and the formal parameters.
17752 Do not remove all children, because it is possible that this
17753 declaration die was forced using force_decl_die(). In such
17754 cases die that forced declaration die (e.g. TAG_imported_module)
17755 is one of the children that we do not want to remove. */
17756 remove_AT (subr_die, DW_AT_declaration);
17757 remove_AT (subr_die, DW_AT_object_pointer);
17758 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17760 else
17762 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17763 add_AT_specification (subr_die, old_die);
17764 add_pubname (decl, subr_die);
17765 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17766 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17767 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17768 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17771 else
17773 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17775 if (TREE_PUBLIC (decl))
17776 add_AT_flag (subr_die, DW_AT_external, 1);
17778 add_name_and_src_coords_attributes (subr_die, decl);
17779 add_pubname (decl, subr_die);
17780 if (debug_info_level > DINFO_LEVEL_TERSE)
17782 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17783 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17784 0, 0, context_die);
17787 add_pure_or_virtual_attribute (subr_die, decl);
17788 if (DECL_ARTIFICIAL (decl))
17789 add_AT_flag (subr_die, DW_AT_artificial, 1);
17791 add_accessibility_attribute (subr_die, decl);
17794 if (declaration)
17796 if (!old_die || !get_AT (old_die, DW_AT_inline))
17798 add_AT_flag (subr_die, DW_AT_declaration, 1);
17800 /* If this is an explicit function declaration then generate
17801 a DW_AT_explicit attribute. */
17802 if (lang_hooks.decls.function_decl_explicit_p (decl)
17803 && (dwarf_version >= 3 || !dwarf_strict))
17804 add_AT_flag (subr_die, DW_AT_explicit, 1);
17806 /* The first time we see a member function, it is in the context of
17807 the class to which it belongs. We make sure of this by emitting
17808 the class first. The next time is the definition, which is
17809 handled above. The two may come from the same source text.
17811 Note that force_decl_die() forces function declaration die. It is
17812 later reused to represent definition. */
17813 equate_decl_number_to_die (decl, subr_die);
17816 else if (DECL_ABSTRACT (decl))
17818 if (DECL_DECLARED_INLINE_P (decl))
17820 if (cgraph_function_possibly_inlined_p (decl))
17821 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17822 else
17823 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17825 else
17827 if (cgraph_function_possibly_inlined_p (decl))
17828 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17829 else
17830 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17833 if (DECL_DECLARED_INLINE_P (decl)
17834 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17835 add_AT_flag (subr_die, DW_AT_artificial, 1);
17837 equate_decl_number_to_die (decl, subr_die);
17839 else if (!DECL_EXTERNAL (decl))
17841 HOST_WIDE_INT cfa_fb_offset;
17842 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17844 if (!old_die || !get_AT (old_die, DW_AT_inline))
17845 equate_decl_number_to_die (decl, subr_die);
17847 gcc_checking_assert (fun);
17848 if (!flag_reorder_blocks_and_partition)
17850 dw_fde_ref fde = fun->fde;
17851 if (fde->dw_fde_begin)
17853 /* We have already generated the labels. */
17854 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17855 fde->dw_fde_end, false);
17857 else
17859 /* Create start/end labels and add the range. */
17860 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17861 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17862 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17863 current_function_funcdef_no);
17864 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17865 current_function_funcdef_no);
17866 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17867 false);
17870 #if VMS_DEBUGGING_INFO
17871 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17872 Section 2.3 Prologue and Epilogue Attributes:
17873 When a breakpoint is set on entry to a function, it is generally
17874 desirable for execution to be suspended, not on the very first
17875 instruction of the function, but rather at a point after the
17876 function's frame has been set up, after any language defined local
17877 declaration processing has been completed, and before execution of
17878 the first statement of the function begins. Debuggers generally
17879 cannot properly determine where this point is. Similarly for a
17880 breakpoint set on exit from a function. The prologue and epilogue
17881 attributes allow a compiler to communicate the location(s) to use. */
17884 if (fde->dw_fde_vms_end_prologue)
17885 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17886 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17888 if (fde->dw_fde_vms_begin_epilogue)
17889 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17890 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17892 #endif
17895 else
17897 /* Generate pubnames entries for the split function code ranges. */
17898 dw_fde_ref fde = fun->fde;
17900 if (fde->dw_fde_second_begin)
17902 if (dwarf_version >= 3 || !dwarf_strict)
17904 /* We should use ranges for non-contiguous code section
17905 addresses. Use the actual code range for the initial
17906 section, since the HOT/COLD labels might precede an
17907 alignment offset. */
17908 bool range_list_added = false;
17909 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17910 fde->dw_fde_end, &range_list_added,
17911 false);
17912 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17913 fde->dw_fde_second_end,
17914 &range_list_added, false);
17915 if (range_list_added)
17916 add_ranges (NULL);
17918 else
17920 /* There is no real support in DW2 for this .. so we make
17921 a work-around. First, emit the pub name for the segment
17922 containing the function label. Then make and emit a
17923 simplified subprogram DIE for the second segment with the
17924 name pre-fixed by __hot/cold_sect_of_. We use the same
17925 linkage name for the second die so that gdb will find both
17926 sections when given "b foo". */
17927 const char *name = NULL;
17928 tree decl_name = DECL_NAME (decl);
17929 dw_die_ref seg_die;
17931 /* Do the 'primary' section. */
17932 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17933 fde->dw_fde_end, false);
17935 /* Build a minimal DIE for the secondary section. */
17936 seg_die = new_die (DW_TAG_subprogram,
17937 subr_die->die_parent, decl);
17939 if (TREE_PUBLIC (decl))
17940 add_AT_flag (seg_die, DW_AT_external, 1);
17942 if (decl_name != NULL
17943 && IDENTIFIER_POINTER (decl_name) != NULL)
17945 name = dwarf2_name (decl, 1);
17946 if (! DECL_ARTIFICIAL (decl))
17947 add_src_coords_attributes (seg_die, decl);
17949 add_linkage_name (seg_die, decl);
17951 gcc_assert (name != NULL);
17952 add_pure_or_virtual_attribute (seg_die, decl);
17953 if (DECL_ARTIFICIAL (decl))
17954 add_AT_flag (seg_die, DW_AT_artificial, 1);
17956 name = concat ("__second_sect_of_", name, NULL);
17957 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17958 fde->dw_fde_second_end, false);
17959 add_name_attribute (seg_die, name);
17960 if (want_pubnames ())
17961 add_pubname_string (name, seg_die);
17964 else
17965 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17966 false);
17969 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17971 /* We define the "frame base" as the function's CFA. This is more
17972 convenient for several reasons: (1) It's stable across the prologue
17973 and epilogue, which makes it better than just a frame pointer,
17974 (2) With dwarf3, there exists a one-byte encoding that allows us
17975 to reference the .debug_frame data by proxy, but failing that,
17976 (3) We can at least reuse the code inspection and interpretation
17977 code that determines the CFA position at various points in the
17978 function. */
17979 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17981 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17982 add_AT_loc (subr_die, DW_AT_frame_base, op);
17984 else
17986 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17987 if (list->dw_loc_next)
17988 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17989 else
17990 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17993 /* Compute a displacement from the "steady-state frame pointer" to
17994 the CFA. The former is what all stack slots and argument slots
17995 will reference in the rtl; the latter is what we've told the
17996 debugger about. We'll need to adjust all frame_base references
17997 by this displacement. */
17998 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18000 if (fun->static_chain_decl)
18001 add_AT_location_description (subr_die, DW_AT_static_link,
18002 loc_list_from_tree (fun->static_chain_decl, 2));
18005 /* Generate child dies for template paramaters. */
18006 if (debug_info_level > DINFO_LEVEL_TERSE)
18007 gen_generic_params_dies (decl);
18009 /* Now output descriptions of the arguments for this function. This gets
18010 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18011 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18012 `...' at the end of the formal parameter list. In order to find out if
18013 there was a trailing ellipsis or not, we must instead look at the type
18014 associated with the FUNCTION_DECL. This will be a node of type
18015 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18016 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18017 an ellipsis at the end. */
18019 /* In the case where we are describing a mere function declaration, all we
18020 need to do here (and all we *can* do here) is to describe the *types* of
18021 its formal parameters. */
18022 if (debug_info_level <= DINFO_LEVEL_TERSE)
18024 else if (declaration)
18025 gen_formal_types_die (decl, subr_die);
18026 else
18028 /* Generate DIEs to represent all known formal parameters. */
18029 tree parm = DECL_ARGUMENTS (decl);
18030 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18031 tree generic_decl_parm = generic_decl
18032 ? DECL_ARGUMENTS (generic_decl)
18033 : NULL;
18035 /* Now we want to walk the list of parameters of the function and
18036 emit their relevant DIEs.
18038 We consider the case of DECL being an instance of a generic function
18039 as well as it being a normal function.
18041 If DECL is an instance of a generic function we walk the
18042 parameters of the generic function declaration _and_ the parameters of
18043 DECL itself. This is useful because we want to emit specific DIEs for
18044 function parameter packs and those are declared as part of the
18045 generic function declaration. In that particular case,
18046 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18047 That DIE has children DIEs representing the set of arguments
18048 of the pack. Note that the set of pack arguments can be empty.
18049 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18050 children DIE.
18052 Otherwise, we just consider the parameters of DECL. */
18053 while (generic_decl_parm || parm)
18055 if (generic_decl_parm
18056 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18057 gen_formal_parameter_pack_die (generic_decl_parm,
18058 parm, subr_die,
18059 &parm);
18060 else if (parm)
18062 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18064 if (parm == DECL_ARGUMENTS (decl)
18065 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18066 && parm_die
18067 && (dwarf_version >= 3 || !dwarf_strict))
18068 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18070 parm = DECL_CHAIN (parm);
18073 if (generic_decl_parm)
18074 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18077 /* Decide whether we need an unspecified_parameters DIE at the end.
18078 There are 2 more cases to do this for: 1) the ansi ... declaration -
18079 this is detectable when the end of the arg list is not a
18080 void_type_node 2) an unprototyped function declaration (not a
18081 definition). This just means that we have no info about the
18082 parameters at all. */
18083 if (prototype_p (TREE_TYPE (decl)))
18085 /* This is the prototyped case, check for.... */
18086 if (stdarg_p (TREE_TYPE (decl)))
18087 gen_unspecified_parameters_die (decl, subr_die);
18089 else if (DECL_INITIAL (decl) == NULL_TREE)
18090 gen_unspecified_parameters_die (decl, subr_die);
18093 /* Output Dwarf info for all of the stuff within the body of the function
18094 (if it has one - it may be just a declaration). */
18095 outer_scope = DECL_INITIAL (decl);
18097 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18098 a function. This BLOCK actually represents the outermost binding contour
18099 for the function, i.e. the contour in which the function's formal
18100 parameters and labels get declared. Curiously, it appears that the front
18101 end doesn't actually put the PARM_DECL nodes for the current function onto
18102 the BLOCK_VARS list for this outer scope, but are strung off of the
18103 DECL_ARGUMENTS list for the function instead.
18105 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18106 the LABEL_DECL nodes for the function however, and we output DWARF info
18107 for those in decls_for_scope. Just within the `outer_scope' there will be
18108 a BLOCK node representing the function's outermost pair of curly braces,
18109 and any blocks used for the base and member initializers of a C++
18110 constructor function. */
18111 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18113 int call_site_note_count = 0;
18114 int tail_call_site_note_count = 0;
18116 /* Emit a DW_TAG_variable DIE for a named return value. */
18117 if (DECL_NAME (DECL_RESULT (decl)))
18118 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18120 current_function_has_inlines = 0;
18121 decls_for_scope (outer_scope, subr_die, 0);
18123 if (call_arg_locations && !dwarf_strict)
18125 struct call_arg_loc_node *ca_loc;
18126 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18128 dw_die_ref die = NULL;
18129 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18130 rtx arg, next_arg;
18132 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18133 arg; arg = next_arg)
18135 dw_loc_descr_ref reg, val;
18136 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18137 dw_die_ref cdie, tdie = NULL;
18139 next_arg = XEXP (arg, 1);
18140 if (REG_P (XEXP (XEXP (arg, 0), 0))
18141 && next_arg
18142 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18143 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18144 && REGNO (XEXP (XEXP (arg, 0), 0))
18145 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18146 next_arg = XEXP (next_arg, 1);
18147 if (mode == VOIDmode)
18149 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18150 if (mode == VOIDmode)
18151 mode = GET_MODE (XEXP (arg, 0));
18153 if (mode == VOIDmode || mode == BLKmode)
18154 continue;
18155 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18157 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18158 tloc = XEXP (XEXP (arg, 0), 1);
18159 continue;
18161 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18162 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18164 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18165 tlocc = XEXP (XEXP (arg, 0), 1);
18166 continue;
18168 reg = NULL;
18169 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18170 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18171 VAR_INIT_STATUS_INITIALIZED);
18172 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18174 rtx mem = XEXP (XEXP (arg, 0), 0);
18175 reg = mem_loc_descriptor (XEXP (mem, 0),
18176 get_address_mode (mem),
18177 GET_MODE (mem),
18178 VAR_INIT_STATUS_INITIALIZED);
18180 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18181 == DEBUG_PARAMETER_REF)
18183 tree tdecl
18184 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18185 tdie = lookup_decl_die (tdecl);
18186 if (tdie == NULL)
18187 continue;
18189 else
18190 continue;
18191 if (reg == NULL
18192 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18193 != DEBUG_PARAMETER_REF)
18194 continue;
18195 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18196 VOIDmode,
18197 VAR_INIT_STATUS_INITIALIZED);
18198 if (val == NULL)
18199 continue;
18200 if (die == NULL)
18201 die = gen_call_site_die (decl, subr_die, ca_loc);
18202 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18203 NULL_TREE);
18204 if (reg != NULL)
18205 add_AT_loc (cdie, DW_AT_location, reg);
18206 else if (tdie != NULL)
18207 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18208 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18209 if (next_arg != XEXP (arg, 1))
18211 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18212 if (mode == VOIDmode)
18213 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18214 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18215 0), 1),
18216 mode, VOIDmode,
18217 VAR_INIT_STATUS_INITIALIZED);
18218 if (val != NULL)
18219 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18222 if (die == NULL
18223 && (ca_loc->symbol_ref || tloc))
18224 die = gen_call_site_die (decl, subr_die, ca_loc);
18225 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18227 dw_loc_descr_ref tval = NULL;
18229 if (tloc != NULL_RTX)
18230 tval = mem_loc_descriptor (tloc,
18231 GET_MODE (tloc) == VOIDmode
18232 ? Pmode : GET_MODE (tloc),
18233 VOIDmode,
18234 VAR_INIT_STATUS_INITIALIZED);
18235 if (tval)
18236 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18237 else if (tlocc != NULL_RTX)
18239 tval = mem_loc_descriptor (tlocc,
18240 GET_MODE (tlocc) == VOIDmode
18241 ? Pmode : GET_MODE (tlocc),
18242 VOIDmode,
18243 VAR_INIT_STATUS_INITIALIZED);
18244 if (tval)
18245 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18246 tval);
18249 if (die != NULL)
18251 call_site_note_count++;
18252 if (ca_loc->tail_call_p)
18253 tail_call_site_note_count++;
18257 call_arg_locations = NULL;
18258 call_arg_loc_last = NULL;
18259 if (tail_call_site_count >= 0
18260 && tail_call_site_count == tail_call_site_note_count
18261 && !dwarf_strict)
18263 if (call_site_count >= 0
18264 && call_site_count == call_site_note_count)
18265 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18266 else
18267 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18269 call_site_count = -1;
18270 tail_call_site_count = -1;
18272 /* Add the calling convention attribute if requested. */
18273 add_calling_convention_attribute (subr_die, decl);
18277 /* Returns a hash value for X (which really is a die_struct). */
18279 static hashval_t
18280 common_block_die_table_hash (const void *x)
18282 const_dw_die_ref d = (const_dw_die_ref) x;
18283 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18286 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18287 as decl_id and die_parent of die_struct Y. */
18289 static int
18290 common_block_die_table_eq (const void *x, const void *y)
18292 const_dw_die_ref d = (const_dw_die_ref) x;
18293 const_dw_die_ref e = (const_dw_die_ref) y;
18294 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18297 /* Generate a DIE to represent a declared data object.
18298 Either DECL or ORIGIN must be non-null. */
18300 static void
18301 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18303 HOST_WIDE_INT off = 0;
18304 tree com_decl;
18305 tree decl_or_origin = decl ? decl : origin;
18306 tree ultimate_origin;
18307 dw_die_ref var_die;
18308 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18309 dw_die_ref origin_die;
18310 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18311 || class_or_namespace_scope_p (context_die));
18312 bool specialization_p = false;
18314 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18315 if (decl || ultimate_origin)
18316 origin = ultimate_origin;
18317 com_decl = fortran_common (decl_or_origin, &off);
18319 /* Symbol in common gets emitted as a child of the common block, in the form
18320 of a data member. */
18321 if (com_decl)
18323 dw_die_ref com_die;
18324 dw_loc_list_ref loc;
18325 die_node com_die_arg;
18327 var_die = lookup_decl_die (decl_or_origin);
18328 if (var_die)
18330 if (get_AT (var_die, DW_AT_location) == NULL)
18332 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18333 if (loc)
18335 if (off)
18337 /* Optimize the common case. */
18338 if (single_element_loc_list_p (loc)
18339 && loc->expr->dw_loc_opc == DW_OP_addr
18340 && loc->expr->dw_loc_next == NULL
18341 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18342 == SYMBOL_REF)
18344 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18345 loc->expr->dw_loc_oprnd1.v.val_addr
18346 = plus_constant (GET_MODE (x), x , off);
18348 else
18349 loc_list_plus_const (loc, off);
18351 add_AT_location_description (var_die, DW_AT_location, loc);
18352 remove_AT (var_die, DW_AT_declaration);
18355 return;
18358 if (common_block_die_table == NULL)
18359 common_block_die_table
18360 = htab_create_ggc (10, common_block_die_table_hash,
18361 common_block_die_table_eq, NULL);
18363 com_die_arg.decl_id = DECL_UID (com_decl);
18364 com_die_arg.die_parent = context_die;
18365 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18366 loc = loc_list_from_tree (com_decl, 2);
18367 if (com_die == NULL)
18369 const char *cnam
18370 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18371 void **slot;
18373 com_die = new_die (DW_TAG_common_block, context_die, decl);
18374 add_name_and_src_coords_attributes (com_die, com_decl);
18375 if (loc)
18377 add_AT_location_description (com_die, DW_AT_location, loc);
18378 /* Avoid sharing the same loc descriptor between
18379 DW_TAG_common_block and DW_TAG_variable. */
18380 loc = loc_list_from_tree (com_decl, 2);
18382 else if (DECL_EXTERNAL (decl))
18383 add_AT_flag (com_die, DW_AT_declaration, 1);
18384 if (want_pubnames ())
18385 add_pubname_string (cnam, com_die); /* ??? needed? */
18386 com_die->decl_id = DECL_UID (com_decl);
18387 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18388 *slot = (void *) com_die;
18390 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18392 add_AT_location_description (com_die, DW_AT_location, loc);
18393 loc = loc_list_from_tree (com_decl, 2);
18394 remove_AT (com_die, DW_AT_declaration);
18396 var_die = new_die (DW_TAG_variable, com_die, decl);
18397 add_name_and_src_coords_attributes (var_die, decl);
18398 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18399 TREE_THIS_VOLATILE (decl), context_die);
18400 add_AT_flag (var_die, DW_AT_external, 1);
18401 if (loc)
18403 if (off)
18405 /* Optimize the common case. */
18406 if (single_element_loc_list_p (loc)
18407 && loc->expr->dw_loc_opc == DW_OP_addr
18408 && loc->expr->dw_loc_next == NULL
18409 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18411 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18412 loc->expr->dw_loc_oprnd1.v.val_addr
18413 = plus_constant (GET_MODE (x), x, off);
18415 else
18416 loc_list_plus_const (loc, off);
18418 add_AT_location_description (var_die, DW_AT_location, loc);
18420 else if (DECL_EXTERNAL (decl))
18421 add_AT_flag (var_die, DW_AT_declaration, 1);
18422 equate_decl_number_to_die (decl, var_die);
18423 return;
18426 /* If the compiler emitted a definition for the DECL declaration
18427 and if we already emitted a DIE for it, don't emit a second
18428 DIE for it again. Allow re-declarations of DECLs that are
18429 inside functions, though. */
18430 if (old_die && declaration && !local_scope_p (context_die))
18431 return;
18433 /* For static data members, the declaration in the class is supposed
18434 to have DW_TAG_member tag; the specification should still be
18435 DW_TAG_variable referencing the DW_TAG_member DIE. */
18436 if (declaration && class_scope_p (context_die))
18437 var_die = new_die (DW_TAG_member, context_die, decl);
18438 else
18439 var_die = new_die (DW_TAG_variable, context_die, decl);
18441 origin_die = NULL;
18442 if (origin != NULL)
18443 origin_die = add_abstract_origin_attribute (var_die, origin);
18445 /* Loop unrolling can create multiple blocks that refer to the same
18446 static variable, so we must test for the DW_AT_declaration flag.
18448 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18449 copy decls and set the DECL_ABSTRACT flag on them instead of
18450 sharing them.
18452 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18454 ??? The declare_in_namespace support causes us to get two DIEs for one
18455 variable, both of which are declarations. We want to avoid considering
18456 one to be a specification, so we must test that this DIE is not a
18457 declaration. */
18458 else if (old_die && TREE_STATIC (decl) && ! declaration
18459 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18461 /* This is a definition of a C++ class level static. */
18462 add_AT_specification (var_die, old_die);
18463 specialization_p = true;
18464 if (DECL_NAME (decl))
18466 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18467 struct dwarf_file_data * file_index = lookup_filename (s.file);
18469 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18470 add_AT_file (var_die, DW_AT_decl_file, file_index);
18472 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18473 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18475 if (old_die->die_tag == DW_TAG_member)
18476 add_linkage_name (var_die, decl);
18479 else
18480 add_name_and_src_coords_attributes (var_die, decl);
18482 if ((origin == NULL && !specialization_p)
18483 || (origin != NULL
18484 && !DECL_ABSTRACT (decl_or_origin)
18485 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18486 decl_function_context
18487 (decl_or_origin))))
18489 tree type = TREE_TYPE (decl_or_origin);
18491 if (decl_by_reference_p (decl_or_origin))
18492 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18493 else
18494 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18495 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18498 if (origin == NULL && !specialization_p)
18500 if (TREE_PUBLIC (decl))
18501 add_AT_flag (var_die, DW_AT_external, 1);
18503 if (DECL_ARTIFICIAL (decl))
18504 add_AT_flag (var_die, DW_AT_artificial, 1);
18506 add_accessibility_attribute (var_die, decl);
18509 if (declaration)
18510 add_AT_flag (var_die, DW_AT_declaration, 1);
18512 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18513 equate_decl_number_to_die (decl, var_die);
18515 if (! declaration
18516 && (! DECL_ABSTRACT (decl_or_origin)
18517 /* Local static vars are shared between all clones/inlines,
18518 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18519 already set. */
18520 || (TREE_CODE (decl_or_origin) == VAR_DECL
18521 && TREE_STATIC (decl_or_origin)
18522 && DECL_RTL_SET_P (decl_or_origin)))
18523 /* When abstract origin already has DW_AT_location attribute, no need
18524 to add it again. */
18525 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18527 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18528 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18529 defer_location (decl_or_origin, var_die);
18530 else
18531 add_location_or_const_value_attribute (var_die, decl_or_origin,
18532 decl == NULL, DW_AT_location);
18533 add_pubname (decl_or_origin, var_die);
18535 else
18536 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18539 /* Generate a DIE to represent a named constant. */
18541 static void
18542 gen_const_die (tree decl, dw_die_ref context_die)
18544 dw_die_ref const_die;
18545 tree type = TREE_TYPE (decl);
18547 const_die = new_die (DW_TAG_constant, context_die, decl);
18548 add_name_and_src_coords_attributes (const_die, decl);
18549 add_type_attribute (const_die, type, 1, 0, context_die);
18550 if (TREE_PUBLIC (decl))
18551 add_AT_flag (const_die, DW_AT_external, 1);
18552 if (DECL_ARTIFICIAL (decl))
18553 add_AT_flag (const_die, DW_AT_artificial, 1);
18554 tree_add_const_value_attribute_for_decl (const_die, decl);
18557 /* Generate a DIE to represent a label identifier. */
18559 static void
18560 gen_label_die (tree decl, dw_die_ref context_die)
18562 tree origin = decl_ultimate_origin (decl);
18563 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18564 rtx insn;
18565 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18567 if (origin != NULL)
18568 add_abstract_origin_attribute (lbl_die, origin);
18569 else
18570 add_name_and_src_coords_attributes (lbl_die, decl);
18572 if (DECL_ABSTRACT (decl))
18573 equate_decl_number_to_die (decl, lbl_die);
18574 else
18576 insn = DECL_RTL_IF_SET (decl);
18578 /* Deleted labels are programmer specified labels which have been
18579 eliminated because of various optimizations. We still emit them
18580 here so that it is possible to put breakpoints on them. */
18581 if (insn
18582 && (LABEL_P (insn)
18583 || ((NOTE_P (insn)
18584 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18586 /* When optimization is enabled (via -O) some parts of the compiler
18587 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18588 represent source-level labels which were explicitly declared by
18589 the user. This really shouldn't be happening though, so catch
18590 it if it ever does happen. */
18591 gcc_assert (!INSN_DELETED_P (insn));
18593 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18594 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18596 else if (insn
18597 && NOTE_P (insn)
18598 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18599 && CODE_LABEL_NUMBER (insn) != -1)
18601 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18602 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18607 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18608 attributes to the DIE for a block STMT, to describe where the inlined
18609 function was called from. This is similar to add_src_coords_attributes. */
18611 static inline void
18612 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18614 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18616 if (dwarf_version >= 3 || !dwarf_strict)
18618 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18619 add_AT_unsigned (die, DW_AT_call_line, s.line);
18624 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18625 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18627 static inline void
18628 add_high_low_attributes (tree stmt, dw_die_ref die)
18630 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18632 if (BLOCK_FRAGMENT_CHAIN (stmt)
18633 && (dwarf_version >= 3 || !dwarf_strict))
18635 tree chain, superblock = NULL_TREE;
18636 dw_die_ref pdie;
18637 dw_attr_ref attr = NULL;
18639 if (inlined_function_outer_scope_p (stmt))
18641 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18642 BLOCK_NUMBER (stmt));
18643 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18646 /* Optimize duplicate .debug_ranges lists or even tails of
18647 lists. If this BLOCK has same ranges as its supercontext,
18648 lookup DW_AT_ranges attribute in the supercontext (and
18649 recursively so), verify that the ranges_table contains the
18650 right values and use it instead of adding a new .debug_range. */
18651 for (chain = stmt, pdie = die;
18652 BLOCK_SAME_RANGE (chain);
18653 chain = BLOCK_SUPERCONTEXT (chain))
18655 dw_attr_ref new_attr;
18657 pdie = pdie->die_parent;
18658 if (pdie == NULL)
18659 break;
18660 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18661 break;
18662 new_attr = get_AT (pdie, DW_AT_ranges);
18663 if (new_attr == NULL
18664 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18665 break;
18666 attr = new_attr;
18667 superblock = BLOCK_SUPERCONTEXT (chain);
18669 if (attr != NULL
18670 && (ranges_table[attr->dw_attr_val.v.val_offset
18671 / 2 / DWARF2_ADDR_SIZE].num
18672 == BLOCK_NUMBER (superblock))
18673 && BLOCK_FRAGMENT_CHAIN (superblock))
18675 unsigned long off = attr->dw_attr_val.v.val_offset
18676 / 2 / DWARF2_ADDR_SIZE;
18677 unsigned long supercnt = 0, thiscnt = 0;
18678 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18679 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18681 ++supercnt;
18682 gcc_checking_assert (ranges_table[off + supercnt].num
18683 == BLOCK_NUMBER (chain));
18685 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18686 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18687 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18688 ++thiscnt;
18689 gcc_assert (supercnt >= thiscnt);
18690 add_AT_range_list (die, DW_AT_ranges,
18691 ((off + supercnt - thiscnt)
18692 * 2 * DWARF2_ADDR_SIZE),
18693 false);
18694 return;
18697 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18699 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18702 add_ranges (chain);
18703 chain = BLOCK_FRAGMENT_CHAIN (chain);
18705 while (chain);
18706 add_ranges (NULL);
18708 else
18710 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18711 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18712 BLOCK_NUMBER (stmt));
18713 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18714 BLOCK_NUMBER (stmt));
18715 add_AT_low_high_pc (die, label, label_high, false);
18719 /* Generate a DIE for a lexical block. */
18721 static void
18722 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18724 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18726 if (call_arg_locations)
18728 if (block_map.length () <= BLOCK_NUMBER (stmt))
18729 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18730 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18733 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18734 add_high_low_attributes (stmt, stmt_die);
18736 decls_for_scope (stmt, stmt_die, depth);
18739 /* Generate a DIE for an inlined subprogram. */
18741 static void
18742 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18744 tree decl;
18746 /* The instance of function that is effectively being inlined shall not
18747 be abstract. */
18748 gcc_assert (! BLOCK_ABSTRACT (stmt));
18750 decl = block_ultimate_origin (stmt);
18752 /* Emit info for the abstract instance first, if we haven't yet. We
18753 must emit this even if the block is abstract, otherwise when we
18754 emit the block below (or elsewhere), we may end up trying to emit
18755 a die whose origin die hasn't been emitted, and crashing. */
18756 dwarf2out_abstract_function (decl);
18758 if (! BLOCK_ABSTRACT (stmt))
18760 dw_die_ref subr_die
18761 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18763 if (call_arg_locations)
18765 if (block_map.length () <= BLOCK_NUMBER (stmt))
18766 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18767 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18769 add_abstract_origin_attribute (subr_die, decl);
18770 if (TREE_ASM_WRITTEN (stmt))
18771 add_high_low_attributes (stmt, subr_die);
18772 add_call_src_coords_attributes (stmt, subr_die);
18774 decls_for_scope (stmt, subr_die, depth);
18775 current_function_has_inlines = 1;
18779 /* Generate a DIE for a field in a record, or structure. */
18781 static void
18782 gen_field_die (tree decl, dw_die_ref context_die)
18784 dw_die_ref decl_die;
18786 if (TREE_TYPE (decl) == error_mark_node)
18787 return;
18789 decl_die = new_die (DW_TAG_member, context_die, decl);
18790 add_name_and_src_coords_attributes (decl_die, decl);
18791 add_type_attribute (decl_die, member_declared_type (decl),
18792 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18793 context_die);
18795 if (DECL_BIT_FIELD_TYPE (decl))
18797 add_byte_size_attribute (decl_die, decl);
18798 add_bit_size_attribute (decl_die, decl);
18799 add_bit_offset_attribute (decl_die, decl);
18802 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18803 add_data_member_location_attribute (decl_die, decl);
18805 if (DECL_ARTIFICIAL (decl))
18806 add_AT_flag (decl_die, DW_AT_artificial, 1);
18808 add_accessibility_attribute (decl_die, decl);
18810 /* Equate decl number to die, so that we can look up this decl later on. */
18811 equate_decl_number_to_die (decl, decl_die);
18814 #if 0
18815 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18816 Use modified_type_die instead.
18817 We keep this code here just in case these types of DIEs may be needed to
18818 represent certain things in other languages (e.g. Pascal) someday. */
18820 static void
18821 gen_pointer_type_die (tree type, dw_die_ref context_die)
18823 dw_die_ref ptr_die
18824 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18826 equate_type_number_to_die (type, ptr_die);
18827 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18828 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18831 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18832 Use modified_type_die instead.
18833 We keep this code here just in case these types of DIEs may be needed to
18834 represent certain things in other languages (e.g. Pascal) someday. */
18836 static void
18837 gen_reference_type_die (tree type, dw_die_ref context_die)
18839 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18841 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18842 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18843 else
18844 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18846 equate_type_number_to_die (type, ref_die);
18847 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18848 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18850 #endif
18852 /* Generate a DIE for a pointer to a member type. */
18854 static void
18855 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18857 dw_die_ref ptr_die
18858 = new_die (DW_TAG_ptr_to_member_type,
18859 scope_die_for (type, context_die), type);
18861 equate_type_number_to_die (type, ptr_die);
18862 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18863 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18864 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18867 typedef const char *dchar_p; /* For DEF_VEC_P. */
18869 static char *producer_string;
18871 /* Return a heap allocated producer string including command line options
18872 if -grecord-gcc-switches. */
18874 static char *
18875 gen_producer_string (void)
18877 size_t j;
18878 vec<dchar_p> switches = vNULL;
18879 const char *language_string = lang_hooks.name;
18880 char *producer, *tail;
18881 const char *p;
18882 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18883 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18885 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18886 switch (save_decoded_options[j].opt_index)
18888 case OPT_o:
18889 case OPT_d:
18890 case OPT_dumpbase:
18891 case OPT_dumpdir:
18892 case OPT_auxbase:
18893 case OPT_auxbase_strip:
18894 case OPT_quiet:
18895 case OPT_version:
18896 case OPT_v:
18897 case OPT_w:
18898 case OPT_L:
18899 case OPT_D:
18900 case OPT_I:
18901 case OPT_U:
18902 case OPT_SPECIAL_unknown:
18903 case OPT_SPECIAL_ignore:
18904 case OPT_SPECIAL_program_name:
18905 case OPT_SPECIAL_input_file:
18906 case OPT_grecord_gcc_switches:
18907 case OPT_gno_record_gcc_switches:
18908 case OPT__output_pch_:
18909 case OPT_fdiagnostics_show_location_:
18910 case OPT_fdiagnostics_show_option:
18911 case OPT_fdiagnostics_show_caret:
18912 case OPT_fdiagnostics_color_:
18913 case OPT_fverbose_asm:
18914 case OPT____:
18915 case OPT__sysroot_:
18916 case OPT_nostdinc:
18917 case OPT_nostdinc__:
18918 /* Ignore these. */
18919 continue;
18920 default:
18921 if (cl_options[save_decoded_options[j].opt_index].flags
18922 & CL_NO_DWARF_RECORD)
18923 continue;
18924 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18925 == '-');
18926 switch (save_decoded_options[j].canonical_option[0][1])
18928 case 'M':
18929 case 'i':
18930 case 'W':
18931 continue;
18932 case 'f':
18933 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18934 "dump", 4) == 0)
18935 continue;
18936 break;
18937 default:
18938 break;
18940 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18941 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18942 break;
18945 producer = XNEWVEC (char, plen + 1 + len + 1);
18946 tail = producer;
18947 sprintf (tail, "%s %s", language_string, version_string);
18948 tail += plen;
18950 FOR_EACH_VEC_ELT (switches, j, p)
18952 len = strlen (p);
18953 *tail = ' ';
18954 memcpy (tail + 1, p, len);
18955 tail += len + 1;
18958 *tail = '\0';
18959 switches.release ();
18960 return producer;
18963 /* Generate the DIE for the compilation unit. */
18965 static dw_die_ref
18966 gen_compile_unit_die (const char *filename)
18968 dw_die_ref die;
18969 const char *language_string = lang_hooks.name;
18970 int language;
18972 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18974 if (filename)
18976 add_name_attribute (die, filename);
18977 /* Don't add cwd for <built-in>. */
18978 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18979 add_comp_dir_attribute (die);
18982 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
18984 /* If our producer is LTO try to figure out a common language to use
18985 from the global list of translation units. */
18986 if (strcmp (language_string, "GNU GIMPLE") == 0)
18988 unsigned i;
18989 tree t;
18990 const char *common_lang = NULL;
18992 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
18994 if (!TRANSLATION_UNIT_LANGUAGE (t))
18995 continue;
18996 if (!common_lang)
18997 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18998 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19000 else if (strncmp (common_lang, "GNU C", 5) == 0
19001 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19002 /* Mixing C and C++ is ok, use C++ in that case. */
19003 common_lang = "GNU C++";
19004 else
19006 /* Fall back to C. */
19007 common_lang = NULL;
19008 break;
19012 if (common_lang)
19013 language_string = common_lang;
19016 language = DW_LANG_C89;
19017 if (strcmp (language_string, "GNU C++") == 0)
19018 language = DW_LANG_C_plus_plus;
19019 else if (strcmp (language_string, "GNU F77") == 0)
19020 language = DW_LANG_Fortran77;
19021 else if (strcmp (language_string, "GNU Pascal") == 0)
19022 language = DW_LANG_Pascal83;
19023 else if (dwarf_version >= 3 || !dwarf_strict)
19025 if (strcmp (language_string, "GNU Ada") == 0)
19026 language = DW_LANG_Ada95;
19027 else if (strcmp (language_string, "GNU Fortran") == 0)
19028 language = DW_LANG_Fortran95;
19029 else if (strcmp (language_string, "GNU Java") == 0)
19030 language = DW_LANG_Java;
19031 else if (strcmp (language_string, "GNU Objective-C") == 0)
19032 language = DW_LANG_ObjC;
19033 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19034 language = DW_LANG_ObjC_plus_plus;
19035 else if (dwarf_version >= 5 || !dwarf_strict)
19037 if (strcmp (language_string, "GNU Go") == 0)
19038 language = DW_LANG_Go;
19041 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19042 else if (strcmp (language_string, "GNU Fortran") == 0)
19043 language = DW_LANG_Fortran90;
19045 add_AT_unsigned (die, DW_AT_language, language);
19047 switch (language)
19049 case DW_LANG_Fortran77:
19050 case DW_LANG_Fortran90:
19051 case DW_LANG_Fortran95:
19052 /* Fortran has case insensitive identifiers and the front-end
19053 lowercases everything. */
19054 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19055 break;
19056 default:
19057 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19058 break;
19060 return die;
19063 /* Generate the DIE for a base class. */
19065 static void
19066 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19068 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19070 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19071 add_data_member_location_attribute (die, binfo);
19073 if (BINFO_VIRTUAL_P (binfo))
19074 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19076 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19077 children, otherwise the default is DW_ACCESS_public. In DWARF2
19078 the default has always been DW_ACCESS_private. */
19079 if (access == access_public_node)
19081 if (dwarf_version == 2
19082 || context_die->die_tag == DW_TAG_class_type)
19083 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19085 else if (access == access_protected_node)
19086 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19087 else if (dwarf_version > 2
19088 && context_die->die_tag != DW_TAG_class_type)
19089 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19092 /* Generate a DIE for a class member. */
19094 static void
19095 gen_member_die (tree type, dw_die_ref context_die)
19097 tree member;
19098 tree binfo = TYPE_BINFO (type);
19099 dw_die_ref child;
19101 /* If this is not an incomplete type, output descriptions of each of its
19102 members. Note that as we output the DIEs necessary to represent the
19103 members of this record or union type, we will also be trying to output
19104 DIEs to represent the *types* of those members. However the `type'
19105 function (above) will specifically avoid generating type DIEs for member
19106 types *within* the list of member DIEs for this (containing) type except
19107 for those types (of members) which are explicitly marked as also being
19108 members of this (containing) type themselves. The g++ front- end can
19109 force any given type to be treated as a member of some other (containing)
19110 type by setting the TYPE_CONTEXT of the given (member) type to point to
19111 the TREE node representing the appropriate (containing) type. */
19113 /* First output info about the base classes. */
19114 if (binfo)
19116 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19117 int i;
19118 tree base;
19120 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19121 gen_inheritance_die (base,
19122 (accesses ? (*accesses)[i] : access_public_node),
19123 context_die);
19126 /* Now output info about the data members and type members. */
19127 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19129 /* If we thought we were generating minimal debug info for TYPE
19130 and then changed our minds, some of the member declarations
19131 may have already been defined. Don't define them again, but
19132 do put them in the right order. */
19134 child = lookup_decl_die (member);
19135 if (child)
19136 splice_child_die (context_die, child);
19137 else
19138 gen_decl_die (member, NULL, context_die);
19141 /* Now output info about the function members (if any). */
19142 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19144 /* Don't include clones in the member list. */
19145 if (DECL_ABSTRACT_ORIGIN (member))
19146 continue;
19148 child = lookup_decl_die (member);
19149 if (child)
19150 splice_child_die (context_die, child);
19151 else
19152 gen_decl_die (member, NULL, context_die);
19156 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19157 is set, we pretend that the type was never defined, so we only get the
19158 member DIEs needed by later specification DIEs. */
19160 static void
19161 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19162 enum debug_info_usage usage)
19164 dw_die_ref type_die = lookup_type_die (type);
19165 dw_die_ref scope_die = 0;
19166 int nested = 0;
19167 int complete = (TYPE_SIZE (type)
19168 && (! TYPE_STUB_DECL (type)
19169 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19170 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19171 complete = complete && should_emit_struct_debug (type, usage);
19173 if (type_die && ! complete)
19174 return;
19176 if (TYPE_CONTEXT (type) != NULL_TREE
19177 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19178 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19179 nested = 1;
19181 scope_die = scope_die_for (type, context_die);
19183 /* Generate child dies for template paramaters. */
19184 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19185 schedule_generic_params_dies_gen (type);
19187 if (! type_die || (nested && is_cu_die (scope_die)))
19188 /* First occurrence of type or toplevel definition of nested class. */
19190 dw_die_ref old_die = type_die;
19192 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19193 ? record_type_tag (type) : DW_TAG_union_type,
19194 scope_die, type);
19195 equate_type_number_to_die (type, type_die);
19196 if (old_die)
19197 add_AT_specification (type_die, old_die);
19198 else
19199 add_name_attribute (type_die, type_tag (type));
19201 else
19202 remove_AT (type_die, DW_AT_declaration);
19204 /* If this type has been completed, then give it a byte_size attribute and
19205 then give a list of members. */
19206 if (complete && !ns_decl)
19208 /* Prevent infinite recursion in cases where the type of some member of
19209 this type is expressed in terms of this type itself. */
19210 TREE_ASM_WRITTEN (type) = 1;
19211 add_byte_size_attribute (type_die, type);
19212 if (TYPE_STUB_DECL (type) != NULL_TREE)
19214 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19215 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19218 /* If the first reference to this type was as the return type of an
19219 inline function, then it may not have a parent. Fix this now. */
19220 if (type_die->die_parent == NULL)
19221 add_child_die (scope_die, type_die);
19223 push_decl_scope (type);
19224 gen_member_die (type, type_die);
19225 pop_decl_scope ();
19227 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19228 if (TYPE_ARTIFICIAL (type))
19229 add_AT_flag (type_die, DW_AT_artificial, 1);
19231 /* GNU extension: Record what type our vtable lives in. */
19232 if (TYPE_VFIELD (type))
19234 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19236 gen_type_die (vtype, context_die);
19237 add_AT_die_ref (type_die, DW_AT_containing_type,
19238 lookup_type_die (vtype));
19241 else
19243 add_AT_flag (type_die, DW_AT_declaration, 1);
19245 /* We don't need to do this for function-local types. */
19246 if (TYPE_STUB_DECL (type)
19247 && ! decl_function_context (TYPE_STUB_DECL (type)))
19248 vec_safe_push (incomplete_types, type);
19251 if (get_AT (type_die, DW_AT_name))
19252 add_pubtype (type, type_die);
19255 /* Generate a DIE for a subroutine _type_. */
19257 static void
19258 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19260 tree return_type = TREE_TYPE (type);
19261 dw_die_ref subr_die
19262 = new_die (DW_TAG_subroutine_type,
19263 scope_die_for (type, context_die), type);
19265 equate_type_number_to_die (type, subr_die);
19266 add_prototyped_attribute (subr_die, type);
19267 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19268 gen_formal_types_die (type, subr_die);
19270 if (get_AT (subr_die, DW_AT_name))
19271 add_pubtype (type, subr_die);
19274 /* Generate a DIE for a type definition. */
19276 static void
19277 gen_typedef_die (tree decl, dw_die_ref context_die)
19279 dw_die_ref type_die;
19280 tree origin;
19282 if (TREE_ASM_WRITTEN (decl))
19283 return;
19285 TREE_ASM_WRITTEN (decl) = 1;
19286 type_die = new_die (DW_TAG_typedef, context_die, decl);
19287 origin = decl_ultimate_origin (decl);
19288 if (origin != NULL)
19289 add_abstract_origin_attribute (type_die, origin);
19290 else
19292 tree type;
19294 add_name_and_src_coords_attributes (type_die, decl);
19295 if (DECL_ORIGINAL_TYPE (decl))
19297 type = DECL_ORIGINAL_TYPE (decl);
19299 gcc_assert (type != TREE_TYPE (decl));
19300 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19302 else
19304 type = TREE_TYPE (decl);
19306 if (is_naming_typedef_decl (TYPE_NAME (type)))
19308 /* Here, we are in the case of decl being a typedef naming
19309 an anonymous type, e.g:
19310 typedef struct {...} foo;
19311 In that case TREE_TYPE (decl) is not a typedef variant
19312 type and TYPE_NAME of the anonymous type is set to the
19313 TYPE_DECL of the typedef. This construct is emitted by
19314 the C++ FE.
19316 TYPE is the anonymous struct named by the typedef
19317 DECL. As we need the DW_AT_type attribute of the
19318 DW_TAG_typedef to point to the DIE of TYPE, let's
19319 generate that DIE right away. add_type_attribute
19320 called below will then pick (via lookup_type_die) that
19321 anonymous struct DIE. */
19322 if (!TREE_ASM_WRITTEN (type))
19323 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19325 /* This is a GNU Extension. We are adding a
19326 DW_AT_linkage_name attribute to the DIE of the
19327 anonymous struct TYPE. The value of that attribute
19328 is the name of the typedef decl naming the anonymous
19329 struct. This greatly eases the work of consumers of
19330 this debug info. */
19331 add_linkage_attr (lookup_type_die (type), decl);
19335 add_type_attribute (type_die, type, TREE_READONLY (decl),
19336 TREE_THIS_VOLATILE (decl), context_die);
19338 if (is_naming_typedef_decl (decl))
19339 /* We want that all subsequent calls to lookup_type_die with
19340 TYPE in argument yield the DW_TAG_typedef we have just
19341 created. */
19342 equate_type_number_to_die (type, type_die);
19344 add_accessibility_attribute (type_die, decl);
19347 if (DECL_ABSTRACT (decl))
19348 equate_decl_number_to_die (decl, type_die);
19350 if (get_AT (type_die, DW_AT_name))
19351 add_pubtype (decl, type_die);
19354 /* Generate a DIE for a struct, class, enum or union type. */
19356 static void
19357 gen_tagged_type_die (tree type,
19358 dw_die_ref context_die,
19359 enum debug_info_usage usage)
19361 int need_pop;
19363 if (type == NULL_TREE
19364 || !is_tagged_type (type))
19365 return;
19367 /* If this is a nested type whose containing class hasn't been written
19368 out yet, writing it out will cover this one, too. This does not apply
19369 to instantiations of member class templates; they need to be added to
19370 the containing class as they are generated. FIXME: This hurts the
19371 idea of combining type decls from multiple TUs, since we can't predict
19372 what set of template instantiations we'll get. */
19373 if (TYPE_CONTEXT (type)
19374 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19375 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19377 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19379 if (TREE_ASM_WRITTEN (type))
19380 return;
19382 /* If that failed, attach ourselves to the stub. */
19383 push_decl_scope (TYPE_CONTEXT (type));
19384 context_die = lookup_type_die (TYPE_CONTEXT (type));
19385 need_pop = 1;
19387 else if (TYPE_CONTEXT (type) != NULL_TREE
19388 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19390 /* If this type is local to a function that hasn't been written
19391 out yet, use a NULL context for now; it will be fixed up in
19392 decls_for_scope. */
19393 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19394 /* A declaration DIE doesn't count; nested types need to go in the
19395 specification. */
19396 if (context_die && is_declaration_die (context_die))
19397 context_die = NULL;
19398 need_pop = 0;
19400 else
19402 context_die = declare_in_namespace (type, context_die);
19403 need_pop = 0;
19406 if (TREE_CODE (type) == ENUMERAL_TYPE)
19408 /* This might have been written out by the call to
19409 declare_in_namespace. */
19410 if (!TREE_ASM_WRITTEN (type))
19411 gen_enumeration_type_die (type, context_die);
19413 else
19414 gen_struct_or_union_type_die (type, context_die, usage);
19416 if (need_pop)
19417 pop_decl_scope ();
19419 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19420 it up if it is ever completed. gen_*_type_die will set it for us
19421 when appropriate. */
19424 /* Generate a type description DIE. */
19426 static void
19427 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19428 enum debug_info_usage usage)
19430 struct array_descr_info info;
19432 if (type == NULL_TREE || type == error_mark_node)
19433 return;
19435 if (TYPE_NAME (type) != NULL_TREE
19436 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19437 && is_redundant_typedef (TYPE_NAME (type))
19438 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19439 /* The DECL of this type is a typedef we don't want to emit debug
19440 info for but we want debug info for its underlying typedef.
19441 This can happen for e.g, the injected-class-name of a C++
19442 type. */
19443 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19445 /* If TYPE is a typedef type variant, let's generate debug info
19446 for the parent typedef which TYPE is a type of. */
19447 if (typedef_variant_p (type))
19449 if (TREE_ASM_WRITTEN (type))
19450 return;
19452 /* Prevent broken recursion; we can't hand off to the same type. */
19453 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19455 /* Give typedefs the right scope. */
19456 context_die = scope_die_for (type, context_die);
19458 TREE_ASM_WRITTEN (type) = 1;
19460 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19461 return;
19464 /* If type is an anonymous tagged type named by a typedef, let's
19465 generate debug info for the typedef. */
19466 if (is_naming_typedef_decl (TYPE_NAME (type)))
19468 /* Use the DIE of the containing namespace as the parent DIE of
19469 the type description DIE we want to generate. */
19470 if (DECL_CONTEXT (TYPE_NAME (type))
19471 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19472 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19474 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19475 return;
19478 /* If this is an array type with hidden descriptor, handle it first. */
19479 if (!TREE_ASM_WRITTEN (type)
19480 && lang_hooks.types.get_array_descr_info
19481 && lang_hooks.types.get_array_descr_info (type, &info)
19482 && (dwarf_version >= 3 || !dwarf_strict))
19484 gen_descr_array_type_die (type, &info, context_die);
19485 TREE_ASM_WRITTEN (type) = 1;
19486 return;
19489 /* We are going to output a DIE to represent the unqualified version
19490 of this type (i.e. without any const or volatile qualifiers) so
19491 get the main variant (i.e. the unqualified version) of this type
19492 now. (Vectors are special because the debugging info is in the
19493 cloned type itself). */
19494 if (TREE_CODE (type) != VECTOR_TYPE)
19495 type = type_main_variant (type);
19497 if (TREE_ASM_WRITTEN (type))
19498 return;
19500 switch (TREE_CODE (type))
19502 case ERROR_MARK:
19503 break;
19505 case POINTER_TYPE:
19506 case REFERENCE_TYPE:
19507 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19508 ensures that the gen_type_die recursion will terminate even if the
19509 type is recursive. Recursive types are possible in Ada. */
19510 /* ??? We could perhaps do this for all types before the switch
19511 statement. */
19512 TREE_ASM_WRITTEN (type) = 1;
19514 /* For these types, all that is required is that we output a DIE (or a
19515 set of DIEs) to represent the "basis" type. */
19516 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19517 DINFO_USAGE_IND_USE);
19518 break;
19520 case OFFSET_TYPE:
19521 /* This code is used for C++ pointer-to-data-member types.
19522 Output a description of the relevant class type. */
19523 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19524 DINFO_USAGE_IND_USE);
19526 /* Output a description of the type of the object pointed to. */
19527 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19528 DINFO_USAGE_IND_USE);
19530 /* Now output a DIE to represent this pointer-to-data-member type
19531 itself. */
19532 gen_ptr_to_mbr_type_die (type, context_die);
19533 break;
19535 case FUNCTION_TYPE:
19536 /* Force out return type (in case it wasn't forced out already). */
19537 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19538 DINFO_USAGE_DIR_USE);
19539 gen_subroutine_type_die (type, context_die);
19540 break;
19542 case METHOD_TYPE:
19543 /* Force out return type (in case it wasn't forced out already). */
19544 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19545 DINFO_USAGE_DIR_USE);
19546 gen_subroutine_type_die (type, context_die);
19547 break;
19549 case ARRAY_TYPE:
19550 gen_array_type_die (type, context_die);
19551 break;
19553 case VECTOR_TYPE:
19554 gen_array_type_die (type, context_die);
19555 break;
19557 case ENUMERAL_TYPE:
19558 case RECORD_TYPE:
19559 case UNION_TYPE:
19560 case QUAL_UNION_TYPE:
19561 gen_tagged_type_die (type, context_die, usage);
19562 return;
19564 case VOID_TYPE:
19565 case INTEGER_TYPE:
19566 case REAL_TYPE:
19567 case FIXED_POINT_TYPE:
19568 case COMPLEX_TYPE:
19569 case BOOLEAN_TYPE:
19570 /* No DIEs needed for fundamental types. */
19571 break;
19573 case NULLPTR_TYPE:
19574 case LANG_TYPE:
19575 /* Just use DW_TAG_unspecified_type. */
19577 dw_die_ref type_die = lookup_type_die (type);
19578 if (type_die == NULL)
19580 tree name = TYPE_NAME (type);
19581 if (TREE_CODE (name) == TYPE_DECL)
19582 name = DECL_NAME (name);
19583 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19584 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19585 equate_type_number_to_die (type, type_die);
19588 break;
19590 default:
19591 gcc_unreachable ();
19594 TREE_ASM_WRITTEN (type) = 1;
19597 static void
19598 gen_type_die (tree type, dw_die_ref context_die)
19600 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19603 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19604 things which are local to the given block. */
19606 static void
19607 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19609 int must_output_die = 0;
19610 bool inlined_func;
19612 /* Ignore blocks that are NULL. */
19613 if (stmt == NULL_TREE)
19614 return;
19616 inlined_func = inlined_function_outer_scope_p (stmt);
19618 /* If the block is one fragment of a non-contiguous block, do not
19619 process the variables, since they will have been done by the
19620 origin block. Do process subblocks. */
19621 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19623 tree sub;
19625 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19626 gen_block_die (sub, context_die, depth + 1);
19628 return;
19631 /* Determine if we need to output any Dwarf DIEs at all to represent this
19632 block. */
19633 if (inlined_func)
19634 /* The outer scopes for inlinings *must* always be represented. We
19635 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19636 must_output_die = 1;
19637 else
19639 /* Determine if this block directly contains any "significant"
19640 local declarations which we will need to output DIEs for. */
19641 if (debug_info_level > DINFO_LEVEL_TERSE)
19642 /* We are not in terse mode so *any* local declaration counts
19643 as being a "significant" one. */
19644 must_output_die = ((BLOCK_VARS (stmt) != NULL
19645 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19646 && (TREE_USED (stmt)
19647 || TREE_ASM_WRITTEN (stmt)
19648 || BLOCK_ABSTRACT (stmt)));
19649 else if ((TREE_USED (stmt)
19650 || TREE_ASM_WRITTEN (stmt)
19651 || BLOCK_ABSTRACT (stmt))
19652 && !dwarf2out_ignore_block (stmt))
19653 must_output_die = 1;
19656 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19657 DIE for any block which contains no significant local declarations at
19658 all. Rather, in such cases we just call `decls_for_scope' so that any
19659 needed Dwarf info for any sub-blocks will get properly generated. Note
19660 that in terse mode, our definition of what constitutes a "significant"
19661 local declaration gets restricted to include only inlined function
19662 instances and local (nested) function definitions. */
19663 if (must_output_die)
19665 if (inlined_func)
19667 /* If STMT block is abstract, that means we have been called
19668 indirectly from dwarf2out_abstract_function.
19669 That function rightfully marks the descendent blocks (of
19670 the abstract function it is dealing with) as being abstract,
19671 precisely to prevent us from emitting any
19672 DW_TAG_inlined_subroutine DIE as a descendent
19673 of an abstract function instance. So in that case, we should
19674 not call gen_inlined_subroutine_die.
19676 Later though, when cgraph asks dwarf2out to emit info
19677 for the concrete instance of the function decl into which
19678 the concrete instance of STMT got inlined, the later will lead
19679 to the generation of a DW_TAG_inlined_subroutine DIE. */
19680 if (! BLOCK_ABSTRACT (stmt))
19681 gen_inlined_subroutine_die (stmt, context_die, depth);
19683 else
19684 gen_lexical_block_die (stmt, context_die, depth);
19686 else
19687 decls_for_scope (stmt, context_die, depth);
19690 /* Process variable DECL (or variable with origin ORIGIN) within
19691 block STMT and add it to CONTEXT_DIE. */
19692 static void
19693 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19695 dw_die_ref die;
19696 tree decl_or_origin = decl ? decl : origin;
19698 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19699 die = lookup_decl_die (decl_or_origin);
19700 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19701 && TYPE_DECL_IS_STUB (decl_or_origin))
19702 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19703 else
19704 die = NULL;
19706 if (die != NULL && die->die_parent == NULL)
19707 add_child_die (context_die, die);
19708 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19709 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19710 stmt, context_die);
19711 else
19712 gen_decl_die (decl, origin, context_die);
19715 /* Generate all of the decls declared within a given scope and (recursively)
19716 all of its sub-blocks. */
19718 static void
19719 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19721 tree decl;
19722 unsigned int i;
19723 tree subblocks;
19725 /* Ignore NULL blocks. */
19726 if (stmt == NULL_TREE)
19727 return;
19729 /* Output the DIEs to represent all of the data objects and typedefs
19730 declared directly within this block but not within any nested
19731 sub-blocks. Also, nested function and tag DIEs have been
19732 generated with a parent of NULL; fix that up now. */
19733 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19734 process_scope_var (stmt, decl, NULL_TREE, context_die);
19735 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19736 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19737 context_die);
19739 /* If we're at -g1, we're not interested in subblocks. */
19740 if (debug_info_level <= DINFO_LEVEL_TERSE)
19741 return;
19743 /* Output the DIEs to represent all sub-blocks (and the items declared
19744 therein) of this block. */
19745 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19746 subblocks != NULL;
19747 subblocks = BLOCK_CHAIN (subblocks))
19748 gen_block_die (subblocks, context_die, depth + 1);
19751 /* Is this a typedef we can avoid emitting? */
19753 static inline int
19754 is_redundant_typedef (const_tree decl)
19756 if (TYPE_DECL_IS_STUB (decl))
19757 return 1;
19759 if (DECL_ARTIFICIAL (decl)
19760 && DECL_CONTEXT (decl)
19761 && is_tagged_type (DECL_CONTEXT (decl))
19762 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19763 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19764 /* Also ignore the artificial member typedef for the class name. */
19765 return 1;
19767 return 0;
19770 /* Return TRUE if TYPE is a typedef that names a type for linkage
19771 purposes. This kind of typedefs is produced by the C++ FE for
19772 constructs like:
19774 typedef struct {...} foo;
19776 In that case, there is no typedef variant type produced for foo.
19777 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19778 struct type. */
19780 static bool
19781 is_naming_typedef_decl (const_tree decl)
19783 if (decl == NULL_TREE
19784 || TREE_CODE (decl) != TYPE_DECL
19785 || !is_tagged_type (TREE_TYPE (decl))
19786 || DECL_IS_BUILTIN (decl)
19787 || is_redundant_typedef (decl)
19788 /* It looks like Ada produces TYPE_DECLs that are very similar
19789 to C++ naming typedefs but that have different
19790 semantics. Let's be specific to c++ for now. */
19791 || !is_cxx ())
19792 return FALSE;
19794 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19795 && TYPE_NAME (TREE_TYPE (decl)) == decl
19796 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19797 != TYPE_NAME (TREE_TYPE (decl))));
19800 /* Returns the DIE for a context. */
19802 static inline dw_die_ref
19803 get_context_die (tree context)
19805 if (context)
19807 /* Find die that represents this context. */
19808 if (TYPE_P (context))
19810 context = TYPE_MAIN_VARIANT (context);
19811 return strip_naming_typedef (context, force_type_die (context));
19813 else
19814 return force_decl_die (context);
19816 return comp_unit_die ();
19819 /* Returns the DIE for decl. A DIE will always be returned. */
19821 static dw_die_ref
19822 force_decl_die (tree decl)
19824 dw_die_ref decl_die;
19825 unsigned saved_external_flag;
19826 tree save_fn = NULL_TREE;
19827 decl_die = lookup_decl_die (decl);
19828 if (!decl_die)
19830 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19832 decl_die = lookup_decl_die (decl);
19833 if (decl_die)
19834 return decl_die;
19836 switch (TREE_CODE (decl))
19838 case FUNCTION_DECL:
19839 /* Clear current_function_decl, so that gen_subprogram_die thinks
19840 that this is a declaration. At this point, we just want to force
19841 declaration die. */
19842 save_fn = current_function_decl;
19843 current_function_decl = NULL_TREE;
19844 gen_subprogram_die (decl, context_die);
19845 current_function_decl = save_fn;
19846 break;
19848 case VAR_DECL:
19849 /* Set external flag to force declaration die. Restore it after
19850 gen_decl_die() call. */
19851 saved_external_flag = DECL_EXTERNAL (decl);
19852 DECL_EXTERNAL (decl) = 1;
19853 gen_decl_die (decl, NULL, context_die);
19854 DECL_EXTERNAL (decl) = saved_external_flag;
19855 break;
19857 case NAMESPACE_DECL:
19858 if (dwarf_version >= 3 || !dwarf_strict)
19859 dwarf2out_decl (decl);
19860 else
19861 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19862 decl_die = comp_unit_die ();
19863 break;
19865 case TRANSLATION_UNIT_DECL:
19866 decl_die = comp_unit_die ();
19867 break;
19869 default:
19870 gcc_unreachable ();
19873 /* We should be able to find the DIE now. */
19874 if (!decl_die)
19875 decl_die = lookup_decl_die (decl);
19876 gcc_assert (decl_die);
19879 return decl_die;
19882 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19883 always returned. */
19885 static dw_die_ref
19886 force_type_die (tree type)
19888 dw_die_ref type_die;
19890 type_die = lookup_type_die (type);
19891 if (!type_die)
19893 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19895 type_die = modified_type_die (type, TYPE_READONLY (type),
19896 TYPE_VOLATILE (type), context_die);
19897 gcc_assert (type_die);
19899 return type_die;
19902 /* Force out any required namespaces to be able to output DECL,
19903 and return the new context_die for it, if it's changed. */
19905 static dw_die_ref
19906 setup_namespace_context (tree thing, dw_die_ref context_die)
19908 tree context = (DECL_P (thing)
19909 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19910 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19911 /* Force out the namespace. */
19912 context_die = force_decl_die (context);
19914 return context_die;
19917 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19918 type) within its namespace, if appropriate.
19920 For compatibility with older debuggers, namespace DIEs only contain
19921 declarations; all definitions are emitted at CU scope. */
19923 static dw_die_ref
19924 declare_in_namespace (tree thing, dw_die_ref context_die)
19926 dw_die_ref ns_context;
19928 if (debug_info_level <= DINFO_LEVEL_TERSE)
19929 return context_die;
19931 /* If this decl is from an inlined function, then don't try to emit it in its
19932 namespace, as we will get confused. It would have already been emitted
19933 when the abstract instance of the inline function was emitted anyways. */
19934 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19935 return context_die;
19937 ns_context = setup_namespace_context (thing, context_die);
19939 if (ns_context != context_die)
19941 if (is_fortran ())
19942 return ns_context;
19943 if (DECL_P (thing))
19944 gen_decl_die (thing, NULL, ns_context);
19945 else
19946 gen_type_die (thing, ns_context);
19948 return context_die;
19951 /* Generate a DIE for a namespace or namespace alias. */
19953 static void
19954 gen_namespace_die (tree decl, dw_die_ref context_die)
19956 dw_die_ref namespace_die;
19958 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19959 they are an alias of. */
19960 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19962 /* Output a real namespace or module. */
19963 context_die = setup_namespace_context (decl, comp_unit_die ());
19964 namespace_die = new_die (is_fortran ()
19965 ? DW_TAG_module : DW_TAG_namespace,
19966 context_die, decl);
19967 /* For Fortran modules defined in different CU don't add src coords. */
19968 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19970 const char *name = dwarf2_name (decl, 0);
19971 if (name)
19972 add_name_attribute (namespace_die, name);
19974 else
19975 add_name_and_src_coords_attributes (namespace_die, decl);
19976 if (DECL_EXTERNAL (decl))
19977 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19978 equate_decl_number_to_die (decl, namespace_die);
19980 else
19982 /* Output a namespace alias. */
19984 /* Force out the namespace we are an alias of, if necessary. */
19985 dw_die_ref origin_die
19986 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19988 if (DECL_FILE_SCOPE_P (decl)
19989 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19990 context_die = setup_namespace_context (decl, comp_unit_die ());
19991 /* Now create the namespace alias DIE. */
19992 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19993 add_name_and_src_coords_attributes (namespace_die, decl);
19994 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19995 equate_decl_number_to_die (decl, namespace_die);
19997 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
19998 if (want_pubnames ())
19999 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20002 /* Generate Dwarf debug information for a decl described by DECL.
20003 The return value is currently only meaningful for PARM_DECLs,
20004 for all other decls it returns NULL. */
20006 static dw_die_ref
20007 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20009 tree decl_or_origin = decl ? decl : origin;
20010 tree class_origin = NULL, ultimate_origin;
20012 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20013 return NULL;
20015 switch (TREE_CODE (decl_or_origin))
20017 case ERROR_MARK:
20018 break;
20020 case CONST_DECL:
20021 if (!is_fortran () && !is_ada ())
20023 /* The individual enumerators of an enum type get output when we output
20024 the Dwarf representation of the relevant enum type itself. */
20025 break;
20028 /* Emit its type. */
20029 gen_type_die (TREE_TYPE (decl), context_die);
20031 /* And its containing namespace. */
20032 context_die = declare_in_namespace (decl, context_die);
20034 gen_const_die (decl, context_die);
20035 break;
20037 case FUNCTION_DECL:
20038 /* Don't output any DIEs to represent mere function declarations,
20039 unless they are class members or explicit block externs. */
20040 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20041 && DECL_FILE_SCOPE_P (decl_or_origin)
20042 && (current_function_decl == NULL_TREE
20043 || DECL_ARTIFICIAL (decl_or_origin)))
20044 break;
20046 #if 0
20047 /* FIXME */
20048 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20049 on local redeclarations of global functions. That seems broken. */
20050 if (current_function_decl != decl)
20051 /* This is only a declaration. */;
20052 #endif
20054 /* If we're emitting a clone, emit info for the abstract instance. */
20055 if (origin || DECL_ORIGIN (decl) != decl)
20056 dwarf2out_abstract_function (origin
20057 ? DECL_ORIGIN (origin)
20058 : DECL_ABSTRACT_ORIGIN (decl));
20060 /* If we're emitting an out-of-line copy of an inline function,
20061 emit info for the abstract instance and set up to refer to it. */
20062 else if (cgraph_function_possibly_inlined_p (decl)
20063 && ! DECL_ABSTRACT (decl)
20064 && ! class_or_namespace_scope_p (context_die)
20065 /* dwarf2out_abstract_function won't emit a die if this is just
20066 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20067 that case, because that works only if we have a die. */
20068 && DECL_INITIAL (decl) != NULL_TREE)
20070 dwarf2out_abstract_function (decl);
20071 set_decl_origin_self (decl);
20074 /* Otherwise we're emitting the primary DIE for this decl. */
20075 else if (debug_info_level > DINFO_LEVEL_TERSE)
20077 /* Before we describe the FUNCTION_DECL itself, make sure that we
20078 have its containing type. */
20079 if (!origin)
20080 origin = decl_class_context (decl);
20081 if (origin != NULL_TREE)
20082 gen_type_die (origin, context_die);
20084 /* And its return type. */
20085 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20087 /* And its virtual context. */
20088 if (DECL_VINDEX (decl) != NULL_TREE)
20089 gen_type_die (DECL_CONTEXT (decl), context_die);
20091 /* Make sure we have a member DIE for decl. */
20092 if (origin != NULL_TREE)
20093 gen_type_die_for_member (origin, decl, context_die);
20095 /* And its containing namespace. */
20096 context_die = declare_in_namespace (decl, context_die);
20099 /* Now output a DIE to represent the function itself. */
20100 if (decl)
20101 gen_subprogram_die (decl, context_die);
20102 break;
20104 case TYPE_DECL:
20105 /* If we are in terse mode, don't generate any DIEs to represent any
20106 actual typedefs. */
20107 if (debug_info_level <= DINFO_LEVEL_TERSE)
20108 break;
20110 /* In the special case of a TYPE_DECL node representing the declaration
20111 of some type tag, if the given TYPE_DECL is marked as having been
20112 instantiated from some other (original) TYPE_DECL node (e.g. one which
20113 was generated within the original definition of an inline function) we
20114 used to generate a special (abbreviated) DW_TAG_structure_type,
20115 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20116 should be actually referencing those DIEs, as variable DIEs with that
20117 type would be emitted already in the abstract origin, so it was always
20118 removed during unused type prunning. Don't add anything in this
20119 case. */
20120 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20121 break;
20123 if (is_redundant_typedef (decl))
20124 gen_type_die (TREE_TYPE (decl), context_die);
20125 else
20126 /* Output a DIE to represent the typedef itself. */
20127 gen_typedef_die (decl, context_die);
20128 break;
20130 case LABEL_DECL:
20131 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20132 gen_label_die (decl, context_die);
20133 break;
20135 case VAR_DECL:
20136 case RESULT_DECL:
20137 /* If we are in terse mode, don't generate any DIEs to represent any
20138 variable declarations or definitions. */
20139 if (debug_info_level <= DINFO_LEVEL_TERSE)
20140 break;
20142 /* Output any DIEs that are needed to specify the type of this data
20143 object. */
20144 if (decl_by_reference_p (decl_or_origin))
20145 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20146 else
20147 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20149 /* And its containing type. */
20150 class_origin = decl_class_context (decl_or_origin);
20151 if (class_origin != NULL_TREE)
20152 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20154 /* And its containing namespace. */
20155 context_die = declare_in_namespace (decl_or_origin, context_die);
20157 /* Now output the DIE to represent the data object itself. This gets
20158 complicated because of the possibility that the VAR_DECL really
20159 represents an inlined instance of a formal parameter for an inline
20160 function. */
20161 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20162 if (ultimate_origin != NULL_TREE
20163 && TREE_CODE (ultimate_origin) == PARM_DECL)
20164 gen_formal_parameter_die (decl, origin,
20165 true /* Emit name attribute. */,
20166 context_die);
20167 else
20168 gen_variable_die (decl, origin, context_die);
20169 break;
20171 case FIELD_DECL:
20172 /* Ignore the nameless fields that are used to skip bits but handle C++
20173 anonymous unions and structs. */
20174 if (DECL_NAME (decl) != NULL_TREE
20175 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20176 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20178 gen_type_die (member_declared_type (decl), context_die);
20179 gen_field_die (decl, context_die);
20181 break;
20183 case PARM_DECL:
20184 if (DECL_BY_REFERENCE (decl_or_origin))
20185 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20186 else
20187 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20188 return gen_formal_parameter_die (decl, origin,
20189 true /* Emit name attribute. */,
20190 context_die);
20192 case NAMESPACE_DECL:
20193 case IMPORTED_DECL:
20194 if (dwarf_version >= 3 || !dwarf_strict)
20195 gen_namespace_die (decl, context_die);
20196 break;
20198 default:
20199 /* Probably some frontend-internal decl. Assume we don't care. */
20200 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20201 break;
20204 return NULL;
20207 /* Output debug information for global decl DECL. Called from toplev.c after
20208 compilation proper has finished. */
20210 static void
20211 dwarf2out_global_decl (tree decl)
20213 /* Output DWARF2 information for file-scope tentative data object
20214 declarations, file-scope (extern) function declarations (which
20215 had no corresponding body) and file-scope tagged type declarations
20216 and definitions which have not yet been forced out. */
20217 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20218 dwarf2out_decl (decl);
20221 /* Output debug information for type decl DECL. Called from toplev.c
20222 and from language front ends (to record built-in types). */
20223 static void
20224 dwarf2out_type_decl (tree decl, int local)
20226 if (!local)
20227 dwarf2out_decl (decl);
20230 /* Output debug information for imported module or decl DECL.
20231 NAME is non-NULL name in the lexical block if the decl has been renamed.
20232 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20233 that DECL belongs to.
20234 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20235 static void
20236 dwarf2out_imported_module_or_decl_1 (tree decl,
20237 tree name,
20238 tree lexical_block,
20239 dw_die_ref lexical_block_die)
20241 expanded_location xloc;
20242 dw_die_ref imported_die = NULL;
20243 dw_die_ref at_import_die;
20245 if (TREE_CODE (decl) == IMPORTED_DECL)
20247 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20248 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20249 gcc_assert (decl);
20251 else
20252 xloc = expand_location (input_location);
20254 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20256 at_import_die = force_type_die (TREE_TYPE (decl));
20257 /* For namespace N { typedef void T; } using N::T; base_type_die
20258 returns NULL, but DW_TAG_imported_declaration requires
20259 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20260 if (!at_import_die)
20262 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20263 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20264 at_import_die = lookup_type_die (TREE_TYPE (decl));
20265 gcc_assert (at_import_die);
20268 else
20270 at_import_die = lookup_decl_die (decl);
20271 if (!at_import_die)
20273 /* If we're trying to avoid duplicate debug info, we may not have
20274 emitted the member decl for this field. Emit it now. */
20275 if (TREE_CODE (decl) == FIELD_DECL)
20277 tree type = DECL_CONTEXT (decl);
20279 if (TYPE_CONTEXT (type)
20280 && TYPE_P (TYPE_CONTEXT (type))
20281 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20282 DINFO_USAGE_DIR_USE))
20283 return;
20284 gen_type_die_for_member (type, decl,
20285 get_context_die (TYPE_CONTEXT (type)));
20287 at_import_die = force_decl_die (decl);
20291 if (TREE_CODE (decl) == NAMESPACE_DECL)
20293 if (dwarf_version >= 3 || !dwarf_strict)
20294 imported_die = new_die (DW_TAG_imported_module,
20295 lexical_block_die,
20296 lexical_block);
20297 else
20298 return;
20300 else
20301 imported_die = new_die (DW_TAG_imported_declaration,
20302 lexical_block_die,
20303 lexical_block);
20305 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20306 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20307 if (name)
20308 add_AT_string (imported_die, DW_AT_name,
20309 IDENTIFIER_POINTER (name));
20310 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20313 /* Output debug information for imported module or decl DECL.
20314 NAME is non-NULL name in context if the decl has been renamed.
20315 CHILD is true if decl is one of the renamed decls as part of
20316 importing whole module. */
20318 static void
20319 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20320 bool child)
20322 /* dw_die_ref at_import_die; */
20323 dw_die_ref scope_die;
20325 if (debug_info_level <= DINFO_LEVEL_TERSE)
20326 return;
20328 gcc_assert (decl);
20330 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20331 We need decl DIE for reference and scope die. First, get DIE for the decl
20332 itself. */
20334 /* Get the scope die for decl context. Use comp_unit_die for global module
20335 or decl. If die is not found for non globals, force new die. */
20336 if (context
20337 && TYPE_P (context)
20338 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20339 return;
20341 if (!(dwarf_version >= 3 || !dwarf_strict))
20342 return;
20344 scope_die = get_context_die (context);
20346 if (child)
20348 gcc_assert (scope_die->die_child);
20349 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20350 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20351 scope_die = scope_die->die_child;
20354 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20355 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20359 /* Write the debugging output for DECL. */
20361 void
20362 dwarf2out_decl (tree decl)
20364 dw_die_ref context_die = comp_unit_die ();
20366 switch (TREE_CODE (decl))
20368 case ERROR_MARK:
20369 return;
20371 case FUNCTION_DECL:
20372 /* What we would really like to do here is to filter out all mere
20373 file-scope declarations of file-scope functions which are never
20374 referenced later within this translation unit (and keep all of ones
20375 that *are* referenced later on) but we aren't clairvoyant, so we have
20376 no idea which functions will be referenced in the future (i.e. later
20377 on within the current translation unit). So here we just ignore all
20378 file-scope function declarations which are not also definitions. If
20379 and when the debugger needs to know something about these functions,
20380 it will have to hunt around and find the DWARF information associated
20381 with the definition of the function.
20383 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20384 nodes represent definitions and which ones represent mere
20385 declarations. We have to check DECL_INITIAL instead. That's because
20386 the C front-end supports some weird semantics for "extern inline"
20387 function definitions. These can get inlined within the current
20388 translation unit (and thus, we need to generate Dwarf info for their
20389 abstract instances so that the Dwarf info for the concrete inlined
20390 instances can have something to refer to) but the compiler never
20391 generates any out-of-lines instances of such things (despite the fact
20392 that they *are* definitions).
20394 The important point is that the C front-end marks these "extern
20395 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20396 them anyway. Note that the C++ front-end also plays some similar games
20397 for inline function definitions appearing within include files which
20398 also contain `#pragma interface' pragmas.
20400 If we are called from dwarf2out_abstract_function output a DIE
20401 anyway. We can end up here this way with early inlining and LTO
20402 where the inlined function is output in a different LTRANS unit
20403 or not at all. */
20404 if (DECL_INITIAL (decl) == NULL_TREE
20405 && ! DECL_ABSTRACT (decl))
20406 return;
20408 /* If we're a nested function, initially use a parent of NULL; if we're
20409 a plain function, this will be fixed up in decls_for_scope. If
20410 we're a method, it will be ignored, since we already have a DIE. */
20411 if (decl_function_context (decl)
20412 /* But if we're in terse mode, we don't care about scope. */
20413 && debug_info_level > DINFO_LEVEL_TERSE)
20414 context_die = NULL;
20415 break;
20417 case VAR_DECL:
20418 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20419 declaration and if the declaration was never even referenced from
20420 within this entire compilation unit. We suppress these DIEs in
20421 order to save space in the .debug section (by eliminating entries
20422 which are probably useless). Note that we must not suppress
20423 block-local extern declarations (whether used or not) because that
20424 would screw-up the debugger's name lookup mechanism and cause it to
20425 miss things which really ought to be in scope at a given point. */
20426 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20427 return;
20429 /* For local statics lookup proper context die. */
20430 if (TREE_STATIC (decl)
20431 && DECL_CONTEXT (decl)
20432 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20433 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20435 /* If we are in terse mode, don't generate any DIEs to represent any
20436 variable declarations or definitions. */
20437 if (debug_info_level <= DINFO_LEVEL_TERSE)
20438 return;
20439 break;
20441 case CONST_DECL:
20442 if (debug_info_level <= DINFO_LEVEL_TERSE)
20443 return;
20444 if (!is_fortran () && !is_ada ())
20445 return;
20446 if (TREE_STATIC (decl) && decl_function_context (decl))
20447 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20448 break;
20450 case NAMESPACE_DECL:
20451 case IMPORTED_DECL:
20452 if (debug_info_level <= DINFO_LEVEL_TERSE)
20453 return;
20454 if (lookup_decl_die (decl) != NULL)
20455 return;
20456 break;
20458 case TYPE_DECL:
20459 /* Don't emit stubs for types unless they are needed by other DIEs. */
20460 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20461 return;
20463 /* Don't bother trying to generate any DIEs to represent any of the
20464 normal built-in types for the language we are compiling. */
20465 if (DECL_IS_BUILTIN (decl))
20466 return;
20468 /* If we are in terse mode, don't generate any DIEs for types. */
20469 if (debug_info_level <= DINFO_LEVEL_TERSE)
20470 return;
20472 /* If we're a function-scope tag, initially use a parent of NULL;
20473 this will be fixed up in decls_for_scope. */
20474 if (decl_function_context (decl))
20475 context_die = NULL;
20477 break;
20479 default:
20480 return;
20483 gen_decl_die (decl, NULL, context_die);
20486 /* Write the debugging output for DECL. */
20488 static void
20489 dwarf2out_function_decl (tree decl)
20491 dwarf2out_decl (decl);
20492 call_arg_locations = NULL;
20493 call_arg_loc_last = NULL;
20494 call_site_count = -1;
20495 tail_call_site_count = -1;
20496 block_map.release ();
20497 htab_empty (decl_loc_table);
20498 htab_empty (cached_dw_loc_list_table);
20501 /* Output a marker (i.e. a label) for the beginning of the generated code for
20502 a lexical block. */
20504 static void
20505 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20506 unsigned int blocknum)
20508 switch_to_section (current_function_section ());
20509 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20512 /* Output a marker (i.e. a label) for the end of the generated code for a
20513 lexical block. */
20515 static void
20516 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20518 switch_to_section (current_function_section ());
20519 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20522 /* Returns nonzero if it is appropriate not to emit any debugging
20523 information for BLOCK, because it doesn't contain any instructions.
20525 Don't allow this for blocks with nested functions or local classes
20526 as we would end up with orphans, and in the presence of scheduling
20527 we may end up calling them anyway. */
20529 static bool
20530 dwarf2out_ignore_block (const_tree block)
20532 tree decl;
20533 unsigned int i;
20535 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20536 if (TREE_CODE (decl) == FUNCTION_DECL
20537 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20538 return 0;
20539 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20541 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20542 if (TREE_CODE (decl) == FUNCTION_DECL
20543 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20544 return 0;
20547 return 1;
20550 /* Hash table routines for file_hash. */
20552 static int
20553 file_table_eq (const void *p1_p, const void *p2_p)
20555 const struct dwarf_file_data *const p1 =
20556 (const struct dwarf_file_data *) p1_p;
20557 const char *const p2 = (const char *) p2_p;
20558 return filename_cmp (p1->filename, p2) == 0;
20561 static hashval_t
20562 file_table_hash (const void *p_p)
20564 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20565 return htab_hash_string (p->filename);
20568 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20569 dwarf2out.c) and return its "index". The index of each (known) filename is
20570 just a unique number which is associated with only that one filename. We
20571 need such numbers for the sake of generating labels (in the .debug_sfnames
20572 section) and references to those files numbers (in the .debug_srcinfo
20573 and.debug_macinfo sections). If the filename given as an argument is not
20574 found in our current list, add it to the list and assign it the next
20575 available unique index number. In order to speed up searches, we remember
20576 the index of the filename was looked up last. This handles the majority of
20577 all searches. */
20579 static struct dwarf_file_data *
20580 lookup_filename (const char *file_name)
20582 void ** slot;
20583 struct dwarf_file_data * created;
20585 /* Check to see if the file name that was searched on the previous
20586 call matches this file name. If so, return the index. */
20587 if (file_table_last_lookup
20588 && (file_name == file_table_last_lookup->filename
20589 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20590 return file_table_last_lookup;
20592 /* Didn't match the previous lookup, search the table. */
20593 slot = htab_find_slot_with_hash (file_table, file_name,
20594 htab_hash_string (file_name), INSERT);
20595 if (*slot)
20596 return (struct dwarf_file_data *) *slot;
20598 created = ggc_alloc_dwarf_file_data ();
20599 created->filename = file_name;
20600 created->emitted_number = 0;
20601 *slot = created;
20602 return created;
20605 /* If the assembler will construct the file table, then translate the compiler
20606 internal file table number into the assembler file table number, and emit
20607 a .file directive if we haven't already emitted one yet. The file table
20608 numbers are different because we prune debug info for unused variables and
20609 types, which may include filenames. */
20611 static int
20612 maybe_emit_file (struct dwarf_file_data * fd)
20614 if (! fd->emitted_number)
20616 if (last_emitted_file)
20617 fd->emitted_number = last_emitted_file->emitted_number + 1;
20618 else
20619 fd->emitted_number = 1;
20620 last_emitted_file = fd;
20622 if (DWARF2_ASM_LINE_DEBUG_INFO)
20624 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20625 output_quoted_string (asm_out_file,
20626 remap_debug_filename (fd->filename));
20627 fputc ('\n', asm_out_file);
20631 return fd->emitted_number;
20634 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20635 That generation should happen after function debug info has been
20636 generated. The value of the attribute is the constant value of ARG. */
20638 static void
20639 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20641 die_arg_entry entry;
20643 if (!die || !arg)
20644 return;
20646 if (!tmpl_value_parm_die_table)
20647 vec_alloc (tmpl_value_parm_die_table, 32);
20649 entry.die = die;
20650 entry.arg = arg;
20651 vec_safe_push (tmpl_value_parm_die_table, entry);
20654 /* Return TRUE if T is an instance of generic type, FALSE
20655 otherwise. */
20657 static bool
20658 generic_type_p (tree t)
20660 if (t == NULL_TREE || !TYPE_P (t))
20661 return false;
20662 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20665 /* Schedule the generation of the generic parameter dies for the
20666 instance of generic type T. The proper generation itself is later
20667 done by gen_scheduled_generic_parms_dies. */
20669 static void
20670 schedule_generic_params_dies_gen (tree t)
20672 if (!generic_type_p (t))
20673 return;
20675 if (!generic_type_instances)
20676 vec_alloc (generic_type_instances, 256);
20678 vec_safe_push (generic_type_instances, t);
20681 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20682 by append_entry_to_tmpl_value_parm_die_table. This function must
20683 be called after function DIEs have been generated. */
20685 static void
20686 gen_remaining_tmpl_value_param_die_attribute (void)
20688 if (tmpl_value_parm_die_table)
20690 unsigned i;
20691 die_arg_entry *e;
20693 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20694 tree_add_const_value_attribute (e->die, e->arg);
20698 /* Generate generic parameters DIEs for instances of generic types
20699 that have been previously scheduled by
20700 schedule_generic_params_dies_gen. This function must be called
20701 after all the types of the CU have been laid out. */
20703 static void
20704 gen_scheduled_generic_parms_dies (void)
20706 unsigned i;
20707 tree t;
20709 if (!generic_type_instances)
20710 return;
20712 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20713 if (COMPLETE_TYPE_P (t))
20714 gen_generic_params_dies (t);
20718 /* Replace DW_AT_name for the decl with name. */
20720 static void
20721 dwarf2out_set_name (tree decl, tree name)
20723 dw_die_ref die;
20724 dw_attr_ref attr;
20725 const char *dname;
20727 die = TYPE_SYMTAB_DIE (decl);
20728 if (!die)
20729 return;
20731 dname = dwarf2_name (name, 0);
20732 if (!dname)
20733 return;
20735 attr = get_AT (die, DW_AT_name);
20736 if (attr)
20738 struct indirect_string_node *node;
20740 node = find_AT_string (dname);
20741 /* replace the string. */
20742 attr->dw_attr_val.v.val_str = node;
20745 else
20746 add_name_attribute (die, dname);
20749 /* True if before or during processing of the first function being emitted. */
20750 static bool in_first_function_p = true;
20751 /* True if loc_note during dwarf2out_var_location call might still be
20752 before first real instruction at address equal to .Ltext0. */
20753 static bool maybe_at_text_label_p = true;
20754 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20755 static unsigned int first_loclabel_num_not_at_text_label;
20757 /* Called by the final INSN scan whenever we see a var location. We
20758 use it to drop labels in the right places, and throw the location in
20759 our lookup table. */
20761 static void
20762 dwarf2out_var_location (rtx loc_note)
20764 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20765 struct var_loc_node *newloc;
20766 rtx next_real, next_note;
20767 static const char *last_label;
20768 static const char *last_postcall_label;
20769 static bool last_in_cold_section_p;
20770 static rtx expected_next_loc_note;
20771 tree decl;
20772 bool var_loc_p;
20774 if (!NOTE_P (loc_note))
20776 if (CALL_P (loc_note))
20778 call_site_count++;
20779 if (SIBLING_CALL_P (loc_note))
20780 tail_call_site_count++;
20782 return;
20785 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20786 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20787 return;
20789 /* Optimize processing a large consecutive sequence of location
20790 notes so we don't spend too much time in next_real_insn. If the
20791 next insn is another location note, remember the next_real_insn
20792 calculation for next time. */
20793 next_real = cached_next_real_insn;
20794 if (next_real)
20796 if (expected_next_loc_note != loc_note)
20797 next_real = NULL_RTX;
20800 next_note = NEXT_INSN (loc_note);
20801 if (! next_note
20802 || INSN_DELETED_P (next_note)
20803 || ! NOTE_P (next_note)
20804 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20805 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20806 next_note = NULL_RTX;
20808 if (! next_real)
20809 next_real = next_real_insn (loc_note);
20811 if (next_note)
20813 expected_next_loc_note = next_note;
20814 cached_next_real_insn = next_real;
20816 else
20817 cached_next_real_insn = NULL_RTX;
20819 /* If there are no instructions which would be affected by this note,
20820 don't do anything. */
20821 if (var_loc_p
20822 && next_real == NULL_RTX
20823 && !NOTE_DURING_CALL_P (loc_note))
20824 return;
20826 if (next_real == NULL_RTX)
20827 next_real = get_last_insn ();
20829 /* If there were any real insns between note we processed last time
20830 and this note (or if it is the first note), clear
20831 last_{,postcall_}label so that they are not reused this time. */
20832 if (last_var_location_insn == NULL_RTX
20833 || last_var_location_insn != next_real
20834 || last_in_cold_section_p != in_cold_section_p)
20836 last_label = NULL;
20837 last_postcall_label = NULL;
20840 if (var_loc_p)
20842 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20843 newloc = add_var_loc_to_decl (decl, loc_note,
20844 NOTE_DURING_CALL_P (loc_note)
20845 ? last_postcall_label : last_label);
20846 if (newloc == NULL)
20847 return;
20849 else
20851 decl = NULL_TREE;
20852 newloc = NULL;
20855 /* If there were no real insns between note we processed last time
20856 and this note, use the label we emitted last time. Otherwise
20857 create a new label and emit it. */
20858 if (last_label == NULL)
20860 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20861 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20862 loclabel_num++;
20863 last_label = ggc_strdup (loclabel);
20864 /* See if loclabel might be equal to .Ltext0. If yes,
20865 bump first_loclabel_num_not_at_text_label. */
20866 if (!have_multiple_function_sections
20867 && in_first_function_p
20868 && maybe_at_text_label_p)
20870 static rtx last_start;
20871 rtx insn;
20872 for (insn = loc_note; insn; insn = previous_insn (insn))
20873 if (insn == last_start)
20874 break;
20875 else if (!NONDEBUG_INSN_P (insn))
20876 continue;
20877 else
20879 rtx body = PATTERN (insn);
20880 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
20881 continue;
20882 /* Inline asm could occupy zero bytes. */
20883 else if (GET_CODE (body) == ASM_INPUT
20884 || asm_noperands (body) >= 0)
20885 continue;
20886 #ifdef HAVE_attr_length
20887 else if (get_attr_min_length (insn) == 0)
20888 continue;
20889 #endif
20890 else
20892 /* Assume insn has non-zero length. */
20893 maybe_at_text_label_p = false;
20894 break;
20897 if (maybe_at_text_label_p)
20899 last_start = loc_note;
20900 first_loclabel_num_not_at_text_label = loclabel_num;
20905 if (!var_loc_p)
20907 struct call_arg_loc_node *ca_loc
20908 = ggc_alloc_cleared_call_arg_loc_node ();
20909 rtx prev = prev_real_insn (loc_note), x;
20910 ca_loc->call_arg_loc_note = loc_note;
20911 ca_loc->next = NULL;
20912 ca_loc->label = last_label;
20913 gcc_assert (prev
20914 && (CALL_P (prev)
20915 || (NONJUMP_INSN_P (prev)
20916 && GET_CODE (PATTERN (prev)) == SEQUENCE
20917 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20918 if (!CALL_P (prev))
20919 prev = XVECEXP (PATTERN (prev), 0, 0);
20920 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20921 x = get_call_rtx_from (PATTERN (prev));
20922 if (x)
20924 x = XEXP (XEXP (x, 0), 0);
20925 if (GET_CODE (x) == SYMBOL_REF
20926 && SYMBOL_REF_DECL (x)
20927 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20928 ca_loc->symbol_ref = x;
20930 ca_loc->block = insn_scope (prev);
20931 if (call_arg_locations)
20932 call_arg_loc_last->next = ca_loc;
20933 else
20934 call_arg_locations = ca_loc;
20935 call_arg_loc_last = ca_loc;
20937 else if (!NOTE_DURING_CALL_P (loc_note))
20938 newloc->label = last_label;
20939 else
20941 if (!last_postcall_label)
20943 sprintf (loclabel, "%s-1", last_label);
20944 last_postcall_label = ggc_strdup (loclabel);
20946 newloc->label = last_postcall_label;
20949 last_var_location_insn = next_real;
20950 last_in_cold_section_p = in_cold_section_p;
20953 /* Note in one location list that text section has changed. */
20955 static int
20956 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20958 var_loc_list *list = (var_loc_list *) *slot;
20959 if (list->first)
20960 list->last_before_switch
20961 = list->last->next ? list->last->next : list->last;
20962 return 1;
20965 /* Note in all location lists that text section has changed. */
20967 static void
20968 var_location_switch_text_section (void)
20970 if (decl_loc_table == NULL)
20971 return;
20973 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20976 /* Create a new line number table. */
20978 static dw_line_info_table *
20979 new_line_info_table (void)
20981 dw_line_info_table *table;
20983 table = ggc_alloc_cleared_dw_line_info_table_struct ();
20984 table->file_num = 1;
20985 table->line_num = 1;
20986 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20988 return table;
20991 /* Lookup the "current" table into which we emit line info, so
20992 that we don't have to do it for every source line. */
20994 static void
20995 set_cur_line_info_table (section *sec)
20997 dw_line_info_table *table;
20999 if (sec == text_section)
21000 table = text_section_line_info;
21001 else if (sec == cold_text_section)
21003 table = cold_text_section_line_info;
21004 if (!table)
21006 cold_text_section_line_info = table = new_line_info_table ();
21007 table->end_label = cold_end_label;
21010 else
21012 const char *end_label;
21014 if (flag_reorder_blocks_and_partition)
21016 if (in_cold_section_p)
21017 end_label = crtl->subsections.cold_section_end_label;
21018 else
21019 end_label = crtl->subsections.hot_section_end_label;
21021 else
21023 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21024 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21025 current_function_funcdef_no);
21026 end_label = ggc_strdup (label);
21029 table = new_line_info_table ();
21030 table->end_label = end_label;
21032 vec_safe_push (separate_line_info, table);
21035 if (DWARF2_ASM_LINE_DEBUG_INFO)
21036 table->is_stmt = (cur_line_info_table
21037 ? cur_line_info_table->is_stmt
21038 : DWARF_LINE_DEFAULT_IS_STMT_START);
21039 cur_line_info_table = table;
21043 /* We need to reset the locations at the beginning of each
21044 function. We can't do this in the end_function hook, because the
21045 declarations that use the locations won't have been output when
21046 that hook is called. Also compute have_multiple_function_sections here. */
21048 static void
21049 dwarf2out_begin_function (tree fun)
21051 section *sec = function_section (fun);
21053 if (sec != text_section)
21054 have_multiple_function_sections = true;
21056 if (flag_reorder_blocks_and_partition && !cold_text_section)
21058 gcc_assert (current_function_decl == fun);
21059 cold_text_section = unlikely_text_section ();
21060 switch_to_section (cold_text_section);
21061 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21062 switch_to_section (sec);
21065 dwarf2out_note_section_used ();
21066 call_site_count = 0;
21067 tail_call_site_count = 0;
21069 set_cur_line_info_table (sec);
21072 /* Helper function of dwarf2out_end_function, called only after emitting
21073 the very first function into assembly. Check if some .debug_loc range
21074 might end with a .LVL* label that could be equal to .Ltext0.
21075 In that case we must force using absolute addresses in .debug_loc ranges,
21076 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21077 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21078 list terminator.
21079 Set have_multiple_function_sections to true in that case and
21080 terminate htab traversal. */
21082 static int
21083 find_empty_loc_ranges_at_text_label (void **slot, void *)
21085 var_loc_list *entry;
21086 struct var_loc_node *node;
21088 entry = (var_loc_list *) *slot;
21089 node = entry->first;
21090 if (node && node->next && node->next->label)
21092 unsigned int i;
21093 const char *label = node->next->label;
21094 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21096 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21098 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21099 if (strcmp (label, loclabel) == 0)
21101 have_multiple_function_sections = true;
21102 return 0;
21106 return 1;
21109 /* Hook called after emitting a function into assembly.
21110 This does something only for the very first function emitted. */
21112 static void
21113 dwarf2out_end_function (unsigned int)
21115 if (in_first_function_p
21116 && !have_multiple_function_sections
21117 && first_loclabel_num_not_at_text_label
21118 && decl_loc_table)
21119 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21120 NULL);
21121 in_first_function_p = false;
21122 maybe_at_text_label_p = false;
21125 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21127 static void
21128 push_dw_line_info_entry (dw_line_info_table *table,
21129 enum dw_line_info_opcode opcode, unsigned int val)
21131 dw_line_info_entry e;
21132 e.opcode = opcode;
21133 e.val = val;
21134 vec_safe_push (table->entries, e);
21137 /* Output a label to mark the beginning of a source code line entry
21138 and record information relating to this source line, in
21139 'line_info_table' for later output of the .debug_line section. */
21140 /* ??? The discriminator parameter ought to be unsigned. */
21142 static void
21143 dwarf2out_source_line (unsigned int line, const char *filename,
21144 int discriminator, bool is_stmt)
21146 unsigned int file_num;
21147 dw_line_info_table *table;
21149 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21150 return;
21152 /* The discriminator column was added in dwarf4. Simplify the below
21153 by simply removing it if we're not supposed to output it. */
21154 if (dwarf_version < 4 && dwarf_strict)
21155 discriminator = 0;
21157 table = cur_line_info_table;
21158 file_num = maybe_emit_file (lookup_filename (filename));
21160 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21161 the debugger has used the second (possibly duplicate) line number
21162 at the beginning of the function to mark the end of the prologue.
21163 We could eliminate any other duplicates within the function. For
21164 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21165 that second line number entry. */
21166 /* Recall that this end-of-prologue indication is *not* the same thing
21167 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21168 to which the hook corresponds, follows the last insn that was
21169 emitted by gen_prologue. What we need is to precede the first insn
21170 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21171 insn that corresponds to something the user wrote. These may be
21172 very different locations once scheduling is enabled. */
21174 if (0 && file_num == table->file_num
21175 && line == table->line_num
21176 && discriminator == table->discrim_num
21177 && is_stmt == table->is_stmt)
21178 return;
21180 switch_to_section (current_function_section ());
21182 /* If requested, emit something human-readable. */
21183 if (flag_debug_asm)
21184 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21186 if (DWARF2_ASM_LINE_DEBUG_INFO)
21188 /* Emit the .loc directive understood by GNU as. */
21189 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21190 file_num, line, is_stmt, discriminator */
21191 fputs ("\t.loc ", asm_out_file);
21192 fprint_ul (asm_out_file, file_num);
21193 putc (' ', asm_out_file);
21194 fprint_ul (asm_out_file, line);
21195 putc (' ', asm_out_file);
21196 putc ('0', asm_out_file);
21198 if (is_stmt != table->is_stmt)
21200 fputs (" is_stmt ", asm_out_file);
21201 putc (is_stmt ? '1' : '0', asm_out_file);
21203 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21205 gcc_assert (discriminator > 0);
21206 fputs (" discriminator ", asm_out_file);
21207 fprint_ul (asm_out_file, (unsigned long) discriminator);
21209 putc ('\n', asm_out_file);
21211 else
21213 unsigned int label_num = ++line_info_label_num;
21215 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21217 push_dw_line_info_entry (table, LI_set_address, label_num);
21218 if (file_num != table->file_num)
21219 push_dw_line_info_entry (table, LI_set_file, file_num);
21220 if (discriminator != table->discrim_num)
21221 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21222 if (is_stmt != table->is_stmt)
21223 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21224 push_dw_line_info_entry (table, LI_set_line, line);
21227 table->file_num = file_num;
21228 table->line_num = line;
21229 table->discrim_num = discriminator;
21230 table->is_stmt = is_stmt;
21231 table->in_use = true;
21234 /* Record the beginning of a new source file. */
21236 static void
21237 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21239 if (flag_eliminate_dwarf2_dups)
21241 /* Record the beginning of the file for break_out_includes. */
21242 dw_die_ref bincl_die;
21244 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21245 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21248 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21250 macinfo_entry e;
21251 e.code = DW_MACINFO_start_file;
21252 e.lineno = lineno;
21253 e.info = ggc_strdup (filename);
21254 vec_safe_push (macinfo_table, e);
21258 /* Record the end of a source file. */
21260 static void
21261 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21263 if (flag_eliminate_dwarf2_dups)
21264 /* Record the end of the file for break_out_includes. */
21265 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21267 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21269 macinfo_entry e;
21270 e.code = DW_MACINFO_end_file;
21271 e.lineno = lineno;
21272 e.info = NULL;
21273 vec_safe_push (macinfo_table, e);
21277 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21278 the tail part of the directive line, i.e. the part which is past the
21279 initial whitespace, #, whitespace, directive-name, whitespace part. */
21281 static void
21282 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21283 const char *buffer ATTRIBUTE_UNUSED)
21285 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21287 macinfo_entry e;
21288 /* Insert a dummy first entry to be able to optimize the whole
21289 predefined macro block using DW_MACRO_GNU_transparent_include. */
21290 if (macinfo_table->is_empty () && lineno <= 1)
21292 e.code = 0;
21293 e.lineno = 0;
21294 e.info = NULL;
21295 vec_safe_push (macinfo_table, e);
21297 e.code = DW_MACINFO_define;
21298 e.lineno = lineno;
21299 e.info = ggc_strdup (buffer);
21300 vec_safe_push (macinfo_table, e);
21304 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21305 the tail part of the directive line, i.e. the part which is past the
21306 initial whitespace, #, whitespace, directive-name, whitespace part. */
21308 static void
21309 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21310 const char *buffer ATTRIBUTE_UNUSED)
21312 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21314 macinfo_entry e;
21315 /* Insert a dummy first entry to be able to optimize the whole
21316 predefined macro block using DW_MACRO_GNU_transparent_include. */
21317 if (macinfo_table->is_empty () && lineno <= 1)
21319 e.code = 0;
21320 e.lineno = 0;
21321 e.info = NULL;
21322 vec_safe_push (macinfo_table, e);
21324 e.code = DW_MACINFO_undef;
21325 e.lineno = lineno;
21326 e.info = ggc_strdup (buffer);
21327 vec_safe_push (macinfo_table, e);
21331 /* Helpers to manipulate hash table of CUs. */
21333 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21335 typedef macinfo_entry value_type;
21336 typedef macinfo_entry compare_type;
21337 static inline hashval_t hash (const value_type *);
21338 static inline bool equal (const value_type *, const compare_type *);
21341 inline hashval_t
21342 macinfo_entry_hasher::hash (const value_type *entry)
21344 return htab_hash_string (entry->info);
21347 inline bool
21348 macinfo_entry_hasher::equal (const value_type *entry1,
21349 const compare_type *entry2)
21351 return !strcmp (entry1->info, entry2->info);
21354 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21356 /* Output a single .debug_macinfo entry. */
21358 static void
21359 output_macinfo_op (macinfo_entry *ref)
21361 int file_num;
21362 size_t len;
21363 struct indirect_string_node *node;
21364 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21365 struct dwarf_file_data *fd;
21367 switch (ref->code)
21369 case DW_MACINFO_start_file:
21370 fd = lookup_filename (ref->info);
21371 file_num = maybe_emit_file (fd);
21372 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21373 dw2_asm_output_data_uleb128 (ref->lineno,
21374 "Included from line number %lu",
21375 (unsigned long) ref->lineno);
21376 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21377 break;
21378 case DW_MACINFO_end_file:
21379 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21380 break;
21381 case DW_MACINFO_define:
21382 case DW_MACINFO_undef:
21383 len = strlen (ref->info) + 1;
21384 if (!dwarf_strict
21385 && len > DWARF_OFFSET_SIZE
21386 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21387 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21389 ref->code = ref->code == DW_MACINFO_define
21390 ? DW_MACRO_GNU_define_indirect
21391 : DW_MACRO_GNU_undef_indirect;
21392 output_macinfo_op (ref);
21393 return;
21395 dw2_asm_output_data (1, ref->code,
21396 ref->code == DW_MACINFO_define
21397 ? "Define macro" : "Undefine macro");
21398 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21399 (unsigned long) ref->lineno);
21400 dw2_asm_output_nstring (ref->info, -1, "The macro");
21401 break;
21402 case DW_MACRO_GNU_define_indirect:
21403 case DW_MACRO_GNU_undef_indirect:
21404 node = find_AT_string (ref->info);
21405 gcc_assert (node
21406 && ((node->form == DW_FORM_strp)
21407 || (node->form == DW_FORM_GNU_str_index)));
21408 dw2_asm_output_data (1, ref->code,
21409 ref->code == DW_MACRO_GNU_define_indirect
21410 ? "Define macro indirect"
21411 : "Undefine macro indirect");
21412 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21413 (unsigned long) ref->lineno);
21414 if (node->form == DW_FORM_strp)
21415 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21416 debug_str_section, "The macro: \"%s\"",
21417 ref->info);
21418 else
21419 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21420 ref->info);
21421 break;
21422 case DW_MACRO_GNU_transparent_include:
21423 dw2_asm_output_data (1, ref->code, "Transparent include");
21424 ASM_GENERATE_INTERNAL_LABEL (label,
21425 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21426 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21427 break;
21428 default:
21429 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21430 ASM_COMMENT_START, (unsigned long) ref->code);
21431 break;
21435 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21436 other compilation unit .debug_macinfo sections. IDX is the first
21437 index of a define/undef, return the number of ops that should be
21438 emitted in a comdat .debug_macinfo section and emit
21439 a DW_MACRO_GNU_transparent_include entry referencing it.
21440 If the define/undef entry should be emitted normally, return 0. */
21442 static unsigned
21443 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21444 macinfo_hash_type *macinfo_htab)
21446 macinfo_entry *first, *second, *cur, *inc;
21447 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21448 unsigned char checksum[16];
21449 struct md5_ctx ctx;
21450 char *grp_name, *tail;
21451 const char *base;
21452 unsigned int i, count, encoded_filename_len, linebuf_len;
21453 macinfo_entry **slot;
21455 first = &(*macinfo_table)[idx];
21456 second = &(*macinfo_table)[idx + 1];
21458 /* Optimize only if there are at least two consecutive define/undef ops,
21459 and either all of them are before first DW_MACINFO_start_file
21460 with lineno {0,1} (i.e. predefined macro block), or all of them are
21461 in some included header file. */
21462 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21463 return 0;
21464 if (vec_safe_is_empty (files))
21466 if (first->lineno > 1 || second->lineno > 1)
21467 return 0;
21469 else if (first->lineno == 0)
21470 return 0;
21472 /* Find the last define/undef entry that can be grouped together
21473 with first and at the same time compute md5 checksum of their
21474 codes, linenumbers and strings. */
21475 md5_init_ctx (&ctx);
21476 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21477 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21478 break;
21479 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21480 break;
21481 else
21483 unsigned char code = cur->code;
21484 md5_process_bytes (&code, 1, &ctx);
21485 checksum_uleb128 (cur->lineno, &ctx);
21486 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21488 md5_finish_ctx (&ctx, checksum);
21489 count = i - idx;
21491 /* From the containing include filename (if any) pick up just
21492 usable characters from its basename. */
21493 if (vec_safe_is_empty (files))
21494 base = "";
21495 else
21496 base = lbasename (files->last ().info);
21497 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21498 if (ISIDNUM (base[i]) || base[i] == '.')
21499 encoded_filename_len++;
21500 /* Count . at the end. */
21501 if (encoded_filename_len)
21502 encoded_filename_len++;
21504 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21505 linebuf_len = strlen (linebuf);
21507 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21508 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21509 + 16 * 2 + 1);
21510 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21511 tail = grp_name + 4;
21512 if (encoded_filename_len)
21514 for (i = 0; base[i]; i++)
21515 if (ISIDNUM (base[i]) || base[i] == '.')
21516 *tail++ = base[i];
21517 *tail++ = '.';
21519 memcpy (tail, linebuf, linebuf_len);
21520 tail += linebuf_len;
21521 *tail++ = '.';
21522 for (i = 0; i < 16; i++)
21523 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21525 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21526 in the empty vector entry before the first define/undef. */
21527 inc = &(*macinfo_table)[idx - 1];
21528 inc->code = DW_MACRO_GNU_transparent_include;
21529 inc->lineno = 0;
21530 inc->info = ggc_strdup (grp_name);
21531 if (!macinfo_htab->is_created ())
21532 macinfo_htab->create (10);
21533 /* Avoid emitting duplicates. */
21534 slot = macinfo_htab->find_slot (inc, INSERT);
21535 if (*slot != NULL)
21537 inc->code = 0;
21538 inc->info = NULL;
21539 /* If such an entry has been used before, just emit
21540 a DW_MACRO_GNU_transparent_include op. */
21541 inc = *slot;
21542 output_macinfo_op (inc);
21543 /* And clear all macinfo_entry in the range to avoid emitting them
21544 in the second pass. */
21545 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21547 cur->code = 0;
21548 cur->info = NULL;
21551 else
21553 *slot = inc;
21554 inc->lineno = macinfo_htab->elements ();
21555 output_macinfo_op (inc);
21557 return count;
21560 /* Save any strings needed by the macinfo table in the debug str
21561 table. All strings must be collected into the table by the time
21562 index_string is called. */
21564 static void
21565 save_macinfo_strings (void)
21567 unsigned len;
21568 unsigned i;
21569 macinfo_entry *ref;
21571 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21573 switch (ref->code)
21575 /* Match the logic in output_macinfo_op to decide on
21576 indirect strings. */
21577 case DW_MACINFO_define:
21578 case DW_MACINFO_undef:
21579 len = strlen (ref->info) + 1;
21580 if (!dwarf_strict
21581 && len > DWARF_OFFSET_SIZE
21582 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21583 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21584 set_indirect_string (find_AT_string (ref->info));
21585 break;
21586 case DW_MACRO_GNU_define_indirect:
21587 case DW_MACRO_GNU_undef_indirect:
21588 set_indirect_string (find_AT_string (ref->info));
21589 break;
21590 default:
21591 break;
21596 /* Output macinfo section(s). */
21598 static void
21599 output_macinfo (void)
21601 unsigned i;
21602 unsigned long length = vec_safe_length (macinfo_table);
21603 macinfo_entry *ref;
21604 vec<macinfo_entry, va_gc> *files = NULL;
21605 macinfo_hash_type macinfo_htab;
21607 if (! length)
21608 return;
21610 /* output_macinfo* uses these interchangeably. */
21611 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21612 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21613 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21614 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21616 /* For .debug_macro emit the section header. */
21617 if (!dwarf_strict)
21619 dw2_asm_output_data (2, 4, "DWARF macro version number");
21620 if (DWARF_OFFSET_SIZE == 8)
21621 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21622 else
21623 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21624 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21625 (!dwarf_split_debug_info ? debug_line_section_label
21626 : debug_skeleton_line_section_label),
21627 debug_line_section, NULL);
21630 /* In the first loop, it emits the primary .debug_macinfo section
21631 and after each emitted op the macinfo_entry is cleared.
21632 If a longer range of define/undef ops can be optimized using
21633 DW_MACRO_GNU_transparent_include, the
21634 DW_MACRO_GNU_transparent_include op is emitted and kept in
21635 the vector before the first define/undef in the range and the
21636 whole range of define/undef ops is not emitted and kept. */
21637 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21639 switch (ref->code)
21641 case DW_MACINFO_start_file:
21642 vec_safe_push (files, *ref);
21643 break;
21644 case DW_MACINFO_end_file:
21645 if (!vec_safe_is_empty (files))
21646 files->pop ();
21647 break;
21648 case DW_MACINFO_define:
21649 case DW_MACINFO_undef:
21650 if (!dwarf_strict
21651 && HAVE_COMDAT_GROUP
21652 && vec_safe_length (files) != 1
21653 && i > 0
21654 && i + 1 < length
21655 && (*macinfo_table)[i - 1].code == 0)
21657 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21658 if (count)
21660 i += count - 1;
21661 continue;
21664 break;
21665 case 0:
21666 /* A dummy entry may be inserted at the beginning to be able
21667 to optimize the whole block of predefined macros. */
21668 if (i == 0)
21669 continue;
21670 default:
21671 break;
21673 output_macinfo_op (ref);
21674 ref->info = NULL;
21675 ref->code = 0;
21678 if (!macinfo_htab.is_created ())
21679 return;
21681 macinfo_htab.dispose ();
21683 /* If any DW_MACRO_GNU_transparent_include were used, on those
21684 DW_MACRO_GNU_transparent_include entries terminate the
21685 current chain and switch to a new comdat .debug_macinfo
21686 section and emit the define/undef entries within it. */
21687 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21688 switch (ref->code)
21690 case 0:
21691 continue;
21692 case DW_MACRO_GNU_transparent_include:
21694 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21695 tree comdat_key = get_identifier (ref->info);
21696 /* Terminate the previous .debug_macinfo section. */
21697 dw2_asm_output_data (1, 0, "End compilation unit");
21698 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21699 SECTION_DEBUG
21700 | SECTION_LINKONCE,
21701 comdat_key);
21702 ASM_GENERATE_INTERNAL_LABEL (label,
21703 DEBUG_MACRO_SECTION_LABEL,
21704 ref->lineno);
21705 ASM_OUTPUT_LABEL (asm_out_file, label);
21706 ref->code = 0;
21707 ref->info = NULL;
21708 dw2_asm_output_data (2, 4, "DWARF macro version number");
21709 if (DWARF_OFFSET_SIZE == 8)
21710 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21711 else
21712 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21714 break;
21715 case DW_MACINFO_define:
21716 case DW_MACINFO_undef:
21717 output_macinfo_op (ref);
21718 ref->code = 0;
21719 ref->info = NULL;
21720 break;
21721 default:
21722 gcc_unreachable ();
21726 /* Set up for Dwarf output at the start of compilation. */
21728 static void
21729 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21731 /* Allocate the file_table. */
21732 file_table = htab_create_ggc (50, file_table_hash,
21733 file_table_eq, NULL);
21735 /* Allocate the decl_die_table. */
21736 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21737 decl_die_table_eq, NULL);
21739 /* Allocate the decl_loc_table. */
21740 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21741 decl_loc_table_eq, NULL);
21743 /* Allocate the cached_dw_loc_list_table. */
21744 cached_dw_loc_list_table
21745 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21746 cached_dw_loc_list_table_eq, NULL);
21748 /* Allocate the initial hunk of the decl_scope_table. */
21749 vec_alloc (decl_scope_table, 256);
21751 /* Allocate the initial hunk of the abbrev_die_table. */
21752 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21753 (ABBREV_DIE_TABLE_INCREMENT);
21754 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21755 /* Zero-th entry is allocated, but unused. */
21756 abbrev_die_table_in_use = 1;
21758 /* Allocate the pubtypes and pubnames vectors. */
21759 vec_alloc (pubname_table, 32);
21760 vec_alloc (pubtype_table, 32);
21762 vec_alloc (incomplete_types, 64);
21764 vec_alloc (used_rtx_array, 32);
21766 if (!dwarf_split_debug_info)
21768 debug_info_section = get_section (DEBUG_INFO_SECTION,
21769 SECTION_DEBUG, NULL);
21770 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21771 SECTION_DEBUG, NULL);
21772 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21773 SECTION_DEBUG, NULL);
21775 else
21777 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21778 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21779 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21780 SECTION_DEBUG | SECTION_EXCLUDE,
21781 NULL);
21782 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21783 SECTION_DEBUG, NULL);
21784 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21785 SECTION_DEBUG, NULL);
21786 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21787 SECTION_DEBUG, NULL);
21788 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21789 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21791 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21792 the main .o, but the skeleton_line goes into the split off dwo. */
21793 debug_skeleton_line_section
21794 = get_section (DEBUG_DWO_LINE_SECTION,
21795 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21796 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21797 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21798 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21799 SECTION_DEBUG | SECTION_EXCLUDE,
21800 NULL);
21801 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21802 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21803 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21804 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21805 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
21806 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
21808 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21809 SECTION_DEBUG, NULL);
21810 debug_macinfo_section = get_section (dwarf_strict
21811 ? DEBUG_MACINFO_SECTION
21812 : DEBUG_MACRO_SECTION,
21813 DEBUG_MACRO_SECTION_FLAGS, NULL);
21814 debug_line_section = get_section (DEBUG_LINE_SECTION,
21815 SECTION_DEBUG, NULL);
21816 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21817 SECTION_DEBUG, NULL);
21818 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21819 SECTION_DEBUG, NULL);
21820 debug_str_section = get_section (DEBUG_STR_SECTION,
21821 DEBUG_STR_SECTION_FLAGS, NULL);
21822 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21823 SECTION_DEBUG, NULL);
21824 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21825 SECTION_DEBUG, NULL);
21827 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21828 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21829 DEBUG_ABBREV_SECTION_LABEL, 0);
21830 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21831 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21832 COLD_TEXT_SECTION_LABEL, 0);
21833 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21835 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21836 DEBUG_INFO_SECTION_LABEL, 0);
21837 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21838 DEBUG_LINE_SECTION_LABEL, 0);
21839 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21840 DEBUG_RANGES_SECTION_LABEL, 0);
21841 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21842 DEBUG_ADDR_SECTION_LABEL, 0);
21843 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21844 dwarf_strict
21845 ? DEBUG_MACINFO_SECTION_LABEL
21846 : DEBUG_MACRO_SECTION_LABEL, 0);
21847 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21849 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21850 vec_alloc (macinfo_table, 64);
21852 switch_to_section (text_section);
21853 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21855 /* Make sure the line number table for .text always exists. */
21856 text_section_line_info = new_line_info_table ();
21857 text_section_line_info->end_label = text_end_label;
21860 /* Called before compile () starts outputtting functions, variables
21861 and toplevel asms into assembly. */
21863 static void
21864 dwarf2out_assembly_start (void)
21866 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21867 && dwarf2out_do_cfi_asm ()
21868 && (!(flag_unwind_tables || flag_exceptions)
21869 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21870 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21873 /* A helper function for dwarf2out_finish called through
21874 htab_traverse. Assign a string its index. All strings must be
21875 collected into the table by the time index_string is called,
21876 because the indexing code relies on htab_traverse to traverse nodes
21877 in the same order for each run. */
21879 static int
21880 index_string (void **h, void *v)
21882 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21883 unsigned int *index = (unsigned int *) v;
21885 find_string_form (node);
21886 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21888 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21889 node->index = *index;
21890 *index += 1;
21892 return 1;
21895 /* A helper function for output_indirect_strings called through
21896 htab_traverse. Output the offset to a string and update the
21897 current offset. */
21899 static int
21900 output_index_string_offset (void **h, void *v)
21902 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21903 unsigned int *offset = (unsigned int *) v;
21905 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21907 /* Assert that this node has been assigned an index. */
21908 gcc_assert (node->index != NO_INDEX_ASSIGNED
21909 && node->index != NOT_INDEXED);
21910 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21911 "indexed string 0x%x: %s", node->index, node->str);
21912 *offset += strlen (node->str) + 1;
21914 return 1;
21917 /* A helper function for dwarf2out_finish called through
21918 htab_traverse. Output the indexed string. */
21920 static int
21921 output_index_string (void **h, void *v)
21923 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21924 unsigned int *cur_idx = (unsigned int *) v;
21926 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21928 /* Assert that the strings are output in the same order as their
21929 indexes were assigned. */
21930 gcc_assert (*cur_idx == node->index);
21931 assemble_string (node->str, strlen (node->str) + 1);
21932 *cur_idx += 1;
21934 return 1;
21937 /* A helper function for dwarf2out_finish called through
21938 htab_traverse. Emit one queued .debug_str string. */
21940 static int
21941 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21943 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21945 node->form = find_string_form (node);
21946 if (node->form == DW_FORM_strp && node->refcount > 0)
21948 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21949 assemble_string (node->str, strlen (node->str) + 1);
21952 return 1;
21955 /* Output the indexed string table. */
21957 static void
21958 output_indirect_strings (void)
21960 switch_to_section (debug_str_section);
21961 if (!dwarf_split_debug_info)
21962 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21963 else
21965 unsigned int offset = 0;
21966 unsigned int cur_idx = 0;
21968 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
21970 switch_to_section (debug_str_offsets_section);
21971 htab_traverse_noresize (debug_str_hash,
21972 output_index_string_offset,
21973 &offset);
21974 switch_to_section (debug_str_dwo_section);
21975 htab_traverse_noresize (debug_str_hash,
21976 output_index_string,
21977 &cur_idx);
21981 /* Callback for htab_traverse to assign an index to an entry in the
21982 table, and to write that entry to the .debug_addr section. */
21984 static int
21985 output_addr_table_entry (void **slot, void *data)
21987 addr_table_entry *entry = (addr_table_entry *) *slot;
21988 unsigned int *cur_index = (unsigned int *)data;
21990 if (entry->refcount == 0)
21992 gcc_assert (entry->index == NO_INDEX_ASSIGNED
21993 || entry->index == NOT_INDEXED);
21994 return 1;
21997 gcc_assert (entry->index == *cur_index);
21998 (*cur_index)++;
22000 switch (entry->kind)
22002 case ate_kind_rtx:
22003 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22004 "0x%x", entry->index);
22005 break;
22006 case ate_kind_rtx_dtprel:
22007 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22008 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22009 DWARF2_ADDR_SIZE,
22010 entry->addr.rtl);
22011 fputc ('\n', asm_out_file);
22012 break;
22013 case ate_kind_label:
22014 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22015 "0x%x", entry->index);
22016 break;
22017 default:
22018 gcc_unreachable ();
22020 return 1;
22023 /* Produce the .debug_addr section. */
22025 static void
22026 output_addr_table (void)
22028 unsigned int index = 0;
22029 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22030 return;
22032 switch_to_section (debug_addr_section);
22033 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22036 #if ENABLE_ASSERT_CHECKING
22037 /* Verify that all marks are clear. */
22039 static void
22040 verify_marks_clear (dw_die_ref die)
22042 dw_die_ref c;
22044 gcc_assert (! die->die_mark);
22045 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22047 #endif /* ENABLE_ASSERT_CHECKING */
22049 /* Clear the marks for a die and its children.
22050 Be cool if the mark isn't set. */
22052 static void
22053 prune_unmark_dies (dw_die_ref die)
22055 dw_die_ref c;
22057 if (die->die_mark)
22058 die->die_mark = 0;
22059 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22062 /* Given DIE that we're marking as used, find any other dies
22063 it references as attributes and mark them as used. */
22065 static void
22066 prune_unused_types_walk_attribs (dw_die_ref die)
22068 dw_attr_ref a;
22069 unsigned ix;
22071 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22073 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22075 /* A reference to another DIE.
22076 Make sure that it will get emitted.
22077 If it was broken out into a comdat group, don't follow it. */
22078 if (! AT_ref (a)->comdat_type_p
22079 || a->dw_attr == DW_AT_specification)
22080 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22082 /* Set the string's refcount to 0 so that prune_unused_types_mark
22083 accounts properly for it. */
22084 if (AT_class (a) == dw_val_class_str)
22085 a->dw_attr_val.v.val_str->refcount = 0;
22089 /* Mark the generic parameters and arguments children DIEs of DIE. */
22091 static void
22092 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22094 dw_die_ref c;
22096 if (die == NULL || die->die_child == NULL)
22097 return;
22098 c = die->die_child;
22101 switch (c->die_tag)
22103 case DW_TAG_template_type_param:
22104 case DW_TAG_template_value_param:
22105 case DW_TAG_GNU_template_template_param:
22106 case DW_TAG_GNU_template_parameter_pack:
22107 prune_unused_types_mark (c, 1);
22108 break;
22109 default:
22110 break;
22112 c = c->die_sib;
22113 } while (c && c != die->die_child);
22116 /* Mark DIE as being used. If DOKIDS is true, then walk down
22117 to DIE's children. */
22119 static void
22120 prune_unused_types_mark (dw_die_ref die, int dokids)
22122 dw_die_ref c;
22124 if (die->die_mark == 0)
22126 /* We haven't done this node yet. Mark it as used. */
22127 die->die_mark = 1;
22128 /* If this is the DIE of a generic type instantiation,
22129 mark the children DIEs that describe its generic parms and
22130 args. */
22131 prune_unused_types_mark_generic_parms_dies (die);
22133 /* We also have to mark its parents as used.
22134 (But we don't want to mark our parent's kids due to this,
22135 unless it is a class.) */
22136 if (die->die_parent)
22137 prune_unused_types_mark (die->die_parent,
22138 class_scope_p (die->die_parent));
22140 /* Mark any referenced nodes. */
22141 prune_unused_types_walk_attribs (die);
22143 /* If this node is a specification,
22144 also mark the definition, if it exists. */
22145 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22146 prune_unused_types_mark (die->die_definition, 1);
22149 if (dokids && die->die_mark != 2)
22151 /* We need to walk the children, but haven't done so yet.
22152 Remember that we've walked the kids. */
22153 die->die_mark = 2;
22155 /* If this is an array type, we need to make sure our
22156 kids get marked, even if they're types. If we're
22157 breaking out types into comdat sections, do this
22158 for all type definitions. */
22159 if (die->die_tag == DW_TAG_array_type
22160 || (use_debug_types
22161 && is_type_die (die) && ! is_declaration_die (die)))
22162 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22163 else
22164 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22168 /* For local classes, look if any static member functions were emitted
22169 and if so, mark them. */
22171 static void
22172 prune_unused_types_walk_local_classes (dw_die_ref die)
22174 dw_die_ref c;
22176 if (die->die_mark == 2)
22177 return;
22179 switch (die->die_tag)
22181 case DW_TAG_structure_type:
22182 case DW_TAG_union_type:
22183 case DW_TAG_class_type:
22184 break;
22186 case DW_TAG_subprogram:
22187 if (!get_AT_flag (die, DW_AT_declaration)
22188 || die->die_definition != NULL)
22189 prune_unused_types_mark (die, 1);
22190 return;
22192 default:
22193 return;
22196 /* Mark children. */
22197 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22200 /* Walk the tree DIE and mark types that we actually use. */
22202 static void
22203 prune_unused_types_walk (dw_die_ref die)
22205 dw_die_ref c;
22207 /* Don't do anything if this node is already marked and
22208 children have been marked as well. */
22209 if (die->die_mark == 2)
22210 return;
22212 switch (die->die_tag)
22214 case DW_TAG_structure_type:
22215 case DW_TAG_union_type:
22216 case DW_TAG_class_type:
22217 if (die->die_perennial_p)
22218 break;
22220 for (c = die->die_parent; c; c = c->die_parent)
22221 if (c->die_tag == DW_TAG_subprogram)
22222 break;
22224 /* Finding used static member functions inside of classes
22225 is needed just for local classes, because for other classes
22226 static member function DIEs with DW_AT_specification
22227 are emitted outside of the DW_TAG_*_type. If we ever change
22228 it, we'd need to call this even for non-local classes. */
22229 if (c)
22230 prune_unused_types_walk_local_classes (die);
22232 /* It's a type node --- don't mark it. */
22233 return;
22235 case DW_TAG_const_type:
22236 case DW_TAG_packed_type:
22237 case DW_TAG_pointer_type:
22238 case DW_TAG_reference_type:
22239 case DW_TAG_rvalue_reference_type:
22240 case DW_TAG_volatile_type:
22241 case DW_TAG_typedef:
22242 case DW_TAG_array_type:
22243 case DW_TAG_interface_type:
22244 case DW_TAG_friend:
22245 case DW_TAG_variant_part:
22246 case DW_TAG_enumeration_type:
22247 case DW_TAG_subroutine_type:
22248 case DW_TAG_string_type:
22249 case DW_TAG_set_type:
22250 case DW_TAG_subrange_type:
22251 case DW_TAG_ptr_to_member_type:
22252 case DW_TAG_file_type:
22253 if (die->die_perennial_p)
22254 break;
22256 /* It's a type node --- don't mark it. */
22257 return;
22259 default:
22260 /* Mark everything else. */
22261 break;
22264 if (die->die_mark == 0)
22266 die->die_mark = 1;
22268 /* Now, mark any dies referenced from here. */
22269 prune_unused_types_walk_attribs (die);
22272 die->die_mark = 2;
22274 /* Mark children. */
22275 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22278 /* Increment the string counts on strings referred to from DIE's
22279 attributes. */
22281 static void
22282 prune_unused_types_update_strings (dw_die_ref die)
22284 dw_attr_ref a;
22285 unsigned ix;
22287 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22288 if (AT_class (a) == dw_val_class_str)
22290 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22291 s->refcount++;
22292 /* Avoid unnecessarily putting strings that are used less than
22293 twice in the hash table. */
22294 if (s->refcount
22295 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22297 void ** slot;
22298 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22299 htab_hash_string (s->str),
22300 INSERT);
22301 gcc_assert (*slot == NULL);
22302 *slot = s;
22307 /* Remove from the tree DIE any dies that aren't marked. */
22309 static void
22310 prune_unused_types_prune (dw_die_ref die)
22312 dw_die_ref c;
22314 gcc_assert (die->die_mark);
22315 prune_unused_types_update_strings (die);
22317 if (! die->die_child)
22318 return;
22320 c = die->die_child;
22321 do {
22322 dw_die_ref prev = c;
22323 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22324 if (c == die->die_child)
22326 /* No marked children between 'prev' and the end of the list. */
22327 if (prev == c)
22328 /* No marked children at all. */
22329 die->die_child = NULL;
22330 else
22332 prev->die_sib = c->die_sib;
22333 die->die_child = prev;
22335 return;
22338 if (c != prev->die_sib)
22339 prev->die_sib = c;
22340 prune_unused_types_prune (c);
22341 } while (c != die->die_child);
22344 /* Remove dies representing declarations that we never use. */
22346 static void
22347 prune_unused_types (void)
22349 unsigned int i;
22350 limbo_die_node *node;
22351 comdat_type_node *ctnode;
22352 pubname_ref pub;
22353 dw_die_ref base_type;
22355 #if ENABLE_ASSERT_CHECKING
22356 /* All the marks should already be clear. */
22357 verify_marks_clear (comp_unit_die ());
22358 for (node = limbo_die_list; node; node = node->next)
22359 verify_marks_clear (node->die);
22360 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22361 verify_marks_clear (ctnode->root_die);
22362 #endif /* ENABLE_ASSERT_CHECKING */
22364 /* Mark types that are used in global variables. */
22365 premark_types_used_by_global_vars ();
22367 /* Set the mark on nodes that are actually used. */
22368 prune_unused_types_walk (comp_unit_die ());
22369 for (node = limbo_die_list; node; node = node->next)
22370 prune_unused_types_walk (node->die);
22371 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22373 prune_unused_types_walk (ctnode->root_die);
22374 prune_unused_types_mark (ctnode->type_die, 1);
22377 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22378 are unusual in that they are pubnames that are the children of pubtypes.
22379 They should only be marked via their parent DW_TAG_enumeration_type die,
22380 not as roots in themselves. */
22381 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22382 if (pub->die->die_tag != DW_TAG_enumerator)
22383 prune_unused_types_mark (pub->die, 1);
22384 for (i = 0; base_types.iterate (i, &base_type); i++)
22385 prune_unused_types_mark (base_type, 1);
22387 if (debug_str_hash)
22388 htab_empty (debug_str_hash);
22389 if (skeleton_debug_str_hash)
22390 htab_empty (skeleton_debug_str_hash);
22391 prune_unused_types_prune (comp_unit_die ());
22392 for (node = limbo_die_list; node; node = node->next)
22393 prune_unused_types_prune (node->die);
22394 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22395 prune_unused_types_prune (ctnode->root_die);
22397 /* Leave the marks clear. */
22398 prune_unmark_dies (comp_unit_die ());
22399 for (node = limbo_die_list; node; node = node->next)
22400 prune_unmark_dies (node->die);
22401 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22402 prune_unmark_dies (ctnode->root_die);
22405 /* Set the parameter to true if there are any relative pathnames in
22406 the file table. */
22407 static int
22408 file_table_relative_p (void ** slot, void *param)
22410 bool *p = (bool *) param;
22411 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22412 if (!IS_ABSOLUTE_PATH (d->filename))
22414 *p = true;
22415 return 0;
22417 return 1;
22420 /* Helpers to manipulate hash table of comdat type units. */
22422 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22424 typedef comdat_type_node value_type;
22425 typedef comdat_type_node compare_type;
22426 static inline hashval_t hash (const value_type *);
22427 static inline bool equal (const value_type *, const compare_type *);
22430 inline hashval_t
22431 comdat_type_hasher::hash (const value_type *type_node)
22433 hashval_t h;
22434 memcpy (&h, type_node->signature, sizeof (h));
22435 return h;
22438 inline bool
22439 comdat_type_hasher::equal (const value_type *type_node_1,
22440 const compare_type *type_node_2)
22442 return (! memcmp (type_node_1->signature, type_node_2->signature,
22443 DWARF_TYPE_SIGNATURE_SIZE));
22446 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22447 to the location it would have been added, should we know its
22448 DECL_ASSEMBLER_NAME when we added other attributes. This will
22449 probably improve compactness of debug info, removing equivalent
22450 abbrevs, and hide any differences caused by deferring the
22451 computation of the assembler name, triggered by e.g. PCH. */
22453 static inline void
22454 move_linkage_attr (dw_die_ref die)
22456 unsigned ix = vec_safe_length (die->die_attr);
22457 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22459 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22460 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22462 while (--ix > 0)
22464 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22466 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22467 break;
22470 if (ix != vec_safe_length (die->die_attr) - 1)
22472 die->die_attr->pop ();
22473 die->die_attr->quick_insert (ix, linkage);
22477 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22478 referenced from typed stack ops and count how often they are used. */
22480 static void
22481 mark_base_types (dw_loc_descr_ref loc)
22483 dw_die_ref base_type = NULL;
22485 for (; loc; loc = loc->dw_loc_next)
22487 switch (loc->dw_loc_opc)
22489 case DW_OP_GNU_regval_type:
22490 case DW_OP_GNU_deref_type:
22491 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22492 break;
22493 case DW_OP_GNU_convert:
22494 case DW_OP_GNU_reinterpret:
22495 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22496 continue;
22497 /* FALLTHRU */
22498 case DW_OP_GNU_const_type:
22499 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22500 break;
22501 case DW_OP_GNU_entry_value:
22502 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22503 continue;
22504 default:
22505 continue;
22507 gcc_assert (base_type->die_parent == comp_unit_die ());
22508 if (base_type->die_mark)
22509 base_type->die_mark++;
22510 else
22512 base_types.safe_push (base_type);
22513 base_type->die_mark = 1;
22518 /* Comparison function for sorting marked base types. */
22520 static int
22521 base_type_cmp (const void *x, const void *y)
22523 dw_die_ref dx = *(const dw_die_ref *) x;
22524 dw_die_ref dy = *(const dw_die_ref *) y;
22525 unsigned int byte_size1, byte_size2;
22526 unsigned int encoding1, encoding2;
22527 if (dx->die_mark > dy->die_mark)
22528 return -1;
22529 if (dx->die_mark < dy->die_mark)
22530 return 1;
22531 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22532 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22533 if (byte_size1 < byte_size2)
22534 return 1;
22535 if (byte_size1 > byte_size2)
22536 return -1;
22537 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22538 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22539 if (encoding1 < encoding2)
22540 return 1;
22541 if (encoding1 > encoding2)
22542 return -1;
22543 return 0;
22546 /* Move base types marked by mark_base_types as early as possible
22547 in the CU, sorted by decreasing usage count both to make the
22548 uleb128 references as small as possible and to make sure they
22549 will have die_offset already computed by calc_die_sizes when
22550 sizes of typed stack loc ops is computed. */
22552 static void
22553 move_marked_base_types (void)
22555 unsigned int i;
22556 dw_die_ref base_type, die, c;
22558 if (base_types.is_empty ())
22559 return;
22561 /* Sort by decreasing usage count, they will be added again in that
22562 order later on. */
22563 base_types.qsort (base_type_cmp);
22564 die = comp_unit_die ();
22565 c = die->die_child;
22568 dw_die_ref prev = c;
22569 c = c->die_sib;
22570 while (c->die_mark)
22572 remove_child_with_prev (c, prev);
22573 /* As base types got marked, there must be at least
22574 one node other than DW_TAG_base_type. */
22575 gcc_assert (c != c->die_sib);
22576 c = c->die_sib;
22579 while (c != die->die_child);
22580 gcc_assert (die->die_child);
22581 c = die->die_child;
22582 for (i = 0; base_types.iterate (i, &base_type); i++)
22584 base_type->die_mark = 0;
22585 base_type->die_sib = c->die_sib;
22586 c->die_sib = base_type;
22587 c = base_type;
22591 /* Helper function for resolve_addr, attempt to resolve
22592 one CONST_STRING, return non-zero if not successful. Similarly verify that
22593 SYMBOL_REFs refer to variables emitted in the current CU. */
22595 static int
22596 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22598 rtx rtl = *addr;
22600 if (GET_CODE (rtl) == CONST_STRING)
22602 size_t len = strlen (XSTR (rtl, 0)) + 1;
22603 tree t = build_string (len, XSTR (rtl, 0));
22604 tree tlen = size_int (len - 1);
22605 TREE_TYPE (t)
22606 = build_array_type (char_type_node, build_index_type (tlen));
22607 rtl = lookup_constant_def (t);
22608 if (!rtl || !MEM_P (rtl))
22609 return 1;
22610 rtl = XEXP (rtl, 0);
22611 if (GET_CODE (rtl) == SYMBOL_REF
22612 && SYMBOL_REF_DECL (rtl)
22613 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22614 return 1;
22615 vec_safe_push (used_rtx_array, rtl);
22616 *addr = rtl;
22617 return 0;
22620 if (GET_CODE (rtl) == SYMBOL_REF
22621 && SYMBOL_REF_DECL (rtl))
22623 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22625 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22626 return 1;
22628 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22629 return 1;
22632 if (GET_CODE (rtl) == CONST
22633 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22634 return 1;
22636 return 0;
22639 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22640 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22641 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22643 static rtx
22644 string_cst_pool_decl (tree t)
22646 rtx rtl = output_constant_def (t, 1);
22647 unsigned char *array;
22648 dw_loc_descr_ref l;
22649 tree decl;
22650 size_t len;
22651 dw_die_ref ref;
22653 if (!rtl || !MEM_P (rtl))
22654 return NULL_RTX;
22655 rtl = XEXP (rtl, 0);
22656 if (GET_CODE (rtl) != SYMBOL_REF
22657 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22658 return NULL_RTX;
22660 decl = SYMBOL_REF_DECL (rtl);
22661 if (!lookup_decl_die (decl))
22663 len = TREE_STRING_LENGTH (t);
22664 vec_safe_push (used_rtx_array, rtl);
22665 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22666 array = (unsigned char *) ggc_alloc_atomic (len);
22667 memcpy (array, TREE_STRING_POINTER (t), len);
22668 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22669 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22670 l->dw_loc_oprnd2.v.val_vec.length = len;
22671 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22672 l->dw_loc_oprnd2.v.val_vec.array = array;
22673 add_AT_loc (ref, DW_AT_location, l);
22674 equate_decl_number_to_die (decl, ref);
22676 return rtl;
22679 /* Helper function of resolve_addr_in_expr. LOC is
22680 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22681 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22682 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22683 with DW_OP_GNU_implicit_pointer if possible
22684 and return true, if unsuccesful, return false. */
22686 static bool
22687 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22689 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22690 HOST_WIDE_INT offset = 0;
22691 dw_die_ref ref = NULL;
22692 tree decl;
22694 if (GET_CODE (rtl) == CONST
22695 && GET_CODE (XEXP (rtl, 0)) == PLUS
22696 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22698 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22699 rtl = XEXP (XEXP (rtl, 0), 0);
22701 if (GET_CODE (rtl) == CONST_STRING)
22703 size_t len = strlen (XSTR (rtl, 0)) + 1;
22704 tree t = build_string (len, XSTR (rtl, 0));
22705 tree tlen = size_int (len - 1);
22707 TREE_TYPE (t)
22708 = build_array_type (char_type_node, build_index_type (tlen));
22709 rtl = string_cst_pool_decl (t);
22710 if (!rtl)
22711 return false;
22713 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22715 decl = SYMBOL_REF_DECL (rtl);
22716 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22718 ref = lookup_decl_die (decl);
22719 if (ref && (get_AT (ref, DW_AT_location)
22720 || get_AT (ref, DW_AT_const_value)))
22722 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22723 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22724 loc->dw_loc_oprnd1.val_entry = NULL;
22725 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22726 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22727 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22728 loc->dw_loc_oprnd2.v.val_int = offset;
22729 return true;
22733 return false;
22736 /* Helper function for resolve_addr, handle one location
22737 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22738 the location list couldn't be resolved. */
22740 static bool
22741 resolve_addr_in_expr (dw_loc_descr_ref loc)
22743 dw_loc_descr_ref keep = NULL;
22744 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22745 switch (loc->dw_loc_opc)
22747 case DW_OP_addr:
22748 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22750 if ((prev == NULL
22751 || prev->dw_loc_opc == DW_OP_piece
22752 || prev->dw_loc_opc == DW_OP_bit_piece)
22753 && loc->dw_loc_next
22754 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22755 && !dwarf_strict
22756 && optimize_one_addr_into_implicit_ptr (loc))
22757 break;
22758 return false;
22760 break;
22761 case DW_OP_GNU_addr_index:
22762 case DW_OP_GNU_const_index:
22763 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22764 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22765 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22766 NULL))
22767 return false;
22768 break;
22769 case DW_OP_const4u:
22770 case DW_OP_const8u:
22771 if (loc->dtprel
22772 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22773 return false;
22774 break;
22775 case DW_OP_plus_uconst:
22776 if (size_of_loc_descr (loc)
22777 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22779 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22781 dw_loc_descr_ref repl
22782 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22783 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22784 add_loc_descr (&repl, loc->dw_loc_next);
22785 *loc = *repl;
22787 break;
22788 case DW_OP_implicit_value:
22789 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22790 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22791 return false;
22792 break;
22793 case DW_OP_GNU_implicit_pointer:
22794 case DW_OP_GNU_parameter_ref:
22795 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22797 dw_die_ref ref
22798 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22799 if (ref == NULL)
22800 return false;
22801 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22802 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22803 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22805 break;
22806 case DW_OP_GNU_const_type:
22807 case DW_OP_GNU_regval_type:
22808 case DW_OP_GNU_deref_type:
22809 case DW_OP_GNU_convert:
22810 case DW_OP_GNU_reinterpret:
22811 while (loc->dw_loc_next
22812 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22814 dw_die_ref base1, base2;
22815 unsigned enc1, enc2, size1, size2;
22816 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22817 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22818 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22819 else if (loc->dw_loc_oprnd1.val_class
22820 == dw_val_class_unsigned_const)
22821 break;
22822 else
22823 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22824 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22825 == dw_val_class_unsigned_const)
22826 break;
22827 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22828 gcc_assert (base1->die_tag == DW_TAG_base_type
22829 && base2->die_tag == DW_TAG_base_type);
22830 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22831 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22832 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22833 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22834 if (size1 == size2
22835 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22836 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22837 && loc != keep)
22838 || enc1 == enc2))
22840 /* Optimize away next DW_OP_GNU_convert after
22841 adjusting LOC's base type die reference. */
22842 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22843 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22844 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22845 else
22846 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22847 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22848 continue;
22850 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22851 point typed stack entry. */
22852 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22853 keep = loc->dw_loc_next;
22854 break;
22856 break;
22857 default:
22858 break;
22860 return true;
22863 /* Helper function of resolve_addr. DIE had DW_AT_location of
22864 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
22865 and DW_OP_addr couldn't be resolved. resolve_addr has already
22866 removed the DW_AT_location attribute. This function attempts to
22867 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
22868 to it or DW_AT_const_value attribute, if possible. */
22870 static void
22871 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
22873 if (TREE_CODE (decl) != VAR_DECL
22874 || lookup_decl_die (decl) != die
22875 || DECL_EXTERNAL (decl)
22876 || !TREE_STATIC (decl)
22877 || DECL_INITIAL (decl) == NULL_TREE
22878 || DECL_P (DECL_INITIAL (decl))
22879 || get_AT (die, DW_AT_const_value))
22880 return;
22882 tree init = DECL_INITIAL (decl);
22883 HOST_WIDE_INT offset = 0;
22884 /* For variables that have been optimized away and thus
22885 don't have a memory location, see if we can emit
22886 DW_AT_const_value instead. */
22887 if (tree_add_const_value_attribute (die, init))
22888 return;
22889 if (dwarf_strict)
22890 return;
22891 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
22892 and ADDR_EXPR refers to a decl that has DW_AT_location or
22893 DW_AT_const_value (but isn't addressable, otherwise
22894 resolving the original DW_OP_addr wouldn't fail), see if
22895 we can add DW_OP_GNU_implicit_pointer. */
22896 STRIP_NOPS (init);
22897 if (TREE_CODE (init) == POINTER_PLUS_EXPR
22898 && host_integerp (TREE_OPERAND (init, 1), 0))
22900 offset = tree_low_cst (TREE_OPERAND (init, 1), 0);
22901 init = TREE_OPERAND (init, 0);
22902 STRIP_NOPS (init);
22904 if (TREE_CODE (init) != ADDR_EXPR)
22905 return;
22906 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
22907 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
22908 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
22909 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
22910 && TREE_OPERAND (init, 0) != decl))
22912 dw_die_ref ref;
22913 dw_loc_descr_ref l;
22915 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
22917 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
22918 if (!rtl)
22919 return;
22920 decl = SYMBOL_REF_DECL (rtl);
22922 else
22923 decl = TREE_OPERAND (init, 0);
22924 ref = lookup_decl_die (decl);
22925 if (ref == NULL
22926 || (!get_AT (ref, DW_AT_location)
22927 && !get_AT (ref, DW_AT_const_value)))
22928 return;
22929 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
22930 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22931 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
22932 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
22933 add_AT_loc (die, DW_AT_location, l);
22937 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22938 an address in .rodata section if the string literal is emitted there,
22939 or remove the containing location list or replace DW_AT_const_value
22940 with DW_AT_location and empty location expression, if it isn't found
22941 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22942 to something that has been emitted in the current CU. */
22944 static void
22945 resolve_addr (dw_die_ref die)
22947 dw_die_ref c;
22948 dw_attr_ref a;
22949 dw_loc_list_ref *curr, *start, loc;
22950 unsigned ix;
22952 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22953 switch (AT_class (a))
22955 case dw_val_class_loc_list:
22956 start = curr = AT_loc_list_ptr (a);
22957 loc = *curr;
22958 gcc_assert (loc);
22959 /* The same list can be referenced more than once. See if we have
22960 already recorded the result from a previous pass. */
22961 if (loc->replaced)
22962 *curr = loc->dw_loc_next;
22963 else if (!loc->resolved_addr)
22965 /* As things stand, we do not expect or allow one die to
22966 reference a suffix of another die's location list chain.
22967 References must be identical or completely separate.
22968 There is therefore no need to cache the result of this
22969 pass on any list other than the first; doing so
22970 would lead to unnecessary writes. */
22971 while (*curr)
22973 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22974 if (!resolve_addr_in_expr ((*curr)->expr))
22976 dw_loc_list_ref next = (*curr)->dw_loc_next;
22977 dw_loc_descr_ref l = (*curr)->expr;
22979 if (next && (*curr)->ll_symbol)
22981 gcc_assert (!next->ll_symbol);
22982 next->ll_symbol = (*curr)->ll_symbol;
22984 if (dwarf_split_debug_info)
22985 remove_loc_list_addr_table_entries (l);
22986 *curr = next;
22988 else
22990 mark_base_types ((*curr)->expr);
22991 curr = &(*curr)->dw_loc_next;
22994 if (loc == *start)
22995 loc->resolved_addr = 1;
22996 else
22998 loc->replaced = 1;
22999 loc->dw_loc_next = *start;
23002 if (!*start)
23004 remove_AT (die, a->dw_attr);
23005 ix--;
23007 break;
23008 case dw_val_class_loc:
23010 dw_loc_descr_ref l = AT_loc (a);
23011 /* For -gdwarf-2 don't attempt to optimize
23012 DW_AT_data_member_location containing
23013 DW_OP_plus_uconst - older consumers might
23014 rely on it being that op instead of a more complex,
23015 but shorter, location description. */
23016 if ((dwarf_version > 2
23017 || a->dw_attr != DW_AT_data_member_location
23018 || l == NULL
23019 || l->dw_loc_opc != DW_OP_plus_uconst
23020 || l->dw_loc_next != NULL)
23021 && !resolve_addr_in_expr (l))
23023 if (dwarf_split_debug_info)
23024 remove_loc_list_addr_table_entries (l);
23025 if (l != NULL
23026 && l->dw_loc_next == NULL
23027 && l->dw_loc_opc == DW_OP_addr
23028 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23029 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23030 && a->dw_attr == DW_AT_location)
23032 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23033 remove_AT (die, a->dw_attr);
23034 ix--;
23035 optimize_location_into_implicit_ptr (die, decl);
23036 break;
23038 remove_AT (die, a->dw_attr);
23039 ix--;
23041 else
23042 mark_base_types (l);
23044 break;
23045 case dw_val_class_addr:
23046 if (a->dw_attr == DW_AT_const_value
23047 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23049 if (AT_index (a) != NOT_INDEXED)
23050 remove_addr_table_entry (a->dw_attr_val.val_entry);
23051 remove_AT (die, a->dw_attr);
23052 ix--;
23054 if (die->die_tag == DW_TAG_GNU_call_site
23055 && a->dw_attr == DW_AT_abstract_origin)
23057 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23058 dw_die_ref tdie = lookup_decl_die (tdecl);
23059 if (tdie == NULL
23060 && DECL_EXTERNAL (tdecl)
23061 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23063 force_decl_die (tdecl);
23064 tdie = lookup_decl_die (tdecl);
23066 if (tdie)
23068 a->dw_attr_val.val_class = dw_val_class_die_ref;
23069 a->dw_attr_val.v.val_die_ref.die = tdie;
23070 a->dw_attr_val.v.val_die_ref.external = 0;
23072 else
23074 if (AT_index (a) != NOT_INDEXED)
23075 remove_addr_table_entry (a->dw_attr_val.val_entry);
23076 remove_AT (die, a->dw_attr);
23077 ix--;
23080 break;
23081 default:
23082 break;
23085 FOR_EACH_CHILD (die, c, resolve_addr (c));
23088 /* Helper routines for optimize_location_lists.
23089 This pass tries to share identical local lists in .debug_loc
23090 section. */
23092 /* Iteratively hash operands of LOC opcode. */
23094 static inline hashval_t
23095 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23097 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23098 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23100 switch (loc->dw_loc_opc)
23102 case DW_OP_const4u:
23103 case DW_OP_const8u:
23104 if (loc->dtprel)
23105 goto hash_addr;
23106 /* FALLTHRU */
23107 case DW_OP_const1u:
23108 case DW_OP_const1s:
23109 case DW_OP_const2u:
23110 case DW_OP_const2s:
23111 case DW_OP_const4s:
23112 case DW_OP_const8s:
23113 case DW_OP_constu:
23114 case DW_OP_consts:
23115 case DW_OP_pick:
23116 case DW_OP_plus_uconst:
23117 case DW_OP_breg0:
23118 case DW_OP_breg1:
23119 case DW_OP_breg2:
23120 case DW_OP_breg3:
23121 case DW_OP_breg4:
23122 case DW_OP_breg5:
23123 case DW_OP_breg6:
23124 case DW_OP_breg7:
23125 case DW_OP_breg8:
23126 case DW_OP_breg9:
23127 case DW_OP_breg10:
23128 case DW_OP_breg11:
23129 case DW_OP_breg12:
23130 case DW_OP_breg13:
23131 case DW_OP_breg14:
23132 case DW_OP_breg15:
23133 case DW_OP_breg16:
23134 case DW_OP_breg17:
23135 case DW_OP_breg18:
23136 case DW_OP_breg19:
23137 case DW_OP_breg20:
23138 case DW_OP_breg21:
23139 case DW_OP_breg22:
23140 case DW_OP_breg23:
23141 case DW_OP_breg24:
23142 case DW_OP_breg25:
23143 case DW_OP_breg26:
23144 case DW_OP_breg27:
23145 case DW_OP_breg28:
23146 case DW_OP_breg29:
23147 case DW_OP_breg30:
23148 case DW_OP_breg31:
23149 case DW_OP_regx:
23150 case DW_OP_fbreg:
23151 case DW_OP_piece:
23152 case DW_OP_deref_size:
23153 case DW_OP_xderef_size:
23154 hash = iterative_hash_object (val1->v.val_int, hash);
23155 break;
23156 case DW_OP_skip:
23157 case DW_OP_bra:
23159 int offset;
23161 gcc_assert (val1->val_class == dw_val_class_loc);
23162 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23163 hash = iterative_hash_object (offset, hash);
23165 break;
23166 case DW_OP_implicit_value:
23167 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23168 switch (val2->val_class)
23170 case dw_val_class_const:
23171 hash = iterative_hash_object (val2->v.val_int, hash);
23172 break;
23173 case dw_val_class_vec:
23175 unsigned int elt_size = val2->v.val_vec.elt_size;
23176 unsigned int len = val2->v.val_vec.length;
23178 hash = iterative_hash_object (elt_size, hash);
23179 hash = iterative_hash_object (len, hash);
23180 hash = iterative_hash (val2->v.val_vec.array,
23181 len * elt_size, hash);
23183 break;
23184 case dw_val_class_const_double:
23185 hash = iterative_hash_object (val2->v.val_double.low, hash);
23186 hash = iterative_hash_object (val2->v.val_double.high, hash);
23187 break;
23188 case dw_val_class_addr:
23189 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23190 break;
23191 default:
23192 gcc_unreachable ();
23194 break;
23195 case DW_OP_bregx:
23196 case DW_OP_bit_piece:
23197 hash = iterative_hash_object (val1->v.val_int, hash);
23198 hash = iterative_hash_object (val2->v.val_int, hash);
23199 break;
23200 case DW_OP_addr:
23201 hash_addr:
23202 if (loc->dtprel)
23204 unsigned char dtprel = 0xd1;
23205 hash = iterative_hash_object (dtprel, hash);
23207 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23208 break;
23209 case DW_OP_GNU_addr_index:
23210 case DW_OP_GNU_const_index:
23212 if (loc->dtprel)
23214 unsigned char dtprel = 0xd1;
23215 hash = iterative_hash_object (dtprel, hash);
23217 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23219 break;
23220 case DW_OP_GNU_implicit_pointer:
23221 hash = iterative_hash_object (val2->v.val_int, hash);
23222 break;
23223 case DW_OP_GNU_entry_value:
23224 hash = hash_loc_operands (val1->v.val_loc, hash);
23225 break;
23226 case DW_OP_GNU_regval_type:
23227 case DW_OP_GNU_deref_type:
23229 unsigned int byte_size
23230 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23231 unsigned int encoding
23232 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23233 hash = iterative_hash_object (val1->v.val_int, hash);
23234 hash = iterative_hash_object (byte_size, hash);
23235 hash = iterative_hash_object (encoding, hash);
23237 break;
23238 case DW_OP_GNU_convert:
23239 case DW_OP_GNU_reinterpret:
23240 if (val1->val_class == dw_val_class_unsigned_const)
23242 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23243 break;
23245 /* FALLTHRU */
23246 case DW_OP_GNU_const_type:
23248 unsigned int byte_size
23249 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23250 unsigned int encoding
23251 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23252 hash = iterative_hash_object (byte_size, hash);
23253 hash = iterative_hash_object (encoding, hash);
23254 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23255 break;
23256 hash = iterative_hash_object (val2->val_class, hash);
23257 switch (val2->val_class)
23259 case dw_val_class_const:
23260 hash = iterative_hash_object (val2->v.val_int, hash);
23261 break;
23262 case dw_val_class_vec:
23264 unsigned int elt_size = val2->v.val_vec.elt_size;
23265 unsigned int len = val2->v.val_vec.length;
23267 hash = iterative_hash_object (elt_size, hash);
23268 hash = iterative_hash_object (len, hash);
23269 hash = iterative_hash (val2->v.val_vec.array,
23270 len * elt_size, hash);
23272 break;
23273 case dw_val_class_const_double:
23274 hash = iterative_hash_object (val2->v.val_double.low, hash);
23275 hash = iterative_hash_object (val2->v.val_double.high, hash);
23276 break;
23277 default:
23278 gcc_unreachable ();
23281 break;
23283 default:
23284 /* Other codes have no operands. */
23285 break;
23287 return hash;
23290 /* Iteratively hash the whole DWARF location expression LOC. */
23292 static inline hashval_t
23293 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23295 dw_loc_descr_ref l;
23296 bool sizes_computed = false;
23297 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23298 size_of_locs (loc);
23300 for (l = loc; l != NULL; l = l->dw_loc_next)
23302 enum dwarf_location_atom opc = l->dw_loc_opc;
23303 hash = iterative_hash_object (opc, hash);
23304 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23306 size_of_locs (loc);
23307 sizes_computed = true;
23309 hash = hash_loc_operands (l, hash);
23311 return hash;
23314 /* Compute hash of the whole location list LIST_HEAD. */
23316 static inline void
23317 hash_loc_list (dw_loc_list_ref list_head)
23319 dw_loc_list_ref curr = list_head;
23320 hashval_t hash = 0;
23322 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23324 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23325 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23326 if (curr->section)
23327 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23328 hash);
23329 hash = hash_locs (curr->expr, hash);
23331 list_head->hash = hash;
23334 /* Return true if X and Y opcodes have the same operands. */
23336 static inline bool
23337 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23339 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23340 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23341 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23342 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23344 switch (x->dw_loc_opc)
23346 case DW_OP_const4u:
23347 case DW_OP_const8u:
23348 if (x->dtprel)
23349 goto hash_addr;
23350 /* FALLTHRU */
23351 case DW_OP_const1u:
23352 case DW_OP_const1s:
23353 case DW_OP_const2u:
23354 case DW_OP_const2s:
23355 case DW_OP_const4s:
23356 case DW_OP_const8s:
23357 case DW_OP_constu:
23358 case DW_OP_consts:
23359 case DW_OP_pick:
23360 case DW_OP_plus_uconst:
23361 case DW_OP_breg0:
23362 case DW_OP_breg1:
23363 case DW_OP_breg2:
23364 case DW_OP_breg3:
23365 case DW_OP_breg4:
23366 case DW_OP_breg5:
23367 case DW_OP_breg6:
23368 case DW_OP_breg7:
23369 case DW_OP_breg8:
23370 case DW_OP_breg9:
23371 case DW_OP_breg10:
23372 case DW_OP_breg11:
23373 case DW_OP_breg12:
23374 case DW_OP_breg13:
23375 case DW_OP_breg14:
23376 case DW_OP_breg15:
23377 case DW_OP_breg16:
23378 case DW_OP_breg17:
23379 case DW_OP_breg18:
23380 case DW_OP_breg19:
23381 case DW_OP_breg20:
23382 case DW_OP_breg21:
23383 case DW_OP_breg22:
23384 case DW_OP_breg23:
23385 case DW_OP_breg24:
23386 case DW_OP_breg25:
23387 case DW_OP_breg26:
23388 case DW_OP_breg27:
23389 case DW_OP_breg28:
23390 case DW_OP_breg29:
23391 case DW_OP_breg30:
23392 case DW_OP_breg31:
23393 case DW_OP_regx:
23394 case DW_OP_fbreg:
23395 case DW_OP_piece:
23396 case DW_OP_deref_size:
23397 case DW_OP_xderef_size:
23398 return valx1->v.val_int == valy1->v.val_int;
23399 case DW_OP_skip:
23400 case DW_OP_bra:
23401 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23402 can cause irrelevant differences in dw_loc_addr. */
23403 gcc_assert (valx1->val_class == dw_val_class_loc
23404 && valy1->val_class == dw_val_class_loc
23405 && (dwarf_split_debug_info
23406 || x->dw_loc_addr == y->dw_loc_addr));
23407 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23408 case DW_OP_implicit_value:
23409 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23410 || valx2->val_class != valy2->val_class)
23411 return false;
23412 switch (valx2->val_class)
23414 case dw_val_class_const:
23415 return valx2->v.val_int == valy2->v.val_int;
23416 case dw_val_class_vec:
23417 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23418 && valx2->v.val_vec.length == valy2->v.val_vec.length
23419 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23420 valx2->v.val_vec.elt_size
23421 * valx2->v.val_vec.length) == 0;
23422 case dw_val_class_const_double:
23423 return valx2->v.val_double.low == valy2->v.val_double.low
23424 && valx2->v.val_double.high == valy2->v.val_double.high;
23425 case dw_val_class_addr:
23426 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23427 default:
23428 gcc_unreachable ();
23430 case DW_OP_bregx:
23431 case DW_OP_bit_piece:
23432 return valx1->v.val_int == valy1->v.val_int
23433 && valx2->v.val_int == valy2->v.val_int;
23434 case DW_OP_addr:
23435 hash_addr:
23436 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23437 case DW_OP_GNU_addr_index:
23438 case DW_OP_GNU_const_index:
23440 rtx ax1 = valx1->val_entry->addr.rtl;
23441 rtx ay1 = valy1->val_entry->addr.rtl;
23442 return rtx_equal_p (ax1, ay1);
23444 case DW_OP_GNU_implicit_pointer:
23445 return valx1->val_class == dw_val_class_die_ref
23446 && valx1->val_class == valy1->val_class
23447 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23448 && valx2->v.val_int == valy2->v.val_int;
23449 case DW_OP_GNU_entry_value:
23450 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23451 case DW_OP_GNU_const_type:
23452 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23453 || valx2->val_class != valy2->val_class)
23454 return false;
23455 switch (valx2->val_class)
23457 case dw_val_class_const:
23458 return valx2->v.val_int == valy2->v.val_int;
23459 case dw_val_class_vec:
23460 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23461 && valx2->v.val_vec.length == valy2->v.val_vec.length
23462 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23463 valx2->v.val_vec.elt_size
23464 * valx2->v.val_vec.length) == 0;
23465 case dw_val_class_const_double:
23466 return valx2->v.val_double.low == valy2->v.val_double.low
23467 && valx2->v.val_double.high == valy2->v.val_double.high;
23468 default:
23469 gcc_unreachable ();
23471 case DW_OP_GNU_regval_type:
23472 case DW_OP_GNU_deref_type:
23473 return valx1->v.val_int == valy1->v.val_int
23474 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23475 case DW_OP_GNU_convert:
23476 case DW_OP_GNU_reinterpret:
23477 if (valx1->val_class != valy1->val_class)
23478 return false;
23479 if (valx1->val_class == dw_val_class_unsigned_const)
23480 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23481 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23482 case DW_OP_GNU_parameter_ref:
23483 return valx1->val_class == dw_val_class_die_ref
23484 && valx1->val_class == valy1->val_class
23485 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23486 default:
23487 /* Other codes have no operands. */
23488 return true;
23492 /* Return true if DWARF location expressions X and Y are the same. */
23494 static inline bool
23495 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23497 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23498 if (x->dw_loc_opc != y->dw_loc_opc
23499 || x->dtprel != y->dtprel
23500 || !compare_loc_operands (x, y))
23501 break;
23502 return x == NULL && y == NULL;
23505 /* Hashtable helpers. */
23507 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23509 typedef dw_loc_list_struct value_type;
23510 typedef dw_loc_list_struct compare_type;
23511 static inline hashval_t hash (const value_type *);
23512 static inline bool equal (const value_type *, const compare_type *);
23515 /* Return precomputed hash of location list X. */
23517 inline hashval_t
23518 loc_list_hasher::hash (const value_type *x)
23520 return x->hash;
23523 /* Return true if location lists A and B are the same. */
23525 inline bool
23526 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23528 if (a == b)
23529 return 1;
23530 if (a->hash != b->hash)
23531 return 0;
23532 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23533 if (strcmp (a->begin, b->begin) != 0
23534 || strcmp (a->end, b->end) != 0
23535 || (a->section == NULL) != (b->section == NULL)
23536 || (a->section && strcmp (a->section, b->section) != 0)
23537 || !compare_locs (a->expr, b->expr))
23538 break;
23539 return a == NULL && b == NULL;
23542 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23545 /* Recursively optimize location lists referenced from DIE
23546 children and share them whenever possible. */
23548 static void
23549 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23551 dw_die_ref c;
23552 dw_attr_ref a;
23553 unsigned ix;
23554 dw_loc_list_struct **slot;
23556 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23557 if (AT_class (a) == dw_val_class_loc_list)
23559 dw_loc_list_ref list = AT_loc_list (a);
23560 /* TODO: perform some optimizations here, before hashing
23561 it and storing into the hash table. */
23562 hash_loc_list (list);
23563 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23564 if (*slot == NULL)
23565 *slot = list;
23566 else
23567 a->dw_attr_val.v.val_loc_list = *slot;
23570 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23574 /* Recursively assign each location list a unique index into the debug_addr
23575 section. */
23577 static void
23578 index_location_lists (dw_die_ref die)
23580 dw_die_ref c;
23581 dw_attr_ref a;
23582 unsigned ix;
23584 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23585 if (AT_class (a) == dw_val_class_loc_list)
23587 dw_loc_list_ref list = AT_loc_list (a);
23588 dw_loc_list_ref curr;
23589 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23591 /* Don't index an entry that has already been indexed
23592 or won't be output. */
23593 if (curr->begin_entry != NULL
23594 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23595 continue;
23597 curr->begin_entry
23598 = add_addr_table_entry (xstrdup (curr->begin),
23599 ate_kind_label);
23603 FOR_EACH_CHILD (die, c, index_location_lists (c));
23606 /* Optimize location lists referenced from DIE
23607 children and share them whenever possible. */
23609 static void
23610 optimize_location_lists (dw_die_ref die)
23612 loc_list_hash_type htab;
23613 htab.create (500);
23614 optimize_location_lists_1 (die, htab);
23615 htab.dispose ();
23618 /* Output stuff that dwarf requires at the end of every file,
23619 and generate the DWARF-2 debugging info. */
23621 static void
23622 dwarf2out_finish (const char *filename)
23624 limbo_die_node *node, *next_node;
23625 comdat_type_node *ctnode;
23626 hash_table <comdat_type_hasher> comdat_type_table;
23627 unsigned int i;
23628 dw_die_ref main_comp_unit_die;
23630 /* PCH might result in DW_AT_producer string being restored from the
23631 header compilation, so always fill it with empty string initially
23632 and overwrite only here. */
23633 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23634 producer_string = gen_producer_string ();
23635 producer->dw_attr_val.v.val_str->refcount--;
23636 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23638 gen_scheduled_generic_parms_dies ();
23639 gen_remaining_tmpl_value_param_die_attribute ();
23641 /* Add the name for the main input file now. We delayed this from
23642 dwarf2out_init to avoid complications with PCH. */
23643 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23644 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23645 add_comp_dir_attribute (comp_unit_die ());
23646 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23648 bool p = false;
23649 htab_traverse (file_table, file_table_relative_p, &p);
23650 if (p)
23651 add_comp_dir_attribute (comp_unit_die ());
23654 if (deferred_locations_list)
23655 for (i = 0; i < deferred_locations_list->length (); i++)
23657 add_location_or_const_value_attribute (
23658 (*deferred_locations_list)[i].die,
23659 (*deferred_locations_list)[i].variable,
23660 false,
23661 DW_AT_location);
23664 /* Traverse the limbo die list, and add parent/child links. The only
23665 dies without parents that should be here are concrete instances of
23666 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23667 For concrete instances, we can get the parent die from the abstract
23668 instance. */
23669 for (node = limbo_die_list; node; node = next_node)
23671 dw_die_ref die = node->die;
23672 next_node = node->next;
23674 if (die->die_parent == NULL)
23676 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23678 if (origin && origin->die_parent)
23679 add_child_die (origin->die_parent, die);
23680 else if (is_cu_die (die))
23682 else if (seen_error ())
23683 /* It's OK to be confused by errors in the input. */
23684 add_child_die (comp_unit_die (), die);
23685 else
23687 /* In certain situations, the lexical block containing a
23688 nested function can be optimized away, which results
23689 in the nested function die being orphaned. Likewise
23690 with the return type of that nested function. Force
23691 this to be a child of the containing function.
23693 It may happen that even the containing function got fully
23694 inlined and optimized out. In that case we are lost and
23695 assign the empty child. This should not be big issue as
23696 the function is likely unreachable too. */
23697 gcc_assert (node->created_for);
23699 if (DECL_P (node->created_for))
23700 origin = get_context_die (DECL_CONTEXT (node->created_for));
23701 else if (TYPE_P (node->created_for))
23702 origin = scope_die_for (node->created_for, comp_unit_die ());
23703 else
23704 origin = comp_unit_die ();
23706 add_child_die (origin, die);
23711 limbo_die_list = NULL;
23713 #if ENABLE_ASSERT_CHECKING
23715 dw_die_ref die = comp_unit_die (), c;
23716 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23718 #endif
23719 resolve_addr (comp_unit_die ());
23720 move_marked_base_types ();
23722 for (node = deferred_asm_name; node; node = node->next)
23724 tree decl = node->created_for;
23725 /* When generating LTO bytecode we can not generate new assembler
23726 names at this point and all important decls got theirs via
23727 free-lang-data. */
23728 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23729 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23731 add_linkage_attr (node->die, decl);
23732 move_linkage_attr (node->die);
23736 deferred_asm_name = NULL;
23738 /* Walk through the list of incomplete types again, trying once more to
23739 emit full debugging info for them. */
23740 retry_incomplete_types ();
23742 if (flag_eliminate_unused_debug_types)
23743 prune_unused_types ();
23745 /* Generate separate COMDAT sections for type DIEs. */
23746 if (use_debug_types)
23748 break_out_comdat_types (comp_unit_die ());
23750 /* Each new type_unit DIE was added to the limbo die list when created.
23751 Since these have all been added to comdat_type_list, clear the
23752 limbo die list. */
23753 limbo_die_list = NULL;
23755 /* For each new comdat type unit, copy declarations for incomplete
23756 types to make the new unit self-contained (i.e., no direct
23757 references to the main compile unit). */
23758 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23759 copy_decls_for_unworthy_types (ctnode->root_die);
23760 copy_decls_for_unworthy_types (comp_unit_die ());
23762 /* In the process of copying declarations from one unit to another,
23763 we may have left some declarations behind that are no longer
23764 referenced. Prune them. */
23765 prune_unused_types ();
23768 /* Generate separate CUs for each of the include files we've seen.
23769 They will go into limbo_die_list. */
23770 if (flag_eliminate_dwarf2_dups)
23771 break_out_includes (comp_unit_die ());
23773 /* Traverse the DIE's and add add sibling attributes to those DIE's
23774 that have children. */
23775 add_sibling_attributes (comp_unit_die ());
23776 for (node = limbo_die_list; node; node = node->next)
23777 add_sibling_attributes (node->die);
23778 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23779 add_sibling_attributes (ctnode->root_die);
23781 /* When splitting DWARF info, we put some attributes in the
23782 skeleton compile_unit DIE that remains in the .o, while
23783 most attributes go in the DWO compile_unit_die. */
23784 if (dwarf_split_debug_info)
23785 main_comp_unit_die = gen_compile_unit_die (NULL);
23786 else
23787 main_comp_unit_die = comp_unit_die ();
23789 /* Output a terminator label for the .text section. */
23790 switch_to_section (text_section);
23791 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23792 if (cold_text_section)
23794 switch_to_section (cold_text_section);
23795 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23798 /* We can only use the low/high_pc attributes if all of the code was
23799 in .text. */
23800 if (!have_multiple_function_sections
23801 || (dwarf_version < 3 && dwarf_strict))
23803 /* Don't add if the CU has no associated code. */
23804 if (text_section_used)
23805 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23806 text_end_label, true);
23808 else
23810 unsigned fde_idx;
23811 dw_fde_ref fde;
23812 bool range_list_added = false;
23814 if (text_section_used)
23815 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23816 text_end_label, &range_list_added, true);
23817 if (cold_text_section_used)
23818 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23819 cold_end_label, &range_list_added, true);
23821 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23823 if (DECL_IGNORED_P (fde->decl))
23824 continue;
23825 if (!fde->in_std_section)
23826 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23827 fde->dw_fde_end, &range_list_added,
23828 true);
23829 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23830 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23831 fde->dw_fde_second_end, &range_list_added,
23832 true);
23835 if (range_list_added)
23837 /* We need to give .debug_loc and .debug_ranges an appropriate
23838 "base address". Use zero so that these addresses become
23839 absolute. Historically, we've emitted the unexpected
23840 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23841 Emit both to give time for other tools to adapt. */
23842 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23843 if (! dwarf_strict && dwarf_version < 4)
23844 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23846 add_ranges (NULL);
23850 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23851 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23852 debug_line_section_label);
23854 if (have_macinfo)
23855 add_AT_macptr (comp_unit_die (),
23856 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23857 macinfo_section_label);
23859 if (dwarf_split_debug_info && addr_index_table != NULL)
23861 /* optimize_location_lists calculates the size of the lists,
23862 so index them first, and assign indices to the entries.
23863 Although optimize_location_lists will remove entries from
23864 the table, it only does so for duplicates, and therefore
23865 only reduces ref_counts to 1. */
23866 unsigned int index = 0;
23867 index_location_lists (comp_unit_die ());
23868 htab_traverse_noresize (addr_index_table,
23869 index_addr_table_entry, &index);
23871 if (have_location_lists)
23872 optimize_location_lists (comp_unit_die ());
23874 save_macinfo_strings ();
23876 if (dwarf_split_debug_info)
23878 unsigned int index = 0;
23880 /* Add attributes common to skeleton compile_units and
23881 type_units. Because these attributes include strings, it
23882 must be done before freezing the string table. Top-level
23883 skeleton die attrs are added when the skeleton type unit is
23884 created, so ensure it is created by this point. */
23885 add_top_level_skeleton_die_attrs (main_comp_unit_die);
23886 (void) get_skeleton_type_unit ();
23887 htab_traverse_noresize (debug_str_hash, index_string, &index);
23890 /* Output all of the compilation units. We put the main one last so that
23891 the offsets are available to output_pubnames. */
23892 for (node = limbo_die_list; node; node = node->next)
23893 output_comp_unit (node->die, 0);
23895 comdat_type_table.create (100);
23896 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23898 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
23900 /* Don't output duplicate types. */
23901 if (*slot != HTAB_EMPTY_ENTRY)
23902 continue;
23904 /* Add a pointer to the line table for the main compilation unit
23905 so that the debugger can make sense of DW_AT_decl_file
23906 attributes. */
23907 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23908 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23909 (!dwarf_split_debug_info
23910 ? debug_line_section_label
23911 : debug_skeleton_line_section_label));
23913 output_comdat_type_unit (ctnode);
23914 *slot = ctnode;
23916 comdat_type_table.dispose ();
23918 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23919 both the main_cu and all skeleton TUs. Making this call unconditional
23920 would end up either adding a second copy of the AT_pubnames attribute, or
23921 requiring a special case in add_top_level_skeleton_die_attrs. */
23922 if (!dwarf_split_debug_info)
23923 add_AT_pubnames (comp_unit_die ());
23925 if (dwarf_split_debug_info)
23927 int mark;
23928 unsigned char checksum[16];
23929 struct md5_ctx ctx;
23931 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23932 md5_init_ctx (&ctx);
23933 mark = 0;
23934 die_checksum (comp_unit_die (), &ctx, &mark);
23935 unmark_all_dies (comp_unit_die ());
23936 md5_finish_ctx (&ctx, checksum);
23938 /* Use the first 8 bytes of the checksum as the dwo_id,
23939 and add it to both comp-unit DIEs. */
23940 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23941 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23943 /* Add the base offset of the ranges table to the skeleton
23944 comp-unit DIE. */
23945 if (ranges_table_in_use)
23946 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23947 ranges_section_label);
23949 switch_to_section (debug_addr_section);
23950 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23951 output_addr_table ();
23954 /* Output the main compilation unit if non-empty or if .debug_macinfo
23955 or .debug_macro will be emitted. */
23956 output_comp_unit (comp_unit_die (), have_macinfo);
23958 if (dwarf_split_debug_info && info_section_emitted)
23959 output_skeleton_debug_sections (main_comp_unit_die);
23961 /* Output the abbreviation table. */
23962 if (abbrev_die_table_in_use != 1)
23964 switch_to_section (debug_abbrev_section);
23965 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23966 output_abbrev_section ();
23969 /* Output location list section if necessary. */
23970 if (have_location_lists)
23972 /* Output the location lists info. */
23973 switch_to_section (debug_loc_section);
23974 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23975 output_location_lists (comp_unit_die ());
23978 /* Output public names and types tables if necessary. */
23979 output_pubnames (pubname_table);
23980 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23981 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23982 simply won't look for the section. */
23983 output_pubnames (pubtype_table);
23985 /* Output the address range information if a CU (.debug_info section)
23986 was emitted. We output an empty table even if we had no functions
23987 to put in it. This because the consumer has no way to tell the
23988 difference between an empty table that we omitted and failure to
23989 generate a table that would have contained data. */
23990 if (info_section_emitted)
23992 unsigned long aranges_length = size_of_aranges ();
23994 switch_to_section (debug_aranges_section);
23995 output_aranges (aranges_length);
23998 /* Output ranges section if necessary. */
23999 if (ranges_table_in_use)
24001 switch_to_section (debug_ranges_section);
24002 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24003 output_ranges ();
24006 /* Have to end the macro section. */
24007 if (have_macinfo)
24009 switch_to_section (debug_macinfo_section);
24010 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24011 output_macinfo ();
24012 dw2_asm_output_data (1, 0, "End compilation unit");
24015 /* Output the source line correspondence table. We must do this
24016 even if there is no line information. Otherwise, on an empty
24017 translation unit, we will generate a present, but empty,
24018 .debug_info section. IRIX 6.5 `nm' will then complain when
24019 examining the file. This is done late so that any filenames
24020 used by the debug_info section are marked as 'used'. */
24021 switch_to_section (debug_line_section);
24022 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24023 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24024 output_line_info (false);
24026 if (dwarf_split_debug_info && info_section_emitted)
24028 switch_to_section (debug_skeleton_line_section);
24029 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24030 output_line_info (true);
24033 /* If we emitted any indirect strings, output the string table too. */
24034 if (debug_str_hash || skeleton_debug_str_hash)
24035 output_indirect_strings ();
24038 #include "gt-dwarf2out.h"