2013-06-18 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / dwarf2out.c
blobf42ad66353bcb61cac3c48b197cd1f6db2b1243d
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 dw_die_ref die = r->type;
7392 hashval_t h = 0;
7394 /* We can't use the address of the DIE for hashing, because
7395 that will make the order of the stub DIEs non-deterministic. */
7396 if (! die->comdat_type_p)
7397 /* We have a symbol; use it to compute a hash. */
7398 h = htab_hash_string (die->die_id.die_symbol);
7399 else
7401 /* We have a type signature; use a subset of the bits as the hash.
7402 The 8-byte signature is at least as large as hashval_t. */
7403 comdat_type_node_ref type_node = die->die_id.die_type_node;
7404 memcpy (&h, type_node->signature, sizeof (h));
7406 return h;
7409 inline bool
7410 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7412 return r1->type == r2->type;
7415 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7417 /* Return a pointer to the external_ref for references to DIE. */
7419 static struct external_ref *
7420 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7422 struct external_ref ref, *ref_p;
7423 external_ref **slot;
7425 ref.type = die;
7426 slot = map.find_slot (&ref, INSERT);
7427 if (*slot != HTAB_EMPTY_ENTRY)
7428 return *slot;
7430 ref_p = XCNEW (struct external_ref);
7431 ref_p->type = die;
7432 *slot = ref_p;
7433 return ref_p;
7436 /* Subroutine of optimize_external_refs, below.
7438 If we see a type skeleton, record it as our stub. If we see external
7439 references, remember how many we've seen. */
7441 static void
7442 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7444 dw_die_ref c;
7445 dw_attr_ref a;
7446 unsigned ix;
7447 struct external_ref *ref_p;
7449 if (is_type_die (die)
7450 && (c = get_AT_ref (die, DW_AT_signature)))
7452 /* This is a local skeleton; use it for local references. */
7453 ref_p = lookup_external_ref (map, c);
7454 ref_p->stub = die;
7457 /* Scan the DIE references, and remember any that refer to DIEs from
7458 other CUs (i.e. those which are not marked). */
7459 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7460 if (AT_class (a) == dw_val_class_die_ref
7461 && (c = AT_ref (a))->die_mark == 0
7462 && is_type_die (c))
7464 ref_p = lookup_external_ref (map, c);
7465 ref_p->n_refs++;
7468 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7471 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7472 points to an external_ref, DATA is the CU we're processing. If we don't
7473 already have a local stub, and we have multiple refs, build a stub. */
7476 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7478 struct external_ref *ref_p = *slot;
7480 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7482 /* We have multiple references to this type, so build a small stub.
7483 Both of these forms are a bit dodgy from the perspective of the
7484 DWARF standard, since technically they should have names. */
7485 dw_die_ref cu = data;
7486 dw_die_ref type = ref_p->type;
7487 dw_die_ref stub = NULL;
7489 if (type->comdat_type_p)
7491 /* If we refer to this type via sig8, use AT_signature. */
7492 stub = new_die (type->die_tag, cu, NULL_TREE);
7493 add_AT_die_ref (stub, DW_AT_signature, type);
7495 else
7497 /* Otherwise, use a typedef with no name. */
7498 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7499 add_AT_die_ref (stub, DW_AT_type, type);
7502 stub->die_mark++;
7503 ref_p->stub = stub;
7505 return 1;
7508 /* DIE is a unit; look through all the DIE references to see if there are
7509 any external references to types, and if so, create local stubs for
7510 them which will be applied in build_abbrev_table. This is useful because
7511 references to local DIEs are smaller. */
7513 static external_ref_hash_type
7514 optimize_external_refs (dw_die_ref die)
7516 external_ref_hash_type map;
7517 map.create (10);
7518 optimize_external_refs_1 (die, map);
7519 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7520 return map;
7523 /* The format of each DIE (and its attribute value pairs) is encoded in an
7524 abbreviation table. This routine builds the abbreviation table and assigns
7525 a unique abbreviation id for each abbreviation entry. The children of each
7526 die are visited recursively. */
7528 static void
7529 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7531 unsigned long abbrev_id;
7532 unsigned int n_alloc;
7533 dw_die_ref c;
7534 dw_attr_ref a;
7535 unsigned ix;
7537 /* Scan the DIE references, and replace any that refer to
7538 DIEs from other CUs (i.e. those which are not marked) with
7539 the local stubs we built in optimize_external_refs. */
7540 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7541 if (AT_class (a) == dw_val_class_die_ref
7542 && (c = AT_ref (a))->die_mark == 0)
7544 struct external_ref *ref_p;
7545 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7547 ref_p = lookup_external_ref (extern_map, c);
7548 if (ref_p->stub && ref_p->stub != die)
7549 change_AT_die_ref (a, ref_p->stub);
7550 else
7551 /* We aren't changing this reference, so mark it external. */
7552 set_AT_ref_external (a, 1);
7555 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7557 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7558 dw_attr_ref die_a, abbrev_a;
7559 unsigned ix;
7560 bool ok = true;
7562 if (abbrev->die_tag != die->die_tag)
7563 continue;
7564 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7565 continue;
7567 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7568 continue;
7570 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7572 abbrev_a = &(*abbrev->die_attr)[ix];
7573 if ((abbrev_a->dw_attr != die_a->dw_attr)
7574 || (value_format (abbrev_a) != value_format (die_a)))
7576 ok = false;
7577 break;
7580 if (ok)
7581 break;
7584 if (abbrev_id >= abbrev_die_table_in_use)
7586 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7588 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7589 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7590 n_alloc);
7592 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7593 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7594 abbrev_die_table_allocated = n_alloc;
7597 ++abbrev_die_table_in_use;
7598 abbrev_die_table[abbrev_id] = die;
7601 die->die_abbrev = abbrev_id;
7602 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7605 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7607 static int
7608 constant_size (unsigned HOST_WIDE_INT value)
7610 int log;
7612 if (value == 0)
7613 log = 0;
7614 else
7615 log = floor_log2 (value);
7617 log = log / 8;
7618 log = 1 << (floor_log2 (log) + 1);
7620 return log;
7623 /* Return the size of a DIE as it is represented in the
7624 .debug_info section. */
7626 static unsigned long
7627 size_of_die (dw_die_ref die)
7629 unsigned long size = 0;
7630 dw_attr_ref a;
7631 unsigned ix;
7632 enum dwarf_form form;
7634 size += size_of_uleb128 (die->die_abbrev);
7635 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7637 switch (AT_class (a))
7639 case dw_val_class_addr:
7640 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7642 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7643 size += size_of_uleb128 (AT_index (a));
7645 else
7646 size += DWARF2_ADDR_SIZE;
7647 break;
7648 case dw_val_class_offset:
7649 size += DWARF_OFFSET_SIZE;
7650 break;
7651 case dw_val_class_loc:
7653 unsigned long lsize = size_of_locs (AT_loc (a));
7655 /* Block length. */
7656 if (dwarf_version >= 4)
7657 size += size_of_uleb128 (lsize);
7658 else
7659 size += constant_size (lsize);
7660 size += lsize;
7662 break;
7663 case dw_val_class_loc_list:
7664 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7666 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7667 size += size_of_uleb128 (AT_index (a));
7669 else
7670 size += DWARF_OFFSET_SIZE;
7671 break;
7672 case dw_val_class_range_list:
7673 size += DWARF_OFFSET_SIZE;
7674 break;
7675 case dw_val_class_const:
7676 size += size_of_sleb128 (AT_int (a));
7677 break;
7678 case dw_val_class_unsigned_const:
7680 int csize = constant_size (AT_unsigned (a));
7681 if (dwarf_version == 3
7682 && a->dw_attr == DW_AT_data_member_location
7683 && csize >= 4)
7684 size += size_of_uleb128 (AT_unsigned (a));
7685 else
7686 size += csize;
7688 break;
7689 case dw_val_class_const_double:
7690 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7691 if (HOST_BITS_PER_WIDE_INT >= 64)
7692 size++; /* block */
7693 break;
7694 case dw_val_class_vec:
7695 size += constant_size (a->dw_attr_val.v.val_vec.length
7696 * a->dw_attr_val.v.val_vec.elt_size)
7697 + a->dw_attr_val.v.val_vec.length
7698 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7699 break;
7700 case dw_val_class_flag:
7701 if (dwarf_version >= 4)
7702 /* Currently all add_AT_flag calls pass in 1 as last argument,
7703 so DW_FORM_flag_present can be used. If that ever changes,
7704 we'll need to use DW_FORM_flag and have some optimization
7705 in build_abbrev_table that will change those to
7706 DW_FORM_flag_present if it is set to 1 in all DIEs using
7707 the same abbrev entry. */
7708 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7709 else
7710 size += 1;
7711 break;
7712 case dw_val_class_die_ref:
7713 if (AT_ref_external (a))
7715 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7716 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7717 is sized by target address length, whereas in DWARF3
7718 it's always sized as an offset. */
7719 if (use_debug_types)
7720 size += DWARF_TYPE_SIGNATURE_SIZE;
7721 else if (dwarf_version == 2)
7722 size += DWARF2_ADDR_SIZE;
7723 else
7724 size += DWARF_OFFSET_SIZE;
7726 else
7727 size += DWARF_OFFSET_SIZE;
7728 break;
7729 case dw_val_class_fde_ref:
7730 size += DWARF_OFFSET_SIZE;
7731 break;
7732 case dw_val_class_lbl_id:
7733 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7735 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7736 size += size_of_uleb128 (AT_index (a));
7738 else
7739 size += DWARF2_ADDR_SIZE;
7740 break;
7741 case dw_val_class_lineptr:
7742 case dw_val_class_macptr:
7743 size += DWARF_OFFSET_SIZE;
7744 break;
7745 case dw_val_class_str:
7746 form = AT_string_form (a);
7747 if (form == DW_FORM_strp)
7748 size += DWARF_OFFSET_SIZE;
7749 else if (form == DW_FORM_GNU_str_index)
7750 size += size_of_uleb128 (AT_index (a));
7751 else
7752 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7753 break;
7754 case dw_val_class_file:
7755 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7756 break;
7757 case dw_val_class_data8:
7758 size += 8;
7759 break;
7760 case dw_val_class_vms_delta:
7761 size += DWARF_OFFSET_SIZE;
7762 break;
7763 case dw_val_class_high_pc:
7764 size += DWARF2_ADDR_SIZE;
7765 break;
7766 default:
7767 gcc_unreachable ();
7771 return size;
7774 /* Size the debugging information associated with a given DIE. Visits the
7775 DIE's children recursively. Updates the global variable next_die_offset, on
7776 each time through. Uses the current value of next_die_offset to update the
7777 die_offset field in each DIE. */
7779 static void
7780 calc_die_sizes (dw_die_ref die)
7782 dw_die_ref c;
7784 gcc_assert (die->die_offset == 0
7785 || (unsigned long int) die->die_offset == next_die_offset);
7786 die->die_offset = next_die_offset;
7787 next_die_offset += size_of_die (die);
7789 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7791 if (die->die_child != NULL)
7792 /* Count the null byte used to terminate sibling lists. */
7793 next_die_offset += 1;
7796 /* Size just the base type children at the start of the CU.
7797 This is needed because build_abbrev needs to size locs
7798 and sizing of type based stack ops needs to know die_offset
7799 values for the base types. */
7801 static void
7802 calc_base_type_die_sizes (void)
7804 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7805 unsigned int i;
7806 dw_die_ref base_type;
7807 #if ENABLE_ASSERT_CHECKING
7808 dw_die_ref prev = comp_unit_die ()->die_child;
7809 #endif
7811 die_offset += size_of_die (comp_unit_die ());
7812 for (i = 0; base_types.iterate (i, &base_type); i++)
7814 #if ENABLE_ASSERT_CHECKING
7815 gcc_assert (base_type->die_offset == 0
7816 && prev->die_sib == base_type
7817 && base_type->die_child == NULL
7818 && base_type->die_abbrev);
7819 prev = base_type;
7820 #endif
7821 base_type->die_offset = die_offset;
7822 die_offset += size_of_die (base_type);
7826 /* Set the marks for a die and its children. We do this so
7827 that we know whether or not a reference needs to use FORM_ref_addr; only
7828 DIEs in the same CU will be marked. We used to clear out the offset
7829 and use that as the flag, but ran into ordering problems. */
7831 static void
7832 mark_dies (dw_die_ref die)
7834 dw_die_ref c;
7836 gcc_assert (!die->die_mark);
7838 die->die_mark = 1;
7839 FOR_EACH_CHILD (die, c, mark_dies (c));
7842 /* Clear the marks for a die and its children. */
7844 static void
7845 unmark_dies (dw_die_ref die)
7847 dw_die_ref c;
7849 if (! use_debug_types)
7850 gcc_assert (die->die_mark);
7852 die->die_mark = 0;
7853 FOR_EACH_CHILD (die, c, unmark_dies (c));
7856 /* Clear the marks for a die, its children and referred dies. */
7858 static void
7859 unmark_all_dies (dw_die_ref die)
7861 dw_die_ref c;
7862 dw_attr_ref a;
7863 unsigned ix;
7865 if (!die->die_mark)
7866 return;
7867 die->die_mark = 0;
7869 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7871 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7872 if (AT_class (a) == dw_val_class_die_ref)
7873 unmark_all_dies (AT_ref (a));
7876 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7877 generated for the compilation unit. */
7879 static unsigned long
7880 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7882 unsigned long size;
7883 unsigned i;
7884 pubname_ref p;
7886 size = DWARF_PUBNAMES_HEADER_SIZE;
7887 FOR_EACH_VEC_ELT (*names, i, p)
7888 if (names != pubtype_table
7889 || p->die->die_offset != 0
7890 || !flag_eliminate_unused_debug_types)
7891 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7893 size += DWARF_OFFSET_SIZE;
7894 return size;
7897 /* Return the size of the information in the .debug_aranges section. */
7899 static unsigned long
7900 size_of_aranges (void)
7902 unsigned long size;
7904 size = DWARF_ARANGES_HEADER_SIZE;
7906 /* Count the address/length pair for this compilation unit. */
7907 if (text_section_used)
7908 size += 2 * DWARF2_ADDR_SIZE;
7909 if (cold_text_section_used)
7910 size += 2 * DWARF2_ADDR_SIZE;
7911 if (have_multiple_function_sections)
7913 unsigned fde_idx;
7914 dw_fde_ref fde;
7916 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7918 if (DECL_IGNORED_P (fde->decl))
7919 continue;
7920 if (!fde->in_std_section)
7921 size += 2 * DWARF2_ADDR_SIZE;
7922 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7923 size += 2 * DWARF2_ADDR_SIZE;
7927 /* Count the two zero words used to terminated the address range table. */
7928 size += 2 * DWARF2_ADDR_SIZE;
7929 return size;
7932 /* Select the encoding of an attribute value. */
7934 static enum dwarf_form
7935 value_format (dw_attr_ref a)
7937 switch (AT_class (a))
7939 case dw_val_class_addr:
7940 /* Only very few attributes allow DW_FORM_addr. */
7941 switch (a->dw_attr)
7943 case DW_AT_low_pc:
7944 case DW_AT_high_pc:
7945 case DW_AT_entry_pc:
7946 case DW_AT_trampoline:
7947 return (AT_index (a) == NOT_INDEXED
7948 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7949 default:
7950 break;
7952 switch (DWARF2_ADDR_SIZE)
7954 case 1:
7955 return DW_FORM_data1;
7956 case 2:
7957 return DW_FORM_data2;
7958 case 4:
7959 return DW_FORM_data4;
7960 case 8:
7961 return DW_FORM_data8;
7962 default:
7963 gcc_unreachable ();
7965 case dw_val_class_range_list:
7966 case dw_val_class_loc_list:
7967 if (dwarf_version >= 4)
7968 return DW_FORM_sec_offset;
7969 /* FALLTHRU */
7970 case dw_val_class_vms_delta:
7971 case dw_val_class_offset:
7972 switch (DWARF_OFFSET_SIZE)
7974 case 4:
7975 return DW_FORM_data4;
7976 case 8:
7977 return DW_FORM_data8;
7978 default:
7979 gcc_unreachable ();
7981 case dw_val_class_loc:
7982 if (dwarf_version >= 4)
7983 return DW_FORM_exprloc;
7984 switch (constant_size (size_of_locs (AT_loc (a))))
7986 case 1:
7987 return DW_FORM_block1;
7988 case 2:
7989 return DW_FORM_block2;
7990 case 4:
7991 return DW_FORM_block4;
7992 default:
7993 gcc_unreachable ();
7995 case dw_val_class_const:
7996 return DW_FORM_sdata;
7997 case dw_val_class_unsigned_const:
7998 switch (constant_size (AT_unsigned (a)))
8000 case 1:
8001 return DW_FORM_data1;
8002 case 2:
8003 return DW_FORM_data2;
8004 case 4:
8005 /* In DWARF3 DW_AT_data_member_location with
8006 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8007 constant, so we need to use DW_FORM_udata if we need
8008 a large constant. */
8009 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8010 return DW_FORM_udata;
8011 return DW_FORM_data4;
8012 case 8:
8013 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8014 return DW_FORM_udata;
8015 return DW_FORM_data8;
8016 default:
8017 gcc_unreachable ();
8019 case dw_val_class_const_double:
8020 switch (HOST_BITS_PER_WIDE_INT)
8022 case 8:
8023 return DW_FORM_data2;
8024 case 16:
8025 return DW_FORM_data4;
8026 case 32:
8027 return DW_FORM_data8;
8028 case 64:
8029 default:
8030 return DW_FORM_block1;
8032 case dw_val_class_vec:
8033 switch (constant_size (a->dw_attr_val.v.val_vec.length
8034 * a->dw_attr_val.v.val_vec.elt_size))
8036 case 1:
8037 return DW_FORM_block1;
8038 case 2:
8039 return DW_FORM_block2;
8040 case 4:
8041 return DW_FORM_block4;
8042 default:
8043 gcc_unreachable ();
8045 case dw_val_class_flag:
8046 if (dwarf_version >= 4)
8048 /* Currently all add_AT_flag calls pass in 1 as last argument,
8049 so DW_FORM_flag_present can be used. If that ever changes,
8050 we'll need to use DW_FORM_flag and have some optimization
8051 in build_abbrev_table that will change those to
8052 DW_FORM_flag_present if it is set to 1 in all DIEs using
8053 the same abbrev entry. */
8054 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8055 return DW_FORM_flag_present;
8057 return DW_FORM_flag;
8058 case dw_val_class_die_ref:
8059 if (AT_ref_external (a))
8060 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8061 else
8062 return DW_FORM_ref;
8063 case dw_val_class_fde_ref:
8064 return DW_FORM_data;
8065 case dw_val_class_lbl_id:
8066 return (AT_index (a) == NOT_INDEXED
8067 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8068 case dw_val_class_lineptr:
8069 case dw_val_class_macptr:
8070 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8071 case dw_val_class_str:
8072 return AT_string_form (a);
8073 case dw_val_class_file:
8074 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8076 case 1:
8077 return DW_FORM_data1;
8078 case 2:
8079 return DW_FORM_data2;
8080 case 4:
8081 return DW_FORM_data4;
8082 default:
8083 gcc_unreachable ();
8086 case dw_val_class_data8:
8087 return DW_FORM_data8;
8089 case dw_val_class_high_pc:
8090 switch (DWARF2_ADDR_SIZE)
8092 case 1:
8093 return DW_FORM_data1;
8094 case 2:
8095 return DW_FORM_data2;
8096 case 4:
8097 return DW_FORM_data4;
8098 case 8:
8099 return DW_FORM_data8;
8100 default:
8101 gcc_unreachable ();
8104 default:
8105 gcc_unreachable ();
8109 /* Output the encoding of an attribute value. */
8111 static void
8112 output_value_format (dw_attr_ref a)
8114 enum dwarf_form form = value_format (a);
8116 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8119 /* Given a die and id, produce the appropriate abbreviations. */
8121 static void
8122 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8124 unsigned ix;
8125 dw_attr_ref a_attr;
8127 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8128 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8129 dwarf_tag_name (abbrev->die_tag));
8131 if (abbrev->die_child != NULL)
8132 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8133 else
8134 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8136 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8138 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8139 dwarf_attr_name (a_attr->dw_attr));
8140 output_value_format (a_attr);
8143 dw2_asm_output_data (1, 0, NULL);
8144 dw2_asm_output_data (1, 0, NULL);
8148 /* Output the .debug_abbrev section which defines the DIE abbreviation
8149 table. */
8151 static void
8152 output_abbrev_section (void)
8154 unsigned long abbrev_id;
8156 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8157 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8159 /* Terminate the table. */
8160 dw2_asm_output_data (1, 0, NULL);
8163 /* Output a symbol we can use to refer to this DIE from another CU. */
8165 static inline void
8166 output_die_symbol (dw_die_ref die)
8168 const char *sym = die->die_id.die_symbol;
8170 gcc_assert (!die->comdat_type_p);
8172 if (sym == 0)
8173 return;
8175 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8176 /* We make these global, not weak; if the target doesn't support
8177 .linkonce, it doesn't support combining the sections, so debugging
8178 will break. */
8179 targetm.asm_out.globalize_label (asm_out_file, sym);
8181 ASM_OUTPUT_LABEL (asm_out_file, sym);
8184 /* Return a new location list, given the begin and end range, and the
8185 expression. */
8187 static inline dw_loc_list_ref
8188 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8189 const char *section)
8191 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8193 retlist->begin = begin;
8194 retlist->begin_entry = NULL;
8195 retlist->end = end;
8196 retlist->expr = expr;
8197 retlist->section = section;
8199 return retlist;
8202 /* Generate a new internal symbol for this location list node, if it
8203 hasn't got one yet. */
8205 static inline void
8206 gen_llsym (dw_loc_list_ref list)
8208 gcc_assert (!list->ll_symbol);
8209 list->ll_symbol = gen_internal_sym ("LLST");
8212 /* Output the location list given to us. */
8214 static void
8215 output_loc_list (dw_loc_list_ref list_head)
8217 dw_loc_list_ref curr = list_head;
8219 if (list_head->emitted)
8220 return;
8221 list_head->emitted = true;
8223 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8225 /* Walk the location list, and output each range + expression. */
8226 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8228 unsigned long size;
8229 /* Don't output an entry that starts and ends at the same address. */
8230 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8231 continue;
8232 size = size_of_locs (curr->expr);
8233 /* If the expression is too large, drop it on the floor. We could
8234 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8235 in the expression, but >= 64KB expressions for a single value
8236 in a single range are unlikely very useful. */
8237 if (size > 0xffff)
8238 continue;
8239 if (dwarf_split_debug_info)
8241 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8242 "Location list start/length entry (%s)",
8243 list_head->ll_symbol);
8244 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8245 "Location list range start index (%s)",
8246 curr->begin);
8247 /* The length field is 4 bytes. If we ever need to support
8248 an 8-byte length, we can add a new DW_LLE code or fall back
8249 to DW_LLE_GNU_start_end_entry. */
8250 dw2_asm_output_delta (4, curr->end, curr->begin,
8251 "Location list range length (%s)",
8252 list_head->ll_symbol);
8254 else if (!have_multiple_function_sections)
8256 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8257 "Location list begin address (%s)",
8258 list_head->ll_symbol);
8259 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8260 "Location list end address (%s)",
8261 list_head->ll_symbol);
8263 else
8265 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8266 "Location list begin address (%s)",
8267 list_head->ll_symbol);
8268 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8269 "Location list end address (%s)",
8270 list_head->ll_symbol);
8273 /* Output the block length for this list of location operations. */
8274 gcc_assert (size <= 0xffff);
8275 dw2_asm_output_data (2, size, "%s", "Location expression size");
8277 output_loc_sequence (curr->expr, -1);
8280 if (dwarf_split_debug_info)
8281 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8282 "Location list terminator (%s)",
8283 list_head->ll_symbol);
8284 else
8286 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8287 "Location list terminator begin (%s)",
8288 list_head->ll_symbol);
8289 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8290 "Location list terminator end (%s)",
8291 list_head->ll_symbol);
8295 /* Output a range_list offset into the debug_range section. Emit a
8296 relocated reference if val_entry is NULL, otherwise, emit an
8297 indirect reference. */
8299 static void
8300 output_range_list_offset (dw_attr_ref a)
8302 const char *name = dwarf_attr_name (a->dw_attr);
8304 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8306 char *p = strchr (ranges_section_label, '\0');
8307 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8308 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8309 debug_ranges_section, "%s", name);
8310 *p = '\0';
8312 else
8313 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8314 "%s (offset from %s)", name, ranges_section_label);
8317 /* Output the offset into the debug_loc section. */
8319 static void
8320 output_loc_list_offset (dw_attr_ref a)
8322 char *sym = AT_loc_list (a)->ll_symbol;
8324 gcc_assert (sym);
8325 if (dwarf_split_debug_info)
8326 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8327 "%s", dwarf_attr_name (a->dw_attr));
8328 else
8329 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8330 "%s", dwarf_attr_name (a->dw_attr));
8333 /* Output an attribute's index or value appropriately. */
8335 static void
8336 output_attr_index_or_value (dw_attr_ref a)
8338 const char *name = dwarf_attr_name (a->dw_attr);
8340 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8342 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8343 return;
8345 switch (AT_class (a))
8347 case dw_val_class_addr:
8348 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8349 break;
8350 case dw_val_class_high_pc:
8351 case dw_val_class_lbl_id:
8352 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8353 break;
8354 case dw_val_class_loc_list:
8355 output_loc_list_offset (a);
8356 break;
8357 default:
8358 gcc_unreachable ();
8362 /* Output a type signature. */
8364 static inline void
8365 output_signature (const char *sig, const char *name)
8367 int i;
8369 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8370 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8373 /* Output the DIE and its attributes. Called recursively to generate
8374 the definitions of each child DIE. */
8376 static void
8377 output_die (dw_die_ref die)
8379 dw_attr_ref a;
8380 dw_die_ref c;
8381 unsigned long size;
8382 unsigned ix;
8384 /* If someone in another CU might refer to us, set up a symbol for
8385 them to point to. */
8386 if (! die->comdat_type_p && die->die_id.die_symbol)
8387 output_die_symbol (die);
8389 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8390 (unsigned long)die->die_offset,
8391 dwarf_tag_name (die->die_tag));
8393 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8395 const char *name = dwarf_attr_name (a->dw_attr);
8397 switch (AT_class (a))
8399 case dw_val_class_addr:
8400 output_attr_index_or_value (a);
8401 break;
8403 case dw_val_class_offset:
8404 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8405 "%s", name);
8406 break;
8408 case dw_val_class_range_list:
8409 output_range_list_offset (a);
8410 break;
8412 case dw_val_class_loc:
8413 size = size_of_locs (AT_loc (a));
8415 /* Output the block length for this list of location operations. */
8416 if (dwarf_version >= 4)
8417 dw2_asm_output_data_uleb128 (size, "%s", name);
8418 else
8419 dw2_asm_output_data (constant_size (size), size, "%s", name);
8421 output_loc_sequence (AT_loc (a), -1);
8422 break;
8424 case dw_val_class_const:
8425 /* ??? It would be slightly more efficient to use a scheme like is
8426 used for unsigned constants below, but gdb 4.x does not sign
8427 extend. Gdb 5.x does sign extend. */
8428 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8429 break;
8431 case dw_val_class_unsigned_const:
8433 int csize = constant_size (AT_unsigned (a));
8434 if (dwarf_version == 3
8435 && a->dw_attr == DW_AT_data_member_location
8436 && csize >= 4)
8437 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8438 else
8439 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8441 break;
8443 case dw_val_class_const_double:
8445 unsigned HOST_WIDE_INT first, second;
8447 if (HOST_BITS_PER_WIDE_INT >= 64)
8448 dw2_asm_output_data (1,
8449 HOST_BITS_PER_DOUBLE_INT
8450 / HOST_BITS_PER_CHAR,
8451 NULL);
8453 if (WORDS_BIG_ENDIAN)
8455 first = a->dw_attr_val.v.val_double.high;
8456 second = a->dw_attr_val.v.val_double.low;
8458 else
8460 first = a->dw_attr_val.v.val_double.low;
8461 second = a->dw_attr_val.v.val_double.high;
8464 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8465 first, "%s", name);
8466 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8467 second, NULL);
8469 break;
8471 case dw_val_class_vec:
8473 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8474 unsigned int len = a->dw_attr_val.v.val_vec.length;
8475 unsigned int i;
8476 unsigned char *p;
8478 dw2_asm_output_data (constant_size (len * elt_size),
8479 len * elt_size, "%s", name);
8480 if (elt_size > sizeof (HOST_WIDE_INT))
8482 elt_size /= 2;
8483 len *= 2;
8485 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8486 i < len;
8487 i++, p += elt_size)
8488 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8489 "fp or vector constant word %u", i);
8490 break;
8493 case dw_val_class_flag:
8494 if (dwarf_version >= 4)
8496 /* Currently all add_AT_flag calls pass in 1 as last argument,
8497 so DW_FORM_flag_present can be used. If that ever changes,
8498 we'll need to use DW_FORM_flag and have some optimization
8499 in build_abbrev_table that will change those to
8500 DW_FORM_flag_present if it is set to 1 in all DIEs using
8501 the same abbrev entry. */
8502 gcc_assert (AT_flag (a) == 1);
8503 if (flag_debug_asm)
8504 fprintf (asm_out_file, "\t\t\t%s %s\n",
8505 ASM_COMMENT_START, name);
8506 break;
8508 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8509 break;
8511 case dw_val_class_loc_list:
8512 output_attr_index_or_value (a);
8513 break;
8515 case dw_val_class_die_ref:
8516 if (AT_ref_external (a))
8518 if (AT_ref (a)->comdat_type_p)
8520 comdat_type_node_ref type_node =
8521 AT_ref (a)->die_id.die_type_node;
8523 gcc_assert (type_node);
8524 output_signature (type_node->signature, name);
8526 else
8528 const char *sym = AT_ref (a)->die_id.die_symbol;
8529 int size;
8531 gcc_assert (sym);
8532 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8533 length, whereas in DWARF3 it's always sized as an
8534 offset. */
8535 if (dwarf_version == 2)
8536 size = DWARF2_ADDR_SIZE;
8537 else
8538 size = DWARF_OFFSET_SIZE;
8539 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8540 name);
8543 else
8545 gcc_assert (AT_ref (a)->die_offset);
8546 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8547 "%s", name);
8549 break;
8551 case dw_val_class_fde_ref:
8553 char l1[20];
8555 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8556 a->dw_attr_val.v.val_fde_index * 2);
8557 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8558 "%s", name);
8560 break;
8562 case dw_val_class_vms_delta:
8563 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8564 AT_vms_delta2 (a), AT_vms_delta1 (a),
8565 "%s", name);
8566 break;
8568 case dw_val_class_lbl_id:
8569 output_attr_index_or_value (a);
8570 break;
8572 case dw_val_class_lineptr:
8573 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8574 debug_line_section, "%s", name);
8575 break;
8577 case dw_val_class_macptr:
8578 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8579 debug_macinfo_section, "%s", name);
8580 break;
8582 case dw_val_class_str:
8583 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8584 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8585 a->dw_attr_val.v.val_str->label,
8586 debug_str_section,
8587 "%s: \"%s\"", name, AT_string (a));
8588 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8589 dw2_asm_output_data_uleb128 (AT_index (a),
8590 "%s: \"%s\"", name, AT_string (a));
8591 else
8592 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8593 break;
8595 case dw_val_class_file:
8597 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8599 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8600 a->dw_attr_val.v.val_file->filename);
8601 break;
8604 case dw_val_class_data8:
8606 int i;
8608 for (i = 0; i < 8; i++)
8609 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8610 i == 0 ? "%s" : NULL, name);
8611 break;
8614 case dw_val_class_high_pc:
8615 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8616 get_AT_low_pc (die), "DW_AT_high_pc");
8617 break;
8619 default:
8620 gcc_unreachable ();
8624 FOR_EACH_CHILD (die, c, output_die (c));
8626 /* Add null byte to terminate sibling list. */
8627 if (die->die_child != NULL)
8628 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8629 (unsigned long) die->die_offset);
8632 /* Output the compilation unit that appears at the beginning of the
8633 .debug_info section, and precedes the DIE descriptions. */
8635 static void
8636 output_compilation_unit_header (void)
8638 int ver = dwarf_version;
8640 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8641 dw2_asm_output_data (4, 0xffffffff,
8642 "Initial length escape value indicating 64-bit DWARF extension");
8643 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8644 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8645 "Length of Compilation Unit Info");
8646 dw2_asm_output_data (2, ver, "DWARF version number");
8647 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8648 debug_abbrev_section,
8649 "Offset Into Abbrev. Section");
8650 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8653 /* Output the compilation unit DIE and its children. */
8655 static void
8656 output_comp_unit (dw_die_ref die, int output_if_empty)
8658 const char *secname, *oldsym;
8659 char *tmp;
8660 external_ref_hash_type extern_map;
8662 /* Unless we are outputting main CU, we may throw away empty ones. */
8663 if (!output_if_empty && die->die_child == NULL)
8664 return;
8666 /* Even if there are no children of this DIE, we must output the information
8667 about the compilation unit. Otherwise, on an empty translation unit, we
8668 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8669 will then complain when examining the file. First mark all the DIEs in
8670 this CU so we know which get local refs. */
8671 mark_dies (die);
8673 extern_map = optimize_external_refs (die);
8675 build_abbrev_table (die, extern_map);
8677 extern_map.dispose ();
8679 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8680 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8681 calc_die_sizes (die);
8683 oldsym = die->die_id.die_symbol;
8684 if (oldsym)
8686 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8688 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8689 secname = tmp;
8690 die->die_id.die_symbol = NULL;
8691 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8693 else
8695 switch_to_section (debug_info_section);
8696 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8697 info_section_emitted = true;
8700 /* Output debugging information. */
8701 output_compilation_unit_header ();
8702 output_die (die);
8704 /* Leave the marks on the main CU, so we can check them in
8705 output_pubnames. */
8706 if (oldsym)
8708 unmark_dies (die);
8709 die->die_id.die_symbol = oldsym;
8713 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8714 and .debug_pubtypes. This is configured per-target, but can be
8715 overridden by the -gpubnames or -gno-pubnames options. */
8717 static inline bool
8718 want_pubnames (void)
8720 return (debug_generate_pub_sections != -1
8721 ? debug_generate_pub_sections
8722 : targetm.want_debug_pub_sections);
8725 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8727 static void
8728 add_AT_pubnames (dw_die_ref die)
8730 if (want_pubnames ())
8731 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8734 /* Add a string attribute value to a skeleton DIE. */
8736 static inline void
8737 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8738 const char *str)
8740 dw_attr_node attr;
8741 struct indirect_string_node *node;
8743 if (! skeleton_debug_str_hash)
8744 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8745 debug_str_eq, NULL);
8747 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8748 find_string_form (node);
8749 if (node->form == DW_FORM_GNU_str_index)
8750 node->form = DW_FORM_strp;
8752 attr.dw_attr = attr_kind;
8753 attr.dw_attr_val.val_class = dw_val_class_str;
8754 attr.dw_attr_val.val_entry = NULL;
8755 attr.dw_attr_val.v.val_str = node;
8756 add_dwarf_attr (die, &attr);
8759 /* Helper function to generate top-level dies for skeleton debug_info and
8760 debug_types. */
8762 static void
8763 add_top_level_skeleton_die_attrs (dw_die_ref die)
8765 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8766 const char *comp_dir = comp_dir_string ();
8768 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8769 if (comp_dir != NULL)
8770 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8771 add_AT_pubnames (die);
8772 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8775 /* Return the single type-unit die for skeleton type units. */
8777 static dw_die_ref
8778 get_skeleton_type_unit (void)
8780 /* For dwarf_split_debug_sections with use_type info, all type units in the
8781 skeleton sections have identical dies (but different headers). This
8782 single die will be output many times. */
8784 static dw_die_ref skeleton_type_unit = NULL;
8786 if (skeleton_type_unit == NULL)
8788 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8789 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8790 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8792 return skeleton_type_unit;
8795 /* Output skeleton debug sections that point to the dwo file. */
8797 static void
8798 output_skeleton_debug_sections (dw_die_ref comp_unit)
8800 /* These attributes will be found in the full debug_info section. */
8801 remove_AT (comp_unit, DW_AT_producer);
8802 remove_AT (comp_unit, DW_AT_language);
8804 switch_to_section (debug_skeleton_info_section);
8805 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8807 /* Produce the skeleton compilation-unit header. This one differs enough from
8808 a normal CU header that it's better not to call output_compilation_unit
8809 header. */
8810 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8811 dw2_asm_output_data (4, 0xffffffff,
8812 "Initial length escape value indicating 64-bit DWARF extension");
8814 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8815 DWARF_COMPILE_UNIT_HEADER_SIZE
8816 - DWARF_INITIAL_LENGTH_SIZE
8817 + size_of_die (comp_unit),
8818 "Length of Compilation Unit Info");
8819 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8820 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8821 debug_abbrev_section,
8822 "Offset Into Abbrev. Section");
8823 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8825 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8826 output_die (comp_unit);
8828 /* Build the skeleton debug_abbrev section. */
8829 switch_to_section (debug_skeleton_abbrev_section);
8830 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8832 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8833 if (use_debug_types)
8834 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8836 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8839 /* Output a comdat type unit DIE and its children. */
8841 static void
8842 output_comdat_type_unit (comdat_type_node *node)
8844 const char *secname;
8845 char *tmp;
8846 int i;
8847 #if defined (OBJECT_FORMAT_ELF)
8848 tree comdat_key;
8849 #endif
8850 external_ref_hash_type extern_map;
8852 /* First mark all the DIEs in this CU so we know which get local refs. */
8853 mark_dies (node->root_die);
8855 extern_map = optimize_external_refs (node->root_die);
8857 build_abbrev_table (node->root_die, extern_map);
8859 extern_map.dispose ();
8861 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8862 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8863 calc_die_sizes (node->root_die);
8865 #if defined (OBJECT_FORMAT_ELF)
8866 if (!dwarf_split_debug_info)
8867 secname = ".debug_types";
8868 else
8869 secname = ".debug_types.dwo";
8871 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8872 sprintf (tmp, "wt.");
8873 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8874 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8875 comdat_key = get_identifier (tmp);
8876 targetm.asm_out.named_section (secname,
8877 SECTION_DEBUG | SECTION_LINKONCE,
8878 comdat_key);
8879 #else
8880 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8881 sprintf (tmp, ".gnu.linkonce.wt.");
8882 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8883 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8884 secname = tmp;
8885 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8886 #endif
8888 /* Output debugging information. */
8889 output_compilation_unit_header ();
8890 output_signature (node->signature, "Type Signature");
8891 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8892 "Offset to Type DIE");
8893 output_die (node->root_die);
8895 unmark_dies (node->root_die);
8897 #if defined (OBJECT_FORMAT_ELF)
8898 if (dwarf_split_debug_info)
8900 /* Produce the skeleton type-unit header. */
8901 const char *secname = ".debug_types";
8903 targetm.asm_out.named_section (secname,
8904 SECTION_DEBUG | SECTION_LINKONCE,
8905 comdat_key);
8906 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8907 dw2_asm_output_data (4, 0xffffffff,
8908 "Initial length escape value indicating 64-bit DWARF extension");
8910 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8911 DWARF_COMPILE_UNIT_HEADER_SIZE
8912 - DWARF_INITIAL_LENGTH_SIZE
8913 + size_of_die (get_skeleton_type_unit ())
8914 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8915 "Length of Type Unit Info");
8916 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8917 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8918 debug_skeleton_abbrev_section_label,
8919 debug_abbrev_section,
8920 "Offset Into Abbrev. Section");
8921 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8922 output_signature (node->signature, "Type Signature");
8923 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8925 output_die (get_skeleton_type_unit ());
8927 #endif
8930 /* Return the DWARF2/3 pubname associated with a decl. */
8932 static const char *
8933 dwarf2_name (tree decl, int scope)
8935 if (DECL_NAMELESS (decl))
8936 return NULL;
8937 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8940 /* Add a new entry to .debug_pubnames if appropriate. */
8942 static void
8943 add_pubname_string (const char *str, dw_die_ref die)
8945 pubname_entry e;
8947 e.die = die;
8948 e.name = xstrdup (str);
8949 vec_safe_push (pubname_table, e);
8952 static void
8953 add_pubname (tree decl, dw_die_ref die)
8955 if (!want_pubnames ())
8956 return;
8958 /* Don't add items to the table when we expect that the consumer will have
8959 just read the enclosing die. For example, if the consumer is looking at a
8960 class_member, it will either be inside the class already, or will have just
8961 looked up the class to find the member. Either way, searching the class is
8962 faster than searching the index. */
8963 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8964 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8966 const char *name = dwarf2_name (decl, 1);
8968 if (name)
8969 add_pubname_string (name, die);
8973 /* Add an enumerator to the pubnames section. */
8975 static void
8976 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8978 pubname_entry e;
8980 gcc_assert (scope_name);
8981 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8982 e.die = die;
8983 vec_safe_push (pubname_table, e);
8986 /* Add a new entry to .debug_pubtypes if appropriate. */
8988 static void
8989 add_pubtype (tree decl, dw_die_ref die)
8991 pubname_entry e;
8993 if (!want_pubnames ())
8994 return;
8996 if ((TREE_PUBLIC (decl)
8997 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8998 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9000 tree scope = NULL;
9001 const char *scope_name = "";
9002 const char *sep = is_cxx () ? "::" : ".";
9003 const char *name;
9005 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9006 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9008 scope_name = lang_hooks.dwarf_name (scope, 1);
9009 if (scope_name != NULL && scope_name[0] != '\0')
9010 scope_name = concat (scope_name, sep, NULL);
9011 else
9012 scope_name = "";
9015 if (TYPE_P (decl))
9016 name = type_tag (decl);
9017 else
9018 name = lang_hooks.dwarf_name (decl, 1);
9020 /* If we don't have a name for the type, there's no point in adding
9021 it to the table. */
9022 if (name != NULL && name[0] != '\0')
9024 e.die = die;
9025 e.name = concat (scope_name, name, NULL);
9026 vec_safe_push (pubtype_table, e);
9029 /* Although it might be more consistent to add the pubinfo for the
9030 enumerators as their dies are created, they should only be added if the
9031 enum type meets the criteria above. So rather than re-check the parent
9032 enum type whenever an enumerator die is created, just output them all
9033 here. This isn't protected by the name conditional because anonymous
9034 enums don't have names. */
9035 if (die->die_tag == DW_TAG_enumeration_type)
9037 dw_die_ref c;
9039 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9044 /* Output the public names table used to speed up access to externally
9045 visible names; or the public types table used to find type definitions. */
9047 static void
9048 output_pubnames (vec<pubname_entry, va_gc> *names)
9050 unsigned i;
9051 unsigned long pubnames_length = size_of_pubnames (names);
9052 pubname_ref pub;
9054 if (!want_pubnames () || !info_section_emitted)
9055 return;
9056 if (names == pubname_table)
9057 switch_to_section (debug_pubnames_section);
9058 else
9059 switch_to_section (debug_pubtypes_section);
9060 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9061 dw2_asm_output_data (4, 0xffffffff,
9062 "Initial length escape value indicating 64-bit DWARF extension");
9063 if (names == pubname_table)
9064 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9065 "Length of Public Names Info");
9066 else
9067 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9068 "Length of Public Type Names Info");
9069 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
9070 dw2_asm_output_data (2, 2, "DWARF Version");
9071 if (dwarf_split_debug_info)
9072 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9073 debug_skeleton_info_section,
9074 "Offset of Compilation Unit Info");
9075 else
9076 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9077 debug_info_section,
9078 "Offset of Compilation Unit Info");
9079 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9080 "Compilation Unit Length");
9082 FOR_EACH_VEC_ELT (*names, i, pub)
9084 /* Enumerator names are part of the pubname table, but the parent
9085 DW_TAG_enumeration_type die may have been pruned. Don't output
9086 them if that is the case. */
9087 if (pub->die->die_tag == DW_TAG_enumerator &&
9088 (pub->die->die_parent == NULL
9089 || !pub->die->die_parent->die_perennial_p))
9090 continue;
9092 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9093 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9094 gcc_assert (pub->die->die_mark);
9096 if (names != pubtype_table
9097 || pub->die->die_offset != 0
9098 || !flag_eliminate_unused_debug_types)
9100 dw_offset die_offset = pub->die->die_offset;
9102 /* If we're putting types in their own .debug_types sections,
9103 the .debug_pubtypes table will still point to the compile
9104 unit (not the type unit), so we want to use the offset of
9105 the skeleton DIE (if there is one). */
9106 if (pub->die->comdat_type_p && names == pubtype_table)
9108 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9110 if (type_node != NULL)
9111 die_offset = (type_node->skeleton_die != NULL
9112 ? type_node->skeleton_die->die_offset
9113 : 0);
9116 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9118 dw2_asm_output_nstring (pub->name, -1, "external name");
9122 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9125 /* Output the information that goes into the .debug_aranges table.
9126 Namely, define the beginning and ending address range of the
9127 text section generated for this compilation unit. */
9129 static void
9130 output_aranges (unsigned long aranges_length)
9132 unsigned i;
9134 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9135 dw2_asm_output_data (4, 0xffffffff,
9136 "Initial length escape value indicating 64-bit DWARF extension");
9137 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9138 "Length of Address Ranges Info");
9139 /* Version number for aranges is still 2, even in DWARF3. */
9140 dw2_asm_output_data (2, 2, "DWARF Version");
9141 if (dwarf_split_debug_info)
9142 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9143 debug_skeleton_info_section,
9144 "Offset of Compilation Unit Info");
9145 else
9146 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9147 debug_info_section,
9148 "Offset of Compilation Unit Info");
9149 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9150 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9152 /* We need to align to twice the pointer size here. */
9153 if (DWARF_ARANGES_PAD_SIZE)
9155 /* Pad using a 2 byte words so that padding is correct for any
9156 pointer size. */
9157 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9158 2 * DWARF2_ADDR_SIZE);
9159 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9160 dw2_asm_output_data (2, 0, NULL);
9163 /* It is necessary not to output these entries if the sections were
9164 not used; if the sections were not used, the length will be 0 and
9165 the address may end up as 0 if the section is discarded by ld
9166 --gc-sections, leaving an invalid (0, 0) entry that can be
9167 confused with the terminator. */
9168 if (text_section_used)
9170 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9171 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9172 text_section_label, "Length");
9174 if (cold_text_section_used)
9176 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9177 "Address");
9178 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9179 cold_text_section_label, "Length");
9182 if (have_multiple_function_sections)
9184 unsigned fde_idx;
9185 dw_fde_ref fde;
9187 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9189 if (DECL_IGNORED_P (fde->decl))
9190 continue;
9191 if (!fde->in_std_section)
9193 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9194 "Address");
9195 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9196 fde->dw_fde_begin, "Length");
9198 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9200 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9201 "Address");
9202 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9203 fde->dw_fde_second_begin, "Length");
9208 /* Output the terminator words. */
9209 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9210 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9213 /* Add a new entry to .debug_ranges. Return the offset at which it
9214 was placed. */
9216 static unsigned int
9217 add_ranges_num (int num)
9219 unsigned int in_use = ranges_table_in_use;
9221 if (in_use == ranges_table_allocated)
9223 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9224 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9225 ranges_table_allocated);
9226 memset (ranges_table + ranges_table_in_use, 0,
9227 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9230 ranges_table[in_use].num = num;
9231 ranges_table_in_use = in_use + 1;
9233 return in_use * 2 * DWARF2_ADDR_SIZE;
9236 /* Add a new entry to .debug_ranges corresponding to a block, or a
9237 range terminator if BLOCK is NULL. */
9239 static unsigned int
9240 add_ranges (const_tree block)
9242 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9245 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9246 When using dwarf_split_debug_info, address attributes in dies destined
9247 for the final executable should be direct references--setting the
9248 parameter force_direct ensures this behavior. */
9250 static void
9251 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9252 bool *added, bool force_direct)
9254 unsigned int in_use = ranges_by_label_in_use;
9255 unsigned int offset;
9257 if (in_use == ranges_by_label_allocated)
9259 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9260 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9261 ranges_by_label,
9262 ranges_by_label_allocated);
9263 memset (ranges_by_label + ranges_by_label_in_use, 0,
9264 RANGES_TABLE_INCREMENT
9265 * sizeof (struct dw_ranges_by_label_struct));
9268 ranges_by_label[in_use].begin = begin;
9269 ranges_by_label[in_use].end = end;
9270 ranges_by_label_in_use = in_use + 1;
9272 offset = add_ranges_num (-(int)in_use - 1);
9273 if (!*added)
9275 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9276 *added = true;
9280 static void
9281 output_ranges (void)
9283 unsigned i;
9284 static const char *const start_fmt = "Offset %#x";
9285 const char *fmt = start_fmt;
9287 for (i = 0; i < ranges_table_in_use; i++)
9289 int block_num = ranges_table[i].num;
9291 if (block_num > 0)
9293 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9294 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9296 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9297 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9299 /* If all code is in the text section, then the compilation
9300 unit base address defaults to DW_AT_low_pc, which is the
9301 base of the text section. */
9302 if (!have_multiple_function_sections)
9304 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9305 text_section_label,
9306 fmt, i * 2 * DWARF2_ADDR_SIZE);
9307 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9308 text_section_label, NULL);
9311 /* Otherwise, the compilation unit base address is zero,
9312 which allows us to use absolute addresses, and not worry
9313 about whether the target supports cross-section
9314 arithmetic. */
9315 else
9317 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9318 fmt, i * 2 * DWARF2_ADDR_SIZE);
9319 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9322 fmt = NULL;
9325 /* Negative block_num stands for an index into ranges_by_label. */
9326 else if (block_num < 0)
9328 int lab_idx = - block_num - 1;
9330 if (!have_multiple_function_sections)
9332 gcc_unreachable ();
9333 #if 0
9334 /* If we ever use add_ranges_by_labels () for a single
9335 function section, all we have to do is to take out
9336 the #if 0 above. */
9337 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9338 ranges_by_label[lab_idx].begin,
9339 text_section_label,
9340 fmt, i * 2 * DWARF2_ADDR_SIZE);
9341 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9342 ranges_by_label[lab_idx].end,
9343 text_section_label, NULL);
9344 #endif
9346 else
9348 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9349 ranges_by_label[lab_idx].begin,
9350 fmt, i * 2 * DWARF2_ADDR_SIZE);
9351 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9352 ranges_by_label[lab_idx].end,
9353 NULL);
9356 else
9358 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9359 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9360 fmt = start_fmt;
9365 /* Data structure containing information about input files. */
9366 struct file_info
9368 const char *path; /* Complete file name. */
9369 const char *fname; /* File name part. */
9370 int length; /* Length of entire string. */
9371 struct dwarf_file_data * file_idx; /* Index in input file table. */
9372 int dir_idx; /* Index in directory table. */
9375 /* Data structure containing information about directories with source
9376 files. */
9377 struct dir_info
9379 const char *path; /* Path including directory name. */
9380 int length; /* Path length. */
9381 int prefix; /* Index of directory entry which is a prefix. */
9382 int count; /* Number of files in this directory. */
9383 int dir_idx; /* Index of directory used as base. */
9386 /* Callback function for file_info comparison. We sort by looking at
9387 the directories in the path. */
9389 static int
9390 file_info_cmp (const void *p1, const void *p2)
9392 const struct file_info *const s1 = (const struct file_info *) p1;
9393 const struct file_info *const s2 = (const struct file_info *) p2;
9394 const unsigned char *cp1;
9395 const unsigned char *cp2;
9397 /* Take care of file names without directories. We need to make sure that
9398 we return consistent values to qsort since some will get confused if
9399 we return the same value when identical operands are passed in opposite
9400 orders. So if neither has a directory, return 0 and otherwise return
9401 1 or -1 depending on which one has the directory. */
9402 if ((s1->path == s1->fname || s2->path == s2->fname))
9403 return (s2->path == s2->fname) - (s1->path == s1->fname);
9405 cp1 = (const unsigned char *) s1->path;
9406 cp2 = (const unsigned char *) s2->path;
9408 while (1)
9410 ++cp1;
9411 ++cp2;
9412 /* Reached the end of the first path? If so, handle like above. */
9413 if ((cp1 == (const unsigned char *) s1->fname)
9414 || (cp2 == (const unsigned char *) s2->fname))
9415 return ((cp2 == (const unsigned char *) s2->fname)
9416 - (cp1 == (const unsigned char *) s1->fname));
9418 /* Character of current path component the same? */
9419 else if (*cp1 != *cp2)
9420 return *cp1 - *cp2;
9424 struct file_name_acquire_data
9426 struct file_info *files;
9427 int used_files;
9428 int max_files;
9431 /* Traversal function for the hash table. */
9433 static int
9434 file_name_acquire (void ** slot, void *data)
9436 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9437 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9438 struct file_info *fi;
9439 const char *f;
9441 gcc_assert (fnad->max_files >= d->emitted_number);
9443 if (! d->emitted_number)
9444 return 1;
9446 gcc_assert (fnad->max_files != fnad->used_files);
9448 fi = fnad->files + fnad->used_files++;
9450 /* Skip all leading "./". */
9451 f = d->filename;
9452 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9453 f += 2;
9455 /* Create a new array entry. */
9456 fi->path = f;
9457 fi->length = strlen (f);
9458 fi->file_idx = d;
9460 /* Search for the file name part. */
9461 f = strrchr (f, DIR_SEPARATOR);
9462 #if defined (DIR_SEPARATOR_2)
9464 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9466 if (g != NULL)
9468 if (f == NULL || f < g)
9469 f = g;
9472 #endif
9474 fi->fname = f == NULL ? fi->path : f + 1;
9475 return 1;
9478 /* Output the directory table and the file name table. We try to minimize
9479 the total amount of memory needed. A heuristic is used to avoid large
9480 slowdowns with many input files. */
9482 static void
9483 output_file_names (void)
9485 struct file_name_acquire_data fnad;
9486 int numfiles;
9487 struct file_info *files;
9488 struct dir_info *dirs;
9489 int *saved;
9490 int *savehere;
9491 int *backmap;
9492 int ndirs;
9493 int idx_offset;
9494 int i;
9496 if (!last_emitted_file)
9498 dw2_asm_output_data (1, 0, "End directory table");
9499 dw2_asm_output_data (1, 0, "End file name table");
9500 return;
9503 numfiles = last_emitted_file->emitted_number;
9505 /* Allocate the various arrays we need. */
9506 files = XALLOCAVEC (struct file_info, numfiles);
9507 dirs = XALLOCAVEC (struct dir_info, numfiles);
9509 fnad.files = files;
9510 fnad.used_files = 0;
9511 fnad.max_files = numfiles;
9512 htab_traverse (file_table, file_name_acquire, &fnad);
9513 gcc_assert (fnad.used_files == fnad.max_files);
9515 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9517 /* Find all the different directories used. */
9518 dirs[0].path = files[0].path;
9519 dirs[0].length = files[0].fname - files[0].path;
9520 dirs[0].prefix = -1;
9521 dirs[0].count = 1;
9522 dirs[0].dir_idx = 0;
9523 files[0].dir_idx = 0;
9524 ndirs = 1;
9526 for (i = 1; i < numfiles; i++)
9527 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9528 && memcmp (dirs[ndirs - 1].path, files[i].path,
9529 dirs[ndirs - 1].length) == 0)
9531 /* Same directory as last entry. */
9532 files[i].dir_idx = ndirs - 1;
9533 ++dirs[ndirs - 1].count;
9535 else
9537 int j;
9539 /* This is a new directory. */
9540 dirs[ndirs].path = files[i].path;
9541 dirs[ndirs].length = files[i].fname - files[i].path;
9542 dirs[ndirs].count = 1;
9543 dirs[ndirs].dir_idx = ndirs;
9544 files[i].dir_idx = ndirs;
9546 /* Search for a prefix. */
9547 dirs[ndirs].prefix = -1;
9548 for (j = 0; j < ndirs; j++)
9549 if (dirs[j].length < dirs[ndirs].length
9550 && dirs[j].length > 1
9551 && (dirs[ndirs].prefix == -1
9552 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9553 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9554 dirs[ndirs].prefix = j;
9556 ++ndirs;
9559 /* Now to the actual work. We have to find a subset of the directories which
9560 allow expressing the file name using references to the directory table
9561 with the least amount of characters. We do not do an exhaustive search
9562 where we would have to check out every combination of every single
9563 possible prefix. Instead we use a heuristic which provides nearly optimal
9564 results in most cases and never is much off. */
9565 saved = XALLOCAVEC (int, ndirs);
9566 savehere = XALLOCAVEC (int, ndirs);
9568 memset (saved, '\0', ndirs * sizeof (saved[0]));
9569 for (i = 0; i < ndirs; i++)
9571 int j;
9572 int total;
9574 /* We can always save some space for the current directory. But this
9575 does not mean it will be enough to justify adding the directory. */
9576 savehere[i] = dirs[i].length;
9577 total = (savehere[i] - saved[i]) * dirs[i].count;
9579 for (j = i + 1; j < ndirs; j++)
9581 savehere[j] = 0;
9582 if (saved[j] < dirs[i].length)
9584 /* Determine whether the dirs[i] path is a prefix of the
9585 dirs[j] path. */
9586 int k;
9588 k = dirs[j].prefix;
9589 while (k != -1 && k != (int) i)
9590 k = dirs[k].prefix;
9592 if (k == (int) i)
9594 /* Yes it is. We can possibly save some memory by
9595 writing the filenames in dirs[j] relative to
9596 dirs[i]. */
9597 savehere[j] = dirs[i].length;
9598 total += (savehere[j] - saved[j]) * dirs[j].count;
9603 /* Check whether we can save enough to justify adding the dirs[i]
9604 directory. */
9605 if (total > dirs[i].length + 1)
9607 /* It's worthwhile adding. */
9608 for (j = i; j < ndirs; j++)
9609 if (savehere[j] > 0)
9611 /* Remember how much we saved for this directory so far. */
9612 saved[j] = savehere[j];
9614 /* Remember the prefix directory. */
9615 dirs[j].dir_idx = i;
9620 /* Emit the directory name table. */
9621 idx_offset = dirs[0].length > 0 ? 1 : 0;
9622 for (i = 1 - idx_offset; i < ndirs; i++)
9623 dw2_asm_output_nstring (dirs[i].path,
9624 dirs[i].length
9625 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9626 "Directory Entry: %#x", i + idx_offset);
9628 dw2_asm_output_data (1, 0, "End directory table");
9630 /* We have to emit them in the order of emitted_number since that's
9631 used in the debug info generation. To do this efficiently we
9632 generate a back-mapping of the indices first. */
9633 backmap = XALLOCAVEC (int, numfiles);
9634 for (i = 0; i < numfiles; i++)
9635 backmap[files[i].file_idx->emitted_number - 1] = i;
9637 /* Now write all the file names. */
9638 for (i = 0; i < numfiles; i++)
9640 int file_idx = backmap[i];
9641 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9643 #ifdef VMS_DEBUGGING_INFO
9644 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9646 /* Setting these fields can lead to debugger miscomparisons,
9647 but VMS Debug requires them to be set correctly. */
9649 int ver;
9650 long long cdt;
9651 long siz;
9652 int maxfilelen = strlen (files[file_idx].path)
9653 + dirs[dir_idx].length
9654 + MAX_VMS_VERSION_LEN + 1;
9655 char *filebuf = XALLOCAVEC (char, maxfilelen);
9657 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9658 snprintf (filebuf, maxfilelen, "%s;%d",
9659 files[file_idx].path + dirs[dir_idx].length, ver);
9661 dw2_asm_output_nstring
9662 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9664 /* Include directory index. */
9665 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9667 /* Modification time. */
9668 dw2_asm_output_data_uleb128
9669 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9670 ? cdt : 0,
9671 NULL);
9673 /* File length in bytes. */
9674 dw2_asm_output_data_uleb128
9675 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9676 ? siz : 0,
9677 NULL);
9678 #else
9679 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9680 "File Entry: %#x", (unsigned) i + 1);
9682 /* Include directory index. */
9683 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9685 /* Modification time. */
9686 dw2_asm_output_data_uleb128 (0, NULL);
9688 /* File length in bytes. */
9689 dw2_asm_output_data_uleb128 (0, NULL);
9690 #endif /* VMS_DEBUGGING_INFO */
9693 dw2_asm_output_data (1, 0, "End file name table");
9697 /* Output one line number table into the .debug_line section. */
9699 static void
9700 output_one_line_info_table (dw_line_info_table *table)
9702 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9703 unsigned int current_line = 1;
9704 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9705 dw_line_info_entry *ent;
9706 size_t i;
9708 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9710 switch (ent->opcode)
9712 case LI_set_address:
9713 /* ??? Unfortunately, we have little choice here currently, and
9714 must always use the most general form. GCC does not know the
9715 address delta itself, so we can't use DW_LNS_advance_pc. Many
9716 ports do have length attributes which will give an upper bound
9717 on the address range. We could perhaps use length attributes
9718 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9719 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9721 /* This can handle any delta. This takes
9722 4+DWARF2_ADDR_SIZE bytes. */
9723 dw2_asm_output_data (1, 0, "set address %s", line_label);
9724 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9725 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9726 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9727 break;
9729 case LI_set_line:
9730 if (ent->val == current_line)
9732 /* We still need to start a new row, so output a copy insn. */
9733 dw2_asm_output_data (1, DW_LNS_copy,
9734 "copy line %u", current_line);
9736 else
9738 int line_offset = ent->val - current_line;
9739 int line_delta = line_offset - DWARF_LINE_BASE;
9741 current_line = ent->val;
9742 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9744 /* This can handle deltas from -10 to 234, using the current
9745 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9746 This takes 1 byte. */
9747 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9748 "line %u", current_line);
9750 else
9752 /* This can handle any delta. This takes at least 4 bytes,
9753 depending on the value being encoded. */
9754 dw2_asm_output_data (1, DW_LNS_advance_line,
9755 "advance to line %u", current_line);
9756 dw2_asm_output_data_sleb128 (line_offset, NULL);
9757 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9760 break;
9762 case LI_set_file:
9763 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9764 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9765 break;
9767 case LI_set_column:
9768 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9769 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9770 break;
9772 case LI_negate_stmt:
9773 current_is_stmt = !current_is_stmt;
9774 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9775 "is_stmt %d", current_is_stmt);
9776 break;
9778 case LI_set_prologue_end:
9779 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9780 "set prologue end");
9781 break;
9783 case LI_set_epilogue_begin:
9784 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9785 "set epilogue begin");
9786 break;
9788 case LI_set_discriminator:
9789 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9790 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9791 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9792 dw2_asm_output_data_uleb128 (ent->val, NULL);
9793 break;
9797 /* Emit debug info for the address of the end of the table. */
9798 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9799 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9800 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9801 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9803 dw2_asm_output_data (1, 0, "end sequence");
9804 dw2_asm_output_data_uleb128 (1, NULL);
9805 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9808 /* Output the source line number correspondence information. This
9809 information goes into the .debug_line section. */
9811 static void
9812 output_line_info (bool prologue_only)
9814 char l1[20], l2[20], p1[20], p2[20];
9815 int ver = dwarf_version;
9816 bool saw_one = false;
9817 int opc;
9819 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9820 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9821 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9822 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9824 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9825 dw2_asm_output_data (4, 0xffffffff,
9826 "Initial length escape value indicating 64-bit DWARF extension");
9827 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9828 "Length of Source Line Info");
9829 ASM_OUTPUT_LABEL (asm_out_file, l1);
9831 dw2_asm_output_data (2, ver, "DWARF Version");
9832 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9833 ASM_OUTPUT_LABEL (asm_out_file, p1);
9835 /* Define the architecture-dependent minimum instruction length (in bytes).
9836 In this implementation of DWARF, this field is used for information
9837 purposes only. Since GCC generates assembly language, we have no
9838 a priori knowledge of how many instruction bytes are generated for each
9839 source line, and therefore can use only the DW_LNE_set_address and
9840 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9841 this as '1', which is "correct enough" for all architectures,
9842 and don't let the target override. */
9843 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9845 if (ver >= 4)
9846 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9847 "Maximum Operations Per Instruction");
9848 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9849 "Default is_stmt_start flag");
9850 dw2_asm_output_data (1, DWARF_LINE_BASE,
9851 "Line Base Value (Special Opcodes)");
9852 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9853 "Line Range Value (Special Opcodes)");
9854 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9855 "Special Opcode Base");
9857 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9859 int n_op_args;
9860 switch (opc)
9862 case DW_LNS_advance_pc:
9863 case DW_LNS_advance_line:
9864 case DW_LNS_set_file:
9865 case DW_LNS_set_column:
9866 case DW_LNS_fixed_advance_pc:
9867 case DW_LNS_set_isa:
9868 n_op_args = 1;
9869 break;
9870 default:
9871 n_op_args = 0;
9872 break;
9875 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9876 opc, n_op_args);
9879 /* Write out the information about the files we use. */
9880 output_file_names ();
9881 ASM_OUTPUT_LABEL (asm_out_file, p2);
9882 if (prologue_only)
9884 /* Output the marker for the end of the line number info. */
9885 ASM_OUTPUT_LABEL (asm_out_file, l2);
9886 return;
9889 if (separate_line_info)
9891 dw_line_info_table *table;
9892 size_t i;
9894 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9895 if (table->in_use)
9897 output_one_line_info_table (table);
9898 saw_one = true;
9901 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9903 output_one_line_info_table (cold_text_section_line_info);
9904 saw_one = true;
9907 /* ??? Some Darwin linkers crash on a .debug_line section with no
9908 sequences. Further, merely a DW_LNE_end_sequence entry is not
9909 sufficient -- the address column must also be initialized.
9910 Make sure to output at least one set_address/end_sequence pair,
9911 choosing .text since that section is always present. */
9912 if (text_section_line_info->in_use || !saw_one)
9913 output_one_line_info_table (text_section_line_info);
9915 /* Output the marker for the end of the line number info. */
9916 ASM_OUTPUT_LABEL (asm_out_file, l2);
9919 /* Given a pointer to a tree node for some base type, return a pointer to
9920 a DIE that describes the given type.
9922 This routine must only be called for GCC type nodes that correspond to
9923 Dwarf base (fundamental) types. */
9925 static dw_die_ref
9926 base_type_die (tree type)
9928 dw_die_ref base_type_result;
9929 enum dwarf_type encoding;
9931 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9932 return 0;
9934 /* If this is a subtype that should not be emitted as a subrange type,
9935 use the base type. See subrange_type_for_debug_p. */
9936 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9937 type = TREE_TYPE (type);
9939 switch (TREE_CODE (type))
9941 case INTEGER_TYPE:
9942 if ((dwarf_version >= 4 || !dwarf_strict)
9943 && TYPE_NAME (type)
9944 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9945 && DECL_IS_BUILTIN (TYPE_NAME (type))
9946 && DECL_NAME (TYPE_NAME (type)))
9948 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9949 if (strcmp (name, "char16_t") == 0
9950 || strcmp (name, "char32_t") == 0)
9952 encoding = DW_ATE_UTF;
9953 break;
9956 if (TYPE_STRING_FLAG (type))
9958 if (TYPE_UNSIGNED (type))
9959 encoding = DW_ATE_unsigned_char;
9960 else
9961 encoding = DW_ATE_signed_char;
9963 else if (TYPE_UNSIGNED (type))
9964 encoding = DW_ATE_unsigned;
9965 else
9966 encoding = DW_ATE_signed;
9967 break;
9969 case REAL_TYPE:
9970 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9972 if (dwarf_version >= 3 || !dwarf_strict)
9973 encoding = DW_ATE_decimal_float;
9974 else
9975 encoding = DW_ATE_lo_user;
9977 else
9978 encoding = DW_ATE_float;
9979 break;
9981 case FIXED_POINT_TYPE:
9982 if (!(dwarf_version >= 3 || !dwarf_strict))
9983 encoding = DW_ATE_lo_user;
9984 else if (TYPE_UNSIGNED (type))
9985 encoding = DW_ATE_unsigned_fixed;
9986 else
9987 encoding = DW_ATE_signed_fixed;
9988 break;
9990 /* Dwarf2 doesn't know anything about complex ints, so use
9991 a user defined type for it. */
9992 case COMPLEX_TYPE:
9993 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9994 encoding = DW_ATE_complex_float;
9995 else
9996 encoding = DW_ATE_lo_user;
9997 break;
9999 case BOOLEAN_TYPE:
10000 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10001 encoding = DW_ATE_boolean;
10002 break;
10004 default:
10005 /* No other TREE_CODEs are Dwarf fundamental types. */
10006 gcc_unreachable ();
10009 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10011 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10012 int_size_in_bytes (type));
10013 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10014 add_pubtype (type, base_type_result);
10016 return base_type_result;
10019 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10020 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10022 static inline int
10023 is_base_type (tree type)
10025 switch (TREE_CODE (type))
10027 case ERROR_MARK:
10028 case VOID_TYPE:
10029 case INTEGER_TYPE:
10030 case REAL_TYPE:
10031 case FIXED_POINT_TYPE:
10032 case COMPLEX_TYPE:
10033 case BOOLEAN_TYPE:
10034 return 1;
10036 case ARRAY_TYPE:
10037 case RECORD_TYPE:
10038 case UNION_TYPE:
10039 case QUAL_UNION_TYPE:
10040 case ENUMERAL_TYPE:
10041 case FUNCTION_TYPE:
10042 case METHOD_TYPE:
10043 case POINTER_TYPE:
10044 case REFERENCE_TYPE:
10045 case NULLPTR_TYPE:
10046 case OFFSET_TYPE:
10047 case LANG_TYPE:
10048 case VECTOR_TYPE:
10049 return 0;
10051 default:
10052 gcc_unreachable ();
10055 return 0;
10058 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10059 node, return the size in bits for the type if it is a constant, or else
10060 return the alignment for the type if the type's size is not constant, or
10061 else return BITS_PER_WORD if the type actually turns out to be an
10062 ERROR_MARK node. */
10064 static inline unsigned HOST_WIDE_INT
10065 simple_type_size_in_bits (const_tree type)
10067 if (TREE_CODE (type) == ERROR_MARK)
10068 return BITS_PER_WORD;
10069 else if (TYPE_SIZE (type) == NULL_TREE)
10070 return 0;
10071 else if (host_integerp (TYPE_SIZE (type), 1))
10072 return tree_low_cst (TYPE_SIZE (type), 1);
10073 else
10074 return TYPE_ALIGN (type);
10077 /* Similarly, but return a double_int instead of UHWI. */
10079 static inline double_int
10080 double_int_type_size_in_bits (const_tree type)
10082 if (TREE_CODE (type) == ERROR_MARK)
10083 return double_int::from_uhwi (BITS_PER_WORD);
10084 else if (TYPE_SIZE (type) == NULL_TREE)
10085 return double_int_zero;
10086 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10087 return tree_to_double_int (TYPE_SIZE (type));
10088 else
10089 return double_int::from_uhwi (TYPE_ALIGN (type));
10092 /* Given a pointer to a tree node for a subrange type, return a pointer
10093 to a DIE that describes the given type. */
10095 static dw_die_ref
10096 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10098 dw_die_ref subrange_die;
10099 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10101 if (context_die == NULL)
10102 context_die = comp_unit_die ();
10104 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10106 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10108 /* The size of the subrange type and its base type do not match,
10109 so we need to generate a size attribute for the subrange type. */
10110 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10113 if (low)
10114 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10115 if (high)
10116 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10118 return subrange_die;
10121 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10122 entry that chains various modifiers in front of the given type. */
10124 static dw_die_ref
10125 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10126 dw_die_ref context_die)
10128 enum tree_code code = TREE_CODE (type);
10129 dw_die_ref mod_type_die;
10130 dw_die_ref sub_die = NULL;
10131 tree item_type = NULL;
10132 tree qualified_type;
10133 tree name, low, high;
10134 dw_die_ref mod_scope;
10136 if (code == ERROR_MARK)
10137 return NULL;
10139 /* See if we already have the appropriately qualified variant of
10140 this type. */
10141 qualified_type
10142 = get_qualified_type (type,
10143 ((is_const_type ? TYPE_QUAL_CONST : 0)
10144 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10146 if (qualified_type == sizetype
10147 && TYPE_NAME (qualified_type)
10148 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10150 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10152 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10153 && TYPE_PRECISION (t)
10154 == TYPE_PRECISION (qualified_type)
10155 && TYPE_UNSIGNED (t)
10156 == TYPE_UNSIGNED (qualified_type));
10157 qualified_type = t;
10160 /* If we do, then we can just use its DIE, if it exists. */
10161 if (qualified_type)
10163 mod_type_die = lookup_type_die (qualified_type);
10164 if (mod_type_die)
10165 return mod_type_die;
10168 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10170 /* Handle C typedef types. */
10171 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10172 && !DECL_ARTIFICIAL (name))
10174 tree dtype = TREE_TYPE (name);
10176 if (qualified_type == dtype)
10178 /* For a named type, use the typedef. */
10179 gen_type_die (qualified_type, context_die);
10180 return lookup_type_die (qualified_type);
10182 else if (is_const_type < TYPE_READONLY (dtype)
10183 || is_volatile_type < TYPE_VOLATILE (dtype)
10184 || (is_const_type <= TYPE_READONLY (dtype)
10185 && is_volatile_type <= TYPE_VOLATILE (dtype)
10186 && DECL_ORIGINAL_TYPE (name) != type))
10187 /* cv-unqualified version of named type. Just use the unnamed
10188 type to which it refers. */
10189 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10190 is_const_type, is_volatile_type,
10191 context_die);
10192 /* Else cv-qualified version of named type; fall through. */
10195 mod_scope = scope_die_for (type, context_die);
10197 if (is_const_type
10198 /* If both is_const_type and is_volatile_type, prefer the path
10199 which leads to a qualified type. */
10200 && (!is_volatile_type
10201 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10202 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10204 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10205 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10207 else if (is_volatile_type)
10209 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10210 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10212 else if (code == POINTER_TYPE)
10214 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10215 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10216 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10217 item_type = TREE_TYPE (type);
10218 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10219 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10220 TYPE_ADDR_SPACE (item_type));
10222 else if (code == REFERENCE_TYPE)
10224 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10225 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10226 type);
10227 else
10228 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10229 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10230 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10231 item_type = TREE_TYPE (type);
10232 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10233 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10234 TYPE_ADDR_SPACE (item_type));
10236 else if (code == INTEGER_TYPE
10237 && TREE_TYPE (type) != NULL_TREE
10238 && subrange_type_for_debug_p (type, &low, &high))
10240 mod_type_die = subrange_type_die (type, low, high, context_die);
10241 item_type = TREE_TYPE (type);
10243 else if (is_base_type (type))
10244 mod_type_die = base_type_die (type);
10245 else
10247 gen_type_die (type, context_die);
10249 /* We have to get the type_main_variant here (and pass that to the
10250 `lookup_type_die' routine) because the ..._TYPE node we have
10251 might simply be a *copy* of some original type node (where the
10252 copy was created to help us keep track of typedef names) and
10253 that copy might have a different TYPE_UID from the original
10254 ..._TYPE node. */
10255 if (TREE_CODE (type) != VECTOR_TYPE)
10256 return lookup_type_die (type_main_variant (type));
10257 else
10258 /* Vectors have the debugging information in the type,
10259 not the main variant. */
10260 return lookup_type_die (type);
10263 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10264 don't output a DW_TAG_typedef, since there isn't one in the
10265 user's program; just attach a DW_AT_name to the type.
10266 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10267 if the base type already has the same name. */
10268 if (name
10269 && ((TREE_CODE (name) != TYPE_DECL
10270 && (qualified_type == TYPE_MAIN_VARIANT (type)
10271 || (!is_const_type && !is_volatile_type)))
10272 || (TREE_CODE (name) == TYPE_DECL
10273 && TREE_TYPE (name) == qualified_type
10274 && DECL_NAME (name))))
10276 if (TREE_CODE (name) == TYPE_DECL)
10277 /* Could just call add_name_and_src_coords_attributes here,
10278 but since this is a builtin type it doesn't have any
10279 useful source coordinates anyway. */
10280 name = DECL_NAME (name);
10281 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10283 /* This probably indicates a bug. */
10284 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10286 name = TYPE_NAME (type);
10287 if (name
10288 && TREE_CODE (name) == TYPE_DECL)
10289 name = DECL_NAME (name);
10290 add_name_attribute (mod_type_die,
10291 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10294 if (qualified_type)
10295 equate_type_number_to_die (qualified_type, mod_type_die);
10297 if (item_type)
10298 /* We must do this after the equate_type_number_to_die call, in case
10299 this is a recursive type. This ensures that the modified_type_die
10300 recursion will terminate even if the type is recursive. Recursive
10301 types are possible in Ada. */
10302 sub_die = modified_type_die (item_type,
10303 TYPE_READONLY (item_type),
10304 TYPE_VOLATILE (item_type),
10305 context_die);
10307 if (sub_die != NULL)
10308 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10310 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10311 if (TYPE_ARTIFICIAL (type))
10312 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10314 return mod_type_die;
10317 /* Generate DIEs for the generic parameters of T.
10318 T must be either a generic type or a generic function.
10319 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10321 static void
10322 gen_generic_params_dies (tree t)
10324 tree parms, args;
10325 int parms_num, i;
10326 dw_die_ref die = NULL;
10327 int non_default;
10329 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10330 return;
10332 if (TYPE_P (t))
10333 die = lookup_type_die (t);
10334 else if (DECL_P (t))
10335 die = lookup_decl_die (t);
10337 gcc_assert (die);
10339 parms = lang_hooks.get_innermost_generic_parms (t);
10340 if (!parms)
10341 /* T has no generic parameter. It means T is neither a generic type
10342 or function. End of story. */
10343 return;
10345 parms_num = TREE_VEC_LENGTH (parms);
10346 args = lang_hooks.get_innermost_generic_args (t);
10347 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10348 non_default = int_cst_value (TREE_CHAIN (args));
10349 else
10350 non_default = TREE_VEC_LENGTH (args);
10351 for (i = 0; i < parms_num; i++)
10353 tree parm, arg, arg_pack_elems;
10354 dw_die_ref parm_die;
10356 parm = TREE_VEC_ELT (parms, i);
10357 arg = TREE_VEC_ELT (args, i);
10358 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10359 gcc_assert (parm && TREE_VALUE (parm) && arg);
10361 if (parm && TREE_VALUE (parm) && arg)
10363 /* If PARM represents a template parameter pack,
10364 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10365 by DW_TAG_template_*_parameter DIEs for the argument
10366 pack elements of ARG. Note that ARG would then be
10367 an argument pack. */
10368 if (arg_pack_elems)
10369 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10370 arg_pack_elems,
10371 die);
10372 else
10373 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10374 true /* emit name */, die);
10375 if (i >= non_default)
10376 add_AT_flag (parm_die, DW_AT_default_value, 1);
10381 /* Create and return a DIE for PARM which should be
10382 the representation of a generic type parameter.
10383 For instance, in the C++ front end, PARM would be a template parameter.
10384 ARG is the argument to PARM.
10385 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10386 name of the PARM.
10387 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10388 as a child node. */
10390 static dw_die_ref
10391 generic_parameter_die (tree parm, tree arg,
10392 bool emit_name_p,
10393 dw_die_ref parent_die)
10395 dw_die_ref tmpl_die = NULL;
10396 const char *name = NULL;
10398 if (!parm || !DECL_NAME (parm) || !arg)
10399 return NULL;
10401 /* We support non-type generic parameters and arguments,
10402 type generic parameters and arguments, as well as
10403 generic generic parameters (a.k.a. template template parameters in C++)
10404 and arguments. */
10405 if (TREE_CODE (parm) == PARM_DECL)
10406 /* PARM is a nontype generic parameter */
10407 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10408 else if (TREE_CODE (parm) == TYPE_DECL)
10409 /* PARM is a type generic parameter. */
10410 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10411 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10412 /* PARM is a generic generic parameter.
10413 Its DIE is a GNU extension. It shall have a
10414 DW_AT_name attribute to represent the name of the template template
10415 parameter, and a DW_AT_GNU_template_name attribute to represent the
10416 name of the template template argument. */
10417 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10418 parent_die, parm);
10419 else
10420 gcc_unreachable ();
10422 if (tmpl_die)
10424 tree tmpl_type;
10426 /* If PARM is a generic parameter pack, it means we are
10427 emitting debug info for a template argument pack element.
10428 In other terms, ARG is a template argument pack element.
10429 In that case, we don't emit any DW_AT_name attribute for
10430 the die. */
10431 if (emit_name_p)
10433 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10434 gcc_assert (name);
10435 add_AT_string (tmpl_die, DW_AT_name, name);
10438 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10440 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10441 TMPL_DIE should have a child DW_AT_type attribute that is set
10442 to the type of the argument to PARM, which is ARG.
10443 If PARM is a type generic parameter, TMPL_DIE should have a
10444 child DW_AT_type that is set to ARG. */
10445 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10446 add_type_attribute (tmpl_die, tmpl_type, 0,
10447 TREE_THIS_VOLATILE (tmpl_type),
10448 parent_die);
10450 else
10452 /* So TMPL_DIE is a DIE representing a
10453 a generic generic template parameter, a.k.a template template
10454 parameter in C++ and arg is a template. */
10456 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10457 to the name of the argument. */
10458 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10459 if (name)
10460 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10463 if (TREE_CODE (parm) == PARM_DECL)
10464 /* So PARM is a non-type generic parameter.
10465 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10466 attribute of TMPL_DIE which value represents the value
10467 of ARG.
10468 We must be careful here:
10469 The value of ARG might reference some function decls.
10470 We might currently be emitting debug info for a generic
10471 type and types are emitted before function decls, we don't
10472 know if the function decls referenced by ARG will actually be
10473 emitted after cgraph computations.
10474 So must defer the generation of the DW_AT_const_value to
10475 after cgraph is ready. */
10476 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10479 return tmpl_die;
10482 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10483 PARM_PACK must be a template parameter pack. The returned DIE
10484 will be child DIE of PARENT_DIE. */
10486 static dw_die_ref
10487 template_parameter_pack_die (tree parm_pack,
10488 tree parm_pack_args,
10489 dw_die_ref parent_die)
10491 dw_die_ref die;
10492 int j;
10494 gcc_assert (parent_die && parm_pack);
10496 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10497 add_name_and_src_coords_attributes (die, parm_pack);
10498 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10499 generic_parameter_die (parm_pack,
10500 TREE_VEC_ELT (parm_pack_args, j),
10501 false /* Don't emit DW_AT_name */,
10502 die);
10503 return die;
10506 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10507 an enumerated type. */
10509 static inline int
10510 type_is_enum (const_tree type)
10512 return TREE_CODE (type) == ENUMERAL_TYPE;
10515 /* Return the DBX register number described by a given RTL node. */
10517 static unsigned int
10518 dbx_reg_number (const_rtx rtl)
10520 unsigned regno = REGNO (rtl);
10522 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10524 #ifdef LEAF_REG_REMAP
10525 if (crtl->uses_only_leaf_regs)
10527 int leaf_reg = LEAF_REG_REMAP (regno);
10528 if (leaf_reg != -1)
10529 regno = (unsigned) leaf_reg;
10531 #endif
10533 regno = DBX_REGISTER_NUMBER (regno);
10534 gcc_assert (regno != INVALID_REGNUM);
10535 return regno;
10538 /* Optionally add a DW_OP_piece term to a location description expression.
10539 DW_OP_piece is only added if the location description expression already
10540 doesn't end with DW_OP_piece. */
10542 static void
10543 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10545 dw_loc_descr_ref loc;
10547 if (*list_head != NULL)
10549 /* Find the end of the chain. */
10550 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10553 if (loc->dw_loc_opc != DW_OP_piece)
10554 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10558 /* Return a location descriptor that designates a machine register or
10559 zero if there is none. */
10561 static dw_loc_descr_ref
10562 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10564 rtx regs;
10566 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10567 return 0;
10569 /* We only use "frame base" when we're sure we're talking about the
10570 post-prologue local stack frame. We do this by *not* running
10571 register elimination until this point, and recognizing the special
10572 argument pointer and soft frame pointer rtx's.
10573 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10574 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10575 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10577 dw_loc_descr_ref result = NULL;
10579 if (dwarf_version >= 4 || !dwarf_strict)
10581 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10582 initialized);
10583 if (result)
10584 add_loc_descr (&result,
10585 new_loc_descr (DW_OP_stack_value, 0, 0));
10587 return result;
10590 regs = targetm.dwarf_register_span (rtl);
10592 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10593 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10594 else
10596 unsigned int dbx_regnum = dbx_reg_number (rtl);
10597 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10598 return 0;
10599 return one_reg_loc_descriptor (dbx_regnum, initialized);
10603 /* Return a location descriptor that designates a machine register for
10604 a given hard register number. */
10606 static dw_loc_descr_ref
10607 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10609 dw_loc_descr_ref reg_loc_descr;
10611 if (regno <= 31)
10612 reg_loc_descr
10613 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10614 else
10615 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10617 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10618 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10620 return reg_loc_descr;
10623 /* Given an RTL of a register, return a location descriptor that
10624 designates a value that spans more than one register. */
10626 static dw_loc_descr_ref
10627 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10628 enum var_init_status initialized)
10630 int size, i;
10631 dw_loc_descr_ref loc_result = NULL;
10633 /* Simple, contiguous registers. */
10634 if (regs == NULL_RTX)
10636 unsigned reg = REGNO (rtl);
10637 int nregs;
10639 #ifdef LEAF_REG_REMAP
10640 if (crtl->uses_only_leaf_regs)
10642 int leaf_reg = LEAF_REG_REMAP (reg);
10643 if (leaf_reg != -1)
10644 reg = (unsigned) leaf_reg;
10646 #endif
10648 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10649 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10651 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10653 loc_result = NULL;
10654 while (nregs--)
10656 dw_loc_descr_ref t;
10658 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10659 VAR_INIT_STATUS_INITIALIZED);
10660 add_loc_descr (&loc_result, t);
10661 add_loc_descr_op_piece (&loc_result, size);
10662 ++reg;
10664 return loc_result;
10667 /* Now onto stupid register sets in non contiguous locations. */
10669 gcc_assert (GET_CODE (regs) == PARALLEL);
10671 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10672 loc_result = NULL;
10674 for (i = 0; i < XVECLEN (regs, 0); ++i)
10676 dw_loc_descr_ref t;
10678 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10679 VAR_INIT_STATUS_INITIALIZED);
10680 add_loc_descr (&loc_result, t);
10681 add_loc_descr_op_piece (&loc_result, size);
10684 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10685 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10686 return loc_result;
10689 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10691 /* Return a location descriptor that designates a constant i,
10692 as a compound operation from constant (i >> shift), constant shift
10693 and DW_OP_shl. */
10695 static dw_loc_descr_ref
10696 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10698 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10699 add_loc_descr (&ret, int_loc_descriptor (shift));
10700 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10701 return ret;
10704 /* Return a location descriptor that designates a constant. */
10706 static dw_loc_descr_ref
10707 int_loc_descriptor (HOST_WIDE_INT i)
10709 enum dwarf_location_atom op;
10711 /* Pick the smallest representation of a constant, rather than just
10712 defaulting to the LEB encoding. */
10713 if (i >= 0)
10715 int clz = clz_hwi (i);
10716 int ctz = ctz_hwi (i);
10717 if (i <= 31)
10718 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10719 else if (i <= 0xff)
10720 op = DW_OP_const1u;
10721 else if (i <= 0xffff)
10722 op = DW_OP_const2u;
10723 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10724 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10725 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10726 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10727 while DW_OP_const4u is 5 bytes. */
10728 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10729 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10730 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10731 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10732 while DW_OP_const4u is 5 bytes. */
10733 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10734 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10735 op = DW_OP_const4u;
10736 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10737 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10738 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10739 while DW_OP_constu of constant >= 0x100000000 takes at least
10740 6 bytes. */
10741 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10742 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10743 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10744 >= HOST_BITS_PER_WIDE_INT)
10745 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10746 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10747 while DW_OP_constu takes in this case at least 6 bytes. */
10748 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10749 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10750 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10751 && size_of_uleb128 (i) > 6)
10752 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10753 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10754 else
10755 op = DW_OP_constu;
10757 else
10759 if (i >= -0x80)
10760 op = DW_OP_const1s;
10761 else if (i >= -0x8000)
10762 op = DW_OP_const2s;
10763 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10765 if (size_of_int_loc_descriptor (i) < 5)
10767 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10768 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10769 return ret;
10771 op = DW_OP_const4s;
10773 else
10775 if (size_of_int_loc_descriptor (i)
10776 < (unsigned long) 1 + size_of_sleb128 (i))
10778 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10779 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10780 return ret;
10782 op = DW_OP_consts;
10786 return new_loc_descr (op, i, 0);
10789 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10790 without actually allocating it. */
10792 static unsigned long
10793 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10795 return size_of_int_loc_descriptor (i >> shift)
10796 + size_of_int_loc_descriptor (shift)
10797 + 1;
10800 /* Return size_of_locs (int_loc_descriptor (i)) without
10801 actually allocating it. */
10803 static unsigned long
10804 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10806 unsigned long s;
10808 if (i >= 0)
10810 int clz, ctz;
10811 if (i <= 31)
10812 return 1;
10813 else if (i <= 0xff)
10814 return 2;
10815 else if (i <= 0xffff)
10816 return 3;
10817 clz = clz_hwi (i);
10818 ctz = ctz_hwi (i);
10819 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10820 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10821 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10822 - clz - 5);
10823 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10824 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10825 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10826 - clz - 8);
10827 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10828 return 5;
10829 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10830 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10831 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10832 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10833 - clz - 8);
10834 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10835 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10836 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10837 - clz - 16);
10838 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10839 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10840 && s > 6)
10841 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10842 - clz - 32);
10843 else
10844 return 1 + s;
10846 else
10848 if (i >= -0x80)
10849 return 2;
10850 else if (i >= -0x8000)
10851 return 3;
10852 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10854 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10856 s = size_of_int_loc_descriptor (-i) + 1;
10857 if (s < 5)
10858 return s;
10860 return 5;
10862 else
10864 unsigned long r = 1 + size_of_sleb128 (i);
10865 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10867 s = size_of_int_loc_descriptor (-i) + 1;
10868 if (s < r)
10869 return s;
10871 return r;
10876 /* Return loc description representing "address" of integer value.
10877 This can appear only as toplevel expression. */
10879 static dw_loc_descr_ref
10880 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10882 int litsize;
10883 dw_loc_descr_ref loc_result = NULL;
10885 if (!(dwarf_version >= 4 || !dwarf_strict))
10886 return NULL;
10888 litsize = size_of_int_loc_descriptor (i);
10889 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10890 is more compact. For DW_OP_stack_value we need:
10891 litsize + 1 (DW_OP_stack_value)
10892 and for DW_OP_implicit_value:
10893 1 (DW_OP_implicit_value) + 1 (length) + size. */
10894 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10896 loc_result = int_loc_descriptor (i);
10897 add_loc_descr (&loc_result,
10898 new_loc_descr (DW_OP_stack_value, 0, 0));
10899 return loc_result;
10902 loc_result = new_loc_descr (DW_OP_implicit_value,
10903 size, 0);
10904 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10905 loc_result->dw_loc_oprnd2.v.val_int = i;
10906 return loc_result;
10909 /* Return a location descriptor that designates a base+offset location. */
10911 static dw_loc_descr_ref
10912 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10913 enum var_init_status initialized)
10915 unsigned int regno;
10916 dw_loc_descr_ref result;
10917 dw_fde_ref fde = cfun->fde;
10919 /* We only use "frame base" when we're sure we're talking about the
10920 post-prologue local stack frame. We do this by *not* running
10921 register elimination until this point, and recognizing the special
10922 argument pointer and soft frame pointer rtx's. */
10923 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10925 rtx elim = (ira_use_lra_p
10926 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10927 : eliminate_regs (reg, VOIDmode, NULL_RTX));
10929 if (elim != reg)
10931 if (GET_CODE (elim) == PLUS)
10933 offset += INTVAL (XEXP (elim, 1));
10934 elim = XEXP (elim, 0);
10936 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10937 && (elim == hard_frame_pointer_rtx
10938 || elim == stack_pointer_rtx))
10939 || elim == (frame_pointer_needed
10940 ? hard_frame_pointer_rtx
10941 : stack_pointer_rtx));
10943 /* If drap register is used to align stack, use frame
10944 pointer + offset to access stack variables. If stack
10945 is aligned without drap, use stack pointer + offset to
10946 access stack variables. */
10947 if (crtl->stack_realign_tried
10948 && reg == frame_pointer_rtx)
10950 int base_reg
10951 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10952 ? HARD_FRAME_POINTER_REGNUM
10953 : REGNO (elim));
10954 return new_reg_loc_descr (base_reg, offset);
10957 gcc_assert (frame_pointer_fb_offset_valid);
10958 offset += frame_pointer_fb_offset;
10959 return new_loc_descr (DW_OP_fbreg, offset, 0);
10963 regno = REGNO (reg);
10964 #ifdef LEAF_REG_REMAP
10965 if (crtl->uses_only_leaf_regs)
10967 int leaf_reg = LEAF_REG_REMAP (regno);
10968 if (leaf_reg != -1)
10969 regno = (unsigned) leaf_reg;
10971 #endif
10972 regno = DWARF_FRAME_REGNUM (regno);
10974 if (!optimize && fde
10975 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10977 /* Use cfa+offset to represent the location of arguments passed
10978 on the stack when drap is used to align stack.
10979 Only do this when not optimizing, for optimized code var-tracking
10980 is supposed to track where the arguments live and the register
10981 used as vdrap or drap in some spot might be used for something
10982 else in other part of the routine. */
10983 return new_loc_descr (DW_OP_fbreg, offset, 0);
10986 if (regno <= 31)
10987 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10988 offset, 0);
10989 else
10990 result = new_loc_descr (DW_OP_bregx, regno, offset);
10992 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10993 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10995 return result;
10998 /* Return true if this RTL expression describes a base+offset calculation. */
11000 static inline int
11001 is_based_loc (const_rtx rtl)
11003 return (GET_CODE (rtl) == PLUS
11004 && ((REG_P (XEXP (rtl, 0))
11005 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11006 && CONST_INT_P (XEXP (rtl, 1)))));
11009 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11010 failed. */
11012 static dw_loc_descr_ref
11013 tls_mem_loc_descriptor (rtx mem)
11015 tree base;
11016 dw_loc_descr_ref loc_result;
11018 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11019 return NULL;
11021 base = get_base_address (MEM_EXPR (mem));
11022 if (base == NULL
11023 || TREE_CODE (base) != VAR_DECL
11024 || !DECL_THREAD_LOCAL_P (base))
11025 return NULL;
11027 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11028 if (loc_result == NULL)
11029 return NULL;
11031 if (MEM_OFFSET (mem))
11032 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11034 return loc_result;
11037 /* Output debug info about reason why we failed to expand expression as dwarf
11038 expression. */
11040 static void
11041 expansion_failed (tree expr, rtx rtl, char const *reason)
11043 if (dump_file && (dump_flags & TDF_DETAILS))
11045 fprintf (dump_file, "Failed to expand as dwarf: ");
11046 if (expr)
11047 print_generic_expr (dump_file, expr, dump_flags);
11048 if (rtl)
11050 fprintf (dump_file, "\n");
11051 print_rtl (dump_file, rtl);
11053 fprintf (dump_file, "\nReason: %s\n", reason);
11057 /* Helper function for const_ok_for_output, called either directly
11058 or via for_each_rtx. */
11060 static int
11061 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11063 rtx rtl = *rtlp;
11065 if (GET_CODE (rtl) == UNSPEC)
11067 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11068 we can't express it in the debug info. */
11069 #ifdef ENABLE_CHECKING
11070 /* Don't complain about TLS UNSPECs, those are just too hard to
11071 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11072 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11073 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11074 if (XVECLEN (rtl, 0) == 0
11075 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11076 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11077 inform (current_function_decl
11078 ? DECL_SOURCE_LOCATION (current_function_decl)
11079 : UNKNOWN_LOCATION,
11080 #if NUM_UNSPEC_VALUES > 0
11081 "non-delegitimized UNSPEC %s (%d) found in variable location",
11082 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11083 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11084 XINT (rtl, 1));
11085 #else
11086 "non-delegitimized UNSPEC %d found in variable location",
11087 XINT (rtl, 1));
11088 #endif
11089 #endif
11090 expansion_failed (NULL_TREE, rtl,
11091 "UNSPEC hasn't been delegitimized.\n");
11092 return 1;
11095 if (targetm.const_not_ok_for_debug_p (rtl))
11097 expansion_failed (NULL_TREE, rtl,
11098 "Expression rejected for debug by the backend.\n");
11099 return 1;
11102 if (GET_CODE (rtl) != SYMBOL_REF)
11103 return 0;
11105 if (CONSTANT_POOL_ADDRESS_P (rtl))
11107 bool marked;
11108 get_pool_constant_mark (rtl, &marked);
11109 /* If all references to this pool constant were optimized away,
11110 it was not output and thus we can't represent it. */
11111 if (!marked)
11113 expansion_failed (NULL_TREE, rtl,
11114 "Constant was removed from constant pool.\n");
11115 return 1;
11119 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11120 return 1;
11122 /* Avoid references to external symbols in debug info, on several targets
11123 the linker might even refuse to link when linking a shared library,
11124 and in many other cases the relocations for .debug_info/.debug_loc are
11125 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11126 to be defined within the same shared library or executable are fine. */
11127 if (SYMBOL_REF_EXTERNAL_P (rtl))
11129 tree decl = SYMBOL_REF_DECL (rtl);
11131 if (decl == NULL || !targetm.binds_local_p (decl))
11133 expansion_failed (NULL_TREE, rtl,
11134 "Symbol not defined in current TU.\n");
11135 return 1;
11139 return 0;
11142 /* Return true if constant RTL can be emitted in DW_OP_addr or
11143 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11144 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11146 static bool
11147 const_ok_for_output (rtx rtl)
11149 if (GET_CODE (rtl) == SYMBOL_REF)
11150 return const_ok_for_output_1 (&rtl, NULL) == 0;
11152 if (GET_CODE (rtl) == CONST)
11153 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11155 return true;
11158 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11159 if possible, NULL otherwise. */
11161 static dw_die_ref
11162 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11164 dw_die_ref type_die;
11165 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11167 if (type == NULL)
11168 return NULL;
11169 switch (TREE_CODE (type))
11171 case INTEGER_TYPE:
11172 case REAL_TYPE:
11173 break;
11174 default:
11175 return NULL;
11177 type_die = lookup_type_die (type);
11178 if (!type_die)
11179 type_die = modified_type_die (type, false, false, comp_unit_die ());
11180 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11181 return NULL;
11182 return type_die;
11185 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11186 type matching MODE, or, if MODE is narrower than or as wide as
11187 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11188 possible. */
11190 static dw_loc_descr_ref
11191 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11193 enum machine_mode outer_mode = mode;
11194 dw_die_ref type_die;
11195 dw_loc_descr_ref cvt;
11197 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11199 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11200 return op;
11202 type_die = base_type_for_mode (outer_mode, 1);
11203 if (type_die == NULL)
11204 return NULL;
11205 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11206 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11207 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11208 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11209 add_loc_descr (&op, cvt);
11210 return op;
11213 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11215 static dw_loc_descr_ref
11216 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11217 dw_loc_descr_ref op1)
11219 dw_loc_descr_ref ret = op0;
11220 add_loc_descr (&ret, op1);
11221 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11222 if (STORE_FLAG_VALUE != 1)
11224 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11225 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11227 return ret;
11230 /* Return location descriptor for signed comparison OP RTL. */
11232 static dw_loc_descr_ref
11233 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11234 enum machine_mode mem_mode)
11236 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11237 dw_loc_descr_ref op0, op1;
11238 int shift;
11240 if (op_mode == VOIDmode)
11241 op_mode = GET_MODE (XEXP (rtl, 1));
11242 if (op_mode == VOIDmode)
11243 return NULL;
11245 if (dwarf_strict
11246 && (GET_MODE_CLASS (op_mode) != MODE_INT
11247 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11248 return NULL;
11250 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11251 VAR_INIT_STATUS_INITIALIZED);
11252 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11253 VAR_INIT_STATUS_INITIALIZED);
11255 if (op0 == NULL || op1 == NULL)
11256 return NULL;
11258 if (GET_MODE_CLASS (op_mode) != MODE_INT
11259 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11260 return compare_loc_descriptor (op, op0, op1);
11262 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11264 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11265 dw_loc_descr_ref cvt;
11267 if (type_die == NULL)
11268 return NULL;
11269 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11270 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11271 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11272 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11273 add_loc_descr (&op0, cvt);
11274 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11275 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11276 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11277 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11278 add_loc_descr (&op1, cvt);
11279 return compare_loc_descriptor (op, op0, op1);
11282 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11283 /* For eq/ne, if the operands are known to be zero-extended,
11284 there is no need to do the fancy shifting up. */
11285 if (op == DW_OP_eq || op == DW_OP_ne)
11287 dw_loc_descr_ref last0, last1;
11288 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11290 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11292 /* deref_size zero extends, and for constants we can check
11293 whether they are zero extended or not. */
11294 if (((last0->dw_loc_opc == DW_OP_deref_size
11295 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11296 || (CONST_INT_P (XEXP (rtl, 0))
11297 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11298 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11299 && ((last1->dw_loc_opc == DW_OP_deref_size
11300 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11301 || (CONST_INT_P (XEXP (rtl, 1))
11302 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11303 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11304 return compare_loc_descriptor (op, op0, op1);
11306 /* EQ/NE comparison against constant in narrower type than
11307 DWARF2_ADDR_SIZE can be performed either as
11308 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11309 DW_OP_{eq,ne}
11311 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11312 DW_OP_{eq,ne}. Pick whatever is shorter. */
11313 if (CONST_INT_P (XEXP (rtl, 1))
11314 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11315 && (size_of_int_loc_descriptor (shift) + 1
11316 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11317 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11318 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11319 & GET_MODE_MASK (op_mode))))
11321 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11322 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11323 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11324 & GET_MODE_MASK (op_mode));
11325 return compare_loc_descriptor (op, op0, op1);
11328 add_loc_descr (&op0, int_loc_descriptor (shift));
11329 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11330 if (CONST_INT_P (XEXP (rtl, 1)))
11331 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11332 else
11334 add_loc_descr (&op1, int_loc_descriptor (shift));
11335 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11337 return compare_loc_descriptor (op, op0, op1);
11340 /* Return location descriptor for unsigned comparison OP RTL. */
11342 static dw_loc_descr_ref
11343 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11344 enum machine_mode mem_mode)
11346 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11347 dw_loc_descr_ref op0, op1;
11349 if (op_mode == VOIDmode)
11350 op_mode = GET_MODE (XEXP (rtl, 1));
11351 if (op_mode == VOIDmode)
11352 return NULL;
11353 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11354 return NULL;
11356 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11357 return NULL;
11359 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11360 VAR_INIT_STATUS_INITIALIZED);
11361 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11362 VAR_INIT_STATUS_INITIALIZED);
11364 if (op0 == NULL || op1 == NULL)
11365 return NULL;
11367 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11369 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11370 dw_loc_descr_ref last0, last1;
11371 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11373 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11375 if (CONST_INT_P (XEXP (rtl, 0)))
11376 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11377 /* deref_size zero extends, so no need to mask it again. */
11378 else if (last0->dw_loc_opc != DW_OP_deref_size
11379 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11381 add_loc_descr (&op0, int_loc_descriptor (mask));
11382 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11384 if (CONST_INT_P (XEXP (rtl, 1)))
11385 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11386 /* deref_size zero extends, so no need to mask it again. */
11387 else if (last1->dw_loc_opc != DW_OP_deref_size
11388 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11390 add_loc_descr (&op1, int_loc_descriptor (mask));
11391 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11394 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11396 HOST_WIDE_INT bias = 1;
11397 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11398 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11399 if (CONST_INT_P (XEXP (rtl, 1)))
11400 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11401 + INTVAL (XEXP (rtl, 1)));
11402 else
11403 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11404 bias, 0));
11406 return compare_loc_descriptor (op, op0, op1);
11409 /* Return location descriptor for {U,S}{MIN,MAX}. */
11411 static dw_loc_descr_ref
11412 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11413 enum machine_mode mem_mode)
11415 enum dwarf_location_atom op;
11416 dw_loc_descr_ref op0, op1, ret;
11417 dw_loc_descr_ref bra_node, drop_node;
11419 if (dwarf_strict
11420 && (GET_MODE_CLASS (mode) != MODE_INT
11421 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11422 return NULL;
11424 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11425 VAR_INIT_STATUS_INITIALIZED);
11426 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11427 VAR_INIT_STATUS_INITIALIZED);
11429 if (op0 == NULL || op1 == NULL)
11430 return NULL;
11432 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11433 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11434 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11435 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11437 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11439 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11440 add_loc_descr (&op0, int_loc_descriptor (mask));
11441 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11442 add_loc_descr (&op1, int_loc_descriptor (mask));
11443 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11445 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11447 HOST_WIDE_INT bias = 1;
11448 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11449 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11450 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11453 else if (GET_MODE_CLASS (mode) == MODE_INT
11454 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11456 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11457 add_loc_descr (&op0, int_loc_descriptor (shift));
11458 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11459 add_loc_descr (&op1, int_loc_descriptor (shift));
11460 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11462 else if (GET_MODE_CLASS (mode) == MODE_INT
11463 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11465 dw_die_ref type_die = base_type_for_mode (mode, 0);
11466 dw_loc_descr_ref cvt;
11467 if (type_die == NULL)
11468 return NULL;
11469 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11470 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11471 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11472 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11473 add_loc_descr (&op0, cvt);
11474 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11475 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11476 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11477 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11478 add_loc_descr (&op1, cvt);
11481 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11482 op = DW_OP_lt;
11483 else
11484 op = DW_OP_gt;
11485 ret = op0;
11486 add_loc_descr (&ret, op1);
11487 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11488 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11489 add_loc_descr (&ret, bra_node);
11490 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11491 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11492 add_loc_descr (&ret, drop_node);
11493 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11494 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11495 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11496 && GET_MODE_CLASS (mode) == MODE_INT
11497 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11498 ret = convert_descriptor_to_mode (mode, ret);
11499 return ret;
11502 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11503 but after converting arguments to type_die, afterwards
11504 convert back to unsigned. */
11506 static dw_loc_descr_ref
11507 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11508 enum machine_mode mode, enum machine_mode mem_mode)
11510 dw_loc_descr_ref cvt, op0, op1;
11512 if (type_die == NULL)
11513 return NULL;
11514 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11515 VAR_INIT_STATUS_INITIALIZED);
11516 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11517 VAR_INIT_STATUS_INITIALIZED);
11518 if (op0 == NULL || op1 == NULL)
11519 return NULL;
11520 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11521 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11522 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11523 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11524 add_loc_descr (&op0, cvt);
11525 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11526 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11527 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11528 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11529 add_loc_descr (&op1, cvt);
11530 add_loc_descr (&op0, op1);
11531 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11532 return convert_descriptor_to_mode (mode, op0);
11535 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11536 const0 is DW_OP_lit0 or corresponding typed constant,
11537 const1 is DW_OP_lit1 or corresponding typed constant
11538 and constMSB is constant with just the MSB bit set
11539 for the mode):
11540 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11541 L1: const0 DW_OP_swap
11542 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
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 CTZ is similar:
11548 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11549 L1: const0 DW_OP_swap
11550 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11551 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11552 L3: DW_OP_drop
11553 L4: DW_OP_nop
11555 FFS is similar:
11556 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11557 L1: const1 DW_OP_swap
11558 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11559 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11560 L3: DW_OP_drop
11561 L4: DW_OP_nop */
11563 static dw_loc_descr_ref
11564 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11565 enum machine_mode mem_mode)
11567 dw_loc_descr_ref op0, ret, tmp;
11568 HOST_WIDE_INT valv;
11569 dw_loc_descr_ref l1jump, l1label;
11570 dw_loc_descr_ref l2jump, l2label;
11571 dw_loc_descr_ref l3jump, l3label;
11572 dw_loc_descr_ref l4jump, l4label;
11573 rtx msb;
11575 if (GET_MODE_CLASS (mode) != MODE_INT
11576 || GET_MODE (XEXP (rtl, 0)) != mode
11577 || (GET_CODE (rtl) == CLZ
11578 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11579 return NULL;
11581 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11582 VAR_INIT_STATUS_INITIALIZED);
11583 if (op0 == NULL)
11584 return NULL;
11585 ret = op0;
11586 if (GET_CODE (rtl) == CLZ)
11588 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11589 valv = GET_MODE_BITSIZE (mode);
11591 else if (GET_CODE (rtl) == FFS)
11592 valv = 0;
11593 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11594 valv = GET_MODE_BITSIZE (mode);
11595 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11596 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11597 add_loc_descr (&ret, l1jump);
11598 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11599 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11600 VAR_INIT_STATUS_INITIALIZED);
11601 if (tmp == NULL)
11602 return NULL;
11603 add_loc_descr (&ret, tmp);
11604 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11605 add_loc_descr (&ret, l4jump);
11606 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11607 ? const1_rtx : const0_rtx,
11608 mode, mem_mode,
11609 VAR_INIT_STATUS_INITIALIZED);
11610 if (l1label == NULL)
11611 return NULL;
11612 add_loc_descr (&ret, l1label);
11613 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11614 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11615 add_loc_descr (&ret, l2label);
11616 if (GET_CODE (rtl) != CLZ)
11617 msb = const1_rtx;
11618 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11619 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11620 << (GET_MODE_BITSIZE (mode) - 1));
11621 else
11622 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11623 << (GET_MODE_BITSIZE (mode)
11624 - HOST_BITS_PER_WIDE_INT - 1), mode);
11625 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11626 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11627 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11628 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11629 else
11630 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11631 VAR_INIT_STATUS_INITIALIZED);
11632 if (tmp == NULL)
11633 return NULL;
11634 add_loc_descr (&ret, tmp);
11635 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11636 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11637 add_loc_descr (&ret, l3jump);
11638 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11639 VAR_INIT_STATUS_INITIALIZED);
11640 if (tmp == NULL)
11641 return NULL;
11642 add_loc_descr (&ret, tmp);
11643 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11644 ? DW_OP_shl : DW_OP_shr, 0, 0));
11645 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11646 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11647 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11648 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11649 add_loc_descr (&ret, l2jump);
11650 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11651 add_loc_descr (&ret, l3label);
11652 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11653 add_loc_descr (&ret, l4label);
11654 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11655 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11656 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11657 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11658 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11659 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11660 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11661 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11662 return ret;
11665 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11666 const1 is DW_OP_lit1 or corresponding typed constant):
11667 const0 DW_OP_swap
11668 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11669 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11670 L2: DW_OP_drop
11672 PARITY is similar:
11673 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11674 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11675 L2: DW_OP_drop */
11677 static dw_loc_descr_ref
11678 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11679 enum machine_mode mem_mode)
11681 dw_loc_descr_ref op0, ret, tmp;
11682 dw_loc_descr_ref l1jump, l1label;
11683 dw_loc_descr_ref l2jump, l2label;
11685 if (GET_MODE_CLASS (mode) != MODE_INT
11686 || GET_MODE (XEXP (rtl, 0)) != mode)
11687 return NULL;
11689 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11690 VAR_INIT_STATUS_INITIALIZED);
11691 if (op0 == NULL)
11692 return NULL;
11693 ret = op0;
11694 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11695 VAR_INIT_STATUS_INITIALIZED);
11696 if (tmp == NULL)
11697 return NULL;
11698 add_loc_descr (&ret, tmp);
11699 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11700 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11701 add_loc_descr (&ret, l1label);
11702 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11703 add_loc_descr (&ret, l2jump);
11704 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11705 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11706 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11707 VAR_INIT_STATUS_INITIALIZED);
11708 if (tmp == NULL)
11709 return NULL;
11710 add_loc_descr (&ret, tmp);
11711 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11712 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11713 ? DW_OP_plus : DW_OP_xor, 0, 0));
11714 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11715 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11716 VAR_INIT_STATUS_INITIALIZED);
11717 add_loc_descr (&ret, tmp);
11718 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11719 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11720 add_loc_descr (&ret, l1jump);
11721 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11722 add_loc_descr (&ret, l2label);
11723 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11724 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11725 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11726 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11727 return ret;
11730 /* BSWAP (constS is initial shift count, either 56 or 24):
11731 constS const0
11732 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11733 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11734 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11735 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11736 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11738 static dw_loc_descr_ref
11739 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11740 enum machine_mode mem_mode)
11742 dw_loc_descr_ref op0, ret, tmp;
11743 dw_loc_descr_ref l1jump, l1label;
11744 dw_loc_descr_ref l2jump, l2label;
11746 if (GET_MODE_CLASS (mode) != MODE_INT
11747 || BITS_PER_UNIT != 8
11748 || (GET_MODE_BITSIZE (mode) != 32
11749 && GET_MODE_BITSIZE (mode) != 64))
11750 return NULL;
11752 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11753 VAR_INIT_STATUS_INITIALIZED);
11754 if (op0 == NULL)
11755 return NULL;
11757 ret = op0;
11758 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11759 mode, mem_mode,
11760 VAR_INIT_STATUS_INITIALIZED);
11761 if (tmp == NULL)
11762 return NULL;
11763 add_loc_descr (&ret, tmp);
11764 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11765 VAR_INIT_STATUS_INITIALIZED);
11766 if (tmp == NULL)
11767 return NULL;
11768 add_loc_descr (&ret, tmp);
11769 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11770 add_loc_descr (&ret, l1label);
11771 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11772 mode, mem_mode,
11773 VAR_INIT_STATUS_INITIALIZED);
11774 add_loc_descr (&ret, tmp);
11775 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11776 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11777 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11778 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11779 VAR_INIT_STATUS_INITIALIZED);
11780 if (tmp == NULL)
11781 return NULL;
11782 add_loc_descr (&ret, tmp);
11783 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11784 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11785 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11786 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11787 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11788 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11789 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11790 VAR_INIT_STATUS_INITIALIZED);
11791 add_loc_descr (&ret, tmp);
11792 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11793 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11794 add_loc_descr (&ret, l2jump);
11795 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11796 VAR_INIT_STATUS_INITIALIZED);
11797 add_loc_descr (&ret, tmp);
11798 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11799 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11800 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11801 add_loc_descr (&ret, l1jump);
11802 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11803 add_loc_descr (&ret, l2label);
11804 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11805 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11806 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11807 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11808 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11809 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11810 return ret;
11813 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11814 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11815 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11816 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11818 ROTATERT is similar:
11819 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11820 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11821 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11823 static dw_loc_descr_ref
11824 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11825 enum machine_mode mem_mode)
11827 rtx rtlop1 = XEXP (rtl, 1);
11828 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11829 int i;
11831 if (GET_MODE_CLASS (mode) != MODE_INT)
11832 return NULL;
11834 if (GET_MODE (rtlop1) != VOIDmode
11835 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11836 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11837 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11838 VAR_INIT_STATUS_INITIALIZED);
11839 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11840 VAR_INIT_STATUS_INITIALIZED);
11841 if (op0 == NULL || op1 == NULL)
11842 return NULL;
11843 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11844 for (i = 0; i < 2; i++)
11846 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11847 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11848 mode, mem_mode,
11849 VAR_INIT_STATUS_INITIALIZED);
11850 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11851 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11852 ? DW_OP_const4u
11853 : HOST_BITS_PER_WIDE_INT == 64
11854 ? DW_OP_const8u : DW_OP_constu,
11855 GET_MODE_MASK (mode), 0);
11856 else
11857 mask[i] = NULL;
11858 if (mask[i] == NULL)
11859 return NULL;
11860 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11862 ret = op0;
11863 add_loc_descr (&ret, op1);
11864 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11865 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11866 if (GET_CODE (rtl) == ROTATERT)
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_shl, 0, 0));
11873 if (mask[0] != NULL)
11874 add_loc_descr (&ret, mask[0]);
11875 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11876 if (mask[1] != NULL)
11878 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11879 add_loc_descr (&ret, mask[1]);
11880 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11882 if (GET_CODE (rtl) == ROTATE)
11884 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11885 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11886 GET_MODE_BITSIZE (mode), 0));
11888 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11889 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11890 return ret;
11893 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11894 for DEBUG_PARAMETER_REF RTL. */
11896 static dw_loc_descr_ref
11897 parameter_ref_descriptor (rtx rtl)
11899 dw_loc_descr_ref ret;
11900 dw_die_ref ref;
11902 if (dwarf_strict)
11903 return NULL;
11904 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11905 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11906 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11907 if (ref)
11909 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11910 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11911 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11913 else
11915 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11916 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11918 return ret;
11921 /* The following routine converts the RTL for a variable or parameter
11922 (resident in memory) into an equivalent Dwarf representation of a
11923 mechanism for getting the address of that same variable onto the top of a
11924 hypothetical "address evaluation" stack.
11926 When creating memory location descriptors, we are effectively transforming
11927 the RTL for a memory-resident object into its Dwarf postfix expression
11928 equivalent. This routine recursively descends an RTL tree, turning
11929 it into Dwarf postfix code as it goes.
11931 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11933 MEM_MODE is the mode of the memory reference, needed to handle some
11934 autoincrement addressing modes.
11936 Return 0 if we can't represent the location. */
11938 dw_loc_descr_ref
11939 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11940 enum machine_mode mem_mode,
11941 enum var_init_status initialized)
11943 dw_loc_descr_ref mem_loc_result = NULL;
11944 enum dwarf_location_atom op;
11945 dw_loc_descr_ref op0, op1;
11946 rtx inner = NULL_RTX;
11948 if (mode == VOIDmode)
11949 mode = GET_MODE (rtl);
11951 /* Note that for a dynamically sized array, the location we will generate a
11952 description of here will be the lowest numbered location which is
11953 actually within the array. That's *not* necessarily the same as the
11954 zeroth element of the array. */
11956 rtl = targetm.delegitimize_address (rtl);
11958 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11959 return NULL;
11961 switch (GET_CODE (rtl))
11963 case POST_INC:
11964 case POST_DEC:
11965 case POST_MODIFY:
11966 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11968 case SUBREG:
11969 /* The case of a subreg may arise when we have a local (register)
11970 variable or a formal (register) parameter which doesn't quite fill
11971 up an entire register. For now, just assume that it is
11972 legitimate to make the Dwarf info refer to the whole register which
11973 contains the given subreg. */
11974 if (!subreg_lowpart_p (rtl))
11975 break;
11976 inner = SUBREG_REG (rtl);
11977 case TRUNCATE:
11978 if (inner == NULL_RTX)
11979 inner = XEXP (rtl, 0);
11980 if (GET_MODE_CLASS (mode) == MODE_INT
11981 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11982 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11983 #ifdef POINTERS_EXTEND_UNSIGNED
11984 || (mode == Pmode && mem_mode != VOIDmode)
11985 #endif
11987 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
11989 mem_loc_result = mem_loc_descriptor (inner,
11990 GET_MODE (inner),
11991 mem_mode, initialized);
11992 break;
11994 if (dwarf_strict)
11995 break;
11996 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
11997 break;
11998 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
11999 && (GET_MODE_CLASS (mode) != MODE_INT
12000 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12001 break;
12002 else
12004 dw_die_ref type_die;
12005 dw_loc_descr_ref cvt;
12007 mem_loc_result = mem_loc_descriptor (inner,
12008 GET_MODE (inner),
12009 mem_mode, initialized);
12010 if (mem_loc_result == NULL)
12011 break;
12012 type_die = base_type_for_mode (mode,
12013 GET_MODE_CLASS (mode) == MODE_INT);
12014 if (type_die == NULL)
12016 mem_loc_result = NULL;
12017 break;
12019 if (GET_MODE_SIZE (mode)
12020 != GET_MODE_SIZE (GET_MODE (inner)))
12021 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12022 else
12023 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12024 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12025 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12026 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12027 add_loc_descr (&mem_loc_result, cvt);
12029 break;
12031 case REG:
12032 if (GET_MODE_CLASS (mode) != MODE_INT
12033 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12034 && rtl != arg_pointer_rtx
12035 && rtl != frame_pointer_rtx
12036 #ifdef POINTERS_EXTEND_UNSIGNED
12037 && (mode != Pmode || mem_mode == VOIDmode)
12038 #endif
12041 dw_die_ref type_die;
12042 unsigned int dbx_regnum;
12044 if (dwarf_strict)
12045 break;
12046 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12047 break;
12048 type_die = base_type_for_mode (mode,
12049 GET_MODE_CLASS (mode) == MODE_INT);
12050 if (type_die == NULL)
12051 break;
12053 dbx_regnum = dbx_reg_number (rtl);
12054 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12055 break;
12056 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12057 dbx_regnum, 0);
12058 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12059 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12060 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12061 break;
12063 /* Whenever a register number forms a part of the description of the
12064 method for calculating the (dynamic) address of a memory resident
12065 object, DWARF rules require the register number be referred to as
12066 a "base register". This distinction is not based in any way upon
12067 what category of register the hardware believes the given register
12068 belongs to. This is strictly DWARF terminology we're dealing with
12069 here. Note that in cases where the location of a memory-resident
12070 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12071 OP_CONST (0)) the actual DWARF location descriptor that we generate
12072 may just be OP_BASEREG (basereg). This may look deceptively like
12073 the object in question was allocated to a register (rather than in
12074 memory) so DWARF consumers need to be aware of the subtle
12075 distinction between OP_REG and OP_BASEREG. */
12076 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12077 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12078 else if (stack_realign_drap
12079 && crtl->drap_reg
12080 && crtl->args.internal_arg_pointer == rtl
12081 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12083 /* If RTL is internal_arg_pointer, which has been optimized
12084 out, use DRAP instead. */
12085 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12086 VAR_INIT_STATUS_INITIALIZED);
12088 break;
12090 case SIGN_EXTEND:
12091 case ZERO_EXTEND:
12092 if (GET_MODE_CLASS (mode) != MODE_INT)
12093 break;
12094 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12095 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12096 if (op0 == 0)
12097 break;
12098 else if (GET_CODE (rtl) == ZERO_EXTEND
12099 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12100 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12101 < HOST_BITS_PER_WIDE_INT
12102 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12103 to expand zero extend as two shifts instead of
12104 masking. */
12105 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12107 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12108 mem_loc_result = op0;
12109 add_loc_descr (&mem_loc_result,
12110 int_loc_descriptor (GET_MODE_MASK (imode)));
12111 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12113 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12115 int shift = DWARF2_ADDR_SIZE
12116 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12117 shift *= BITS_PER_UNIT;
12118 if (GET_CODE (rtl) == SIGN_EXTEND)
12119 op = DW_OP_shra;
12120 else
12121 op = DW_OP_shr;
12122 mem_loc_result = op0;
12123 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12124 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12125 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12126 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12128 else if (!dwarf_strict)
12130 dw_die_ref type_die1, type_die2;
12131 dw_loc_descr_ref cvt;
12133 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12134 GET_CODE (rtl) == ZERO_EXTEND);
12135 if (type_die1 == NULL)
12136 break;
12137 type_die2 = base_type_for_mode (mode, 1);
12138 if (type_die2 == NULL)
12139 break;
12140 mem_loc_result = op0;
12141 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12142 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12143 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12144 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12145 add_loc_descr (&mem_loc_result, cvt);
12146 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12147 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12148 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12149 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12150 add_loc_descr (&mem_loc_result, cvt);
12152 break;
12154 case MEM:
12156 rtx new_rtl = avoid_constant_pool_reference (rtl);
12157 if (new_rtl != rtl)
12159 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12160 initialized);
12161 if (mem_loc_result != NULL)
12162 return mem_loc_result;
12165 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12166 get_address_mode (rtl), mode,
12167 VAR_INIT_STATUS_INITIALIZED);
12168 if (mem_loc_result == NULL)
12169 mem_loc_result = tls_mem_loc_descriptor (rtl);
12170 if (mem_loc_result != NULL)
12172 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12173 || GET_MODE_CLASS (mode) != MODE_INT)
12175 dw_die_ref type_die;
12176 dw_loc_descr_ref deref;
12178 if (dwarf_strict)
12179 return NULL;
12180 type_die
12181 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12182 if (type_die == NULL)
12183 return NULL;
12184 deref = new_loc_descr (DW_OP_GNU_deref_type,
12185 GET_MODE_SIZE (mode), 0);
12186 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12187 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12188 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12189 add_loc_descr (&mem_loc_result, deref);
12191 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12192 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12193 else
12194 add_loc_descr (&mem_loc_result,
12195 new_loc_descr (DW_OP_deref_size,
12196 GET_MODE_SIZE (mode), 0));
12198 break;
12200 case LO_SUM:
12201 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12203 case LABEL_REF:
12204 /* Some ports can transform a symbol ref into a label ref, because
12205 the symbol ref is too far away and has to be dumped into a constant
12206 pool. */
12207 case CONST:
12208 case SYMBOL_REF:
12209 if (GET_MODE_CLASS (mode) != MODE_INT
12210 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12211 #ifdef POINTERS_EXTEND_UNSIGNED
12212 && (mode != Pmode || mem_mode == VOIDmode)
12213 #endif
12215 break;
12216 if (GET_CODE (rtl) == SYMBOL_REF
12217 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12219 dw_loc_descr_ref temp;
12221 /* If this is not defined, we have no way to emit the data. */
12222 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12223 break;
12225 temp = new_addr_loc_descr (rtl, dtprel_true);
12227 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12228 add_loc_descr (&mem_loc_result, temp);
12230 break;
12233 if (!const_ok_for_output (rtl))
12234 break;
12236 symref:
12237 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12238 vec_safe_push (used_rtx_array, rtl);
12239 break;
12241 case CONCAT:
12242 case CONCATN:
12243 case VAR_LOCATION:
12244 case DEBUG_IMPLICIT_PTR:
12245 expansion_failed (NULL_TREE, rtl,
12246 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12247 return 0;
12249 case ENTRY_VALUE:
12250 if (dwarf_strict)
12251 return NULL;
12252 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12254 if (GET_MODE_CLASS (mode) != MODE_INT
12255 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12256 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12257 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12258 else
12260 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12261 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12262 return NULL;
12263 op0 = one_reg_loc_descriptor (dbx_regnum,
12264 VAR_INIT_STATUS_INITIALIZED);
12267 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12268 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12270 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12271 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12272 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12273 return NULL;
12275 else
12276 gcc_unreachable ();
12277 if (op0 == NULL)
12278 return NULL;
12279 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12280 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12281 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12282 break;
12284 case DEBUG_PARAMETER_REF:
12285 mem_loc_result = parameter_ref_descriptor (rtl);
12286 break;
12288 case PRE_MODIFY:
12289 /* Extract the PLUS expression nested inside and fall into
12290 PLUS code below. */
12291 rtl = XEXP (rtl, 1);
12292 goto plus;
12294 case PRE_INC:
12295 case PRE_DEC:
12296 /* Turn these into a PLUS expression and fall into the PLUS code
12297 below. */
12298 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12299 GEN_INT (GET_CODE (rtl) == PRE_INC
12300 ? GET_MODE_UNIT_SIZE (mem_mode)
12301 : -GET_MODE_UNIT_SIZE (mem_mode)));
12303 /* ... fall through ... */
12305 case PLUS:
12306 plus:
12307 if (is_based_loc (rtl)
12308 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12309 || XEXP (rtl, 0) == arg_pointer_rtx
12310 || XEXP (rtl, 0) == frame_pointer_rtx)
12311 && GET_MODE_CLASS (mode) == MODE_INT)
12312 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12313 INTVAL (XEXP (rtl, 1)),
12314 VAR_INIT_STATUS_INITIALIZED);
12315 else
12317 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12318 VAR_INIT_STATUS_INITIALIZED);
12319 if (mem_loc_result == 0)
12320 break;
12322 if (CONST_INT_P (XEXP (rtl, 1))
12323 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12324 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12325 else
12327 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12328 VAR_INIT_STATUS_INITIALIZED);
12329 if (op1 == 0)
12330 break;
12331 add_loc_descr (&mem_loc_result, op1);
12332 add_loc_descr (&mem_loc_result,
12333 new_loc_descr (DW_OP_plus, 0, 0));
12336 break;
12338 /* If a pseudo-reg is optimized away, it is possible for it to
12339 be replaced with a MEM containing a multiply or shift. */
12340 case MINUS:
12341 op = DW_OP_minus;
12342 goto do_binop;
12344 case MULT:
12345 op = DW_OP_mul;
12346 goto do_binop;
12348 case DIV:
12349 if (!dwarf_strict
12350 && GET_MODE_CLASS (mode) == MODE_INT
12351 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12353 mem_loc_result = typed_binop (DW_OP_div, rtl,
12354 base_type_for_mode (mode, 0),
12355 mode, mem_mode);
12356 break;
12358 op = DW_OP_div;
12359 goto do_binop;
12361 case UMOD:
12362 op = DW_OP_mod;
12363 goto do_binop;
12365 case ASHIFT:
12366 op = DW_OP_shl;
12367 goto do_shift;
12369 case ASHIFTRT:
12370 op = DW_OP_shra;
12371 goto do_shift;
12373 case LSHIFTRT:
12374 op = DW_OP_shr;
12375 goto do_shift;
12377 do_shift:
12378 if (GET_MODE_CLASS (mode) != MODE_INT)
12379 break;
12380 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12381 VAR_INIT_STATUS_INITIALIZED);
12383 rtx rtlop1 = XEXP (rtl, 1);
12384 if (GET_MODE (rtlop1) != VOIDmode
12385 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12386 < GET_MODE_BITSIZE (mode))
12387 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12388 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12389 VAR_INIT_STATUS_INITIALIZED);
12392 if (op0 == 0 || op1 == 0)
12393 break;
12395 mem_loc_result = op0;
12396 add_loc_descr (&mem_loc_result, op1);
12397 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12398 break;
12400 case AND:
12401 op = DW_OP_and;
12402 goto do_binop;
12404 case IOR:
12405 op = DW_OP_or;
12406 goto do_binop;
12408 case XOR:
12409 op = DW_OP_xor;
12410 goto do_binop;
12412 do_binop:
12413 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12414 VAR_INIT_STATUS_INITIALIZED);
12415 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12416 VAR_INIT_STATUS_INITIALIZED);
12418 if (op0 == 0 || op1 == 0)
12419 break;
12421 mem_loc_result = op0;
12422 add_loc_descr (&mem_loc_result, op1);
12423 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12424 break;
12426 case MOD:
12427 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12429 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12430 base_type_for_mode (mode, 0),
12431 mode, mem_mode);
12432 break;
12435 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12436 VAR_INIT_STATUS_INITIALIZED);
12437 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12438 VAR_INIT_STATUS_INITIALIZED);
12440 if (op0 == 0 || op1 == 0)
12441 break;
12443 mem_loc_result = op0;
12444 add_loc_descr (&mem_loc_result, op1);
12445 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12446 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12447 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12448 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12449 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12450 break;
12452 case UDIV:
12453 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12455 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12457 op = DW_OP_div;
12458 goto do_binop;
12460 mem_loc_result = typed_binop (DW_OP_div, rtl,
12461 base_type_for_mode (mode, 1),
12462 mode, mem_mode);
12464 break;
12466 case NOT:
12467 op = DW_OP_not;
12468 goto do_unop;
12470 case ABS:
12471 op = DW_OP_abs;
12472 goto do_unop;
12474 case NEG:
12475 op = DW_OP_neg;
12476 goto do_unop;
12478 do_unop:
12479 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12480 VAR_INIT_STATUS_INITIALIZED);
12482 if (op0 == 0)
12483 break;
12485 mem_loc_result = op0;
12486 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12487 break;
12489 case CONST_INT:
12490 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12491 #ifdef POINTERS_EXTEND_UNSIGNED
12492 || (mode == Pmode
12493 && mem_mode != VOIDmode
12494 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12495 #endif
12498 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12499 break;
12501 if (!dwarf_strict
12502 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12503 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12505 dw_die_ref type_die = base_type_for_mode (mode, 1);
12506 enum machine_mode amode;
12507 if (type_die == NULL)
12508 return NULL;
12509 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12510 MODE_INT, 0);
12511 if (INTVAL (rtl) >= 0
12512 && amode != BLKmode
12513 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12514 /* const DW_OP_GNU_convert <XXX> vs.
12515 DW_OP_GNU_const_type <XXX, 1, const>. */
12516 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12517 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12519 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12520 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12521 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12522 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12523 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12524 add_loc_descr (&mem_loc_result, op0);
12525 return mem_loc_result;
12527 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12528 INTVAL (rtl));
12529 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12530 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12531 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12532 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12533 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12534 else
12536 mem_loc_result->dw_loc_oprnd2.val_class
12537 = dw_val_class_const_double;
12538 mem_loc_result->dw_loc_oprnd2.v.val_double
12539 = double_int::from_shwi (INTVAL (rtl));
12542 break;
12544 case CONST_DOUBLE:
12545 if (!dwarf_strict)
12547 dw_die_ref type_die;
12549 /* Note that a CONST_DOUBLE rtx could represent either an integer
12550 or a floating-point constant. A CONST_DOUBLE is used whenever
12551 the constant requires more than one word in order to be
12552 adequately represented. We output CONST_DOUBLEs as blocks. */
12553 if (mode == VOIDmode
12554 || (GET_MODE (rtl) == VOIDmode
12555 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12556 break;
12557 type_die = base_type_for_mode (mode,
12558 GET_MODE_CLASS (mode) == MODE_INT);
12559 if (type_die == NULL)
12560 return NULL;
12561 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12562 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12563 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12564 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12565 if (SCALAR_FLOAT_MODE_P (mode))
12567 unsigned int length = GET_MODE_SIZE (mode);
12568 unsigned char *array
12569 = (unsigned char*) ggc_alloc_atomic (length);
12571 insert_float (rtl, array);
12572 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12573 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12574 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12575 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12577 else
12579 mem_loc_result->dw_loc_oprnd2.val_class
12580 = dw_val_class_const_double;
12581 mem_loc_result->dw_loc_oprnd2.v.val_double
12582 = rtx_to_double_int (rtl);
12585 break;
12587 case EQ:
12588 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12589 break;
12591 case GE:
12592 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12593 break;
12595 case GT:
12596 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12597 break;
12599 case LE:
12600 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12601 break;
12603 case LT:
12604 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12605 break;
12607 case NE:
12608 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12609 break;
12611 case GEU:
12612 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12613 break;
12615 case GTU:
12616 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12617 break;
12619 case LEU:
12620 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12621 break;
12623 case LTU:
12624 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12625 break;
12627 case UMIN:
12628 case UMAX:
12629 if (GET_MODE_CLASS (mode) != MODE_INT)
12630 break;
12631 /* FALLTHRU */
12632 case SMIN:
12633 case SMAX:
12634 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12635 break;
12637 case ZERO_EXTRACT:
12638 case SIGN_EXTRACT:
12639 if (CONST_INT_P (XEXP (rtl, 1))
12640 && CONST_INT_P (XEXP (rtl, 2))
12641 && ((unsigned) INTVAL (XEXP (rtl, 1))
12642 + (unsigned) INTVAL (XEXP (rtl, 2))
12643 <= GET_MODE_BITSIZE (mode))
12644 && GET_MODE_CLASS (mode) == MODE_INT
12645 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12646 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12648 int shift, size;
12649 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12650 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12651 if (op0 == 0)
12652 break;
12653 if (GET_CODE (rtl) == SIGN_EXTRACT)
12654 op = DW_OP_shra;
12655 else
12656 op = DW_OP_shr;
12657 mem_loc_result = op0;
12658 size = INTVAL (XEXP (rtl, 1));
12659 shift = INTVAL (XEXP (rtl, 2));
12660 if (BITS_BIG_ENDIAN)
12661 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12662 - shift - size;
12663 if (shift + size != (int) DWARF2_ADDR_SIZE)
12665 add_loc_descr (&mem_loc_result,
12666 int_loc_descriptor (DWARF2_ADDR_SIZE
12667 - shift - size));
12668 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12670 if (size != (int) DWARF2_ADDR_SIZE)
12672 add_loc_descr (&mem_loc_result,
12673 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12674 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12677 break;
12679 case IF_THEN_ELSE:
12681 dw_loc_descr_ref op2, bra_node, drop_node;
12682 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12683 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12684 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12685 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12686 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12687 VAR_INIT_STATUS_INITIALIZED);
12688 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12689 VAR_INIT_STATUS_INITIALIZED);
12690 if (op0 == NULL || op1 == NULL || op2 == NULL)
12691 break;
12693 mem_loc_result = op1;
12694 add_loc_descr (&mem_loc_result, op2);
12695 add_loc_descr (&mem_loc_result, op0);
12696 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12697 add_loc_descr (&mem_loc_result, bra_node);
12698 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12699 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12700 add_loc_descr (&mem_loc_result, drop_node);
12701 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12702 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12704 break;
12706 case FLOAT_EXTEND:
12707 case FLOAT_TRUNCATE:
12708 case FLOAT:
12709 case UNSIGNED_FLOAT:
12710 case FIX:
12711 case UNSIGNED_FIX:
12712 if (!dwarf_strict)
12714 dw_die_ref type_die;
12715 dw_loc_descr_ref cvt;
12717 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12718 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12719 if (op0 == NULL)
12720 break;
12721 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12722 && (GET_CODE (rtl) == FLOAT
12723 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12724 <= DWARF2_ADDR_SIZE))
12726 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12727 GET_CODE (rtl) == UNSIGNED_FLOAT);
12728 if (type_die == NULL)
12729 break;
12730 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12731 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12732 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12733 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12734 add_loc_descr (&op0, cvt);
12736 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12737 if (type_die == NULL)
12738 break;
12739 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12740 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12741 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12742 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12743 add_loc_descr (&op0, cvt);
12744 if (GET_MODE_CLASS (mode) == MODE_INT
12745 && (GET_CODE (rtl) == FIX
12746 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12748 op0 = convert_descriptor_to_mode (mode, op0);
12749 if (op0 == NULL)
12750 break;
12752 mem_loc_result = op0;
12754 break;
12756 case CLZ:
12757 case CTZ:
12758 case FFS:
12759 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12760 break;
12762 case POPCOUNT:
12763 case PARITY:
12764 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12765 break;
12767 case BSWAP:
12768 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12769 break;
12771 case ROTATE:
12772 case ROTATERT:
12773 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12774 break;
12776 case COMPARE:
12777 /* In theory, we could implement the above. */
12778 /* DWARF cannot represent the unsigned compare operations
12779 natively. */
12780 case SS_MULT:
12781 case US_MULT:
12782 case SS_DIV:
12783 case US_DIV:
12784 case SS_PLUS:
12785 case US_PLUS:
12786 case SS_MINUS:
12787 case US_MINUS:
12788 case SS_NEG:
12789 case US_NEG:
12790 case SS_ABS:
12791 case SS_ASHIFT:
12792 case US_ASHIFT:
12793 case SS_TRUNCATE:
12794 case US_TRUNCATE:
12795 case UNORDERED:
12796 case ORDERED:
12797 case UNEQ:
12798 case UNGE:
12799 case UNGT:
12800 case UNLE:
12801 case UNLT:
12802 case LTGT:
12803 case FRACT_CONVERT:
12804 case UNSIGNED_FRACT_CONVERT:
12805 case SAT_FRACT:
12806 case UNSIGNED_SAT_FRACT:
12807 case SQRT:
12808 case ASM_OPERANDS:
12809 case VEC_MERGE:
12810 case VEC_SELECT:
12811 case VEC_CONCAT:
12812 case VEC_DUPLICATE:
12813 case UNSPEC:
12814 case HIGH:
12815 case FMA:
12816 case STRICT_LOW_PART:
12817 case CONST_VECTOR:
12818 case CONST_FIXED:
12819 case CLRSB:
12820 case CLOBBER:
12821 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12822 can't express it in the debug info. This can happen e.g. with some
12823 TLS UNSPECs. */
12824 break;
12826 case CONST_STRING:
12827 resolve_one_addr (&rtl, NULL);
12828 goto symref;
12830 default:
12831 #ifdef ENABLE_CHECKING
12832 print_rtl (stderr, rtl);
12833 gcc_unreachable ();
12834 #else
12835 break;
12836 #endif
12839 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12840 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12842 return mem_loc_result;
12845 /* Return a descriptor that describes the concatenation of two locations.
12846 This is typically a complex variable. */
12848 static dw_loc_descr_ref
12849 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12851 dw_loc_descr_ref cc_loc_result = NULL;
12852 dw_loc_descr_ref x0_ref
12853 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12854 dw_loc_descr_ref x1_ref
12855 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12857 if (x0_ref == 0 || x1_ref == 0)
12858 return 0;
12860 cc_loc_result = x0_ref;
12861 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12863 add_loc_descr (&cc_loc_result, x1_ref);
12864 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12866 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12867 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12869 return cc_loc_result;
12872 /* Return a descriptor that describes the concatenation of N
12873 locations. */
12875 static dw_loc_descr_ref
12876 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12878 unsigned int i;
12879 dw_loc_descr_ref cc_loc_result = NULL;
12880 unsigned int n = XVECLEN (concatn, 0);
12882 for (i = 0; i < n; ++i)
12884 dw_loc_descr_ref ref;
12885 rtx x = XVECEXP (concatn, 0, i);
12887 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12888 if (ref == NULL)
12889 return NULL;
12891 add_loc_descr (&cc_loc_result, ref);
12892 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12895 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12896 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12898 return cc_loc_result;
12901 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12902 for DEBUG_IMPLICIT_PTR RTL. */
12904 static dw_loc_descr_ref
12905 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12907 dw_loc_descr_ref ret;
12908 dw_die_ref ref;
12910 if (dwarf_strict)
12911 return NULL;
12912 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12913 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12914 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12915 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12916 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12917 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12918 if (ref)
12920 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12921 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12922 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12924 else
12926 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12927 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12929 return ret;
12932 /* Output a proper Dwarf location descriptor for a variable or parameter
12933 which is either allocated in a register or in a memory location. For a
12934 register, we just generate an OP_REG and the register number. For a
12935 memory location we provide a Dwarf postfix expression describing how to
12936 generate the (dynamic) address of the object onto the address stack.
12938 MODE is mode of the decl if this loc_descriptor is going to be used in
12939 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12940 allowed, VOIDmode otherwise.
12942 If we don't know how to describe it, return 0. */
12944 static dw_loc_descr_ref
12945 loc_descriptor (rtx rtl, enum machine_mode mode,
12946 enum var_init_status initialized)
12948 dw_loc_descr_ref loc_result = NULL;
12950 switch (GET_CODE (rtl))
12952 case SUBREG:
12953 /* The case of a subreg may arise when we have a local (register)
12954 variable or a formal (register) parameter which doesn't quite fill
12955 up an entire register. For now, just assume that it is
12956 legitimate to make the Dwarf info refer to the whole register which
12957 contains the given subreg. */
12958 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12959 loc_result = loc_descriptor (SUBREG_REG (rtl),
12960 GET_MODE (SUBREG_REG (rtl)), initialized);
12961 else
12962 goto do_default;
12963 break;
12965 case REG:
12966 loc_result = reg_loc_descriptor (rtl, initialized);
12967 break;
12969 case MEM:
12970 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12971 GET_MODE (rtl), initialized);
12972 if (loc_result == NULL)
12973 loc_result = tls_mem_loc_descriptor (rtl);
12974 if (loc_result == NULL)
12976 rtx new_rtl = avoid_constant_pool_reference (rtl);
12977 if (new_rtl != rtl)
12978 loc_result = loc_descriptor (new_rtl, mode, initialized);
12980 break;
12982 case CONCAT:
12983 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12984 initialized);
12985 break;
12987 case CONCATN:
12988 loc_result = concatn_loc_descriptor (rtl, initialized);
12989 break;
12991 case VAR_LOCATION:
12992 /* Single part. */
12993 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12995 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12996 if (GET_CODE (loc) == EXPR_LIST)
12997 loc = XEXP (loc, 0);
12998 loc_result = loc_descriptor (loc, mode, initialized);
12999 break;
13002 rtl = XEXP (rtl, 1);
13003 /* FALLTHRU */
13005 case PARALLEL:
13007 rtvec par_elems = XVEC (rtl, 0);
13008 int num_elem = GET_NUM_ELEM (par_elems);
13009 enum machine_mode mode;
13010 int i;
13012 /* Create the first one, so we have something to add to. */
13013 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13014 VOIDmode, initialized);
13015 if (loc_result == NULL)
13016 return NULL;
13017 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13018 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13019 for (i = 1; i < num_elem; i++)
13021 dw_loc_descr_ref temp;
13023 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13024 VOIDmode, initialized);
13025 if (temp == NULL)
13026 return NULL;
13027 add_loc_descr (&loc_result, temp);
13028 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13029 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13032 break;
13034 case CONST_INT:
13035 if (mode != VOIDmode && mode != BLKmode)
13036 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13037 INTVAL (rtl));
13038 break;
13040 case CONST_DOUBLE:
13041 if (mode == VOIDmode)
13042 mode = GET_MODE (rtl);
13044 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13046 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13048 /* Note that a CONST_DOUBLE rtx could represent either an integer
13049 or a floating-point constant. A CONST_DOUBLE is used whenever
13050 the constant requires more than one word in order to be
13051 adequately represented. We output CONST_DOUBLEs as blocks. */
13052 loc_result = new_loc_descr (DW_OP_implicit_value,
13053 GET_MODE_SIZE (mode), 0);
13054 if (SCALAR_FLOAT_MODE_P (mode))
13056 unsigned int length = GET_MODE_SIZE (mode);
13057 unsigned char *array
13058 = (unsigned char*) ggc_alloc_atomic (length);
13060 insert_float (rtl, array);
13061 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13062 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13063 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13064 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13066 else
13068 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13069 loc_result->dw_loc_oprnd2.v.val_double
13070 = rtx_to_double_int (rtl);
13073 break;
13075 case CONST_VECTOR:
13076 if (mode == VOIDmode)
13077 mode = GET_MODE (rtl);
13079 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13081 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13082 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13083 unsigned char *array = (unsigned char *)
13084 ggc_alloc_atomic (length * elt_size);
13085 unsigned int i;
13086 unsigned char *p;
13088 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13089 switch (GET_MODE_CLASS (mode))
13091 case MODE_VECTOR_INT:
13092 for (i = 0, p = array; i < length; i++, p += elt_size)
13094 rtx elt = CONST_VECTOR_ELT (rtl, i);
13095 double_int val = rtx_to_double_int (elt);
13097 if (elt_size <= sizeof (HOST_WIDE_INT))
13098 insert_int (val.to_shwi (), elt_size, p);
13099 else
13101 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13102 insert_double (val, p);
13105 break;
13107 case MODE_VECTOR_FLOAT:
13108 for (i = 0, p = array; i < length; i++, p += elt_size)
13110 rtx elt = CONST_VECTOR_ELT (rtl, i);
13111 insert_float (elt, p);
13113 break;
13115 default:
13116 gcc_unreachable ();
13119 loc_result = new_loc_descr (DW_OP_implicit_value,
13120 length * elt_size, 0);
13121 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13122 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13123 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13124 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13126 break;
13128 case CONST:
13129 if (mode == VOIDmode
13130 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13131 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13132 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13134 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13135 break;
13137 /* FALLTHROUGH */
13138 case SYMBOL_REF:
13139 if (!const_ok_for_output (rtl))
13140 break;
13141 case LABEL_REF:
13142 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13143 && (dwarf_version >= 4 || !dwarf_strict))
13145 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13146 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13147 vec_safe_push (used_rtx_array, rtl);
13149 break;
13151 case DEBUG_IMPLICIT_PTR:
13152 loc_result = implicit_ptr_descriptor (rtl, 0);
13153 break;
13155 case PLUS:
13156 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13157 && CONST_INT_P (XEXP (rtl, 1)))
13159 loc_result
13160 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13161 break;
13163 /* FALLTHRU */
13164 do_default:
13165 default:
13166 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13167 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13168 && dwarf_version >= 4)
13169 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13171 /* Value expression. */
13172 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13173 if (loc_result)
13174 add_loc_descr (&loc_result,
13175 new_loc_descr (DW_OP_stack_value, 0, 0));
13177 break;
13180 return loc_result;
13183 /* We need to figure out what section we should use as the base for the
13184 address ranges where a given location is valid.
13185 1. If this particular DECL has a section associated with it, use that.
13186 2. If this function has a section associated with it, use that.
13187 3. Otherwise, use the text section.
13188 XXX: If you split a variable across multiple sections, we won't notice. */
13190 static const char *
13191 secname_for_decl (const_tree decl)
13193 const char *secname;
13195 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13197 tree sectree = DECL_SECTION_NAME (decl);
13198 secname = TREE_STRING_POINTER (sectree);
13200 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13202 tree sectree = DECL_SECTION_NAME (current_function_decl);
13203 secname = TREE_STRING_POINTER (sectree);
13205 else if (cfun && in_cold_section_p)
13206 secname = crtl->subsections.cold_section_label;
13207 else
13208 secname = text_section_label;
13210 return secname;
13213 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13215 static bool
13216 decl_by_reference_p (tree decl)
13218 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13219 || TREE_CODE (decl) == VAR_DECL)
13220 && DECL_BY_REFERENCE (decl));
13223 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13224 for VARLOC. */
13226 static dw_loc_descr_ref
13227 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13228 enum var_init_status initialized)
13230 int have_address = 0;
13231 dw_loc_descr_ref descr;
13232 enum machine_mode mode;
13234 if (want_address != 2)
13236 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13237 /* Single part. */
13238 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13240 varloc = PAT_VAR_LOCATION_LOC (varloc);
13241 if (GET_CODE (varloc) == EXPR_LIST)
13242 varloc = XEXP (varloc, 0);
13243 mode = GET_MODE (varloc);
13244 if (MEM_P (varloc))
13246 rtx addr = XEXP (varloc, 0);
13247 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13248 mode, initialized);
13249 if (descr)
13250 have_address = 1;
13251 else
13253 rtx x = avoid_constant_pool_reference (varloc);
13254 if (x != varloc)
13255 descr = mem_loc_descriptor (x, mode, VOIDmode,
13256 initialized);
13259 else
13260 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13262 else
13263 return 0;
13265 else
13267 if (GET_CODE (varloc) == VAR_LOCATION)
13268 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13269 else
13270 mode = DECL_MODE (loc);
13271 descr = loc_descriptor (varloc, mode, initialized);
13272 have_address = 1;
13275 if (!descr)
13276 return 0;
13278 if (want_address == 2 && !have_address
13279 && (dwarf_version >= 4 || !dwarf_strict))
13281 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13283 expansion_failed (loc, NULL_RTX,
13284 "DWARF address size mismatch");
13285 return 0;
13287 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13288 have_address = 1;
13290 /* Show if we can't fill the request for an address. */
13291 if (want_address && !have_address)
13293 expansion_failed (loc, NULL_RTX,
13294 "Want address and only have value");
13295 return 0;
13298 /* If we've got an address and don't want one, dereference. */
13299 if (!want_address && have_address)
13301 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13302 enum dwarf_location_atom op;
13304 if (size > DWARF2_ADDR_SIZE || size == -1)
13306 expansion_failed (loc, NULL_RTX,
13307 "DWARF address size mismatch");
13308 return 0;
13310 else if (size == DWARF2_ADDR_SIZE)
13311 op = DW_OP_deref;
13312 else
13313 op = DW_OP_deref_size;
13315 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13318 return descr;
13321 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13322 if it is not possible. */
13324 static dw_loc_descr_ref
13325 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13327 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13328 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13329 else if (dwarf_version >= 3 || !dwarf_strict)
13330 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13331 else
13332 return NULL;
13335 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13336 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13338 static dw_loc_descr_ref
13339 dw_sra_loc_expr (tree decl, rtx loc)
13341 rtx p;
13342 unsigned int padsize = 0;
13343 dw_loc_descr_ref descr, *descr_tail;
13344 unsigned HOST_WIDE_INT decl_size;
13345 rtx varloc;
13346 enum var_init_status initialized;
13348 if (DECL_SIZE (decl) == NULL
13349 || !host_integerp (DECL_SIZE (decl), 1))
13350 return NULL;
13352 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13353 descr = NULL;
13354 descr_tail = &descr;
13356 for (p = loc; p; p = XEXP (p, 1))
13358 unsigned int bitsize = decl_piece_bitsize (p);
13359 rtx loc_note = *decl_piece_varloc_ptr (p);
13360 dw_loc_descr_ref cur_descr;
13361 dw_loc_descr_ref *tail, last = NULL;
13362 unsigned int opsize = 0;
13364 if (loc_note == NULL_RTX
13365 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13367 padsize += bitsize;
13368 continue;
13370 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13371 varloc = NOTE_VAR_LOCATION (loc_note);
13372 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13373 if (cur_descr == NULL)
13375 padsize += bitsize;
13376 continue;
13379 /* Check that cur_descr either doesn't use
13380 DW_OP_*piece operations, or their sum is equal
13381 to bitsize. Otherwise we can't embed it. */
13382 for (tail = &cur_descr; *tail != NULL;
13383 tail = &(*tail)->dw_loc_next)
13384 if ((*tail)->dw_loc_opc == DW_OP_piece)
13386 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13387 * BITS_PER_UNIT;
13388 last = *tail;
13390 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13392 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13393 last = *tail;
13396 if (last != NULL && opsize != bitsize)
13398 padsize += bitsize;
13399 continue;
13402 /* If there is a hole, add DW_OP_*piece after empty DWARF
13403 expression, which means that those bits are optimized out. */
13404 if (padsize)
13406 if (padsize > decl_size)
13407 return NULL;
13408 decl_size -= padsize;
13409 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13410 if (*descr_tail == NULL)
13411 return NULL;
13412 descr_tail = &(*descr_tail)->dw_loc_next;
13413 padsize = 0;
13415 *descr_tail = cur_descr;
13416 descr_tail = tail;
13417 if (bitsize > decl_size)
13418 return NULL;
13419 decl_size -= bitsize;
13420 if (last == NULL)
13422 HOST_WIDE_INT offset = 0;
13423 if (GET_CODE (varloc) == VAR_LOCATION
13424 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13426 varloc = PAT_VAR_LOCATION_LOC (varloc);
13427 if (GET_CODE (varloc) == EXPR_LIST)
13428 varloc = XEXP (varloc, 0);
13432 if (GET_CODE (varloc) == CONST
13433 || GET_CODE (varloc) == SIGN_EXTEND
13434 || GET_CODE (varloc) == ZERO_EXTEND)
13435 varloc = XEXP (varloc, 0);
13436 else if (GET_CODE (varloc) == SUBREG)
13437 varloc = SUBREG_REG (varloc);
13438 else
13439 break;
13441 while (1);
13442 /* DW_OP_bit_size offset should be zero for register
13443 or implicit location descriptions and empty location
13444 descriptions, but for memory addresses needs big endian
13445 adjustment. */
13446 if (MEM_P (varloc))
13448 unsigned HOST_WIDE_INT memsize
13449 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13450 if (memsize != bitsize)
13452 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13453 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13454 return NULL;
13455 if (memsize < bitsize)
13456 return NULL;
13457 if (BITS_BIG_ENDIAN)
13458 offset = memsize - bitsize;
13462 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13463 if (*descr_tail == NULL)
13464 return NULL;
13465 descr_tail = &(*descr_tail)->dw_loc_next;
13469 /* If there were any non-empty expressions, add padding till the end of
13470 the decl. */
13471 if (descr != NULL && decl_size != 0)
13473 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13474 if (*descr_tail == NULL)
13475 return NULL;
13477 return descr;
13480 /* Return the dwarf representation of the location list LOC_LIST of
13481 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13482 function. */
13484 static dw_loc_list_ref
13485 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13487 const char *endname, *secname;
13488 rtx varloc;
13489 enum var_init_status initialized;
13490 struct var_loc_node *node;
13491 dw_loc_descr_ref descr;
13492 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13493 dw_loc_list_ref list = NULL;
13494 dw_loc_list_ref *listp = &list;
13496 /* Now that we know what section we are using for a base,
13497 actually construct the list of locations.
13498 The first location information is what is passed to the
13499 function that creates the location list, and the remaining
13500 locations just get added on to that list.
13501 Note that we only know the start address for a location
13502 (IE location changes), so to build the range, we use
13503 the range [current location start, next location start].
13504 This means we have to special case the last node, and generate
13505 a range of [last location start, end of function label]. */
13507 secname = secname_for_decl (decl);
13509 for (node = loc_list->first; node; node = node->next)
13510 if (GET_CODE (node->loc) == EXPR_LIST
13511 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13513 if (GET_CODE (node->loc) == EXPR_LIST)
13515 /* This requires DW_OP_{,bit_}piece, which is not usable
13516 inside DWARF expressions. */
13517 if (want_address != 2)
13518 continue;
13519 descr = dw_sra_loc_expr (decl, node->loc);
13520 if (descr == NULL)
13521 continue;
13523 else
13525 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13526 varloc = NOTE_VAR_LOCATION (node->loc);
13527 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13529 if (descr)
13531 bool range_across_switch = false;
13532 /* If section switch happens in between node->label
13533 and node->next->label (or end of function) and
13534 we can't emit it as a single entry list,
13535 emit two ranges, first one ending at the end
13536 of first partition and second one starting at the
13537 beginning of second partition. */
13538 if (node == loc_list->last_before_switch
13539 && (node != loc_list->first || loc_list->first->next)
13540 && current_function_decl)
13542 endname = cfun->fde->dw_fde_end;
13543 range_across_switch = true;
13545 /* The variable has a location between NODE->LABEL and
13546 NODE->NEXT->LABEL. */
13547 else if (node->next)
13548 endname = node->next->label;
13549 /* If the variable has a location at the last label
13550 it keeps its location until the end of function. */
13551 else if (!current_function_decl)
13552 endname = text_end_label;
13553 else
13555 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13556 current_function_funcdef_no);
13557 endname = ggc_strdup (label_id);
13560 *listp = new_loc_list (descr, node->label, endname, secname);
13561 if (TREE_CODE (decl) == PARM_DECL
13562 && node == loc_list->first
13563 && NOTE_P (node->loc)
13564 && strcmp (node->label, endname) == 0)
13565 (*listp)->force = true;
13566 listp = &(*listp)->dw_loc_next;
13568 if (range_across_switch)
13570 if (GET_CODE (node->loc) == EXPR_LIST)
13571 descr = dw_sra_loc_expr (decl, node->loc);
13572 else
13574 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13575 varloc = NOTE_VAR_LOCATION (node->loc);
13576 descr = dw_loc_list_1 (decl, varloc, want_address,
13577 initialized);
13579 gcc_assert (descr);
13580 /* The variable has a location between NODE->LABEL and
13581 NODE->NEXT->LABEL. */
13582 if (node->next)
13583 endname = node->next->label;
13584 else
13585 endname = cfun->fde->dw_fde_second_end;
13586 *listp = new_loc_list (descr,
13587 cfun->fde->dw_fde_second_begin,
13588 endname, secname);
13589 listp = &(*listp)->dw_loc_next;
13594 /* Try to avoid the overhead of a location list emitting a location
13595 expression instead, but only if we didn't have more than one
13596 location entry in the first place. If some entries were not
13597 representable, we don't want to pretend a single entry that was
13598 applies to the entire scope in which the variable is
13599 available. */
13600 if (list && loc_list->first->next)
13601 gen_llsym (list);
13603 return list;
13606 /* Return if the loc_list has only single element and thus can be represented
13607 as location description. */
13609 static bool
13610 single_element_loc_list_p (dw_loc_list_ref list)
13612 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13613 return !list->ll_symbol;
13616 /* To each location in list LIST add loc descr REF. */
13618 static void
13619 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13621 dw_loc_descr_ref copy;
13622 add_loc_descr (&list->expr, ref);
13623 list = list->dw_loc_next;
13624 while (list)
13626 copy = ggc_alloc_dw_loc_descr_node ();
13627 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13628 add_loc_descr (&list->expr, copy);
13629 while (copy->dw_loc_next)
13631 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13632 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13633 copy->dw_loc_next = new_copy;
13634 copy = new_copy;
13636 list = list->dw_loc_next;
13640 /* Given two lists RET and LIST
13641 produce location list that is result of adding expression in LIST
13642 to expression in RET on each position in program.
13643 Might be destructive on both RET and LIST.
13645 TODO: We handle only simple cases of RET or LIST having at most one
13646 element. General case would inolve sorting the lists in program order
13647 and merging them that will need some additional work.
13648 Adding that will improve quality of debug info especially for SRA-ed
13649 structures. */
13651 static void
13652 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13654 if (!list)
13655 return;
13656 if (!*ret)
13658 *ret = list;
13659 return;
13661 if (!list->dw_loc_next)
13663 add_loc_descr_to_each (*ret, list->expr);
13664 return;
13666 if (!(*ret)->dw_loc_next)
13668 add_loc_descr_to_each (list, (*ret)->expr);
13669 *ret = list;
13670 return;
13672 expansion_failed (NULL_TREE, NULL_RTX,
13673 "Don't know how to merge two non-trivial"
13674 " location lists.\n");
13675 *ret = NULL;
13676 return;
13679 /* LOC is constant expression. Try a luck, look it up in constant
13680 pool and return its loc_descr of its address. */
13682 static dw_loc_descr_ref
13683 cst_pool_loc_descr (tree loc)
13685 /* Get an RTL for this, if something has been emitted. */
13686 rtx rtl = lookup_constant_def (loc);
13688 if (!rtl || !MEM_P (rtl))
13690 gcc_assert (!rtl);
13691 return 0;
13693 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13695 /* TODO: We might get more coverage if we was actually delaying expansion
13696 of all expressions till end of compilation when constant pools are fully
13697 populated. */
13698 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13700 expansion_failed (loc, NULL_RTX,
13701 "CST value in contant pool but not marked.");
13702 return 0;
13704 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13705 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13708 /* Return dw_loc_list representing address of addr_expr LOC
13709 by looking for inner INDIRECT_REF expression and turning
13710 it into simple arithmetics. */
13712 static dw_loc_list_ref
13713 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13715 tree obj, offset;
13716 HOST_WIDE_INT bitsize, bitpos, bytepos;
13717 enum machine_mode mode;
13718 int unsignedp, volatilep = 0;
13719 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13721 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13722 &bitsize, &bitpos, &offset, &mode,
13723 &unsignedp, &volatilep, false);
13724 STRIP_NOPS (obj);
13725 if (bitpos % BITS_PER_UNIT)
13727 expansion_failed (loc, NULL_RTX, "bitfield access");
13728 return 0;
13730 if (!INDIRECT_REF_P (obj))
13732 expansion_failed (obj,
13733 NULL_RTX, "no indirect ref in inner refrence");
13734 return 0;
13736 if (!offset && !bitpos)
13737 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13738 else if (toplev
13739 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13740 && (dwarf_version >= 4 || !dwarf_strict))
13742 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13743 if (!list_ret)
13744 return 0;
13745 if (offset)
13747 /* Variable offset. */
13748 list_ret1 = loc_list_from_tree (offset, 0);
13749 if (list_ret1 == 0)
13750 return 0;
13751 add_loc_list (&list_ret, list_ret1);
13752 if (!list_ret)
13753 return 0;
13754 add_loc_descr_to_each (list_ret,
13755 new_loc_descr (DW_OP_plus, 0, 0));
13757 bytepos = bitpos / BITS_PER_UNIT;
13758 if (bytepos > 0)
13759 add_loc_descr_to_each (list_ret,
13760 new_loc_descr (DW_OP_plus_uconst,
13761 bytepos, 0));
13762 else if (bytepos < 0)
13763 loc_list_plus_const (list_ret, bytepos);
13764 add_loc_descr_to_each (list_ret,
13765 new_loc_descr (DW_OP_stack_value, 0, 0));
13767 return list_ret;
13771 /* Generate Dwarf location list representing LOC.
13772 If WANT_ADDRESS is false, expression computing LOC will be computed
13773 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13774 if WANT_ADDRESS is 2, expression computing address useable in location
13775 will be returned (i.e. DW_OP_reg can be used
13776 to refer to register values). */
13778 static dw_loc_list_ref
13779 loc_list_from_tree (tree loc, int want_address)
13781 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13782 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13783 int have_address = 0;
13784 enum dwarf_location_atom op;
13786 /* ??? Most of the time we do not take proper care for sign/zero
13787 extending the values properly. Hopefully this won't be a real
13788 problem... */
13790 switch (TREE_CODE (loc))
13792 case ERROR_MARK:
13793 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13794 return 0;
13796 case PLACEHOLDER_EXPR:
13797 /* This case involves extracting fields from an object to determine the
13798 position of other fields. We don't try to encode this here. The
13799 only user of this is Ada, which encodes the needed information using
13800 the names of types. */
13801 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13802 return 0;
13804 case CALL_EXPR:
13805 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13806 /* There are no opcodes for these operations. */
13807 return 0;
13809 case PREINCREMENT_EXPR:
13810 case PREDECREMENT_EXPR:
13811 case POSTINCREMENT_EXPR:
13812 case POSTDECREMENT_EXPR:
13813 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13814 /* There are no opcodes for these operations. */
13815 return 0;
13817 case ADDR_EXPR:
13818 /* If we already want an address, see if there is INDIRECT_REF inside
13819 e.g. for &this->field. */
13820 if (want_address)
13822 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13823 (loc, want_address == 2);
13824 if (list_ret)
13825 have_address = 1;
13826 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13827 && (ret = cst_pool_loc_descr (loc)))
13828 have_address = 1;
13830 /* Otherwise, process the argument and look for the address. */
13831 if (!list_ret && !ret)
13832 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13833 else
13835 if (want_address)
13836 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13837 return NULL;
13839 break;
13841 case VAR_DECL:
13842 if (DECL_THREAD_LOCAL_P (loc))
13844 rtx rtl;
13845 enum dwarf_location_atom tls_op;
13846 enum dtprel_bool dtprel = dtprel_false;
13848 if (targetm.have_tls)
13850 /* If this is not defined, we have no way to emit the
13851 data. */
13852 if (!targetm.asm_out.output_dwarf_dtprel)
13853 return 0;
13855 /* The way DW_OP_GNU_push_tls_address is specified, we
13856 can only look up addresses of objects in the current
13857 module. We used DW_OP_addr as first op, but that's
13858 wrong, because DW_OP_addr is relocated by the debug
13859 info consumer, while DW_OP_GNU_push_tls_address
13860 operand shouldn't be. */
13861 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13862 return 0;
13863 dtprel = dtprel_true;
13864 tls_op = DW_OP_GNU_push_tls_address;
13866 else
13868 if (!targetm.emutls.debug_form_tls_address
13869 || !(dwarf_version >= 3 || !dwarf_strict))
13870 return 0;
13871 /* We stuffed the control variable into the DECL_VALUE_EXPR
13872 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13873 no longer appear in gimple code. We used the control
13874 variable in specific so that we could pick it up here. */
13875 loc = DECL_VALUE_EXPR (loc);
13876 tls_op = DW_OP_form_tls_address;
13879 rtl = rtl_for_decl_location (loc);
13880 if (rtl == NULL_RTX)
13881 return 0;
13883 if (!MEM_P (rtl))
13884 return 0;
13885 rtl = XEXP (rtl, 0);
13886 if (! CONSTANT_P (rtl))
13887 return 0;
13889 ret = new_addr_loc_descr (rtl, dtprel);
13890 ret1 = new_loc_descr (tls_op, 0, 0);
13891 add_loc_descr (&ret, ret1);
13893 have_address = 1;
13894 break;
13896 /* FALLTHRU */
13898 case PARM_DECL:
13899 case RESULT_DECL:
13900 if (DECL_HAS_VALUE_EXPR_P (loc))
13901 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13902 want_address);
13903 /* FALLTHRU */
13905 case FUNCTION_DECL:
13907 rtx rtl;
13908 var_loc_list *loc_list = lookup_decl_loc (loc);
13910 if (loc_list && loc_list->first)
13912 list_ret = dw_loc_list (loc_list, loc, want_address);
13913 have_address = want_address != 0;
13914 break;
13916 rtl = rtl_for_decl_location (loc);
13917 if (rtl == NULL_RTX)
13919 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13920 return 0;
13922 else if (CONST_INT_P (rtl))
13924 HOST_WIDE_INT val = INTVAL (rtl);
13925 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13926 val &= GET_MODE_MASK (DECL_MODE (loc));
13927 ret = int_loc_descriptor (val);
13929 else if (GET_CODE (rtl) == CONST_STRING)
13931 expansion_failed (loc, NULL_RTX, "CONST_STRING");
13932 return 0;
13934 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13935 ret = new_addr_loc_descr (rtl, dtprel_false);
13936 else
13938 enum machine_mode mode, mem_mode;
13940 /* Certain constructs can only be represented at top-level. */
13941 if (want_address == 2)
13943 ret = loc_descriptor (rtl, VOIDmode,
13944 VAR_INIT_STATUS_INITIALIZED);
13945 have_address = 1;
13947 else
13949 mode = GET_MODE (rtl);
13950 mem_mode = VOIDmode;
13951 if (MEM_P (rtl))
13953 mem_mode = mode;
13954 mode = get_address_mode (rtl);
13955 rtl = XEXP (rtl, 0);
13956 have_address = 1;
13958 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13959 VAR_INIT_STATUS_INITIALIZED);
13961 if (!ret)
13962 expansion_failed (loc, rtl,
13963 "failed to produce loc descriptor for rtl");
13966 break;
13968 case MEM_REF:
13969 /* ??? FIXME. */
13970 if (!integer_zerop (TREE_OPERAND (loc, 1)))
13971 return 0;
13972 /* Fallthru. */
13973 case INDIRECT_REF:
13974 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13975 have_address = 1;
13976 break;
13978 case COMPOUND_EXPR:
13979 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13981 CASE_CONVERT:
13982 case VIEW_CONVERT_EXPR:
13983 case SAVE_EXPR:
13984 case MODIFY_EXPR:
13985 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13987 case COMPONENT_REF:
13988 case BIT_FIELD_REF:
13989 case ARRAY_REF:
13990 case ARRAY_RANGE_REF:
13991 case REALPART_EXPR:
13992 case IMAGPART_EXPR:
13994 tree obj, offset;
13995 HOST_WIDE_INT bitsize, bitpos, bytepos;
13996 enum machine_mode mode;
13997 int unsignedp, volatilep = 0;
13999 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14000 &unsignedp, &volatilep, false);
14002 gcc_assert (obj != loc);
14004 list_ret = loc_list_from_tree (obj,
14005 want_address == 2
14006 && !bitpos && !offset ? 2 : 1);
14007 /* TODO: We can extract value of the small expression via shifting even
14008 for nonzero bitpos. */
14009 if (list_ret == 0)
14010 return 0;
14011 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14013 expansion_failed (loc, NULL_RTX,
14014 "bitfield access");
14015 return 0;
14018 if (offset != NULL_TREE)
14020 /* Variable offset. */
14021 list_ret1 = loc_list_from_tree (offset, 0);
14022 if (list_ret1 == 0)
14023 return 0;
14024 add_loc_list (&list_ret, list_ret1);
14025 if (!list_ret)
14026 return 0;
14027 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14030 bytepos = bitpos / BITS_PER_UNIT;
14031 if (bytepos > 0)
14032 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14033 else if (bytepos < 0)
14034 loc_list_plus_const (list_ret, bytepos);
14036 have_address = 1;
14037 break;
14040 case INTEGER_CST:
14041 if ((want_address || !host_integerp (loc, 0))
14042 && (ret = cst_pool_loc_descr (loc)))
14043 have_address = 1;
14044 else if (want_address == 2
14045 && host_integerp (loc, 0)
14046 && (ret = address_of_int_loc_descriptor
14047 (int_size_in_bytes (TREE_TYPE (loc)),
14048 tree_low_cst (loc, 0))))
14049 have_address = 1;
14050 else if (host_integerp (loc, 0))
14051 ret = int_loc_descriptor (tree_low_cst (loc, 0));
14052 else
14054 expansion_failed (loc, NULL_RTX,
14055 "Integer operand is not host integer");
14056 return 0;
14058 break;
14060 case CONSTRUCTOR:
14061 case REAL_CST:
14062 case STRING_CST:
14063 case COMPLEX_CST:
14064 if ((ret = cst_pool_loc_descr (loc)))
14065 have_address = 1;
14066 else
14067 /* We can construct small constants here using int_loc_descriptor. */
14068 expansion_failed (loc, NULL_RTX,
14069 "constructor or constant not in constant pool");
14070 break;
14072 case TRUTH_AND_EXPR:
14073 case TRUTH_ANDIF_EXPR:
14074 case BIT_AND_EXPR:
14075 op = DW_OP_and;
14076 goto do_binop;
14078 case TRUTH_XOR_EXPR:
14079 case BIT_XOR_EXPR:
14080 op = DW_OP_xor;
14081 goto do_binop;
14083 case TRUTH_OR_EXPR:
14084 case TRUTH_ORIF_EXPR:
14085 case BIT_IOR_EXPR:
14086 op = DW_OP_or;
14087 goto do_binop;
14089 case FLOOR_DIV_EXPR:
14090 case CEIL_DIV_EXPR:
14091 case ROUND_DIV_EXPR:
14092 case TRUNC_DIV_EXPR:
14093 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14094 return 0;
14095 op = DW_OP_div;
14096 goto do_binop;
14098 case MINUS_EXPR:
14099 op = DW_OP_minus;
14100 goto do_binop;
14102 case FLOOR_MOD_EXPR:
14103 case CEIL_MOD_EXPR:
14104 case ROUND_MOD_EXPR:
14105 case TRUNC_MOD_EXPR:
14106 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14108 op = DW_OP_mod;
14109 goto do_binop;
14111 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14112 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14113 if (list_ret == 0 || list_ret1 == 0)
14114 return 0;
14116 add_loc_list (&list_ret, list_ret1);
14117 if (list_ret == 0)
14118 return 0;
14119 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14120 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14121 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14122 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14123 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14124 break;
14126 case MULT_EXPR:
14127 op = DW_OP_mul;
14128 goto do_binop;
14130 case LSHIFT_EXPR:
14131 op = DW_OP_shl;
14132 goto do_binop;
14134 case RSHIFT_EXPR:
14135 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14136 goto do_binop;
14138 case POINTER_PLUS_EXPR:
14139 case PLUS_EXPR:
14140 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14142 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14143 if (list_ret == 0)
14144 return 0;
14146 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14147 break;
14150 op = DW_OP_plus;
14151 goto do_binop;
14153 case LE_EXPR:
14154 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14155 return 0;
14157 op = DW_OP_le;
14158 goto do_binop;
14160 case GE_EXPR:
14161 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14162 return 0;
14164 op = DW_OP_ge;
14165 goto do_binop;
14167 case LT_EXPR:
14168 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14169 return 0;
14171 op = DW_OP_lt;
14172 goto do_binop;
14174 case GT_EXPR:
14175 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14176 return 0;
14178 op = DW_OP_gt;
14179 goto do_binop;
14181 case EQ_EXPR:
14182 op = DW_OP_eq;
14183 goto do_binop;
14185 case NE_EXPR:
14186 op = DW_OP_ne;
14187 goto do_binop;
14189 do_binop:
14190 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14191 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14192 if (list_ret == 0 || list_ret1 == 0)
14193 return 0;
14195 add_loc_list (&list_ret, list_ret1);
14196 if (list_ret == 0)
14197 return 0;
14198 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14199 break;
14201 case TRUTH_NOT_EXPR:
14202 case BIT_NOT_EXPR:
14203 op = DW_OP_not;
14204 goto do_unop;
14206 case ABS_EXPR:
14207 op = DW_OP_abs;
14208 goto do_unop;
14210 case NEGATE_EXPR:
14211 op = DW_OP_neg;
14212 goto do_unop;
14214 do_unop:
14215 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14216 if (list_ret == 0)
14217 return 0;
14219 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14220 break;
14222 case MIN_EXPR:
14223 case MAX_EXPR:
14225 const enum tree_code code =
14226 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14228 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14229 build2 (code, integer_type_node,
14230 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14231 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14234 /* ... fall through ... */
14236 case COND_EXPR:
14238 dw_loc_descr_ref lhs
14239 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14240 dw_loc_list_ref rhs
14241 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14242 dw_loc_descr_ref bra_node, jump_node, tmp;
14244 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14245 if (list_ret == 0 || lhs == 0 || rhs == 0)
14246 return 0;
14248 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14249 add_loc_descr_to_each (list_ret, bra_node);
14251 add_loc_list (&list_ret, rhs);
14252 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14253 add_loc_descr_to_each (list_ret, jump_node);
14255 add_loc_descr_to_each (list_ret, lhs);
14256 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14257 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14259 /* ??? Need a node to point the skip at. Use a nop. */
14260 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14261 add_loc_descr_to_each (list_ret, tmp);
14262 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14263 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14265 break;
14267 case FIX_TRUNC_EXPR:
14268 return 0;
14270 default:
14271 /* Leave front-end specific codes as simply unknown. This comes
14272 up, for instance, with the C STMT_EXPR. */
14273 if ((unsigned int) TREE_CODE (loc)
14274 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14276 expansion_failed (loc, NULL_RTX,
14277 "language specific tree node");
14278 return 0;
14281 #ifdef ENABLE_CHECKING
14282 /* Otherwise this is a generic code; we should just lists all of
14283 these explicitly. We forgot one. */
14284 gcc_unreachable ();
14285 #else
14286 /* In a release build, we want to degrade gracefully: better to
14287 generate incomplete debugging information than to crash. */
14288 return NULL;
14289 #endif
14292 if (!ret && !list_ret)
14293 return 0;
14295 if (want_address == 2 && !have_address
14296 && (dwarf_version >= 4 || !dwarf_strict))
14298 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14300 expansion_failed (loc, NULL_RTX,
14301 "DWARF address size mismatch");
14302 return 0;
14304 if (ret)
14305 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14306 else
14307 add_loc_descr_to_each (list_ret,
14308 new_loc_descr (DW_OP_stack_value, 0, 0));
14309 have_address = 1;
14311 /* Show if we can't fill the request for an address. */
14312 if (want_address && !have_address)
14314 expansion_failed (loc, NULL_RTX,
14315 "Want address and only have value");
14316 return 0;
14319 gcc_assert (!ret || !list_ret);
14321 /* If we've got an address and don't want one, dereference. */
14322 if (!want_address && have_address)
14324 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14326 if (size > DWARF2_ADDR_SIZE || size == -1)
14328 expansion_failed (loc, NULL_RTX,
14329 "DWARF address size mismatch");
14330 return 0;
14332 else if (size == DWARF2_ADDR_SIZE)
14333 op = DW_OP_deref;
14334 else
14335 op = DW_OP_deref_size;
14337 if (ret)
14338 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14339 else
14340 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14342 if (ret)
14343 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14345 return list_ret;
14348 /* Same as above but return only single location expression. */
14349 static dw_loc_descr_ref
14350 loc_descriptor_from_tree (tree loc, int want_address)
14352 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14353 if (!ret)
14354 return NULL;
14355 if (ret->dw_loc_next)
14357 expansion_failed (loc, NULL_RTX,
14358 "Location list where only loc descriptor needed");
14359 return NULL;
14361 return ret->expr;
14364 /* Given a value, round it up to the lowest multiple of `boundary'
14365 which is not less than the value itself. */
14367 static inline HOST_WIDE_INT
14368 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14370 return (((value + boundary - 1) / boundary) * boundary);
14373 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14374 pointer to the declared type for the relevant field variable, or return
14375 `integer_type_node' if the given node turns out to be an
14376 ERROR_MARK node. */
14378 static inline tree
14379 field_type (const_tree decl)
14381 tree type;
14383 if (TREE_CODE (decl) == ERROR_MARK)
14384 return integer_type_node;
14386 type = DECL_BIT_FIELD_TYPE (decl);
14387 if (type == NULL_TREE)
14388 type = TREE_TYPE (decl);
14390 return type;
14393 /* Given a pointer to a tree node, return the alignment in bits for
14394 it, or else return BITS_PER_WORD if the node actually turns out to
14395 be an ERROR_MARK node. */
14397 static inline unsigned
14398 simple_type_align_in_bits (const_tree type)
14400 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14403 static inline unsigned
14404 simple_decl_align_in_bits (const_tree decl)
14406 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14409 /* Return the result of rounding T up to ALIGN. */
14411 static inline double_int
14412 round_up_to_align (double_int t, unsigned int align)
14414 double_int alignd = double_int::from_uhwi (align);
14415 t += alignd;
14416 t += double_int_minus_one;
14417 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14418 t *= alignd;
14419 return t;
14422 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14423 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14424 or return 0 if we are unable to determine what that offset is, either
14425 because the argument turns out to be a pointer to an ERROR_MARK node, or
14426 because the offset is actually variable. (We can't handle the latter case
14427 just yet). */
14429 static HOST_WIDE_INT
14430 field_byte_offset (const_tree decl)
14432 double_int object_offset_in_bits;
14433 double_int object_offset_in_bytes;
14434 double_int bitpos_int;
14436 if (TREE_CODE (decl) == ERROR_MARK)
14437 return 0;
14439 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14441 /* We cannot yet cope with fields whose positions are variable, so
14442 for now, when we see such things, we simply return 0. Someday, we may
14443 be able to handle such cases, but it will be damn difficult. */
14444 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14445 return 0;
14447 bitpos_int = tree_to_double_int (bit_position (decl));
14449 #ifdef PCC_BITFIELD_TYPE_MATTERS
14450 if (PCC_BITFIELD_TYPE_MATTERS)
14452 tree type;
14453 tree field_size_tree;
14454 double_int deepest_bitpos;
14455 double_int field_size_in_bits;
14456 unsigned int type_align_in_bits;
14457 unsigned int decl_align_in_bits;
14458 double_int type_size_in_bits;
14460 type = field_type (decl);
14461 type_size_in_bits = double_int_type_size_in_bits (type);
14462 type_align_in_bits = simple_type_align_in_bits (type);
14464 field_size_tree = DECL_SIZE (decl);
14466 /* The size could be unspecified if there was an error, or for
14467 a flexible array member. */
14468 if (!field_size_tree)
14469 field_size_tree = bitsize_zero_node;
14471 /* If the size of the field is not constant, use the type size. */
14472 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14473 field_size_in_bits = tree_to_double_int (field_size_tree);
14474 else
14475 field_size_in_bits = type_size_in_bits;
14477 decl_align_in_bits = simple_decl_align_in_bits (decl);
14479 /* The GCC front-end doesn't make any attempt to keep track of the
14480 starting bit offset (relative to the start of the containing
14481 structure type) of the hypothetical "containing object" for a
14482 bit-field. Thus, when computing the byte offset value for the
14483 start of the "containing object" of a bit-field, we must deduce
14484 this information on our own. This can be rather tricky to do in
14485 some cases. For example, handling the following structure type
14486 definition when compiling for an i386/i486 target (which only
14487 aligns long long's to 32-bit boundaries) can be very tricky:
14489 struct S { int field1; long long field2:31; };
14491 Fortunately, there is a simple rule-of-thumb which can be used
14492 in such cases. When compiling for an i386/i486, GCC will
14493 allocate 8 bytes for the structure shown above. It decides to
14494 do this based upon one simple rule for bit-field allocation.
14495 GCC allocates each "containing object" for each bit-field at
14496 the first (i.e. lowest addressed) legitimate alignment boundary
14497 (based upon the required minimum alignment for the declared
14498 type of the field) which it can possibly use, subject to the
14499 condition that there is still enough available space remaining
14500 in the containing object (when allocated at the selected point)
14501 to fully accommodate all of the bits of the bit-field itself.
14503 This simple rule makes it obvious why GCC allocates 8 bytes for
14504 each object of the structure type shown above. When looking
14505 for a place to allocate the "containing object" for `field2',
14506 the compiler simply tries to allocate a 64-bit "containing
14507 object" at each successive 32-bit boundary (starting at zero)
14508 until it finds a place to allocate that 64- bit field such that
14509 at least 31 contiguous (and previously unallocated) bits remain
14510 within that selected 64 bit field. (As it turns out, for the
14511 example above, the compiler finds it is OK to allocate the
14512 "containing object" 64-bit field at bit-offset zero within the
14513 structure type.)
14515 Here we attempt to work backwards from the limited set of facts
14516 we're given, and we try to deduce from those facts, where GCC
14517 must have believed that the containing object started (within
14518 the structure type). The value we deduce is then used (by the
14519 callers of this routine) to generate DW_AT_location and
14520 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14521 the case of DW_AT_location, regular fields as well). */
14523 /* Figure out the bit-distance from the start of the structure to
14524 the "deepest" bit of the bit-field. */
14525 deepest_bitpos = bitpos_int + field_size_in_bits;
14527 /* This is the tricky part. Use some fancy footwork to deduce
14528 where the lowest addressed bit of the containing object must
14529 be. */
14530 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14532 /* Round up to type_align by default. This works best for
14533 bitfields. */
14534 object_offset_in_bits
14535 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14537 if (object_offset_in_bits.ugt (bitpos_int))
14539 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14541 /* Round up to decl_align instead. */
14542 object_offset_in_bits
14543 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14546 else
14547 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14548 object_offset_in_bits = bitpos_int;
14550 object_offset_in_bytes
14551 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14552 true, TRUNC_DIV_EXPR);
14553 return object_offset_in_bytes.to_shwi ();
14556 /* The following routines define various Dwarf attributes and any data
14557 associated with them. */
14559 /* Add a location description attribute value to a DIE.
14561 This emits location attributes suitable for whole variables and
14562 whole parameters. Note that the location attributes for struct fields are
14563 generated by the routine `data_member_location_attribute' below. */
14565 static inline void
14566 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14567 dw_loc_list_ref descr)
14569 if (descr == 0)
14570 return;
14571 if (single_element_loc_list_p (descr))
14572 add_AT_loc (die, attr_kind, descr->expr);
14573 else
14574 add_AT_loc_list (die, attr_kind, descr);
14577 /* Add DW_AT_accessibility attribute to DIE if needed. */
14579 static void
14580 add_accessibility_attribute (dw_die_ref die, tree decl)
14582 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14583 children, otherwise the default is DW_ACCESS_public. In DWARF2
14584 the default has always been DW_ACCESS_public. */
14585 if (TREE_PROTECTED (decl))
14586 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14587 else if (TREE_PRIVATE (decl))
14589 if (dwarf_version == 2
14590 || die->die_parent == NULL
14591 || die->die_parent->die_tag != DW_TAG_class_type)
14592 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14594 else if (dwarf_version > 2
14595 && die->die_parent
14596 && die->die_parent->die_tag == DW_TAG_class_type)
14597 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14600 /* Attach the specialized form of location attribute used for data members of
14601 struct and union types. In the special case of a FIELD_DECL node which
14602 represents a bit-field, the "offset" part of this special location
14603 descriptor must indicate the distance in bytes from the lowest-addressed
14604 byte of the containing struct or union type to the lowest-addressed byte of
14605 the "containing object" for the bit-field. (See the `field_byte_offset'
14606 function above).
14608 For any given bit-field, the "containing object" is a hypothetical object
14609 (of some integral or enum type) within which the given bit-field lives. The
14610 type of this hypothetical "containing object" is always the same as the
14611 declared type of the individual bit-field itself (for GCC anyway... the
14612 DWARF spec doesn't actually mandate this). Note that it is the size (in
14613 bytes) of the hypothetical "containing object" which will be given in the
14614 DW_AT_byte_size attribute for this bit-field. (See the
14615 `byte_size_attribute' function below.) It is also used when calculating the
14616 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14617 function below.) */
14619 static void
14620 add_data_member_location_attribute (dw_die_ref die, tree decl)
14622 HOST_WIDE_INT offset;
14623 dw_loc_descr_ref loc_descr = 0;
14625 if (TREE_CODE (decl) == TREE_BINFO)
14627 /* We're working on the TAG_inheritance for a base class. */
14628 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14630 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14631 aren't at a fixed offset from all (sub)objects of the same
14632 type. We need to extract the appropriate offset from our
14633 vtable. The following dwarf expression means
14635 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14637 This is specific to the V3 ABI, of course. */
14639 dw_loc_descr_ref tmp;
14641 /* Make a copy of the object address. */
14642 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14643 add_loc_descr (&loc_descr, tmp);
14645 /* Extract the vtable address. */
14646 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14647 add_loc_descr (&loc_descr, tmp);
14649 /* Calculate the address of the offset. */
14650 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14651 gcc_assert (offset < 0);
14653 tmp = int_loc_descriptor (-offset);
14654 add_loc_descr (&loc_descr, tmp);
14655 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14656 add_loc_descr (&loc_descr, tmp);
14658 /* Extract the offset. */
14659 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14660 add_loc_descr (&loc_descr, tmp);
14662 /* Add it to the object address. */
14663 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14664 add_loc_descr (&loc_descr, tmp);
14666 else
14667 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14669 else
14670 offset = field_byte_offset (decl);
14672 if (! loc_descr)
14674 if (dwarf_version > 2)
14676 /* Don't need to output a location expression, just the constant. */
14677 if (offset < 0)
14678 add_AT_int (die, DW_AT_data_member_location, offset);
14679 else
14680 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14681 return;
14683 else
14685 enum dwarf_location_atom op;
14687 /* The DWARF2 standard says that we should assume that the structure
14688 address is already on the stack, so we can specify a structure
14689 field address by using DW_OP_plus_uconst. */
14690 op = DW_OP_plus_uconst;
14691 loc_descr = new_loc_descr (op, offset, 0);
14695 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14698 /* Writes integer values to dw_vec_const array. */
14700 static void
14701 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14703 while (size != 0)
14705 *dest++ = val & 0xff;
14706 val >>= 8;
14707 --size;
14711 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14713 static HOST_WIDE_INT
14714 extract_int (const unsigned char *src, unsigned int size)
14716 HOST_WIDE_INT val = 0;
14718 src += size;
14719 while (size != 0)
14721 val <<= 8;
14722 val |= *--src & 0xff;
14723 --size;
14725 return val;
14728 /* Writes double_int values to dw_vec_const array. */
14730 static void
14731 insert_double (double_int val, unsigned char *dest)
14733 unsigned char *p0 = dest;
14734 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14736 if (WORDS_BIG_ENDIAN)
14738 p0 = p1;
14739 p1 = dest;
14742 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14743 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14746 /* Writes floating point values to dw_vec_const array. */
14748 static void
14749 insert_float (const_rtx rtl, unsigned char *array)
14751 REAL_VALUE_TYPE rv;
14752 long val[4];
14753 int i;
14755 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14756 real_to_target (val, &rv, GET_MODE (rtl));
14758 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14759 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14761 insert_int (val[i], 4, array);
14762 array += 4;
14766 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14767 does not have a "location" either in memory or in a register. These
14768 things can arise in GNU C when a constant is passed as an actual parameter
14769 to an inlined function. They can also arise in C++ where declared
14770 constants do not necessarily get memory "homes". */
14772 static bool
14773 add_const_value_attribute (dw_die_ref die, rtx rtl)
14775 switch (GET_CODE (rtl))
14777 case CONST_INT:
14779 HOST_WIDE_INT val = INTVAL (rtl);
14781 if (val < 0)
14782 add_AT_int (die, DW_AT_const_value, val);
14783 else
14784 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14786 return true;
14788 case CONST_DOUBLE:
14789 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14790 floating-point constant. A CONST_DOUBLE is used whenever the
14791 constant requires more than one word in order to be adequately
14792 represented. */
14794 enum machine_mode mode = GET_MODE (rtl);
14796 if (SCALAR_FLOAT_MODE_P (mode))
14798 unsigned int length = GET_MODE_SIZE (mode);
14799 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14801 insert_float (rtl, array);
14802 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14804 else
14805 add_AT_double (die, DW_AT_const_value,
14806 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14808 return true;
14810 case CONST_VECTOR:
14812 enum machine_mode mode = GET_MODE (rtl);
14813 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14814 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14815 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14816 (length * elt_size);
14817 unsigned int i;
14818 unsigned char *p;
14820 switch (GET_MODE_CLASS (mode))
14822 case MODE_VECTOR_INT:
14823 for (i = 0, p = array; i < length; i++, p += elt_size)
14825 rtx elt = CONST_VECTOR_ELT (rtl, i);
14826 double_int val = rtx_to_double_int (elt);
14828 if (elt_size <= sizeof (HOST_WIDE_INT))
14829 insert_int (val.to_shwi (), elt_size, p);
14830 else
14832 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14833 insert_double (val, p);
14836 break;
14838 case MODE_VECTOR_FLOAT:
14839 for (i = 0, p = array; i < length; i++, p += elt_size)
14841 rtx elt = CONST_VECTOR_ELT (rtl, i);
14842 insert_float (elt, p);
14844 break;
14846 default:
14847 gcc_unreachable ();
14850 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14852 return true;
14854 case CONST_STRING:
14855 if (dwarf_version >= 4 || !dwarf_strict)
14857 dw_loc_descr_ref loc_result;
14858 resolve_one_addr (&rtl, NULL);
14859 rtl_addr:
14860 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14861 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14862 add_AT_loc (die, DW_AT_location, loc_result);
14863 vec_safe_push (used_rtx_array, rtl);
14864 return true;
14866 return false;
14868 case CONST:
14869 if (CONSTANT_P (XEXP (rtl, 0)))
14870 return add_const_value_attribute (die, XEXP (rtl, 0));
14871 /* FALLTHROUGH */
14872 case SYMBOL_REF:
14873 if (!const_ok_for_output (rtl))
14874 return false;
14875 case LABEL_REF:
14876 if (dwarf_version >= 4 || !dwarf_strict)
14877 goto rtl_addr;
14878 return false;
14880 case PLUS:
14881 /* In cases where an inlined instance of an inline function is passed
14882 the address of an `auto' variable (which is local to the caller) we
14883 can get a situation where the DECL_RTL of the artificial local
14884 variable (for the inlining) which acts as a stand-in for the
14885 corresponding formal parameter (of the inline function) will look
14886 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14887 exactly a compile-time constant expression, but it isn't the address
14888 of the (artificial) local variable either. Rather, it represents the
14889 *value* which the artificial local variable always has during its
14890 lifetime. We currently have no way to represent such quasi-constant
14891 values in Dwarf, so for now we just punt and generate nothing. */
14892 return false;
14894 case HIGH:
14895 case CONST_FIXED:
14896 return false;
14898 case MEM:
14899 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14900 && MEM_READONLY_P (rtl)
14901 && GET_MODE (rtl) == BLKmode)
14903 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14904 return true;
14906 return false;
14908 default:
14909 /* No other kinds of rtx should be possible here. */
14910 gcc_unreachable ();
14912 return false;
14915 /* Determine whether the evaluation of EXPR references any variables
14916 or functions which aren't otherwise used (and therefore may not be
14917 output). */
14918 static tree
14919 reference_to_unused (tree * tp, int * walk_subtrees,
14920 void * data ATTRIBUTE_UNUSED)
14922 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14923 *walk_subtrees = 0;
14925 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14926 && ! TREE_ASM_WRITTEN (*tp))
14927 return *tp;
14928 /* ??? The C++ FE emits debug information for using decls, so
14929 putting gcc_unreachable here falls over. See PR31899. For now
14930 be conservative. */
14931 else if (!cgraph_global_info_ready
14932 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14933 return *tp;
14934 else if (TREE_CODE (*tp) == VAR_DECL)
14936 struct varpool_node *node = varpool_get_node (*tp);
14937 if (!node || !node->symbol.definition)
14938 return *tp;
14940 else if (TREE_CODE (*tp) == FUNCTION_DECL
14941 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14943 /* The call graph machinery must have finished analyzing,
14944 optimizing and gimplifying the CU by now.
14945 So if *TP has no call graph node associated
14946 to it, it means *TP will not be emitted. */
14947 if (!cgraph_get_node (*tp))
14948 return *tp;
14950 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14951 return *tp;
14953 return NULL_TREE;
14956 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14957 for use in a later add_const_value_attribute call. */
14959 static rtx
14960 rtl_for_decl_init (tree init, tree type)
14962 rtx rtl = NULL_RTX;
14964 STRIP_NOPS (init);
14966 /* If a variable is initialized with a string constant without embedded
14967 zeros, build CONST_STRING. */
14968 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14970 tree enttype = TREE_TYPE (type);
14971 tree domain = TYPE_DOMAIN (type);
14972 enum machine_mode mode = TYPE_MODE (enttype);
14974 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14975 && domain
14976 && integer_zerop (TYPE_MIN_VALUE (domain))
14977 && compare_tree_int (TYPE_MAX_VALUE (domain),
14978 TREE_STRING_LENGTH (init) - 1) == 0
14979 && ((size_t) TREE_STRING_LENGTH (init)
14980 == strlen (TREE_STRING_POINTER (init)) + 1))
14982 rtl = gen_rtx_CONST_STRING (VOIDmode,
14983 ggc_strdup (TREE_STRING_POINTER (init)));
14984 rtl = gen_rtx_MEM (BLKmode, rtl);
14985 MEM_READONLY_P (rtl) = 1;
14988 /* Other aggregates, and complex values, could be represented using
14989 CONCAT: FIXME! */
14990 else if (AGGREGATE_TYPE_P (type)
14991 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14992 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14993 || TREE_CODE (type) == COMPLEX_TYPE)
14995 /* Vectors only work if their mode is supported by the target.
14996 FIXME: generic vectors ought to work too. */
14997 else if (TREE_CODE (type) == VECTOR_TYPE
14998 && !VECTOR_MODE_P (TYPE_MODE (type)))
15000 /* If the initializer is something that we know will expand into an
15001 immediate RTL constant, expand it now. We must be careful not to
15002 reference variables which won't be output. */
15003 else if (initializer_constant_valid_p (init, type)
15004 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15006 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15007 possible. */
15008 if (TREE_CODE (type) == VECTOR_TYPE)
15009 switch (TREE_CODE (init))
15011 case VECTOR_CST:
15012 break;
15013 case CONSTRUCTOR:
15014 if (TREE_CONSTANT (init))
15016 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15017 bool constant_p = true;
15018 tree value;
15019 unsigned HOST_WIDE_INT ix;
15021 /* Even when ctor is constant, it might contain non-*_CST
15022 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15023 belong into VECTOR_CST nodes. */
15024 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15025 if (!CONSTANT_CLASS_P (value))
15027 constant_p = false;
15028 break;
15031 if (constant_p)
15033 init = build_vector_from_ctor (type, elts);
15034 break;
15037 /* FALLTHRU */
15039 default:
15040 return NULL;
15043 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15045 /* If expand_expr returns a MEM, it wasn't immediate. */
15046 gcc_assert (!rtl || !MEM_P (rtl));
15049 return rtl;
15052 /* Generate RTL for the variable DECL to represent its location. */
15054 static rtx
15055 rtl_for_decl_location (tree decl)
15057 rtx rtl;
15059 /* Here we have to decide where we are going to say the parameter "lives"
15060 (as far as the debugger is concerned). We only have a couple of
15061 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15063 DECL_RTL normally indicates where the parameter lives during most of the
15064 activation of the function. If optimization is enabled however, this
15065 could be either NULL or else a pseudo-reg. Both of those cases indicate
15066 that the parameter doesn't really live anywhere (as far as the code
15067 generation parts of GCC are concerned) during most of the function's
15068 activation. That will happen (for example) if the parameter is never
15069 referenced within the function.
15071 We could just generate a location descriptor here for all non-NULL
15072 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15073 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15074 where DECL_RTL is NULL or is a pseudo-reg.
15076 Note however that we can only get away with using DECL_INCOMING_RTL as
15077 a backup substitute for DECL_RTL in certain limited cases. In cases
15078 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15079 we can be sure that the parameter was passed using the same type as it is
15080 declared to have within the function, and that its DECL_INCOMING_RTL
15081 points us to a place where a value of that type is passed.
15083 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15084 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15085 because in these cases DECL_INCOMING_RTL points us to a value of some
15086 type which is *different* from the type of the parameter itself. Thus,
15087 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15088 such cases, the debugger would end up (for example) trying to fetch a
15089 `float' from a place which actually contains the first part of a
15090 `double'. That would lead to really incorrect and confusing
15091 output at debug-time.
15093 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15094 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15095 are a couple of exceptions however. On little-endian machines we can
15096 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15097 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15098 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15099 when (on a little-endian machine) a non-prototyped function has a
15100 parameter declared to be of type `short' or `char'. In such cases,
15101 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15102 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15103 passed `int' value. If the debugger then uses that address to fetch
15104 a `short' or a `char' (on a little-endian machine) the result will be
15105 the correct data, so we allow for such exceptional cases below.
15107 Note that our goal here is to describe the place where the given formal
15108 parameter lives during most of the function's activation (i.e. between the
15109 end of the prologue and the start of the epilogue). We'll do that as best
15110 as we can. Note however that if the given formal parameter is modified
15111 sometime during the execution of the function, then a stack backtrace (at
15112 debug-time) will show the function as having been called with the *new*
15113 value rather than the value which was originally passed in. This happens
15114 rarely enough that it is not a major problem, but it *is* a problem, and
15115 I'd like to fix it.
15117 A future version of dwarf2out.c may generate two additional attributes for
15118 any given DW_TAG_formal_parameter DIE which will describe the "passed
15119 type" and the "passed location" for the given formal parameter in addition
15120 to the attributes we now generate to indicate the "declared type" and the
15121 "active location" for each parameter. This additional set of attributes
15122 could be used by debuggers for stack backtraces. Separately, note that
15123 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15124 This happens (for example) for inlined-instances of inline function formal
15125 parameters which are never referenced. This really shouldn't be
15126 happening. All PARM_DECL nodes should get valid non-NULL
15127 DECL_INCOMING_RTL values. FIXME. */
15129 /* Use DECL_RTL as the "location" unless we find something better. */
15130 rtl = DECL_RTL_IF_SET (decl);
15132 /* When generating abstract instances, ignore everything except
15133 constants, symbols living in memory, and symbols living in
15134 fixed registers. */
15135 if (! reload_completed)
15137 if (rtl
15138 && (CONSTANT_P (rtl)
15139 || (MEM_P (rtl)
15140 && CONSTANT_P (XEXP (rtl, 0)))
15141 || (REG_P (rtl)
15142 && TREE_CODE (decl) == VAR_DECL
15143 && TREE_STATIC (decl))))
15145 rtl = targetm.delegitimize_address (rtl);
15146 return rtl;
15148 rtl = NULL_RTX;
15150 else if (TREE_CODE (decl) == PARM_DECL)
15152 if (rtl == NULL_RTX
15153 || is_pseudo_reg (rtl)
15154 || (MEM_P (rtl)
15155 && is_pseudo_reg (XEXP (rtl, 0))
15156 && DECL_INCOMING_RTL (decl)
15157 && MEM_P (DECL_INCOMING_RTL (decl))
15158 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15160 tree declared_type = TREE_TYPE (decl);
15161 tree passed_type = DECL_ARG_TYPE (decl);
15162 enum machine_mode dmode = TYPE_MODE (declared_type);
15163 enum machine_mode pmode = TYPE_MODE (passed_type);
15165 /* This decl represents a formal parameter which was optimized out.
15166 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15167 all cases where (rtl == NULL_RTX) just below. */
15168 if (dmode == pmode)
15169 rtl = DECL_INCOMING_RTL (decl);
15170 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15171 && SCALAR_INT_MODE_P (dmode)
15172 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15173 && DECL_INCOMING_RTL (decl))
15175 rtx inc = DECL_INCOMING_RTL (decl);
15176 if (REG_P (inc))
15177 rtl = inc;
15178 else if (MEM_P (inc))
15180 if (BYTES_BIG_ENDIAN)
15181 rtl = adjust_address_nv (inc, dmode,
15182 GET_MODE_SIZE (pmode)
15183 - GET_MODE_SIZE (dmode));
15184 else
15185 rtl = inc;
15190 /* If the parm was passed in registers, but lives on the stack, then
15191 make a big endian correction if the mode of the type of the
15192 parameter is not the same as the mode of the rtl. */
15193 /* ??? This is the same series of checks that are made in dbxout.c before
15194 we reach the big endian correction code there. It isn't clear if all
15195 of these checks are necessary here, but keeping them all is the safe
15196 thing to do. */
15197 else if (MEM_P (rtl)
15198 && XEXP (rtl, 0) != const0_rtx
15199 && ! CONSTANT_P (XEXP (rtl, 0))
15200 /* Not passed in memory. */
15201 && !MEM_P (DECL_INCOMING_RTL (decl))
15202 /* Not passed by invisible reference. */
15203 && (!REG_P (XEXP (rtl, 0))
15204 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15205 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15206 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15207 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15208 #endif
15210 /* Big endian correction check. */
15211 && BYTES_BIG_ENDIAN
15212 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15213 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15214 < UNITS_PER_WORD))
15216 enum machine_mode addr_mode = get_address_mode (rtl);
15217 int offset = (UNITS_PER_WORD
15218 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15220 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15221 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15224 else if (TREE_CODE (decl) == VAR_DECL
15225 && rtl
15226 && MEM_P (rtl)
15227 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15228 && BYTES_BIG_ENDIAN)
15230 enum machine_mode addr_mode = get_address_mode (rtl);
15231 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15232 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15234 /* If a variable is declared "register" yet is smaller than
15235 a register, then if we store the variable to memory, it
15236 looks like we're storing a register-sized value, when in
15237 fact we are not. We need to adjust the offset of the
15238 storage location to reflect the actual value's bytes,
15239 else gdb will not be able to display it. */
15240 if (rsize > dsize)
15241 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15242 plus_constant (addr_mode, XEXP (rtl, 0),
15243 rsize - dsize));
15246 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15247 and will have been substituted directly into all expressions that use it.
15248 C does not have such a concept, but C++ and other languages do. */
15249 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15250 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15252 if (rtl)
15253 rtl = targetm.delegitimize_address (rtl);
15255 /* If we don't look past the constant pool, we risk emitting a
15256 reference to a constant pool entry that isn't referenced from
15257 code, and thus is not emitted. */
15258 if (rtl)
15259 rtl = avoid_constant_pool_reference (rtl);
15261 /* Try harder to get a rtl. If this symbol ends up not being emitted
15262 in the current CU, resolve_addr will remove the expression referencing
15263 it. */
15264 if (rtl == NULL_RTX
15265 && TREE_CODE (decl) == VAR_DECL
15266 && !DECL_EXTERNAL (decl)
15267 && TREE_STATIC (decl)
15268 && DECL_NAME (decl)
15269 && !DECL_HARD_REGISTER (decl)
15270 && DECL_MODE (decl) != VOIDmode)
15272 rtl = make_decl_rtl_for_debug (decl);
15273 if (!MEM_P (rtl)
15274 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15275 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15276 rtl = NULL_RTX;
15279 return rtl;
15282 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15283 returned. If so, the decl for the COMMON block is returned, and the
15284 value is the offset into the common block for the symbol. */
15286 static tree
15287 fortran_common (tree decl, HOST_WIDE_INT *value)
15289 tree val_expr, cvar;
15290 enum machine_mode mode;
15291 HOST_WIDE_INT bitsize, bitpos;
15292 tree offset;
15293 int unsignedp, volatilep = 0;
15295 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15296 it does not have a value (the offset into the common area), or if it
15297 is thread local (as opposed to global) then it isn't common, and shouldn't
15298 be handled as such. */
15299 if (TREE_CODE (decl) != VAR_DECL
15300 || !TREE_STATIC (decl)
15301 || !DECL_HAS_VALUE_EXPR_P (decl)
15302 || !is_fortran ())
15303 return NULL_TREE;
15305 val_expr = DECL_VALUE_EXPR (decl);
15306 if (TREE_CODE (val_expr) != COMPONENT_REF)
15307 return NULL_TREE;
15309 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15310 &mode, &unsignedp, &volatilep, true);
15312 if (cvar == NULL_TREE
15313 || TREE_CODE (cvar) != VAR_DECL
15314 || DECL_ARTIFICIAL (cvar)
15315 || !TREE_PUBLIC (cvar))
15316 return NULL_TREE;
15318 *value = 0;
15319 if (offset != NULL)
15321 if (!host_integerp (offset, 0))
15322 return NULL_TREE;
15323 *value = tree_low_cst (offset, 0);
15325 if (bitpos != 0)
15326 *value += bitpos / BITS_PER_UNIT;
15328 return cvar;
15331 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15332 data attribute for a variable or a parameter. We generate the
15333 DW_AT_const_value attribute only in those cases where the given variable
15334 or parameter does not have a true "location" either in memory or in a
15335 register. This can happen (for example) when a constant is passed as an
15336 actual argument in a call to an inline function. (It's possible that
15337 these things can crop up in other ways also.) Note that one type of
15338 constant value which can be passed into an inlined function is a constant
15339 pointer. This can happen for example if an actual argument in an inlined
15340 function call evaluates to a compile-time constant address.
15342 CACHE_P is true if it is worth caching the location list for DECL,
15343 so that future calls can reuse it rather than regenerate it from scratch.
15344 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15345 since we will need to refer to them each time the function is inlined. */
15347 static bool
15348 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15349 enum dwarf_attribute attr)
15351 rtx rtl;
15352 dw_loc_list_ref list;
15353 var_loc_list *loc_list;
15354 cached_dw_loc_list *cache;
15355 void **slot;
15357 if (TREE_CODE (decl) == ERROR_MARK)
15358 return false;
15360 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15361 || TREE_CODE (decl) == RESULT_DECL);
15363 /* Try to get some constant RTL for this decl, and use that as the value of
15364 the location. */
15366 rtl = rtl_for_decl_location (decl);
15367 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15368 && add_const_value_attribute (die, rtl))
15369 return true;
15371 /* See if we have single element location list that is equivalent to
15372 a constant value. That way we are better to use add_const_value_attribute
15373 rather than expanding constant value equivalent. */
15374 loc_list = lookup_decl_loc (decl);
15375 if (loc_list
15376 && loc_list->first
15377 && loc_list->first->next == NULL
15378 && NOTE_P (loc_list->first->loc)
15379 && NOTE_VAR_LOCATION (loc_list->first->loc)
15380 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15382 struct var_loc_node *node;
15384 node = loc_list->first;
15385 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15386 if (GET_CODE (rtl) == EXPR_LIST)
15387 rtl = XEXP (rtl, 0);
15388 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15389 && add_const_value_attribute (die, rtl))
15390 return true;
15392 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15393 list several times. See if we've already cached the contents. */
15394 list = NULL;
15395 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15396 cache_p = false;
15397 if (cache_p)
15399 cache = (cached_dw_loc_list *)
15400 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15401 if (cache)
15402 list = cache->loc_list;
15404 if (list == NULL)
15406 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15407 /* It is usually worth caching this result if the decl is from
15408 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15409 if (cache_p && list && list->dw_loc_next)
15411 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15412 DECL_UID (decl), INSERT);
15413 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15414 cache->decl_id = DECL_UID (decl);
15415 cache->loc_list = list;
15416 *slot = cache;
15419 if (list)
15421 add_AT_location_description (die, attr, list);
15422 return true;
15424 /* None of that worked, so it must not really have a location;
15425 try adding a constant value attribute from the DECL_INITIAL. */
15426 return tree_add_const_value_attribute_for_decl (die, decl);
15429 /* Add VARIABLE and DIE into deferred locations list. */
15431 static void
15432 defer_location (tree variable, dw_die_ref die)
15434 deferred_locations entry;
15435 entry.variable = variable;
15436 entry.die = die;
15437 vec_safe_push (deferred_locations_list, entry);
15440 /* Helper function for tree_add_const_value_attribute. Natively encode
15441 initializer INIT into an array. Return true if successful. */
15443 static bool
15444 native_encode_initializer (tree init, unsigned char *array, int size)
15446 tree type;
15448 if (init == NULL_TREE)
15449 return false;
15451 STRIP_NOPS (init);
15452 switch (TREE_CODE (init))
15454 case STRING_CST:
15455 type = TREE_TYPE (init);
15456 if (TREE_CODE (type) == ARRAY_TYPE)
15458 tree enttype = TREE_TYPE (type);
15459 enum machine_mode mode = TYPE_MODE (enttype);
15461 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15462 return false;
15463 if (int_size_in_bytes (type) != size)
15464 return false;
15465 if (size > TREE_STRING_LENGTH (init))
15467 memcpy (array, TREE_STRING_POINTER (init),
15468 TREE_STRING_LENGTH (init));
15469 memset (array + TREE_STRING_LENGTH (init),
15470 '\0', size - TREE_STRING_LENGTH (init));
15472 else
15473 memcpy (array, TREE_STRING_POINTER (init), size);
15474 return true;
15476 return false;
15477 case CONSTRUCTOR:
15478 type = TREE_TYPE (init);
15479 if (int_size_in_bytes (type) != size)
15480 return false;
15481 if (TREE_CODE (type) == ARRAY_TYPE)
15483 HOST_WIDE_INT min_index;
15484 unsigned HOST_WIDE_INT cnt;
15485 int curpos = 0, fieldsize;
15486 constructor_elt *ce;
15488 if (TYPE_DOMAIN (type) == NULL_TREE
15489 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15490 return false;
15492 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15493 if (fieldsize <= 0)
15494 return false;
15496 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15497 memset (array, '\0', size);
15498 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15500 tree val = ce->value;
15501 tree index = ce->index;
15502 int pos = curpos;
15503 if (index && TREE_CODE (index) == RANGE_EXPR)
15504 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15505 * fieldsize;
15506 else if (index)
15507 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15509 if (val)
15511 STRIP_NOPS (val);
15512 if (!native_encode_initializer (val, array + pos, fieldsize))
15513 return false;
15515 curpos = pos + fieldsize;
15516 if (index && TREE_CODE (index) == RANGE_EXPR)
15518 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15519 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15520 while (count-- > 0)
15522 if (val)
15523 memcpy (array + curpos, array + pos, fieldsize);
15524 curpos += fieldsize;
15527 gcc_assert (curpos <= size);
15529 return true;
15531 else if (TREE_CODE (type) == RECORD_TYPE
15532 || TREE_CODE (type) == UNION_TYPE)
15534 tree field = NULL_TREE;
15535 unsigned HOST_WIDE_INT cnt;
15536 constructor_elt *ce;
15538 if (int_size_in_bytes (type) != size)
15539 return false;
15541 if (TREE_CODE (type) == RECORD_TYPE)
15542 field = TYPE_FIELDS (type);
15544 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15546 tree val = ce->value;
15547 int pos, fieldsize;
15549 if (ce->index != 0)
15550 field = ce->index;
15552 if (val)
15553 STRIP_NOPS (val);
15555 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15556 return false;
15558 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15559 && TYPE_DOMAIN (TREE_TYPE (field))
15560 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15561 return false;
15562 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15563 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15564 return false;
15565 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15566 pos = int_byte_position (field);
15567 gcc_assert (pos + fieldsize <= size);
15568 if (val
15569 && !native_encode_initializer (val, array + pos, fieldsize))
15570 return false;
15572 return true;
15574 return false;
15575 case VIEW_CONVERT_EXPR:
15576 case NON_LVALUE_EXPR:
15577 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15578 default:
15579 return native_encode_expr (init, array, size) == size;
15583 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15584 attribute is the const value T. */
15586 static bool
15587 tree_add_const_value_attribute (dw_die_ref die, tree t)
15589 tree init;
15590 tree type = TREE_TYPE (t);
15591 rtx rtl;
15593 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15594 return false;
15596 init = t;
15597 gcc_assert (!DECL_P (init));
15599 rtl = rtl_for_decl_init (init, type);
15600 if (rtl)
15601 return add_const_value_attribute (die, rtl);
15602 /* If the host and target are sane, try harder. */
15603 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15604 && initializer_constant_valid_p (init, type))
15606 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15607 if (size > 0 && (int) size == size)
15609 unsigned char *array = (unsigned char *)
15610 ggc_alloc_cleared_atomic (size);
15612 if (native_encode_initializer (init, array, size))
15614 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15615 return true;
15617 ggc_free (array);
15620 return false;
15623 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15624 attribute is the const value of T, where T is an integral constant
15625 variable with static storage duration
15626 (so it can't be a PARM_DECL or a RESULT_DECL). */
15628 static bool
15629 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15632 if (!decl
15633 || (TREE_CODE (decl) != VAR_DECL
15634 && TREE_CODE (decl) != CONST_DECL)
15635 || (TREE_CODE (decl) == VAR_DECL
15636 && !TREE_STATIC (decl)))
15637 return false;
15639 if (TREE_READONLY (decl)
15640 && ! TREE_THIS_VOLATILE (decl)
15641 && DECL_INITIAL (decl))
15642 /* OK */;
15643 else
15644 return false;
15646 /* Don't add DW_AT_const_value if abstract origin already has one. */
15647 if (get_AT (var_die, DW_AT_const_value))
15648 return false;
15650 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15653 /* Convert the CFI instructions for the current function into a
15654 location list. This is used for DW_AT_frame_base when we targeting
15655 a dwarf2 consumer that does not support the dwarf3
15656 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15657 expressions. */
15659 static dw_loc_list_ref
15660 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15662 int ix;
15663 dw_fde_ref fde;
15664 dw_loc_list_ref list, *list_tail;
15665 dw_cfi_ref cfi;
15666 dw_cfa_location last_cfa, next_cfa;
15667 const char *start_label, *last_label, *section;
15668 dw_cfa_location remember;
15670 fde = cfun->fde;
15671 gcc_assert (fde != NULL);
15673 section = secname_for_decl (current_function_decl);
15674 list_tail = &list;
15675 list = NULL;
15677 memset (&next_cfa, 0, sizeof (next_cfa));
15678 next_cfa.reg = INVALID_REGNUM;
15679 remember = next_cfa;
15681 start_label = fde->dw_fde_begin;
15683 /* ??? Bald assumption that the CIE opcode list does not contain
15684 advance opcodes. */
15685 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15686 lookup_cfa_1 (cfi, &next_cfa, &remember);
15688 last_cfa = next_cfa;
15689 last_label = start_label;
15691 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15693 /* If the first partition contained no CFI adjustments, the
15694 CIE opcodes apply to the whole first partition. */
15695 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15696 fde->dw_fde_begin, fde->dw_fde_end, section);
15697 list_tail =&(*list_tail)->dw_loc_next;
15698 start_label = last_label = fde->dw_fde_second_begin;
15701 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15703 switch (cfi->dw_cfi_opc)
15705 case DW_CFA_set_loc:
15706 case DW_CFA_advance_loc1:
15707 case DW_CFA_advance_loc2:
15708 case DW_CFA_advance_loc4:
15709 if (!cfa_equal_p (&last_cfa, &next_cfa))
15711 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15712 start_label, last_label, section);
15714 list_tail = &(*list_tail)->dw_loc_next;
15715 last_cfa = next_cfa;
15716 start_label = last_label;
15718 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15719 break;
15721 case DW_CFA_advance_loc:
15722 /* The encoding is complex enough that we should never emit this. */
15723 gcc_unreachable ();
15725 default:
15726 lookup_cfa_1 (cfi, &next_cfa, &remember);
15727 break;
15729 if (ix + 1 == fde->dw_fde_switch_cfi_index)
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);
15736 list_tail = &(*list_tail)->dw_loc_next;
15737 last_cfa = next_cfa;
15738 start_label = last_label;
15740 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15741 start_label, fde->dw_fde_end, section);
15742 list_tail = &(*list_tail)->dw_loc_next;
15743 start_label = last_label = fde->dw_fde_second_begin;
15747 if (!cfa_equal_p (&last_cfa, &next_cfa))
15749 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15750 start_label, last_label, section);
15751 list_tail = &(*list_tail)->dw_loc_next;
15752 start_label = last_label;
15755 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15756 start_label,
15757 fde->dw_fde_second_begin
15758 ? fde->dw_fde_second_end : fde->dw_fde_end,
15759 section);
15761 if (list && list->dw_loc_next)
15762 gen_llsym (list);
15764 return list;
15767 /* Compute a displacement from the "steady-state frame pointer" to the
15768 frame base (often the same as the CFA), and store it in
15769 frame_pointer_fb_offset. OFFSET is added to the displacement
15770 before the latter is negated. */
15772 static void
15773 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15775 rtx reg, elim;
15777 #ifdef FRAME_POINTER_CFA_OFFSET
15778 reg = frame_pointer_rtx;
15779 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15780 #else
15781 reg = arg_pointer_rtx;
15782 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15783 #endif
15785 elim = (ira_use_lra_p
15786 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15787 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15788 if (GET_CODE (elim) == PLUS)
15790 offset += INTVAL (XEXP (elim, 1));
15791 elim = XEXP (elim, 0);
15794 frame_pointer_fb_offset = -offset;
15796 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15797 in which to eliminate. This is because it's stack pointer isn't
15798 directly accessible as a register within the ISA. To work around
15799 this, assume that while we cannot provide a proper value for
15800 frame_pointer_fb_offset, we won't need one either. */
15801 frame_pointer_fb_offset_valid
15802 = ((SUPPORTS_STACK_ALIGNMENT
15803 && (elim == hard_frame_pointer_rtx
15804 || elim == stack_pointer_rtx))
15805 || elim == (frame_pointer_needed
15806 ? hard_frame_pointer_rtx
15807 : stack_pointer_rtx));
15810 /* Generate a DW_AT_name attribute given some string value to be included as
15811 the value of the attribute. */
15813 static void
15814 add_name_attribute (dw_die_ref die, const char *name_string)
15816 if (name_string != NULL && *name_string != 0)
15818 if (demangle_name_func)
15819 name_string = (*demangle_name_func) (name_string);
15821 add_AT_string (die, DW_AT_name, name_string);
15825 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15826 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15827 of TYPE accordingly.
15829 ??? This is a temporary measure until after we're able to generate
15830 regular DWARF for the complex Ada type system. */
15832 static void
15833 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15834 dw_die_ref context_die)
15836 tree dtype;
15837 dw_die_ref dtype_die;
15839 if (!lang_hooks.types.descriptive_type)
15840 return;
15842 dtype = lang_hooks.types.descriptive_type (type);
15843 if (!dtype)
15844 return;
15846 dtype_die = lookup_type_die (dtype);
15847 if (!dtype_die)
15849 gen_type_die (dtype, context_die);
15850 dtype_die = lookup_type_die (dtype);
15851 gcc_assert (dtype_die);
15854 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15857 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
15859 static const char *
15860 comp_dir_string (void)
15862 const char *wd;
15863 char *wd1;
15864 static const char *cached_wd = NULL;
15866 if (cached_wd != NULL)
15867 return cached_wd;
15869 wd = get_src_pwd ();
15870 if (wd == NULL)
15871 return NULL;
15873 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15875 int wdlen;
15877 wdlen = strlen (wd);
15878 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15879 strcpy (wd1, wd);
15880 wd1 [wdlen] = DIR_SEPARATOR;
15881 wd1 [wdlen + 1] = 0;
15882 wd = wd1;
15885 cached_wd = remap_debug_filename (wd);
15886 return cached_wd;
15889 /* Generate a DW_AT_comp_dir attribute for DIE. */
15891 static void
15892 add_comp_dir_attribute (dw_die_ref die)
15894 const char * wd = comp_dir_string ();
15895 if (wd != NULL)
15896 add_AT_string (die, DW_AT_comp_dir, wd);
15899 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15900 default. */
15902 static int
15903 lower_bound_default (void)
15905 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15907 case DW_LANG_C:
15908 case DW_LANG_C89:
15909 case DW_LANG_C99:
15910 case DW_LANG_C_plus_plus:
15911 case DW_LANG_ObjC:
15912 case DW_LANG_ObjC_plus_plus:
15913 case DW_LANG_Java:
15914 return 0;
15915 case DW_LANG_Fortran77:
15916 case DW_LANG_Fortran90:
15917 case DW_LANG_Fortran95:
15918 return 1;
15919 case DW_LANG_UPC:
15920 case DW_LANG_D:
15921 case DW_LANG_Python:
15922 return dwarf_version >= 4 ? 0 : -1;
15923 case DW_LANG_Ada95:
15924 case DW_LANG_Ada83:
15925 case DW_LANG_Cobol74:
15926 case DW_LANG_Cobol85:
15927 case DW_LANG_Pascal83:
15928 case DW_LANG_Modula2:
15929 case DW_LANG_PLI:
15930 return dwarf_version >= 4 ? 1 : -1;
15931 default:
15932 return -1;
15936 /* Given a tree node describing an array bound (either lower or upper) output
15937 a representation for that bound. */
15939 static void
15940 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15942 switch (TREE_CODE (bound))
15944 case ERROR_MARK:
15945 return;
15947 /* All fixed-bounds are represented by INTEGER_CST nodes. */
15948 case INTEGER_CST:
15950 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15951 int dflt;
15953 /* Use the default if possible. */
15954 if (bound_attr == DW_AT_lower_bound
15955 && host_integerp (bound, 0)
15956 && (dflt = lower_bound_default ()) != -1
15957 && tree_low_cst (bound, 0) == dflt)
15960 /* Otherwise represent the bound as an unsigned value with the
15961 precision of its type. The precision and signedness of the
15962 type will be necessary to re-interpret it unambiguously. */
15963 else if (prec < HOST_BITS_PER_WIDE_INT)
15965 unsigned HOST_WIDE_INT mask
15966 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15967 add_AT_unsigned (subrange_die, bound_attr,
15968 TREE_INT_CST_LOW (bound) & mask);
15970 else if (prec == HOST_BITS_PER_WIDE_INT
15971 || TREE_INT_CST_HIGH (bound) == 0)
15972 add_AT_unsigned (subrange_die, bound_attr,
15973 TREE_INT_CST_LOW (bound));
15974 else
15975 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15976 TREE_INT_CST_LOW (bound));
15978 break;
15980 CASE_CONVERT:
15981 case VIEW_CONVERT_EXPR:
15982 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15983 break;
15985 case SAVE_EXPR:
15986 break;
15988 case VAR_DECL:
15989 case PARM_DECL:
15990 case RESULT_DECL:
15992 dw_die_ref decl_die = lookup_decl_die (bound);
15994 /* ??? Can this happen, or should the variable have been bound
15995 first? Probably it can, since I imagine that we try to create
15996 the types of parameters in the order in which they exist in
15997 the list, and won't have created a forward reference to a
15998 later parameter. */
15999 if (decl_die != NULL)
16001 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16002 break;
16005 /* FALLTHRU */
16007 default:
16009 /* Otherwise try to create a stack operation procedure to
16010 evaluate the value of the array bound. */
16012 dw_die_ref ctx, decl_die;
16013 dw_loc_list_ref list;
16015 list = loc_list_from_tree (bound, 2);
16016 if (list == NULL || single_element_loc_list_p (list))
16018 /* If DW_AT_*bound is not a reference nor constant, it is
16019 a DWARF expression rather than location description.
16020 For that loc_list_from_tree (bound, 0) is needed.
16021 If that fails to give a single element list,
16022 fall back to outputting this as a reference anyway. */
16023 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16024 if (list2 && single_element_loc_list_p (list2))
16026 add_AT_loc (subrange_die, bound_attr, list2->expr);
16027 break;
16030 if (list == NULL)
16031 break;
16033 if (current_function_decl == 0)
16034 ctx = comp_unit_die ();
16035 else
16036 ctx = lookup_decl_die (current_function_decl);
16038 decl_die = new_die (DW_TAG_variable, ctx, bound);
16039 add_AT_flag (decl_die, DW_AT_artificial, 1);
16040 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16041 add_AT_location_description (decl_die, DW_AT_location, list);
16042 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16043 break;
16048 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16049 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16050 Note that the block of subscript information for an array type also
16051 includes information about the element type of the given array type. */
16053 static void
16054 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16056 unsigned dimension_number;
16057 tree lower, upper;
16058 dw_die_ref subrange_die;
16060 for (dimension_number = 0;
16061 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16062 type = TREE_TYPE (type), dimension_number++)
16064 tree domain = TYPE_DOMAIN (type);
16066 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16067 break;
16069 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16070 and (in GNU C only) variable bounds. Handle all three forms
16071 here. */
16072 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16073 if (domain)
16075 /* We have an array type with specified bounds. */
16076 lower = TYPE_MIN_VALUE (domain);
16077 upper = TYPE_MAX_VALUE (domain);
16079 /* Define the index type. */
16080 if (TREE_TYPE (domain))
16082 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16083 TREE_TYPE field. We can't emit debug info for this
16084 because it is an unnamed integral type. */
16085 if (TREE_CODE (domain) == INTEGER_TYPE
16086 && TYPE_NAME (domain) == NULL_TREE
16087 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16088 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16090 else
16091 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16092 type_die);
16095 /* ??? If upper is NULL, the array has unspecified length,
16096 but it does have a lower bound. This happens with Fortran
16097 dimension arr(N:*)
16098 Since the debugger is definitely going to need to know N
16099 to produce useful results, go ahead and output the lower
16100 bound solo, and hope the debugger can cope. */
16102 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16103 if (upper)
16104 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16107 /* Otherwise we have an array type with an unspecified length. The
16108 DWARF-2 spec does not say how to handle this; let's just leave out the
16109 bounds. */
16113 static void
16114 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16116 dw_die_ref decl_die;
16117 unsigned size;
16119 switch (TREE_CODE (tree_node))
16121 case ERROR_MARK:
16122 size = 0;
16123 break;
16124 case ENUMERAL_TYPE:
16125 case RECORD_TYPE:
16126 case UNION_TYPE:
16127 case QUAL_UNION_TYPE:
16128 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16129 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16131 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16132 return;
16134 size = int_size_in_bytes (tree_node);
16135 break;
16136 case FIELD_DECL:
16137 /* For a data member of a struct or union, the DW_AT_byte_size is
16138 generally given as the number of bytes normally allocated for an
16139 object of the *declared* type of the member itself. This is true
16140 even for bit-fields. */
16141 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16142 break;
16143 default:
16144 gcc_unreachable ();
16147 /* Note that `size' might be -1 when we get to this point. If it is, that
16148 indicates that the byte size of the entity in question is variable. We
16149 have no good way of expressing this fact in Dwarf at the present time,
16150 so just let the -1 pass on through. */
16151 add_AT_unsigned (die, DW_AT_byte_size, size);
16154 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16155 which specifies the distance in bits from the highest order bit of the
16156 "containing object" for the bit-field to the highest order bit of the
16157 bit-field itself.
16159 For any given bit-field, the "containing object" is a hypothetical object
16160 (of some integral or enum type) within which the given bit-field lives. The
16161 type of this hypothetical "containing object" is always the same as the
16162 declared type of the individual bit-field itself. The determination of the
16163 exact location of the "containing object" for a bit-field is rather
16164 complicated. It's handled by the `field_byte_offset' function (above).
16166 Note that it is the size (in bytes) of the hypothetical "containing object"
16167 which will be given in the DW_AT_byte_size attribute for this bit-field.
16168 (See `byte_size_attribute' above). */
16170 static inline void
16171 add_bit_offset_attribute (dw_die_ref die, tree decl)
16173 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16174 tree type = DECL_BIT_FIELD_TYPE (decl);
16175 HOST_WIDE_INT bitpos_int;
16176 HOST_WIDE_INT highest_order_object_bit_offset;
16177 HOST_WIDE_INT highest_order_field_bit_offset;
16178 HOST_WIDE_INT bit_offset;
16180 /* Must be a field and a bit field. */
16181 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16183 /* We can't yet handle bit-fields whose offsets are variable, so if we
16184 encounter such things, just return without generating any attribute
16185 whatsoever. Likewise for variable or too large size. */
16186 if (! host_integerp (bit_position (decl), 0)
16187 || ! host_integerp (DECL_SIZE (decl), 1))
16188 return;
16190 bitpos_int = int_bit_position (decl);
16192 /* Note that the bit offset is always the distance (in bits) from the
16193 highest-order bit of the "containing object" to the highest-order bit of
16194 the bit-field itself. Since the "high-order end" of any object or field
16195 is different on big-endian and little-endian machines, the computation
16196 below must take account of these differences. */
16197 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16198 highest_order_field_bit_offset = bitpos_int;
16200 if (! BYTES_BIG_ENDIAN)
16202 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16203 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16206 bit_offset
16207 = (! BYTES_BIG_ENDIAN
16208 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16209 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16211 if (bit_offset < 0)
16212 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16213 else
16214 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16217 /* For a FIELD_DECL node which represents a bit field, output an attribute
16218 which specifies the length in bits of the given field. */
16220 static inline void
16221 add_bit_size_attribute (dw_die_ref die, tree decl)
16223 /* Must be a field and a bit field. */
16224 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16225 && DECL_BIT_FIELD_TYPE (decl));
16227 if (host_integerp (DECL_SIZE (decl), 1))
16228 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16231 /* If the compiled language is ANSI C, then add a 'prototyped'
16232 attribute, if arg types are given for the parameters of a function. */
16234 static inline void
16235 add_prototyped_attribute (dw_die_ref die, tree func_type)
16237 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16238 && prototype_p (func_type))
16239 add_AT_flag (die, DW_AT_prototyped, 1);
16242 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16243 by looking in either the type declaration or object declaration
16244 equate table. */
16246 static inline dw_die_ref
16247 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16249 dw_die_ref origin_die = NULL;
16251 if (TREE_CODE (origin) != FUNCTION_DECL)
16253 /* We may have gotten separated from the block for the inlined
16254 function, if we're in an exception handler or some such; make
16255 sure that the abstract function has been written out.
16257 Doing this for nested functions is wrong, however; functions are
16258 distinct units, and our context might not even be inline. */
16259 tree fn = origin;
16261 if (TYPE_P (fn))
16262 fn = TYPE_STUB_DECL (fn);
16264 fn = decl_function_context (fn);
16265 if (fn)
16266 dwarf2out_abstract_function (fn);
16269 if (DECL_P (origin))
16270 origin_die = lookup_decl_die (origin);
16271 else if (TYPE_P (origin))
16272 origin_die = lookup_type_die (origin);
16274 /* XXX: Functions that are never lowered don't always have correct block
16275 trees (in the case of java, they simply have no block tree, in some other
16276 languages). For these functions, there is nothing we can really do to
16277 output correct debug info for inlined functions in all cases. Rather
16278 than die, we'll just produce deficient debug info now, in that we will
16279 have variables without a proper abstract origin. In the future, when all
16280 functions are lowered, we should re-add a gcc_assert (origin_die)
16281 here. */
16283 if (origin_die)
16284 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16285 return origin_die;
16288 /* We do not currently support the pure_virtual attribute. */
16290 static inline void
16291 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16293 if (DECL_VINDEX (func_decl))
16295 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16297 if (host_integerp (DECL_VINDEX (func_decl), 0))
16298 add_AT_loc (die, DW_AT_vtable_elem_location,
16299 new_loc_descr (DW_OP_constu,
16300 tree_low_cst (DECL_VINDEX (func_decl), 0),
16301 0));
16303 /* GNU extension: Record what type this method came from originally. */
16304 if (debug_info_level > DINFO_LEVEL_TERSE
16305 && DECL_CONTEXT (func_decl))
16306 add_AT_die_ref (die, DW_AT_containing_type,
16307 lookup_type_die (DECL_CONTEXT (func_decl)));
16311 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16312 given decl. This used to be a vendor extension until after DWARF 4
16313 standardized it. */
16315 static void
16316 add_linkage_attr (dw_die_ref die, tree decl)
16318 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16320 /* Mimic what assemble_name_raw does with a leading '*'. */
16321 if (name[0] == '*')
16322 name = &name[1];
16324 if (dwarf_version >= 4)
16325 add_AT_string (die, DW_AT_linkage_name, name);
16326 else
16327 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16330 /* Add source coordinate attributes for the given decl. */
16332 static void
16333 add_src_coords_attributes (dw_die_ref die, tree decl)
16335 expanded_location s;
16337 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16338 return;
16339 s = expand_location (DECL_SOURCE_LOCATION (decl));
16340 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16341 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16344 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16346 static void
16347 add_linkage_name (dw_die_ref die, tree decl)
16349 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16350 && TREE_PUBLIC (decl)
16351 && !DECL_ABSTRACT (decl)
16352 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16353 && die->die_tag != DW_TAG_member)
16355 /* Defer until we have an assembler name set. */
16356 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16358 limbo_die_node *asm_name;
16360 asm_name = ggc_alloc_cleared_limbo_die_node ();
16361 asm_name->die = die;
16362 asm_name->created_for = decl;
16363 asm_name->next = deferred_asm_name;
16364 deferred_asm_name = asm_name;
16366 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16367 add_linkage_attr (die, decl);
16371 /* Add a DW_AT_name attribute and source coordinate attribute for the
16372 given decl, but only if it actually has a name. */
16374 static void
16375 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16377 tree decl_name;
16379 decl_name = DECL_NAME (decl);
16380 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16382 const char *name = dwarf2_name (decl, 0);
16383 if (name)
16384 add_name_attribute (die, name);
16385 if (! DECL_ARTIFICIAL (decl))
16386 add_src_coords_attributes (die, decl);
16388 add_linkage_name (die, decl);
16391 #ifdef VMS_DEBUGGING_INFO
16392 /* Get the function's name, as described by its RTL. This may be different
16393 from the DECL_NAME name used in the source file. */
16394 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16396 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16397 XEXP (DECL_RTL (decl), 0), false);
16398 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16400 #endif /* VMS_DEBUGGING_INFO */
16403 #ifdef VMS_DEBUGGING_INFO
16404 /* Output the debug main pointer die for VMS */
16406 void
16407 dwarf2out_vms_debug_main_pointer (void)
16409 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16410 dw_die_ref die;
16412 /* Allocate the VMS debug main subprogram die. */
16413 die = ggc_alloc_cleared_die_node ();
16414 die->die_tag = DW_TAG_subprogram;
16415 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16416 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16417 current_function_funcdef_no);
16418 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16420 /* Make it the first child of comp_unit_die (). */
16421 die->die_parent = comp_unit_die ();
16422 if (comp_unit_die ()->die_child)
16424 die->die_sib = comp_unit_die ()->die_child->die_sib;
16425 comp_unit_die ()->die_child->die_sib = die;
16427 else
16429 die->die_sib = die;
16430 comp_unit_die ()->die_child = die;
16433 #endif /* VMS_DEBUGGING_INFO */
16435 /* Push a new declaration scope. */
16437 static void
16438 push_decl_scope (tree scope)
16440 vec_safe_push (decl_scope_table, scope);
16443 /* Pop a declaration scope. */
16445 static inline void
16446 pop_decl_scope (void)
16448 decl_scope_table->pop ();
16451 /* walk_tree helper function for uses_local_type, below. */
16453 static tree
16454 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16456 if (!TYPE_P (*tp))
16457 *walk_subtrees = 0;
16458 else
16460 tree name = TYPE_NAME (*tp);
16461 if (name && DECL_P (name) && decl_function_context (name))
16462 return *tp;
16464 return NULL_TREE;
16467 /* If TYPE involves a function-local type (including a local typedef to a
16468 non-local type), returns that type; otherwise returns NULL_TREE. */
16470 static tree
16471 uses_local_type (tree type)
16473 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16474 return used;
16477 /* Return the DIE for the scope that immediately contains this type.
16478 Non-named types that do not involve a function-local type get global
16479 scope. Named types nested in namespaces or other types get their
16480 containing scope. All other types (i.e. function-local named types) get
16481 the current active scope. */
16483 static dw_die_ref
16484 scope_die_for (tree t, dw_die_ref context_die)
16486 dw_die_ref scope_die = NULL;
16487 tree containing_scope;
16489 /* Non-types always go in the current scope. */
16490 gcc_assert (TYPE_P (t));
16492 /* Use the scope of the typedef, rather than the scope of the type
16493 it refers to. */
16494 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16495 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16496 else
16497 containing_scope = TYPE_CONTEXT (t);
16499 /* Use the containing namespace if there is one. */
16500 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16502 if (context_die == lookup_decl_die (containing_scope))
16503 /* OK */;
16504 else if (debug_info_level > DINFO_LEVEL_TERSE)
16505 context_die = get_context_die (containing_scope);
16506 else
16507 containing_scope = NULL_TREE;
16510 /* Ignore function type "scopes" from the C frontend. They mean that
16511 a tagged type is local to a parmlist of a function declarator, but
16512 that isn't useful to DWARF. */
16513 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16514 containing_scope = NULL_TREE;
16516 if (SCOPE_FILE_SCOPE_P (containing_scope))
16518 /* If T uses a local type keep it local as well, to avoid references
16519 to function-local DIEs from outside the function. */
16520 if (current_function_decl && uses_local_type (t))
16521 scope_die = context_die;
16522 else
16523 scope_die = comp_unit_die ();
16525 else if (TYPE_P (containing_scope))
16527 /* For types, we can just look up the appropriate DIE. */
16528 if (debug_info_level > DINFO_LEVEL_TERSE)
16529 scope_die = get_context_die (containing_scope);
16530 else
16532 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16533 if (scope_die == NULL)
16534 scope_die = comp_unit_die ();
16537 else
16538 scope_die = context_die;
16540 return scope_die;
16543 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16545 static inline int
16546 local_scope_p (dw_die_ref context_die)
16548 for (; context_die; context_die = context_die->die_parent)
16549 if (context_die->die_tag == DW_TAG_inlined_subroutine
16550 || context_die->die_tag == DW_TAG_subprogram)
16551 return 1;
16553 return 0;
16556 /* Returns nonzero if CONTEXT_DIE is a class. */
16558 static inline int
16559 class_scope_p (dw_die_ref context_die)
16561 return (context_die
16562 && (context_die->die_tag == DW_TAG_structure_type
16563 || context_die->die_tag == DW_TAG_class_type
16564 || context_die->die_tag == DW_TAG_interface_type
16565 || context_die->die_tag == DW_TAG_union_type));
16568 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16569 whether or not to treat a DIE in this context as a declaration. */
16571 static inline int
16572 class_or_namespace_scope_p (dw_die_ref context_die)
16574 return (class_scope_p (context_die)
16575 || (context_die && context_die->die_tag == DW_TAG_namespace));
16578 /* Many forms of DIEs require a "type description" attribute. This
16579 routine locates the proper "type descriptor" die for the type given
16580 by 'type', and adds a DW_AT_type attribute below the given die. */
16582 static void
16583 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16584 int decl_volatile, dw_die_ref context_die)
16586 enum tree_code code = TREE_CODE (type);
16587 dw_die_ref type_die = NULL;
16589 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16590 or fixed-point type, use the inner type. This is because we have no
16591 support for unnamed types in base_type_die. This can happen if this is
16592 an Ada subrange type. Correct solution is emit a subrange type die. */
16593 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16594 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16595 type = TREE_TYPE (type), code = TREE_CODE (type);
16597 if (code == ERROR_MARK
16598 /* Handle a special case. For functions whose return type is void, we
16599 generate *no* type attribute. (Note that no object may have type
16600 `void', so this only applies to function return types). */
16601 || code == VOID_TYPE)
16602 return;
16604 type_die = modified_type_die (type,
16605 decl_const || TYPE_READONLY (type),
16606 decl_volatile || TYPE_VOLATILE (type),
16607 context_die);
16609 if (type_die != NULL)
16610 add_AT_die_ref (object_die, DW_AT_type, type_die);
16613 /* Given an object die, add the calling convention attribute for the
16614 function call type. */
16615 static void
16616 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16618 enum dwarf_calling_convention value = DW_CC_normal;
16620 value = ((enum dwarf_calling_convention)
16621 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16623 if (is_fortran ()
16624 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16626 /* DWARF 2 doesn't provide a way to identify a program's source-level
16627 entry point. DW_AT_calling_convention attributes are only meant
16628 to describe functions' calling conventions. However, lacking a
16629 better way to signal the Fortran main program, we used this for
16630 a long time, following existing custom. Now, DWARF 4 has
16631 DW_AT_main_subprogram, which we add below, but some tools still
16632 rely on the old way, which we thus keep. */
16633 value = DW_CC_program;
16635 if (dwarf_version >= 4 || !dwarf_strict)
16636 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16639 /* Only add the attribute if the backend requests it, and
16640 is not DW_CC_normal. */
16641 if (value && (value != DW_CC_normal))
16642 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16645 /* Given a tree pointer to a struct, class, union, or enum type node, return
16646 a pointer to the (string) tag name for the given type, or zero if the type
16647 was declared without a tag. */
16649 static const char *
16650 type_tag (const_tree type)
16652 const char *name = 0;
16654 if (TYPE_NAME (type) != 0)
16656 tree t = 0;
16658 /* Find the IDENTIFIER_NODE for the type name. */
16659 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16660 && !TYPE_NAMELESS (type))
16661 t = TYPE_NAME (type);
16663 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16664 a TYPE_DECL node, regardless of whether or not a `typedef' was
16665 involved. */
16666 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16667 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16669 /* We want to be extra verbose. Don't call dwarf_name if
16670 DECL_NAME isn't set. The default hook for decl_printable_name
16671 doesn't like that, and in this context it's correct to return
16672 0, instead of "<anonymous>" or the like. */
16673 if (DECL_NAME (TYPE_NAME (type))
16674 && !DECL_NAMELESS (TYPE_NAME (type)))
16675 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16678 /* Now get the name as a string, or invent one. */
16679 if (!name && t != 0)
16680 name = IDENTIFIER_POINTER (t);
16683 return (name == 0 || *name == '\0') ? 0 : name;
16686 /* Return the type associated with a data member, make a special check
16687 for bit field types. */
16689 static inline tree
16690 member_declared_type (const_tree member)
16692 return (DECL_BIT_FIELD_TYPE (member)
16693 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16696 /* Get the decl's label, as described by its RTL. This may be different
16697 from the DECL_NAME name used in the source file. */
16699 #if 0
16700 static const char *
16701 decl_start_label (tree decl)
16703 rtx x;
16704 const char *fnname;
16706 x = DECL_RTL (decl);
16707 gcc_assert (MEM_P (x));
16709 x = XEXP (x, 0);
16710 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16712 fnname = XSTR (x, 0);
16713 return fnname;
16715 #endif
16717 /* These routines generate the internal representation of the DIE's for
16718 the compilation unit. Debugging information is collected by walking
16719 the declaration trees passed in from dwarf2out_decl(). */
16721 static void
16722 gen_array_type_die (tree type, dw_die_ref context_die)
16724 dw_die_ref scope_die = scope_die_for (type, context_die);
16725 dw_die_ref array_die;
16727 /* GNU compilers represent multidimensional array types as sequences of one
16728 dimensional array types whose element types are themselves array types.
16729 We sometimes squish that down to a single array_type DIE with multiple
16730 subscripts in the Dwarf debugging info. The draft Dwarf specification
16731 say that we are allowed to do this kind of compression in C, because
16732 there is no difference between an array of arrays and a multidimensional
16733 array. We don't do this for Ada to remain as close as possible to the
16734 actual representation, which is especially important against the language
16735 flexibilty wrt arrays of variable size. */
16737 bool collapse_nested_arrays = !is_ada ();
16738 tree element_type;
16740 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16741 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16742 if (TYPE_STRING_FLAG (type)
16743 && TREE_CODE (type) == ARRAY_TYPE
16744 && is_fortran ()
16745 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16747 HOST_WIDE_INT size;
16749 array_die = new_die (DW_TAG_string_type, scope_die, type);
16750 add_name_attribute (array_die, type_tag (type));
16751 equate_type_number_to_die (type, array_die);
16752 size = int_size_in_bytes (type);
16753 if (size >= 0)
16754 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16755 else if (TYPE_DOMAIN (type) != NULL_TREE
16756 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16757 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16759 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16760 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16762 size = int_size_in_bytes (TREE_TYPE (szdecl));
16763 if (loc && size > 0)
16765 add_AT_location_description (array_die, DW_AT_string_length, loc);
16766 if (size != DWARF2_ADDR_SIZE)
16767 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16770 return;
16773 array_die = new_die (DW_TAG_array_type, scope_die, type);
16774 add_name_attribute (array_die, type_tag (type));
16775 equate_type_number_to_die (type, array_die);
16777 if (TREE_CODE (type) == VECTOR_TYPE)
16778 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16780 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16781 if (is_fortran ()
16782 && TREE_CODE (type) == ARRAY_TYPE
16783 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16784 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16785 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16787 #if 0
16788 /* We default the array ordering. SDB will probably do
16789 the right things even if DW_AT_ordering is not present. It's not even
16790 an issue until we start to get into multidimensional arrays anyway. If
16791 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16792 then we'll have to put the DW_AT_ordering attribute back in. (But if
16793 and when we find out that we need to put these in, we will only do so
16794 for multidimensional arrays. */
16795 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16796 #endif
16798 if (TREE_CODE (type) == VECTOR_TYPE)
16800 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16801 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16802 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16803 add_bound_info (subrange_die, DW_AT_upper_bound,
16804 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16806 else
16807 add_subscript_info (array_die, type, collapse_nested_arrays);
16809 /* Add representation of the type of the elements of this array type and
16810 emit the corresponding DIE if we haven't done it already. */
16811 element_type = TREE_TYPE (type);
16812 if (collapse_nested_arrays)
16813 while (TREE_CODE (element_type) == ARRAY_TYPE)
16815 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16816 break;
16817 element_type = TREE_TYPE (element_type);
16820 add_type_attribute (array_die, element_type, 0, 0, context_die);
16822 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16823 if (TYPE_ARTIFICIAL (type))
16824 add_AT_flag (array_die, DW_AT_artificial, 1);
16826 if (get_AT (array_die, DW_AT_name))
16827 add_pubtype (type, array_die);
16830 static dw_loc_descr_ref
16831 descr_info_loc (tree val, tree base_decl)
16833 HOST_WIDE_INT size;
16834 dw_loc_descr_ref loc, loc2;
16835 enum dwarf_location_atom op;
16837 if (val == base_decl)
16838 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16840 switch (TREE_CODE (val))
16842 CASE_CONVERT:
16843 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16844 case VAR_DECL:
16845 return loc_descriptor_from_tree (val, 0);
16846 case INTEGER_CST:
16847 if (host_integerp (val, 0))
16848 return int_loc_descriptor (tree_low_cst (val, 0));
16849 break;
16850 case INDIRECT_REF:
16851 size = int_size_in_bytes (TREE_TYPE (val));
16852 if (size < 0)
16853 break;
16854 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16855 if (!loc)
16856 break;
16857 if (size == DWARF2_ADDR_SIZE)
16858 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16859 else
16860 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16861 return loc;
16862 case POINTER_PLUS_EXPR:
16863 case PLUS_EXPR:
16864 if (host_integerp (TREE_OPERAND (val, 1), 1)
16865 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16866 < 16384)
16868 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16869 if (!loc)
16870 break;
16871 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16873 else
16875 op = DW_OP_plus;
16876 do_binop:
16877 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16878 if (!loc)
16879 break;
16880 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16881 if (!loc2)
16882 break;
16883 add_loc_descr (&loc, loc2);
16884 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16886 return loc;
16887 case MINUS_EXPR:
16888 op = DW_OP_minus;
16889 goto do_binop;
16890 case MULT_EXPR:
16891 op = DW_OP_mul;
16892 goto do_binop;
16893 case EQ_EXPR:
16894 op = DW_OP_eq;
16895 goto do_binop;
16896 case NE_EXPR:
16897 op = DW_OP_ne;
16898 goto do_binop;
16899 default:
16900 break;
16902 return NULL;
16905 static void
16906 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16907 tree val, tree base_decl)
16909 dw_loc_descr_ref loc;
16911 if (host_integerp (val, 0))
16913 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16914 return;
16917 loc = descr_info_loc (val, base_decl);
16918 if (!loc)
16919 return;
16921 add_AT_loc (die, attr, loc);
16924 /* This routine generates DIE for array with hidden descriptor, details
16925 are filled into *info by a langhook. */
16927 static void
16928 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16929 dw_die_ref context_die)
16931 dw_die_ref scope_die = scope_die_for (type, context_die);
16932 dw_die_ref array_die;
16933 int dim;
16935 array_die = new_die (DW_TAG_array_type, scope_die, type);
16936 add_name_attribute (array_die, type_tag (type));
16937 equate_type_number_to_die (type, array_die);
16939 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16940 if (is_fortran ()
16941 && info->ndimensions >= 2)
16942 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16944 if (info->data_location)
16945 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16946 info->base_decl);
16947 if (info->associated)
16948 add_descr_info_field (array_die, DW_AT_associated, info->associated,
16949 info->base_decl);
16950 if (info->allocated)
16951 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16952 info->base_decl);
16954 for (dim = 0; dim < info->ndimensions; dim++)
16956 dw_die_ref subrange_die
16957 = new_die (DW_TAG_subrange_type, array_die, NULL);
16959 if (info->dimen[dim].lower_bound)
16961 /* If it is the default value, omit it. */
16962 int dflt;
16964 if (host_integerp (info->dimen[dim].lower_bound, 0)
16965 && (dflt = lower_bound_default ()) != -1
16966 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16968 else
16969 add_descr_info_field (subrange_die, DW_AT_lower_bound,
16970 info->dimen[dim].lower_bound,
16971 info->base_decl);
16973 if (info->dimen[dim].upper_bound)
16974 add_descr_info_field (subrange_die, DW_AT_upper_bound,
16975 info->dimen[dim].upper_bound,
16976 info->base_decl);
16977 if (info->dimen[dim].stride)
16978 add_descr_info_field (subrange_die, DW_AT_byte_stride,
16979 info->dimen[dim].stride,
16980 info->base_decl);
16983 gen_type_die (info->element_type, context_die);
16984 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16986 if (get_AT (array_die, DW_AT_name))
16987 add_pubtype (type, array_die);
16990 #if 0
16991 static void
16992 gen_entry_point_die (tree decl, dw_die_ref context_die)
16994 tree origin = decl_ultimate_origin (decl);
16995 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16997 if (origin != NULL)
16998 add_abstract_origin_attribute (decl_die, origin);
16999 else
17001 add_name_and_src_coords_attributes (decl_die, decl);
17002 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17003 0, 0, context_die);
17006 if (DECL_ABSTRACT (decl))
17007 equate_decl_number_to_die (decl, decl_die);
17008 else
17009 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17011 #endif
17013 /* Walk through the list of incomplete types again, trying once more to
17014 emit full debugging info for them. */
17016 static void
17017 retry_incomplete_types (void)
17019 int i;
17021 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17022 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17023 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17026 /* Determine what tag to use for a record type. */
17028 static enum dwarf_tag
17029 record_type_tag (tree type)
17031 if (! lang_hooks.types.classify_record)
17032 return DW_TAG_structure_type;
17034 switch (lang_hooks.types.classify_record (type))
17036 case RECORD_IS_STRUCT:
17037 return DW_TAG_structure_type;
17039 case RECORD_IS_CLASS:
17040 return DW_TAG_class_type;
17042 case RECORD_IS_INTERFACE:
17043 if (dwarf_version >= 3 || !dwarf_strict)
17044 return DW_TAG_interface_type;
17045 return DW_TAG_structure_type;
17047 default:
17048 gcc_unreachable ();
17052 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17053 include all of the information about the enumeration values also. Each
17054 enumerated type name/value is listed as a child of the enumerated type
17055 DIE. */
17057 static dw_die_ref
17058 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17060 dw_die_ref type_die = lookup_type_die (type);
17062 if (type_die == NULL)
17064 type_die = new_die (DW_TAG_enumeration_type,
17065 scope_die_for (type, context_die), type);
17066 equate_type_number_to_die (type, type_die);
17067 add_name_attribute (type_die, type_tag (type));
17068 if (dwarf_version >= 4 || !dwarf_strict)
17070 if (ENUM_IS_SCOPED (type))
17071 add_AT_flag (type_die, DW_AT_enum_class, 1);
17072 if (ENUM_IS_OPAQUE (type))
17073 add_AT_flag (type_die, DW_AT_declaration, 1);
17076 else if (! TYPE_SIZE (type))
17077 return type_die;
17078 else
17079 remove_AT (type_die, DW_AT_declaration);
17081 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17082 given enum type is incomplete, do not generate the DW_AT_byte_size
17083 attribute or the DW_AT_element_list attribute. */
17084 if (TYPE_SIZE (type))
17086 tree link;
17088 TREE_ASM_WRITTEN (type) = 1;
17089 add_byte_size_attribute (type_die, type);
17090 if (TYPE_STUB_DECL (type) != NULL_TREE)
17092 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17093 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17096 /* If the first reference to this type was as the return type of an
17097 inline function, then it may not have a parent. Fix this now. */
17098 if (type_die->die_parent == NULL)
17099 add_child_die (scope_die_for (type, context_die), type_die);
17101 for (link = TYPE_VALUES (type);
17102 link != NULL; link = TREE_CHAIN (link))
17104 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17105 tree value = TREE_VALUE (link);
17107 add_name_attribute (enum_die,
17108 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17110 if (TREE_CODE (value) == CONST_DECL)
17111 value = DECL_INITIAL (value);
17113 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value)))
17114 && (simple_type_size_in_bits (TREE_TYPE (value))
17115 <= HOST_BITS_PER_WIDE_INT || host_integerp (value, 0)))
17116 /* DWARF2 does not provide a way of indicating whether or
17117 not enumeration constants are signed or unsigned. GDB
17118 always assumes the values are signed, so we output all
17119 values as if they were signed. That means that
17120 enumeration constants with very large unsigned values
17121 will appear to have negative values in the debugger.
17123 TODO: the above comment is wrong, DWARF2 does provide
17124 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17125 This should be re-worked to use correct signed/unsigned
17126 int/double tags for all cases, instead of always treating as
17127 signed. */
17128 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17129 else
17130 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17131 that here. */
17132 add_AT_double (enum_die, DW_AT_const_value,
17133 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17136 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17137 if (TYPE_ARTIFICIAL (type))
17138 add_AT_flag (type_die, DW_AT_artificial, 1);
17140 else
17141 add_AT_flag (type_die, DW_AT_declaration, 1);
17143 add_pubtype (type, type_die);
17145 return type_die;
17148 /* Generate a DIE to represent either a real live formal parameter decl or to
17149 represent just the type of some formal parameter position in some function
17150 type.
17152 Note that this routine is a bit unusual because its argument may be a
17153 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17154 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17155 node. If it's the former then this function is being called to output a
17156 DIE to represent a formal parameter object (or some inlining thereof). If
17157 it's the latter, then this function is only being called to output a
17158 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17159 argument type of some subprogram type.
17160 If EMIT_NAME_P is true, name and source coordinate attributes
17161 are emitted. */
17163 static dw_die_ref
17164 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17165 dw_die_ref context_die)
17167 tree node_or_origin = node ? node : origin;
17168 tree ultimate_origin;
17169 dw_die_ref parm_die
17170 = new_die (DW_TAG_formal_parameter, context_die, node);
17172 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17174 case tcc_declaration:
17175 ultimate_origin = decl_ultimate_origin (node_or_origin);
17176 if (node || ultimate_origin)
17177 origin = ultimate_origin;
17178 if (origin != NULL)
17179 add_abstract_origin_attribute (parm_die, origin);
17180 else if (emit_name_p)
17181 add_name_and_src_coords_attributes (parm_die, node);
17182 if (origin == NULL
17183 || (! DECL_ABSTRACT (node_or_origin)
17184 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17185 decl_function_context
17186 (node_or_origin))))
17188 tree type = TREE_TYPE (node_or_origin);
17189 if (decl_by_reference_p (node_or_origin))
17190 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17191 context_die);
17192 else
17193 add_type_attribute (parm_die, type,
17194 TREE_READONLY (node_or_origin),
17195 TREE_THIS_VOLATILE (node_or_origin),
17196 context_die);
17198 if (origin == NULL && DECL_ARTIFICIAL (node))
17199 add_AT_flag (parm_die, DW_AT_artificial, 1);
17201 if (node && node != origin)
17202 equate_decl_number_to_die (node, parm_die);
17203 if (! DECL_ABSTRACT (node_or_origin))
17204 add_location_or_const_value_attribute (parm_die, node_or_origin,
17205 node == NULL, DW_AT_location);
17207 break;
17209 case tcc_type:
17210 /* We were called with some kind of a ..._TYPE node. */
17211 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17212 break;
17214 default:
17215 gcc_unreachable ();
17218 return parm_die;
17221 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17222 children DW_TAG_formal_parameter DIEs representing the arguments of the
17223 parameter pack.
17225 PARM_PACK must be a function parameter pack.
17226 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17227 must point to the subsequent arguments of the function PACK_ARG belongs to.
17228 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17229 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17230 following the last one for which a DIE was generated. */
17232 static dw_die_ref
17233 gen_formal_parameter_pack_die (tree parm_pack,
17234 tree pack_arg,
17235 dw_die_ref subr_die,
17236 tree *next_arg)
17238 tree arg;
17239 dw_die_ref parm_pack_die;
17241 gcc_assert (parm_pack
17242 && lang_hooks.function_parameter_pack_p (parm_pack)
17243 && subr_die);
17245 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17246 add_src_coords_attributes (parm_pack_die, parm_pack);
17248 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17250 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17251 parm_pack))
17252 break;
17253 gen_formal_parameter_die (arg, NULL,
17254 false /* Don't emit name attribute. */,
17255 parm_pack_die);
17257 if (next_arg)
17258 *next_arg = arg;
17259 return parm_pack_die;
17262 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17263 at the end of an (ANSI prototyped) formal parameters list. */
17265 static void
17266 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17268 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17271 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17272 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17273 parameters as specified in some function type specification (except for
17274 those which appear as part of a function *definition*). */
17276 static void
17277 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17279 tree link;
17280 tree formal_type = NULL;
17281 tree first_parm_type;
17282 tree arg;
17284 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17286 arg = DECL_ARGUMENTS (function_or_method_type);
17287 function_or_method_type = TREE_TYPE (function_or_method_type);
17289 else
17290 arg = NULL_TREE;
17292 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17294 /* Make our first pass over the list of formal parameter types and output a
17295 DW_TAG_formal_parameter DIE for each one. */
17296 for (link = first_parm_type; link; )
17298 dw_die_ref parm_die;
17300 formal_type = TREE_VALUE (link);
17301 if (formal_type == void_type_node)
17302 break;
17304 /* Output a (nameless) DIE to represent the formal parameter itself. */
17305 parm_die = gen_formal_parameter_die (formal_type, NULL,
17306 true /* Emit name attribute. */,
17307 context_die);
17308 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17309 && link == first_parm_type)
17311 add_AT_flag (parm_die, DW_AT_artificial, 1);
17312 if (dwarf_version >= 3 || !dwarf_strict)
17313 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17315 else if (arg && DECL_ARTIFICIAL (arg))
17316 add_AT_flag (parm_die, DW_AT_artificial, 1);
17318 link = TREE_CHAIN (link);
17319 if (arg)
17320 arg = DECL_CHAIN (arg);
17323 /* If this function type has an ellipsis, add a
17324 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17325 if (formal_type != void_type_node)
17326 gen_unspecified_parameters_die (function_or_method_type, context_die);
17328 /* Make our second (and final) pass over the list of formal parameter types
17329 and output DIEs to represent those types (as necessary). */
17330 for (link = TYPE_ARG_TYPES (function_or_method_type);
17331 link && TREE_VALUE (link);
17332 link = TREE_CHAIN (link))
17333 gen_type_die (TREE_VALUE (link), context_die);
17336 /* We want to generate the DIE for TYPE so that we can generate the
17337 die for MEMBER, which has been defined; we will need to refer back
17338 to the member declaration nested within TYPE. If we're trying to
17339 generate minimal debug info for TYPE, processing TYPE won't do the
17340 trick; we need to attach the member declaration by hand. */
17342 static void
17343 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17345 gen_type_die (type, context_die);
17347 /* If we're trying to avoid duplicate debug info, we may not have
17348 emitted the member decl for this function. Emit it now. */
17349 if (TYPE_STUB_DECL (type)
17350 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17351 && ! lookup_decl_die (member))
17353 dw_die_ref type_die;
17354 gcc_assert (!decl_ultimate_origin (member));
17356 push_decl_scope (type);
17357 type_die = lookup_type_die_strip_naming_typedef (type);
17358 if (TREE_CODE (member) == FUNCTION_DECL)
17359 gen_subprogram_die (member, type_die);
17360 else if (TREE_CODE (member) == FIELD_DECL)
17362 /* Ignore the nameless fields that are used to skip bits but handle
17363 C++ anonymous unions and structs. */
17364 if (DECL_NAME (member) != NULL_TREE
17365 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17366 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17368 gen_type_die (member_declared_type (member), type_die);
17369 gen_field_die (member, type_die);
17372 else
17373 gen_variable_die (member, NULL_TREE, type_die);
17375 pop_decl_scope ();
17379 /* Forward declare these functions, because they are mutually recursive
17380 with their set_block_* pairing functions. */
17381 static void set_decl_origin_self (tree);
17382 static void set_decl_abstract_flags (tree, int);
17384 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17385 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17386 that it points to the node itself, thus indicating that the node is its
17387 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17388 the given node is NULL, recursively descend the decl/block tree which
17389 it is the root of, and for each other ..._DECL or BLOCK node contained
17390 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17391 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17392 values to point to themselves. */
17394 static void
17395 set_block_origin_self (tree stmt)
17397 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17399 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17402 tree local_decl;
17404 for (local_decl = BLOCK_VARS (stmt);
17405 local_decl != NULL_TREE;
17406 local_decl = DECL_CHAIN (local_decl))
17407 if (! DECL_EXTERNAL (local_decl))
17408 set_decl_origin_self (local_decl); /* Potential recursion. */
17412 tree subblock;
17414 for (subblock = BLOCK_SUBBLOCKS (stmt);
17415 subblock != NULL_TREE;
17416 subblock = BLOCK_CHAIN (subblock))
17417 set_block_origin_self (subblock); /* Recurse. */
17422 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17423 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17424 node to so that it points to the node itself, thus indicating that the
17425 node represents its own (abstract) origin. Additionally, if the
17426 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17427 the decl/block tree of which the given node is the root of, and for
17428 each other ..._DECL or BLOCK node contained therein whose
17429 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17430 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17431 point to themselves. */
17433 static void
17434 set_decl_origin_self (tree decl)
17436 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17438 DECL_ABSTRACT_ORIGIN (decl) = decl;
17439 if (TREE_CODE (decl) == FUNCTION_DECL)
17441 tree arg;
17443 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17444 DECL_ABSTRACT_ORIGIN (arg) = arg;
17445 if (DECL_INITIAL (decl) != NULL_TREE
17446 && DECL_INITIAL (decl) != error_mark_node)
17447 set_block_origin_self (DECL_INITIAL (decl));
17452 /* Given a pointer to some BLOCK node, and a boolean value to set the
17453 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17454 the given block, and for all local decls and all local sub-blocks
17455 (recursively) which are contained therein. */
17457 static void
17458 set_block_abstract_flags (tree stmt, int setting)
17460 tree local_decl;
17461 tree subblock;
17462 unsigned int i;
17464 BLOCK_ABSTRACT (stmt) = setting;
17466 for (local_decl = BLOCK_VARS (stmt);
17467 local_decl != NULL_TREE;
17468 local_decl = DECL_CHAIN (local_decl))
17469 if (! DECL_EXTERNAL (local_decl))
17470 set_decl_abstract_flags (local_decl, setting);
17472 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17474 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17475 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17476 || TREE_CODE (local_decl) == PARM_DECL)
17477 set_decl_abstract_flags (local_decl, setting);
17480 for (subblock = BLOCK_SUBBLOCKS (stmt);
17481 subblock != NULL_TREE;
17482 subblock = BLOCK_CHAIN (subblock))
17483 set_block_abstract_flags (subblock, setting);
17486 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17487 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17488 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17489 set the abstract flags for all of the parameters, local vars, local
17490 blocks and sub-blocks (recursively) to the same setting. */
17492 static void
17493 set_decl_abstract_flags (tree decl, int setting)
17495 DECL_ABSTRACT (decl) = setting;
17496 if (TREE_CODE (decl) == FUNCTION_DECL)
17498 tree arg;
17500 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17501 DECL_ABSTRACT (arg) = setting;
17502 if (DECL_INITIAL (decl) != NULL_TREE
17503 && DECL_INITIAL (decl) != error_mark_node)
17504 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17508 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17509 may later generate inlined and/or out-of-line instances of. */
17511 static void
17512 dwarf2out_abstract_function (tree decl)
17514 dw_die_ref old_die;
17515 tree save_fn;
17516 tree context;
17517 int was_abstract;
17518 htab_t old_decl_loc_table;
17519 htab_t old_cached_dw_loc_list_table;
17520 int old_call_site_count, old_tail_call_site_count;
17521 struct call_arg_loc_node *old_call_arg_locations;
17523 /* Make sure we have the actual abstract inline, not a clone. */
17524 decl = DECL_ORIGIN (decl);
17526 old_die = lookup_decl_die (decl);
17527 if (old_die && get_AT (old_die, DW_AT_inline))
17528 /* We've already generated the abstract instance. */
17529 return;
17531 /* We can be called while recursively when seeing block defining inlined subroutine
17532 DIE. Be sure to not clobber the outer location table nor use it or we would
17533 get locations in abstract instantces. */
17534 old_decl_loc_table = decl_loc_table;
17535 decl_loc_table = NULL;
17536 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17537 cached_dw_loc_list_table = NULL;
17538 old_call_arg_locations = call_arg_locations;
17539 call_arg_locations = NULL;
17540 old_call_site_count = call_site_count;
17541 call_site_count = -1;
17542 old_tail_call_site_count = tail_call_site_count;
17543 tail_call_site_count = -1;
17545 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17546 we don't get confused by DECL_ABSTRACT. */
17547 if (debug_info_level > DINFO_LEVEL_TERSE)
17549 context = decl_class_context (decl);
17550 if (context)
17551 gen_type_die_for_member
17552 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17555 /* Pretend we've just finished compiling this function. */
17556 save_fn = current_function_decl;
17557 current_function_decl = decl;
17559 was_abstract = DECL_ABSTRACT (decl);
17560 set_decl_abstract_flags (decl, 1);
17561 dwarf2out_decl (decl);
17562 if (! was_abstract)
17563 set_decl_abstract_flags (decl, 0);
17565 current_function_decl = save_fn;
17566 decl_loc_table = old_decl_loc_table;
17567 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17568 call_arg_locations = old_call_arg_locations;
17569 call_site_count = old_call_site_count;
17570 tail_call_site_count = old_tail_call_site_count;
17573 /* Helper function of premark_used_types() which gets called through
17574 htab_traverse.
17576 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17577 marked as unused by prune_unused_types. */
17579 static int
17580 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17582 tree type;
17583 dw_die_ref die;
17585 type = (tree) *slot;
17586 die = lookup_type_die (type);
17587 if (die != NULL)
17588 die->die_perennial_p = 1;
17589 return 1;
17592 /* Helper function of premark_types_used_by_global_vars which gets called
17593 through htab_traverse.
17595 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17596 marked as unused by prune_unused_types. The DIE of the type is marked
17597 only if the global variable using the type will actually be emitted. */
17599 static int
17600 premark_types_used_by_global_vars_helper (void **slot,
17601 void *data ATTRIBUTE_UNUSED)
17603 struct types_used_by_vars_entry *entry;
17604 dw_die_ref die;
17606 entry = (struct types_used_by_vars_entry *) *slot;
17607 gcc_assert (entry->type != NULL
17608 && entry->var_decl != NULL);
17609 die = lookup_type_die (entry->type);
17610 if (die)
17612 /* Ask cgraph if the global variable really is to be emitted.
17613 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17614 struct varpool_node *node = varpool_get_node (entry->var_decl);
17615 if (node && node->symbol.definition)
17617 die->die_perennial_p = 1;
17618 /* Keep the parent DIEs as well. */
17619 while ((die = die->die_parent) && die->die_perennial_p == 0)
17620 die->die_perennial_p = 1;
17623 return 1;
17626 /* Mark all members of used_types_hash as perennial. */
17628 static void
17629 premark_used_types (struct function *fun)
17631 if (fun && fun->used_types_hash)
17632 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17635 /* Mark all members of types_used_by_vars_entry as perennial. */
17637 static void
17638 premark_types_used_by_global_vars (void)
17640 if (types_used_by_vars_hash)
17641 htab_traverse (types_used_by_vars_hash,
17642 premark_types_used_by_global_vars_helper, NULL);
17645 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17646 for CA_LOC call arg loc node. */
17648 static dw_die_ref
17649 gen_call_site_die (tree decl, dw_die_ref subr_die,
17650 struct call_arg_loc_node *ca_loc)
17652 dw_die_ref stmt_die = NULL, die;
17653 tree block = ca_loc->block;
17655 while (block
17656 && block != DECL_INITIAL (decl)
17657 && TREE_CODE (block) == BLOCK)
17659 if (block_map.length () > BLOCK_NUMBER (block))
17660 stmt_die = block_map[BLOCK_NUMBER (block)];
17661 if (stmt_die)
17662 break;
17663 block = BLOCK_SUPERCONTEXT (block);
17665 if (stmt_die == NULL)
17666 stmt_die = subr_die;
17667 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17668 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17669 if (ca_loc->tail_call_p)
17670 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17671 if (ca_loc->symbol_ref)
17673 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17674 if (tdie)
17675 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17676 else
17677 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17679 return die;
17682 /* Generate a DIE to represent a declared function (either file-scope or
17683 block-local). */
17685 static void
17686 gen_subprogram_die (tree decl, dw_die_ref context_die)
17688 tree origin = decl_ultimate_origin (decl);
17689 dw_die_ref subr_die;
17690 tree outer_scope;
17691 dw_die_ref old_die = lookup_decl_die (decl);
17692 int declaration = (current_function_decl != decl
17693 || class_or_namespace_scope_p (context_die));
17695 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17697 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17698 started to generate the abstract instance of an inline, decided to output
17699 its containing class, and proceeded to emit the declaration of the inline
17700 from the member list for the class. If so, DECLARATION takes priority;
17701 we'll get back to the abstract instance when done with the class. */
17703 /* The class-scope declaration DIE must be the primary DIE. */
17704 if (origin && declaration && class_or_namespace_scope_p (context_die))
17706 origin = NULL;
17707 gcc_assert (!old_die);
17710 /* Now that the C++ front end lazily declares artificial member fns, we
17711 might need to retrofit the declaration into its class. */
17712 if (!declaration && !origin && !old_die
17713 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17714 && !class_or_namespace_scope_p (context_die)
17715 && debug_info_level > DINFO_LEVEL_TERSE)
17716 old_die = force_decl_die (decl);
17718 if (origin != NULL)
17720 gcc_assert (!declaration || local_scope_p (context_die));
17722 /* Fixup die_parent for the abstract instance of a nested
17723 inline function. */
17724 if (old_die && old_die->die_parent == NULL)
17725 add_child_die (context_die, old_die);
17727 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17728 add_abstract_origin_attribute (subr_die, origin);
17729 /* This is where the actual code for a cloned function is.
17730 Let's emit linkage name attribute for it. This helps
17731 debuggers to e.g, set breakpoints into
17732 constructors/destructors when the user asks "break
17733 K::K". */
17734 add_linkage_name (subr_die, decl);
17736 else if (old_die)
17738 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17739 struct dwarf_file_data * file_index = lookup_filename (s.file);
17741 if (!get_AT_flag (old_die, DW_AT_declaration)
17742 /* We can have a normal definition following an inline one in the
17743 case of redefinition of GNU C extern inlines.
17744 It seems reasonable to use AT_specification in this case. */
17745 && !get_AT (old_die, DW_AT_inline))
17747 /* Detect and ignore this case, where we are trying to output
17748 something we have already output. */
17749 return;
17752 /* If the definition comes from the same place as the declaration,
17753 maybe use the old DIE. We always want the DIE for this function
17754 that has the *_pc attributes to be under comp_unit_die so the
17755 debugger can find it. We also need to do this for abstract
17756 instances of inlines, since the spec requires the out-of-line copy
17757 to have the same parent. For local class methods, this doesn't
17758 apply; we just use the old DIE. */
17759 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17760 && (DECL_ARTIFICIAL (decl)
17761 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17762 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17763 == (unsigned) s.line))))
17765 subr_die = old_die;
17767 /* Clear out the declaration attribute and the formal parameters.
17768 Do not remove all children, because it is possible that this
17769 declaration die was forced using force_decl_die(). In such
17770 cases die that forced declaration die (e.g. TAG_imported_module)
17771 is one of the children that we do not want to remove. */
17772 remove_AT (subr_die, DW_AT_declaration);
17773 remove_AT (subr_die, DW_AT_object_pointer);
17774 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17776 else
17778 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17779 add_AT_specification (subr_die, old_die);
17780 add_pubname (decl, subr_die);
17781 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17782 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17783 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17784 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17787 else
17789 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17791 if (TREE_PUBLIC (decl))
17792 add_AT_flag (subr_die, DW_AT_external, 1);
17794 add_name_and_src_coords_attributes (subr_die, decl);
17795 add_pubname (decl, subr_die);
17796 if (debug_info_level > DINFO_LEVEL_TERSE)
17798 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17799 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17800 0, 0, context_die);
17803 add_pure_or_virtual_attribute (subr_die, decl);
17804 if (DECL_ARTIFICIAL (decl))
17805 add_AT_flag (subr_die, DW_AT_artificial, 1);
17807 add_accessibility_attribute (subr_die, decl);
17810 if (declaration)
17812 if (!old_die || !get_AT (old_die, DW_AT_inline))
17814 add_AT_flag (subr_die, DW_AT_declaration, 1);
17816 /* If this is an explicit function declaration then generate
17817 a DW_AT_explicit attribute. */
17818 if (lang_hooks.decls.function_decl_explicit_p (decl)
17819 && (dwarf_version >= 3 || !dwarf_strict))
17820 add_AT_flag (subr_die, DW_AT_explicit, 1);
17822 /* The first time we see a member function, it is in the context of
17823 the class to which it belongs. We make sure of this by emitting
17824 the class first. The next time is the definition, which is
17825 handled above. The two may come from the same source text.
17827 Note that force_decl_die() forces function declaration die. It is
17828 later reused to represent definition. */
17829 equate_decl_number_to_die (decl, subr_die);
17832 else if (DECL_ABSTRACT (decl))
17834 if (DECL_DECLARED_INLINE_P (decl))
17836 if (cgraph_function_possibly_inlined_p (decl))
17837 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17838 else
17839 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17841 else
17843 if (cgraph_function_possibly_inlined_p (decl))
17844 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17845 else
17846 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17849 if (DECL_DECLARED_INLINE_P (decl)
17850 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17851 add_AT_flag (subr_die, DW_AT_artificial, 1);
17853 equate_decl_number_to_die (decl, subr_die);
17855 else if (!DECL_EXTERNAL (decl))
17857 HOST_WIDE_INT cfa_fb_offset;
17858 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17860 if (!old_die || !get_AT (old_die, DW_AT_inline))
17861 equate_decl_number_to_die (decl, subr_die);
17863 gcc_checking_assert (fun);
17864 if (!flag_reorder_blocks_and_partition)
17866 dw_fde_ref fde = fun->fde;
17867 if (fde->dw_fde_begin)
17869 /* We have already generated the labels. */
17870 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17871 fde->dw_fde_end, false);
17873 else
17875 /* Create start/end labels and add the range. */
17876 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17877 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17878 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17879 current_function_funcdef_no);
17880 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17881 current_function_funcdef_no);
17882 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17883 false);
17886 #if VMS_DEBUGGING_INFO
17887 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17888 Section 2.3 Prologue and Epilogue Attributes:
17889 When a breakpoint is set on entry to a function, it is generally
17890 desirable for execution to be suspended, not on the very first
17891 instruction of the function, but rather at a point after the
17892 function's frame has been set up, after any language defined local
17893 declaration processing has been completed, and before execution of
17894 the first statement of the function begins. Debuggers generally
17895 cannot properly determine where this point is. Similarly for a
17896 breakpoint set on exit from a function. The prologue and epilogue
17897 attributes allow a compiler to communicate the location(s) to use. */
17900 if (fde->dw_fde_vms_end_prologue)
17901 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17902 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17904 if (fde->dw_fde_vms_begin_epilogue)
17905 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17906 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17908 #endif
17911 else
17913 /* Generate pubnames entries for the split function code ranges. */
17914 dw_fde_ref fde = fun->fde;
17916 if (fde->dw_fde_second_begin)
17918 if (dwarf_version >= 3 || !dwarf_strict)
17920 /* We should use ranges for non-contiguous code section
17921 addresses. Use the actual code range for the initial
17922 section, since the HOT/COLD labels might precede an
17923 alignment offset. */
17924 bool range_list_added = false;
17925 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17926 fde->dw_fde_end, &range_list_added,
17927 false);
17928 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17929 fde->dw_fde_second_end,
17930 &range_list_added, false);
17931 if (range_list_added)
17932 add_ranges (NULL);
17934 else
17936 /* There is no real support in DW2 for this .. so we make
17937 a work-around. First, emit the pub name for the segment
17938 containing the function label. Then make and emit a
17939 simplified subprogram DIE for the second segment with the
17940 name pre-fixed by __hot/cold_sect_of_. We use the same
17941 linkage name for the second die so that gdb will find both
17942 sections when given "b foo". */
17943 const char *name = NULL;
17944 tree decl_name = DECL_NAME (decl);
17945 dw_die_ref seg_die;
17947 /* Do the 'primary' section. */
17948 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17949 fde->dw_fde_end, false);
17951 /* Build a minimal DIE for the secondary section. */
17952 seg_die = new_die (DW_TAG_subprogram,
17953 subr_die->die_parent, decl);
17955 if (TREE_PUBLIC (decl))
17956 add_AT_flag (seg_die, DW_AT_external, 1);
17958 if (decl_name != NULL
17959 && IDENTIFIER_POINTER (decl_name) != NULL)
17961 name = dwarf2_name (decl, 1);
17962 if (! DECL_ARTIFICIAL (decl))
17963 add_src_coords_attributes (seg_die, decl);
17965 add_linkage_name (seg_die, decl);
17967 gcc_assert (name != NULL);
17968 add_pure_or_virtual_attribute (seg_die, decl);
17969 if (DECL_ARTIFICIAL (decl))
17970 add_AT_flag (seg_die, DW_AT_artificial, 1);
17972 name = concat ("__second_sect_of_", name, NULL);
17973 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17974 fde->dw_fde_second_end, false);
17975 add_name_attribute (seg_die, name);
17976 if (want_pubnames ())
17977 add_pubname_string (name, seg_die);
17980 else
17981 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17982 false);
17985 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17987 /* We define the "frame base" as the function's CFA. This is more
17988 convenient for several reasons: (1) It's stable across the prologue
17989 and epilogue, which makes it better than just a frame pointer,
17990 (2) With dwarf3, there exists a one-byte encoding that allows us
17991 to reference the .debug_frame data by proxy, but failing that,
17992 (3) We can at least reuse the code inspection and interpretation
17993 code that determines the CFA position at various points in the
17994 function. */
17995 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17997 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17998 add_AT_loc (subr_die, DW_AT_frame_base, op);
18000 else
18002 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18003 if (list->dw_loc_next)
18004 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18005 else
18006 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18009 /* Compute a displacement from the "steady-state frame pointer" to
18010 the CFA. The former is what all stack slots and argument slots
18011 will reference in the rtl; the latter is what we've told the
18012 debugger about. We'll need to adjust all frame_base references
18013 by this displacement. */
18014 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18016 if (fun->static_chain_decl)
18017 add_AT_location_description (subr_die, DW_AT_static_link,
18018 loc_list_from_tree (fun->static_chain_decl, 2));
18021 /* Generate child dies for template paramaters. */
18022 if (debug_info_level > DINFO_LEVEL_TERSE)
18023 gen_generic_params_dies (decl);
18025 /* Now output descriptions of the arguments for this function. This gets
18026 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18027 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18028 `...' at the end of the formal parameter list. In order to find out if
18029 there was a trailing ellipsis or not, we must instead look at the type
18030 associated with the FUNCTION_DECL. This will be a node of type
18031 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18032 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18033 an ellipsis at the end. */
18035 /* In the case where we are describing a mere function declaration, all we
18036 need to do here (and all we *can* do here) is to describe the *types* of
18037 its formal parameters. */
18038 if (debug_info_level <= DINFO_LEVEL_TERSE)
18040 else if (declaration)
18041 gen_formal_types_die (decl, subr_die);
18042 else
18044 /* Generate DIEs to represent all known formal parameters. */
18045 tree parm = DECL_ARGUMENTS (decl);
18046 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18047 tree generic_decl_parm = generic_decl
18048 ? DECL_ARGUMENTS (generic_decl)
18049 : NULL;
18051 /* Now we want to walk the list of parameters of the function and
18052 emit their relevant DIEs.
18054 We consider the case of DECL being an instance of a generic function
18055 as well as it being a normal function.
18057 If DECL is an instance of a generic function we walk the
18058 parameters of the generic function declaration _and_ the parameters of
18059 DECL itself. This is useful because we want to emit specific DIEs for
18060 function parameter packs and those are declared as part of the
18061 generic function declaration. In that particular case,
18062 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18063 That DIE has children DIEs representing the set of arguments
18064 of the pack. Note that the set of pack arguments can be empty.
18065 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18066 children DIE.
18068 Otherwise, we just consider the parameters of DECL. */
18069 while (generic_decl_parm || parm)
18071 if (generic_decl_parm
18072 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18073 gen_formal_parameter_pack_die (generic_decl_parm,
18074 parm, subr_die,
18075 &parm);
18076 else if (parm)
18078 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18080 if (parm == DECL_ARGUMENTS (decl)
18081 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18082 && parm_die
18083 && (dwarf_version >= 3 || !dwarf_strict))
18084 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18086 parm = DECL_CHAIN (parm);
18089 if (generic_decl_parm)
18090 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18093 /* Decide whether we need an unspecified_parameters DIE at the end.
18094 There are 2 more cases to do this for: 1) the ansi ... declaration -
18095 this is detectable when the end of the arg list is not a
18096 void_type_node 2) an unprototyped function declaration (not a
18097 definition). This just means that we have no info about the
18098 parameters at all. */
18099 if (prototype_p (TREE_TYPE (decl)))
18101 /* This is the prototyped case, check for.... */
18102 if (stdarg_p (TREE_TYPE (decl)))
18103 gen_unspecified_parameters_die (decl, subr_die);
18105 else if (DECL_INITIAL (decl) == NULL_TREE)
18106 gen_unspecified_parameters_die (decl, subr_die);
18109 /* Output Dwarf info for all of the stuff within the body of the function
18110 (if it has one - it may be just a declaration). */
18111 outer_scope = DECL_INITIAL (decl);
18113 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18114 a function. This BLOCK actually represents the outermost binding contour
18115 for the function, i.e. the contour in which the function's formal
18116 parameters and labels get declared. Curiously, it appears that the front
18117 end doesn't actually put the PARM_DECL nodes for the current function onto
18118 the BLOCK_VARS list for this outer scope, but are strung off of the
18119 DECL_ARGUMENTS list for the function instead.
18121 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18122 the LABEL_DECL nodes for the function however, and we output DWARF info
18123 for those in decls_for_scope. Just within the `outer_scope' there will be
18124 a BLOCK node representing the function's outermost pair of curly braces,
18125 and any blocks used for the base and member initializers of a C++
18126 constructor function. */
18127 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18129 int call_site_note_count = 0;
18130 int tail_call_site_note_count = 0;
18132 /* Emit a DW_TAG_variable DIE for a named return value. */
18133 if (DECL_NAME (DECL_RESULT (decl)))
18134 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18136 current_function_has_inlines = 0;
18137 decls_for_scope (outer_scope, subr_die, 0);
18139 if (call_arg_locations && !dwarf_strict)
18141 struct call_arg_loc_node *ca_loc;
18142 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18144 dw_die_ref die = NULL;
18145 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18146 rtx arg, next_arg;
18148 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18149 arg; arg = next_arg)
18151 dw_loc_descr_ref reg, val;
18152 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18153 dw_die_ref cdie, tdie = NULL;
18155 next_arg = XEXP (arg, 1);
18156 if (REG_P (XEXP (XEXP (arg, 0), 0))
18157 && next_arg
18158 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18159 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18160 && REGNO (XEXP (XEXP (arg, 0), 0))
18161 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18162 next_arg = XEXP (next_arg, 1);
18163 if (mode == VOIDmode)
18165 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18166 if (mode == VOIDmode)
18167 mode = GET_MODE (XEXP (arg, 0));
18169 if (mode == VOIDmode || mode == BLKmode)
18170 continue;
18171 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18173 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18174 tloc = XEXP (XEXP (arg, 0), 1);
18175 continue;
18177 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18178 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18180 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18181 tlocc = XEXP (XEXP (arg, 0), 1);
18182 continue;
18184 reg = NULL;
18185 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18186 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18187 VAR_INIT_STATUS_INITIALIZED);
18188 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18190 rtx mem = XEXP (XEXP (arg, 0), 0);
18191 reg = mem_loc_descriptor (XEXP (mem, 0),
18192 get_address_mode (mem),
18193 GET_MODE (mem),
18194 VAR_INIT_STATUS_INITIALIZED);
18196 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18197 == DEBUG_PARAMETER_REF)
18199 tree tdecl
18200 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18201 tdie = lookup_decl_die (tdecl);
18202 if (tdie == NULL)
18203 continue;
18205 else
18206 continue;
18207 if (reg == NULL
18208 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18209 != DEBUG_PARAMETER_REF)
18210 continue;
18211 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18212 VOIDmode,
18213 VAR_INIT_STATUS_INITIALIZED);
18214 if (val == NULL)
18215 continue;
18216 if (die == NULL)
18217 die = gen_call_site_die (decl, subr_die, ca_loc);
18218 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18219 NULL_TREE);
18220 if (reg != NULL)
18221 add_AT_loc (cdie, DW_AT_location, reg);
18222 else if (tdie != NULL)
18223 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18224 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18225 if (next_arg != XEXP (arg, 1))
18227 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18228 if (mode == VOIDmode)
18229 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18230 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18231 0), 1),
18232 mode, VOIDmode,
18233 VAR_INIT_STATUS_INITIALIZED);
18234 if (val != NULL)
18235 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18238 if (die == NULL
18239 && (ca_loc->symbol_ref || tloc))
18240 die = gen_call_site_die (decl, subr_die, ca_loc);
18241 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18243 dw_loc_descr_ref tval = NULL;
18245 if (tloc != NULL_RTX)
18246 tval = mem_loc_descriptor (tloc,
18247 GET_MODE (tloc) == VOIDmode
18248 ? Pmode : GET_MODE (tloc),
18249 VOIDmode,
18250 VAR_INIT_STATUS_INITIALIZED);
18251 if (tval)
18252 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18253 else if (tlocc != NULL_RTX)
18255 tval = mem_loc_descriptor (tlocc,
18256 GET_MODE (tlocc) == VOIDmode
18257 ? Pmode : GET_MODE (tlocc),
18258 VOIDmode,
18259 VAR_INIT_STATUS_INITIALIZED);
18260 if (tval)
18261 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18262 tval);
18265 if (die != NULL)
18267 call_site_note_count++;
18268 if (ca_loc->tail_call_p)
18269 tail_call_site_note_count++;
18273 call_arg_locations = NULL;
18274 call_arg_loc_last = NULL;
18275 if (tail_call_site_count >= 0
18276 && tail_call_site_count == tail_call_site_note_count
18277 && !dwarf_strict)
18279 if (call_site_count >= 0
18280 && call_site_count == call_site_note_count)
18281 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18282 else
18283 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18285 call_site_count = -1;
18286 tail_call_site_count = -1;
18288 /* Add the calling convention attribute if requested. */
18289 add_calling_convention_attribute (subr_die, decl);
18293 /* Returns a hash value for X (which really is a die_struct). */
18295 static hashval_t
18296 common_block_die_table_hash (const void *x)
18298 const_dw_die_ref d = (const_dw_die_ref) x;
18299 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18302 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18303 as decl_id and die_parent of die_struct Y. */
18305 static int
18306 common_block_die_table_eq (const void *x, const void *y)
18308 const_dw_die_ref d = (const_dw_die_ref) x;
18309 const_dw_die_ref e = (const_dw_die_ref) y;
18310 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18313 /* Generate a DIE to represent a declared data object.
18314 Either DECL or ORIGIN must be non-null. */
18316 static void
18317 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18319 HOST_WIDE_INT off = 0;
18320 tree com_decl;
18321 tree decl_or_origin = decl ? decl : origin;
18322 tree ultimate_origin;
18323 dw_die_ref var_die;
18324 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18325 dw_die_ref origin_die;
18326 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18327 || class_or_namespace_scope_p (context_die));
18328 bool specialization_p = false;
18330 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18331 if (decl || ultimate_origin)
18332 origin = ultimate_origin;
18333 com_decl = fortran_common (decl_or_origin, &off);
18335 /* Symbol in common gets emitted as a child of the common block, in the form
18336 of a data member. */
18337 if (com_decl)
18339 dw_die_ref com_die;
18340 dw_loc_list_ref loc;
18341 die_node com_die_arg;
18343 var_die = lookup_decl_die (decl_or_origin);
18344 if (var_die)
18346 if (get_AT (var_die, DW_AT_location) == NULL)
18348 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18349 if (loc)
18351 if (off)
18353 /* Optimize the common case. */
18354 if (single_element_loc_list_p (loc)
18355 && loc->expr->dw_loc_opc == DW_OP_addr
18356 && loc->expr->dw_loc_next == NULL
18357 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18358 == SYMBOL_REF)
18360 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18361 loc->expr->dw_loc_oprnd1.v.val_addr
18362 = plus_constant (GET_MODE (x), x , off);
18364 else
18365 loc_list_plus_const (loc, off);
18367 add_AT_location_description (var_die, DW_AT_location, loc);
18368 remove_AT (var_die, DW_AT_declaration);
18371 return;
18374 if (common_block_die_table == NULL)
18375 common_block_die_table
18376 = htab_create_ggc (10, common_block_die_table_hash,
18377 common_block_die_table_eq, NULL);
18379 com_die_arg.decl_id = DECL_UID (com_decl);
18380 com_die_arg.die_parent = context_die;
18381 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18382 loc = loc_list_from_tree (com_decl, 2);
18383 if (com_die == NULL)
18385 const char *cnam
18386 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18387 void **slot;
18389 com_die = new_die (DW_TAG_common_block, context_die, decl);
18390 add_name_and_src_coords_attributes (com_die, com_decl);
18391 if (loc)
18393 add_AT_location_description (com_die, DW_AT_location, loc);
18394 /* Avoid sharing the same loc descriptor between
18395 DW_TAG_common_block and DW_TAG_variable. */
18396 loc = loc_list_from_tree (com_decl, 2);
18398 else if (DECL_EXTERNAL (decl))
18399 add_AT_flag (com_die, DW_AT_declaration, 1);
18400 if (want_pubnames ())
18401 add_pubname_string (cnam, com_die); /* ??? needed? */
18402 com_die->decl_id = DECL_UID (com_decl);
18403 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18404 *slot = (void *) com_die;
18406 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18408 add_AT_location_description (com_die, DW_AT_location, loc);
18409 loc = loc_list_from_tree (com_decl, 2);
18410 remove_AT (com_die, DW_AT_declaration);
18412 var_die = new_die (DW_TAG_variable, com_die, decl);
18413 add_name_and_src_coords_attributes (var_die, decl);
18414 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18415 TREE_THIS_VOLATILE (decl), context_die);
18416 add_AT_flag (var_die, DW_AT_external, 1);
18417 if (loc)
18419 if (off)
18421 /* Optimize the common case. */
18422 if (single_element_loc_list_p (loc)
18423 && loc->expr->dw_loc_opc == DW_OP_addr
18424 && loc->expr->dw_loc_next == NULL
18425 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18427 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18428 loc->expr->dw_loc_oprnd1.v.val_addr
18429 = plus_constant (GET_MODE (x), x, off);
18431 else
18432 loc_list_plus_const (loc, off);
18434 add_AT_location_description (var_die, DW_AT_location, loc);
18436 else if (DECL_EXTERNAL (decl))
18437 add_AT_flag (var_die, DW_AT_declaration, 1);
18438 equate_decl_number_to_die (decl, var_die);
18439 return;
18442 /* If the compiler emitted a definition for the DECL declaration
18443 and if we already emitted a DIE for it, don't emit a second
18444 DIE for it again. Allow re-declarations of DECLs that are
18445 inside functions, though. */
18446 if (old_die && declaration && !local_scope_p (context_die))
18447 return;
18449 /* For static data members, the declaration in the class is supposed
18450 to have DW_TAG_member tag; the specification should still be
18451 DW_TAG_variable referencing the DW_TAG_member DIE. */
18452 if (declaration && class_scope_p (context_die))
18453 var_die = new_die (DW_TAG_member, context_die, decl);
18454 else
18455 var_die = new_die (DW_TAG_variable, context_die, decl);
18457 origin_die = NULL;
18458 if (origin != NULL)
18459 origin_die = add_abstract_origin_attribute (var_die, origin);
18461 /* Loop unrolling can create multiple blocks that refer to the same
18462 static variable, so we must test for the DW_AT_declaration flag.
18464 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18465 copy decls and set the DECL_ABSTRACT flag on them instead of
18466 sharing them.
18468 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18470 ??? The declare_in_namespace support causes us to get two DIEs for one
18471 variable, both of which are declarations. We want to avoid considering
18472 one to be a specification, so we must test that this DIE is not a
18473 declaration. */
18474 else if (old_die && TREE_STATIC (decl) && ! declaration
18475 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18477 /* This is a definition of a C++ class level static. */
18478 add_AT_specification (var_die, old_die);
18479 specialization_p = true;
18480 if (DECL_NAME (decl))
18482 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18483 struct dwarf_file_data * file_index = lookup_filename (s.file);
18485 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18486 add_AT_file (var_die, DW_AT_decl_file, file_index);
18488 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18489 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18491 if (old_die->die_tag == DW_TAG_member)
18492 add_linkage_name (var_die, decl);
18495 else
18496 add_name_and_src_coords_attributes (var_die, decl);
18498 if ((origin == NULL && !specialization_p)
18499 || (origin != NULL
18500 && !DECL_ABSTRACT (decl_or_origin)
18501 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18502 decl_function_context
18503 (decl_or_origin))))
18505 tree type = TREE_TYPE (decl_or_origin);
18507 if (decl_by_reference_p (decl_or_origin))
18508 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18509 else
18510 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18511 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18514 if (origin == NULL && !specialization_p)
18516 if (TREE_PUBLIC (decl))
18517 add_AT_flag (var_die, DW_AT_external, 1);
18519 if (DECL_ARTIFICIAL (decl))
18520 add_AT_flag (var_die, DW_AT_artificial, 1);
18522 add_accessibility_attribute (var_die, decl);
18525 if (declaration)
18526 add_AT_flag (var_die, DW_AT_declaration, 1);
18528 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18529 equate_decl_number_to_die (decl, var_die);
18531 if (! declaration
18532 && (! DECL_ABSTRACT (decl_or_origin)
18533 /* Local static vars are shared between all clones/inlines,
18534 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18535 already set. */
18536 || (TREE_CODE (decl_or_origin) == VAR_DECL
18537 && TREE_STATIC (decl_or_origin)
18538 && DECL_RTL_SET_P (decl_or_origin)))
18539 /* When abstract origin already has DW_AT_location attribute, no need
18540 to add it again. */
18541 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18543 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18544 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18545 defer_location (decl_or_origin, var_die);
18546 else
18547 add_location_or_const_value_attribute (var_die, decl_or_origin,
18548 decl == NULL, DW_AT_location);
18549 add_pubname (decl_or_origin, var_die);
18551 else
18552 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18555 /* Generate a DIE to represent a named constant. */
18557 static void
18558 gen_const_die (tree decl, dw_die_ref context_die)
18560 dw_die_ref const_die;
18561 tree type = TREE_TYPE (decl);
18563 const_die = new_die (DW_TAG_constant, context_die, decl);
18564 add_name_and_src_coords_attributes (const_die, decl);
18565 add_type_attribute (const_die, type, 1, 0, context_die);
18566 if (TREE_PUBLIC (decl))
18567 add_AT_flag (const_die, DW_AT_external, 1);
18568 if (DECL_ARTIFICIAL (decl))
18569 add_AT_flag (const_die, DW_AT_artificial, 1);
18570 tree_add_const_value_attribute_for_decl (const_die, decl);
18573 /* Generate a DIE to represent a label identifier. */
18575 static void
18576 gen_label_die (tree decl, dw_die_ref context_die)
18578 tree origin = decl_ultimate_origin (decl);
18579 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18580 rtx insn;
18581 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18583 if (origin != NULL)
18584 add_abstract_origin_attribute (lbl_die, origin);
18585 else
18586 add_name_and_src_coords_attributes (lbl_die, decl);
18588 if (DECL_ABSTRACT (decl))
18589 equate_decl_number_to_die (decl, lbl_die);
18590 else
18592 insn = DECL_RTL_IF_SET (decl);
18594 /* Deleted labels are programmer specified labels which have been
18595 eliminated because of various optimizations. We still emit them
18596 here so that it is possible to put breakpoints on them. */
18597 if (insn
18598 && (LABEL_P (insn)
18599 || ((NOTE_P (insn)
18600 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18602 /* When optimization is enabled (via -O) some parts of the compiler
18603 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18604 represent source-level labels which were explicitly declared by
18605 the user. This really shouldn't be happening though, so catch
18606 it if it ever does happen. */
18607 gcc_assert (!INSN_DELETED_P (insn));
18609 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18610 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18612 else if (insn
18613 && NOTE_P (insn)
18614 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18615 && CODE_LABEL_NUMBER (insn) != -1)
18617 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18618 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18623 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18624 attributes to the DIE for a block STMT, to describe where the inlined
18625 function was called from. This is similar to add_src_coords_attributes. */
18627 static inline void
18628 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18630 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18632 if (dwarf_version >= 3 || !dwarf_strict)
18634 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18635 add_AT_unsigned (die, DW_AT_call_line, s.line);
18640 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18641 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18643 static inline void
18644 add_high_low_attributes (tree stmt, dw_die_ref die)
18646 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18648 if (BLOCK_FRAGMENT_CHAIN (stmt)
18649 && (dwarf_version >= 3 || !dwarf_strict))
18651 tree chain, superblock = NULL_TREE;
18652 dw_die_ref pdie;
18653 dw_attr_ref attr = NULL;
18655 if (inlined_function_outer_scope_p (stmt))
18657 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18658 BLOCK_NUMBER (stmt));
18659 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18662 /* Optimize duplicate .debug_ranges lists or even tails of
18663 lists. If this BLOCK has same ranges as its supercontext,
18664 lookup DW_AT_ranges attribute in the supercontext (and
18665 recursively so), verify that the ranges_table contains the
18666 right values and use it instead of adding a new .debug_range. */
18667 for (chain = stmt, pdie = die;
18668 BLOCK_SAME_RANGE (chain);
18669 chain = BLOCK_SUPERCONTEXT (chain))
18671 dw_attr_ref new_attr;
18673 pdie = pdie->die_parent;
18674 if (pdie == NULL)
18675 break;
18676 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18677 break;
18678 new_attr = get_AT (pdie, DW_AT_ranges);
18679 if (new_attr == NULL
18680 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18681 break;
18682 attr = new_attr;
18683 superblock = BLOCK_SUPERCONTEXT (chain);
18685 if (attr != NULL
18686 && (ranges_table[attr->dw_attr_val.v.val_offset
18687 / 2 / DWARF2_ADDR_SIZE].num
18688 == BLOCK_NUMBER (superblock))
18689 && BLOCK_FRAGMENT_CHAIN (superblock))
18691 unsigned long off = attr->dw_attr_val.v.val_offset
18692 / 2 / DWARF2_ADDR_SIZE;
18693 unsigned long supercnt = 0, thiscnt = 0;
18694 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18695 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18697 ++supercnt;
18698 gcc_checking_assert (ranges_table[off + supercnt].num
18699 == BLOCK_NUMBER (chain));
18701 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18702 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18703 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18704 ++thiscnt;
18705 gcc_assert (supercnt >= thiscnt);
18706 add_AT_range_list (die, DW_AT_ranges,
18707 ((off + supercnt - thiscnt)
18708 * 2 * DWARF2_ADDR_SIZE),
18709 false);
18710 return;
18713 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18715 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18718 add_ranges (chain);
18719 chain = BLOCK_FRAGMENT_CHAIN (chain);
18721 while (chain);
18722 add_ranges (NULL);
18724 else
18726 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18727 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18728 BLOCK_NUMBER (stmt));
18729 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18730 BLOCK_NUMBER (stmt));
18731 add_AT_low_high_pc (die, label, label_high, false);
18735 /* Generate a DIE for a lexical block. */
18737 static void
18738 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18740 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18742 if (call_arg_locations)
18744 if (block_map.length () <= BLOCK_NUMBER (stmt))
18745 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18746 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18749 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18750 add_high_low_attributes (stmt, stmt_die);
18752 decls_for_scope (stmt, stmt_die, depth);
18755 /* Generate a DIE for an inlined subprogram. */
18757 static void
18758 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18760 tree decl;
18762 /* The instance of function that is effectively being inlined shall not
18763 be abstract. */
18764 gcc_assert (! BLOCK_ABSTRACT (stmt));
18766 decl = block_ultimate_origin (stmt);
18768 /* Emit info for the abstract instance first, if we haven't yet. We
18769 must emit this even if the block is abstract, otherwise when we
18770 emit the block below (or elsewhere), we may end up trying to emit
18771 a die whose origin die hasn't been emitted, and crashing. */
18772 dwarf2out_abstract_function (decl);
18774 if (! BLOCK_ABSTRACT (stmt))
18776 dw_die_ref subr_die
18777 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18779 if (call_arg_locations)
18781 if (block_map.length () <= BLOCK_NUMBER (stmt))
18782 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18783 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18785 add_abstract_origin_attribute (subr_die, decl);
18786 if (TREE_ASM_WRITTEN (stmt))
18787 add_high_low_attributes (stmt, subr_die);
18788 add_call_src_coords_attributes (stmt, subr_die);
18790 decls_for_scope (stmt, subr_die, depth);
18791 current_function_has_inlines = 1;
18795 /* Generate a DIE for a field in a record, or structure. */
18797 static void
18798 gen_field_die (tree decl, dw_die_ref context_die)
18800 dw_die_ref decl_die;
18802 if (TREE_TYPE (decl) == error_mark_node)
18803 return;
18805 decl_die = new_die (DW_TAG_member, context_die, decl);
18806 add_name_and_src_coords_attributes (decl_die, decl);
18807 add_type_attribute (decl_die, member_declared_type (decl),
18808 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18809 context_die);
18811 if (DECL_BIT_FIELD_TYPE (decl))
18813 add_byte_size_attribute (decl_die, decl);
18814 add_bit_size_attribute (decl_die, decl);
18815 add_bit_offset_attribute (decl_die, decl);
18818 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18819 add_data_member_location_attribute (decl_die, decl);
18821 if (DECL_ARTIFICIAL (decl))
18822 add_AT_flag (decl_die, DW_AT_artificial, 1);
18824 add_accessibility_attribute (decl_die, decl);
18826 /* Equate decl number to die, so that we can look up this decl later on. */
18827 equate_decl_number_to_die (decl, decl_die);
18830 #if 0
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_pointer_type_die (tree type, dw_die_ref context_die)
18839 dw_die_ref ptr_die
18840 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18842 equate_type_number_to_die (type, ptr_die);
18843 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18844 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18847 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18848 Use modified_type_die instead.
18849 We keep this code here just in case these types of DIEs may be needed to
18850 represent certain things in other languages (e.g. Pascal) someday. */
18852 static void
18853 gen_reference_type_die (tree type, dw_die_ref context_die)
18855 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18857 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18858 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18859 else
18860 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18862 equate_type_number_to_die (type, ref_die);
18863 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18864 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18866 #endif
18868 /* Generate a DIE for a pointer to a member type. */
18870 static void
18871 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18873 dw_die_ref ptr_die
18874 = new_die (DW_TAG_ptr_to_member_type,
18875 scope_die_for (type, context_die), type);
18877 equate_type_number_to_die (type, ptr_die);
18878 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18879 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18880 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18883 typedef const char *dchar_p; /* For DEF_VEC_P. */
18885 static char *producer_string;
18887 /* Return a heap allocated producer string including command line options
18888 if -grecord-gcc-switches. */
18890 static char *
18891 gen_producer_string (void)
18893 size_t j;
18894 vec<dchar_p> switches = vNULL;
18895 const char *language_string = lang_hooks.name;
18896 char *producer, *tail;
18897 const char *p;
18898 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18899 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18901 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18902 switch (save_decoded_options[j].opt_index)
18904 case OPT_o:
18905 case OPT_d:
18906 case OPT_dumpbase:
18907 case OPT_dumpdir:
18908 case OPT_auxbase:
18909 case OPT_auxbase_strip:
18910 case OPT_quiet:
18911 case OPT_version:
18912 case OPT_v:
18913 case OPT_w:
18914 case OPT_L:
18915 case OPT_D:
18916 case OPT_I:
18917 case OPT_U:
18918 case OPT_SPECIAL_unknown:
18919 case OPT_SPECIAL_ignore:
18920 case OPT_SPECIAL_program_name:
18921 case OPT_SPECIAL_input_file:
18922 case OPT_grecord_gcc_switches:
18923 case OPT_gno_record_gcc_switches:
18924 case OPT__output_pch_:
18925 case OPT_fdiagnostics_show_location_:
18926 case OPT_fdiagnostics_show_option:
18927 case OPT_fdiagnostics_show_caret:
18928 case OPT_fdiagnostics_color_:
18929 case OPT_fverbose_asm:
18930 case OPT____:
18931 case OPT__sysroot_:
18932 case OPT_nostdinc:
18933 case OPT_nostdinc__:
18934 /* Ignore these. */
18935 continue;
18936 default:
18937 if (cl_options[save_decoded_options[j].opt_index].flags
18938 & CL_NO_DWARF_RECORD)
18939 continue;
18940 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18941 == '-');
18942 switch (save_decoded_options[j].canonical_option[0][1])
18944 case 'M':
18945 case 'i':
18946 case 'W':
18947 continue;
18948 case 'f':
18949 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18950 "dump", 4) == 0)
18951 continue;
18952 break;
18953 default:
18954 break;
18956 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18957 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18958 break;
18961 producer = XNEWVEC (char, plen + 1 + len + 1);
18962 tail = producer;
18963 sprintf (tail, "%s %s", language_string, version_string);
18964 tail += plen;
18966 FOR_EACH_VEC_ELT (switches, j, p)
18968 len = strlen (p);
18969 *tail = ' ';
18970 memcpy (tail + 1, p, len);
18971 tail += len + 1;
18974 *tail = '\0';
18975 switches.release ();
18976 return producer;
18979 /* Generate the DIE for the compilation unit. */
18981 static dw_die_ref
18982 gen_compile_unit_die (const char *filename)
18984 dw_die_ref die;
18985 const char *language_string = lang_hooks.name;
18986 int language;
18988 die = new_die (DW_TAG_compile_unit, NULL, NULL);
18990 if (filename)
18992 add_name_attribute (die, filename);
18993 /* Don't add cwd for <built-in>. */
18994 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18995 add_comp_dir_attribute (die);
18998 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19000 /* If our producer is LTO try to figure out a common language to use
19001 from the global list of translation units. */
19002 if (strcmp (language_string, "GNU GIMPLE") == 0)
19004 unsigned i;
19005 tree t;
19006 const char *common_lang = NULL;
19008 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19010 if (!TRANSLATION_UNIT_LANGUAGE (t))
19011 continue;
19012 if (!common_lang)
19013 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19014 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19016 else if (strncmp (common_lang, "GNU C", 5) == 0
19017 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19018 /* Mixing C and C++ is ok, use C++ in that case. */
19019 common_lang = "GNU C++";
19020 else
19022 /* Fall back to C. */
19023 common_lang = NULL;
19024 break;
19028 if (common_lang)
19029 language_string = common_lang;
19032 language = DW_LANG_C89;
19033 if (strcmp (language_string, "GNU C++") == 0)
19034 language = DW_LANG_C_plus_plus;
19035 else if (strcmp (language_string, "GNU F77") == 0)
19036 language = DW_LANG_Fortran77;
19037 else if (strcmp (language_string, "GNU Pascal") == 0)
19038 language = DW_LANG_Pascal83;
19039 else if (dwarf_version >= 3 || !dwarf_strict)
19041 if (strcmp (language_string, "GNU Ada") == 0)
19042 language = DW_LANG_Ada95;
19043 else if (strcmp (language_string, "GNU Fortran") == 0)
19044 language = DW_LANG_Fortran95;
19045 else if (strcmp (language_string, "GNU Java") == 0)
19046 language = DW_LANG_Java;
19047 else if (strcmp (language_string, "GNU Objective-C") == 0)
19048 language = DW_LANG_ObjC;
19049 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19050 language = DW_LANG_ObjC_plus_plus;
19051 else if (dwarf_version >= 5 || !dwarf_strict)
19053 if (strcmp (language_string, "GNU Go") == 0)
19054 language = DW_LANG_Go;
19057 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19058 else if (strcmp (language_string, "GNU Fortran") == 0)
19059 language = DW_LANG_Fortran90;
19061 add_AT_unsigned (die, DW_AT_language, language);
19063 switch (language)
19065 case DW_LANG_Fortran77:
19066 case DW_LANG_Fortran90:
19067 case DW_LANG_Fortran95:
19068 /* Fortran has case insensitive identifiers and the front-end
19069 lowercases everything. */
19070 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19071 break;
19072 default:
19073 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19074 break;
19076 return die;
19079 /* Generate the DIE for a base class. */
19081 static void
19082 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19084 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19086 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19087 add_data_member_location_attribute (die, binfo);
19089 if (BINFO_VIRTUAL_P (binfo))
19090 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19092 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19093 children, otherwise the default is DW_ACCESS_public. In DWARF2
19094 the default has always been DW_ACCESS_private. */
19095 if (access == access_public_node)
19097 if (dwarf_version == 2
19098 || context_die->die_tag == DW_TAG_class_type)
19099 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19101 else if (access == access_protected_node)
19102 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19103 else if (dwarf_version > 2
19104 && context_die->die_tag != DW_TAG_class_type)
19105 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19108 /* Generate a DIE for a class member. */
19110 static void
19111 gen_member_die (tree type, dw_die_ref context_die)
19113 tree member;
19114 tree binfo = TYPE_BINFO (type);
19115 dw_die_ref child;
19117 /* If this is not an incomplete type, output descriptions of each of its
19118 members. Note that as we output the DIEs necessary to represent the
19119 members of this record or union type, we will also be trying to output
19120 DIEs to represent the *types* of those members. However the `type'
19121 function (above) will specifically avoid generating type DIEs for member
19122 types *within* the list of member DIEs for this (containing) type except
19123 for those types (of members) which are explicitly marked as also being
19124 members of this (containing) type themselves. The g++ front- end can
19125 force any given type to be treated as a member of some other (containing)
19126 type by setting the TYPE_CONTEXT of the given (member) type to point to
19127 the TREE node representing the appropriate (containing) type. */
19129 /* First output info about the base classes. */
19130 if (binfo)
19132 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19133 int i;
19134 tree base;
19136 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19137 gen_inheritance_die (base,
19138 (accesses ? (*accesses)[i] : access_public_node),
19139 context_die);
19142 /* Now output info about the data members and type members. */
19143 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19145 /* If we thought we were generating minimal debug info for TYPE
19146 and then changed our minds, some of the member declarations
19147 may have already been defined. Don't define them again, but
19148 do put them in the right order. */
19150 child = lookup_decl_die (member);
19151 if (child)
19152 splice_child_die (context_die, child);
19153 else
19154 gen_decl_die (member, NULL, context_die);
19157 /* Now output info about the function members (if any). */
19158 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19160 /* Don't include clones in the member list. */
19161 if (DECL_ABSTRACT_ORIGIN (member))
19162 continue;
19164 child = lookup_decl_die (member);
19165 if (child)
19166 splice_child_die (context_die, child);
19167 else
19168 gen_decl_die (member, NULL, context_die);
19172 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19173 is set, we pretend that the type was never defined, so we only get the
19174 member DIEs needed by later specification DIEs. */
19176 static void
19177 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19178 enum debug_info_usage usage)
19180 dw_die_ref type_die = lookup_type_die (type);
19181 dw_die_ref scope_die = 0;
19182 int nested = 0;
19183 int complete = (TYPE_SIZE (type)
19184 && (! TYPE_STUB_DECL (type)
19185 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19186 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19187 complete = complete && should_emit_struct_debug (type, usage);
19189 if (type_die && ! complete)
19190 return;
19192 if (TYPE_CONTEXT (type) != NULL_TREE
19193 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19194 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19195 nested = 1;
19197 scope_die = scope_die_for (type, context_die);
19199 /* Generate child dies for template paramaters. */
19200 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19201 schedule_generic_params_dies_gen (type);
19203 if (! type_die || (nested && is_cu_die (scope_die)))
19204 /* First occurrence of type or toplevel definition of nested class. */
19206 dw_die_ref old_die = type_die;
19208 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19209 ? record_type_tag (type) : DW_TAG_union_type,
19210 scope_die, type);
19211 equate_type_number_to_die (type, type_die);
19212 if (old_die)
19213 add_AT_specification (type_die, old_die);
19214 else
19215 add_name_attribute (type_die, type_tag (type));
19217 else
19218 remove_AT (type_die, DW_AT_declaration);
19220 /* If this type has been completed, then give it a byte_size attribute and
19221 then give a list of members. */
19222 if (complete && !ns_decl)
19224 /* Prevent infinite recursion in cases where the type of some member of
19225 this type is expressed in terms of this type itself. */
19226 TREE_ASM_WRITTEN (type) = 1;
19227 add_byte_size_attribute (type_die, type);
19228 if (TYPE_STUB_DECL (type) != NULL_TREE)
19230 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19231 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19234 /* If the first reference to this type was as the return type of an
19235 inline function, then it may not have a parent. Fix this now. */
19236 if (type_die->die_parent == NULL)
19237 add_child_die (scope_die, type_die);
19239 push_decl_scope (type);
19240 gen_member_die (type, type_die);
19241 pop_decl_scope ();
19243 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19244 if (TYPE_ARTIFICIAL (type))
19245 add_AT_flag (type_die, DW_AT_artificial, 1);
19247 /* GNU extension: Record what type our vtable lives in. */
19248 if (TYPE_VFIELD (type))
19250 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19252 gen_type_die (vtype, context_die);
19253 add_AT_die_ref (type_die, DW_AT_containing_type,
19254 lookup_type_die (vtype));
19257 else
19259 add_AT_flag (type_die, DW_AT_declaration, 1);
19261 /* We don't need to do this for function-local types. */
19262 if (TYPE_STUB_DECL (type)
19263 && ! decl_function_context (TYPE_STUB_DECL (type)))
19264 vec_safe_push (incomplete_types, type);
19267 if (get_AT (type_die, DW_AT_name))
19268 add_pubtype (type, type_die);
19271 /* Generate a DIE for a subroutine _type_. */
19273 static void
19274 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19276 tree return_type = TREE_TYPE (type);
19277 dw_die_ref subr_die
19278 = new_die (DW_TAG_subroutine_type,
19279 scope_die_for (type, context_die), type);
19281 equate_type_number_to_die (type, subr_die);
19282 add_prototyped_attribute (subr_die, type);
19283 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19284 gen_formal_types_die (type, subr_die);
19286 if (get_AT (subr_die, DW_AT_name))
19287 add_pubtype (type, subr_die);
19290 /* Generate a DIE for a type definition. */
19292 static void
19293 gen_typedef_die (tree decl, dw_die_ref context_die)
19295 dw_die_ref type_die;
19296 tree origin;
19298 if (TREE_ASM_WRITTEN (decl))
19299 return;
19301 TREE_ASM_WRITTEN (decl) = 1;
19302 type_die = new_die (DW_TAG_typedef, context_die, decl);
19303 origin = decl_ultimate_origin (decl);
19304 if (origin != NULL)
19305 add_abstract_origin_attribute (type_die, origin);
19306 else
19308 tree type;
19310 add_name_and_src_coords_attributes (type_die, decl);
19311 if (DECL_ORIGINAL_TYPE (decl))
19313 type = DECL_ORIGINAL_TYPE (decl);
19315 gcc_assert (type != TREE_TYPE (decl));
19316 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19318 else
19320 type = TREE_TYPE (decl);
19322 if (is_naming_typedef_decl (TYPE_NAME (type)))
19324 /* Here, we are in the case of decl being a typedef naming
19325 an anonymous type, e.g:
19326 typedef struct {...} foo;
19327 In that case TREE_TYPE (decl) is not a typedef variant
19328 type and TYPE_NAME of the anonymous type is set to the
19329 TYPE_DECL of the typedef. This construct is emitted by
19330 the C++ FE.
19332 TYPE is the anonymous struct named by the typedef
19333 DECL. As we need the DW_AT_type attribute of the
19334 DW_TAG_typedef to point to the DIE of TYPE, let's
19335 generate that DIE right away. add_type_attribute
19336 called below will then pick (via lookup_type_die) that
19337 anonymous struct DIE. */
19338 if (!TREE_ASM_WRITTEN (type))
19339 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19341 /* This is a GNU Extension. We are adding a
19342 DW_AT_linkage_name attribute to the DIE of the
19343 anonymous struct TYPE. The value of that attribute
19344 is the name of the typedef decl naming the anonymous
19345 struct. This greatly eases the work of consumers of
19346 this debug info. */
19347 add_linkage_attr (lookup_type_die (type), decl);
19351 add_type_attribute (type_die, type, TREE_READONLY (decl),
19352 TREE_THIS_VOLATILE (decl), context_die);
19354 if (is_naming_typedef_decl (decl))
19355 /* We want that all subsequent calls to lookup_type_die with
19356 TYPE in argument yield the DW_TAG_typedef we have just
19357 created. */
19358 equate_type_number_to_die (type, type_die);
19360 add_accessibility_attribute (type_die, decl);
19363 if (DECL_ABSTRACT (decl))
19364 equate_decl_number_to_die (decl, type_die);
19366 if (get_AT (type_die, DW_AT_name))
19367 add_pubtype (decl, type_die);
19370 /* Generate a DIE for a struct, class, enum or union type. */
19372 static void
19373 gen_tagged_type_die (tree type,
19374 dw_die_ref context_die,
19375 enum debug_info_usage usage)
19377 int need_pop;
19379 if (type == NULL_TREE
19380 || !is_tagged_type (type))
19381 return;
19383 /* If this is a nested type whose containing class hasn't been written
19384 out yet, writing it out will cover this one, too. This does not apply
19385 to instantiations of member class templates; they need to be added to
19386 the containing class as they are generated. FIXME: This hurts the
19387 idea of combining type decls from multiple TUs, since we can't predict
19388 what set of template instantiations we'll get. */
19389 if (TYPE_CONTEXT (type)
19390 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19391 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19393 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19395 if (TREE_ASM_WRITTEN (type))
19396 return;
19398 /* If that failed, attach ourselves to the stub. */
19399 push_decl_scope (TYPE_CONTEXT (type));
19400 context_die = lookup_type_die (TYPE_CONTEXT (type));
19401 need_pop = 1;
19403 else if (TYPE_CONTEXT (type) != NULL_TREE
19404 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19406 /* If this type is local to a function that hasn't been written
19407 out yet, use a NULL context for now; it will be fixed up in
19408 decls_for_scope. */
19409 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19410 /* A declaration DIE doesn't count; nested types need to go in the
19411 specification. */
19412 if (context_die && is_declaration_die (context_die))
19413 context_die = NULL;
19414 need_pop = 0;
19416 else
19418 context_die = declare_in_namespace (type, context_die);
19419 need_pop = 0;
19422 if (TREE_CODE (type) == ENUMERAL_TYPE)
19424 /* This might have been written out by the call to
19425 declare_in_namespace. */
19426 if (!TREE_ASM_WRITTEN (type))
19427 gen_enumeration_type_die (type, context_die);
19429 else
19430 gen_struct_or_union_type_die (type, context_die, usage);
19432 if (need_pop)
19433 pop_decl_scope ();
19435 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19436 it up if it is ever completed. gen_*_type_die will set it for us
19437 when appropriate. */
19440 /* Generate a type description DIE. */
19442 static void
19443 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19444 enum debug_info_usage usage)
19446 struct array_descr_info info;
19448 if (type == NULL_TREE || type == error_mark_node)
19449 return;
19451 if (TYPE_NAME (type) != NULL_TREE
19452 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19453 && is_redundant_typedef (TYPE_NAME (type))
19454 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19455 /* The DECL of this type is a typedef we don't want to emit debug
19456 info for but we want debug info for its underlying typedef.
19457 This can happen for e.g, the injected-class-name of a C++
19458 type. */
19459 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19461 /* If TYPE is a typedef type variant, let's generate debug info
19462 for the parent typedef which TYPE is a type of. */
19463 if (typedef_variant_p (type))
19465 if (TREE_ASM_WRITTEN (type))
19466 return;
19468 /* Prevent broken recursion; we can't hand off to the same type. */
19469 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19471 /* Give typedefs the right scope. */
19472 context_die = scope_die_for (type, context_die);
19474 TREE_ASM_WRITTEN (type) = 1;
19476 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19477 return;
19480 /* If type is an anonymous tagged type named by a typedef, let's
19481 generate debug info for the typedef. */
19482 if (is_naming_typedef_decl (TYPE_NAME (type)))
19484 /* Use the DIE of the containing namespace as the parent DIE of
19485 the type description DIE we want to generate. */
19486 if (DECL_CONTEXT (TYPE_NAME (type))
19487 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19488 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19490 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19491 return;
19494 /* If this is an array type with hidden descriptor, handle it first. */
19495 if (!TREE_ASM_WRITTEN (type)
19496 && lang_hooks.types.get_array_descr_info
19497 && lang_hooks.types.get_array_descr_info (type, &info)
19498 && (dwarf_version >= 3 || !dwarf_strict))
19500 gen_descr_array_type_die (type, &info, context_die);
19501 TREE_ASM_WRITTEN (type) = 1;
19502 return;
19505 /* We are going to output a DIE to represent the unqualified version
19506 of this type (i.e. without any const or volatile qualifiers) so
19507 get the main variant (i.e. the unqualified version) of this type
19508 now. (Vectors are special because the debugging info is in the
19509 cloned type itself). */
19510 if (TREE_CODE (type) != VECTOR_TYPE)
19511 type = type_main_variant (type);
19513 if (TREE_ASM_WRITTEN (type))
19514 return;
19516 switch (TREE_CODE (type))
19518 case ERROR_MARK:
19519 break;
19521 case POINTER_TYPE:
19522 case REFERENCE_TYPE:
19523 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19524 ensures that the gen_type_die recursion will terminate even if the
19525 type is recursive. Recursive types are possible in Ada. */
19526 /* ??? We could perhaps do this for all types before the switch
19527 statement. */
19528 TREE_ASM_WRITTEN (type) = 1;
19530 /* For these types, all that is required is that we output a DIE (or a
19531 set of DIEs) to represent the "basis" type. */
19532 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19533 DINFO_USAGE_IND_USE);
19534 break;
19536 case OFFSET_TYPE:
19537 /* This code is used for C++ pointer-to-data-member types.
19538 Output a description of the relevant class type. */
19539 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19540 DINFO_USAGE_IND_USE);
19542 /* Output a description of the type of the object pointed to. */
19543 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19544 DINFO_USAGE_IND_USE);
19546 /* Now output a DIE to represent this pointer-to-data-member type
19547 itself. */
19548 gen_ptr_to_mbr_type_die (type, context_die);
19549 break;
19551 case FUNCTION_TYPE:
19552 /* Force out return type (in case it wasn't forced out already). */
19553 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19554 DINFO_USAGE_DIR_USE);
19555 gen_subroutine_type_die (type, context_die);
19556 break;
19558 case METHOD_TYPE:
19559 /* Force out return type (in case it wasn't forced out already). */
19560 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19561 DINFO_USAGE_DIR_USE);
19562 gen_subroutine_type_die (type, context_die);
19563 break;
19565 case ARRAY_TYPE:
19566 gen_array_type_die (type, context_die);
19567 break;
19569 case VECTOR_TYPE:
19570 gen_array_type_die (type, context_die);
19571 break;
19573 case ENUMERAL_TYPE:
19574 case RECORD_TYPE:
19575 case UNION_TYPE:
19576 case QUAL_UNION_TYPE:
19577 gen_tagged_type_die (type, context_die, usage);
19578 return;
19580 case VOID_TYPE:
19581 case INTEGER_TYPE:
19582 case REAL_TYPE:
19583 case FIXED_POINT_TYPE:
19584 case COMPLEX_TYPE:
19585 case BOOLEAN_TYPE:
19586 /* No DIEs needed for fundamental types. */
19587 break;
19589 case NULLPTR_TYPE:
19590 case LANG_TYPE:
19591 /* Just use DW_TAG_unspecified_type. */
19593 dw_die_ref type_die = lookup_type_die (type);
19594 if (type_die == NULL)
19596 tree name = TYPE_NAME (type);
19597 if (TREE_CODE (name) == TYPE_DECL)
19598 name = DECL_NAME (name);
19599 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19600 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19601 equate_type_number_to_die (type, type_die);
19604 break;
19606 default:
19607 gcc_unreachable ();
19610 TREE_ASM_WRITTEN (type) = 1;
19613 static void
19614 gen_type_die (tree type, dw_die_ref context_die)
19616 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19619 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19620 things which are local to the given block. */
19622 static void
19623 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19625 int must_output_die = 0;
19626 bool inlined_func;
19628 /* Ignore blocks that are NULL. */
19629 if (stmt == NULL_TREE)
19630 return;
19632 inlined_func = inlined_function_outer_scope_p (stmt);
19634 /* If the block is one fragment of a non-contiguous block, do not
19635 process the variables, since they will have been done by the
19636 origin block. Do process subblocks. */
19637 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19639 tree sub;
19641 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19642 gen_block_die (sub, context_die, depth + 1);
19644 return;
19647 /* Determine if we need to output any Dwarf DIEs at all to represent this
19648 block. */
19649 if (inlined_func)
19650 /* The outer scopes for inlinings *must* always be represented. We
19651 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19652 must_output_die = 1;
19653 else
19655 /* Determine if this block directly contains any "significant"
19656 local declarations which we will need to output DIEs for. */
19657 if (debug_info_level > DINFO_LEVEL_TERSE)
19658 /* We are not in terse mode so *any* local declaration counts
19659 as being a "significant" one. */
19660 must_output_die = ((BLOCK_VARS (stmt) != NULL
19661 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19662 && (TREE_USED (stmt)
19663 || TREE_ASM_WRITTEN (stmt)
19664 || BLOCK_ABSTRACT (stmt)));
19665 else if ((TREE_USED (stmt)
19666 || TREE_ASM_WRITTEN (stmt)
19667 || BLOCK_ABSTRACT (stmt))
19668 && !dwarf2out_ignore_block (stmt))
19669 must_output_die = 1;
19672 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19673 DIE for any block which contains no significant local declarations at
19674 all. Rather, in such cases we just call `decls_for_scope' so that any
19675 needed Dwarf info for any sub-blocks will get properly generated. Note
19676 that in terse mode, our definition of what constitutes a "significant"
19677 local declaration gets restricted to include only inlined function
19678 instances and local (nested) function definitions. */
19679 if (must_output_die)
19681 if (inlined_func)
19683 /* If STMT block is abstract, that means we have been called
19684 indirectly from dwarf2out_abstract_function.
19685 That function rightfully marks the descendent blocks (of
19686 the abstract function it is dealing with) as being abstract,
19687 precisely to prevent us from emitting any
19688 DW_TAG_inlined_subroutine DIE as a descendent
19689 of an abstract function instance. So in that case, we should
19690 not call gen_inlined_subroutine_die.
19692 Later though, when cgraph asks dwarf2out to emit info
19693 for the concrete instance of the function decl into which
19694 the concrete instance of STMT got inlined, the later will lead
19695 to the generation of a DW_TAG_inlined_subroutine DIE. */
19696 if (! BLOCK_ABSTRACT (stmt))
19697 gen_inlined_subroutine_die (stmt, context_die, depth);
19699 else
19700 gen_lexical_block_die (stmt, context_die, depth);
19702 else
19703 decls_for_scope (stmt, context_die, depth);
19706 /* Process variable DECL (or variable with origin ORIGIN) within
19707 block STMT and add it to CONTEXT_DIE. */
19708 static void
19709 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19711 dw_die_ref die;
19712 tree decl_or_origin = decl ? decl : origin;
19714 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19715 die = lookup_decl_die (decl_or_origin);
19716 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19717 && TYPE_DECL_IS_STUB (decl_or_origin))
19718 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19719 else
19720 die = NULL;
19722 if (die != NULL && die->die_parent == NULL)
19723 add_child_die (context_die, die);
19724 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19725 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19726 stmt, context_die);
19727 else
19728 gen_decl_die (decl, origin, context_die);
19731 /* Generate all of the decls declared within a given scope and (recursively)
19732 all of its sub-blocks. */
19734 static void
19735 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19737 tree decl;
19738 unsigned int i;
19739 tree subblocks;
19741 /* Ignore NULL blocks. */
19742 if (stmt == NULL_TREE)
19743 return;
19745 /* Output the DIEs to represent all of the data objects and typedefs
19746 declared directly within this block but not within any nested
19747 sub-blocks. Also, nested function and tag DIEs have been
19748 generated with a parent of NULL; fix that up now. */
19749 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19750 process_scope_var (stmt, decl, NULL_TREE, context_die);
19751 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19752 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19753 context_die);
19755 /* If we're at -g1, we're not interested in subblocks. */
19756 if (debug_info_level <= DINFO_LEVEL_TERSE)
19757 return;
19759 /* Output the DIEs to represent all sub-blocks (and the items declared
19760 therein) of this block. */
19761 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19762 subblocks != NULL;
19763 subblocks = BLOCK_CHAIN (subblocks))
19764 gen_block_die (subblocks, context_die, depth + 1);
19767 /* Is this a typedef we can avoid emitting? */
19769 static inline int
19770 is_redundant_typedef (const_tree decl)
19772 if (TYPE_DECL_IS_STUB (decl))
19773 return 1;
19775 if (DECL_ARTIFICIAL (decl)
19776 && DECL_CONTEXT (decl)
19777 && is_tagged_type (DECL_CONTEXT (decl))
19778 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19779 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19780 /* Also ignore the artificial member typedef for the class name. */
19781 return 1;
19783 return 0;
19786 /* Return TRUE if TYPE is a typedef that names a type for linkage
19787 purposes. This kind of typedefs is produced by the C++ FE for
19788 constructs like:
19790 typedef struct {...} foo;
19792 In that case, there is no typedef variant type produced for foo.
19793 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19794 struct type. */
19796 static bool
19797 is_naming_typedef_decl (const_tree decl)
19799 if (decl == NULL_TREE
19800 || TREE_CODE (decl) != TYPE_DECL
19801 || !is_tagged_type (TREE_TYPE (decl))
19802 || DECL_IS_BUILTIN (decl)
19803 || is_redundant_typedef (decl)
19804 /* It looks like Ada produces TYPE_DECLs that are very similar
19805 to C++ naming typedefs but that have different
19806 semantics. Let's be specific to c++ for now. */
19807 || !is_cxx ())
19808 return FALSE;
19810 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19811 && TYPE_NAME (TREE_TYPE (decl)) == decl
19812 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19813 != TYPE_NAME (TREE_TYPE (decl))));
19816 /* Returns the DIE for a context. */
19818 static inline dw_die_ref
19819 get_context_die (tree context)
19821 if (context)
19823 /* Find die that represents this context. */
19824 if (TYPE_P (context))
19826 context = TYPE_MAIN_VARIANT (context);
19827 return strip_naming_typedef (context, force_type_die (context));
19829 else
19830 return force_decl_die (context);
19832 return comp_unit_die ();
19835 /* Returns the DIE for decl. A DIE will always be returned. */
19837 static dw_die_ref
19838 force_decl_die (tree decl)
19840 dw_die_ref decl_die;
19841 unsigned saved_external_flag;
19842 tree save_fn = NULL_TREE;
19843 decl_die = lookup_decl_die (decl);
19844 if (!decl_die)
19846 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19848 decl_die = lookup_decl_die (decl);
19849 if (decl_die)
19850 return decl_die;
19852 switch (TREE_CODE (decl))
19854 case FUNCTION_DECL:
19855 /* Clear current_function_decl, so that gen_subprogram_die thinks
19856 that this is a declaration. At this point, we just want to force
19857 declaration die. */
19858 save_fn = current_function_decl;
19859 current_function_decl = NULL_TREE;
19860 gen_subprogram_die (decl, context_die);
19861 current_function_decl = save_fn;
19862 break;
19864 case VAR_DECL:
19865 /* Set external flag to force declaration die. Restore it after
19866 gen_decl_die() call. */
19867 saved_external_flag = DECL_EXTERNAL (decl);
19868 DECL_EXTERNAL (decl) = 1;
19869 gen_decl_die (decl, NULL, context_die);
19870 DECL_EXTERNAL (decl) = saved_external_flag;
19871 break;
19873 case NAMESPACE_DECL:
19874 if (dwarf_version >= 3 || !dwarf_strict)
19875 dwarf2out_decl (decl);
19876 else
19877 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19878 decl_die = comp_unit_die ();
19879 break;
19881 case TRANSLATION_UNIT_DECL:
19882 decl_die = comp_unit_die ();
19883 break;
19885 default:
19886 gcc_unreachable ();
19889 /* We should be able to find the DIE now. */
19890 if (!decl_die)
19891 decl_die = lookup_decl_die (decl);
19892 gcc_assert (decl_die);
19895 return decl_die;
19898 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19899 always returned. */
19901 static dw_die_ref
19902 force_type_die (tree type)
19904 dw_die_ref type_die;
19906 type_die = lookup_type_die (type);
19907 if (!type_die)
19909 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19911 type_die = modified_type_die (type, TYPE_READONLY (type),
19912 TYPE_VOLATILE (type), context_die);
19913 gcc_assert (type_die);
19915 return type_die;
19918 /* Force out any required namespaces to be able to output DECL,
19919 and return the new context_die for it, if it's changed. */
19921 static dw_die_ref
19922 setup_namespace_context (tree thing, dw_die_ref context_die)
19924 tree context = (DECL_P (thing)
19925 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19926 if (context && TREE_CODE (context) == NAMESPACE_DECL)
19927 /* Force out the namespace. */
19928 context_die = force_decl_die (context);
19930 return context_die;
19933 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19934 type) within its namespace, if appropriate.
19936 For compatibility with older debuggers, namespace DIEs only contain
19937 declarations; all definitions are emitted at CU scope. */
19939 static dw_die_ref
19940 declare_in_namespace (tree thing, dw_die_ref context_die)
19942 dw_die_ref ns_context;
19944 if (debug_info_level <= DINFO_LEVEL_TERSE)
19945 return context_die;
19947 /* If this decl is from an inlined function, then don't try to emit it in its
19948 namespace, as we will get confused. It would have already been emitted
19949 when the abstract instance of the inline function was emitted anyways. */
19950 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19951 return context_die;
19953 ns_context = setup_namespace_context (thing, context_die);
19955 if (ns_context != context_die)
19957 if (is_fortran ())
19958 return ns_context;
19959 if (DECL_P (thing))
19960 gen_decl_die (thing, NULL, ns_context);
19961 else
19962 gen_type_die (thing, ns_context);
19964 return context_die;
19967 /* Generate a DIE for a namespace or namespace alias. */
19969 static void
19970 gen_namespace_die (tree decl, dw_die_ref context_die)
19972 dw_die_ref namespace_die;
19974 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19975 they are an alias of. */
19976 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19978 /* Output a real namespace or module. */
19979 context_die = setup_namespace_context (decl, comp_unit_die ());
19980 namespace_die = new_die (is_fortran ()
19981 ? DW_TAG_module : DW_TAG_namespace,
19982 context_die, decl);
19983 /* For Fortran modules defined in different CU don't add src coords. */
19984 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19986 const char *name = dwarf2_name (decl, 0);
19987 if (name)
19988 add_name_attribute (namespace_die, name);
19990 else
19991 add_name_and_src_coords_attributes (namespace_die, decl);
19992 if (DECL_EXTERNAL (decl))
19993 add_AT_flag (namespace_die, DW_AT_declaration, 1);
19994 equate_decl_number_to_die (decl, namespace_die);
19996 else
19998 /* Output a namespace alias. */
20000 /* Force out the namespace we are an alias of, if necessary. */
20001 dw_die_ref origin_die
20002 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20004 if (DECL_FILE_SCOPE_P (decl)
20005 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20006 context_die = setup_namespace_context (decl, comp_unit_die ());
20007 /* Now create the namespace alias DIE. */
20008 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20009 add_name_and_src_coords_attributes (namespace_die, decl);
20010 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20011 equate_decl_number_to_die (decl, namespace_die);
20013 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20014 if (want_pubnames ())
20015 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20018 /* Generate Dwarf debug information for a decl described by DECL.
20019 The return value is currently only meaningful for PARM_DECLs,
20020 for all other decls it returns NULL. */
20022 static dw_die_ref
20023 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20025 tree decl_or_origin = decl ? decl : origin;
20026 tree class_origin = NULL, ultimate_origin;
20028 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20029 return NULL;
20031 switch (TREE_CODE (decl_or_origin))
20033 case ERROR_MARK:
20034 break;
20036 case CONST_DECL:
20037 if (!is_fortran () && !is_ada ())
20039 /* The individual enumerators of an enum type get output when we output
20040 the Dwarf representation of the relevant enum type itself. */
20041 break;
20044 /* Emit its type. */
20045 gen_type_die (TREE_TYPE (decl), context_die);
20047 /* And its containing namespace. */
20048 context_die = declare_in_namespace (decl, context_die);
20050 gen_const_die (decl, context_die);
20051 break;
20053 case FUNCTION_DECL:
20054 /* Don't output any DIEs to represent mere function declarations,
20055 unless they are class members or explicit block externs. */
20056 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20057 && DECL_FILE_SCOPE_P (decl_or_origin)
20058 && (current_function_decl == NULL_TREE
20059 || DECL_ARTIFICIAL (decl_or_origin)))
20060 break;
20062 #if 0
20063 /* FIXME */
20064 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20065 on local redeclarations of global functions. That seems broken. */
20066 if (current_function_decl != decl)
20067 /* This is only a declaration. */;
20068 #endif
20070 /* If we're emitting a clone, emit info for the abstract instance. */
20071 if (origin || DECL_ORIGIN (decl) != decl)
20072 dwarf2out_abstract_function (origin
20073 ? DECL_ORIGIN (origin)
20074 : DECL_ABSTRACT_ORIGIN (decl));
20076 /* If we're emitting an out-of-line copy of an inline function,
20077 emit info for the abstract instance and set up to refer to it. */
20078 else if (cgraph_function_possibly_inlined_p (decl)
20079 && ! DECL_ABSTRACT (decl)
20080 && ! class_or_namespace_scope_p (context_die)
20081 /* dwarf2out_abstract_function won't emit a die if this is just
20082 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20083 that case, because that works only if we have a die. */
20084 && DECL_INITIAL (decl) != NULL_TREE)
20086 dwarf2out_abstract_function (decl);
20087 set_decl_origin_self (decl);
20090 /* Otherwise we're emitting the primary DIE for this decl. */
20091 else if (debug_info_level > DINFO_LEVEL_TERSE)
20093 /* Before we describe the FUNCTION_DECL itself, make sure that we
20094 have its containing type. */
20095 if (!origin)
20096 origin = decl_class_context (decl);
20097 if (origin != NULL_TREE)
20098 gen_type_die (origin, context_die);
20100 /* And its return type. */
20101 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20103 /* And its virtual context. */
20104 if (DECL_VINDEX (decl) != NULL_TREE)
20105 gen_type_die (DECL_CONTEXT (decl), context_die);
20107 /* Make sure we have a member DIE for decl. */
20108 if (origin != NULL_TREE)
20109 gen_type_die_for_member (origin, decl, context_die);
20111 /* And its containing namespace. */
20112 context_die = declare_in_namespace (decl, context_die);
20115 /* Now output a DIE to represent the function itself. */
20116 if (decl)
20117 gen_subprogram_die (decl, context_die);
20118 break;
20120 case TYPE_DECL:
20121 /* If we are in terse mode, don't generate any DIEs to represent any
20122 actual typedefs. */
20123 if (debug_info_level <= DINFO_LEVEL_TERSE)
20124 break;
20126 /* In the special case of a TYPE_DECL node representing the declaration
20127 of some type tag, if the given TYPE_DECL is marked as having been
20128 instantiated from some other (original) TYPE_DECL node (e.g. one which
20129 was generated within the original definition of an inline function) we
20130 used to generate a special (abbreviated) DW_TAG_structure_type,
20131 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20132 should be actually referencing those DIEs, as variable DIEs with that
20133 type would be emitted already in the abstract origin, so it was always
20134 removed during unused type prunning. Don't add anything in this
20135 case. */
20136 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20137 break;
20139 if (is_redundant_typedef (decl))
20140 gen_type_die (TREE_TYPE (decl), context_die);
20141 else
20142 /* Output a DIE to represent the typedef itself. */
20143 gen_typedef_die (decl, context_die);
20144 break;
20146 case LABEL_DECL:
20147 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20148 gen_label_die (decl, context_die);
20149 break;
20151 case VAR_DECL:
20152 case RESULT_DECL:
20153 /* If we are in terse mode, don't generate any DIEs to represent any
20154 variable declarations or definitions. */
20155 if (debug_info_level <= DINFO_LEVEL_TERSE)
20156 break;
20158 /* Output any DIEs that are needed to specify the type of this data
20159 object. */
20160 if (decl_by_reference_p (decl_or_origin))
20161 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20162 else
20163 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20165 /* And its containing type. */
20166 class_origin = decl_class_context (decl_or_origin);
20167 if (class_origin != NULL_TREE)
20168 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20170 /* And its containing namespace. */
20171 context_die = declare_in_namespace (decl_or_origin, context_die);
20173 /* Now output the DIE to represent the data object itself. This gets
20174 complicated because of the possibility that the VAR_DECL really
20175 represents an inlined instance of a formal parameter for an inline
20176 function. */
20177 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20178 if (ultimate_origin != NULL_TREE
20179 && TREE_CODE (ultimate_origin) == PARM_DECL)
20180 gen_formal_parameter_die (decl, origin,
20181 true /* Emit name attribute. */,
20182 context_die);
20183 else
20184 gen_variable_die (decl, origin, context_die);
20185 break;
20187 case FIELD_DECL:
20188 /* Ignore the nameless fields that are used to skip bits but handle C++
20189 anonymous unions and structs. */
20190 if (DECL_NAME (decl) != NULL_TREE
20191 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20192 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20194 gen_type_die (member_declared_type (decl), context_die);
20195 gen_field_die (decl, context_die);
20197 break;
20199 case PARM_DECL:
20200 if (DECL_BY_REFERENCE (decl_or_origin))
20201 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20202 else
20203 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20204 return gen_formal_parameter_die (decl, origin,
20205 true /* Emit name attribute. */,
20206 context_die);
20208 case NAMESPACE_DECL:
20209 case IMPORTED_DECL:
20210 if (dwarf_version >= 3 || !dwarf_strict)
20211 gen_namespace_die (decl, context_die);
20212 break;
20214 default:
20215 /* Probably some frontend-internal decl. Assume we don't care. */
20216 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20217 break;
20220 return NULL;
20223 /* Output debug information for global decl DECL. Called from toplev.c after
20224 compilation proper has finished. */
20226 static void
20227 dwarf2out_global_decl (tree decl)
20229 /* Output DWARF2 information for file-scope tentative data object
20230 declarations, file-scope (extern) function declarations (which
20231 had no corresponding body) and file-scope tagged type declarations
20232 and definitions which have not yet been forced out. */
20233 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20234 dwarf2out_decl (decl);
20237 /* Output debug information for type decl DECL. Called from toplev.c
20238 and from language front ends (to record built-in types). */
20239 static void
20240 dwarf2out_type_decl (tree decl, int local)
20242 if (!local)
20243 dwarf2out_decl (decl);
20246 /* Output debug information for imported module or decl DECL.
20247 NAME is non-NULL name in the lexical block if the decl has been renamed.
20248 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20249 that DECL belongs to.
20250 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20251 static void
20252 dwarf2out_imported_module_or_decl_1 (tree decl,
20253 tree name,
20254 tree lexical_block,
20255 dw_die_ref lexical_block_die)
20257 expanded_location xloc;
20258 dw_die_ref imported_die = NULL;
20259 dw_die_ref at_import_die;
20261 if (TREE_CODE (decl) == IMPORTED_DECL)
20263 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20264 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20265 gcc_assert (decl);
20267 else
20268 xloc = expand_location (input_location);
20270 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20272 at_import_die = force_type_die (TREE_TYPE (decl));
20273 /* For namespace N { typedef void T; } using N::T; base_type_die
20274 returns NULL, but DW_TAG_imported_declaration requires
20275 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20276 if (!at_import_die)
20278 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20279 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20280 at_import_die = lookup_type_die (TREE_TYPE (decl));
20281 gcc_assert (at_import_die);
20284 else
20286 at_import_die = lookup_decl_die (decl);
20287 if (!at_import_die)
20289 /* If we're trying to avoid duplicate debug info, we may not have
20290 emitted the member decl for this field. Emit it now. */
20291 if (TREE_CODE (decl) == FIELD_DECL)
20293 tree type = DECL_CONTEXT (decl);
20295 if (TYPE_CONTEXT (type)
20296 && TYPE_P (TYPE_CONTEXT (type))
20297 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20298 DINFO_USAGE_DIR_USE))
20299 return;
20300 gen_type_die_for_member (type, decl,
20301 get_context_die (TYPE_CONTEXT (type)));
20303 at_import_die = force_decl_die (decl);
20307 if (TREE_CODE (decl) == NAMESPACE_DECL)
20309 if (dwarf_version >= 3 || !dwarf_strict)
20310 imported_die = new_die (DW_TAG_imported_module,
20311 lexical_block_die,
20312 lexical_block);
20313 else
20314 return;
20316 else
20317 imported_die = new_die (DW_TAG_imported_declaration,
20318 lexical_block_die,
20319 lexical_block);
20321 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20322 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20323 if (name)
20324 add_AT_string (imported_die, DW_AT_name,
20325 IDENTIFIER_POINTER (name));
20326 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20329 /* Output debug information for imported module or decl DECL.
20330 NAME is non-NULL name in context if the decl has been renamed.
20331 CHILD is true if decl is one of the renamed decls as part of
20332 importing whole module. */
20334 static void
20335 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20336 bool child)
20338 /* dw_die_ref at_import_die; */
20339 dw_die_ref scope_die;
20341 if (debug_info_level <= DINFO_LEVEL_TERSE)
20342 return;
20344 gcc_assert (decl);
20346 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20347 We need decl DIE for reference and scope die. First, get DIE for the decl
20348 itself. */
20350 /* Get the scope die for decl context. Use comp_unit_die for global module
20351 or decl. If die is not found for non globals, force new die. */
20352 if (context
20353 && TYPE_P (context)
20354 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20355 return;
20357 if (!(dwarf_version >= 3 || !dwarf_strict))
20358 return;
20360 scope_die = get_context_die (context);
20362 if (child)
20364 gcc_assert (scope_die->die_child);
20365 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20366 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20367 scope_die = scope_die->die_child;
20370 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20371 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20375 /* Write the debugging output for DECL. */
20377 void
20378 dwarf2out_decl (tree decl)
20380 dw_die_ref context_die = comp_unit_die ();
20382 switch (TREE_CODE (decl))
20384 case ERROR_MARK:
20385 return;
20387 case FUNCTION_DECL:
20388 /* What we would really like to do here is to filter out all mere
20389 file-scope declarations of file-scope functions which are never
20390 referenced later within this translation unit (and keep all of ones
20391 that *are* referenced later on) but we aren't clairvoyant, so we have
20392 no idea which functions will be referenced in the future (i.e. later
20393 on within the current translation unit). So here we just ignore all
20394 file-scope function declarations which are not also definitions. If
20395 and when the debugger needs to know something about these functions,
20396 it will have to hunt around and find the DWARF information associated
20397 with the definition of the function.
20399 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20400 nodes represent definitions and which ones represent mere
20401 declarations. We have to check DECL_INITIAL instead. That's because
20402 the C front-end supports some weird semantics for "extern inline"
20403 function definitions. These can get inlined within the current
20404 translation unit (and thus, we need to generate Dwarf info for their
20405 abstract instances so that the Dwarf info for the concrete inlined
20406 instances can have something to refer to) but the compiler never
20407 generates any out-of-lines instances of such things (despite the fact
20408 that they *are* definitions).
20410 The important point is that the C front-end marks these "extern
20411 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20412 them anyway. Note that the C++ front-end also plays some similar games
20413 for inline function definitions appearing within include files which
20414 also contain `#pragma interface' pragmas.
20416 If we are called from dwarf2out_abstract_function output a DIE
20417 anyway. We can end up here this way with early inlining and LTO
20418 where the inlined function is output in a different LTRANS unit
20419 or not at all. */
20420 if (DECL_INITIAL (decl) == NULL_TREE
20421 && ! DECL_ABSTRACT (decl))
20422 return;
20424 /* If we're a nested function, initially use a parent of NULL; if we're
20425 a plain function, this will be fixed up in decls_for_scope. If
20426 we're a method, it will be ignored, since we already have a DIE. */
20427 if (decl_function_context (decl)
20428 /* But if we're in terse mode, we don't care about scope. */
20429 && debug_info_level > DINFO_LEVEL_TERSE)
20430 context_die = NULL;
20431 break;
20433 case VAR_DECL:
20434 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20435 declaration and if the declaration was never even referenced from
20436 within this entire compilation unit. We suppress these DIEs in
20437 order to save space in the .debug section (by eliminating entries
20438 which are probably useless). Note that we must not suppress
20439 block-local extern declarations (whether used or not) because that
20440 would screw-up the debugger's name lookup mechanism and cause it to
20441 miss things which really ought to be in scope at a given point. */
20442 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20443 return;
20445 /* For local statics lookup proper context die. */
20446 if (TREE_STATIC (decl)
20447 && DECL_CONTEXT (decl)
20448 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20449 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20451 /* If we are in terse mode, don't generate any DIEs to represent any
20452 variable declarations or definitions. */
20453 if (debug_info_level <= DINFO_LEVEL_TERSE)
20454 return;
20455 break;
20457 case CONST_DECL:
20458 if (debug_info_level <= DINFO_LEVEL_TERSE)
20459 return;
20460 if (!is_fortran () && !is_ada ())
20461 return;
20462 if (TREE_STATIC (decl) && decl_function_context (decl))
20463 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20464 break;
20466 case NAMESPACE_DECL:
20467 case IMPORTED_DECL:
20468 if (debug_info_level <= DINFO_LEVEL_TERSE)
20469 return;
20470 if (lookup_decl_die (decl) != NULL)
20471 return;
20472 break;
20474 case TYPE_DECL:
20475 /* Don't emit stubs for types unless they are needed by other DIEs. */
20476 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20477 return;
20479 /* Don't bother trying to generate any DIEs to represent any of the
20480 normal built-in types for the language we are compiling. */
20481 if (DECL_IS_BUILTIN (decl))
20482 return;
20484 /* If we are in terse mode, don't generate any DIEs for types. */
20485 if (debug_info_level <= DINFO_LEVEL_TERSE)
20486 return;
20488 /* If we're a function-scope tag, initially use a parent of NULL;
20489 this will be fixed up in decls_for_scope. */
20490 if (decl_function_context (decl))
20491 context_die = NULL;
20493 break;
20495 default:
20496 return;
20499 gen_decl_die (decl, NULL, context_die);
20502 /* Write the debugging output for DECL. */
20504 static void
20505 dwarf2out_function_decl (tree decl)
20507 dwarf2out_decl (decl);
20508 call_arg_locations = NULL;
20509 call_arg_loc_last = NULL;
20510 call_site_count = -1;
20511 tail_call_site_count = -1;
20512 block_map.release ();
20513 htab_empty (decl_loc_table);
20514 htab_empty (cached_dw_loc_list_table);
20517 /* Output a marker (i.e. a label) for the beginning of the generated code for
20518 a lexical block. */
20520 static void
20521 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20522 unsigned int blocknum)
20524 switch_to_section (current_function_section ());
20525 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20528 /* Output a marker (i.e. a label) for the end of the generated code for a
20529 lexical block. */
20531 static void
20532 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20534 switch_to_section (current_function_section ());
20535 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20538 /* Returns nonzero if it is appropriate not to emit any debugging
20539 information for BLOCK, because it doesn't contain any instructions.
20541 Don't allow this for blocks with nested functions or local classes
20542 as we would end up with orphans, and in the presence of scheduling
20543 we may end up calling them anyway. */
20545 static bool
20546 dwarf2out_ignore_block (const_tree block)
20548 tree decl;
20549 unsigned int i;
20551 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20552 if (TREE_CODE (decl) == FUNCTION_DECL
20553 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20554 return 0;
20555 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20557 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20558 if (TREE_CODE (decl) == FUNCTION_DECL
20559 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20560 return 0;
20563 return 1;
20566 /* Hash table routines for file_hash. */
20568 static int
20569 file_table_eq (const void *p1_p, const void *p2_p)
20571 const struct dwarf_file_data *const p1 =
20572 (const struct dwarf_file_data *) p1_p;
20573 const char *const p2 = (const char *) p2_p;
20574 return filename_cmp (p1->filename, p2) == 0;
20577 static hashval_t
20578 file_table_hash (const void *p_p)
20580 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20581 return htab_hash_string (p->filename);
20584 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20585 dwarf2out.c) and return its "index". The index of each (known) filename is
20586 just a unique number which is associated with only that one filename. We
20587 need such numbers for the sake of generating labels (in the .debug_sfnames
20588 section) and references to those files numbers (in the .debug_srcinfo
20589 and.debug_macinfo sections). If the filename given as an argument is not
20590 found in our current list, add it to the list and assign it the next
20591 available unique index number. In order to speed up searches, we remember
20592 the index of the filename was looked up last. This handles the majority of
20593 all searches. */
20595 static struct dwarf_file_data *
20596 lookup_filename (const char *file_name)
20598 void ** slot;
20599 struct dwarf_file_data * created;
20601 /* Check to see if the file name that was searched on the previous
20602 call matches this file name. If so, return the index. */
20603 if (file_table_last_lookup
20604 && (file_name == file_table_last_lookup->filename
20605 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20606 return file_table_last_lookup;
20608 /* Didn't match the previous lookup, search the table. */
20609 slot = htab_find_slot_with_hash (file_table, file_name,
20610 htab_hash_string (file_name), INSERT);
20611 if (*slot)
20612 return (struct dwarf_file_data *) *slot;
20614 created = ggc_alloc_dwarf_file_data ();
20615 created->filename = file_name;
20616 created->emitted_number = 0;
20617 *slot = created;
20618 return created;
20621 /* If the assembler will construct the file table, then translate the compiler
20622 internal file table number into the assembler file table number, and emit
20623 a .file directive if we haven't already emitted one yet. The file table
20624 numbers are different because we prune debug info for unused variables and
20625 types, which may include filenames. */
20627 static int
20628 maybe_emit_file (struct dwarf_file_data * fd)
20630 if (! fd->emitted_number)
20632 if (last_emitted_file)
20633 fd->emitted_number = last_emitted_file->emitted_number + 1;
20634 else
20635 fd->emitted_number = 1;
20636 last_emitted_file = fd;
20638 if (DWARF2_ASM_LINE_DEBUG_INFO)
20640 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20641 output_quoted_string (asm_out_file,
20642 remap_debug_filename (fd->filename));
20643 fputc ('\n', asm_out_file);
20647 return fd->emitted_number;
20650 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20651 That generation should happen after function debug info has been
20652 generated. The value of the attribute is the constant value of ARG. */
20654 static void
20655 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20657 die_arg_entry entry;
20659 if (!die || !arg)
20660 return;
20662 if (!tmpl_value_parm_die_table)
20663 vec_alloc (tmpl_value_parm_die_table, 32);
20665 entry.die = die;
20666 entry.arg = arg;
20667 vec_safe_push (tmpl_value_parm_die_table, entry);
20670 /* Return TRUE if T is an instance of generic type, FALSE
20671 otherwise. */
20673 static bool
20674 generic_type_p (tree t)
20676 if (t == NULL_TREE || !TYPE_P (t))
20677 return false;
20678 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20681 /* Schedule the generation of the generic parameter dies for the
20682 instance of generic type T. The proper generation itself is later
20683 done by gen_scheduled_generic_parms_dies. */
20685 static void
20686 schedule_generic_params_dies_gen (tree t)
20688 if (!generic_type_p (t))
20689 return;
20691 if (!generic_type_instances)
20692 vec_alloc (generic_type_instances, 256);
20694 vec_safe_push (generic_type_instances, t);
20697 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20698 by append_entry_to_tmpl_value_parm_die_table. This function must
20699 be called after function DIEs have been generated. */
20701 static void
20702 gen_remaining_tmpl_value_param_die_attribute (void)
20704 if (tmpl_value_parm_die_table)
20706 unsigned i;
20707 die_arg_entry *e;
20709 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20710 tree_add_const_value_attribute (e->die, e->arg);
20714 /* Generate generic parameters DIEs for instances of generic types
20715 that have been previously scheduled by
20716 schedule_generic_params_dies_gen. This function must be called
20717 after all the types of the CU have been laid out. */
20719 static void
20720 gen_scheduled_generic_parms_dies (void)
20722 unsigned i;
20723 tree t;
20725 if (!generic_type_instances)
20726 return;
20728 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20729 if (COMPLETE_TYPE_P (t))
20730 gen_generic_params_dies (t);
20734 /* Replace DW_AT_name for the decl with name. */
20736 static void
20737 dwarf2out_set_name (tree decl, tree name)
20739 dw_die_ref die;
20740 dw_attr_ref attr;
20741 const char *dname;
20743 die = TYPE_SYMTAB_DIE (decl);
20744 if (!die)
20745 return;
20747 dname = dwarf2_name (name, 0);
20748 if (!dname)
20749 return;
20751 attr = get_AT (die, DW_AT_name);
20752 if (attr)
20754 struct indirect_string_node *node;
20756 node = find_AT_string (dname);
20757 /* replace the string. */
20758 attr->dw_attr_val.v.val_str = node;
20761 else
20762 add_name_attribute (die, dname);
20765 /* True if before or during processing of the first function being emitted. */
20766 static bool in_first_function_p = true;
20767 /* True if loc_note during dwarf2out_var_location call might still be
20768 before first real instruction at address equal to .Ltext0. */
20769 static bool maybe_at_text_label_p = true;
20770 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20771 static unsigned int first_loclabel_num_not_at_text_label;
20773 /* Called by the final INSN scan whenever we see a var location. We
20774 use it to drop labels in the right places, and throw the location in
20775 our lookup table. */
20777 static void
20778 dwarf2out_var_location (rtx loc_note)
20780 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20781 struct var_loc_node *newloc;
20782 rtx next_real, next_note;
20783 static const char *last_label;
20784 static const char *last_postcall_label;
20785 static bool last_in_cold_section_p;
20786 static rtx expected_next_loc_note;
20787 tree decl;
20788 bool var_loc_p;
20790 if (!NOTE_P (loc_note))
20792 if (CALL_P (loc_note))
20794 call_site_count++;
20795 if (SIBLING_CALL_P (loc_note))
20796 tail_call_site_count++;
20798 return;
20801 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20802 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20803 return;
20805 /* Optimize processing a large consecutive sequence of location
20806 notes so we don't spend too much time in next_real_insn. If the
20807 next insn is another location note, remember the next_real_insn
20808 calculation for next time. */
20809 next_real = cached_next_real_insn;
20810 if (next_real)
20812 if (expected_next_loc_note != loc_note)
20813 next_real = NULL_RTX;
20816 next_note = NEXT_INSN (loc_note);
20817 if (! next_note
20818 || INSN_DELETED_P (next_note)
20819 || ! NOTE_P (next_note)
20820 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20821 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20822 next_note = NULL_RTX;
20824 if (! next_real)
20825 next_real = next_real_insn (loc_note);
20827 if (next_note)
20829 expected_next_loc_note = next_note;
20830 cached_next_real_insn = next_real;
20832 else
20833 cached_next_real_insn = NULL_RTX;
20835 /* If there are no instructions which would be affected by this note,
20836 don't do anything. */
20837 if (var_loc_p
20838 && next_real == NULL_RTX
20839 && !NOTE_DURING_CALL_P (loc_note))
20840 return;
20842 if (next_real == NULL_RTX)
20843 next_real = get_last_insn ();
20845 /* If there were any real insns between note we processed last time
20846 and this note (or if it is the first note), clear
20847 last_{,postcall_}label so that they are not reused this time. */
20848 if (last_var_location_insn == NULL_RTX
20849 || last_var_location_insn != next_real
20850 || last_in_cold_section_p != in_cold_section_p)
20852 last_label = NULL;
20853 last_postcall_label = NULL;
20856 if (var_loc_p)
20858 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20859 newloc = add_var_loc_to_decl (decl, loc_note,
20860 NOTE_DURING_CALL_P (loc_note)
20861 ? last_postcall_label : last_label);
20862 if (newloc == NULL)
20863 return;
20865 else
20867 decl = NULL_TREE;
20868 newloc = NULL;
20871 /* If there were no real insns between note we processed last time
20872 and this note, use the label we emitted last time. Otherwise
20873 create a new label and emit it. */
20874 if (last_label == NULL)
20876 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20877 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20878 loclabel_num++;
20879 last_label = ggc_strdup (loclabel);
20880 /* See if loclabel might be equal to .Ltext0. If yes,
20881 bump first_loclabel_num_not_at_text_label. */
20882 if (!have_multiple_function_sections
20883 && in_first_function_p
20884 && maybe_at_text_label_p)
20886 static rtx last_start;
20887 rtx insn;
20888 for (insn = loc_note; insn; insn = previous_insn (insn))
20889 if (insn == last_start)
20890 break;
20891 else if (!NONDEBUG_INSN_P (insn))
20892 continue;
20893 else
20895 rtx body = PATTERN (insn);
20896 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
20897 continue;
20898 /* Inline asm could occupy zero bytes. */
20899 else if (GET_CODE (body) == ASM_INPUT
20900 || asm_noperands (body) >= 0)
20901 continue;
20902 #ifdef HAVE_attr_length
20903 else if (get_attr_min_length (insn) == 0)
20904 continue;
20905 #endif
20906 else
20908 /* Assume insn has non-zero length. */
20909 maybe_at_text_label_p = false;
20910 break;
20913 if (maybe_at_text_label_p)
20915 last_start = loc_note;
20916 first_loclabel_num_not_at_text_label = loclabel_num;
20921 if (!var_loc_p)
20923 struct call_arg_loc_node *ca_loc
20924 = ggc_alloc_cleared_call_arg_loc_node ();
20925 rtx prev = prev_real_insn (loc_note), x;
20926 ca_loc->call_arg_loc_note = loc_note;
20927 ca_loc->next = NULL;
20928 ca_loc->label = last_label;
20929 gcc_assert (prev
20930 && (CALL_P (prev)
20931 || (NONJUMP_INSN_P (prev)
20932 && GET_CODE (PATTERN (prev)) == SEQUENCE
20933 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20934 if (!CALL_P (prev))
20935 prev = XVECEXP (PATTERN (prev), 0, 0);
20936 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20937 x = get_call_rtx_from (PATTERN (prev));
20938 if (x)
20940 x = XEXP (XEXP (x, 0), 0);
20941 if (GET_CODE (x) == SYMBOL_REF
20942 && SYMBOL_REF_DECL (x)
20943 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20944 ca_loc->symbol_ref = x;
20946 ca_loc->block = insn_scope (prev);
20947 if (call_arg_locations)
20948 call_arg_loc_last->next = ca_loc;
20949 else
20950 call_arg_locations = ca_loc;
20951 call_arg_loc_last = ca_loc;
20953 else if (!NOTE_DURING_CALL_P (loc_note))
20954 newloc->label = last_label;
20955 else
20957 if (!last_postcall_label)
20959 sprintf (loclabel, "%s-1", last_label);
20960 last_postcall_label = ggc_strdup (loclabel);
20962 newloc->label = last_postcall_label;
20965 last_var_location_insn = next_real;
20966 last_in_cold_section_p = in_cold_section_p;
20969 /* Note in one location list that text section has changed. */
20971 static int
20972 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20974 var_loc_list *list = (var_loc_list *) *slot;
20975 if (list->first)
20976 list->last_before_switch
20977 = list->last->next ? list->last->next : list->last;
20978 return 1;
20981 /* Note in all location lists that text section has changed. */
20983 static void
20984 var_location_switch_text_section (void)
20986 if (decl_loc_table == NULL)
20987 return;
20989 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20992 /* Create a new line number table. */
20994 static dw_line_info_table *
20995 new_line_info_table (void)
20997 dw_line_info_table *table;
20999 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21000 table->file_num = 1;
21001 table->line_num = 1;
21002 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21004 return table;
21007 /* Lookup the "current" table into which we emit line info, so
21008 that we don't have to do it for every source line. */
21010 static void
21011 set_cur_line_info_table (section *sec)
21013 dw_line_info_table *table;
21015 if (sec == text_section)
21016 table = text_section_line_info;
21017 else if (sec == cold_text_section)
21019 table = cold_text_section_line_info;
21020 if (!table)
21022 cold_text_section_line_info = table = new_line_info_table ();
21023 table->end_label = cold_end_label;
21026 else
21028 const char *end_label;
21030 if (flag_reorder_blocks_and_partition)
21032 if (in_cold_section_p)
21033 end_label = crtl->subsections.cold_section_end_label;
21034 else
21035 end_label = crtl->subsections.hot_section_end_label;
21037 else
21039 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21040 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21041 current_function_funcdef_no);
21042 end_label = ggc_strdup (label);
21045 table = new_line_info_table ();
21046 table->end_label = end_label;
21048 vec_safe_push (separate_line_info, table);
21051 if (DWARF2_ASM_LINE_DEBUG_INFO)
21052 table->is_stmt = (cur_line_info_table
21053 ? cur_line_info_table->is_stmt
21054 : DWARF_LINE_DEFAULT_IS_STMT_START);
21055 cur_line_info_table = table;
21059 /* We need to reset the locations at the beginning of each
21060 function. We can't do this in the end_function hook, because the
21061 declarations that use the locations won't have been output when
21062 that hook is called. Also compute have_multiple_function_sections here. */
21064 static void
21065 dwarf2out_begin_function (tree fun)
21067 section *sec = function_section (fun);
21069 if (sec != text_section)
21070 have_multiple_function_sections = true;
21072 if (flag_reorder_blocks_and_partition && !cold_text_section)
21074 gcc_assert (current_function_decl == fun);
21075 cold_text_section = unlikely_text_section ();
21076 switch_to_section (cold_text_section);
21077 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21078 switch_to_section (sec);
21081 dwarf2out_note_section_used ();
21082 call_site_count = 0;
21083 tail_call_site_count = 0;
21085 set_cur_line_info_table (sec);
21088 /* Helper function of dwarf2out_end_function, called only after emitting
21089 the very first function into assembly. Check if some .debug_loc range
21090 might end with a .LVL* label that could be equal to .Ltext0.
21091 In that case we must force using absolute addresses in .debug_loc ranges,
21092 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21093 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21094 list terminator.
21095 Set have_multiple_function_sections to true in that case and
21096 terminate htab traversal. */
21098 static int
21099 find_empty_loc_ranges_at_text_label (void **slot, void *)
21101 var_loc_list *entry;
21102 struct var_loc_node *node;
21104 entry = (var_loc_list *) *slot;
21105 node = entry->first;
21106 if (node && node->next && node->next->label)
21108 unsigned int i;
21109 const char *label = node->next->label;
21110 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21112 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21114 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21115 if (strcmp (label, loclabel) == 0)
21117 have_multiple_function_sections = true;
21118 return 0;
21122 return 1;
21125 /* Hook called after emitting a function into assembly.
21126 This does something only for the very first function emitted. */
21128 static void
21129 dwarf2out_end_function (unsigned int)
21131 if (in_first_function_p
21132 && !have_multiple_function_sections
21133 && first_loclabel_num_not_at_text_label
21134 && decl_loc_table)
21135 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21136 NULL);
21137 in_first_function_p = false;
21138 maybe_at_text_label_p = false;
21141 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21143 static void
21144 push_dw_line_info_entry (dw_line_info_table *table,
21145 enum dw_line_info_opcode opcode, unsigned int val)
21147 dw_line_info_entry e;
21148 e.opcode = opcode;
21149 e.val = val;
21150 vec_safe_push (table->entries, e);
21153 /* Output a label to mark the beginning of a source code line entry
21154 and record information relating to this source line, in
21155 'line_info_table' for later output of the .debug_line section. */
21156 /* ??? The discriminator parameter ought to be unsigned. */
21158 static void
21159 dwarf2out_source_line (unsigned int line, const char *filename,
21160 int discriminator, bool is_stmt)
21162 unsigned int file_num;
21163 dw_line_info_table *table;
21165 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21166 return;
21168 /* The discriminator column was added in dwarf4. Simplify the below
21169 by simply removing it if we're not supposed to output it. */
21170 if (dwarf_version < 4 && dwarf_strict)
21171 discriminator = 0;
21173 table = cur_line_info_table;
21174 file_num = maybe_emit_file (lookup_filename (filename));
21176 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21177 the debugger has used the second (possibly duplicate) line number
21178 at the beginning of the function to mark the end of the prologue.
21179 We could eliminate any other duplicates within the function. For
21180 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21181 that second line number entry. */
21182 /* Recall that this end-of-prologue indication is *not* the same thing
21183 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21184 to which the hook corresponds, follows the last insn that was
21185 emitted by gen_prologue. What we need is to precede the first insn
21186 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21187 insn that corresponds to something the user wrote. These may be
21188 very different locations once scheduling is enabled. */
21190 if (0 && file_num == table->file_num
21191 && line == table->line_num
21192 && discriminator == table->discrim_num
21193 && is_stmt == table->is_stmt)
21194 return;
21196 switch_to_section (current_function_section ());
21198 /* If requested, emit something human-readable. */
21199 if (flag_debug_asm)
21200 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21202 if (DWARF2_ASM_LINE_DEBUG_INFO)
21204 /* Emit the .loc directive understood by GNU as. */
21205 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21206 file_num, line, is_stmt, discriminator */
21207 fputs ("\t.loc ", asm_out_file);
21208 fprint_ul (asm_out_file, file_num);
21209 putc (' ', asm_out_file);
21210 fprint_ul (asm_out_file, line);
21211 putc (' ', asm_out_file);
21212 putc ('0', asm_out_file);
21214 if (is_stmt != table->is_stmt)
21216 fputs (" is_stmt ", asm_out_file);
21217 putc (is_stmt ? '1' : '0', asm_out_file);
21219 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21221 gcc_assert (discriminator > 0);
21222 fputs (" discriminator ", asm_out_file);
21223 fprint_ul (asm_out_file, (unsigned long) discriminator);
21225 putc ('\n', asm_out_file);
21227 else
21229 unsigned int label_num = ++line_info_label_num;
21231 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21233 push_dw_line_info_entry (table, LI_set_address, label_num);
21234 if (file_num != table->file_num)
21235 push_dw_line_info_entry (table, LI_set_file, file_num);
21236 if (discriminator != table->discrim_num)
21237 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21238 if (is_stmt != table->is_stmt)
21239 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21240 push_dw_line_info_entry (table, LI_set_line, line);
21243 table->file_num = file_num;
21244 table->line_num = line;
21245 table->discrim_num = discriminator;
21246 table->is_stmt = is_stmt;
21247 table->in_use = true;
21250 /* Record the beginning of a new source file. */
21252 static void
21253 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21255 if (flag_eliminate_dwarf2_dups)
21257 /* Record the beginning of the file for break_out_includes. */
21258 dw_die_ref bincl_die;
21260 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21261 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21264 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21266 macinfo_entry e;
21267 e.code = DW_MACINFO_start_file;
21268 e.lineno = lineno;
21269 e.info = ggc_strdup (filename);
21270 vec_safe_push (macinfo_table, e);
21274 /* Record the end of a source file. */
21276 static void
21277 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21279 if (flag_eliminate_dwarf2_dups)
21280 /* Record the end of the file for break_out_includes. */
21281 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21283 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21285 macinfo_entry e;
21286 e.code = DW_MACINFO_end_file;
21287 e.lineno = lineno;
21288 e.info = NULL;
21289 vec_safe_push (macinfo_table, e);
21293 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21294 the tail part of the directive line, i.e. the part which is past the
21295 initial whitespace, #, whitespace, directive-name, whitespace part. */
21297 static void
21298 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21299 const char *buffer ATTRIBUTE_UNUSED)
21301 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21303 macinfo_entry e;
21304 /* Insert a dummy first entry to be able to optimize the whole
21305 predefined macro block using DW_MACRO_GNU_transparent_include. */
21306 if (macinfo_table->is_empty () && lineno <= 1)
21308 e.code = 0;
21309 e.lineno = 0;
21310 e.info = NULL;
21311 vec_safe_push (macinfo_table, e);
21313 e.code = DW_MACINFO_define;
21314 e.lineno = lineno;
21315 e.info = ggc_strdup (buffer);
21316 vec_safe_push (macinfo_table, e);
21320 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21321 the tail part of the directive line, i.e. the part which is past the
21322 initial whitespace, #, whitespace, directive-name, whitespace part. */
21324 static void
21325 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21326 const char *buffer ATTRIBUTE_UNUSED)
21328 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21330 macinfo_entry e;
21331 /* Insert a dummy first entry to be able to optimize the whole
21332 predefined macro block using DW_MACRO_GNU_transparent_include. */
21333 if (macinfo_table->is_empty () && lineno <= 1)
21335 e.code = 0;
21336 e.lineno = 0;
21337 e.info = NULL;
21338 vec_safe_push (macinfo_table, e);
21340 e.code = DW_MACINFO_undef;
21341 e.lineno = lineno;
21342 e.info = ggc_strdup (buffer);
21343 vec_safe_push (macinfo_table, e);
21347 /* Helpers to manipulate hash table of CUs. */
21349 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21351 typedef macinfo_entry value_type;
21352 typedef macinfo_entry compare_type;
21353 static inline hashval_t hash (const value_type *);
21354 static inline bool equal (const value_type *, const compare_type *);
21357 inline hashval_t
21358 macinfo_entry_hasher::hash (const value_type *entry)
21360 return htab_hash_string (entry->info);
21363 inline bool
21364 macinfo_entry_hasher::equal (const value_type *entry1,
21365 const compare_type *entry2)
21367 return !strcmp (entry1->info, entry2->info);
21370 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21372 /* Output a single .debug_macinfo entry. */
21374 static void
21375 output_macinfo_op (macinfo_entry *ref)
21377 int file_num;
21378 size_t len;
21379 struct indirect_string_node *node;
21380 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21381 struct dwarf_file_data *fd;
21383 switch (ref->code)
21385 case DW_MACINFO_start_file:
21386 fd = lookup_filename (ref->info);
21387 file_num = maybe_emit_file (fd);
21388 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21389 dw2_asm_output_data_uleb128 (ref->lineno,
21390 "Included from line number %lu",
21391 (unsigned long) ref->lineno);
21392 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21393 break;
21394 case DW_MACINFO_end_file:
21395 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21396 break;
21397 case DW_MACINFO_define:
21398 case DW_MACINFO_undef:
21399 len = strlen (ref->info) + 1;
21400 if (!dwarf_strict
21401 && len > DWARF_OFFSET_SIZE
21402 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21403 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21405 ref->code = ref->code == DW_MACINFO_define
21406 ? DW_MACRO_GNU_define_indirect
21407 : DW_MACRO_GNU_undef_indirect;
21408 output_macinfo_op (ref);
21409 return;
21411 dw2_asm_output_data (1, ref->code,
21412 ref->code == DW_MACINFO_define
21413 ? "Define macro" : "Undefine macro");
21414 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21415 (unsigned long) ref->lineno);
21416 dw2_asm_output_nstring (ref->info, -1, "The macro");
21417 break;
21418 case DW_MACRO_GNU_define_indirect:
21419 case DW_MACRO_GNU_undef_indirect:
21420 node = find_AT_string (ref->info);
21421 gcc_assert (node
21422 && ((node->form == DW_FORM_strp)
21423 || (node->form == DW_FORM_GNU_str_index)));
21424 dw2_asm_output_data (1, ref->code,
21425 ref->code == DW_MACRO_GNU_define_indirect
21426 ? "Define macro indirect"
21427 : "Undefine macro indirect");
21428 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21429 (unsigned long) ref->lineno);
21430 if (node->form == DW_FORM_strp)
21431 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21432 debug_str_section, "The macro: \"%s\"",
21433 ref->info);
21434 else
21435 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21436 ref->info);
21437 break;
21438 case DW_MACRO_GNU_transparent_include:
21439 dw2_asm_output_data (1, ref->code, "Transparent include");
21440 ASM_GENERATE_INTERNAL_LABEL (label,
21441 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21442 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21443 break;
21444 default:
21445 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21446 ASM_COMMENT_START, (unsigned long) ref->code);
21447 break;
21451 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21452 other compilation unit .debug_macinfo sections. IDX is the first
21453 index of a define/undef, return the number of ops that should be
21454 emitted in a comdat .debug_macinfo section and emit
21455 a DW_MACRO_GNU_transparent_include entry referencing it.
21456 If the define/undef entry should be emitted normally, return 0. */
21458 static unsigned
21459 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21460 macinfo_hash_type *macinfo_htab)
21462 macinfo_entry *first, *second, *cur, *inc;
21463 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21464 unsigned char checksum[16];
21465 struct md5_ctx ctx;
21466 char *grp_name, *tail;
21467 const char *base;
21468 unsigned int i, count, encoded_filename_len, linebuf_len;
21469 macinfo_entry **slot;
21471 first = &(*macinfo_table)[idx];
21472 second = &(*macinfo_table)[idx + 1];
21474 /* Optimize only if there are at least two consecutive define/undef ops,
21475 and either all of them are before first DW_MACINFO_start_file
21476 with lineno {0,1} (i.e. predefined macro block), or all of them are
21477 in some included header file. */
21478 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21479 return 0;
21480 if (vec_safe_is_empty (files))
21482 if (first->lineno > 1 || second->lineno > 1)
21483 return 0;
21485 else if (first->lineno == 0)
21486 return 0;
21488 /* Find the last define/undef entry that can be grouped together
21489 with first and at the same time compute md5 checksum of their
21490 codes, linenumbers and strings. */
21491 md5_init_ctx (&ctx);
21492 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21493 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21494 break;
21495 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21496 break;
21497 else
21499 unsigned char code = cur->code;
21500 md5_process_bytes (&code, 1, &ctx);
21501 checksum_uleb128 (cur->lineno, &ctx);
21502 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21504 md5_finish_ctx (&ctx, checksum);
21505 count = i - idx;
21507 /* From the containing include filename (if any) pick up just
21508 usable characters from its basename. */
21509 if (vec_safe_is_empty (files))
21510 base = "";
21511 else
21512 base = lbasename (files->last ().info);
21513 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21514 if (ISIDNUM (base[i]) || base[i] == '.')
21515 encoded_filename_len++;
21516 /* Count . at the end. */
21517 if (encoded_filename_len)
21518 encoded_filename_len++;
21520 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21521 linebuf_len = strlen (linebuf);
21523 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21524 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21525 + 16 * 2 + 1);
21526 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21527 tail = grp_name + 4;
21528 if (encoded_filename_len)
21530 for (i = 0; base[i]; i++)
21531 if (ISIDNUM (base[i]) || base[i] == '.')
21532 *tail++ = base[i];
21533 *tail++ = '.';
21535 memcpy (tail, linebuf, linebuf_len);
21536 tail += linebuf_len;
21537 *tail++ = '.';
21538 for (i = 0; i < 16; i++)
21539 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21541 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21542 in the empty vector entry before the first define/undef. */
21543 inc = &(*macinfo_table)[idx - 1];
21544 inc->code = DW_MACRO_GNU_transparent_include;
21545 inc->lineno = 0;
21546 inc->info = ggc_strdup (grp_name);
21547 if (!macinfo_htab->is_created ())
21548 macinfo_htab->create (10);
21549 /* Avoid emitting duplicates. */
21550 slot = macinfo_htab->find_slot (inc, INSERT);
21551 if (*slot != NULL)
21553 inc->code = 0;
21554 inc->info = NULL;
21555 /* If such an entry has been used before, just emit
21556 a DW_MACRO_GNU_transparent_include op. */
21557 inc = *slot;
21558 output_macinfo_op (inc);
21559 /* And clear all macinfo_entry in the range to avoid emitting them
21560 in the second pass. */
21561 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21563 cur->code = 0;
21564 cur->info = NULL;
21567 else
21569 *slot = inc;
21570 inc->lineno = macinfo_htab->elements ();
21571 output_macinfo_op (inc);
21573 return count;
21576 /* Save any strings needed by the macinfo table in the debug str
21577 table. All strings must be collected into the table by the time
21578 index_string is called. */
21580 static void
21581 save_macinfo_strings (void)
21583 unsigned len;
21584 unsigned i;
21585 macinfo_entry *ref;
21587 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21589 switch (ref->code)
21591 /* Match the logic in output_macinfo_op to decide on
21592 indirect strings. */
21593 case DW_MACINFO_define:
21594 case DW_MACINFO_undef:
21595 len = strlen (ref->info) + 1;
21596 if (!dwarf_strict
21597 && len > DWARF_OFFSET_SIZE
21598 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21599 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21600 set_indirect_string (find_AT_string (ref->info));
21601 break;
21602 case DW_MACRO_GNU_define_indirect:
21603 case DW_MACRO_GNU_undef_indirect:
21604 set_indirect_string (find_AT_string (ref->info));
21605 break;
21606 default:
21607 break;
21612 /* Output macinfo section(s). */
21614 static void
21615 output_macinfo (void)
21617 unsigned i;
21618 unsigned long length = vec_safe_length (macinfo_table);
21619 macinfo_entry *ref;
21620 vec<macinfo_entry, va_gc> *files = NULL;
21621 macinfo_hash_type macinfo_htab;
21623 if (! length)
21624 return;
21626 /* output_macinfo* uses these interchangeably. */
21627 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21628 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21629 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21630 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21632 /* For .debug_macro emit the section header. */
21633 if (!dwarf_strict)
21635 dw2_asm_output_data (2, 4, "DWARF macro version number");
21636 if (DWARF_OFFSET_SIZE == 8)
21637 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21638 else
21639 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21640 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21641 (!dwarf_split_debug_info ? debug_line_section_label
21642 : debug_skeleton_line_section_label),
21643 debug_line_section, NULL);
21646 /* In the first loop, it emits the primary .debug_macinfo section
21647 and after each emitted op the macinfo_entry is cleared.
21648 If a longer range of define/undef ops can be optimized using
21649 DW_MACRO_GNU_transparent_include, the
21650 DW_MACRO_GNU_transparent_include op is emitted and kept in
21651 the vector before the first define/undef in the range and the
21652 whole range of define/undef ops is not emitted and kept. */
21653 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21655 switch (ref->code)
21657 case DW_MACINFO_start_file:
21658 vec_safe_push (files, *ref);
21659 break;
21660 case DW_MACINFO_end_file:
21661 if (!vec_safe_is_empty (files))
21662 files->pop ();
21663 break;
21664 case DW_MACINFO_define:
21665 case DW_MACINFO_undef:
21666 if (!dwarf_strict
21667 && HAVE_COMDAT_GROUP
21668 && vec_safe_length (files) != 1
21669 && i > 0
21670 && i + 1 < length
21671 && (*macinfo_table)[i - 1].code == 0)
21673 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21674 if (count)
21676 i += count - 1;
21677 continue;
21680 break;
21681 case 0:
21682 /* A dummy entry may be inserted at the beginning to be able
21683 to optimize the whole block of predefined macros. */
21684 if (i == 0)
21685 continue;
21686 default:
21687 break;
21689 output_macinfo_op (ref);
21690 ref->info = NULL;
21691 ref->code = 0;
21694 if (!macinfo_htab.is_created ())
21695 return;
21697 macinfo_htab.dispose ();
21699 /* If any DW_MACRO_GNU_transparent_include were used, on those
21700 DW_MACRO_GNU_transparent_include entries terminate the
21701 current chain and switch to a new comdat .debug_macinfo
21702 section and emit the define/undef entries within it. */
21703 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21704 switch (ref->code)
21706 case 0:
21707 continue;
21708 case DW_MACRO_GNU_transparent_include:
21710 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21711 tree comdat_key = get_identifier (ref->info);
21712 /* Terminate the previous .debug_macinfo section. */
21713 dw2_asm_output_data (1, 0, "End compilation unit");
21714 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21715 SECTION_DEBUG
21716 | SECTION_LINKONCE,
21717 comdat_key);
21718 ASM_GENERATE_INTERNAL_LABEL (label,
21719 DEBUG_MACRO_SECTION_LABEL,
21720 ref->lineno);
21721 ASM_OUTPUT_LABEL (asm_out_file, label);
21722 ref->code = 0;
21723 ref->info = NULL;
21724 dw2_asm_output_data (2, 4, "DWARF macro version number");
21725 if (DWARF_OFFSET_SIZE == 8)
21726 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21727 else
21728 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21730 break;
21731 case DW_MACINFO_define:
21732 case DW_MACINFO_undef:
21733 output_macinfo_op (ref);
21734 ref->code = 0;
21735 ref->info = NULL;
21736 break;
21737 default:
21738 gcc_unreachable ();
21742 /* Set up for Dwarf output at the start of compilation. */
21744 static void
21745 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21747 /* Allocate the file_table. */
21748 file_table = htab_create_ggc (50, file_table_hash,
21749 file_table_eq, NULL);
21751 /* Allocate the decl_die_table. */
21752 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21753 decl_die_table_eq, NULL);
21755 /* Allocate the decl_loc_table. */
21756 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21757 decl_loc_table_eq, NULL);
21759 /* Allocate the cached_dw_loc_list_table. */
21760 cached_dw_loc_list_table
21761 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21762 cached_dw_loc_list_table_eq, NULL);
21764 /* Allocate the initial hunk of the decl_scope_table. */
21765 vec_alloc (decl_scope_table, 256);
21767 /* Allocate the initial hunk of the abbrev_die_table. */
21768 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21769 (ABBREV_DIE_TABLE_INCREMENT);
21770 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21771 /* Zero-th entry is allocated, but unused. */
21772 abbrev_die_table_in_use = 1;
21774 /* Allocate the pubtypes and pubnames vectors. */
21775 vec_alloc (pubname_table, 32);
21776 vec_alloc (pubtype_table, 32);
21778 vec_alloc (incomplete_types, 64);
21780 vec_alloc (used_rtx_array, 32);
21782 if (!dwarf_split_debug_info)
21784 debug_info_section = get_section (DEBUG_INFO_SECTION,
21785 SECTION_DEBUG, NULL);
21786 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21787 SECTION_DEBUG, NULL);
21788 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21789 SECTION_DEBUG, NULL);
21791 else
21793 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21794 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21795 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21796 SECTION_DEBUG | SECTION_EXCLUDE,
21797 NULL);
21798 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21799 SECTION_DEBUG, NULL);
21800 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21801 SECTION_DEBUG, NULL);
21802 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21803 SECTION_DEBUG, NULL);
21804 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21805 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21807 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21808 the main .o, but the skeleton_line goes into the split off dwo. */
21809 debug_skeleton_line_section
21810 = get_section (DEBUG_DWO_LINE_SECTION,
21811 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21812 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21813 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21814 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21815 SECTION_DEBUG | SECTION_EXCLUDE,
21816 NULL);
21817 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21818 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21819 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21820 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21821 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
21822 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
21824 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21825 SECTION_DEBUG, NULL);
21826 debug_macinfo_section = get_section (dwarf_strict
21827 ? DEBUG_MACINFO_SECTION
21828 : DEBUG_MACRO_SECTION,
21829 DEBUG_MACRO_SECTION_FLAGS, NULL);
21830 debug_line_section = get_section (DEBUG_LINE_SECTION,
21831 SECTION_DEBUG, NULL);
21832 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21833 SECTION_DEBUG, NULL);
21834 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21835 SECTION_DEBUG, NULL);
21836 debug_str_section = get_section (DEBUG_STR_SECTION,
21837 DEBUG_STR_SECTION_FLAGS, NULL);
21838 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21839 SECTION_DEBUG, NULL);
21840 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21841 SECTION_DEBUG, NULL);
21843 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21844 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21845 DEBUG_ABBREV_SECTION_LABEL, 0);
21846 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21847 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21848 COLD_TEXT_SECTION_LABEL, 0);
21849 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21851 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21852 DEBUG_INFO_SECTION_LABEL, 0);
21853 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21854 DEBUG_LINE_SECTION_LABEL, 0);
21855 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21856 DEBUG_RANGES_SECTION_LABEL, 0);
21857 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21858 DEBUG_ADDR_SECTION_LABEL, 0);
21859 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21860 dwarf_strict
21861 ? DEBUG_MACINFO_SECTION_LABEL
21862 : DEBUG_MACRO_SECTION_LABEL, 0);
21863 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21865 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21866 vec_alloc (macinfo_table, 64);
21868 switch_to_section (text_section);
21869 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21871 /* Make sure the line number table for .text always exists. */
21872 text_section_line_info = new_line_info_table ();
21873 text_section_line_info->end_label = text_end_label;
21876 /* Called before compile () starts outputtting functions, variables
21877 and toplevel asms into assembly. */
21879 static void
21880 dwarf2out_assembly_start (void)
21882 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21883 && dwarf2out_do_cfi_asm ()
21884 && (!(flag_unwind_tables || flag_exceptions)
21885 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21886 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21889 /* A helper function for dwarf2out_finish called through
21890 htab_traverse. Assign a string its index. All strings must be
21891 collected into the table by the time index_string is called,
21892 because the indexing code relies on htab_traverse to traverse nodes
21893 in the same order for each run. */
21895 static int
21896 index_string (void **h, void *v)
21898 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21899 unsigned int *index = (unsigned int *) v;
21901 find_string_form (node);
21902 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21904 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21905 node->index = *index;
21906 *index += 1;
21908 return 1;
21911 /* A helper function for output_indirect_strings called through
21912 htab_traverse. Output the offset to a string and update the
21913 current offset. */
21915 static int
21916 output_index_string_offset (void **h, void *v)
21918 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21919 unsigned int *offset = (unsigned int *) v;
21921 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21923 /* Assert that this node has been assigned an index. */
21924 gcc_assert (node->index != NO_INDEX_ASSIGNED
21925 && node->index != NOT_INDEXED);
21926 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21927 "indexed string 0x%x: %s", node->index, node->str);
21928 *offset += strlen (node->str) + 1;
21930 return 1;
21933 /* A helper function for dwarf2out_finish called through
21934 htab_traverse. Output the indexed string. */
21936 static int
21937 output_index_string (void **h, void *v)
21939 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21940 unsigned int *cur_idx = (unsigned int *) v;
21942 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21944 /* Assert that the strings are output in the same order as their
21945 indexes were assigned. */
21946 gcc_assert (*cur_idx == node->index);
21947 assemble_string (node->str, strlen (node->str) + 1);
21948 *cur_idx += 1;
21950 return 1;
21953 /* A helper function for dwarf2out_finish called through
21954 htab_traverse. Emit one queued .debug_str string. */
21956 static int
21957 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21959 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21961 node->form = find_string_form (node);
21962 if (node->form == DW_FORM_strp && node->refcount > 0)
21964 ASM_OUTPUT_LABEL (asm_out_file, node->label);
21965 assemble_string (node->str, strlen (node->str) + 1);
21968 return 1;
21971 /* Output the indexed string table. */
21973 static void
21974 output_indirect_strings (void)
21976 switch_to_section (debug_str_section);
21977 if (!dwarf_split_debug_info)
21978 htab_traverse (debug_str_hash, output_indirect_string, NULL);
21979 else
21981 unsigned int offset = 0;
21982 unsigned int cur_idx = 0;
21984 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
21986 switch_to_section (debug_str_offsets_section);
21987 htab_traverse_noresize (debug_str_hash,
21988 output_index_string_offset,
21989 &offset);
21990 switch_to_section (debug_str_dwo_section);
21991 htab_traverse_noresize (debug_str_hash,
21992 output_index_string,
21993 &cur_idx);
21997 /* Callback for htab_traverse to assign an index to an entry in the
21998 table, and to write that entry to the .debug_addr section. */
22000 static int
22001 output_addr_table_entry (void **slot, void *data)
22003 addr_table_entry *entry = (addr_table_entry *) *slot;
22004 unsigned int *cur_index = (unsigned int *)data;
22006 if (entry->refcount == 0)
22008 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22009 || entry->index == NOT_INDEXED);
22010 return 1;
22013 gcc_assert (entry->index == *cur_index);
22014 (*cur_index)++;
22016 switch (entry->kind)
22018 case ate_kind_rtx:
22019 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22020 "0x%x", entry->index);
22021 break;
22022 case ate_kind_rtx_dtprel:
22023 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22024 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22025 DWARF2_ADDR_SIZE,
22026 entry->addr.rtl);
22027 fputc ('\n', asm_out_file);
22028 break;
22029 case ate_kind_label:
22030 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22031 "0x%x", entry->index);
22032 break;
22033 default:
22034 gcc_unreachable ();
22036 return 1;
22039 /* Produce the .debug_addr section. */
22041 static void
22042 output_addr_table (void)
22044 unsigned int index = 0;
22045 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22046 return;
22048 switch_to_section (debug_addr_section);
22049 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22052 #if ENABLE_ASSERT_CHECKING
22053 /* Verify that all marks are clear. */
22055 static void
22056 verify_marks_clear (dw_die_ref die)
22058 dw_die_ref c;
22060 gcc_assert (! die->die_mark);
22061 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22063 #endif /* ENABLE_ASSERT_CHECKING */
22065 /* Clear the marks for a die and its children.
22066 Be cool if the mark isn't set. */
22068 static void
22069 prune_unmark_dies (dw_die_ref die)
22071 dw_die_ref c;
22073 if (die->die_mark)
22074 die->die_mark = 0;
22075 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22078 /* Given DIE that we're marking as used, find any other dies
22079 it references as attributes and mark them as used. */
22081 static void
22082 prune_unused_types_walk_attribs (dw_die_ref die)
22084 dw_attr_ref a;
22085 unsigned ix;
22087 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22089 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22091 /* A reference to another DIE.
22092 Make sure that it will get emitted.
22093 If it was broken out into a comdat group, don't follow it. */
22094 if (! AT_ref (a)->comdat_type_p
22095 || a->dw_attr == DW_AT_specification)
22096 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22098 /* Set the string's refcount to 0 so that prune_unused_types_mark
22099 accounts properly for it. */
22100 if (AT_class (a) == dw_val_class_str)
22101 a->dw_attr_val.v.val_str->refcount = 0;
22105 /* Mark the generic parameters and arguments children DIEs of DIE. */
22107 static void
22108 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22110 dw_die_ref c;
22112 if (die == NULL || die->die_child == NULL)
22113 return;
22114 c = die->die_child;
22117 switch (c->die_tag)
22119 case DW_TAG_template_type_param:
22120 case DW_TAG_template_value_param:
22121 case DW_TAG_GNU_template_template_param:
22122 case DW_TAG_GNU_template_parameter_pack:
22123 prune_unused_types_mark (c, 1);
22124 break;
22125 default:
22126 break;
22128 c = c->die_sib;
22129 } while (c && c != die->die_child);
22132 /* Mark DIE as being used. If DOKIDS is true, then walk down
22133 to DIE's children. */
22135 static void
22136 prune_unused_types_mark (dw_die_ref die, int dokids)
22138 dw_die_ref c;
22140 if (die->die_mark == 0)
22142 /* We haven't done this node yet. Mark it as used. */
22143 die->die_mark = 1;
22144 /* If this is the DIE of a generic type instantiation,
22145 mark the children DIEs that describe its generic parms and
22146 args. */
22147 prune_unused_types_mark_generic_parms_dies (die);
22149 /* We also have to mark its parents as used.
22150 (But we don't want to mark our parent's kids due to this,
22151 unless it is a class.) */
22152 if (die->die_parent)
22153 prune_unused_types_mark (die->die_parent,
22154 class_scope_p (die->die_parent));
22156 /* Mark any referenced nodes. */
22157 prune_unused_types_walk_attribs (die);
22159 /* If this node is a specification,
22160 also mark the definition, if it exists. */
22161 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22162 prune_unused_types_mark (die->die_definition, 1);
22165 if (dokids && die->die_mark != 2)
22167 /* We need to walk the children, but haven't done so yet.
22168 Remember that we've walked the kids. */
22169 die->die_mark = 2;
22171 /* If this is an array type, we need to make sure our
22172 kids get marked, even if they're types. If we're
22173 breaking out types into comdat sections, do this
22174 for all type definitions. */
22175 if (die->die_tag == DW_TAG_array_type
22176 || (use_debug_types
22177 && is_type_die (die) && ! is_declaration_die (die)))
22178 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22179 else
22180 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22184 /* For local classes, look if any static member functions were emitted
22185 and if so, mark them. */
22187 static void
22188 prune_unused_types_walk_local_classes (dw_die_ref die)
22190 dw_die_ref c;
22192 if (die->die_mark == 2)
22193 return;
22195 switch (die->die_tag)
22197 case DW_TAG_structure_type:
22198 case DW_TAG_union_type:
22199 case DW_TAG_class_type:
22200 break;
22202 case DW_TAG_subprogram:
22203 if (!get_AT_flag (die, DW_AT_declaration)
22204 || die->die_definition != NULL)
22205 prune_unused_types_mark (die, 1);
22206 return;
22208 default:
22209 return;
22212 /* Mark children. */
22213 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22216 /* Walk the tree DIE and mark types that we actually use. */
22218 static void
22219 prune_unused_types_walk (dw_die_ref die)
22221 dw_die_ref c;
22223 /* Don't do anything if this node is already marked and
22224 children have been marked as well. */
22225 if (die->die_mark == 2)
22226 return;
22228 switch (die->die_tag)
22230 case DW_TAG_structure_type:
22231 case DW_TAG_union_type:
22232 case DW_TAG_class_type:
22233 if (die->die_perennial_p)
22234 break;
22236 for (c = die->die_parent; c; c = c->die_parent)
22237 if (c->die_tag == DW_TAG_subprogram)
22238 break;
22240 /* Finding used static member functions inside of classes
22241 is needed just for local classes, because for other classes
22242 static member function DIEs with DW_AT_specification
22243 are emitted outside of the DW_TAG_*_type. If we ever change
22244 it, we'd need to call this even for non-local classes. */
22245 if (c)
22246 prune_unused_types_walk_local_classes (die);
22248 /* It's a type node --- don't mark it. */
22249 return;
22251 case DW_TAG_const_type:
22252 case DW_TAG_packed_type:
22253 case DW_TAG_pointer_type:
22254 case DW_TAG_reference_type:
22255 case DW_TAG_rvalue_reference_type:
22256 case DW_TAG_volatile_type:
22257 case DW_TAG_typedef:
22258 case DW_TAG_array_type:
22259 case DW_TAG_interface_type:
22260 case DW_TAG_friend:
22261 case DW_TAG_variant_part:
22262 case DW_TAG_enumeration_type:
22263 case DW_TAG_subroutine_type:
22264 case DW_TAG_string_type:
22265 case DW_TAG_set_type:
22266 case DW_TAG_subrange_type:
22267 case DW_TAG_ptr_to_member_type:
22268 case DW_TAG_file_type:
22269 if (die->die_perennial_p)
22270 break;
22272 /* It's a type node --- don't mark it. */
22273 return;
22275 default:
22276 /* Mark everything else. */
22277 break;
22280 if (die->die_mark == 0)
22282 die->die_mark = 1;
22284 /* Now, mark any dies referenced from here. */
22285 prune_unused_types_walk_attribs (die);
22288 die->die_mark = 2;
22290 /* Mark children. */
22291 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22294 /* Increment the string counts on strings referred to from DIE's
22295 attributes. */
22297 static void
22298 prune_unused_types_update_strings (dw_die_ref die)
22300 dw_attr_ref a;
22301 unsigned ix;
22303 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22304 if (AT_class (a) == dw_val_class_str)
22306 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22307 s->refcount++;
22308 /* Avoid unnecessarily putting strings that are used less than
22309 twice in the hash table. */
22310 if (s->refcount
22311 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22313 void ** slot;
22314 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22315 htab_hash_string (s->str),
22316 INSERT);
22317 gcc_assert (*slot == NULL);
22318 *slot = s;
22323 /* Remove from the tree DIE any dies that aren't marked. */
22325 static void
22326 prune_unused_types_prune (dw_die_ref die)
22328 dw_die_ref c;
22330 gcc_assert (die->die_mark);
22331 prune_unused_types_update_strings (die);
22333 if (! die->die_child)
22334 return;
22336 c = die->die_child;
22337 do {
22338 dw_die_ref prev = c;
22339 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22340 if (c == die->die_child)
22342 /* No marked children between 'prev' and the end of the list. */
22343 if (prev == c)
22344 /* No marked children at all. */
22345 die->die_child = NULL;
22346 else
22348 prev->die_sib = c->die_sib;
22349 die->die_child = prev;
22351 return;
22354 if (c != prev->die_sib)
22355 prev->die_sib = c;
22356 prune_unused_types_prune (c);
22357 } while (c != die->die_child);
22360 /* Remove dies representing declarations that we never use. */
22362 static void
22363 prune_unused_types (void)
22365 unsigned int i;
22366 limbo_die_node *node;
22367 comdat_type_node *ctnode;
22368 pubname_ref pub;
22369 dw_die_ref base_type;
22371 #if ENABLE_ASSERT_CHECKING
22372 /* All the marks should already be clear. */
22373 verify_marks_clear (comp_unit_die ());
22374 for (node = limbo_die_list; node; node = node->next)
22375 verify_marks_clear (node->die);
22376 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22377 verify_marks_clear (ctnode->root_die);
22378 #endif /* ENABLE_ASSERT_CHECKING */
22380 /* Mark types that are used in global variables. */
22381 premark_types_used_by_global_vars ();
22383 /* Set the mark on nodes that are actually used. */
22384 prune_unused_types_walk (comp_unit_die ());
22385 for (node = limbo_die_list; node; node = node->next)
22386 prune_unused_types_walk (node->die);
22387 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22389 prune_unused_types_walk (ctnode->root_die);
22390 prune_unused_types_mark (ctnode->type_die, 1);
22393 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22394 are unusual in that they are pubnames that are the children of pubtypes.
22395 They should only be marked via their parent DW_TAG_enumeration_type die,
22396 not as roots in themselves. */
22397 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22398 if (pub->die->die_tag != DW_TAG_enumerator)
22399 prune_unused_types_mark (pub->die, 1);
22400 for (i = 0; base_types.iterate (i, &base_type); i++)
22401 prune_unused_types_mark (base_type, 1);
22403 if (debug_str_hash)
22404 htab_empty (debug_str_hash);
22405 if (skeleton_debug_str_hash)
22406 htab_empty (skeleton_debug_str_hash);
22407 prune_unused_types_prune (comp_unit_die ());
22408 for (node = limbo_die_list; node; node = node->next)
22409 prune_unused_types_prune (node->die);
22410 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22411 prune_unused_types_prune (ctnode->root_die);
22413 /* Leave the marks clear. */
22414 prune_unmark_dies (comp_unit_die ());
22415 for (node = limbo_die_list; node; node = node->next)
22416 prune_unmark_dies (node->die);
22417 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22418 prune_unmark_dies (ctnode->root_die);
22421 /* Set the parameter to true if there are any relative pathnames in
22422 the file table. */
22423 static int
22424 file_table_relative_p (void ** slot, void *param)
22426 bool *p = (bool *) param;
22427 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22428 if (!IS_ABSOLUTE_PATH (d->filename))
22430 *p = true;
22431 return 0;
22433 return 1;
22436 /* Helpers to manipulate hash table of comdat type units. */
22438 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22440 typedef comdat_type_node value_type;
22441 typedef comdat_type_node compare_type;
22442 static inline hashval_t hash (const value_type *);
22443 static inline bool equal (const value_type *, const compare_type *);
22446 inline hashval_t
22447 comdat_type_hasher::hash (const value_type *type_node)
22449 hashval_t h;
22450 memcpy (&h, type_node->signature, sizeof (h));
22451 return h;
22454 inline bool
22455 comdat_type_hasher::equal (const value_type *type_node_1,
22456 const compare_type *type_node_2)
22458 return (! memcmp (type_node_1->signature, type_node_2->signature,
22459 DWARF_TYPE_SIGNATURE_SIZE));
22462 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22463 to the location it would have been added, should we know its
22464 DECL_ASSEMBLER_NAME when we added other attributes. This will
22465 probably improve compactness of debug info, removing equivalent
22466 abbrevs, and hide any differences caused by deferring the
22467 computation of the assembler name, triggered by e.g. PCH. */
22469 static inline void
22470 move_linkage_attr (dw_die_ref die)
22472 unsigned ix = vec_safe_length (die->die_attr);
22473 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22475 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22476 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22478 while (--ix > 0)
22480 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22482 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22483 break;
22486 if (ix != vec_safe_length (die->die_attr) - 1)
22488 die->die_attr->pop ();
22489 die->die_attr->quick_insert (ix, linkage);
22493 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22494 referenced from typed stack ops and count how often they are used. */
22496 static void
22497 mark_base_types (dw_loc_descr_ref loc)
22499 dw_die_ref base_type = NULL;
22501 for (; loc; loc = loc->dw_loc_next)
22503 switch (loc->dw_loc_opc)
22505 case DW_OP_GNU_regval_type:
22506 case DW_OP_GNU_deref_type:
22507 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22508 break;
22509 case DW_OP_GNU_convert:
22510 case DW_OP_GNU_reinterpret:
22511 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22512 continue;
22513 /* FALLTHRU */
22514 case DW_OP_GNU_const_type:
22515 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22516 break;
22517 case DW_OP_GNU_entry_value:
22518 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22519 continue;
22520 default:
22521 continue;
22523 gcc_assert (base_type->die_parent == comp_unit_die ());
22524 if (base_type->die_mark)
22525 base_type->die_mark++;
22526 else
22528 base_types.safe_push (base_type);
22529 base_type->die_mark = 1;
22534 /* Comparison function for sorting marked base types. */
22536 static int
22537 base_type_cmp (const void *x, const void *y)
22539 dw_die_ref dx = *(const dw_die_ref *) x;
22540 dw_die_ref dy = *(const dw_die_ref *) y;
22541 unsigned int byte_size1, byte_size2;
22542 unsigned int encoding1, encoding2;
22543 if (dx->die_mark > dy->die_mark)
22544 return -1;
22545 if (dx->die_mark < dy->die_mark)
22546 return 1;
22547 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22548 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22549 if (byte_size1 < byte_size2)
22550 return 1;
22551 if (byte_size1 > byte_size2)
22552 return -1;
22553 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22554 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22555 if (encoding1 < encoding2)
22556 return 1;
22557 if (encoding1 > encoding2)
22558 return -1;
22559 return 0;
22562 /* Move base types marked by mark_base_types as early as possible
22563 in the CU, sorted by decreasing usage count both to make the
22564 uleb128 references as small as possible and to make sure they
22565 will have die_offset already computed by calc_die_sizes when
22566 sizes of typed stack loc ops is computed. */
22568 static void
22569 move_marked_base_types (void)
22571 unsigned int i;
22572 dw_die_ref base_type, die, c;
22574 if (base_types.is_empty ())
22575 return;
22577 /* Sort by decreasing usage count, they will be added again in that
22578 order later on. */
22579 base_types.qsort (base_type_cmp);
22580 die = comp_unit_die ();
22581 c = die->die_child;
22584 dw_die_ref prev = c;
22585 c = c->die_sib;
22586 while (c->die_mark)
22588 remove_child_with_prev (c, prev);
22589 /* As base types got marked, there must be at least
22590 one node other than DW_TAG_base_type. */
22591 gcc_assert (c != c->die_sib);
22592 c = c->die_sib;
22595 while (c != die->die_child);
22596 gcc_assert (die->die_child);
22597 c = die->die_child;
22598 for (i = 0; base_types.iterate (i, &base_type); i++)
22600 base_type->die_mark = 0;
22601 base_type->die_sib = c->die_sib;
22602 c->die_sib = base_type;
22603 c = base_type;
22607 /* Helper function for resolve_addr, attempt to resolve
22608 one CONST_STRING, return non-zero if not successful. Similarly verify that
22609 SYMBOL_REFs refer to variables emitted in the current CU. */
22611 static int
22612 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22614 rtx rtl = *addr;
22616 if (GET_CODE (rtl) == CONST_STRING)
22618 size_t len = strlen (XSTR (rtl, 0)) + 1;
22619 tree t = build_string (len, XSTR (rtl, 0));
22620 tree tlen = size_int (len - 1);
22621 TREE_TYPE (t)
22622 = build_array_type (char_type_node, build_index_type (tlen));
22623 rtl = lookup_constant_def (t);
22624 if (!rtl || !MEM_P (rtl))
22625 return 1;
22626 rtl = XEXP (rtl, 0);
22627 if (GET_CODE (rtl) == SYMBOL_REF
22628 && SYMBOL_REF_DECL (rtl)
22629 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22630 return 1;
22631 vec_safe_push (used_rtx_array, rtl);
22632 *addr = rtl;
22633 return 0;
22636 if (GET_CODE (rtl) == SYMBOL_REF
22637 && SYMBOL_REF_DECL (rtl))
22639 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22641 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22642 return 1;
22644 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22645 return 1;
22648 if (GET_CODE (rtl) == CONST
22649 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22650 return 1;
22652 return 0;
22655 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22656 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22657 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22659 static rtx
22660 string_cst_pool_decl (tree t)
22662 rtx rtl = output_constant_def (t, 1);
22663 unsigned char *array;
22664 dw_loc_descr_ref l;
22665 tree decl;
22666 size_t len;
22667 dw_die_ref ref;
22669 if (!rtl || !MEM_P (rtl))
22670 return NULL_RTX;
22671 rtl = XEXP (rtl, 0);
22672 if (GET_CODE (rtl) != SYMBOL_REF
22673 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22674 return NULL_RTX;
22676 decl = SYMBOL_REF_DECL (rtl);
22677 if (!lookup_decl_die (decl))
22679 len = TREE_STRING_LENGTH (t);
22680 vec_safe_push (used_rtx_array, rtl);
22681 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22682 array = (unsigned char *) ggc_alloc_atomic (len);
22683 memcpy (array, TREE_STRING_POINTER (t), len);
22684 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22685 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22686 l->dw_loc_oprnd2.v.val_vec.length = len;
22687 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22688 l->dw_loc_oprnd2.v.val_vec.array = array;
22689 add_AT_loc (ref, DW_AT_location, l);
22690 equate_decl_number_to_die (decl, ref);
22692 return rtl;
22695 /* Helper function of resolve_addr_in_expr. LOC is
22696 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22697 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22698 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22699 with DW_OP_GNU_implicit_pointer if possible
22700 and return true, if unsuccesful, return false. */
22702 static bool
22703 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22705 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22706 HOST_WIDE_INT offset = 0;
22707 dw_die_ref ref = NULL;
22708 tree decl;
22710 if (GET_CODE (rtl) == CONST
22711 && GET_CODE (XEXP (rtl, 0)) == PLUS
22712 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22714 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22715 rtl = XEXP (XEXP (rtl, 0), 0);
22717 if (GET_CODE (rtl) == CONST_STRING)
22719 size_t len = strlen (XSTR (rtl, 0)) + 1;
22720 tree t = build_string (len, XSTR (rtl, 0));
22721 tree tlen = size_int (len - 1);
22723 TREE_TYPE (t)
22724 = build_array_type (char_type_node, build_index_type (tlen));
22725 rtl = string_cst_pool_decl (t);
22726 if (!rtl)
22727 return false;
22729 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22731 decl = SYMBOL_REF_DECL (rtl);
22732 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22734 ref = lookup_decl_die (decl);
22735 if (ref && (get_AT (ref, DW_AT_location)
22736 || get_AT (ref, DW_AT_const_value)))
22738 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22739 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22740 loc->dw_loc_oprnd1.val_entry = NULL;
22741 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22742 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22743 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22744 loc->dw_loc_oprnd2.v.val_int = offset;
22745 return true;
22749 return false;
22752 /* Helper function for resolve_addr, handle one location
22753 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22754 the location list couldn't be resolved. */
22756 static bool
22757 resolve_addr_in_expr (dw_loc_descr_ref loc)
22759 dw_loc_descr_ref keep = NULL;
22760 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22761 switch (loc->dw_loc_opc)
22763 case DW_OP_addr:
22764 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22766 if ((prev == NULL
22767 || prev->dw_loc_opc == DW_OP_piece
22768 || prev->dw_loc_opc == DW_OP_bit_piece)
22769 && loc->dw_loc_next
22770 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22771 && !dwarf_strict
22772 && optimize_one_addr_into_implicit_ptr (loc))
22773 break;
22774 return false;
22776 break;
22777 case DW_OP_GNU_addr_index:
22778 case DW_OP_GNU_const_index:
22779 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22780 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22781 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22782 NULL))
22783 return false;
22784 break;
22785 case DW_OP_const4u:
22786 case DW_OP_const8u:
22787 if (loc->dtprel
22788 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22789 return false;
22790 break;
22791 case DW_OP_plus_uconst:
22792 if (size_of_loc_descr (loc)
22793 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22795 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22797 dw_loc_descr_ref repl
22798 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22799 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22800 add_loc_descr (&repl, loc->dw_loc_next);
22801 *loc = *repl;
22803 break;
22804 case DW_OP_implicit_value:
22805 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22806 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22807 return false;
22808 break;
22809 case DW_OP_GNU_implicit_pointer:
22810 case DW_OP_GNU_parameter_ref:
22811 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22813 dw_die_ref ref
22814 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22815 if (ref == NULL)
22816 return false;
22817 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22818 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22819 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22821 break;
22822 case DW_OP_GNU_const_type:
22823 case DW_OP_GNU_regval_type:
22824 case DW_OP_GNU_deref_type:
22825 case DW_OP_GNU_convert:
22826 case DW_OP_GNU_reinterpret:
22827 while (loc->dw_loc_next
22828 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22830 dw_die_ref base1, base2;
22831 unsigned enc1, enc2, size1, size2;
22832 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22833 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22834 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22835 else if (loc->dw_loc_oprnd1.val_class
22836 == dw_val_class_unsigned_const)
22837 break;
22838 else
22839 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22840 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22841 == dw_val_class_unsigned_const)
22842 break;
22843 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22844 gcc_assert (base1->die_tag == DW_TAG_base_type
22845 && base2->die_tag == DW_TAG_base_type);
22846 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22847 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22848 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22849 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22850 if (size1 == size2
22851 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22852 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22853 && loc != keep)
22854 || enc1 == enc2))
22856 /* Optimize away next DW_OP_GNU_convert after
22857 adjusting LOC's base type die reference. */
22858 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22859 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22860 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22861 else
22862 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22863 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22864 continue;
22866 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22867 point typed stack entry. */
22868 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22869 keep = loc->dw_loc_next;
22870 break;
22872 break;
22873 default:
22874 break;
22876 return true;
22879 /* Helper function of resolve_addr. DIE had DW_AT_location of
22880 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
22881 and DW_OP_addr couldn't be resolved. resolve_addr has already
22882 removed the DW_AT_location attribute. This function attempts to
22883 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
22884 to it or DW_AT_const_value attribute, if possible. */
22886 static void
22887 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
22889 if (TREE_CODE (decl) != VAR_DECL
22890 || lookup_decl_die (decl) != die
22891 || DECL_EXTERNAL (decl)
22892 || !TREE_STATIC (decl)
22893 || DECL_INITIAL (decl) == NULL_TREE
22894 || DECL_P (DECL_INITIAL (decl))
22895 || get_AT (die, DW_AT_const_value))
22896 return;
22898 tree init = DECL_INITIAL (decl);
22899 HOST_WIDE_INT offset = 0;
22900 /* For variables that have been optimized away and thus
22901 don't have a memory location, see if we can emit
22902 DW_AT_const_value instead. */
22903 if (tree_add_const_value_attribute (die, init))
22904 return;
22905 if (dwarf_strict)
22906 return;
22907 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
22908 and ADDR_EXPR refers to a decl that has DW_AT_location or
22909 DW_AT_const_value (but isn't addressable, otherwise
22910 resolving the original DW_OP_addr wouldn't fail), see if
22911 we can add DW_OP_GNU_implicit_pointer. */
22912 STRIP_NOPS (init);
22913 if (TREE_CODE (init) == POINTER_PLUS_EXPR
22914 && host_integerp (TREE_OPERAND (init, 1), 0))
22916 offset = tree_low_cst (TREE_OPERAND (init, 1), 0);
22917 init = TREE_OPERAND (init, 0);
22918 STRIP_NOPS (init);
22920 if (TREE_CODE (init) != ADDR_EXPR)
22921 return;
22922 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
22923 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
22924 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
22925 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
22926 && TREE_OPERAND (init, 0) != decl))
22928 dw_die_ref ref;
22929 dw_loc_descr_ref l;
22931 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
22933 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
22934 if (!rtl)
22935 return;
22936 decl = SYMBOL_REF_DECL (rtl);
22938 else
22939 decl = TREE_OPERAND (init, 0);
22940 ref = lookup_decl_die (decl);
22941 if (ref == NULL
22942 || (!get_AT (ref, DW_AT_location)
22943 && !get_AT (ref, DW_AT_const_value)))
22944 return;
22945 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
22946 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22947 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
22948 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
22949 add_AT_loc (die, DW_AT_location, l);
22953 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22954 an address in .rodata section if the string literal is emitted there,
22955 or remove the containing location list or replace DW_AT_const_value
22956 with DW_AT_location and empty location expression, if it isn't found
22957 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
22958 to something that has been emitted in the current CU. */
22960 static void
22961 resolve_addr (dw_die_ref die)
22963 dw_die_ref c;
22964 dw_attr_ref a;
22965 dw_loc_list_ref *curr, *start, loc;
22966 unsigned ix;
22968 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22969 switch (AT_class (a))
22971 case dw_val_class_loc_list:
22972 start = curr = AT_loc_list_ptr (a);
22973 loc = *curr;
22974 gcc_assert (loc);
22975 /* The same list can be referenced more than once. See if we have
22976 already recorded the result from a previous pass. */
22977 if (loc->replaced)
22978 *curr = loc->dw_loc_next;
22979 else if (!loc->resolved_addr)
22981 /* As things stand, we do not expect or allow one die to
22982 reference a suffix of another die's location list chain.
22983 References must be identical or completely separate.
22984 There is therefore no need to cache the result of this
22985 pass on any list other than the first; doing so
22986 would lead to unnecessary writes. */
22987 while (*curr)
22989 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22990 if (!resolve_addr_in_expr ((*curr)->expr))
22992 dw_loc_list_ref next = (*curr)->dw_loc_next;
22993 dw_loc_descr_ref l = (*curr)->expr;
22995 if (next && (*curr)->ll_symbol)
22997 gcc_assert (!next->ll_symbol);
22998 next->ll_symbol = (*curr)->ll_symbol;
23000 if (dwarf_split_debug_info)
23001 remove_loc_list_addr_table_entries (l);
23002 *curr = next;
23004 else
23006 mark_base_types ((*curr)->expr);
23007 curr = &(*curr)->dw_loc_next;
23010 if (loc == *start)
23011 loc->resolved_addr = 1;
23012 else
23014 loc->replaced = 1;
23015 loc->dw_loc_next = *start;
23018 if (!*start)
23020 remove_AT (die, a->dw_attr);
23021 ix--;
23023 break;
23024 case dw_val_class_loc:
23026 dw_loc_descr_ref l = AT_loc (a);
23027 /* For -gdwarf-2 don't attempt to optimize
23028 DW_AT_data_member_location containing
23029 DW_OP_plus_uconst - older consumers might
23030 rely on it being that op instead of a more complex,
23031 but shorter, location description. */
23032 if ((dwarf_version > 2
23033 || a->dw_attr != DW_AT_data_member_location
23034 || l == NULL
23035 || l->dw_loc_opc != DW_OP_plus_uconst
23036 || l->dw_loc_next != NULL)
23037 && !resolve_addr_in_expr (l))
23039 if (dwarf_split_debug_info)
23040 remove_loc_list_addr_table_entries (l);
23041 if (l != NULL
23042 && l->dw_loc_next == NULL
23043 && l->dw_loc_opc == DW_OP_addr
23044 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23045 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23046 && a->dw_attr == DW_AT_location)
23048 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23049 remove_AT (die, a->dw_attr);
23050 ix--;
23051 optimize_location_into_implicit_ptr (die, decl);
23052 break;
23054 remove_AT (die, a->dw_attr);
23055 ix--;
23057 else
23058 mark_base_types (l);
23060 break;
23061 case dw_val_class_addr:
23062 if (a->dw_attr == DW_AT_const_value
23063 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23065 if (AT_index (a) != NOT_INDEXED)
23066 remove_addr_table_entry (a->dw_attr_val.val_entry);
23067 remove_AT (die, a->dw_attr);
23068 ix--;
23070 if (die->die_tag == DW_TAG_GNU_call_site
23071 && a->dw_attr == DW_AT_abstract_origin)
23073 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23074 dw_die_ref tdie = lookup_decl_die (tdecl);
23075 if (tdie == NULL
23076 && DECL_EXTERNAL (tdecl)
23077 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23079 force_decl_die (tdecl);
23080 tdie = lookup_decl_die (tdecl);
23082 if (tdie)
23084 a->dw_attr_val.val_class = dw_val_class_die_ref;
23085 a->dw_attr_val.v.val_die_ref.die = tdie;
23086 a->dw_attr_val.v.val_die_ref.external = 0;
23088 else
23090 if (AT_index (a) != NOT_INDEXED)
23091 remove_addr_table_entry (a->dw_attr_val.val_entry);
23092 remove_AT (die, a->dw_attr);
23093 ix--;
23096 break;
23097 default:
23098 break;
23101 FOR_EACH_CHILD (die, c, resolve_addr (c));
23104 /* Helper routines for optimize_location_lists.
23105 This pass tries to share identical local lists in .debug_loc
23106 section. */
23108 /* Iteratively hash operands of LOC opcode. */
23110 static inline hashval_t
23111 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23113 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23114 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23116 switch (loc->dw_loc_opc)
23118 case DW_OP_const4u:
23119 case DW_OP_const8u:
23120 if (loc->dtprel)
23121 goto hash_addr;
23122 /* FALLTHRU */
23123 case DW_OP_const1u:
23124 case DW_OP_const1s:
23125 case DW_OP_const2u:
23126 case DW_OP_const2s:
23127 case DW_OP_const4s:
23128 case DW_OP_const8s:
23129 case DW_OP_constu:
23130 case DW_OP_consts:
23131 case DW_OP_pick:
23132 case DW_OP_plus_uconst:
23133 case DW_OP_breg0:
23134 case DW_OP_breg1:
23135 case DW_OP_breg2:
23136 case DW_OP_breg3:
23137 case DW_OP_breg4:
23138 case DW_OP_breg5:
23139 case DW_OP_breg6:
23140 case DW_OP_breg7:
23141 case DW_OP_breg8:
23142 case DW_OP_breg9:
23143 case DW_OP_breg10:
23144 case DW_OP_breg11:
23145 case DW_OP_breg12:
23146 case DW_OP_breg13:
23147 case DW_OP_breg14:
23148 case DW_OP_breg15:
23149 case DW_OP_breg16:
23150 case DW_OP_breg17:
23151 case DW_OP_breg18:
23152 case DW_OP_breg19:
23153 case DW_OP_breg20:
23154 case DW_OP_breg21:
23155 case DW_OP_breg22:
23156 case DW_OP_breg23:
23157 case DW_OP_breg24:
23158 case DW_OP_breg25:
23159 case DW_OP_breg26:
23160 case DW_OP_breg27:
23161 case DW_OP_breg28:
23162 case DW_OP_breg29:
23163 case DW_OP_breg30:
23164 case DW_OP_breg31:
23165 case DW_OP_regx:
23166 case DW_OP_fbreg:
23167 case DW_OP_piece:
23168 case DW_OP_deref_size:
23169 case DW_OP_xderef_size:
23170 hash = iterative_hash_object (val1->v.val_int, hash);
23171 break;
23172 case DW_OP_skip:
23173 case DW_OP_bra:
23175 int offset;
23177 gcc_assert (val1->val_class == dw_val_class_loc);
23178 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23179 hash = iterative_hash_object (offset, hash);
23181 break;
23182 case DW_OP_implicit_value:
23183 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23184 switch (val2->val_class)
23186 case dw_val_class_const:
23187 hash = iterative_hash_object (val2->v.val_int, hash);
23188 break;
23189 case dw_val_class_vec:
23191 unsigned int elt_size = val2->v.val_vec.elt_size;
23192 unsigned int len = val2->v.val_vec.length;
23194 hash = iterative_hash_object (elt_size, hash);
23195 hash = iterative_hash_object (len, hash);
23196 hash = iterative_hash (val2->v.val_vec.array,
23197 len * elt_size, hash);
23199 break;
23200 case dw_val_class_const_double:
23201 hash = iterative_hash_object (val2->v.val_double.low, hash);
23202 hash = iterative_hash_object (val2->v.val_double.high, hash);
23203 break;
23204 case dw_val_class_addr:
23205 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23206 break;
23207 default:
23208 gcc_unreachable ();
23210 break;
23211 case DW_OP_bregx:
23212 case DW_OP_bit_piece:
23213 hash = iterative_hash_object (val1->v.val_int, hash);
23214 hash = iterative_hash_object (val2->v.val_int, hash);
23215 break;
23216 case DW_OP_addr:
23217 hash_addr:
23218 if (loc->dtprel)
23220 unsigned char dtprel = 0xd1;
23221 hash = iterative_hash_object (dtprel, hash);
23223 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23224 break;
23225 case DW_OP_GNU_addr_index:
23226 case DW_OP_GNU_const_index:
23228 if (loc->dtprel)
23230 unsigned char dtprel = 0xd1;
23231 hash = iterative_hash_object (dtprel, hash);
23233 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23235 break;
23236 case DW_OP_GNU_implicit_pointer:
23237 hash = iterative_hash_object (val2->v.val_int, hash);
23238 break;
23239 case DW_OP_GNU_entry_value:
23240 hash = hash_loc_operands (val1->v.val_loc, hash);
23241 break;
23242 case DW_OP_GNU_regval_type:
23243 case DW_OP_GNU_deref_type:
23245 unsigned int byte_size
23246 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23247 unsigned int encoding
23248 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23249 hash = iterative_hash_object (val1->v.val_int, hash);
23250 hash = iterative_hash_object (byte_size, hash);
23251 hash = iterative_hash_object (encoding, hash);
23253 break;
23254 case DW_OP_GNU_convert:
23255 case DW_OP_GNU_reinterpret:
23256 if (val1->val_class == dw_val_class_unsigned_const)
23258 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23259 break;
23261 /* FALLTHRU */
23262 case DW_OP_GNU_const_type:
23264 unsigned int byte_size
23265 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23266 unsigned int encoding
23267 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23268 hash = iterative_hash_object (byte_size, hash);
23269 hash = iterative_hash_object (encoding, hash);
23270 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23271 break;
23272 hash = iterative_hash_object (val2->val_class, hash);
23273 switch (val2->val_class)
23275 case dw_val_class_const:
23276 hash = iterative_hash_object (val2->v.val_int, hash);
23277 break;
23278 case dw_val_class_vec:
23280 unsigned int elt_size = val2->v.val_vec.elt_size;
23281 unsigned int len = val2->v.val_vec.length;
23283 hash = iterative_hash_object (elt_size, hash);
23284 hash = iterative_hash_object (len, hash);
23285 hash = iterative_hash (val2->v.val_vec.array,
23286 len * elt_size, hash);
23288 break;
23289 case dw_val_class_const_double:
23290 hash = iterative_hash_object (val2->v.val_double.low, hash);
23291 hash = iterative_hash_object (val2->v.val_double.high, hash);
23292 break;
23293 default:
23294 gcc_unreachable ();
23297 break;
23299 default:
23300 /* Other codes have no operands. */
23301 break;
23303 return hash;
23306 /* Iteratively hash the whole DWARF location expression LOC. */
23308 static inline hashval_t
23309 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23311 dw_loc_descr_ref l;
23312 bool sizes_computed = false;
23313 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23314 size_of_locs (loc);
23316 for (l = loc; l != NULL; l = l->dw_loc_next)
23318 enum dwarf_location_atom opc = l->dw_loc_opc;
23319 hash = iterative_hash_object (opc, hash);
23320 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23322 size_of_locs (loc);
23323 sizes_computed = true;
23325 hash = hash_loc_operands (l, hash);
23327 return hash;
23330 /* Compute hash of the whole location list LIST_HEAD. */
23332 static inline void
23333 hash_loc_list (dw_loc_list_ref list_head)
23335 dw_loc_list_ref curr = list_head;
23336 hashval_t hash = 0;
23338 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23340 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23341 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23342 if (curr->section)
23343 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23344 hash);
23345 hash = hash_locs (curr->expr, hash);
23347 list_head->hash = hash;
23350 /* Return true if X and Y opcodes have the same operands. */
23352 static inline bool
23353 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23355 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23356 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23357 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23358 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23360 switch (x->dw_loc_opc)
23362 case DW_OP_const4u:
23363 case DW_OP_const8u:
23364 if (x->dtprel)
23365 goto hash_addr;
23366 /* FALLTHRU */
23367 case DW_OP_const1u:
23368 case DW_OP_const1s:
23369 case DW_OP_const2u:
23370 case DW_OP_const2s:
23371 case DW_OP_const4s:
23372 case DW_OP_const8s:
23373 case DW_OP_constu:
23374 case DW_OP_consts:
23375 case DW_OP_pick:
23376 case DW_OP_plus_uconst:
23377 case DW_OP_breg0:
23378 case DW_OP_breg1:
23379 case DW_OP_breg2:
23380 case DW_OP_breg3:
23381 case DW_OP_breg4:
23382 case DW_OP_breg5:
23383 case DW_OP_breg6:
23384 case DW_OP_breg7:
23385 case DW_OP_breg8:
23386 case DW_OP_breg9:
23387 case DW_OP_breg10:
23388 case DW_OP_breg11:
23389 case DW_OP_breg12:
23390 case DW_OP_breg13:
23391 case DW_OP_breg14:
23392 case DW_OP_breg15:
23393 case DW_OP_breg16:
23394 case DW_OP_breg17:
23395 case DW_OP_breg18:
23396 case DW_OP_breg19:
23397 case DW_OP_breg20:
23398 case DW_OP_breg21:
23399 case DW_OP_breg22:
23400 case DW_OP_breg23:
23401 case DW_OP_breg24:
23402 case DW_OP_breg25:
23403 case DW_OP_breg26:
23404 case DW_OP_breg27:
23405 case DW_OP_breg28:
23406 case DW_OP_breg29:
23407 case DW_OP_breg30:
23408 case DW_OP_breg31:
23409 case DW_OP_regx:
23410 case DW_OP_fbreg:
23411 case DW_OP_piece:
23412 case DW_OP_deref_size:
23413 case DW_OP_xderef_size:
23414 return valx1->v.val_int == valy1->v.val_int;
23415 case DW_OP_skip:
23416 case DW_OP_bra:
23417 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23418 can cause irrelevant differences in dw_loc_addr. */
23419 gcc_assert (valx1->val_class == dw_val_class_loc
23420 && valy1->val_class == dw_val_class_loc
23421 && (dwarf_split_debug_info
23422 || x->dw_loc_addr == y->dw_loc_addr));
23423 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23424 case DW_OP_implicit_value:
23425 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23426 || valx2->val_class != valy2->val_class)
23427 return false;
23428 switch (valx2->val_class)
23430 case dw_val_class_const:
23431 return valx2->v.val_int == valy2->v.val_int;
23432 case dw_val_class_vec:
23433 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23434 && valx2->v.val_vec.length == valy2->v.val_vec.length
23435 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23436 valx2->v.val_vec.elt_size
23437 * valx2->v.val_vec.length) == 0;
23438 case dw_val_class_const_double:
23439 return valx2->v.val_double.low == valy2->v.val_double.low
23440 && valx2->v.val_double.high == valy2->v.val_double.high;
23441 case dw_val_class_addr:
23442 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23443 default:
23444 gcc_unreachable ();
23446 case DW_OP_bregx:
23447 case DW_OP_bit_piece:
23448 return valx1->v.val_int == valy1->v.val_int
23449 && valx2->v.val_int == valy2->v.val_int;
23450 case DW_OP_addr:
23451 hash_addr:
23452 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23453 case DW_OP_GNU_addr_index:
23454 case DW_OP_GNU_const_index:
23456 rtx ax1 = valx1->val_entry->addr.rtl;
23457 rtx ay1 = valy1->val_entry->addr.rtl;
23458 return rtx_equal_p (ax1, ay1);
23460 case DW_OP_GNU_implicit_pointer:
23461 return valx1->val_class == dw_val_class_die_ref
23462 && valx1->val_class == valy1->val_class
23463 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23464 && valx2->v.val_int == valy2->v.val_int;
23465 case DW_OP_GNU_entry_value:
23466 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23467 case DW_OP_GNU_const_type:
23468 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23469 || valx2->val_class != valy2->val_class)
23470 return false;
23471 switch (valx2->val_class)
23473 case dw_val_class_const:
23474 return valx2->v.val_int == valy2->v.val_int;
23475 case dw_val_class_vec:
23476 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23477 && valx2->v.val_vec.length == valy2->v.val_vec.length
23478 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23479 valx2->v.val_vec.elt_size
23480 * valx2->v.val_vec.length) == 0;
23481 case dw_val_class_const_double:
23482 return valx2->v.val_double.low == valy2->v.val_double.low
23483 && valx2->v.val_double.high == valy2->v.val_double.high;
23484 default:
23485 gcc_unreachable ();
23487 case DW_OP_GNU_regval_type:
23488 case DW_OP_GNU_deref_type:
23489 return valx1->v.val_int == valy1->v.val_int
23490 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23491 case DW_OP_GNU_convert:
23492 case DW_OP_GNU_reinterpret:
23493 if (valx1->val_class != valy1->val_class)
23494 return false;
23495 if (valx1->val_class == dw_val_class_unsigned_const)
23496 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23497 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23498 case DW_OP_GNU_parameter_ref:
23499 return valx1->val_class == dw_val_class_die_ref
23500 && valx1->val_class == valy1->val_class
23501 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23502 default:
23503 /* Other codes have no operands. */
23504 return true;
23508 /* Return true if DWARF location expressions X and Y are the same. */
23510 static inline bool
23511 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23513 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23514 if (x->dw_loc_opc != y->dw_loc_opc
23515 || x->dtprel != y->dtprel
23516 || !compare_loc_operands (x, y))
23517 break;
23518 return x == NULL && y == NULL;
23521 /* Hashtable helpers. */
23523 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23525 typedef dw_loc_list_struct value_type;
23526 typedef dw_loc_list_struct compare_type;
23527 static inline hashval_t hash (const value_type *);
23528 static inline bool equal (const value_type *, const compare_type *);
23531 /* Return precomputed hash of location list X. */
23533 inline hashval_t
23534 loc_list_hasher::hash (const value_type *x)
23536 return x->hash;
23539 /* Return true if location lists A and B are the same. */
23541 inline bool
23542 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23544 if (a == b)
23545 return 1;
23546 if (a->hash != b->hash)
23547 return 0;
23548 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23549 if (strcmp (a->begin, b->begin) != 0
23550 || strcmp (a->end, b->end) != 0
23551 || (a->section == NULL) != (b->section == NULL)
23552 || (a->section && strcmp (a->section, b->section) != 0)
23553 || !compare_locs (a->expr, b->expr))
23554 break;
23555 return a == NULL && b == NULL;
23558 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23561 /* Recursively optimize location lists referenced from DIE
23562 children and share them whenever possible. */
23564 static void
23565 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23567 dw_die_ref c;
23568 dw_attr_ref a;
23569 unsigned ix;
23570 dw_loc_list_struct **slot;
23572 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23573 if (AT_class (a) == dw_val_class_loc_list)
23575 dw_loc_list_ref list = AT_loc_list (a);
23576 /* TODO: perform some optimizations here, before hashing
23577 it and storing into the hash table. */
23578 hash_loc_list (list);
23579 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23580 if (*slot == NULL)
23581 *slot = list;
23582 else
23583 a->dw_attr_val.v.val_loc_list = *slot;
23586 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23590 /* Recursively assign each location list a unique index into the debug_addr
23591 section. */
23593 static void
23594 index_location_lists (dw_die_ref die)
23596 dw_die_ref c;
23597 dw_attr_ref a;
23598 unsigned ix;
23600 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23601 if (AT_class (a) == dw_val_class_loc_list)
23603 dw_loc_list_ref list = AT_loc_list (a);
23604 dw_loc_list_ref curr;
23605 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23607 /* Don't index an entry that has already been indexed
23608 or won't be output. */
23609 if (curr->begin_entry != NULL
23610 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23611 continue;
23613 curr->begin_entry
23614 = add_addr_table_entry (xstrdup (curr->begin),
23615 ate_kind_label);
23619 FOR_EACH_CHILD (die, c, index_location_lists (c));
23622 /* Optimize location lists referenced from DIE
23623 children and share them whenever possible. */
23625 static void
23626 optimize_location_lists (dw_die_ref die)
23628 loc_list_hash_type htab;
23629 htab.create (500);
23630 optimize_location_lists_1 (die, htab);
23631 htab.dispose ();
23634 /* Output stuff that dwarf requires at the end of every file,
23635 and generate the DWARF-2 debugging info. */
23637 static void
23638 dwarf2out_finish (const char *filename)
23640 limbo_die_node *node, *next_node;
23641 comdat_type_node *ctnode;
23642 hash_table <comdat_type_hasher> comdat_type_table;
23643 unsigned int i;
23644 dw_die_ref main_comp_unit_die;
23646 /* PCH might result in DW_AT_producer string being restored from the
23647 header compilation, so always fill it with empty string initially
23648 and overwrite only here. */
23649 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23650 producer_string = gen_producer_string ();
23651 producer->dw_attr_val.v.val_str->refcount--;
23652 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23654 gen_scheduled_generic_parms_dies ();
23655 gen_remaining_tmpl_value_param_die_attribute ();
23657 /* Add the name for the main input file now. We delayed this from
23658 dwarf2out_init to avoid complications with PCH. */
23659 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23660 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23661 add_comp_dir_attribute (comp_unit_die ());
23662 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23664 bool p = false;
23665 htab_traverse (file_table, file_table_relative_p, &p);
23666 if (p)
23667 add_comp_dir_attribute (comp_unit_die ());
23670 if (deferred_locations_list)
23671 for (i = 0; i < deferred_locations_list->length (); i++)
23673 add_location_or_const_value_attribute (
23674 (*deferred_locations_list)[i].die,
23675 (*deferred_locations_list)[i].variable,
23676 false,
23677 DW_AT_location);
23680 /* Traverse the limbo die list, and add parent/child links. The only
23681 dies without parents that should be here are concrete instances of
23682 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23683 For concrete instances, we can get the parent die from the abstract
23684 instance. */
23685 for (node = limbo_die_list; node; node = next_node)
23687 dw_die_ref die = node->die;
23688 next_node = node->next;
23690 if (die->die_parent == NULL)
23692 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23694 if (origin && origin->die_parent)
23695 add_child_die (origin->die_parent, die);
23696 else if (is_cu_die (die))
23698 else if (seen_error ())
23699 /* It's OK to be confused by errors in the input. */
23700 add_child_die (comp_unit_die (), die);
23701 else
23703 /* In certain situations, the lexical block containing a
23704 nested function can be optimized away, which results
23705 in the nested function die being orphaned. Likewise
23706 with the return type of that nested function. Force
23707 this to be a child of the containing function.
23709 It may happen that even the containing function got fully
23710 inlined and optimized out. In that case we are lost and
23711 assign the empty child. This should not be big issue as
23712 the function is likely unreachable too. */
23713 gcc_assert (node->created_for);
23715 if (DECL_P (node->created_for))
23716 origin = get_context_die (DECL_CONTEXT (node->created_for));
23717 else if (TYPE_P (node->created_for))
23718 origin = scope_die_for (node->created_for, comp_unit_die ());
23719 else
23720 origin = comp_unit_die ();
23722 add_child_die (origin, die);
23727 limbo_die_list = NULL;
23729 #if ENABLE_ASSERT_CHECKING
23731 dw_die_ref die = comp_unit_die (), c;
23732 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23734 #endif
23735 resolve_addr (comp_unit_die ());
23736 move_marked_base_types ();
23738 for (node = deferred_asm_name; node; node = node->next)
23740 tree decl = node->created_for;
23741 /* When generating LTO bytecode we can not generate new assembler
23742 names at this point and all important decls got theirs via
23743 free-lang-data. */
23744 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23745 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23747 add_linkage_attr (node->die, decl);
23748 move_linkage_attr (node->die);
23752 deferred_asm_name = NULL;
23754 /* Walk through the list of incomplete types again, trying once more to
23755 emit full debugging info for them. */
23756 retry_incomplete_types ();
23758 if (flag_eliminate_unused_debug_types)
23759 prune_unused_types ();
23761 /* Generate separate COMDAT sections for type DIEs. */
23762 if (use_debug_types)
23764 break_out_comdat_types (comp_unit_die ());
23766 /* Each new type_unit DIE was added to the limbo die list when created.
23767 Since these have all been added to comdat_type_list, clear the
23768 limbo die list. */
23769 limbo_die_list = NULL;
23771 /* For each new comdat type unit, copy declarations for incomplete
23772 types to make the new unit self-contained (i.e., no direct
23773 references to the main compile unit). */
23774 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23775 copy_decls_for_unworthy_types (ctnode->root_die);
23776 copy_decls_for_unworthy_types (comp_unit_die ());
23778 /* In the process of copying declarations from one unit to another,
23779 we may have left some declarations behind that are no longer
23780 referenced. Prune them. */
23781 prune_unused_types ();
23784 /* Generate separate CUs for each of the include files we've seen.
23785 They will go into limbo_die_list. */
23786 if (flag_eliminate_dwarf2_dups)
23787 break_out_includes (comp_unit_die ());
23789 /* Traverse the DIE's and add add sibling attributes to those DIE's
23790 that have children. */
23791 add_sibling_attributes (comp_unit_die ());
23792 for (node = limbo_die_list; node; node = node->next)
23793 add_sibling_attributes (node->die);
23794 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23795 add_sibling_attributes (ctnode->root_die);
23797 /* When splitting DWARF info, we put some attributes in the
23798 skeleton compile_unit DIE that remains in the .o, while
23799 most attributes go in the DWO compile_unit_die. */
23800 if (dwarf_split_debug_info)
23801 main_comp_unit_die = gen_compile_unit_die (NULL);
23802 else
23803 main_comp_unit_die = comp_unit_die ();
23805 /* Output a terminator label for the .text section. */
23806 switch_to_section (text_section);
23807 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23808 if (cold_text_section)
23810 switch_to_section (cold_text_section);
23811 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23814 /* We can only use the low/high_pc attributes if all of the code was
23815 in .text. */
23816 if (!have_multiple_function_sections
23817 || (dwarf_version < 3 && dwarf_strict))
23819 /* Don't add if the CU has no associated code. */
23820 if (text_section_used)
23821 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23822 text_end_label, true);
23824 else
23826 unsigned fde_idx;
23827 dw_fde_ref fde;
23828 bool range_list_added = false;
23830 if (text_section_used)
23831 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23832 text_end_label, &range_list_added, true);
23833 if (cold_text_section_used)
23834 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23835 cold_end_label, &range_list_added, true);
23837 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23839 if (DECL_IGNORED_P (fde->decl))
23840 continue;
23841 if (!fde->in_std_section)
23842 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23843 fde->dw_fde_end, &range_list_added,
23844 true);
23845 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23846 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23847 fde->dw_fde_second_end, &range_list_added,
23848 true);
23851 if (range_list_added)
23853 /* We need to give .debug_loc and .debug_ranges an appropriate
23854 "base address". Use zero so that these addresses become
23855 absolute. Historically, we've emitted the unexpected
23856 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23857 Emit both to give time for other tools to adapt. */
23858 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23859 if (! dwarf_strict && dwarf_version < 4)
23860 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23862 add_ranges (NULL);
23866 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23867 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23868 debug_line_section_label);
23870 if (have_macinfo)
23871 add_AT_macptr (comp_unit_die (),
23872 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23873 macinfo_section_label);
23875 if (dwarf_split_debug_info && addr_index_table != NULL)
23877 /* optimize_location_lists calculates the size of the lists,
23878 so index them first, and assign indices to the entries.
23879 Although optimize_location_lists will remove entries from
23880 the table, it only does so for duplicates, and therefore
23881 only reduces ref_counts to 1. */
23882 unsigned int index = 0;
23883 index_location_lists (comp_unit_die ());
23884 htab_traverse_noresize (addr_index_table,
23885 index_addr_table_entry, &index);
23887 if (have_location_lists)
23888 optimize_location_lists (comp_unit_die ());
23890 save_macinfo_strings ();
23892 if (dwarf_split_debug_info)
23894 unsigned int index = 0;
23896 /* Add attributes common to skeleton compile_units and
23897 type_units. Because these attributes include strings, it
23898 must be done before freezing the string table. Top-level
23899 skeleton die attrs are added when the skeleton type unit is
23900 created, so ensure it is created by this point. */
23901 add_top_level_skeleton_die_attrs (main_comp_unit_die);
23902 (void) get_skeleton_type_unit ();
23903 htab_traverse_noresize (debug_str_hash, index_string, &index);
23906 /* Output all of the compilation units. We put the main one last so that
23907 the offsets are available to output_pubnames. */
23908 for (node = limbo_die_list; node; node = node->next)
23909 output_comp_unit (node->die, 0);
23911 comdat_type_table.create (100);
23912 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23914 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
23916 /* Don't output duplicate types. */
23917 if (*slot != HTAB_EMPTY_ENTRY)
23918 continue;
23920 /* Add a pointer to the line table for the main compilation unit
23921 so that the debugger can make sense of DW_AT_decl_file
23922 attributes. */
23923 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23924 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23925 (!dwarf_split_debug_info
23926 ? debug_line_section_label
23927 : debug_skeleton_line_section_label));
23929 output_comdat_type_unit (ctnode);
23930 *slot = ctnode;
23932 comdat_type_table.dispose ();
23934 /* The AT_pubnames attribute needs to go in all skeleton dies, including
23935 both the main_cu and all skeleton TUs. Making this call unconditional
23936 would end up either adding a second copy of the AT_pubnames attribute, or
23937 requiring a special case in add_top_level_skeleton_die_attrs. */
23938 if (!dwarf_split_debug_info)
23939 add_AT_pubnames (comp_unit_die ());
23941 if (dwarf_split_debug_info)
23943 int mark;
23944 unsigned char checksum[16];
23945 struct md5_ctx ctx;
23947 /* Compute a checksum of the comp_unit to use as the dwo_id. */
23948 md5_init_ctx (&ctx);
23949 mark = 0;
23950 die_checksum (comp_unit_die (), &ctx, &mark);
23951 unmark_all_dies (comp_unit_die ());
23952 md5_finish_ctx (&ctx, checksum);
23954 /* Use the first 8 bytes of the checksum as the dwo_id,
23955 and add it to both comp-unit DIEs. */
23956 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23957 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23959 /* Add the base offset of the ranges table to the skeleton
23960 comp-unit DIE. */
23961 if (ranges_table_in_use)
23962 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23963 ranges_section_label);
23965 switch_to_section (debug_addr_section);
23966 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23967 output_addr_table ();
23970 /* Output the main compilation unit if non-empty or if .debug_macinfo
23971 or .debug_macro will be emitted. */
23972 output_comp_unit (comp_unit_die (), have_macinfo);
23974 if (dwarf_split_debug_info && info_section_emitted)
23975 output_skeleton_debug_sections (main_comp_unit_die);
23977 /* Output the abbreviation table. */
23978 if (abbrev_die_table_in_use != 1)
23980 switch_to_section (debug_abbrev_section);
23981 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23982 output_abbrev_section ();
23985 /* Output location list section if necessary. */
23986 if (have_location_lists)
23988 /* Output the location lists info. */
23989 switch_to_section (debug_loc_section);
23990 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23991 output_location_lists (comp_unit_die ());
23994 /* Output public names and types tables if necessary. */
23995 output_pubnames (pubname_table);
23996 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23997 It shouldn't hurt to emit it always, since pure DWARF2 consumers
23998 simply won't look for the section. */
23999 output_pubnames (pubtype_table);
24001 /* Output the address range information if a CU (.debug_info section)
24002 was emitted. We output an empty table even if we had no functions
24003 to put in it. This because the consumer has no way to tell the
24004 difference between an empty table that we omitted and failure to
24005 generate a table that would have contained data. */
24006 if (info_section_emitted)
24008 unsigned long aranges_length = size_of_aranges ();
24010 switch_to_section (debug_aranges_section);
24011 output_aranges (aranges_length);
24014 /* Output ranges section if necessary. */
24015 if (ranges_table_in_use)
24017 switch_to_section (debug_ranges_section);
24018 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24019 output_ranges ();
24022 /* Have to end the macro section. */
24023 if (have_macinfo)
24025 switch_to_section (debug_macinfo_section);
24026 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24027 output_macinfo ();
24028 dw2_asm_output_data (1, 0, "End compilation unit");
24031 /* Output the source line correspondence table. We must do this
24032 even if there is no line information. Otherwise, on an empty
24033 translation unit, we will generate a present, but empty,
24034 .debug_info section. IRIX 6.5 `nm' will then complain when
24035 examining the file. This is done late so that any filenames
24036 used by the debug_info section are marked as 'used'. */
24037 switch_to_section (debug_line_section);
24038 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24039 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24040 output_line_info (false);
24042 if (dwarf_split_debug_info && info_section_emitted)
24044 switch_to_section (debug_skeleton_line_section);
24045 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24046 output_line_info (true);
24049 /* If we emitted any indirect strings, output the string table too. */
24050 if (debug_str_hash || skeleton_debug_str_hash)
24051 output_indirect_strings ();
24054 #include "gt-dwarf2out.h"