Daily bump.
[official-gcc.git] / gcc / dwarf2out.c
blob66cbfb032e0d9742ef88199a08941f87b8bf5747
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 inline bool is_template_instantiation (dw_die_ref);
3027 static void assign_symbol_names (dw_die_ref);
3028 static void break_out_includes (dw_die_ref);
3029 static int is_declaration_die (dw_die_ref);
3030 static int should_move_die_to_comdat (dw_die_ref);
3031 static dw_die_ref clone_as_declaration (dw_die_ref);
3032 static dw_die_ref clone_die (dw_die_ref);
3033 static dw_die_ref clone_tree (dw_die_ref);
3034 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3035 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3036 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3037 static dw_die_ref generate_skeleton (dw_die_ref);
3038 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3039 dw_die_ref,
3040 dw_die_ref);
3041 static void break_out_comdat_types (dw_die_ref);
3042 static void copy_decls_for_unworthy_types (dw_die_ref);
3044 static void add_sibling_attributes (dw_die_ref);
3045 static void output_location_lists (dw_die_ref);
3046 static int constant_size (unsigned HOST_WIDE_INT);
3047 static unsigned long size_of_die (dw_die_ref);
3048 static void calc_die_sizes (dw_die_ref);
3049 static void calc_base_type_die_sizes (void);
3050 static void mark_dies (dw_die_ref);
3051 static void unmark_dies (dw_die_ref);
3052 static void unmark_all_dies (dw_die_ref);
3053 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3054 static unsigned long size_of_aranges (void);
3055 static enum dwarf_form value_format (dw_attr_ref);
3056 static void output_value_format (dw_attr_ref);
3057 static void output_abbrev_section (void);
3058 static void output_die_abbrevs (unsigned long, dw_die_ref);
3059 static void output_die_symbol (dw_die_ref);
3060 static void output_die (dw_die_ref);
3061 static void output_compilation_unit_header (void);
3062 static void output_comp_unit (dw_die_ref, int);
3063 static void output_comdat_type_unit (comdat_type_node *);
3064 static const char *dwarf2_name (tree, int);
3065 static void add_pubname (tree, dw_die_ref);
3066 static void add_enumerator_pubname (const char *, dw_die_ref);
3067 static void add_pubname_string (const char *, dw_die_ref);
3068 static void add_pubtype (tree, dw_die_ref);
3069 static void output_pubnames (vec<pubname_entry, va_gc> *);
3070 static void output_aranges (unsigned long);
3071 static unsigned int add_ranges_num (int);
3072 static unsigned int add_ranges (const_tree);
3073 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3074 bool *, bool);
3075 static void output_ranges (void);
3076 static dw_line_info_table *new_line_info_table (void);
3077 static void output_line_info (bool);
3078 static void output_file_names (void);
3079 static dw_die_ref base_type_die (tree);
3080 static int is_base_type (tree);
3081 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3082 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3083 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3084 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3085 static int type_is_enum (const_tree);
3086 static unsigned int dbx_reg_number (const_rtx);
3087 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3088 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3089 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3090 enum var_init_status);
3091 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3092 enum var_init_status);
3093 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3094 enum var_init_status);
3095 static int is_based_loc (const_rtx);
3096 static int resolve_one_addr (rtx *, void *);
3097 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3098 enum var_init_status);
3099 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3100 enum var_init_status);
3101 static dw_loc_list_ref loc_list_from_tree (tree, int);
3102 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3103 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3104 static tree field_type (const_tree);
3105 static unsigned int simple_type_align_in_bits (const_tree);
3106 static unsigned int simple_decl_align_in_bits (const_tree);
3107 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3108 static HOST_WIDE_INT field_byte_offset (const_tree);
3109 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3110 dw_loc_list_ref);
3111 static void add_data_member_location_attribute (dw_die_ref, tree);
3112 static bool add_const_value_attribute (dw_die_ref, rtx);
3113 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3114 static void insert_double (double_int, unsigned char *);
3115 static void insert_float (const_rtx, unsigned char *);
3116 static rtx rtl_for_decl_location (tree);
3117 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3118 enum dwarf_attribute);
3119 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3120 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3121 static void add_name_attribute (dw_die_ref, const char *);
3122 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3123 static void add_comp_dir_attribute (dw_die_ref);
3124 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3125 static void add_subscript_info (dw_die_ref, tree, bool);
3126 static void add_byte_size_attribute (dw_die_ref, tree);
3127 static void add_bit_offset_attribute (dw_die_ref, tree);
3128 static void add_bit_size_attribute (dw_die_ref, tree);
3129 static void add_prototyped_attribute (dw_die_ref, tree);
3130 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3131 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3132 static void add_src_coords_attributes (dw_die_ref, tree);
3133 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3134 static void push_decl_scope (tree);
3135 static void pop_decl_scope (void);
3136 static dw_die_ref scope_die_for (tree, dw_die_ref);
3137 static inline int local_scope_p (dw_die_ref);
3138 static inline int class_scope_p (dw_die_ref);
3139 static inline int class_or_namespace_scope_p (dw_die_ref);
3140 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3141 static void add_calling_convention_attribute (dw_die_ref, tree);
3142 static const char *type_tag (const_tree);
3143 static tree member_declared_type (const_tree);
3144 #if 0
3145 static const char *decl_start_label (tree);
3146 #endif
3147 static void gen_array_type_die (tree, dw_die_ref);
3148 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3149 #if 0
3150 static void gen_entry_point_die (tree, dw_die_ref);
3151 #endif
3152 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3153 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3154 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3155 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3156 static void gen_formal_types_die (tree, dw_die_ref);
3157 static void gen_subprogram_die (tree, dw_die_ref);
3158 static void gen_variable_die (tree, tree, dw_die_ref);
3159 static void gen_const_die (tree, dw_die_ref);
3160 static void gen_label_die (tree, dw_die_ref);
3161 static void gen_lexical_block_die (tree, dw_die_ref, int);
3162 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3163 static void gen_field_die (tree, dw_die_ref);
3164 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3165 static dw_die_ref gen_compile_unit_die (const char *);
3166 static void gen_inheritance_die (tree, tree, dw_die_ref);
3167 static void gen_member_die (tree, dw_die_ref);
3168 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3169 enum debug_info_usage);
3170 static void gen_subroutine_type_die (tree, dw_die_ref);
3171 static void gen_typedef_die (tree, dw_die_ref);
3172 static void gen_type_die (tree, dw_die_ref);
3173 static void gen_block_die (tree, dw_die_ref, int);
3174 static void decls_for_scope (tree, dw_die_ref, int);
3175 static inline int is_redundant_typedef (const_tree);
3176 static bool is_naming_typedef_decl (const_tree);
3177 static inline dw_die_ref get_context_die (tree);
3178 static void gen_namespace_die (tree, dw_die_ref);
3179 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3180 static dw_die_ref force_decl_die (tree);
3181 static dw_die_ref force_type_die (tree);
3182 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3183 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3184 static struct dwarf_file_data * lookup_filename (const char *);
3185 static void retry_incomplete_types (void);
3186 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3187 static void gen_generic_params_dies (tree);
3188 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3189 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3190 static void splice_child_die (dw_die_ref, dw_die_ref);
3191 static int file_info_cmp (const void *, const void *);
3192 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3193 const char *, const char *);
3194 static void output_loc_list (dw_loc_list_ref);
3195 static char *gen_internal_sym (const char *);
3196 static bool want_pubnames (void);
3198 static void prune_unmark_dies (dw_die_ref);
3199 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3200 static void prune_unused_types_mark (dw_die_ref, int);
3201 static void prune_unused_types_walk (dw_die_ref);
3202 static void prune_unused_types_walk_attribs (dw_die_ref);
3203 static void prune_unused_types_prune (dw_die_ref);
3204 static void prune_unused_types (void);
3205 static int maybe_emit_file (struct dwarf_file_data *fd);
3206 static inline const char *AT_vms_delta1 (dw_attr_ref);
3207 static inline const char *AT_vms_delta2 (dw_attr_ref);
3208 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3209 const char *, const char *);
3210 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3211 static void gen_remaining_tmpl_value_param_die_attribute (void);
3212 static bool generic_type_p (tree);
3213 static void schedule_generic_params_dies_gen (tree t);
3214 static void gen_scheduled_generic_parms_dies (void);
3216 static const char *comp_dir_string (void);
3218 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3220 /* enum for tracking thread-local variables whose address is really an offset
3221 relative to the TLS pointer, which will need link-time relocation, but will
3222 not need relocation by the DWARF consumer. */
3224 enum dtprel_bool
3226 dtprel_false = 0,
3227 dtprel_true = 1
3230 /* Return the operator to use for an address of a variable. For dtprel_true, we
3231 use DW_OP_const*. For regular variables, which need both link-time
3232 relocation and consumer-level relocation (e.g., to account for shared objects
3233 loaded at a random address), we use DW_OP_addr*. */
3235 static inline enum dwarf_location_atom
3236 dw_addr_op (enum dtprel_bool dtprel)
3238 if (dtprel == dtprel_true)
3239 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3240 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3241 else
3242 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3245 /* Return a pointer to a newly allocated address location description. If
3246 dwarf_split_debug_info is true, then record the address with the appropriate
3247 relocation. */
3248 static inline dw_loc_descr_ref
3249 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3251 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3253 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3254 ref->dw_loc_oprnd1.v.val_addr = addr;
3255 ref->dtprel = dtprel;
3256 if (dwarf_split_debug_info)
3257 ref->dw_loc_oprnd1.val_entry
3258 = add_addr_table_entry (addr,
3259 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3260 else
3261 ref->dw_loc_oprnd1.val_entry = NULL;
3263 return ref;
3266 /* Section names used to hold DWARF debugging information. */
3268 #ifndef DEBUG_INFO_SECTION
3269 #define DEBUG_INFO_SECTION ".debug_info"
3270 #endif
3271 #ifndef DEBUG_DWO_INFO_SECTION
3272 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3273 #endif
3274 #ifndef DEBUG_ABBREV_SECTION
3275 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3276 #endif
3277 #ifndef DEBUG_DWO_ABBREV_SECTION
3278 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3279 #endif
3280 #ifndef DEBUG_ARANGES_SECTION
3281 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3282 #endif
3283 #ifndef DEBUG_ADDR_SECTION
3284 #define DEBUG_ADDR_SECTION ".debug_addr"
3285 #endif
3286 #ifndef DEBUG_NORM_MACINFO_SECTION
3287 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3288 #endif
3289 #ifndef DEBUG_DWO_MACINFO_SECTION
3290 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3291 #endif
3292 #ifndef DEBUG_MACINFO_SECTION
3293 #define DEBUG_MACINFO_SECTION \
3294 (!dwarf_split_debug_info \
3295 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3296 #endif
3297 #ifndef DEBUG_NORM_MACRO_SECTION
3298 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3299 #endif
3300 #ifndef DEBUG_DWO_MACRO_SECTION
3301 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3302 #endif
3303 #ifndef DEBUG_MACRO_SECTION
3304 #define DEBUG_MACRO_SECTION \
3305 (!dwarf_split_debug_info \
3306 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3307 #endif
3308 #ifndef DEBUG_LINE_SECTION
3309 #define DEBUG_LINE_SECTION ".debug_line"
3310 #endif
3311 #ifndef DEBUG_DWO_LINE_SECTION
3312 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3313 #endif
3314 #ifndef DEBUG_LOC_SECTION
3315 #define DEBUG_LOC_SECTION ".debug_loc"
3316 #endif
3317 #ifndef DEBUG_DWO_LOC_SECTION
3318 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3319 #endif
3320 #ifndef DEBUG_PUBNAMES_SECTION
3321 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3322 #endif
3323 #ifndef DEBUG_PUBTYPES_SECTION
3324 #define DEBUG_PUBTYPES_SECTION ".debug_pubtypes"
3325 #endif
3326 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3327 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3328 #ifndef DEBUG_STR_OFFSETS_SECTION
3329 #define DEBUG_STR_OFFSETS_SECTION \
3330 (!dwarf_split_debug_info \
3331 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3332 #endif
3333 #ifndef DEBUG_STR_DWO_SECTION
3334 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3335 #endif
3336 #ifndef DEBUG_STR_SECTION
3337 #define DEBUG_STR_SECTION ".debug_str"
3338 #endif
3339 #ifndef DEBUG_RANGES_SECTION
3340 #define DEBUG_RANGES_SECTION ".debug_ranges"
3341 #endif
3343 /* Standard ELF section names for compiled code and data. */
3344 #ifndef TEXT_SECTION_NAME
3345 #define TEXT_SECTION_NAME ".text"
3346 #endif
3348 /* Section flags for .debug_macinfo/.debug_macro section. */
3349 #define DEBUG_MACRO_SECTION_FLAGS \
3350 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3352 /* Section flags for .debug_str section. */
3353 #define DEBUG_STR_SECTION_FLAGS \
3354 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3355 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3356 : SECTION_DEBUG)
3358 /* Section flags for .debug_str.dwo section. */
3359 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3361 /* Labels we insert at beginning sections we can reference instead of
3362 the section names themselves. */
3364 #ifndef TEXT_SECTION_LABEL
3365 #define TEXT_SECTION_LABEL "Ltext"
3366 #endif
3367 #ifndef COLD_TEXT_SECTION_LABEL
3368 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3369 #endif
3370 #ifndef DEBUG_LINE_SECTION_LABEL
3371 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3372 #endif
3373 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3374 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3375 #endif
3376 #ifndef DEBUG_INFO_SECTION_LABEL
3377 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3378 #endif
3379 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3380 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3381 #endif
3382 #ifndef DEBUG_ABBREV_SECTION_LABEL
3383 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3384 #endif
3385 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3386 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3387 #endif
3388 #ifndef DEBUG_ADDR_SECTION_LABEL
3389 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3390 #endif
3391 #ifndef DEBUG_LOC_SECTION_LABEL
3392 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3393 #endif
3394 #ifndef DEBUG_RANGES_SECTION_LABEL
3395 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3396 #endif
3397 #ifndef DEBUG_MACINFO_SECTION_LABEL
3398 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3399 #endif
3400 #ifndef DEBUG_MACRO_SECTION_LABEL
3401 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3402 #endif
3403 #define SKELETON_COMP_DIE_ABBREV 1
3404 #define SKELETON_TYPE_DIE_ABBREV 2
3406 /* Definitions of defaults for formats and names of various special
3407 (artificial) labels which may be generated within this file (when the -g
3408 options is used and DWARF2_DEBUGGING_INFO is in effect.
3409 If necessary, these may be overridden from within the tm.h file, but
3410 typically, overriding these defaults is unnecessary. */
3412 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3413 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3414 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3415 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3416 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3417 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3418 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3419 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3420 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3421 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3422 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3423 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3424 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3425 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3427 #ifndef TEXT_END_LABEL
3428 #define TEXT_END_LABEL "Letext"
3429 #endif
3430 #ifndef COLD_END_LABEL
3431 #define COLD_END_LABEL "Letext_cold"
3432 #endif
3433 #ifndef BLOCK_BEGIN_LABEL
3434 #define BLOCK_BEGIN_LABEL "LBB"
3435 #endif
3436 #ifndef BLOCK_END_LABEL
3437 #define BLOCK_END_LABEL "LBE"
3438 #endif
3439 #ifndef LINE_CODE_LABEL
3440 #define LINE_CODE_LABEL "LM"
3441 #endif
3444 /* Return the root of the DIE's built for the current compilation unit. */
3445 static dw_die_ref
3446 comp_unit_die (void)
3448 if (!single_comp_unit_die)
3449 single_comp_unit_die = gen_compile_unit_die (NULL);
3450 return single_comp_unit_die;
3453 /* We allow a language front-end to designate a function that is to be
3454 called to "demangle" any name before it is put into a DIE. */
3456 static const char *(*demangle_name_func) (const char *);
3458 void
3459 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3461 demangle_name_func = func;
3464 /* Test if rtl node points to a pseudo register. */
3466 static inline int
3467 is_pseudo_reg (const_rtx rtl)
3469 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3470 || (GET_CODE (rtl) == SUBREG
3471 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3474 /* Return a reference to a type, with its const and volatile qualifiers
3475 removed. */
3477 static inline tree
3478 type_main_variant (tree type)
3480 type = TYPE_MAIN_VARIANT (type);
3482 /* ??? There really should be only one main variant among any group of
3483 variants of a given type (and all of the MAIN_VARIANT values for all
3484 members of the group should point to that one type) but sometimes the C
3485 front-end messes this up for array types, so we work around that bug
3486 here. */
3487 if (TREE_CODE (type) == ARRAY_TYPE)
3488 while (type != TYPE_MAIN_VARIANT (type))
3489 type = TYPE_MAIN_VARIANT (type);
3491 return type;
3494 /* Return nonzero if the given type node represents a tagged type. */
3496 static inline int
3497 is_tagged_type (const_tree type)
3499 enum tree_code code = TREE_CODE (type);
3501 return (code == RECORD_TYPE || code == UNION_TYPE
3502 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3505 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3507 static void
3508 get_ref_die_offset_label (char *label, dw_die_ref ref)
3510 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3513 /* Return die_offset of a DIE reference to a base type. */
3515 static unsigned long int
3516 get_base_type_offset (dw_die_ref ref)
3518 if (ref->die_offset)
3519 return ref->die_offset;
3520 if (comp_unit_die ()->die_abbrev)
3522 calc_base_type_die_sizes ();
3523 gcc_assert (ref->die_offset);
3525 return ref->die_offset;
3528 /* Return die_offset of a DIE reference other than base type. */
3530 static unsigned long int
3531 get_ref_die_offset (dw_die_ref ref)
3533 gcc_assert (ref->die_offset);
3534 return ref->die_offset;
3537 /* Convert a DIE tag into its string name. */
3539 static const char *
3540 dwarf_tag_name (unsigned int tag)
3542 const char *name = get_DW_TAG_name (tag);
3544 if (name != NULL)
3545 return name;
3547 return "DW_TAG_<unknown>";
3550 /* Convert a DWARF attribute code into its string name. */
3552 static const char *
3553 dwarf_attr_name (unsigned int attr)
3555 const char *name;
3557 switch (attr)
3559 #if VMS_DEBUGGING_INFO
3560 case DW_AT_HP_prologue:
3561 return "DW_AT_HP_prologue";
3562 #else
3563 case DW_AT_MIPS_loop_unroll_factor:
3564 return "DW_AT_MIPS_loop_unroll_factor";
3565 #endif
3567 #if VMS_DEBUGGING_INFO
3568 case DW_AT_HP_epilogue:
3569 return "DW_AT_HP_epilogue";
3570 #else
3571 case DW_AT_MIPS_stride:
3572 return "DW_AT_MIPS_stride";
3573 #endif
3576 name = get_DW_AT_name (attr);
3578 if (name != NULL)
3579 return name;
3581 return "DW_AT_<unknown>";
3584 /* Convert a DWARF value form code into its string name. */
3586 static const char *
3587 dwarf_form_name (unsigned int form)
3589 const char *name = get_DW_FORM_name (form);
3591 if (name != NULL)
3592 return name;
3594 return "DW_FORM_<unknown>";
3597 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3598 instance of an inlined instance of a decl which is local to an inline
3599 function, so we have to trace all of the way back through the origin chain
3600 to find out what sort of node actually served as the original seed for the
3601 given block. */
3603 static tree
3604 decl_ultimate_origin (const_tree decl)
3606 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3607 return NULL_TREE;
3609 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3610 nodes in the function to point to themselves; ignore that if
3611 we're trying to output the abstract instance of this function. */
3612 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3613 return NULL_TREE;
3615 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3616 most distant ancestor, this should never happen. */
3617 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3619 return DECL_ABSTRACT_ORIGIN (decl);
3622 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3623 of a virtual function may refer to a base class, so we check the 'this'
3624 parameter. */
3626 static tree
3627 decl_class_context (tree decl)
3629 tree context = NULL_TREE;
3631 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3632 context = DECL_CONTEXT (decl);
3633 else
3634 context = TYPE_MAIN_VARIANT
3635 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3637 if (context && !TYPE_P (context))
3638 context = NULL_TREE;
3640 return context;
3643 /* Add an attribute/value pair to a DIE. */
3645 static inline void
3646 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3648 /* Maybe this should be an assert? */
3649 if (die == NULL)
3650 return;
3652 vec_safe_reserve (die->die_attr, 1);
3653 vec_safe_push (die->die_attr, *attr);
3656 static inline enum dw_val_class
3657 AT_class (dw_attr_ref a)
3659 return a->dw_attr_val.val_class;
3662 /* Return the index for any attribute that will be referenced with a
3663 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3664 are stored in dw_attr_val.v.val_str for reference counting
3665 pruning. */
3667 static inline unsigned int
3668 AT_index (dw_attr_ref a)
3670 if (AT_class (a) == dw_val_class_str)
3671 return a->dw_attr_val.v.val_str->index;
3672 else if (a->dw_attr_val.val_entry != NULL)
3673 return a->dw_attr_val.val_entry->index;
3674 return NOT_INDEXED;
3677 /* Add a flag value attribute to a DIE. */
3679 static inline void
3680 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3682 dw_attr_node attr;
3684 attr.dw_attr = attr_kind;
3685 attr.dw_attr_val.val_class = dw_val_class_flag;
3686 attr.dw_attr_val.val_entry = NULL;
3687 attr.dw_attr_val.v.val_flag = flag;
3688 add_dwarf_attr (die, &attr);
3691 static inline unsigned
3692 AT_flag (dw_attr_ref a)
3694 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3695 return a->dw_attr_val.v.val_flag;
3698 /* Add a signed integer attribute value to a DIE. */
3700 static inline void
3701 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3703 dw_attr_node attr;
3705 attr.dw_attr = attr_kind;
3706 attr.dw_attr_val.val_class = dw_val_class_const;
3707 attr.dw_attr_val.val_entry = NULL;
3708 attr.dw_attr_val.v.val_int = int_val;
3709 add_dwarf_attr (die, &attr);
3712 static inline HOST_WIDE_INT
3713 AT_int (dw_attr_ref a)
3715 gcc_assert (a && AT_class (a) == dw_val_class_const);
3716 return a->dw_attr_val.v.val_int;
3719 /* Add an unsigned integer attribute value to a DIE. */
3721 static inline void
3722 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3723 unsigned HOST_WIDE_INT unsigned_val)
3725 dw_attr_node attr;
3727 attr.dw_attr = attr_kind;
3728 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3729 attr.dw_attr_val.val_entry = NULL;
3730 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3731 add_dwarf_attr (die, &attr);
3734 static inline unsigned HOST_WIDE_INT
3735 AT_unsigned (dw_attr_ref a)
3737 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3738 return a->dw_attr_val.v.val_unsigned;
3741 /* Add an unsigned double integer attribute value to a DIE. */
3743 static inline void
3744 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3745 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3747 dw_attr_node attr;
3749 attr.dw_attr = attr_kind;
3750 attr.dw_attr_val.val_class = dw_val_class_const_double;
3751 attr.dw_attr_val.val_entry = NULL;
3752 attr.dw_attr_val.v.val_double.high = high;
3753 attr.dw_attr_val.v.val_double.low = low;
3754 add_dwarf_attr (die, &attr);
3757 /* Add a floating point attribute value to a DIE and return it. */
3759 static inline void
3760 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3761 unsigned int length, unsigned int elt_size, unsigned char *array)
3763 dw_attr_node attr;
3765 attr.dw_attr = attr_kind;
3766 attr.dw_attr_val.val_class = dw_val_class_vec;
3767 attr.dw_attr_val.val_entry = NULL;
3768 attr.dw_attr_val.v.val_vec.length = length;
3769 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3770 attr.dw_attr_val.v.val_vec.array = array;
3771 add_dwarf_attr (die, &attr);
3774 /* Add an 8-byte data attribute value to a DIE. */
3776 static inline void
3777 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3778 unsigned char data8[8])
3780 dw_attr_node attr;
3782 attr.dw_attr = attr_kind;
3783 attr.dw_attr_val.val_class = dw_val_class_data8;
3784 attr.dw_attr_val.val_entry = NULL;
3785 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3786 add_dwarf_attr (die, &attr);
3789 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3790 dwarf_split_debug_info, address attributes in dies destined for the
3791 final executable have force_direct set to avoid using indexed
3792 references. */
3794 static inline void
3795 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3796 bool force_direct)
3798 dw_attr_node attr;
3799 char * lbl_id;
3801 lbl_id = xstrdup (lbl_low);
3802 attr.dw_attr = DW_AT_low_pc;
3803 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3804 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3805 if (dwarf_split_debug_info && !force_direct)
3806 attr.dw_attr_val.val_entry
3807 = add_addr_table_entry (lbl_id, ate_kind_label);
3808 else
3809 attr.dw_attr_val.val_entry = NULL;
3810 add_dwarf_attr (die, &attr);
3812 attr.dw_attr = DW_AT_high_pc;
3813 if (dwarf_version < 4)
3814 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3815 else
3816 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3817 lbl_id = xstrdup (lbl_high);
3818 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3819 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3820 && dwarf_split_debug_info && !force_direct)
3821 attr.dw_attr_val.val_entry
3822 = add_addr_table_entry (lbl_id, ate_kind_label);
3823 else
3824 attr.dw_attr_val.val_entry = NULL;
3825 add_dwarf_attr (die, &attr);
3828 /* Hash and equality functions for debug_str_hash. */
3830 static hashval_t
3831 debug_str_do_hash (const void *x)
3833 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3836 static int
3837 debug_str_eq (const void *x1, const void *x2)
3839 return strcmp ((((const struct indirect_string_node *)x1)->str),
3840 (const char *)x2) == 0;
3843 /* Add STR to the given string hash table. */
3845 static struct indirect_string_node *
3846 find_AT_string_in_table (const char *str, htab_t table)
3848 struct indirect_string_node *node;
3849 void **slot;
3851 slot = htab_find_slot_with_hash (table, str,
3852 htab_hash_string (str), INSERT);
3853 if (*slot == NULL)
3855 node = ggc_alloc_cleared_indirect_string_node ();
3856 node->str = ggc_strdup (str);
3857 *slot = node;
3859 else
3860 node = (struct indirect_string_node *) *slot;
3862 node->refcount++;
3863 return node;
3866 /* Add STR to the indirect string hash table. */
3868 static struct indirect_string_node *
3869 find_AT_string (const char *str)
3871 if (! debug_str_hash)
3872 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3873 debug_str_eq, NULL);
3875 return find_AT_string_in_table (str, debug_str_hash);
3878 /* Add a string attribute value to a DIE. */
3880 static inline void
3881 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3883 dw_attr_node attr;
3884 struct indirect_string_node *node;
3886 node = find_AT_string (str);
3888 attr.dw_attr = attr_kind;
3889 attr.dw_attr_val.val_class = dw_val_class_str;
3890 attr.dw_attr_val.val_entry = NULL;
3891 attr.dw_attr_val.v.val_str = node;
3892 add_dwarf_attr (die, &attr);
3895 static inline const char *
3896 AT_string (dw_attr_ref a)
3898 gcc_assert (a && AT_class (a) == dw_val_class_str);
3899 return a->dw_attr_val.v.val_str->str;
3902 /* Call this function directly to bypass AT_string_form's logic to put
3903 the string inline in the die. */
3905 static void
3906 set_indirect_string (struct indirect_string_node *node)
3908 char label[32];
3909 /* Already indirect is a no op. */
3910 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3912 gcc_assert (node->label);
3913 return;
3915 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3916 ++dw2_string_counter;
3917 node->label = xstrdup (label);
3919 if (!dwarf_split_debug_info)
3921 node->form = DW_FORM_strp;
3922 node->index = NOT_INDEXED;
3924 else
3926 node->form = DW_FORM_GNU_str_index;
3927 node->index = NO_INDEX_ASSIGNED;
3931 /* Find out whether a string should be output inline in DIE
3932 or out-of-line in .debug_str section. */
3934 static enum dwarf_form
3935 find_string_form (struct indirect_string_node *node)
3937 unsigned int len;
3939 if (node->form)
3940 return node->form;
3942 len = strlen (node->str) + 1;
3944 /* If the string is shorter or equal to the size of the reference, it is
3945 always better to put it inline. */
3946 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3947 return node->form = DW_FORM_string;
3949 /* If we cannot expect the linker to merge strings in .debug_str
3950 section, only put it into .debug_str if it is worth even in this
3951 single module. */
3952 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3953 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3954 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3955 return node->form = DW_FORM_string;
3957 set_indirect_string (node);
3959 return node->form;
3962 /* Find out whether the string referenced from the attribute should be
3963 output inline in DIE or out-of-line in .debug_str section. */
3965 static enum dwarf_form
3966 AT_string_form (dw_attr_ref a)
3968 gcc_assert (a && AT_class (a) == dw_val_class_str);
3969 return find_string_form (a->dw_attr_val.v.val_str);
3972 /* Add a DIE reference attribute value to a DIE. */
3974 static inline void
3975 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3977 dw_attr_node attr;
3979 #ifdef ENABLE_CHECKING
3980 gcc_assert (targ_die != NULL);
3981 #else
3982 /* With LTO we can end up trying to reference something we didn't create
3983 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3984 if (targ_die == NULL)
3985 return;
3986 #endif
3988 attr.dw_attr = attr_kind;
3989 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3990 attr.dw_attr_val.val_entry = NULL;
3991 attr.dw_attr_val.v.val_die_ref.die = targ_die;
3992 attr.dw_attr_val.v.val_die_ref.external = 0;
3993 add_dwarf_attr (die, &attr);
3996 /* Change DIE reference REF to point to NEW_DIE instead. */
3998 static inline void
3999 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4001 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4002 ref->dw_attr_val.v.val_die_ref.die = new_die;
4003 ref->dw_attr_val.v.val_die_ref.external = 0;
4006 /* Add an AT_specification attribute to a DIE, and also make the back
4007 pointer from the specification to the definition. */
4009 static inline void
4010 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4012 add_AT_die_ref (die, DW_AT_specification, targ_die);
4013 gcc_assert (!targ_die->die_definition);
4014 targ_die->die_definition = die;
4017 static inline dw_die_ref
4018 AT_ref (dw_attr_ref a)
4020 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4021 return a->dw_attr_val.v.val_die_ref.die;
4024 static inline int
4025 AT_ref_external (dw_attr_ref a)
4027 if (a && AT_class (a) == dw_val_class_die_ref)
4028 return a->dw_attr_val.v.val_die_ref.external;
4030 return 0;
4033 static inline void
4034 set_AT_ref_external (dw_attr_ref a, int i)
4036 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4037 a->dw_attr_val.v.val_die_ref.external = i;
4040 /* Add an FDE reference attribute value to a DIE. */
4042 static inline void
4043 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4045 dw_attr_node attr;
4047 attr.dw_attr = attr_kind;
4048 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4049 attr.dw_attr_val.val_entry = NULL;
4050 attr.dw_attr_val.v.val_fde_index = targ_fde;
4051 add_dwarf_attr (die, &attr);
4054 /* Add a location description attribute value to a DIE. */
4056 static inline void
4057 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4059 dw_attr_node attr;
4061 attr.dw_attr = attr_kind;
4062 attr.dw_attr_val.val_class = dw_val_class_loc;
4063 attr.dw_attr_val.val_entry = NULL;
4064 attr.dw_attr_val.v.val_loc = loc;
4065 add_dwarf_attr (die, &attr);
4068 static inline dw_loc_descr_ref
4069 AT_loc (dw_attr_ref a)
4071 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4072 return a->dw_attr_val.v.val_loc;
4075 static inline void
4076 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4078 dw_attr_node attr;
4080 attr.dw_attr = attr_kind;
4081 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4082 attr.dw_attr_val.val_entry = NULL;
4083 attr.dw_attr_val.v.val_loc_list = loc_list;
4084 add_dwarf_attr (die, &attr);
4085 have_location_lists = true;
4088 static inline dw_loc_list_ref
4089 AT_loc_list (dw_attr_ref a)
4091 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4092 return a->dw_attr_val.v.val_loc_list;
4095 static inline dw_loc_list_ref *
4096 AT_loc_list_ptr (dw_attr_ref a)
4098 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4099 return &a->dw_attr_val.v.val_loc_list;
4102 /* Table of entries into the .debug_addr section. */
4104 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4106 /* Hash an address_table_entry. */
4108 static hashval_t
4109 addr_table_entry_do_hash (const void *x)
4111 const addr_table_entry *a = (const addr_table_entry *) x;
4112 switch (a->kind)
4114 case ate_kind_rtx:
4115 return iterative_hash_rtx (a->addr.rtl, 0);
4116 case ate_kind_rtx_dtprel:
4117 return iterative_hash_rtx (a->addr.rtl, 1);
4118 case ate_kind_label:
4119 return htab_hash_string (a->addr.label);
4120 default:
4121 gcc_unreachable ();
4125 /* Determine equality for two address_table_entries. */
4127 static int
4128 addr_table_entry_eq (const void *x1, const void *x2)
4130 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4131 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4133 if (a1->kind != a2->kind)
4134 return 0;
4135 switch (a1->kind)
4137 case ate_kind_rtx:
4138 case ate_kind_rtx_dtprel:
4139 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4140 case ate_kind_label:
4141 return strcmp (a1->addr.label, a2->addr.label) == 0;
4142 default:
4143 gcc_unreachable ();
4147 /* Initialize an addr_table_entry. */
4149 void
4150 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4152 e->kind = kind;
4153 switch (kind)
4155 case ate_kind_rtx:
4156 case ate_kind_rtx_dtprel:
4157 e->addr.rtl = (rtx) addr;
4158 break;
4159 case ate_kind_label:
4160 e->addr.label = (char *) addr;
4161 break;
4163 e->refcount = 0;
4164 e->index = NO_INDEX_ASSIGNED;
4167 /* Add attr to the address table entry to the table. Defer setting an
4168 index until output time. */
4170 static addr_table_entry *
4171 add_addr_table_entry (void *addr, enum ate_kind kind)
4173 addr_table_entry *node;
4174 addr_table_entry finder;
4175 void **slot;
4177 gcc_assert (dwarf_split_debug_info);
4178 if (! addr_index_table)
4179 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4180 addr_table_entry_eq, NULL);
4181 init_addr_table_entry (&finder, kind, addr);
4182 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4184 if (*slot == HTAB_EMPTY_ENTRY)
4186 node = ggc_alloc_cleared_addr_table_entry ();
4187 init_addr_table_entry (node, kind, addr);
4188 *slot = node;
4190 else
4191 node = (addr_table_entry *) *slot;
4193 node->refcount++;
4194 return node;
4197 /* Remove an entry from the addr table by decrementing its refcount.
4198 Strictly, decrementing the refcount would be enough, but the
4199 assertion that the entry is actually in the table has found
4200 bugs. */
4202 static void
4203 remove_addr_table_entry (addr_table_entry *entry)
4205 addr_table_entry *node;
4207 gcc_assert (dwarf_split_debug_info && addr_index_table);
4208 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4209 /* After an index is assigned, the table is frozen. */
4210 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4211 node->refcount--;
4214 /* Given a location list, remove all addresses it refers to from the
4215 address_table. */
4217 static void
4218 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4220 for (; descr; descr = descr->dw_loc_next)
4221 if (descr->dw_loc_oprnd1.val_entry != NULL)
4223 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4224 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4228 /* A helper function for dwarf2out_finish called through
4229 htab_traverse. Assign an addr_table_entry its index. All entries
4230 must be collected into the table when this function is called,
4231 because the indexing code relies on htab_traverse to traverse nodes
4232 in the same order for each run. */
4234 static int
4235 index_addr_table_entry (void **h, void *v)
4237 addr_table_entry *node = (addr_table_entry *) *h;
4238 unsigned int *index = (unsigned int *) v;
4240 /* Don't index unreferenced nodes. */
4241 if (node->refcount == 0)
4242 return 1;
4244 gcc_assert(node->index == NO_INDEX_ASSIGNED);
4245 node->index = *index;
4246 *index += 1;
4248 return 1;
4251 /* Add an address constant attribute value to a DIE. When using
4252 dwarf_split_debug_info, address attributes in dies destined for the
4253 final executable should be direct references--setting the parameter
4254 force_direct ensures this behavior. */
4256 static inline void
4257 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4258 bool force_direct)
4260 dw_attr_node attr;
4262 attr.dw_attr = attr_kind;
4263 attr.dw_attr_val.val_class = dw_val_class_addr;
4264 attr.dw_attr_val.v.val_addr = addr;
4265 if (dwarf_split_debug_info && !force_direct)
4266 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4267 else
4268 attr.dw_attr_val.val_entry = NULL;
4269 add_dwarf_attr (die, &attr);
4272 /* Get the RTX from to an address DIE attribute. */
4274 static inline rtx
4275 AT_addr (dw_attr_ref a)
4277 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4278 return a->dw_attr_val.v.val_addr;
4281 /* Add a file attribute value to a DIE. */
4283 static inline void
4284 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4285 struct dwarf_file_data *fd)
4287 dw_attr_node attr;
4289 attr.dw_attr = attr_kind;
4290 attr.dw_attr_val.val_class = dw_val_class_file;
4291 attr.dw_attr_val.val_entry = NULL;
4292 attr.dw_attr_val.v.val_file = fd;
4293 add_dwarf_attr (die, &attr);
4296 /* Get the dwarf_file_data from a file DIE attribute. */
4298 static inline struct dwarf_file_data *
4299 AT_file (dw_attr_ref a)
4301 gcc_assert (a && AT_class (a) == dw_val_class_file);
4302 return a->dw_attr_val.v.val_file;
4305 /* Add a vms delta attribute value to a DIE. */
4307 static inline void
4308 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4309 const char *lbl1, const char *lbl2)
4311 dw_attr_node attr;
4313 attr.dw_attr = attr_kind;
4314 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4315 attr.dw_attr_val.val_entry = NULL;
4316 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4317 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4318 add_dwarf_attr (die, &attr);
4321 /* Add a label identifier attribute value to a DIE. */
4323 static inline void
4324 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4325 const char *lbl_id)
4327 dw_attr_node attr;
4329 attr.dw_attr = attr_kind;
4330 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4331 attr.dw_attr_val.val_entry = NULL;
4332 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4333 if (dwarf_split_debug_info)
4334 attr.dw_attr_val.val_entry
4335 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4336 ate_kind_label);
4337 add_dwarf_attr (die, &attr);
4340 /* Add a section offset attribute value to a DIE, an offset into the
4341 debug_line section. */
4343 static inline void
4344 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4345 const char *label)
4347 dw_attr_node attr;
4349 attr.dw_attr = attr_kind;
4350 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4351 attr.dw_attr_val.val_entry = NULL;
4352 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4353 add_dwarf_attr (die, &attr);
4356 /* Add a section offset attribute value to a DIE, an offset into the
4357 debug_macinfo section. */
4359 static inline void
4360 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4361 const char *label)
4363 dw_attr_node attr;
4365 attr.dw_attr = attr_kind;
4366 attr.dw_attr_val.val_class = dw_val_class_macptr;
4367 attr.dw_attr_val.val_entry = NULL;
4368 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4369 add_dwarf_attr (die, &attr);
4372 /* Add an offset attribute value to a DIE. */
4374 static inline void
4375 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4376 unsigned HOST_WIDE_INT offset)
4378 dw_attr_node attr;
4380 attr.dw_attr = attr_kind;
4381 attr.dw_attr_val.val_class = dw_val_class_offset;
4382 attr.dw_attr_val.val_entry = NULL;
4383 attr.dw_attr_val.v.val_offset = offset;
4384 add_dwarf_attr (die, &attr);
4387 /* Add a range_list attribute value to a DIE. When using
4388 dwarf_split_debug_info, address attributes in dies destined for the
4389 final executable should be direct references--setting the parameter
4390 force_direct ensures this behavior. */
4392 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4393 #define RELOCATED_OFFSET (NULL)
4395 static void
4396 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4397 long unsigned int offset, bool force_direct)
4399 dw_attr_node attr;
4401 attr.dw_attr = attr_kind;
4402 attr.dw_attr_val.val_class = dw_val_class_range_list;
4403 /* For the range_list attribute, use val_entry to store whether the
4404 offset should follow split-debug-info or normal semantics. This
4405 value is read in output_range_list_offset. */
4406 if (dwarf_split_debug_info && !force_direct)
4407 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4408 else
4409 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4410 attr.dw_attr_val.v.val_offset = offset;
4411 add_dwarf_attr (die, &attr);
4414 /* Return the start label of a delta attribute. */
4416 static inline const char *
4417 AT_vms_delta1 (dw_attr_ref a)
4419 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4420 return a->dw_attr_val.v.val_vms_delta.lbl1;
4423 /* Return the end label of a delta attribute. */
4425 static inline const char *
4426 AT_vms_delta2 (dw_attr_ref a)
4428 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4429 return a->dw_attr_val.v.val_vms_delta.lbl2;
4432 static inline const char *
4433 AT_lbl (dw_attr_ref a)
4435 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4436 || AT_class (a) == dw_val_class_lineptr
4437 || AT_class (a) == dw_val_class_macptr
4438 || AT_class (a) == dw_val_class_high_pc));
4439 return a->dw_attr_val.v.val_lbl_id;
4442 /* Get the attribute of type attr_kind. */
4444 static dw_attr_ref
4445 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4447 dw_attr_ref a;
4448 unsigned ix;
4449 dw_die_ref spec = NULL;
4451 if (! die)
4452 return NULL;
4454 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4455 if (a->dw_attr == attr_kind)
4456 return a;
4457 else if (a->dw_attr == DW_AT_specification
4458 || a->dw_attr == DW_AT_abstract_origin)
4459 spec = AT_ref (a);
4461 if (spec)
4462 return get_AT (spec, attr_kind);
4464 return NULL;
4467 /* Returns the parent of the declaration of DIE. */
4469 static dw_die_ref
4470 get_die_parent (dw_die_ref die)
4472 dw_die_ref t;
4474 if (!die)
4475 return NULL;
4477 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4478 || (t = get_AT_ref (die, DW_AT_specification)))
4479 die = t;
4481 return die->die_parent;
4484 /* Return the "low pc" attribute value, typically associated with a subprogram
4485 DIE. Return null if the "low pc" attribute is either not present, or if it
4486 cannot be represented as an assembler label identifier. */
4488 static inline const char *
4489 get_AT_low_pc (dw_die_ref die)
4491 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4493 return a ? AT_lbl (a) : NULL;
4496 /* Return the "high pc" attribute value, typically associated with a subprogram
4497 DIE. Return null if the "high pc" attribute is either not present, or if it
4498 cannot be represented as an assembler label identifier. */
4500 static inline const char *
4501 get_AT_hi_pc (dw_die_ref die)
4503 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4505 return a ? AT_lbl (a) : NULL;
4508 /* Return the value of the string attribute designated by ATTR_KIND, or
4509 NULL if it is not present. */
4511 static inline const char *
4512 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4514 dw_attr_ref a = get_AT (die, attr_kind);
4516 return a ? AT_string (a) : NULL;
4519 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4520 if it is not present. */
4522 static inline int
4523 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4525 dw_attr_ref a = get_AT (die, attr_kind);
4527 return a ? AT_flag (a) : 0;
4530 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4531 if it is not present. */
4533 static inline unsigned
4534 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4536 dw_attr_ref a = get_AT (die, attr_kind);
4538 return a ? AT_unsigned (a) : 0;
4541 static inline dw_die_ref
4542 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4544 dw_attr_ref a = get_AT (die, attr_kind);
4546 return a ? AT_ref (a) : NULL;
4549 static inline struct dwarf_file_data *
4550 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4552 dw_attr_ref a = get_AT (die, attr_kind);
4554 return a ? AT_file (a) : NULL;
4557 /* Return TRUE if the language is C++. */
4559 static inline bool
4560 is_cxx (void)
4562 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4564 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4567 /* Return TRUE if the language is Fortran. */
4569 static inline bool
4570 is_fortran (void)
4572 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4574 return (lang == DW_LANG_Fortran77
4575 || lang == DW_LANG_Fortran90
4576 || lang == DW_LANG_Fortran95);
4579 /* Return TRUE if the language is Ada. */
4581 static inline bool
4582 is_ada (void)
4584 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4586 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4589 /* Remove the specified attribute if present. */
4591 static void
4592 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4594 dw_attr_ref a;
4595 unsigned ix;
4597 if (! die)
4598 return;
4600 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4601 if (a->dw_attr == attr_kind)
4603 if (AT_class (a) == dw_val_class_str)
4604 if (a->dw_attr_val.v.val_str->refcount)
4605 a->dw_attr_val.v.val_str->refcount--;
4607 /* vec::ordered_remove should help reduce the number of abbrevs
4608 that are needed. */
4609 die->die_attr->ordered_remove (ix);
4610 return;
4614 /* Remove CHILD from its parent. PREV must have the property that
4615 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4617 static void
4618 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4620 gcc_assert (child->die_parent == prev->die_parent);
4621 gcc_assert (prev->die_sib == child);
4622 if (prev == child)
4624 gcc_assert (child->die_parent->die_child == child);
4625 prev = NULL;
4627 else
4628 prev->die_sib = child->die_sib;
4629 if (child->die_parent->die_child == child)
4630 child->die_parent->die_child = prev;
4633 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4634 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4636 static void
4637 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4639 dw_die_ref parent = old_child->die_parent;
4641 gcc_assert (parent == prev->die_parent);
4642 gcc_assert (prev->die_sib == old_child);
4644 new_child->die_parent = parent;
4645 if (prev == old_child)
4647 gcc_assert (parent->die_child == old_child);
4648 new_child->die_sib = new_child;
4650 else
4652 prev->die_sib = new_child;
4653 new_child->die_sib = old_child->die_sib;
4655 if (old_child->die_parent->die_child == old_child)
4656 old_child->die_parent->die_child = new_child;
4659 /* Move all children from OLD_PARENT to NEW_PARENT. */
4661 static void
4662 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4664 dw_die_ref c;
4665 new_parent->die_child = old_parent->die_child;
4666 old_parent->die_child = NULL;
4667 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4670 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4671 matches TAG. */
4673 static void
4674 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4676 dw_die_ref c;
4678 c = die->die_child;
4679 if (c) do {
4680 dw_die_ref prev = c;
4681 c = c->die_sib;
4682 while (c->die_tag == tag)
4684 remove_child_with_prev (c, prev);
4685 /* Might have removed every child. */
4686 if (c == c->die_sib)
4687 return;
4688 c = c->die_sib;
4690 } while (c != die->die_child);
4693 /* Add a CHILD_DIE as the last child of DIE. */
4695 static void
4696 add_child_die (dw_die_ref die, dw_die_ref child_die)
4698 /* FIXME this should probably be an assert. */
4699 if (! die || ! child_die)
4700 return;
4701 gcc_assert (die != child_die);
4703 child_die->die_parent = die;
4704 if (die->die_child)
4706 child_die->die_sib = die->die_child->die_sib;
4707 die->die_child->die_sib = child_die;
4709 else
4710 child_die->die_sib = child_die;
4711 die->die_child = child_die;
4714 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4715 is the specification, to the end of PARENT's list of children.
4716 This is done by removing and re-adding it. */
4718 static void
4719 splice_child_die (dw_die_ref parent, dw_die_ref child)
4721 dw_die_ref p;
4723 /* We want the declaration DIE from inside the class, not the
4724 specification DIE at toplevel. */
4725 if (child->die_parent != parent)
4727 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4729 if (tmp)
4730 child = tmp;
4733 gcc_assert (child->die_parent == parent
4734 || (child->die_parent
4735 == get_AT_ref (parent, DW_AT_specification)));
4737 for (p = child->die_parent->die_child; ; p = p->die_sib)
4738 if (p->die_sib == child)
4740 remove_child_with_prev (child, p);
4741 break;
4744 add_child_die (parent, child);
4747 /* Return a pointer to a newly created DIE node. */
4749 static inline dw_die_ref
4750 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4752 dw_die_ref die = ggc_alloc_cleared_die_node ();
4754 die->die_tag = tag_value;
4756 if (parent_die != NULL)
4757 add_child_die (parent_die, die);
4758 else
4760 limbo_die_node *limbo_node;
4762 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4763 limbo_node->die = die;
4764 limbo_node->created_for = t;
4765 limbo_node->next = limbo_die_list;
4766 limbo_die_list = limbo_node;
4769 return die;
4772 /* Return the DIE associated with the given type specifier. */
4774 static inline dw_die_ref
4775 lookup_type_die (tree type)
4777 return TYPE_SYMTAB_DIE (type);
4780 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4781 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4782 anonymous type instead the one of the naming typedef. */
4784 static inline dw_die_ref
4785 strip_naming_typedef (tree type, dw_die_ref type_die)
4787 if (type
4788 && TREE_CODE (type) == RECORD_TYPE
4789 && type_die
4790 && type_die->die_tag == DW_TAG_typedef
4791 && is_naming_typedef_decl (TYPE_NAME (type)))
4792 type_die = get_AT_ref (type_die, DW_AT_type);
4793 return type_die;
4796 /* Like lookup_type_die, but if type is an anonymous type named by a
4797 typedef[1], return the DIE of the anonymous type instead the one of
4798 the naming typedef. This is because in gen_typedef_die, we did
4799 equate the anonymous struct named by the typedef with the DIE of
4800 the naming typedef. So by default, lookup_type_die on an anonymous
4801 struct yields the DIE of the naming typedef.
4803 [1]: Read the comment of is_naming_typedef_decl to learn about what
4804 a naming typedef is. */
4806 static inline dw_die_ref
4807 lookup_type_die_strip_naming_typedef (tree type)
4809 dw_die_ref die = lookup_type_die (type);
4810 return strip_naming_typedef (type, die);
4813 /* Equate a DIE to a given type specifier. */
4815 static inline void
4816 equate_type_number_to_die (tree type, dw_die_ref type_die)
4818 TYPE_SYMTAB_DIE (type) = type_die;
4821 /* Returns a hash value for X (which really is a die_struct). */
4823 static hashval_t
4824 decl_die_table_hash (const void *x)
4826 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4829 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4831 static int
4832 decl_die_table_eq (const void *x, const void *y)
4834 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4837 /* Return the DIE associated with a given declaration. */
4839 static inline dw_die_ref
4840 lookup_decl_die (tree decl)
4842 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4845 /* Returns a hash value for X (which really is a var_loc_list). */
4847 static hashval_t
4848 decl_loc_table_hash (const void *x)
4850 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4853 /* Return nonzero if decl_id of var_loc_list X is the same as
4854 UID of decl *Y. */
4856 static int
4857 decl_loc_table_eq (const void *x, const void *y)
4859 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4862 /* Return the var_loc list associated with a given declaration. */
4864 static inline var_loc_list *
4865 lookup_decl_loc (const_tree decl)
4867 if (!decl_loc_table)
4868 return NULL;
4869 return (var_loc_list *)
4870 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4873 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4875 static hashval_t
4876 cached_dw_loc_list_table_hash (const void *x)
4878 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4881 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4882 UID of decl *Y. */
4884 static int
4885 cached_dw_loc_list_table_eq (const void *x, const void *y)
4887 return (((const cached_dw_loc_list *) x)->decl_id
4888 == DECL_UID ((const_tree) y));
4891 /* Equate a DIE to a particular declaration. */
4893 static void
4894 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4896 unsigned int decl_id = DECL_UID (decl);
4897 void **slot;
4899 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4900 *slot = decl_die;
4901 decl_die->decl_id = decl_id;
4904 /* Return how many bits covers PIECE EXPR_LIST. */
4906 static int
4907 decl_piece_bitsize (rtx piece)
4909 int ret = (int) GET_MODE (piece);
4910 if (ret)
4911 return ret;
4912 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4913 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4914 return INTVAL (XEXP (XEXP (piece, 0), 0));
4917 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4919 static rtx *
4920 decl_piece_varloc_ptr (rtx piece)
4922 if ((int) GET_MODE (piece))
4923 return &XEXP (piece, 0);
4924 else
4925 return &XEXP (XEXP (piece, 0), 1);
4928 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4929 Next is the chain of following piece nodes. */
4931 static rtx
4932 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4934 if (bitsize <= (int) MAX_MACHINE_MODE)
4935 return alloc_EXPR_LIST (bitsize, loc_note, next);
4936 else
4937 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4938 GEN_INT (bitsize),
4939 loc_note), next);
4942 /* Return rtx that should be stored into loc field for
4943 LOC_NOTE and BITPOS/BITSIZE. */
4945 static rtx
4946 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4947 HOST_WIDE_INT bitsize)
4949 if (bitsize != -1)
4951 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4952 if (bitpos != 0)
4953 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4955 return loc_note;
4958 /* This function either modifies location piece list *DEST in
4959 place (if SRC and INNER is NULL), or copies location piece list
4960 *SRC to *DEST while modifying it. Location BITPOS is modified
4961 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4962 not copied and if needed some padding around it is added.
4963 When modifying in place, DEST should point to EXPR_LIST where
4964 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4965 to the start of the whole list and INNER points to the EXPR_LIST
4966 where earlier pieces cover PIECE_BITPOS bits. */
4968 static void
4969 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4970 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4971 HOST_WIDE_INT bitsize, rtx loc_note)
4973 int diff;
4974 bool copy = inner != NULL;
4976 if (copy)
4978 /* First copy all nodes preceding the current bitpos. */
4979 while (src != inner)
4981 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4982 decl_piece_bitsize (*src), NULL_RTX);
4983 dest = &XEXP (*dest, 1);
4984 src = &XEXP (*src, 1);
4987 /* Add padding if needed. */
4988 if (bitpos != piece_bitpos)
4990 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4991 copy ? NULL_RTX : *dest);
4992 dest = &XEXP (*dest, 1);
4994 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4996 gcc_assert (!copy);
4997 /* A piece with correct bitpos and bitsize already exist,
4998 just update the location for it and return. */
4999 *decl_piece_varloc_ptr (*dest) = loc_note;
5000 return;
5002 /* Add the piece that changed. */
5003 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5004 dest = &XEXP (*dest, 1);
5005 /* Skip over pieces that overlap it. */
5006 diff = bitpos - piece_bitpos + bitsize;
5007 if (!copy)
5008 src = dest;
5009 while (diff > 0 && *src)
5011 rtx piece = *src;
5012 diff -= decl_piece_bitsize (piece);
5013 if (copy)
5014 src = &XEXP (piece, 1);
5015 else
5017 *src = XEXP (piece, 1);
5018 free_EXPR_LIST_node (piece);
5021 /* Add padding if needed. */
5022 if (diff < 0 && *src)
5024 if (!copy)
5025 dest = src;
5026 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5027 dest = &XEXP (*dest, 1);
5029 if (!copy)
5030 return;
5031 /* Finally copy all nodes following it. */
5032 while (*src)
5034 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5035 decl_piece_bitsize (*src), NULL_RTX);
5036 dest = &XEXP (*dest, 1);
5037 src = &XEXP (*src, 1);
5041 /* Add a variable location node to the linked list for DECL. */
5043 static struct var_loc_node *
5044 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5046 unsigned int decl_id;
5047 var_loc_list *temp;
5048 void **slot;
5049 struct var_loc_node *loc = NULL;
5050 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5052 if (TREE_CODE (decl) == VAR_DECL
5053 && DECL_HAS_DEBUG_EXPR_P (decl))
5055 tree realdecl = DECL_DEBUG_EXPR (decl);
5056 if (handled_component_p (realdecl)
5057 || (TREE_CODE (realdecl) == MEM_REF
5058 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5060 HOST_WIDE_INT maxsize;
5061 tree innerdecl;
5062 innerdecl
5063 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5064 if (!DECL_P (innerdecl)
5065 || DECL_IGNORED_P (innerdecl)
5066 || TREE_STATIC (innerdecl)
5067 || bitsize <= 0
5068 || bitpos + bitsize > 256
5069 || bitsize != maxsize)
5070 return NULL;
5071 decl = innerdecl;
5075 decl_id = DECL_UID (decl);
5076 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5077 if (*slot == NULL)
5079 temp = ggc_alloc_cleared_var_loc_list ();
5080 temp->decl_id = decl_id;
5081 *slot = temp;
5083 else
5084 temp = (var_loc_list *) *slot;
5086 /* For PARM_DECLs try to keep around the original incoming value,
5087 even if that means we'll emit a zero-range .debug_loc entry. */
5088 if (temp->last
5089 && temp->first == temp->last
5090 && TREE_CODE (decl) == PARM_DECL
5091 && NOTE_P (temp->first->loc)
5092 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5093 && DECL_INCOMING_RTL (decl)
5094 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5095 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5096 == GET_CODE (DECL_INCOMING_RTL (decl))
5097 && prev_real_insn (temp->first->loc) == NULL_RTX
5098 && (bitsize != -1
5099 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5100 NOTE_VAR_LOCATION_LOC (loc_note))
5101 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5102 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5104 loc = ggc_alloc_cleared_var_loc_node ();
5105 temp->first->next = loc;
5106 temp->last = loc;
5107 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5109 else if (temp->last)
5111 struct var_loc_node *last = temp->last, *unused = NULL;
5112 rtx *piece_loc = NULL, last_loc_note;
5113 int piece_bitpos = 0;
5114 if (last->next)
5116 last = last->next;
5117 gcc_assert (last->next == NULL);
5119 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5121 piece_loc = &last->loc;
5124 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5125 if (piece_bitpos + cur_bitsize > bitpos)
5126 break;
5127 piece_bitpos += cur_bitsize;
5128 piece_loc = &XEXP (*piece_loc, 1);
5130 while (*piece_loc);
5132 /* TEMP->LAST here is either pointer to the last but one or
5133 last element in the chained list, LAST is pointer to the
5134 last element. */
5135 if (label && strcmp (last->label, label) == 0)
5137 /* For SRA optimized variables if there weren't any real
5138 insns since last note, just modify the last node. */
5139 if (piece_loc != NULL)
5141 adjust_piece_list (piece_loc, NULL, NULL,
5142 bitpos, piece_bitpos, bitsize, loc_note);
5143 return NULL;
5145 /* If the last note doesn't cover any instructions, remove it. */
5146 if (temp->last != last)
5148 temp->last->next = NULL;
5149 unused = last;
5150 last = temp->last;
5151 gcc_assert (strcmp (last->label, label) != 0);
5153 else
5155 gcc_assert (temp->first == temp->last
5156 || (temp->first->next == temp->last
5157 && TREE_CODE (decl) == PARM_DECL));
5158 memset (temp->last, '\0', sizeof (*temp->last));
5159 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5160 return temp->last;
5163 if (bitsize == -1 && NOTE_P (last->loc))
5164 last_loc_note = last->loc;
5165 else if (piece_loc != NULL
5166 && *piece_loc != NULL_RTX
5167 && piece_bitpos == bitpos
5168 && decl_piece_bitsize (*piece_loc) == bitsize)
5169 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5170 else
5171 last_loc_note = NULL_RTX;
5172 /* If the current location is the same as the end of the list,
5173 and either both or neither of the locations is uninitialized,
5174 we have nothing to do. */
5175 if (last_loc_note == NULL_RTX
5176 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5177 NOTE_VAR_LOCATION_LOC (loc_note)))
5178 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5179 != NOTE_VAR_LOCATION_STATUS (loc_note))
5180 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5181 == VAR_INIT_STATUS_UNINITIALIZED)
5182 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5183 == VAR_INIT_STATUS_UNINITIALIZED))))
5185 /* Add LOC to the end of list and update LAST. If the last
5186 element of the list has been removed above, reuse its
5187 memory for the new node, otherwise allocate a new one. */
5188 if (unused)
5190 loc = unused;
5191 memset (loc, '\0', sizeof (*loc));
5193 else
5194 loc = ggc_alloc_cleared_var_loc_node ();
5195 if (bitsize == -1 || piece_loc == NULL)
5196 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5197 else
5198 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5199 bitpos, piece_bitpos, bitsize, loc_note);
5200 last->next = loc;
5201 /* Ensure TEMP->LAST will point either to the new last but one
5202 element of the chain, or to the last element in it. */
5203 if (last != temp->last)
5204 temp->last = last;
5206 else if (unused)
5207 ggc_free (unused);
5209 else
5211 loc = ggc_alloc_cleared_var_loc_node ();
5212 temp->first = loc;
5213 temp->last = loc;
5214 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5216 return loc;
5219 /* Keep track of the number of spaces used to indent the
5220 output of the debugging routines that print the structure of
5221 the DIE internal representation. */
5222 static int print_indent;
5224 /* Indent the line the number of spaces given by print_indent. */
5226 static inline void
5227 print_spaces (FILE *outfile)
5229 fprintf (outfile, "%*s", print_indent, "");
5232 /* Print a type signature in hex. */
5234 static inline void
5235 print_signature (FILE *outfile, char *sig)
5237 int i;
5239 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5240 fprintf (outfile, "%02x", sig[i] & 0xff);
5243 /* Print the information associated with a given DIE, and its children.
5244 This routine is a debugging aid only. */
5246 static void
5247 print_die (dw_die_ref die, FILE *outfile)
5249 dw_attr_ref a;
5250 dw_die_ref c;
5251 unsigned ix;
5253 print_spaces (outfile);
5254 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5255 die->die_offset, dwarf_tag_name (die->die_tag),
5256 (void*) die);
5257 print_spaces (outfile);
5258 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5259 fprintf (outfile, " offset: %ld", die->die_offset);
5260 fprintf (outfile, " mark: %d\n", die->die_mark);
5262 if (die->comdat_type_p)
5264 print_spaces (outfile);
5265 fprintf (outfile, " signature: ");
5266 print_signature (outfile, die->die_id.die_type_node->signature);
5267 fprintf (outfile, "\n");
5270 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5272 print_spaces (outfile);
5273 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5275 switch (AT_class (a))
5277 case dw_val_class_addr:
5278 fprintf (outfile, "address");
5279 break;
5280 case dw_val_class_offset:
5281 fprintf (outfile, "offset");
5282 break;
5283 case dw_val_class_loc:
5284 fprintf (outfile, "location descriptor");
5285 break;
5286 case dw_val_class_loc_list:
5287 fprintf (outfile, "location list -> label:%s",
5288 AT_loc_list (a)->ll_symbol);
5289 break;
5290 case dw_val_class_range_list:
5291 fprintf (outfile, "range list");
5292 break;
5293 case dw_val_class_const:
5294 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5295 break;
5296 case dw_val_class_unsigned_const:
5297 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5298 break;
5299 case dw_val_class_const_double:
5300 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5301 HOST_WIDE_INT_PRINT_UNSIGNED")",
5302 a->dw_attr_val.v.val_double.high,
5303 a->dw_attr_val.v.val_double.low);
5304 break;
5305 case dw_val_class_vec:
5306 fprintf (outfile, "floating-point or vector constant");
5307 break;
5308 case dw_val_class_flag:
5309 fprintf (outfile, "%u", AT_flag (a));
5310 break;
5311 case dw_val_class_die_ref:
5312 if (AT_ref (a) != NULL)
5314 if (AT_ref (a)->comdat_type_p)
5316 fprintf (outfile, "die -> signature: ");
5317 print_signature (outfile,
5318 AT_ref (a)->die_id.die_type_node->signature);
5320 else if (AT_ref (a)->die_id.die_symbol)
5321 fprintf (outfile, "die -> label: %s",
5322 AT_ref (a)->die_id.die_symbol);
5323 else
5324 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5325 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5327 else
5328 fprintf (outfile, "die -> <null>");
5329 break;
5330 case dw_val_class_vms_delta:
5331 fprintf (outfile, "delta: @slotcount(%s-%s)",
5332 AT_vms_delta2 (a), AT_vms_delta1 (a));
5333 break;
5334 case dw_val_class_lbl_id:
5335 case dw_val_class_lineptr:
5336 case dw_val_class_macptr:
5337 case dw_val_class_high_pc:
5338 fprintf (outfile, "label: %s", AT_lbl (a));
5339 break;
5340 case dw_val_class_str:
5341 if (AT_string (a) != NULL)
5342 fprintf (outfile, "\"%s\"", AT_string (a));
5343 else
5344 fprintf (outfile, "<null>");
5345 break;
5346 case dw_val_class_file:
5347 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5348 AT_file (a)->emitted_number);
5349 break;
5350 case dw_val_class_data8:
5352 int i;
5354 for (i = 0; i < 8; i++)
5355 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5356 break;
5358 default:
5359 break;
5362 fprintf (outfile, "\n");
5365 if (die->die_child != NULL)
5367 print_indent += 4;
5368 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5369 print_indent -= 4;
5371 if (print_indent == 0)
5372 fprintf (outfile, "\n");
5375 /* Print the information collected for a given DIE. */
5377 DEBUG_FUNCTION void
5378 debug_dwarf_die (dw_die_ref die)
5380 print_die (die, stderr);
5383 DEBUG_FUNCTION void
5384 debug (die_struct &ref)
5386 print_die (&ref, stderr);
5389 DEBUG_FUNCTION void
5390 debug (die_struct *ptr)
5392 if (ptr)
5393 debug (*ptr);
5394 else
5395 fprintf (stderr, "<nil>\n");
5399 /* Print all DWARF information collected for the compilation unit.
5400 This routine is a debugging aid only. */
5402 DEBUG_FUNCTION void
5403 debug_dwarf (void)
5405 print_indent = 0;
5406 print_die (comp_unit_die (), stderr);
5409 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5410 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5411 DIE that marks the start of the DIEs for this include file. */
5413 static dw_die_ref
5414 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5416 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5417 dw_die_ref new_unit = gen_compile_unit_die (filename);
5419 new_unit->die_sib = old_unit;
5420 return new_unit;
5423 /* Close an include-file CU and reopen the enclosing one. */
5425 static dw_die_ref
5426 pop_compile_unit (dw_die_ref old_unit)
5428 dw_die_ref new_unit = old_unit->die_sib;
5430 old_unit->die_sib = NULL;
5431 return new_unit;
5434 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5435 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5437 /* Calculate the checksum of a location expression. */
5439 static inline void
5440 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5442 int tem;
5443 hashval_t hash = 0;
5445 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5446 CHECKSUM (tem);
5447 hash = hash_loc_operands (loc, hash);
5448 CHECKSUM (hash);
5451 /* Calculate the checksum of an attribute. */
5453 static void
5454 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5456 dw_loc_descr_ref loc;
5457 rtx r;
5459 CHECKSUM (at->dw_attr);
5461 /* We don't care that this was compiled with a different compiler
5462 snapshot; if the output is the same, that's what matters. */
5463 if (at->dw_attr == DW_AT_producer)
5464 return;
5466 switch (AT_class (at))
5468 case dw_val_class_const:
5469 CHECKSUM (at->dw_attr_val.v.val_int);
5470 break;
5471 case dw_val_class_unsigned_const:
5472 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5473 break;
5474 case dw_val_class_const_double:
5475 CHECKSUM (at->dw_attr_val.v.val_double);
5476 break;
5477 case dw_val_class_vec:
5478 CHECKSUM (at->dw_attr_val.v.val_vec);
5479 break;
5480 case dw_val_class_flag:
5481 CHECKSUM (at->dw_attr_val.v.val_flag);
5482 break;
5483 case dw_val_class_str:
5484 CHECKSUM_STRING (AT_string (at));
5485 break;
5487 case dw_val_class_addr:
5488 r = AT_addr (at);
5489 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5490 CHECKSUM_STRING (XSTR (r, 0));
5491 break;
5493 case dw_val_class_offset:
5494 CHECKSUM (at->dw_attr_val.v.val_offset);
5495 break;
5497 case dw_val_class_loc:
5498 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5499 loc_checksum (loc, ctx);
5500 break;
5502 case dw_val_class_die_ref:
5503 die_checksum (AT_ref (at), ctx, mark);
5504 break;
5506 case dw_val_class_fde_ref:
5507 case dw_val_class_vms_delta:
5508 case dw_val_class_lbl_id:
5509 case dw_val_class_lineptr:
5510 case dw_val_class_macptr:
5511 case dw_val_class_high_pc:
5512 break;
5514 case dw_val_class_file:
5515 CHECKSUM_STRING (AT_file (at)->filename);
5516 break;
5518 case dw_val_class_data8:
5519 CHECKSUM (at->dw_attr_val.v.val_data8);
5520 break;
5522 default:
5523 break;
5527 /* Calculate the checksum of a DIE. */
5529 static void
5530 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5532 dw_die_ref c;
5533 dw_attr_ref a;
5534 unsigned ix;
5536 /* To avoid infinite recursion. */
5537 if (die->die_mark)
5539 CHECKSUM (die->die_mark);
5540 return;
5542 die->die_mark = ++(*mark);
5544 CHECKSUM (die->die_tag);
5546 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5547 attr_checksum (a, ctx, mark);
5549 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5552 #undef CHECKSUM
5553 #undef CHECKSUM_STRING
5555 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5556 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5557 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5558 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5559 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5560 #define CHECKSUM_ATTR(FOO) \
5561 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5563 /* Calculate the checksum of a number in signed LEB128 format. */
5565 static void
5566 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5568 unsigned char byte;
5569 bool more;
5571 while (1)
5573 byte = (value & 0x7f);
5574 value >>= 7;
5575 more = !((value == 0 && (byte & 0x40) == 0)
5576 || (value == -1 && (byte & 0x40) != 0));
5577 if (more)
5578 byte |= 0x80;
5579 CHECKSUM (byte);
5580 if (!more)
5581 break;
5585 /* Calculate the checksum of a number in unsigned LEB128 format. */
5587 static void
5588 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5590 while (1)
5592 unsigned char byte = (value & 0x7f);
5593 value >>= 7;
5594 if (value != 0)
5595 /* More bytes to follow. */
5596 byte |= 0x80;
5597 CHECKSUM (byte);
5598 if (value == 0)
5599 break;
5603 /* Checksum the context of the DIE. This adds the names of any
5604 surrounding namespaces or structures to the checksum. */
5606 static void
5607 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5609 const char *name;
5610 dw_die_ref spec;
5611 int tag = die->die_tag;
5613 if (tag != DW_TAG_namespace
5614 && tag != DW_TAG_structure_type
5615 && tag != DW_TAG_class_type)
5616 return;
5618 name = get_AT_string (die, DW_AT_name);
5620 spec = get_AT_ref (die, DW_AT_specification);
5621 if (spec != NULL)
5622 die = spec;
5624 if (die->die_parent != NULL)
5625 checksum_die_context (die->die_parent, ctx);
5627 CHECKSUM_ULEB128 ('C');
5628 CHECKSUM_ULEB128 (tag);
5629 if (name != NULL)
5630 CHECKSUM_STRING (name);
5633 /* Calculate the checksum of a location expression. */
5635 static inline void
5636 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5638 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5639 were emitted as a DW_FORM_sdata instead of a location expression. */
5640 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5642 CHECKSUM_ULEB128 (DW_FORM_sdata);
5643 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5644 return;
5647 /* Otherwise, just checksum the raw location expression. */
5648 while (loc != NULL)
5650 hashval_t hash = 0;
5652 CHECKSUM_ULEB128 (loc->dtprel);
5653 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5654 hash = hash_loc_operands (loc, hash);
5655 CHECKSUM (hash);
5656 loc = loc->dw_loc_next;
5660 /* Calculate the checksum of an attribute. */
5662 static void
5663 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5664 struct md5_ctx *ctx, int *mark)
5666 dw_loc_descr_ref loc;
5667 rtx r;
5669 if (AT_class (at) == dw_val_class_die_ref)
5671 dw_die_ref target_die = AT_ref (at);
5673 /* For pointer and reference types, we checksum only the (qualified)
5674 name of the target type (if there is a name). For friend entries,
5675 we checksum only the (qualified) name of the target type or function.
5676 This allows the checksum to remain the same whether the target type
5677 is complete or not. */
5678 if ((at->dw_attr == DW_AT_type
5679 && (tag == DW_TAG_pointer_type
5680 || tag == DW_TAG_reference_type
5681 || tag == DW_TAG_rvalue_reference_type
5682 || tag == DW_TAG_ptr_to_member_type))
5683 || (at->dw_attr == DW_AT_friend
5684 && tag == DW_TAG_friend))
5686 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5688 if (name_attr != NULL)
5690 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5692 if (decl == NULL)
5693 decl = target_die;
5694 CHECKSUM_ULEB128 ('N');
5695 CHECKSUM_ULEB128 (at->dw_attr);
5696 if (decl->die_parent != NULL)
5697 checksum_die_context (decl->die_parent, ctx);
5698 CHECKSUM_ULEB128 ('E');
5699 CHECKSUM_STRING (AT_string (name_attr));
5700 return;
5704 /* For all other references to another DIE, we check to see if the
5705 target DIE has already been visited. If it has, we emit a
5706 backward reference; if not, we descend recursively. */
5707 if (target_die->die_mark > 0)
5709 CHECKSUM_ULEB128 ('R');
5710 CHECKSUM_ULEB128 (at->dw_attr);
5711 CHECKSUM_ULEB128 (target_die->die_mark);
5713 else
5715 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5717 if (decl == NULL)
5718 decl = target_die;
5719 target_die->die_mark = ++(*mark);
5720 CHECKSUM_ULEB128 ('T');
5721 CHECKSUM_ULEB128 (at->dw_attr);
5722 if (decl->die_parent != NULL)
5723 checksum_die_context (decl->die_parent, ctx);
5724 die_checksum_ordered (target_die, ctx, mark);
5726 return;
5729 CHECKSUM_ULEB128 ('A');
5730 CHECKSUM_ULEB128 (at->dw_attr);
5732 switch (AT_class (at))
5734 case dw_val_class_const:
5735 CHECKSUM_ULEB128 (DW_FORM_sdata);
5736 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5737 break;
5739 case dw_val_class_unsigned_const:
5740 CHECKSUM_ULEB128 (DW_FORM_sdata);
5741 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5742 break;
5744 case dw_val_class_const_double:
5745 CHECKSUM_ULEB128 (DW_FORM_block);
5746 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5747 CHECKSUM (at->dw_attr_val.v.val_double);
5748 break;
5750 case dw_val_class_vec:
5751 CHECKSUM_ULEB128 (DW_FORM_block);
5752 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5753 CHECKSUM (at->dw_attr_val.v.val_vec);
5754 break;
5756 case dw_val_class_flag:
5757 CHECKSUM_ULEB128 (DW_FORM_flag);
5758 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5759 break;
5761 case dw_val_class_str:
5762 CHECKSUM_ULEB128 (DW_FORM_string);
5763 CHECKSUM_STRING (AT_string (at));
5764 break;
5766 case dw_val_class_addr:
5767 r = AT_addr (at);
5768 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5769 CHECKSUM_ULEB128 (DW_FORM_string);
5770 CHECKSUM_STRING (XSTR (r, 0));
5771 break;
5773 case dw_val_class_offset:
5774 CHECKSUM_ULEB128 (DW_FORM_sdata);
5775 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5776 break;
5778 case dw_val_class_loc:
5779 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5780 loc_checksum_ordered (loc, ctx);
5781 break;
5783 case dw_val_class_fde_ref:
5784 case dw_val_class_lbl_id:
5785 case dw_val_class_lineptr:
5786 case dw_val_class_macptr:
5787 case dw_val_class_high_pc:
5788 break;
5790 case dw_val_class_file:
5791 CHECKSUM_ULEB128 (DW_FORM_string);
5792 CHECKSUM_STRING (AT_file (at)->filename);
5793 break;
5795 case dw_val_class_data8:
5796 CHECKSUM (at->dw_attr_val.v.val_data8);
5797 break;
5799 default:
5800 break;
5804 struct checksum_attributes
5806 dw_attr_ref at_name;
5807 dw_attr_ref at_type;
5808 dw_attr_ref at_friend;
5809 dw_attr_ref at_accessibility;
5810 dw_attr_ref at_address_class;
5811 dw_attr_ref at_allocated;
5812 dw_attr_ref at_artificial;
5813 dw_attr_ref at_associated;
5814 dw_attr_ref at_binary_scale;
5815 dw_attr_ref at_bit_offset;
5816 dw_attr_ref at_bit_size;
5817 dw_attr_ref at_bit_stride;
5818 dw_attr_ref at_byte_size;
5819 dw_attr_ref at_byte_stride;
5820 dw_attr_ref at_const_value;
5821 dw_attr_ref at_containing_type;
5822 dw_attr_ref at_count;
5823 dw_attr_ref at_data_location;
5824 dw_attr_ref at_data_member_location;
5825 dw_attr_ref at_decimal_scale;
5826 dw_attr_ref at_decimal_sign;
5827 dw_attr_ref at_default_value;
5828 dw_attr_ref at_digit_count;
5829 dw_attr_ref at_discr;
5830 dw_attr_ref at_discr_list;
5831 dw_attr_ref at_discr_value;
5832 dw_attr_ref at_encoding;
5833 dw_attr_ref at_endianity;
5834 dw_attr_ref at_explicit;
5835 dw_attr_ref at_is_optional;
5836 dw_attr_ref at_location;
5837 dw_attr_ref at_lower_bound;
5838 dw_attr_ref at_mutable;
5839 dw_attr_ref at_ordering;
5840 dw_attr_ref at_picture_string;
5841 dw_attr_ref at_prototyped;
5842 dw_attr_ref at_small;
5843 dw_attr_ref at_segment;
5844 dw_attr_ref at_string_length;
5845 dw_attr_ref at_threads_scaled;
5846 dw_attr_ref at_upper_bound;
5847 dw_attr_ref at_use_location;
5848 dw_attr_ref at_use_UTF8;
5849 dw_attr_ref at_variable_parameter;
5850 dw_attr_ref at_virtuality;
5851 dw_attr_ref at_visibility;
5852 dw_attr_ref at_vtable_elem_location;
5855 /* Collect the attributes that we will want to use for the checksum. */
5857 static void
5858 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5860 dw_attr_ref a;
5861 unsigned ix;
5863 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5865 switch (a->dw_attr)
5867 case DW_AT_name:
5868 attrs->at_name = a;
5869 break;
5870 case DW_AT_type:
5871 attrs->at_type = a;
5872 break;
5873 case DW_AT_friend:
5874 attrs->at_friend = a;
5875 break;
5876 case DW_AT_accessibility:
5877 attrs->at_accessibility = a;
5878 break;
5879 case DW_AT_address_class:
5880 attrs->at_address_class = a;
5881 break;
5882 case DW_AT_allocated:
5883 attrs->at_allocated = a;
5884 break;
5885 case DW_AT_artificial:
5886 attrs->at_artificial = a;
5887 break;
5888 case DW_AT_associated:
5889 attrs->at_associated = a;
5890 break;
5891 case DW_AT_binary_scale:
5892 attrs->at_binary_scale = a;
5893 break;
5894 case DW_AT_bit_offset:
5895 attrs->at_bit_offset = a;
5896 break;
5897 case DW_AT_bit_size:
5898 attrs->at_bit_size = a;
5899 break;
5900 case DW_AT_bit_stride:
5901 attrs->at_bit_stride = a;
5902 break;
5903 case DW_AT_byte_size:
5904 attrs->at_byte_size = a;
5905 break;
5906 case DW_AT_byte_stride:
5907 attrs->at_byte_stride = a;
5908 break;
5909 case DW_AT_const_value:
5910 attrs->at_const_value = a;
5911 break;
5912 case DW_AT_containing_type:
5913 attrs->at_containing_type = a;
5914 break;
5915 case DW_AT_count:
5916 attrs->at_count = a;
5917 break;
5918 case DW_AT_data_location:
5919 attrs->at_data_location = a;
5920 break;
5921 case DW_AT_data_member_location:
5922 attrs->at_data_member_location = a;
5923 break;
5924 case DW_AT_decimal_scale:
5925 attrs->at_decimal_scale = a;
5926 break;
5927 case DW_AT_decimal_sign:
5928 attrs->at_decimal_sign = a;
5929 break;
5930 case DW_AT_default_value:
5931 attrs->at_default_value = a;
5932 break;
5933 case DW_AT_digit_count:
5934 attrs->at_digit_count = a;
5935 break;
5936 case DW_AT_discr:
5937 attrs->at_discr = a;
5938 break;
5939 case DW_AT_discr_list:
5940 attrs->at_discr_list = a;
5941 break;
5942 case DW_AT_discr_value:
5943 attrs->at_discr_value = a;
5944 break;
5945 case DW_AT_encoding:
5946 attrs->at_encoding = a;
5947 break;
5948 case DW_AT_endianity:
5949 attrs->at_endianity = a;
5950 break;
5951 case DW_AT_explicit:
5952 attrs->at_explicit = a;
5953 break;
5954 case DW_AT_is_optional:
5955 attrs->at_is_optional = a;
5956 break;
5957 case DW_AT_location:
5958 attrs->at_location = a;
5959 break;
5960 case DW_AT_lower_bound:
5961 attrs->at_lower_bound = a;
5962 break;
5963 case DW_AT_mutable:
5964 attrs->at_mutable = a;
5965 break;
5966 case DW_AT_ordering:
5967 attrs->at_ordering = a;
5968 break;
5969 case DW_AT_picture_string:
5970 attrs->at_picture_string = a;
5971 break;
5972 case DW_AT_prototyped:
5973 attrs->at_prototyped = a;
5974 break;
5975 case DW_AT_small:
5976 attrs->at_small = a;
5977 break;
5978 case DW_AT_segment:
5979 attrs->at_segment = a;
5980 break;
5981 case DW_AT_string_length:
5982 attrs->at_string_length = a;
5983 break;
5984 case DW_AT_threads_scaled:
5985 attrs->at_threads_scaled = a;
5986 break;
5987 case DW_AT_upper_bound:
5988 attrs->at_upper_bound = a;
5989 break;
5990 case DW_AT_use_location:
5991 attrs->at_use_location = a;
5992 break;
5993 case DW_AT_use_UTF8:
5994 attrs->at_use_UTF8 = a;
5995 break;
5996 case DW_AT_variable_parameter:
5997 attrs->at_variable_parameter = a;
5998 break;
5999 case DW_AT_virtuality:
6000 attrs->at_virtuality = a;
6001 break;
6002 case DW_AT_visibility:
6003 attrs->at_visibility = a;
6004 break;
6005 case DW_AT_vtable_elem_location:
6006 attrs->at_vtable_elem_location = a;
6007 break;
6008 default:
6009 break;
6014 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6016 static void
6017 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6019 dw_die_ref c;
6020 dw_die_ref decl;
6021 struct checksum_attributes attrs;
6023 CHECKSUM_ULEB128 ('D');
6024 CHECKSUM_ULEB128 (die->die_tag);
6026 memset (&attrs, 0, sizeof (attrs));
6028 decl = get_AT_ref (die, DW_AT_specification);
6029 if (decl != NULL)
6030 collect_checksum_attributes (&attrs, decl);
6031 collect_checksum_attributes (&attrs, die);
6033 CHECKSUM_ATTR (attrs.at_name);
6034 CHECKSUM_ATTR (attrs.at_accessibility);
6035 CHECKSUM_ATTR (attrs.at_address_class);
6036 CHECKSUM_ATTR (attrs.at_allocated);
6037 CHECKSUM_ATTR (attrs.at_artificial);
6038 CHECKSUM_ATTR (attrs.at_associated);
6039 CHECKSUM_ATTR (attrs.at_binary_scale);
6040 CHECKSUM_ATTR (attrs.at_bit_offset);
6041 CHECKSUM_ATTR (attrs.at_bit_size);
6042 CHECKSUM_ATTR (attrs.at_bit_stride);
6043 CHECKSUM_ATTR (attrs.at_byte_size);
6044 CHECKSUM_ATTR (attrs.at_byte_stride);
6045 CHECKSUM_ATTR (attrs.at_const_value);
6046 CHECKSUM_ATTR (attrs.at_containing_type);
6047 CHECKSUM_ATTR (attrs.at_count);
6048 CHECKSUM_ATTR (attrs.at_data_location);
6049 CHECKSUM_ATTR (attrs.at_data_member_location);
6050 CHECKSUM_ATTR (attrs.at_decimal_scale);
6051 CHECKSUM_ATTR (attrs.at_decimal_sign);
6052 CHECKSUM_ATTR (attrs.at_default_value);
6053 CHECKSUM_ATTR (attrs.at_digit_count);
6054 CHECKSUM_ATTR (attrs.at_discr);
6055 CHECKSUM_ATTR (attrs.at_discr_list);
6056 CHECKSUM_ATTR (attrs.at_discr_value);
6057 CHECKSUM_ATTR (attrs.at_encoding);
6058 CHECKSUM_ATTR (attrs.at_endianity);
6059 CHECKSUM_ATTR (attrs.at_explicit);
6060 CHECKSUM_ATTR (attrs.at_is_optional);
6061 CHECKSUM_ATTR (attrs.at_location);
6062 CHECKSUM_ATTR (attrs.at_lower_bound);
6063 CHECKSUM_ATTR (attrs.at_mutable);
6064 CHECKSUM_ATTR (attrs.at_ordering);
6065 CHECKSUM_ATTR (attrs.at_picture_string);
6066 CHECKSUM_ATTR (attrs.at_prototyped);
6067 CHECKSUM_ATTR (attrs.at_small);
6068 CHECKSUM_ATTR (attrs.at_segment);
6069 CHECKSUM_ATTR (attrs.at_string_length);
6070 CHECKSUM_ATTR (attrs.at_threads_scaled);
6071 CHECKSUM_ATTR (attrs.at_upper_bound);
6072 CHECKSUM_ATTR (attrs.at_use_location);
6073 CHECKSUM_ATTR (attrs.at_use_UTF8);
6074 CHECKSUM_ATTR (attrs.at_variable_parameter);
6075 CHECKSUM_ATTR (attrs.at_virtuality);
6076 CHECKSUM_ATTR (attrs.at_visibility);
6077 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6078 CHECKSUM_ATTR (attrs.at_type);
6079 CHECKSUM_ATTR (attrs.at_friend);
6081 /* Checksum the child DIEs. */
6082 c = die->die_child;
6083 if (c) do {
6084 dw_attr_ref name_attr;
6086 c = c->die_sib;
6087 name_attr = get_AT (c, DW_AT_name);
6088 if (is_template_instantiation (c))
6090 /* Ignore instantiations of member type and function templates. */
6092 else if (name_attr != NULL
6093 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6095 /* Use a shallow checksum for named nested types and member
6096 functions. */
6097 CHECKSUM_ULEB128 ('S');
6098 CHECKSUM_ULEB128 (c->die_tag);
6099 CHECKSUM_STRING (AT_string (name_attr));
6101 else
6103 /* Use a deep checksum for other children. */
6104 /* Mark this DIE so it gets processed when unmarking. */
6105 if (c->die_mark == 0)
6106 c->die_mark = -1;
6107 die_checksum_ordered (c, ctx, mark);
6109 } while (c != die->die_child);
6111 CHECKSUM_ULEB128 (0);
6114 /* Add a type name and tag to a hash. */
6115 static void
6116 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6118 CHECKSUM_ULEB128 (tag);
6119 CHECKSUM_STRING (name);
6122 #undef CHECKSUM
6123 #undef CHECKSUM_STRING
6124 #undef CHECKSUM_ATTR
6125 #undef CHECKSUM_LEB128
6126 #undef CHECKSUM_ULEB128
6128 /* Generate the type signature for DIE. This is computed by generating an
6129 MD5 checksum over the DIE's tag, its relevant attributes, and its
6130 children. Attributes that are references to other DIEs are processed
6131 by recursion, using the MARK field to prevent infinite recursion.
6132 If the DIE is nested inside a namespace or another type, we also
6133 need to include that context in the signature. The lower 64 bits
6134 of the resulting MD5 checksum comprise the signature. */
6136 static void
6137 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6139 int mark;
6140 const char *name;
6141 unsigned char checksum[16];
6142 struct md5_ctx ctx;
6143 dw_die_ref decl;
6144 dw_die_ref parent;
6146 name = get_AT_string (die, DW_AT_name);
6147 decl = get_AT_ref (die, DW_AT_specification);
6148 parent = get_die_parent (die);
6150 /* First, compute a signature for just the type name (and its surrounding
6151 context, if any. This is stored in the type unit DIE for link-time
6152 ODR (one-definition rule) checking. */
6154 if (is_cxx() && name != NULL)
6156 md5_init_ctx (&ctx);
6158 /* Checksum the names of surrounding namespaces and structures. */
6159 if (parent != NULL)
6160 checksum_die_context (parent, &ctx);
6162 /* Checksum the current DIE. */
6163 die_odr_checksum (die->die_tag, name, &ctx);
6164 md5_finish_ctx (&ctx, checksum);
6166 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6169 /* Next, compute the complete type signature. */
6171 md5_init_ctx (&ctx);
6172 mark = 1;
6173 die->die_mark = mark;
6175 /* Checksum the names of surrounding namespaces and structures. */
6176 if (parent != NULL)
6177 checksum_die_context (parent, &ctx);
6179 /* Checksum the DIE and its children. */
6180 die_checksum_ordered (die, &ctx, &mark);
6181 unmark_all_dies (die);
6182 md5_finish_ctx (&ctx, checksum);
6184 /* Store the signature in the type node and link the type DIE and the
6185 type node together. */
6186 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6187 DWARF_TYPE_SIGNATURE_SIZE);
6188 die->comdat_type_p = true;
6189 die->die_id.die_type_node = type_node;
6190 type_node->type_die = die;
6192 /* If the DIE is a specification, link its declaration to the type node
6193 as well. */
6194 if (decl != NULL)
6196 decl->comdat_type_p = true;
6197 decl->die_id.die_type_node = type_node;
6201 /* Do the location expressions look same? */
6202 static inline int
6203 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6205 return loc1->dw_loc_opc == loc2->dw_loc_opc
6206 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6207 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6210 /* Do the values look the same? */
6211 static int
6212 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6214 dw_loc_descr_ref loc1, loc2;
6215 rtx r1, r2;
6217 if (v1->val_class != v2->val_class)
6218 return 0;
6220 switch (v1->val_class)
6222 case dw_val_class_const:
6223 return v1->v.val_int == v2->v.val_int;
6224 case dw_val_class_unsigned_const:
6225 return v1->v.val_unsigned == v2->v.val_unsigned;
6226 case dw_val_class_const_double:
6227 return v1->v.val_double.high == v2->v.val_double.high
6228 && v1->v.val_double.low == v2->v.val_double.low;
6229 case dw_val_class_vec:
6230 if (v1->v.val_vec.length != v2->v.val_vec.length
6231 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6232 return 0;
6233 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6234 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6235 return 0;
6236 return 1;
6237 case dw_val_class_flag:
6238 return v1->v.val_flag == v2->v.val_flag;
6239 case dw_val_class_str:
6240 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6242 case dw_val_class_addr:
6243 r1 = v1->v.val_addr;
6244 r2 = v2->v.val_addr;
6245 if (GET_CODE (r1) != GET_CODE (r2))
6246 return 0;
6247 return !rtx_equal_p (r1, r2);
6249 case dw_val_class_offset:
6250 return v1->v.val_offset == v2->v.val_offset;
6252 case dw_val_class_loc:
6253 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6254 loc1 && loc2;
6255 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6256 if (!same_loc_p (loc1, loc2, mark))
6257 return 0;
6258 return !loc1 && !loc2;
6260 case dw_val_class_die_ref:
6261 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6263 case dw_val_class_fde_ref:
6264 case dw_val_class_vms_delta:
6265 case dw_val_class_lbl_id:
6266 case dw_val_class_lineptr:
6267 case dw_val_class_macptr:
6268 case dw_val_class_high_pc:
6269 return 1;
6271 case dw_val_class_file:
6272 return v1->v.val_file == v2->v.val_file;
6274 case dw_val_class_data8:
6275 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6277 default:
6278 return 1;
6282 /* Do the attributes look the same? */
6284 static int
6285 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6287 if (at1->dw_attr != at2->dw_attr)
6288 return 0;
6290 /* We don't care that this was compiled with a different compiler
6291 snapshot; if the output is the same, that's what matters. */
6292 if (at1->dw_attr == DW_AT_producer)
6293 return 1;
6295 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6298 /* Do the dies look the same? */
6300 static int
6301 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6303 dw_die_ref c1, c2;
6304 dw_attr_ref a1;
6305 unsigned ix;
6307 /* To avoid infinite recursion. */
6308 if (die1->die_mark)
6309 return die1->die_mark == die2->die_mark;
6310 die1->die_mark = die2->die_mark = ++(*mark);
6312 if (die1->die_tag != die2->die_tag)
6313 return 0;
6315 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6316 return 0;
6318 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6319 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6320 return 0;
6322 c1 = die1->die_child;
6323 c2 = die2->die_child;
6324 if (! c1)
6326 if (c2)
6327 return 0;
6329 else
6330 for (;;)
6332 if (!same_die_p (c1, c2, mark))
6333 return 0;
6334 c1 = c1->die_sib;
6335 c2 = c2->die_sib;
6336 if (c1 == die1->die_child)
6338 if (c2 == die2->die_child)
6339 break;
6340 else
6341 return 0;
6345 return 1;
6348 /* Do the dies look the same? Wrapper around same_die_p. */
6350 static int
6351 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6353 int mark = 0;
6354 int ret = same_die_p (die1, die2, &mark);
6356 unmark_all_dies (die1);
6357 unmark_all_dies (die2);
6359 return ret;
6362 /* The prefix to attach to symbols on DIEs in the current comdat debug
6363 info section. */
6364 static const char *comdat_symbol_id;
6366 /* The index of the current symbol within the current comdat CU. */
6367 static unsigned int comdat_symbol_number;
6369 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6370 children, and set comdat_symbol_id accordingly. */
6372 static void
6373 compute_section_prefix (dw_die_ref unit_die)
6375 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6376 const char *base = die_name ? lbasename (die_name) : "anonymous";
6377 char *name = XALLOCAVEC (char, strlen (base) + 64);
6378 char *p;
6379 int i, mark;
6380 unsigned char checksum[16];
6381 struct md5_ctx ctx;
6383 /* Compute the checksum of the DIE, then append part of it as hex digits to
6384 the name filename of the unit. */
6386 md5_init_ctx (&ctx);
6387 mark = 0;
6388 die_checksum (unit_die, &ctx, &mark);
6389 unmark_all_dies (unit_die);
6390 md5_finish_ctx (&ctx, checksum);
6392 sprintf (name, "%s.", base);
6393 clean_symbol_name (name);
6395 p = name + strlen (name);
6396 for (i = 0; i < 4; i++)
6398 sprintf (p, "%.2x", checksum[i]);
6399 p += 2;
6402 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6403 comdat_symbol_number = 0;
6406 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6408 static int
6409 is_type_die (dw_die_ref die)
6411 switch (die->die_tag)
6413 case DW_TAG_array_type:
6414 case DW_TAG_class_type:
6415 case DW_TAG_interface_type:
6416 case DW_TAG_enumeration_type:
6417 case DW_TAG_pointer_type:
6418 case DW_TAG_reference_type:
6419 case DW_TAG_rvalue_reference_type:
6420 case DW_TAG_string_type:
6421 case DW_TAG_structure_type:
6422 case DW_TAG_subroutine_type:
6423 case DW_TAG_union_type:
6424 case DW_TAG_ptr_to_member_type:
6425 case DW_TAG_set_type:
6426 case DW_TAG_subrange_type:
6427 case DW_TAG_base_type:
6428 case DW_TAG_const_type:
6429 case DW_TAG_file_type:
6430 case DW_TAG_packed_type:
6431 case DW_TAG_volatile_type:
6432 case DW_TAG_typedef:
6433 return 1;
6434 default:
6435 return 0;
6439 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6440 Basically, we want to choose the bits that are likely to be shared between
6441 compilations (types) and leave out the bits that are specific to individual
6442 compilations (functions). */
6444 static int
6445 is_comdat_die (dw_die_ref c)
6447 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6448 we do for stabs. The advantage is a greater likelihood of sharing between
6449 objects that don't include headers in the same order (and therefore would
6450 put the base types in a different comdat). jason 8/28/00 */
6452 if (c->die_tag == DW_TAG_base_type)
6453 return 0;
6455 if (c->die_tag == DW_TAG_pointer_type
6456 || c->die_tag == DW_TAG_reference_type
6457 || c->die_tag == DW_TAG_rvalue_reference_type
6458 || c->die_tag == DW_TAG_const_type
6459 || c->die_tag == DW_TAG_volatile_type)
6461 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6463 return t ? is_comdat_die (t) : 0;
6466 return is_type_die (c);
6469 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6470 compilation unit. */
6472 static int
6473 is_symbol_die (dw_die_ref c)
6475 return (is_type_die (c)
6476 || is_declaration_die (c)
6477 || c->die_tag == DW_TAG_namespace
6478 || c->die_tag == DW_TAG_module);
6481 /* Returns true iff C is a compile-unit DIE. */
6483 static inline bool
6484 is_cu_die (dw_die_ref c)
6486 return c && c->die_tag == DW_TAG_compile_unit;
6489 /* Returns true iff C is a unit DIE of some sort. */
6491 static inline bool
6492 is_unit_die (dw_die_ref c)
6494 return c && (c->die_tag == DW_TAG_compile_unit
6495 || c->die_tag == DW_TAG_partial_unit
6496 || c->die_tag == DW_TAG_type_unit);
6499 /* Returns true iff C is a namespace DIE. */
6501 static inline bool
6502 is_namespace_die (dw_die_ref c)
6504 return c && c->die_tag == DW_TAG_namespace;
6507 /* Returns true iff C is a class or structure DIE. */
6509 static inline bool
6510 is_class_die (dw_die_ref c)
6512 return c && (c->die_tag == DW_TAG_class_type
6513 || c->die_tag == DW_TAG_structure_type);
6516 /* Return non-zero if this DIE is a template parameter. */
6518 static inline bool
6519 is_template_parameter (dw_die_ref die)
6521 switch (die->die_tag)
6523 case DW_TAG_template_type_param:
6524 case DW_TAG_template_value_param:
6525 case DW_TAG_GNU_template_template_param:
6526 case DW_TAG_GNU_template_parameter_pack:
6527 return true;
6528 default:
6529 return false;
6533 /* Return non-zero if this DIE represents a template instantiation. */
6535 static inline bool
6536 is_template_instantiation (dw_die_ref die)
6538 dw_die_ref c;
6540 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6541 return false;
6542 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6543 return false;
6546 static char *
6547 gen_internal_sym (const char *prefix)
6549 char buf[256];
6551 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6552 return xstrdup (buf);
6555 /* Assign symbols to all worthy DIEs under DIE. */
6557 static void
6558 assign_symbol_names (dw_die_ref die)
6560 dw_die_ref c;
6562 if (is_symbol_die (die) && !die->comdat_type_p)
6564 if (comdat_symbol_id)
6566 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6568 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6569 comdat_symbol_id, comdat_symbol_number++);
6570 die->die_id.die_symbol = xstrdup (p);
6572 else
6573 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6576 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6579 struct cu_hash_table_entry
6581 dw_die_ref cu;
6582 unsigned min_comdat_num, max_comdat_num;
6583 struct cu_hash_table_entry *next;
6586 /* Helpers to manipulate hash table of CUs. */
6588 struct cu_hash_table_entry_hasher
6590 typedef cu_hash_table_entry value_type;
6591 typedef die_struct compare_type;
6592 static inline hashval_t hash (const value_type *);
6593 static inline bool equal (const value_type *, const compare_type *);
6594 static inline void remove (value_type *);
6597 inline hashval_t
6598 cu_hash_table_entry_hasher::hash (const value_type *entry)
6600 return htab_hash_string (entry->cu->die_id.die_symbol);
6603 inline bool
6604 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6605 const compare_type *entry2)
6607 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6610 inline void
6611 cu_hash_table_entry_hasher::remove (value_type *entry)
6613 struct cu_hash_table_entry *next;
6615 while (entry)
6617 next = entry->next;
6618 free (entry);
6619 entry = next;
6623 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6625 /* Check whether we have already seen this CU and set up SYM_NUM
6626 accordingly. */
6627 static int
6628 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6630 struct cu_hash_table_entry dummy;
6631 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6633 dummy.max_comdat_num = 0;
6635 slot = htable.find_slot_with_hash (cu,
6636 htab_hash_string (cu->die_id.die_symbol),
6637 INSERT);
6638 entry = *slot;
6640 for (; entry; last = entry, entry = entry->next)
6642 if (same_die_p_wrap (cu, entry->cu))
6643 break;
6646 if (entry)
6648 *sym_num = entry->min_comdat_num;
6649 return 1;
6652 entry = XCNEW (struct cu_hash_table_entry);
6653 entry->cu = cu;
6654 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6655 entry->next = *slot;
6656 *slot = entry;
6658 return 0;
6661 /* Record SYM_NUM to record of CU in HTABLE. */
6662 static void
6663 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6664 unsigned int sym_num)
6666 struct cu_hash_table_entry **slot, *entry;
6668 slot = htable.find_slot_with_hash (cu,
6669 htab_hash_string (cu->die_id.die_symbol),
6670 NO_INSERT);
6671 entry = *slot;
6673 entry->max_comdat_num = sym_num;
6676 /* Traverse the DIE (which is always comp_unit_die), and set up
6677 additional compilation units for each of the include files we see
6678 bracketed by BINCL/EINCL. */
6680 static void
6681 break_out_includes (dw_die_ref die)
6683 dw_die_ref c;
6684 dw_die_ref unit = NULL;
6685 limbo_die_node *node, **pnode;
6686 cu_hash_type cu_hash_table;
6688 c = die->die_child;
6689 if (c) do {
6690 dw_die_ref prev = c;
6691 c = c->die_sib;
6692 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6693 || (unit && is_comdat_die (c)))
6695 dw_die_ref next = c->die_sib;
6697 /* This DIE is for a secondary CU; remove it from the main one. */
6698 remove_child_with_prev (c, prev);
6700 if (c->die_tag == DW_TAG_GNU_BINCL)
6701 unit = push_new_compile_unit (unit, c);
6702 else if (c->die_tag == DW_TAG_GNU_EINCL)
6703 unit = pop_compile_unit (unit);
6704 else
6705 add_child_die (unit, c);
6706 c = next;
6707 if (c == die->die_child)
6708 break;
6710 } while (c != die->die_child);
6712 #if 0
6713 /* We can only use this in debugging, since the frontend doesn't check
6714 to make sure that we leave every include file we enter. */
6715 gcc_assert (!unit);
6716 #endif
6718 assign_symbol_names (die);
6719 cu_hash_table.create (10);
6720 for (node = limbo_die_list, pnode = &limbo_die_list;
6721 node;
6722 node = node->next)
6724 int is_dupl;
6726 compute_section_prefix (node->die);
6727 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6728 &comdat_symbol_number);
6729 assign_symbol_names (node->die);
6730 if (is_dupl)
6731 *pnode = node->next;
6732 else
6734 pnode = &node->next;
6735 record_comdat_symbol_number (node->die, cu_hash_table,
6736 comdat_symbol_number);
6739 cu_hash_table.dispose ();
6742 /* Return non-zero if this DIE is a declaration. */
6744 static int
6745 is_declaration_die (dw_die_ref die)
6747 dw_attr_ref a;
6748 unsigned ix;
6750 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6751 if (a->dw_attr == DW_AT_declaration)
6752 return 1;
6754 return 0;
6757 /* Return non-zero if this DIE is nested inside a subprogram. */
6759 static int
6760 is_nested_in_subprogram (dw_die_ref die)
6762 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6764 if (decl == NULL)
6765 decl = die;
6766 return local_scope_p (decl);
6769 /* Return non-zero if this DIE contains a defining declaration of a
6770 subprogram. */
6772 static int
6773 contains_subprogram_definition (dw_die_ref die)
6775 dw_die_ref c;
6777 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6778 return 1;
6779 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6780 return 0;
6783 /* Return non-zero if this is a type DIE that should be moved to a
6784 COMDAT .debug_types section. */
6786 static int
6787 should_move_die_to_comdat (dw_die_ref die)
6789 switch (die->die_tag)
6791 case DW_TAG_class_type:
6792 case DW_TAG_structure_type:
6793 case DW_TAG_enumeration_type:
6794 case DW_TAG_union_type:
6795 /* Don't move declarations, inlined instances, or types nested in a
6796 subprogram. */
6797 if (is_declaration_die (die)
6798 || get_AT (die, DW_AT_abstract_origin)
6799 || is_nested_in_subprogram (die))
6800 return 0;
6801 /* A type definition should never contain a subprogram definition. */
6802 gcc_assert (!contains_subprogram_definition (die));
6803 return 1;
6804 case DW_TAG_array_type:
6805 case DW_TAG_interface_type:
6806 case DW_TAG_pointer_type:
6807 case DW_TAG_reference_type:
6808 case DW_TAG_rvalue_reference_type:
6809 case DW_TAG_string_type:
6810 case DW_TAG_subroutine_type:
6811 case DW_TAG_ptr_to_member_type:
6812 case DW_TAG_set_type:
6813 case DW_TAG_subrange_type:
6814 case DW_TAG_base_type:
6815 case DW_TAG_const_type:
6816 case DW_TAG_file_type:
6817 case DW_TAG_packed_type:
6818 case DW_TAG_volatile_type:
6819 case DW_TAG_typedef:
6820 default:
6821 return 0;
6825 /* Make a clone of DIE. */
6827 static dw_die_ref
6828 clone_die (dw_die_ref die)
6830 dw_die_ref clone;
6831 dw_attr_ref a;
6832 unsigned ix;
6834 clone = ggc_alloc_cleared_die_node ();
6835 clone->die_tag = die->die_tag;
6837 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6838 add_dwarf_attr (clone, a);
6840 return clone;
6843 /* Make a clone of the tree rooted at DIE. */
6845 static dw_die_ref
6846 clone_tree (dw_die_ref die)
6848 dw_die_ref c;
6849 dw_die_ref clone = clone_die (die);
6851 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6853 return clone;
6856 /* Make a clone of DIE as a declaration. */
6858 static dw_die_ref
6859 clone_as_declaration (dw_die_ref die)
6861 dw_die_ref clone;
6862 dw_die_ref decl;
6863 dw_attr_ref a;
6864 unsigned ix;
6866 /* If the DIE is already a declaration, just clone it. */
6867 if (is_declaration_die (die))
6868 return clone_die (die);
6870 /* If the DIE is a specification, just clone its declaration DIE. */
6871 decl = get_AT_ref (die, DW_AT_specification);
6872 if (decl != NULL)
6874 clone = clone_die (decl);
6875 if (die->comdat_type_p)
6876 add_AT_die_ref (clone, DW_AT_signature, die);
6877 return clone;
6880 clone = ggc_alloc_cleared_die_node ();
6881 clone->die_tag = die->die_tag;
6883 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6885 /* We don't want to copy over all attributes.
6886 For example we don't want DW_AT_byte_size because otherwise we will no
6887 longer have a declaration and GDB will treat it as a definition. */
6889 switch (a->dw_attr)
6891 case DW_AT_artificial:
6892 case DW_AT_containing_type:
6893 case DW_AT_external:
6894 case DW_AT_name:
6895 case DW_AT_type:
6896 case DW_AT_virtuality:
6897 case DW_AT_linkage_name:
6898 case DW_AT_MIPS_linkage_name:
6899 add_dwarf_attr (clone, a);
6900 break;
6901 case DW_AT_byte_size:
6902 default:
6903 break;
6907 if (die->comdat_type_p)
6908 add_AT_die_ref (clone, DW_AT_signature, die);
6910 add_AT_flag (clone, DW_AT_declaration, 1);
6911 return clone;
6915 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6917 struct decl_table_entry
6919 dw_die_ref orig;
6920 dw_die_ref copy;
6923 /* Helpers to manipulate hash table of copied declarations. */
6925 /* Hashtable helpers. */
6927 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6929 typedef decl_table_entry value_type;
6930 typedef die_struct compare_type;
6931 static inline hashval_t hash (const value_type *);
6932 static inline bool equal (const value_type *, const compare_type *);
6935 inline hashval_t
6936 decl_table_entry_hasher::hash (const value_type *entry)
6938 return htab_hash_pointer (entry->orig);
6941 inline bool
6942 decl_table_entry_hasher::equal (const value_type *entry1,
6943 const compare_type *entry2)
6945 return entry1->orig == entry2;
6948 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6950 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6951 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6952 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6953 to check if the ancestor has already been copied into UNIT. */
6955 static dw_die_ref
6956 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6958 dw_die_ref parent = die->die_parent;
6959 dw_die_ref new_parent = unit;
6960 dw_die_ref copy;
6961 decl_table_entry **slot = NULL;
6962 struct decl_table_entry *entry = NULL;
6964 if (decl_table.is_created ())
6966 /* Check if the entry has already been copied to UNIT. */
6967 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
6968 INSERT);
6969 if (*slot != HTAB_EMPTY_ENTRY)
6971 entry = *slot;
6972 return entry->copy;
6975 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
6976 entry = XCNEW (struct decl_table_entry);
6977 entry->orig = die;
6978 entry->copy = NULL;
6979 *slot = entry;
6982 if (parent != NULL)
6984 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
6985 if (spec != NULL)
6986 parent = spec;
6987 if (!is_unit_die (parent))
6988 new_parent = copy_ancestor_tree (unit, parent, decl_table);
6991 copy = clone_as_declaration (die);
6992 add_child_die (new_parent, copy);
6994 if (decl_table.is_created ())
6996 /* Record the pointer to the copy. */
6997 entry->copy = copy;
7000 return copy;
7002 /* Copy the declaration context to the new type unit DIE. This includes
7003 any surrounding namespace or type declarations. If the DIE has an
7004 AT_specification attribute, it also includes attributes and children
7005 attached to the specification, and returns a pointer to the original
7006 parent of the declaration DIE. Returns NULL otherwise. */
7008 static dw_die_ref
7009 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7011 dw_die_ref decl;
7012 dw_die_ref new_decl;
7013 dw_die_ref orig_parent = NULL;
7015 decl = get_AT_ref (die, DW_AT_specification);
7016 if (decl == NULL)
7017 decl = die;
7018 else
7020 unsigned ix;
7021 dw_die_ref c;
7022 dw_attr_ref a;
7024 /* The original DIE will be changed to a declaration, and must
7025 be moved to be a child of the original declaration DIE. */
7026 orig_parent = decl->die_parent;
7028 /* Copy the type node pointer from the new DIE to the original
7029 declaration DIE so we can forward references later. */
7030 decl->comdat_type_p = true;
7031 decl->die_id.die_type_node = die->die_id.die_type_node;
7033 remove_AT (die, DW_AT_specification);
7035 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7037 if (a->dw_attr != DW_AT_name
7038 && a->dw_attr != DW_AT_declaration
7039 && a->dw_attr != DW_AT_external)
7040 add_dwarf_attr (die, a);
7043 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7046 if (decl->die_parent != NULL
7047 && !is_unit_die (decl->die_parent))
7049 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7050 if (new_decl != NULL)
7052 remove_AT (new_decl, DW_AT_signature);
7053 add_AT_specification (die, new_decl);
7057 return orig_parent;
7060 /* Generate the skeleton ancestor tree for the given NODE, then clone
7061 the DIE and add the clone into the tree. */
7063 static void
7064 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7066 if (node->new_die != NULL)
7067 return;
7069 node->new_die = clone_as_declaration (node->old_die);
7071 if (node->parent != NULL)
7073 generate_skeleton_ancestor_tree (node->parent);
7074 add_child_die (node->parent->new_die, node->new_die);
7078 /* Generate a skeleton tree of DIEs containing any declarations that are
7079 found in the original tree. We traverse the tree looking for declaration
7080 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7082 static void
7083 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7085 skeleton_chain_node node;
7086 dw_die_ref c;
7087 dw_die_ref first;
7088 dw_die_ref prev = NULL;
7089 dw_die_ref next = NULL;
7091 node.parent = parent;
7093 first = c = parent->old_die->die_child;
7094 if (c)
7095 next = c->die_sib;
7096 if (c) do {
7097 if (prev == NULL || prev->die_sib == c)
7098 prev = c;
7099 c = next;
7100 next = (c == first ? NULL : c->die_sib);
7101 node.old_die = c;
7102 node.new_die = NULL;
7103 if (is_declaration_die (c))
7105 if (is_template_instantiation (c))
7107 /* Instantiated templates do not need to be cloned into the
7108 type unit. Just move the DIE and its children back to
7109 the skeleton tree (in the main CU). */
7110 remove_child_with_prev (c, prev);
7111 add_child_die (parent->new_die, c);
7112 c = prev;
7114 else
7116 /* Clone the existing DIE, move the original to the skeleton
7117 tree (which is in the main CU), and put the clone, with
7118 all the original's children, where the original came from
7119 (which is about to be moved to the type unit). */
7120 dw_die_ref clone = clone_die (c);
7121 move_all_children (c, clone);
7123 replace_child (c, clone, prev);
7124 generate_skeleton_ancestor_tree (parent);
7125 add_child_die (parent->new_die, c);
7126 node.new_die = c;
7127 c = clone;
7130 generate_skeleton_bottom_up (&node);
7131 } while (next != NULL);
7134 /* Wrapper function for generate_skeleton_bottom_up. */
7136 static dw_die_ref
7137 generate_skeleton (dw_die_ref die)
7139 skeleton_chain_node node;
7141 node.old_die = die;
7142 node.new_die = NULL;
7143 node.parent = NULL;
7145 /* If this type definition is nested inside another type,
7146 and is not an instantiation of a template, always leave
7147 at least a declaration in its place. */
7148 if (die->die_parent != NULL
7149 && is_type_die (die->die_parent)
7150 && !is_template_instantiation (die))
7151 node.new_die = clone_as_declaration (die);
7153 generate_skeleton_bottom_up (&node);
7154 return node.new_die;
7157 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7158 declaration. The original DIE is moved to a new compile unit so that
7159 existing references to it follow it to the new location. If any of the
7160 original DIE's descendants is a declaration, we need to replace the
7161 original DIE with a skeleton tree and move the declarations back into the
7162 skeleton tree. */
7164 static dw_die_ref
7165 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7166 dw_die_ref prev)
7168 dw_die_ref skeleton, orig_parent;
7170 /* Copy the declaration context to the type unit DIE. If the returned
7171 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7172 that DIE. */
7173 orig_parent = copy_declaration_context (unit, child);
7175 skeleton = generate_skeleton (child);
7176 if (skeleton == NULL)
7177 remove_child_with_prev (child, prev);
7178 else
7180 skeleton->comdat_type_p = true;
7181 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7183 /* If the original DIE was a specification, we need to put
7184 the skeleton under the parent DIE of the declaration.
7185 This leaves the original declaration in the tree, but
7186 it will be pruned later since there are no longer any
7187 references to it. */
7188 if (orig_parent != NULL)
7190 remove_child_with_prev (child, prev);
7191 add_child_die (orig_parent, skeleton);
7193 else
7194 replace_child (child, skeleton, prev);
7197 return skeleton;
7200 /* Traverse the DIE and set up additional .debug_types sections for each
7201 type worthy of being placed in a COMDAT section. */
7203 static void
7204 break_out_comdat_types (dw_die_ref die)
7206 dw_die_ref c;
7207 dw_die_ref first;
7208 dw_die_ref prev = NULL;
7209 dw_die_ref next = NULL;
7210 dw_die_ref unit = NULL;
7212 first = c = die->die_child;
7213 if (c)
7214 next = c->die_sib;
7215 if (c) do {
7216 if (prev == NULL || prev->die_sib == c)
7217 prev = c;
7218 c = next;
7219 next = (c == first ? NULL : c->die_sib);
7220 if (should_move_die_to_comdat (c))
7222 dw_die_ref replacement;
7223 comdat_type_node_ref type_node;
7225 /* Break out nested types into their own type units. */
7226 break_out_comdat_types (c);
7228 /* Create a new type unit DIE as the root for the new tree, and
7229 add it to the list of comdat types. */
7230 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7231 add_AT_unsigned (unit, DW_AT_language,
7232 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7233 type_node = ggc_alloc_cleared_comdat_type_node ();
7234 type_node->root_die = unit;
7235 type_node->next = comdat_type_list;
7236 comdat_type_list = type_node;
7238 /* Generate the type signature. */
7239 generate_type_signature (c, type_node);
7241 /* Copy the declaration context, attributes, and children of the
7242 declaration into the new type unit DIE, then remove this DIE
7243 from the main CU (or replace it with a skeleton if necessary). */
7244 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7245 type_node->skeleton_die = replacement;
7247 /* Add the DIE to the new compunit. */
7248 add_child_die (unit, c);
7250 if (replacement != NULL)
7251 c = replacement;
7253 else if (c->die_tag == DW_TAG_namespace
7254 || c->die_tag == DW_TAG_class_type
7255 || c->die_tag == DW_TAG_structure_type
7256 || c->die_tag == DW_TAG_union_type)
7258 /* Look for nested types that can be broken out. */
7259 break_out_comdat_types (c);
7261 } while (next != NULL);
7264 /* Like clone_tree, but additionally enter all the children into
7265 the hash table decl_table. */
7267 static dw_die_ref
7268 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7270 dw_die_ref c;
7271 dw_die_ref clone = clone_die (die);
7272 struct decl_table_entry *entry;
7273 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7274 htab_hash_pointer (die), INSERT);
7275 /* Assert that DIE isn't in the hash table yet. If it would be there
7276 before, the ancestors would be necessarily there as well, therefore
7277 clone_tree_hash wouldn't be called. */
7278 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7279 entry = XCNEW (struct decl_table_entry);
7280 entry->orig = die;
7281 entry->copy = clone;
7282 *slot = entry;
7284 FOR_EACH_CHILD (die, c,
7285 add_child_die (clone, clone_tree_hash (c, decl_table)));
7287 return clone;
7290 /* Walk the DIE and its children, looking for references to incomplete
7291 or trivial types that are unmarked (i.e., that are not in the current
7292 type_unit). */
7294 static void
7295 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7297 dw_die_ref c;
7298 dw_attr_ref a;
7299 unsigned ix;
7301 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7303 if (AT_class (a) == dw_val_class_die_ref)
7305 dw_die_ref targ = AT_ref (a);
7306 decl_table_entry **slot;
7307 struct decl_table_entry *entry;
7309 if (targ->die_mark != 0 || targ->comdat_type_p)
7310 continue;
7312 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7313 INSERT);
7315 if (*slot != HTAB_EMPTY_ENTRY)
7317 /* TARG has already been copied, so we just need to
7318 modify the reference to point to the copy. */
7319 entry = *slot;
7320 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7322 else
7324 dw_die_ref parent = unit;
7325 dw_die_ref copy = clone_die (targ);
7327 /* Record in DECL_TABLE that TARG has been copied.
7328 Need to do this now, before the recursive call,
7329 because DECL_TABLE may be expanded and SLOT
7330 would no longer be a valid pointer. */
7331 entry = XCNEW (struct decl_table_entry);
7332 entry->orig = targ;
7333 entry->copy = copy;
7334 *slot = entry;
7336 FOR_EACH_CHILD (targ, c,
7337 add_child_die (copy,
7338 clone_tree_hash (c, decl_table)));
7340 /* Make sure the cloned tree is marked as part of the
7341 type unit. */
7342 mark_dies (copy);
7344 /* If TARG has surrounding context, copy its ancestor tree
7345 into the new type unit. */
7346 if (targ->die_parent != NULL
7347 && !is_unit_die (targ->die_parent))
7348 parent = copy_ancestor_tree (unit, targ->die_parent,
7349 decl_table);
7351 add_child_die (parent, copy);
7352 a->dw_attr_val.v.val_die_ref.die = copy;
7354 /* Make sure the newly-copied DIE is walked. If it was
7355 installed in a previously-added context, it won't
7356 get visited otherwise. */
7357 if (parent != unit)
7359 /* Find the highest point of the newly-added tree,
7360 mark each node along the way, and walk from there. */
7361 parent->die_mark = 1;
7362 while (parent->die_parent
7363 && parent->die_parent->die_mark == 0)
7365 parent = parent->die_parent;
7366 parent->die_mark = 1;
7368 copy_decls_walk (unit, parent, decl_table);
7374 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7377 /* Copy declarations for "unworthy" types into the new comdat section.
7378 Incomplete types, modified types, and certain other types aren't broken
7379 out into comdat sections of their own, so they don't have a signature,
7380 and we need to copy the declaration into the same section so that we
7381 don't have an external reference. */
7383 static void
7384 copy_decls_for_unworthy_types (dw_die_ref unit)
7386 decl_hash_type decl_table;
7388 mark_dies (unit);
7389 decl_table.create (10);
7390 copy_decls_walk (unit, unit, decl_table);
7391 decl_table.dispose ();
7392 unmark_dies (unit);
7395 /* Traverse the DIE and add a sibling attribute if it may have the
7396 effect of speeding up access to siblings. To save some space,
7397 avoid generating sibling attributes for DIE's without children. */
7399 static void
7400 add_sibling_attributes (dw_die_ref die)
7402 dw_die_ref c;
7404 if (! die->die_child)
7405 return;
7407 if (die->die_parent && die != die->die_parent->die_child)
7408 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7410 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7413 /* Output all location lists for the DIE and its children. */
7415 static void
7416 output_location_lists (dw_die_ref die)
7418 dw_die_ref c;
7419 dw_attr_ref a;
7420 unsigned ix;
7422 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7423 if (AT_class (a) == dw_val_class_loc_list)
7424 output_loc_list (AT_loc_list (a));
7426 FOR_EACH_CHILD (die, c, output_location_lists (c));
7429 /* We want to limit the number of external references, because they are
7430 larger than local references: a relocation takes multiple words, and
7431 even a sig8 reference is always eight bytes, whereas a local reference
7432 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7433 So if we encounter multiple external references to the same type DIE, we
7434 make a local typedef stub for it and redirect all references there.
7436 This is the element of the hash table for keeping track of these
7437 references. */
7439 struct external_ref
7441 dw_die_ref type;
7442 dw_die_ref stub;
7443 unsigned n_refs;
7446 /* Hashtable helpers. */
7448 struct external_ref_hasher : typed_free_remove <external_ref>
7450 typedef external_ref value_type;
7451 typedef external_ref compare_type;
7452 static inline hashval_t hash (const value_type *);
7453 static inline bool equal (const value_type *, const compare_type *);
7456 inline hashval_t
7457 external_ref_hasher::hash (const value_type *r)
7459 dw_die_ref die = r->type;
7460 hashval_t h = 0;
7462 /* We can't use the address of the DIE for hashing, because
7463 that will make the order of the stub DIEs non-deterministic. */
7464 if (! die->comdat_type_p)
7465 /* We have a symbol; use it to compute a hash. */
7466 h = htab_hash_string (die->die_id.die_symbol);
7467 else
7469 /* We have a type signature; use a subset of the bits as the hash.
7470 The 8-byte signature is at least as large as hashval_t. */
7471 comdat_type_node_ref type_node = die->die_id.die_type_node;
7472 memcpy (&h, type_node->signature, sizeof (h));
7474 return h;
7477 inline bool
7478 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7480 return r1->type == r2->type;
7483 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7485 /* Return a pointer to the external_ref for references to DIE. */
7487 static struct external_ref *
7488 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7490 struct external_ref ref, *ref_p;
7491 external_ref **slot;
7493 ref.type = die;
7494 slot = map.find_slot (&ref, INSERT);
7495 if (*slot != HTAB_EMPTY_ENTRY)
7496 return *slot;
7498 ref_p = XCNEW (struct external_ref);
7499 ref_p->type = die;
7500 *slot = ref_p;
7501 return ref_p;
7504 /* Subroutine of optimize_external_refs, below.
7506 If we see a type skeleton, record it as our stub. If we see external
7507 references, remember how many we've seen. */
7509 static void
7510 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7512 dw_die_ref c;
7513 dw_attr_ref a;
7514 unsigned ix;
7515 struct external_ref *ref_p;
7517 if (is_type_die (die)
7518 && (c = get_AT_ref (die, DW_AT_signature)))
7520 /* This is a local skeleton; use it for local references. */
7521 ref_p = lookup_external_ref (map, c);
7522 ref_p->stub = die;
7525 /* Scan the DIE references, and remember any that refer to DIEs from
7526 other CUs (i.e. those which are not marked). */
7527 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7528 if (AT_class (a) == dw_val_class_die_ref
7529 && (c = AT_ref (a))->die_mark == 0
7530 && is_type_die (c))
7532 ref_p = lookup_external_ref (map, c);
7533 ref_p->n_refs++;
7536 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7539 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7540 points to an external_ref, DATA is the CU we're processing. If we don't
7541 already have a local stub, and we have multiple refs, build a stub. */
7544 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7546 struct external_ref *ref_p = *slot;
7548 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7550 /* We have multiple references to this type, so build a small stub.
7551 Both of these forms are a bit dodgy from the perspective of the
7552 DWARF standard, since technically they should have names. */
7553 dw_die_ref cu = data;
7554 dw_die_ref type = ref_p->type;
7555 dw_die_ref stub = NULL;
7557 if (type->comdat_type_p)
7559 /* If we refer to this type via sig8, use AT_signature. */
7560 stub = new_die (type->die_tag, cu, NULL_TREE);
7561 add_AT_die_ref (stub, DW_AT_signature, type);
7563 else
7565 /* Otherwise, use a typedef with no name. */
7566 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7567 add_AT_die_ref (stub, DW_AT_type, type);
7570 stub->die_mark++;
7571 ref_p->stub = stub;
7573 return 1;
7576 /* DIE is a unit; look through all the DIE references to see if there are
7577 any external references to types, and if so, create local stubs for
7578 them which will be applied in build_abbrev_table. This is useful because
7579 references to local DIEs are smaller. */
7581 static external_ref_hash_type
7582 optimize_external_refs (dw_die_ref die)
7584 external_ref_hash_type map;
7585 map.create (10);
7586 optimize_external_refs_1 (die, map);
7587 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7588 return map;
7591 /* The format of each DIE (and its attribute value pairs) is encoded in an
7592 abbreviation table. This routine builds the abbreviation table and assigns
7593 a unique abbreviation id for each abbreviation entry. The children of each
7594 die are visited recursively. */
7596 static void
7597 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7599 unsigned long abbrev_id;
7600 unsigned int n_alloc;
7601 dw_die_ref c;
7602 dw_attr_ref a;
7603 unsigned ix;
7605 /* Scan the DIE references, and replace any that refer to
7606 DIEs from other CUs (i.e. those which are not marked) with
7607 the local stubs we built in optimize_external_refs. */
7608 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7609 if (AT_class (a) == dw_val_class_die_ref
7610 && (c = AT_ref (a))->die_mark == 0)
7612 struct external_ref *ref_p;
7613 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7615 ref_p = lookup_external_ref (extern_map, c);
7616 if (ref_p->stub && ref_p->stub != die)
7617 change_AT_die_ref (a, ref_p->stub);
7618 else
7619 /* We aren't changing this reference, so mark it external. */
7620 set_AT_ref_external (a, 1);
7623 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7625 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7626 dw_attr_ref die_a, abbrev_a;
7627 unsigned ix;
7628 bool ok = true;
7630 if (abbrev->die_tag != die->die_tag)
7631 continue;
7632 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7633 continue;
7635 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7636 continue;
7638 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7640 abbrev_a = &(*abbrev->die_attr)[ix];
7641 if ((abbrev_a->dw_attr != die_a->dw_attr)
7642 || (value_format (abbrev_a) != value_format (die_a)))
7644 ok = false;
7645 break;
7648 if (ok)
7649 break;
7652 if (abbrev_id >= abbrev_die_table_in_use)
7654 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7656 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7657 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7658 n_alloc);
7660 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7661 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7662 abbrev_die_table_allocated = n_alloc;
7665 ++abbrev_die_table_in_use;
7666 abbrev_die_table[abbrev_id] = die;
7669 die->die_abbrev = abbrev_id;
7670 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7673 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7675 static int
7676 constant_size (unsigned HOST_WIDE_INT value)
7678 int log;
7680 if (value == 0)
7681 log = 0;
7682 else
7683 log = floor_log2 (value);
7685 log = log / 8;
7686 log = 1 << (floor_log2 (log) + 1);
7688 return log;
7691 /* Return the size of a DIE as it is represented in the
7692 .debug_info section. */
7694 static unsigned long
7695 size_of_die (dw_die_ref die)
7697 unsigned long size = 0;
7698 dw_attr_ref a;
7699 unsigned ix;
7700 enum dwarf_form form;
7702 size += size_of_uleb128 (die->die_abbrev);
7703 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7705 switch (AT_class (a))
7707 case dw_val_class_addr:
7708 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7710 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7711 size += size_of_uleb128 (AT_index (a));
7713 else
7714 size += DWARF2_ADDR_SIZE;
7715 break;
7716 case dw_val_class_offset:
7717 size += DWARF_OFFSET_SIZE;
7718 break;
7719 case dw_val_class_loc:
7721 unsigned long lsize = size_of_locs (AT_loc (a));
7723 /* Block length. */
7724 if (dwarf_version >= 4)
7725 size += size_of_uleb128 (lsize);
7726 else
7727 size += constant_size (lsize);
7728 size += lsize;
7730 break;
7731 case dw_val_class_loc_list:
7732 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7734 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7735 size += size_of_uleb128 (AT_index (a));
7737 else
7738 size += DWARF_OFFSET_SIZE;
7739 break;
7740 case dw_val_class_range_list:
7741 size += DWARF_OFFSET_SIZE;
7742 break;
7743 case dw_val_class_const:
7744 size += size_of_sleb128 (AT_int (a));
7745 break;
7746 case dw_val_class_unsigned_const:
7748 int csize = constant_size (AT_unsigned (a));
7749 if (dwarf_version == 3
7750 && a->dw_attr == DW_AT_data_member_location
7751 && csize >= 4)
7752 size += size_of_uleb128 (AT_unsigned (a));
7753 else
7754 size += csize;
7756 break;
7757 case dw_val_class_const_double:
7758 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7759 if (HOST_BITS_PER_WIDE_INT >= 64)
7760 size++; /* block */
7761 break;
7762 case dw_val_class_vec:
7763 size += constant_size (a->dw_attr_val.v.val_vec.length
7764 * a->dw_attr_val.v.val_vec.elt_size)
7765 + a->dw_attr_val.v.val_vec.length
7766 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7767 break;
7768 case dw_val_class_flag:
7769 if (dwarf_version >= 4)
7770 /* Currently all add_AT_flag calls pass in 1 as last argument,
7771 so DW_FORM_flag_present can be used. If that ever changes,
7772 we'll need to use DW_FORM_flag and have some optimization
7773 in build_abbrev_table that will change those to
7774 DW_FORM_flag_present if it is set to 1 in all DIEs using
7775 the same abbrev entry. */
7776 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7777 else
7778 size += 1;
7779 break;
7780 case dw_val_class_die_ref:
7781 if (AT_ref_external (a))
7783 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7784 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7785 is sized by target address length, whereas in DWARF3
7786 it's always sized as an offset. */
7787 if (use_debug_types)
7788 size += DWARF_TYPE_SIGNATURE_SIZE;
7789 else if (dwarf_version == 2)
7790 size += DWARF2_ADDR_SIZE;
7791 else
7792 size += DWARF_OFFSET_SIZE;
7794 else
7795 size += DWARF_OFFSET_SIZE;
7796 break;
7797 case dw_val_class_fde_ref:
7798 size += DWARF_OFFSET_SIZE;
7799 break;
7800 case dw_val_class_lbl_id:
7801 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7803 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7804 size += size_of_uleb128 (AT_index (a));
7806 else
7807 size += DWARF2_ADDR_SIZE;
7808 break;
7809 case dw_val_class_lineptr:
7810 case dw_val_class_macptr:
7811 size += DWARF_OFFSET_SIZE;
7812 break;
7813 case dw_val_class_str:
7814 form = AT_string_form (a);
7815 if (form == DW_FORM_strp)
7816 size += DWARF_OFFSET_SIZE;
7817 else if (form == DW_FORM_GNU_str_index)
7818 size += size_of_uleb128 (AT_index (a));
7819 else
7820 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7821 break;
7822 case dw_val_class_file:
7823 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7824 break;
7825 case dw_val_class_data8:
7826 size += 8;
7827 break;
7828 case dw_val_class_vms_delta:
7829 size += DWARF_OFFSET_SIZE;
7830 break;
7831 case dw_val_class_high_pc:
7832 size += DWARF2_ADDR_SIZE;
7833 break;
7834 default:
7835 gcc_unreachable ();
7839 return size;
7842 /* Size the debugging information associated with a given DIE. Visits the
7843 DIE's children recursively. Updates the global variable next_die_offset, on
7844 each time through. Uses the current value of next_die_offset to update the
7845 die_offset field in each DIE. */
7847 static void
7848 calc_die_sizes (dw_die_ref die)
7850 dw_die_ref c;
7852 gcc_assert (die->die_offset == 0
7853 || (unsigned long int) die->die_offset == next_die_offset);
7854 die->die_offset = next_die_offset;
7855 next_die_offset += size_of_die (die);
7857 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7859 if (die->die_child != NULL)
7860 /* Count the null byte used to terminate sibling lists. */
7861 next_die_offset += 1;
7864 /* Size just the base type children at the start of the CU.
7865 This is needed because build_abbrev needs to size locs
7866 and sizing of type based stack ops needs to know die_offset
7867 values for the base types. */
7869 static void
7870 calc_base_type_die_sizes (void)
7872 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7873 unsigned int i;
7874 dw_die_ref base_type;
7875 #if ENABLE_ASSERT_CHECKING
7876 dw_die_ref prev = comp_unit_die ()->die_child;
7877 #endif
7879 die_offset += size_of_die (comp_unit_die ());
7880 for (i = 0; base_types.iterate (i, &base_type); i++)
7882 #if ENABLE_ASSERT_CHECKING
7883 gcc_assert (base_type->die_offset == 0
7884 && prev->die_sib == base_type
7885 && base_type->die_child == NULL
7886 && base_type->die_abbrev);
7887 prev = base_type;
7888 #endif
7889 base_type->die_offset = die_offset;
7890 die_offset += size_of_die (base_type);
7894 /* Set the marks for a die and its children. We do this so
7895 that we know whether or not a reference needs to use FORM_ref_addr; only
7896 DIEs in the same CU will be marked. We used to clear out the offset
7897 and use that as the flag, but ran into ordering problems. */
7899 static void
7900 mark_dies (dw_die_ref die)
7902 dw_die_ref c;
7904 gcc_assert (!die->die_mark);
7906 die->die_mark = 1;
7907 FOR_EACH_CHILD (die, c, mark_dies (c));
7910 /* Clear the marks for a die and its children. */
7912 static void
7913 unmark_dies (dw_die_ref die)
7915 dw_die_ref c;
7917 if (! use_debug_types)
7918 gcc_assert (die->die_mark);
7920 die->die_mark = 0;
7921 FOR_EACH_CHILD (die, c, unmark_dies (c));
7924 /* Clear the marks for a die, its children and referred dies. */
7926 static void
7927 unmark_all_dies (dw_die_ref die)
7929 dw_die_ref c;
7930 dw_attr_ref a;
7931 unsigned ix;
7933 if (!die->die_mark)
7934 return;
7935 die->die_mark = 0;
7937 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7939 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7940 if (AT_class (a) == dw_val_class_die_ref)
7941 unmark_all_dies (AT_ref (a));
7944 /* Calculate if the entry should appear in the final output file. It may be
7945 from a pruned a type. */
7947 static bool
7948 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7950 if (table == pubname_table)
7952 /* Enumerator names are part of the pubname table, but the
7953 parent DW_TAG_enumeration_type die may have been pruned.
7954 Don't output them if that is the case. */
7955 if (p->die->die_tag == DW_TAG_enumerator &&
7956 (p->die->die_parent == NULL
7957 || !p->die->die_parent->die_perennial_p))
7958 return false;
7960 /* Everything else in the pubname table is included. */
7961 return true;
7964 /* The pubtypes table shouldn't include types that have been
7965 pruned. */
7966 return (p->die->die_offset != 0
7967 || !flag_eliminate_unused_debug_types);
7970 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7971 generated for the compilation unit. */
7973 static unsigned long
7974 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7976 unsigned long size;
7977 unsigned i;
7978 pubname_ref p;
7980 size = DWARF_PUBNAMES_HEADER_SIZE;
7981 FOR_EACH_VEC_ELT (*names, i, p)
7982 if (include_pubname_in_output (names, p))
7983 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7985 size += DWARF_OFFSET_SIZE;
7986 return size;
7989 /* Return the size of the information in the .debug_aranges section. */
7991 static unsigned long
7992 size_of_aranges (void)
7994 unsigned long size;
7996 size = DWARF_ARANGES_HEADER_SIZE;
7998 /* Count the address/length pair for this compilation unit. */
7999 if (text_section_used)
8000 size += 2 * DWARF2_ADDR_SIZE;
8001 if (cold_text_section_used)
8002 size += 2 * DWARF2_ADDR_SIZE;
8003 if (have_multiple_function_sections)
8005 unsigned fde_idx;
8006 dw_fde_ref fde;
8008 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8010 if (DECL_IGNORED_P (fde->decl))
8011 continue;
8012 if (!fde->in_std_section)
8013 size += 2 * DWARF2_ADDR_SIZE;
8014 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8015 size += 2 * DWARF2_ADDR_SIZE;
8019 /* Count the two zero words used to terminated the address range table. */
8020 size += 2 * DWARF2_ADDR_SIZE;
8021 return size;
8024 /* Select the encoding of an attribute value. */
8026 static enum dwarf_form
8027 value_format (dw_attr_ref a)
8029 switch (AT_class (a))
8031 case dw_val_class_addr:
8032 /* Only very few attributes allow DW_FORM_addr. */
8033 switch (a->dw_attr)
8035 case DW_AT_low_pc:
8036 case DW_AT_high_pc:
8037 case DW_AT_entry_pc:
8038 case DW_AT_trampoline:
8039 return (AT_index (a) == NOT_INDEXED
8040 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8041 default:
8042 break;
8044 switch (DWARF2_ADDR_SIZE)
8046 case 1:
8047 return DW_FORM_data1;
8048 case 2:
8049 return DW_FORM_data2;
8050 case 4:
8051 return DW_FORM_data4;
8052 case 8:
8053 return DW_FORM_data8;
8054 default:
8055 gcc_unreachable ();
8057 case dw_val_class_range_list:
8058 case dw_val_class_loc_list:
8059 if (dwarf_version >= 4)
8060 return DW_FORM_sec_offset;
8061 /* FALLTHRU */
8062 case dw_val_class_vms_delta:
8063 case dw_val_class_offset:
8064 switch (DWARF_OFFSET_SIZE)
8066 case 4:
8067 return DW_FORM_data4;
8068 case 8:
8069 return DW_FORM_data8;
8070 default:
8071 gcc_unreachable ();
8073 case dw_val_class_loc:
8074 if (dwarf_version >= 4)
8075 return DW_FORM_exprloc;
8076 switch (constant_size (size_of_locs (AT_loc (a))))
8078 case 1:
8079 return DW_FORM_block1;
8080 case 2:
8081 return DW_FORM_block2;
8082 case 4:
8083 return DW_FORM_block4;
8084 default:
8085 gcc_unreachable ();
8087 case dw_val_class_const:
8088 return DW_FORM_sdata;
8089 case dw_val_class_unsigned_const:
8090 switch (constant_size (AT_unsigned (a)))
8092 case 1:
8093 return DW_FORM_data1;
8094 case 2:
8095 return DW_FORM_data2;
8096 case 4:
8097 /* In DWARF3 DW_AT_data_member_location with
8098 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8099 constant, so we need to use DW_FORM_udata if we need
8100 a large constant. */
8101 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8102 return DW_FORM_udata;
8103 return DW_FORM_data4;
8104 case 8:
8105 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8106 return DW_FORM_udata;
8107 return DW_FORM_data8;
8108 default:
8109 gcc_unreachable ();
8111 case dw_val_class_const_double:
8112 switch (HOST_BITS_PER_WIDE_INT)
8114 case 8:
8115 return DW_FORM_data2;
8116 case 16:
8117 return DW_FORM_data4;
8118 case 32:
8119 return DW_FORM_data8;
8120 case 64:
8121 default:
8122 return DW_FORM_block1;
8124 case dw_val_class_vec:
8125 switch (constant_size (a->dw_attr_val.v.val_vec.length
8126 * a->dw_attr_val.v.val_vec.elt_size))
8128 case 1:
8129 return DW_FORM_block1;
8130 case 2:
8131 return DW_FORM_block2;
8132 case 4:
8133 return DW_FORM_block4;
8134 default:
8135 gcc_unreachable ();
8137 case dw_val_class_flag:
8138 if (dwarf_version >= 4)
8140 /* Currently all add_AT_flag calls pass in 1 as last argument,
8141 so DW_FORM_flag_present can be used. If that ever changes,
8142 we'll need to use DW_FORM_flag and have some optimization
8143 in build_abbrev_table that will change those to
8144 DW_FORM_flag_present if it is set to 1 in all DIEs using
8145 the same abbrev entry. */
8146 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8147 return DW_FORM_flag_present;
8149 return DW_FORM_flag;
8150 case dw_val_class_die_ref:
8151 if (AT_ref_external (a))
8152 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8153 else
8154 return DW_FORM_ref;
8155 case dw_val_class_fde_ref:
8156 return DW_FORM_data;
8157 case dw_val_class_lbl_id:
8158 return (AT_index (a) == NOT_INDEXED
8159 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8160 case dw_val_class_lineptr:
8161 case dw_val_class_macptr:
8162 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8163 case dw_val_class_str:
8164 return AT_string_form (a);
8165 case dw_val_class_file:
8166 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8168 case 1:
8169 return DW_FORM_data1;
8170 case 2:
8171 return DW_FORM_data2;
8172 case 4:
8173 return DW_FORM_data4;
8174 default:
8175 gcc_unreachable ();
8178 case dw_val_class_data8:
8179 return DW_FORM_data8;
8181 case dw_val_class_high_pc:
8182 switch (DWARF2_ADDR_SIZE)
8184 case 1:
8185 return DW_FORM_data1;
8186 case 2:
8187 return DW_FORM_data2;
8188 case 4:
8189 return DW_FORM_data4;
8190 case 8:
8191 return DW_FORM_data8;
8192 default:
8193 gcc_unreachable ();
8196 default:
8197 gcc_unreachable ();
8201 /* Output the encoding of an attribute value. */
8203 static void
8204 output_value_format (dw_attr_ref a)
8206 enum dwarf_form form = value_format (a);
8208 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8211 /* Given a die and id, produce the appropriate abbreviations. */
8213 static void
8214 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8216 unsigned ix;
8217 dw_attr_ref a_attr;
8219 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8220 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8221 dwarf_tag_name (abbrev->die_tag));
8223 if (abbrev->die_child != NULL)
8224 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8225 else
8226 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8228 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8230 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8231 dwarf_attr_name (a_attr->dw_attr));
8232 output_value_format (a_attr);
8235 dw2_asm_output_data (1, 0, NULL);
8236 dw2_asm_output_data (1, 0, NULL);
8240 /* Output the .debug_abbrev section which defines the DIE abbreviation
8241 table. */
8243 static void
8244 output_abbrev_section (void)
8246 unsigned long abbrev_id;
8248 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8249 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8251 /* Terminate the table. */
8252 dw2_asm_output_data (1, 0, NULL);
8255 /* Output a symbol we can use to refer to this DIE from another CU. */
8257 static inline void
8258 output_die_symbol (dw_die_ref die)
8260 const char *sym = die->die_id.die_symbol;
8262 gcc_assert (!die->comdat_type_p);
8264 if (sym == 0)
8265 return;
8267 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8268 /* We make these global, not weak; if the target doesn't support
8269 .linkonce, it doesn't support combining the sections, so debugging
8270 will break. */
8271 targetm.asm_out.globalize_label (asm_out_file, sym);
8273 ASM_OUTPUT_LABEL (asm_out_file, sym);
8276 /* Return a new location list, given the begin and end range, and the
8277 expression. */
8279 static inline dw_loc_list_ref
8280 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8281 const char *section)
8283 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8285 retlist->begin = begin;
8286 retlist->begin_entry = NULL;
8287 retlist->end = end;
8288 retlist->expr = expr;
8289 retlist->section = section;
8291 return retlist;
8294 /* Generate a new internal symbol for this location list node, if it
8295 hasn't got one yet. */
8297 static inline void
8298 gen_llsym (dw_loc_list_ref list)
8300 gcc_assert (!list->ll_symbol);
8301 list->ll_symbol = gen_internal_sym ("LLST");
8304 /* Output the location list given to us. */
8306 static void
8307 output_loc_list (dw_loc_list_ref list_head)
8309 dw_loc_list_ref curr = list_head;
8311 if (list_head->emitted)
8312 return;
8313 list_head->emitted = true;
8315 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8317 /* Walk the location list, and output each range + expression. */
8318 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8320 unsigned long size;
8321 /* Don't output an entry that starts and ends at the same address. */
8322 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8323 continue;
8324 size = size_of_locs (curr->expr);
8325 /* If the expression is too large, drop it on the floor. We could
8326 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8327 in the expression, but >= 64KB expressions for a single value
8328 in a single range are unlikely very useful. */
8329 if (size > 0xffff)
8330 continue;
8331 if (dwarf_split_debug_info)
8333 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8334 "Location list start/length entry (%s)",
8335 list_head->ll_symbol);
8336 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8337 "Location list range start index (%s)",
8338 curr->begin);
8339 /* The length field is 4 bytes. If we ever need to support
8340 an 8-byte length, we can add a new DW_LLE code or fall back
8341 to DW_LLE_GNU_start_end_entry. */
8342 dw2_asm_output_delta (4, curr->end, curr->begin,
8343 "Location list range length (%s)",
8344 list_head->ll_symbol);
8346 else if (!have_multiple_function_sections)
8348 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8349 "Location list begin address (%s)",
8350 list_head->ll_symbol);
8351 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8352 "Location list end address (%s)",
8353 list_head->ll_symbol);
8355 else
8357 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8358 "Location list begin address (%s)",
8359 list_head->ll_symbol);
8360 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8361 "Location list end address (%s)",
8362 list_head->ll_symbol);
8365 /* Output the block length for this list of location operations. */
8366 gcc_assert (size <= 0xffff);
8367 dw2_asm_output_data (2, size, "%s", "Location expression size");
8369 output_loc_sequence (curr->expr, -1);
8372 if (dwarf_split_debug_info)
8373 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8374 "Location list terminator (%s)",
8375 list_head->ll_symbol);
8376 else
8378 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8379 "Location list terminator begin (%s)",
8380 list_head->ll_symbol);
8381 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8382 "Location list terminator end (%s)",
8383 list_head->ll_symbol);
8387 /* Output a range_list offset into the debug_range section. Emit a
8388 relocated reference if val_entry is NULL, otherwise, emit an
8389 indirect reference. */
8391 static void
8392 output_range_list_offset (dw_attr_ref a)
8394 const char *name = dwarf_attr_name (a->dw_attr);
8396 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8398 char *p = strchr (ranges_section_label, '\0');
8399 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8400 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8401 debug_ranges_section, "%s", name);
8402 *p = '\0';
8404 else
8405 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8406 "%s (offset from %s)", name, ranges_section_label);
8409 /* Output the offset into the debug_loc section. */
8411 static void
8412 output_loc_list_offset (dw_attr_ref a)
8414 char *sym = AT_loc_list (a)->ll_symbol;
8416 gcc_assert (sym);
8417 if (dwarf_split_debug_info)
8418 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8419 "%s", dwarf_attr_name (a->dw_attr));
8420 else
8421 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8422 "%s", dwarf_attr_name (a->dw_attr));
8425 /* Output an attribute's index or value appropriately. */
8427 static void
8428 output_attr_index_or_value (dw_attr_ref a)
8430 const char *name = dwarf_attr_name (a->dw_attr);
8432 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8434 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8435 return;
8437 switch (AT_class (a))
8439 case dw_val_class_addr:
8440 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8441 break;
8442 case dw_val_class_high_pc:
8443 case dw_val_class_lbl_id:
8444 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8445 break;
8446 case dw_val_class_loc_list:
8447 output_loc_list_offset (a);
8448 break;
8449 default:
8450 gcc_unreachable ();
8454 /* Output a type signature. */
8456 static inline void
8457 output_signature (const char *sig, const char *name)
8459 int i;
8461 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8462 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8465 /* Output the DIE and its attributes. Called recursively to generate
8466 the definitions of each child DIE. */
8468 static void
8469 output_die (dw_die_ref die)
8471 dw_attr_ref a;
8472 dw_die_ref c;
8473 unsigned long size;
8474 unsigned ix;
8476 /* If someone in another CU might refer to us, set up a symbol for
8477 them to point to. */
8478 if (! die->comdat_type_p && die->die_id.die_symbol)
8479 output_die_symbol (die);
8481 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8482 (unsigned long)die->die_offset,
8483 dwarf_tag_name (die->die_tag));
8485 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8487 const char *name = dwarf_attr_name (a->dw_attr);
8489 switch (AT_class (a))
8491 case dw_val_class_addr:
8492 output_attr_index_or_value (a);
8493 break;
8495 case dw_val_class_offset:
8496 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8497 "%s", name);
8498 break;
8500 case dw_val_class_range_list:
8501 output_range_list_offset (a);
8502 break;
8504 case dw_val_class_loc:
8505 size = size_of_locs (AT_loc (a));
8507 /* Output the block length for this list of location operations. */
8508 if (dwarf_version >= 4)
8509 dw2_asm_output_data_uleb128 (size, "%s", name);
8510 else
8511 dw2_asm_output_data (constant_size (size), size, "%s", name);
8513 output_loc_sequence (AT_loc (a), -1);
8514 break;
8516 case dw_val_class_const:
8517 /* ??? It would be slightly more efficient to use a scheme like is
8518 used for unsigned constants below, but gdb 4.x does not sign
8519 extend. Gdb 5.x does sign extend. */
8520 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8521 break;
8523 case dw_val_class_unsigned_const:
8525 int csize = constant_size (AT_unsigned (a));
8526 if (dwarf_version == 3
8527 && a->dw_attr == DW_AT_data_member_location
8528 && csize >= 4)
8529 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8530 else
8531 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8533 break;
8535 case dw_val_class_const_double:
8537 unsigned HOST_WIDE_INT first, second;
8539 if (HOST_BITS_PER_WIDE_INT >= 64)
8540 dw2_asm_output_data (1,
8541 HOST_BITS_PER_DOUBLE_INT
8542 / HOST_BITS_PER_CHAR,
8543 NULL);
8545 if (WORDS_BIG_ENDIAN)
8547 first = a->dw_attr_val.v.val_double.high;
8548 second = a->dw_attr_val.v.val_double.low;
8550 else
8552 first = a->dw_attr_val.v.val_double.low;
8553 second = a->dw_attr_val.v.val_double.high;
8556 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8557 first, "%s", name);
8558 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8559 second, NULL);
8561 break;
8563 case dw_val_class_vec:
8565 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8566 unsigned int len = a->dw_attr_val.v.val_vec.length;
8567 unsigned int i;
8568 unsigned char *p;
8570 dw2_asm_output_data (constant_size (len * elt_size),
8571 len * elt_size, "%s", name);
8572 if (elt_size > sizeof (HOST_WIDE_INT))
8574 elt_size /= 2;
8575 len *= 2;
8577 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8578 i < len;
8579 i++, p += elt_size)
8580 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8581 "fp or vector constant word %u", i);
8582 break;
8585 case dw_val_class_flag:
8586 if (dwarf_version >= 4)
8588 /* Currently all add_AT_flag calls pass in 1 as last argument,
8589 so DW_FORM_flag_present can be used. If that ever changes,
8590 we'll need to use DW_FORM_flag and have some optimization
8591 in build_abbrev_table that will change those to
8592 DW_FORM_flag_present if it is set to 1 in all DIEs using
8593 the same abbrev entry. */
8594 gcc_assert (AT_flag (a) == 1);
8595 if (flag_debug_asm)
8596 fprintf (asm_out_file, "\t\t\t%s %s\n",
8597 ASM_COMMENT_START, name);
8598 break;
8600 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8601 break;
8603 case dw_val_class_loc_list:
8604 output_attr_index_or_value (a);
8605 break;
8607 case dw_val_class_die_ref:
8608 if (AT_ref_external (a))
8610 if (AT_ref (a)->comdat_type_p)
8612 comdat_type_node_ref type_node =
8613 AT_ref (a)->die_id.die_type_node;
8615 gcc_assert (type_node);
8616 output_signature (type_node->signature, name);
8618 else
8620 const char *sym = AT_ref (a)->die_id.die_symbol;
8621 int size;
8623 gcc_assert (sym);
8624 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8625 length, whereas in DWARF3 it's always sized as an
8626 offset. */
8627 if (dwarf_version == 2)
8628 size = DWARF2_ADDR_SIZE;
8629 else
8630 size = DWARF_OFFSET_SIZE;
8631 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8632 name);
8635 else
8637 gcc_assert (AT_ref (a)->die_offset);
8638 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8639 "%s", name);
8641 break;
8643 case dw_val_class_fde_ref:
8645 char l1[20];
8647 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8648 a->dw_attr_val.v.val_fde_index * 2);
8649 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8650 "%s", name);
8652 break;
8654 case dw_val_class_vms_delta:
8655 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8656 AT_vms_delta2 (a), AT_vms_delta1 (a),
8657 "%s", name);
8658 break;
8660 case dw_val_class_lbl_id:
8661 output_attr_index_or_value (a);
8662 break;
8664 case dw_val_class_lineptr:
8665 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8666 debug_line_section, "%s", name);
8667 break;
8669 case dw_val_class_macptr:
8670 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8671 debug_macinfo_section, "%s", name);
8672 break;
8674 case dw_val_class_str:
8675 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8676 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8677 a->dw_attr_val.v.val_str->label,
8678 debug_str_section,
8679 "%s: \"%s\"", name, AT_string (a));
8680 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8681 dw2_asm_output_data_uleb128 (AT_index (a),
8682 "%s: \"%s\"", name, AT_string (a));
8683 else
8684 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8685 break;
8687 case dw_val_class_file:
8689 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8691 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8692 a->dw_attr_val.v.val_file->filename);
8693 break;
8696 case dw_val_class_data8:
8698 int i;
8700 for (i = 0; i < 8; i++)
8701 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8702 i == 0 ? "%s" : NULL, name);
8703 break;
8706 case dw_val_class_high_pc:
8707 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8708 get_AT_low_pc (die), "DW_AT_high_pc");
8709 break;
8711 default:
8712 gcc_unreachable ();
8716 FOR_EACH_CHILD (die, c, output_die (c));
8718 /* Add null byte to terminate sibling list. */
8719 if (die->die_child != NULL)
8720 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8721 (unsigned long) die->die_offset);
8724 /* Output the compilation unit that appears at the beginning of the
8725 .debug_info section, and precedes the DIE descriptions. */
8727 static void
8728 output_compilation_unit_header (void)
8730 int ver = dwarf_version;
8732 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8733 dw2_asm_output_data (4, 0xffffffff,
8734 "Initial length escape value indicating 64-bit DWARF extension");
8735 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8736 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8737 "Length of Compilation Unit Info");
8738 dw2_asm_output_data (2, ver, "DWARF version number");
8739 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8740 debug_abbrev_section,
8741 "Offset Into Abbrev. Section");
8742 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8745 /* Output the compilation unit DIE and its children. */
8747 static void
8748 output_comp_unit (dw_die_ref die, int output_if_empty)
8750 const char *secname, *oldsym;
8751 char *tmp;
8752 external_ref_hash_type extern_map;
8754 /* Unless we are outputting main CU, we may throw away empty ones. */
8755 if (!output_if_empty && die->die_child == NULL)
8756 return;
8758 /* Even if there are no children of this DIE, we must output the information
8759 about the compilation unit. Otherwise, on an empty translation unit, we
8760 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8761 will then complain when examining the file. First mark all the DIEs in
8762 this CU so we know which get local refs. */
8763 mark_dies (die);
8765 extern_map = optimize_external_refs (die);
8767 build_abbrev_table (die, extern_map);
8769 extern_map.dispose ();
8771 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8772 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8773 calc_die_sizes (die);
8775 oldsym = die->die_id.die_symbol;
8776 if (oldsym)
8778 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8780 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8781 secname = tmp;
8782 die->die_id.die_symbol = NULL;
8783 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8785 else
8787 switch_to_section (debug_info_section);
8788 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8789 info_section_emitted = true;
8792 /* Output debugging information. */
8793 output_compilation_unit_header ();
8794 output_die (die);
8796 /* Leave the marks on the main CU, so we can check them in
8797 output_pubnames. */
8798 if (oldsym)
8800 unmark_dies (die);
8801 die->die_id.die_symbol = oldsym;
8805 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8806 and .debug_pubtypes. This is configured per-target, but can be
8807 overridden by the -gpubnames or -gno-pubnames options. */
8809 static inline bool
8810 want_pubnames (void)
8812 if (debug_generate_pub_sections != -1)
8813 return debug_generate_pub_sections;
8814 return targetm.want_debug_pub_sections;
8817 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8819 static void
8820 add_AT_pubnames (dw_die_ref die)
8822 if (want_pubnames ())
8823 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8826 /* Add a string attribute value to a skeleton DIE. */
8828 static inline void
8829 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8830 const char *str)
8832 dw_attr_node attr;
8833 struct indirect_string_node *node;
8835 if (! skeleton_debug_str_hash)
8836 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8837 debug_str_eq, NULL);
8839 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8840 find_string_form (node);
8841 if (node->form == DW_FORM_GNU_str_index)
8842 node->form = DW_FORM_strp;
8844 attr.dw_attr = attr_kind;
8845 attr.dw_attr_val.val_class = dw_val_class_str;
8846 attr.dw_attr_val.val_entry = NULL;
8847 attr.dw_attr_val.v.val_str = node;
8848 add_dwarf_attr (die, &attr);
8851 /* Helper function to generate top-level dies for skeleton debug_info and
8852 debug_types. */
8854 static void
8855 add_top_level_skeleton_die_attrs (dw_die_ref die)
8857 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8858 const char *comp_dir = comp_dir_string ();
8860 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8861 if (comp_dir != NULL)
8862 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8863 add_AT_pubnames (die);
8864 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8867 /* Return the single type-unit die for skeleton type units. */
8869 static dw_die_ref
8870 get_skeleton_type_unit (void)
8872 /* For dwarf_split_debug_sections with use_type info, all type units in the
8873 skeleton sections have identical dies (but different headers). This
8874 single die will be output many times. */
8876 static dw_die_ref skeleton_type_unit = NULL;
8878 if (skeleton_type_unit == NULL)
8880 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8881 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8882 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8884 return skeleton_type_unit;
8887 /* Output skeleton debug sections that point to the dwo file. */
8889 static void
8890 output_skeleton_debug_sections (dw_die_ref comp_unit)
8892 /* These attributes will be found in the full debug_info section. */
8893 remove_AT (comp_unit, DW_AT_producer);
8894 remove_AT (comp_unit, DW_AT_language);
8896 switch_to_section (debug_skeleton_info_section);
8897 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8899 /* Produce the skeleton compilation-unit header. This one differs enough from
8900 a normal CU header that it's better not to call output_compilation_unit
8901 header. */
8902 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8903 dw2_asm_output_data (4, 0xffffffff,
8904 "Initial length escape value indicating 64-bit DWARF extension");
8906 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8907 DWARF_COMPILE_UNIT_HEADER_SIZE
8908 - DWARF_INITIAL_LENGTH_SIZE
8909 + size_of_die (comp_unit),
8910 "Length of Compilation Unit Info");
8911 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8912 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8913 debug_abbrev_section,
8914 "Offset Into Abbrev. Section");
8915 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8917 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8918 output_die (comp_unit);
8920 /* Build the skeleton debug_abbrev section. */
8921 switch_to_section (debug_skeleton_abbrev_section);
8922 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8924 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8925 if (use_debug_types)
8926 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8928 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8931 /* Output a comdat type unit DIE and its children. */
8933 static void
8934 output_comdat_type_unit (comdat_type_node *node)
8936 const char *secname;
8937 char *tmp;
8938 int i;
8939 #if defined (OBJECT_FORMAT_ELF)
8940 tree comdat_key;
8941 #endif
8942 external_ref_hash_type extern_map;
8944 /* First mark all the DIEs in this CU so we know which get local refs. */
8945 mark_dies (node->root_die);
8947 extern_map = optimize_external_refs (node->root_die);
8949 build_abbrev_table (node->root_die, extern_map);
8951 extern_map.dispose ();
8953 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8954 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8955 calc_die_sizes (node->root_die);
8957 #if defined (OBJECT_FORMAT_ELF)
8958 if (!dwarf_split_debug_info)
8959 secname = ".debug_types";
8960 else
8961 secname = ".debug_types.dwo";
8963 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8964 sprintf (tmp, "wt.");
8965 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8966 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8967 comdat_key = get_identifier (tmp);
8968 targetm.asm_out.named_section (secname,
8969 SECTION_DEBUG | SECTION_LINKONCE,
8970 comdat_key);
8971 #else
8972 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8973 sprintf (tmp, ".gnu.linkonce.wt.");
8974 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8975 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8976 secname = tmp;
8977 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8978 #endif
8980 /* Output debugging information. */
8981 output_compilation_unit_header ();
8982 output_signature (node->signature, "Type Signature");
8983 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8984 "Offset to Type DIE");
8985 output_die (node->root_die);
8987 unmark_dies (node->root_die);
8989 #if defined (OBJECT_FORMAT_ELF)
8990 if (dwarf_split_debug_info)
8992 /* Produce the skeleton type-unit header. */
8993 const char *secname = ".debug_types";
8995 targetm.asm_out.named_section (secname,
8996 SECTION_DEBUG | SECTION_LINKONCE,
8997 comdat_key);
8998 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8999 dw2_asm_output_data (4, 0xffffffff,
9000 "Initial length escape value indicating 64-bit DWARF extension");
9002 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9003 DWARF_COMPILE_UNIT_HEADER_SIZE
9004 - DWARF_INITIAL_LENGTH_SIZE
9005 + size_of_die (get_skeleton_type_unit ())
9006 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9007 "Length of Type Unit Info");
9008 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9009 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9010 debug_skeleton_abbrev_section_label,
9011 debug_abbrev_section,
9012 "Offset Into Abbrev. Section");
9013 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9014 output_signature (node->signature, "Type Signature");
9015 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9017 output_die (get_skeleton_type_unit ());
9019 #endif
9022 /* Return the DWARF2/3 pubname associated with a decl. */
9024 static const char *
9025 dwarf2_name (tree decl, int scope)
9027 if (DECL_NAMELESS (decl))
9028 return NULL;
9029 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9032 /* Add a new entry to .debug_pubnames if appropriate. */
9034 static void
9035 add_pubname_string (const char *str, dw_die_ref die)
9037 pubname_entry e;
9039 e.die = die;
9040 e.name = xstrdup (str);
9041 vec_safe_push (pubname_table, e);
9044 static void
9045 add_pubname (tree decl, dw_die_ref die)
9047 if (!want_pubnames ())
9048 return;
9050 /* Don't add items to the table when we expect that the consumer will have
9051 just read the enclosing die. For example, if the consumer is looking at a
9052 class_member, it will either be inside the class already, or will have just
9053 looked up the class to find the member. Either way, searching the class is
9054 faster than searching the index. */
9055 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9056 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9058 const char *name = dwarf2_name (decl, 1);
9060 if (name)
9061 add_pubname_string (name, die);
9065 /* Add an enumerator to the pubnames section. */
9067 static void
9068 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9070 pubname_entry e;
9072 gcc_assert (scope_name);
9073 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9074 e.die = die;
9075 vec_safe_push (pubname_table, e);
9078 /* Add a new entry to .debug_pubtypes if appropriate. */
9080 static void
9081 add_pubtype (tree decl, dw_die_ref die)
9083 pubname_entry e;
9085 if (!want_pubnames ())
9086 return;
9088 if ((TREE_PUBLIC (decl)
9089 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9090 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9092 tree scope = NULL;
9093 const char *scope_name = "";
9094 const char *sep = is_cxx () ? "::" : ".";
9095 const char *name;
9097 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9098 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9100 scope_name = lang_hooks.dwarf_name (scope, 1);
9101 if (scope_name != NULL && scope_name[0] != '\0')
9102 scope_name = concat (scope_name, sep, NULL);
9103 else
9104 scope_name = "";
9107 if (TYPE_P (decl))
9108 name = type_tag (decl);
9109 else
9110 name = lang_hooks.dwarf_name (decl, 1);
9112 /* If we don't have a name for the type, there's no point in adding
9113 it to the table. */
9114 if (name != NULL && name[0] != '\0')
9116 e.die = die;
9117 e.name = concat (scope_name, name, NULL);
9118 vec_safe_push (pubtype_table, e);
9121 /* Although it might be more consistent to add the pubinfo for the
9122 enumerators as their dies are created, they should only be added if the
9123 enum type meets the criteria above. So rather than re-check the parent
9124 enum type whenever an enumerator die is created, just output them all
9125 here. This isn't protected by the name conditional because anonymous
9126 enums don't have names. */
9127 if (die->die_tag == DW_TAG_enumeration_type)
9129 dw_die_ref c;
9131 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9136 /* Output the public names table used to speed up access to externally
9137 visible names; or the public types table used to find type definitions. */
9139 static void
9140 output_pubnames (vec<pubname_entry, va_gc> *names)
9142 unsigned i;
9143 unsigned long pubnames_length = size_of_pubnames (names);
9144 pubname_ref pub;
9146 if (!want_pubnames () || !info_section_emitted)
9147 return;
9148 if (names == pubname_table)
9149 switch_to_section (debug_pubnames_section);
9150 else
9151 switch_to_section (debug_pubtypes_section);
9152 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9153 dw2_asm_output_data (4, 0xffffffff,
9154 "Initial length escape value indicating 64-bit DWARF extension");
9155 if (names == pubname_table)
9156 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9157 "Length of Public Names Info");
9158 else
9159 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9160 "Length of Public Type Names Info");
9161 /* Version number for pubnames/pubtypes is still 2, even in DWARF3. */
9162 dw2_asm_output_data (2, 2, "DWARF Version");
9163 if (dwarf_split_debug_info)
9164 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9165 debug_skeleton_info_section,
9166 "Offset of Compilation Unit Info");
9167 else
9168 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9169 debug_info_section,
9170 "Offset of Compilation Unit Info");
9171 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9172 "Compilation Unit Length");
9174 FOR_EACH_VEC_ELT (*names, i, pub)
9176 if (include_pubname_in_output (names, pub))
9178 dw_offset die_offset = pub->die->die_offset;
9180 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9181 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9182 gcc_assert (pub->die->die_mark);
9184 /* If we're putting types in their own .debug_types sections,
9185 the .debug_pubtypes table will still point to the compile
9186 unit (not the type unit), so we want to use the offset of
9187 the skeleton DIE (if there is one). */
9188 if (pub->die->comdat_type_p && names == pubtype_table)
9190 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9192 if (type_node != NULL)
9193 die_offset = (type_node->skeleton_die != NULL
9194 ? type_node->skeleton_die->die_offset
9195 : 0);
9198 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9200 dw2_asm_output_nstring (pub->name, -1, "external name");
9204 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9207 /* Output the information that goes into the .debug_aranges table.
9208 Namely, define the beginning and ending address range of the
9209 text section generated for this compilation unit. */
9211 static void
9212 output_aranges (unsigned long aranges_length)
9214 unsigned i;
9216 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9217 dw2_asm_output_data (4, 0xffffffff,
9218 "Initial length escape value indicating 64-bit DWARF extension");
9219 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9220 "Length of Address Ranges Info");
9221 /* Version number for aranges is still 2, even in DWARF3. */
9222 dw2_asm_output_data (2, 2, "DWARF Version");
9223 if (dwarf_split_debug_info)
9224 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9225 debug_skeleton_info_section,
9226 "Offset of Compilation Unit Info");
9227 else
9228 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9229 debug_info_section,
9230 "Offset of Compilation Unit Info");
9231 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9232 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9234 /* We need to align to twice the pointer size here. */
9235 if (DWARF_ARANGES_PAD_SIZE)
9237 /* Pad using a 2 byte words so that padding is correct for any
9238 pointer size. */
9239 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9240 2 * DWARF2_ADDR_SIZE);
9241 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9242 dw2_asm_output_data (2, 0, NULL);
9245 /* It is necessary not to output these entries if the sections were
9246 not used; if the sections were not used, the length will be 0 and
9247 the address may end up as 0 if the section is discarded by ld
9248 --gc-sections, leaving an invalid (0, 0) entry that can be
9249 confused with the terminator. */
9250 if (text_section_used)
9252 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9253 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9254 text_section_label, "Length");
9256 if (cold_text_section_used)
9258 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9259 "Address");
9260 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9261 cold_text_section_label, "Length");
9264 if (have_multiple_function_sections)
9266 unsigned fde_idx;
9267 dw_fde_ref fde;
9269 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9271 if (DECL_IGNORED_P (fde->decl))
9272 continue;
9273 if (!fde->in_std_section)
9275 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9276 "Address");
9277 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9278 fde->dw_fde_begin, "Length");
9280 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9282 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9283 "Address");
9284 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9285 fde->dw_fde_second_begin, "Length");
9290 /* Output the terminator words. */
9291 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9292 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9295 /* Add a new entry to .debug_ranges. Return the offset at which it
9296 was placed. */
9298 static unsigned int
9299 add_ranges_num (int num)
9301 unsigned int in_use = ranges_table_in_use;
9303 if (in_use == ranges_table_allocated)
9305 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9306 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9307 ranges_table_allocated);
9308 memset (ranges_table + ranges_table_in_use, 0,
9309 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9312 ranges_table[in_use].num = num;
9313 ranges_table_in_use = in_use + 1;
9315 return in_use * 2 * DWARF2_ADDR_SIZE;
9318 /* Add a new entry to .debug_ranges corresponding to a block, or a
9319 range terminator if BLOCK is NULL. */
9321 static unsigned int
9322 add_ranges (const_tree block)
9324 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9327 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9328 When using dwarf_split_debug_info, address attributes in dies destined
9329 for the final executable should be direct references--setting the
9330 parameter force_direct ensures this behavior. */
9332 static void
9333 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9334 bool *added, bool force_direct)
9336 unsigned int in_use = ranges_by_label_in_use;
9337 unsigned int offset;
9339 if (in_use == ranges_by_label_allocated)
9341 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9342 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9343 ranges_by_label,
9344 ranges_by_label_allocated);
9345 memset (ranges_by_label + ranges_by_label_in_use, 0,
9346 RANGES_TABLE_INCREMENT
9347 * sizeof (struct dw_ranges_by_label_struct));
9350 ranges_by_label[in_use].begin = begin;
9351 ranges_by_label[in_use].end = end;
9352 ranges_by_label_in_use = in_use + 1;
9354 offset = add_ranges_num (-(int)in_use - 1);
9355 if (!*added)
9357 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9358 *added = true;
9362 static void
9363 output_ranges (void)
9365 unsigned i;
9366 static const char *const start_fmt = "Offset %#x";
9367 const char *fmt = start_fmt;
9369 for (i = 0; i < ranges_table_in_use; i++)
9371 int block_num = ranges_table[i].num;
9373 if (block_num > 0)
9375 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9376 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9378 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9379 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9381 /* If all code is in the text section, then the compilation
9382 unit base address defaults to DW_AT_low_pc, which is the
9383 base of the text section. */
9384 if (!have_multiple_function_sections)
9386 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9387 text_section_label,
9388 fmt, i * 2 * DWARF2_ADDR_SIZE);
9389 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9390 text_section_label, NULL);
9393 /* Otherwise, the compilation unit base address is zero,
9394 which allows us to use absolute addresses, and not worry
9395 about whether the target supports cross-section
9396 arithmetic. */
9397 else
9399 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9400 fmt, i * 2 * DWARF2_ADDR_SIZE);
9401 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9404 fmt = NULL;
9407 /* Negative block_num stands for an index into ranges_by_label. */
9408 else if (block_num < 0)
9410 int lab_idx = - block_num - 1;
9412 if (!have_multiple_function_sections)
9414 gcc_unreachable ();
9415 #if 0
9416 /* If we ever use add_ranges_by_labels () for a single
9417 function section, all we have to do is to take out
9418 the #if 0 above. */
9419 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9420 ranges_by_label[lab_idx].begin,
9421 text_section_label,
9422 fmt, i * 2 * DWARF2_ADDR_SIZE);
9423 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9424 ranges_by_label[lab_idx].end,
9425 text_section_label, NULL);
9426 #endif
9428 else
9430 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9431 ranges_by_label[lab_idx].begin,
9432 fmt, i * 2 * DWARF2_ADDR_SIZE);
9433 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9434 ranges_by_label[lab_idx].end,
9435 NULL);
9438 else
9440 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9441 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9442 fmt = start_fmt;
9447 /* Data structure containing information about input files. */
9448 struct file_info
9450 const char *path; /* Complete file name. */
9451 const char *fname; /* File name part. */
9452 int length; /* Length of entire string. */
9453 struct dwarf_file_data * file_idx; /* Index in input file table. */
9454 int dir_idx; /* Index in directory table. */
9457 /* Data structure containing information about directories with source
9458 files. */
9459 struct dir_info
9461 const char *path; /* Path including directory name. */
9462 int length; /* Path length. */
9463 int prefix; /* Index of directory entry which is a prefix. */
9464 int count; /* Number of files in this directory. */
9465 int dir_idx; /* Index of directory used as base. */
9468 /* Callback function for file_info comparison. We sort by looking at
9469 the directories in the path. */
9471 static int
9472 file_info_cmp (const void *p1, const void *p2)
9474 const struct file_info *const s1 = (const struct file_info *) p1;
9475 const struct file_info *const s2 = (const struct file_info *) p2;
9476 const unsigned char *cp1;
9477 const unsigned char *cp2;
9479 /* Take care of file names without directories. We need to make sure that
9480 we return consistent values to qsort since some will get confused if
9481 we return the same value when identical operands are passed in opposite
9482 orders. So if neither has a directory, return 0 and otherwise return
9483 1 or -1 depending on which one has the directory. */
9484 if ((s1->path == s1->fname || s2->path == s2->fname))
9485 return (s2->path == s2->fname) - (s1->path == s1->fname);
9487 cp1 = (const unsigned char *) s1->path;
9488 cp2 = (const unsigned char *) s2->path;
9490 while (1)
9492 ++cp1;
9493 ++cp2;
9494 /* Reached the end of the first path? If so, handle like above. */
9495 if ((cp1 == (const unsigned char *) s1->fname)
9496 || (cp2 == (const unsigned char *) s2->fname))
9497 return ((cp2 == (const unsigned char *) s2->fname)
9498 - (cp1 == (const unsigned char *) s1->fname));
9500 /* Character of current path component the same? */
9501 else if (*cp1 != *cp2)
9502 return *cp1 - *cp2;
9506 struct file_name_acquire_data
9508 struct file_info *files;
9509 int used_files;
9510 int max_files;
9513 /* Traversal function for the hash table. */
9515 static int
9516 file_name_acquire (void ** slot, void *data)
9518 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9519 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9520 struct file_info *fi;
9521 const char *f;
9523 gcc_assert (fnad->max_files >= d->emitted_number);
9525 if (! d->emitted_number)
9526 return 1;
9528 gcc_assert (fnad->max_files != fnad->used_files);
9530 fi = fnad->files + fnad->used_files++;
9532 /* Skip all leading "./". */
9533 f = d->filename;
9534 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9535 f += 2;
9537 /* Create a new array entry. */
9538 fi->path = f;
9539 fi->length = strlen (f);
9540 fi->file_idx = d;
9542 /* Search for the file name part. */
9543 f = strrchr (f, DIR_SEPARATOR);
9544 #if defined (DIR_SEPARATOR_2)
9546 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9548 if (g != NULL)
9550 if (f == NULL || f < g)
9551 f = g;
9554 #endif
9556 fi->fname = f == NULL ? fi->path : f + 1;
9557 return 1;
9560 /* Output the directory table and the file name table. We try to minimize
9561 the total amount of memory needed. A heuristic is used to avoid large
9562 slowdowns with many input files. */
9564 static void
9565 output_file_names (void)
9567 struct file_name_acquire_data fnad;
9568 int numfiles;
9569 struct file_info *files;
9570 struct dir_info *dirs;
9571 int *saved;
9572 int *savehere;
9573 int *backmap;
9574 int ndirs;
9575 int idx_offset;
9576 int i;
9578 if (!last_emitted_file)
9580 dw2_asm_output_data (1, 0, "End directory table");
9581 dw2_asm_output_data (1, 0, "End file name table");
9582 return;
9585 numfiles = last_emitted_file->emitted_number;
9587 /* Allocate the various arrays we need. */
9588 files = XALLOCAVEC (struct file_info, numfiles);
9589 dirs = XALLOCAVEC (struct dir_info, numfiles);
9591 fnad.files = files;
9592 fnad.used_files = 0;
9593 fnad.max_files = numfiles;
9594 htab_traverse (file_table, file_name_acquire, &fnad);
9595 gcc_assert (fnad.used_files == fnad.max_files);
9597 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9599 /* Find all the different directories used. */
9600 dirs[0].path = files[0].path;
9601 dirs[0].length = files[0].fname - files[0].path;
9602 dirs[0].prefix = -1;
9603 dirs[0].count = 1;
9604 dirs[0].dir_idx = 0;
9605 files[0].dir_idx = 0;
9606 ndirs = 1;
9608 for (i = 1; i < numfiles; i++)
9609 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9610 && memcmp (dirs[ndirs - 1].path, files[i].path,
9611 dirs[ndirs - 1].length) == 0)
9613 /* Same directory as last entry. */
9614 files[i].dir_idx = ndirs - 1;
9615 ++dirs[ndirs - 1].count;
9617 else
9619 int j;
9621 /* This is a new directory. */
9622 dirs[ndirs].path = files[i].path;
9623 dirs[ndirs].length = files[i].fname - files[i].path;
9624 dirs[ndirs].count = 1;
9625 dirs[ndirs].dir_idx = ndirs;
9626 files[i].dir_idx = ndirs;
9628 /* Search for a prefix. */
9629 dirs[ndirs].prefix = -1;
9630 for (j = 0; j < ndirs; j++)
9631 if (dirs[j].length < dirs[ndirs].length
9632 && dirs[j].length > 1
9633 && (dirs[ndirs].prefix == -1
9634 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9635 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9636 dirs[ndirs].prefix = j;
9638 ++ndirs;
9641 /* Now to the actual work. We have to find a subset of the directories which
9642 allow expressing the file name using references to the directory table
9643 with the least amount of characters. We do not do an exhaustive search
9644 where we would have to check out every combination of every single
9645 possible prefix. Instead we use a heuristic which provides nearly optimal
9646 results in most cases and never is much off. */
9647 saved = XALLOCAVEC (int, ndirs);
9648 savehere = XALLOCAVEC (int, ndirs);
9650 memset (saved, '\0', ndirs * sizeof (saved[0]));
9651 for (i = 0; i < ndirs; i++)
9653 int j;
9654 int total;
9656 /* We can always save some space for the current directory. But this
9657 does not mean it will be enough to justify adding the directory. */
9658 savehere[i] = dirs[i].length;
9659 total = (savehere[i] - saved[i]) * dirs[i].count;
9661 for (j = i + 1; j < ndirs; j++)
9663 savehere[j] = 0;
9664 if (saved[j] < dirs[i].length)
9666 /* Determine whether the dirs[i] path is a prefix of the
9667 dirs[j] path. */
9668 int k;
9670 k = dirs[j].prefix;
9671 while (k != -1 && k != (int) i)
9672 k = dirs[k].prefix;
9674 if (k == (int) i)
9676 /* Yes it is. We can possibly save some memory by
9677 writing the filenames in dirs[j] relative to
9678 dirs[i]. */
9679 savehere[j] = dirs[i].length;
9680 total += (savehere[j] - saved[j]) * dirs[j].count;
9685 /* Check whether we can save enough to justify adding the dirs[i]
9686 directory. */
9687 if (total > dirs[i].length + 1)
9689 /* It's worthwhile adding. */
9690 for (j = i; j < ndirs; j++)
9691 if (savehere[j] > 0)
9693 /* Remember how much we saved for this directory so far. */
9694 saved[j] = savehere[j];
9696 /* Remember the prefix directory. */
9697 dirs[j].dir_idx = i;
9702 /* Emit the directory name table. */
9703 idx_offset = dirs[0].length > 0 ? 1 : 0;
9704 for (i = 1 - idx_offset; i < ndirs; i++)
9705 dw2_asm_output_nstring (dirs[i].path,
9706 dirs[i].length
9707 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9708 "Directory Entry: %#x", i + idx_offset);
9710 dw2_asm_output_data (1, 0, "End directory table");
9712 /* We have to emit them in the order of emitted_number since that's
9713 used in the debug info generation. To do this efficiently we
9714 generate a back-mapping of the indices first. */
9715 backmap = XALLOCAVEC (int, numfiles);
9716 for (i = 0; i < numfiles; i++)
9717 backmap[files[i].file_idx->emitted_number - 1] = i;
9719 /* Now write all the file names. */
9720 for (i = 0; i < numfiles; i++)
9722 int file_idx = backmap[i];
9723 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9725 #ifdef VMS_DEBUGGING_INFO
9726 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9728 /* Setting these fields can lead to debugger miscomparisons,
9729 but VMS Debug requires them to be set correctly. */
9731 int ver;
9732 long long cdt;
9733 long siz;
9734 int maxfilelen = strlen (files[file_idx].path)
9735 + dirs[dir_idx].length
9736 + MAX_VMS_VERSION_LEN + 1;
9737 char *filebuf = XALLOCAVEC (char, maxfilelen);
9739 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9740 snprintf (filebuf, maxfilelen, "%s;%d",
9741 files[file_idx].path + dirs[dir_idx].length, ver);
9743 dw2_asm_output_nstring
9744 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9746 /* Include directory index. */
9747 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9749 /* Modification time. */
9750 dw2_asm_output_data_uleb128
9751 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9752 ? cdt : 0,
9753 NULL);
9755 /* File length in bytes. */
9756 dw2_asm_output_data_uleb128
9757 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9758 ? siz : 0,
9759 NULL);
9760 #else
9761 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9762 "File Entry: %#x", (unsigned) i + 1);
9764 /* Include directory index. */
9765 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9767 /* Modification time. */
9768 dw2_asm_output_data_uleb128 (0, NULL);
9770 /* File length in bytes. */
9771 dw2_asm_output_data_uleb128 (0, NULL);
9772 #endif /* VMS_DEBUGGING_INFO */
9775 dw2_asm_output_data (1, 0, "End file name table");
9779 /* Output one line number table into the .debug_line section. */
9781 static void
9782 output_one_line_info_table (dw_line_info_table *table)
9784 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9785 unsigned int current_line = 1;
9786 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9787 dw_line_info_entry *ent;
9788 size_t i;
9790 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9792 switch (ent->opcode)
9794 case LI_set_address:
9795 /* ??? Unfortunately, we have little choice here currently, and
9796 must always use the most general form. GCC does not know the
9797 address delta itself, so we can't use DW_LNS_advance_pc. Many
9798 ports do have length attributes which will give an upper bound
9799 on the address range. We could perhaps use length attributes
9800 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9801 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9803 /* This can handle any delta. This takes
9804 4+DWARF2_ADDR_SIZE bytes. */
9805 dw2_asm_output_data (1, 0, "set address %s", line_label);
9806 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9807 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9808 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9809 break;
9811 case LI_set_line:
9812 if (ent->val == current_line)
9814 /* We still need to start a new row, so output a copy insn. */
9815 dw2_asm_output_data (1, DW_LNS_copy,
9816 "copy line %u", current_line);
9818 else
9820 int line_offset = ent->val - current_line;
9821 int line_delta = line_offset - DWARF_LINE_BASE;
9823 current_line = ent->val;
9824 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9826 /* This can handle deltas from -10 to 234, using the current
9827 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9828 This takes 1 byte. */
9829 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9830 "line %u", current_line);
9832 else
9834 /* This can handle any delta. This takes at least 4 bytes,
9835 depending on the value being encoded. */
9836 dw2_asm_output_data (1, DW_LNS_advance_line,
9837 "advance to line %u", current_line);
9838 dw2_asm_output_data_sleb128 (line_offset, NULL);
9839 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9842 break;
9844 case LI_set_file:
9845 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9846 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9847 break;
9849 case LI_set_column:
9850 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9851 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9852 break;
9854 case LI_negate_stmt:
9855 current_is_stmt = !current_is_stmt;
9856 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9857 "is_stmt %d", current_is_stmt);
9858 break;
9860 case LI_set_prologue_end:
9861 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9862 "set prologue end");
9863 break;
9865 case LI_set_epilogue_begin:
9866 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9867 "set epilogue begin");
9868 break;
9870 case LI_set_discriminator:
9871 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9872 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9873 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9874 dw2_asm_output_data_uleb128 (ent->val, NULL);
9875 break;
9879 /* Emit debug info for the address of the end of the table. */
9880 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9881 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9882 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9883 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9885 dw2_asm_output_data (1, 0, "end sequence");
9886 dw2_asm_output_data_uleb128 (1, NULL);
9887 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9890 /* Output the source line number correspondence information. This
9891 information goes into the .debug_line section. */
9893 static void
9894 output_line_info (bool prologue_only)
9896 char l1[20], l2[20], p1[20], p2[20];
9897 int ver = dwarf_version;
9898 bool saw_one = false;
9899 int opc;
9901 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9902 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9903 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9904 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9906 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9907 dw2_asm_output_data (4, 0xffffffff,
9908 "Initial length escape value indicating 64-bit DWARF extension");
9909 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9910 "Length of Source Line Info");
9911 ASM_OUTPUT_LABEL (asm_out_file, l1);
9913 dw2_asm_output_data (2, ver, "DWARF Version");
9914 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9915 ASM_OUTPUT_LABEL (asm_out_file, p1);
9917 /* Define the architecture-dependent minimum instruction length (in bytes).
9918 In this implementation of DWARF, this field is used for information
9919 purposes only. Since GCC generates assembly language, we have no
9920 a priori knowledge of how many instruction bytes are generated for each
9921 source line, and therefore can use only the DW_LNE_set_address and
9922 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
9923 this as '1', which is "correct enough" for all architectures,
9924 and don't let the target override. */
9925 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9927 if (ver >= 4)
9928 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9929 "Maximum Operations Per Instruction");
9930 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9931 "Default is_stmt_start flag");
9932 dw2_asm_output_data (1, DWARF_LINE_BASE,
9933 "Line Base Value (Special Opcodes)");
9934 dw2_asm_output_data (1, DWARF_LINE_RANGE,
9935 "Line Range Value (Special Opcodes)");
9936 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9937 "Special Opcode Base");
9939 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9941 int n_op_args;
9942 switch (opc)
9944 case DW_LNS_advance_pc:
9945 case DW_LNS_advance_line:
9946 case DW_LNS_set_file:
9947 case DW_LNS_set_column:
9948 case DW_LNS_fixed_advance_pc:
9949 case DW_LNS_set_isa:
9950 n_op_args = 1;
9951 break;
9952 default:
9953 n_op_args = 0;
9954 break;
9957 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9958 opc, n_op_args);
9961 /* Write out the information about the files we use. */
9962 output_file_names ();
9963 ASM_OUTPUT_LABEL (asm_out_file, p2);
9964 if (prologue_only)
9966 /* Output the marker for the end of the line number info. */
9967 ASM_OUTPUT_LABEL (asm_out_file, l2);
9968 return;
9971 if (separate_line_info)
9973 dw_line_info_table *table;
9974 size_t i;
9976 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9977 if (table->in_use)
9979 output_one_line_info_table (table);
9980 saw_one = true;
9983 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9985 output_one_line_info_table (cold_text_section_line_info);
9986 saw_one = true;
9989 /* ??? Some Darwin linkers crash on a .debug_line section with no
9990 sequences. Further, merely a DW_LNE_end_sequence entry is not
9991 sufficient -- the address column must also be initialized.
9992 Make sure to output at least one set_address/end_sequence pair,
9993 choosing .text since that section is always present. */
9994 if (text_section_line_info->in_use || !saw_one)
9995 output_one_line_info_table (text_section_line_info);
9997 /* Output the marker for the end of the line number info. */
9998 ASM_OUTPUT_LABEL (asm_out_file, l2);
10001 /* Given a pointer to a tree node for some base type, return a pointer to
10002 a DIE that describes the given type.
10004 This routine must only be called for GCC type nodes that correspond to
10005 Dwarf base (fundamental) types. */
10007 static dw_die_ref
10008 base_type_die (tree type)
10010 dw_die_ref base_type_result;
10011 enum dwarf_type encoding;
10013 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10014 return 0;
10016 /* If this is a subtype that should not be emitted as a subrange type,
10017 use the base type. See subrange_type_for_debug_p. */
10018 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10019 type = TREE_TYPE (type);
10021 switch (TREE_CODE (type))
10023 case INTEGER_TYPE:
10024 if ((dwarf_version >= 4 || !dwarf_strict)
10025 && TYPE_NAME (type)
10026 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10027 && DECL_IS_BUILTIN (TYPE_NAME (type))
10028 && DECL_NAME (TYPE_NAME (type)))
10030 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10031 if (strcmp (name, "char16_t") == 0
10032 || strcmp (name, "char32_t") == 0)
10034 encoding = DW_ATE_UTF;
10035 break;
10038 if (TYPE_STRING_FLAG (type))
10040 if (TYPE_UNSIGNED (type))
10041 encoding = DW_ATE_unsigned_char;
10042 else
10043 encoding = DW_ATE_signed_char;
10045 else if (TYPE_UNSIGNED (type))
10046 encoding = DW_ATE_unsigned;
10047 else
10048 encoding = DW_ATE_signed;
10049 break;
10051 case REAL_TYPE:
10052 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10054 if (dwarf_version >= 3 || !dwarf_strict)
10055 encoding = DW_ATE_decimal_float;
10056 else
10057 encoding = DW_ATE_lo_user;
10059 else
10060 encoding = DW_ATE_float;
10061 break;
10063 case FIXED_POINT_TYPE:
10064 if (!(dwarf_version >= 3 || !dwarf_strict))
10065 encoding = DW_ATE_lo_user;
10066 else if (TYPE_UNSIGNED (type))
10067 encoding = DW_ATE_unsigned_fixed;
10068 else
10069 encoding = DW_ATE_signed_fixed;
10070 break;
10072 /* Dwarf2 doesn't know anything about complex ints, so use
10073 a user defined type for it. */
10074 case COMPLEX_TYPE:
10075 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10076 encoding = DW_ATE_complex_float;
10077 else
10078 encoding = DW_ATE_lo_user;
10079 break;
10081 case BOOLEAN_TYPE:
10082 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10083 encoding = DW_ATE_boolean;
10084 break;
10086 default:
10087 /* No other TREE_CODEs are Dwarf fundamental types. */
10088 gcc_unreachable ();
10091 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10093 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10094 int_size_in_bytes (type));
10095 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10096 add_pubtype (type, base_type_result);
10098 return base_type_result;
10101 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10102 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10104 static inline int
10105 is_base_type (tree type)
10107 switch (TREE_CODE (type))
10109 case ERROR_MARK:
10110 case VOID_TYPE:
10111 case INTEGER_TYPE:
10112 case REAL_TYPE:
10113 case FIXED_POINT_TYPE:
10114 case COMPLEX_TYPE:
10115 case BOOLEAN_TYPE:
10116 return 1;
10118 case ARRAY_TYPE:
10119 case RECORD_TYPE:
10120 case UNION_TYPE:
10121 case QUAL_UNION_TYPE:
10122 case ENUMERAL_TYPE:
10123 case FUNCTION_TYPE:
10124 case METHOD_TYPE:
10125 case POINTER_TYPE:
10126 case REFERENCE_TYPE:
10127 case NULLPTR_TYPE:
10128 case OFFSET_TYPE:
10129 case LANG_TYPE:
10130 case VECTOR_TYPE:
10131 return 0;
10133 default:
10134 gcc_unreachable ();
10137 return 0;
10140 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10141 node, return the size in bits for the type if it is a constant, or else
10142 return the alignment for the type if the type's size is not constant, or
10143 else return BITS_PER_WORD if the type actually turns out to be an
10144 ERROR_MARK node. */
10146 static inline unsigned HOST_WIDE_INT
10147 simple_type_size_in_bits (const_tree type)
10149 if (TREE_CODE (type) == ERROR_MARK)
10150 return BITS_PER_WORD;
10151 else if (TYPE_SIZE (type) == NULL_TREE)
10152 return 0;
10153 else if (host_integerp (TYPE_SIZE (type), 1))
10154 return tree_low_cst (TYPE_SIZE (type), 1);
10155 else
10156 return TYPE_ALIGN (type);
10159 /* Similarly, but return a double_int instead of UHWI. */
10161 static inline double_int
10162 double_int_type_size_in_bits (const_tree type)
10164 if (TREE_CODE (type) == ERROR_MARK)
10165 return double_int::from_uhwi (BITS_PER_WORD);
10166 else if (TYPE_SIZE (type) == NULL_TREE)
10167 return double_int_zero;
10168 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10169 return tree_to_double_int (TYPE_SIZE (type));
10170 else
10171 return double_int::from_uhwi (TYPE_ALIGN (type));
10174 /* Given a pointer to a tree node for a subrange type, return a pointer
10175 to a DIE that describes the given type. */
10177 static dw_die_ref
10178 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10180 dw_die_ref subrange_die;
10181 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10183 if (context_die == NULL)
10184 context_die = comp_unit_die ();
10186 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10188 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10190 /* The size of the subrange type and its base type do not match,
10191 so we need to generate a size attribute for the subrange type. */
10192 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10195 if (low)
10196 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10197 if (high)
10198 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10200 return subrange_die;
10203 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10204 entry that chains various modifiers in front of the given type. */
10206 static dw_die_ref
10207 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10208 dw_die_ref context_die)
10210 enum tree_code code = TREE_CODE (type);
10211 dw_die_ref mod_type_die;
10212 dw_die_ref sub_die = NULL;
10213 tree item_type = NULL;
10214 tree qualified_type;
10215 tree name, low, high;
10216 dw_die_ref mod_scope;
10218 if (code == ERROR_MARK)
10219 return NULL;
10221 /* See if we already have the appropriately qualified variant of
10222 this type. */
10223 qualified_type
10224 = get_qualified_type (type,
10225 ((is_const_type ? TYPE_QUAL_CONST : 0)
10226 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10228 if (qualified_type == sizetype
10229 && TYPE_NAME (qualified_type)
10230 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10232 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10234 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10235 && TYPE_PRECISION (t)
10236 == TYPE_PRECISION (qualified_type)
10237 && TYPE_UNSIGNED (t)
10238 == TYPE_UNSIGNED (qualified_type));
10239 qualified_type = t;
10242 /* If we do, then we can just use its DIE, if it exists. */
10243 if (qualified_type)
10245 mod_type_die = lookup_type_die (qualified_type);
10246 if (mod_type_die)
10247 return mod_type_die;
10250 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10252 /* Handle C typedef types. */
10253 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10254 && !DECL_ARTIFICIAL (name))
10256 tree dtype = TREE_TYPE (name);
10258 if (qualified_type == dtype)
10260 /* For a named type, use the typedef. */
10261 gen_type_die (qualified_type, context_die);
10262 return lookup_type_die (qualified_type);
10264 else if (is_const_type < TYPE_READONLY (dtype)
10265 || is_volatile_type < TYPE_VOLATILE (dtype)
10266 || (is_const_type <= TYPE_READONLY (dtype)
10267 && is_volatile_type <= TYPE_VOLATILE (dtype)
10268 && DECL_ORIGINAL_TYPE (name) != type))
10269 /* cv-unqualified version of named type. Just use the unnamed
10270 type to which it refers. */
10271 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10272 is_const_type, is_volatile_type,
10273 context_die);
10274 /* Else cv-qualified version of named type; fall through. */
10277 mod_scope = scope_die_for (type, context_die);
10279 if (is_const_type
10280 /* If both is_const_type and is_volatile_type, prefer the path
10281 which leads to a qualified type. */
10282 && (!is_volatile_type
10283 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10284 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10286 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10287 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10289 else if (is_volatile_type)
10291 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10292 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10294 else if (code == POINTER_TYPE)
10296 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10297 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10298 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10299 item_type = TREE_TYPE (type);
10300 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10301 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10302 TYPE_ADDR_SPACE (item_type));
10304 else if (code == REFERENCE_TYPE)
10306 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10307 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10308 type);
10309 else
10310 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10311 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10312 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10313 item_type = TREE_TYPE (type);
10314 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10315 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10316 TYPE_ADDR_SPACE (item_type));
10318 else if (code == INTEGER_TYPE
10319 && TREE_TYPE (type) != NULL_TREE
10320 && subrange_type_for_debug_p (type, &low, &high))
10322 mod_type_die = subrange_type_die (type, low, high, context_die);
10323 item_type = TREE_TYPE (type);
10325 else if (is_base_type (type))
10326 mod_type_die = base_type_die (type);
10327 else
10329 gen_type_die (type, context_die);
10331 /* We have to get the type_main_variant here (and pass that to the
10332 `lookup_type_die' routine) because the ..._TYPE node we have
10333 might simply be a *copy* of some original type node (where the
10334 copy was created to help us keep track of typedef names) and
10335 that copy might have a different TYPE_UID from the original
10336 ..._TYPE node. */
10337 if (TREE_CODE (type) != VECTOR_TYPE)
10338 return lookup_type_die (type_main_variant (type));
10339 else
10340 /* Vectors have the debugging information in the type,
10341 not the main variant. */
10342 return lookup_type_die (type);
10345 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10346 don't output a DW_TAG_typedef, since there isn't one in the
10347 user's program; just attach a DW_AT_name to the type.
10348 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10349 if the base type already has the same name. */
10350 if (name
10351 && ((TREE_CODE (name) != TYPE_DECL
10352 && (qualified_type == TYPE_MAIN_VARIANT (type)
10353 || (!is_const_type && !is_volatile_type)))
10354 || (TREE_CODE (name) == TYPE_DECL
10355 && TREE_TYPE (name) == qualified_type
10356 && DECL_NAME (name))))
10358 if (TREE_CODE (name) == TYPE_DECL)
10359 /* Could just call add_name_and_src_coords_attributes here,
10360 but since this is a builtin type it doesn't have any
10361 useful source coordinates anyway. */
10362 name = DECL_NAME (name);
10363 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10365 /* This probably indicates a bug. */
10366 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10368 name = TYPE_NAME (type);
10369 if (name
10370 && TREE_CODE (name) == TYPE_DECL)
10371 name = DECL_NAME (name);
10372 add_name_attribute (mod_type_die,
10373 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10376 if (qualified_type)
10377 equate_type_number_to_die (qualified_type, mod_type_die);
10379 if (item_type)
10380 /* We must do this after the equate_type_number_to_die call, in case
10381 this is a recursive type. This ensures that the modified_type_die
10382 recursion will terminate even if the type is recursive. Recursive
10383 types are possible in Ada. */
10384 sub_die = modified_type_die (item_type,
10385 TYPE_READONLY (item_type),
10386 TYPE_VOLATILE (item_type),
10387 context_die);
10389 if (sub_die != NULL)
10390 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10392 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10393 if (TYPE_ARTIFICIAL (type))
10394 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10396 return mod_type_die;
10399 /* Generate DIEs for the generic parameters of T.
10400 T must be either a generic type or a generic function.
10401 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10403 static void
10404 gen_generic_params_dies (tree t)
10406 tree parms, args;
10407 int parms_num, i;
10408 dw_die_ref die = NULL;
10409 int non_default;
10411 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10412 return;
10414 if (TYPE_P (t))
10415 die = lookup_type_die (t);
10416 else if (DECL_P (t))
10417 die = lookup_decl_die (t);
10419 gcc_assert (die);
10421 parms = lang_hooks.get_innermost_generic_parms (t);
10422 if (!parms)
10423 /* T has no generic parameter. It means T is neither a generic type
10424 or function. End of story. */
10425 return;
10427 parms_num = TREE_VEC_LENGTH (parms);
10428 args = lang_hooks.get_innermost_generic_args (t);
10429 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10430 non_default = int_cst_value (TREE_CHAIN (args));
10431 else
10432 non_default = TREE_VEC_LENGTH (args);
10433 for (i = 0; i < parms_num; i++)
10435 tree parm, arg, arg_pack_elems;
10436 dw_die_ref parm_die;
10438 parm = TREE_VEC_ELT (parms, i);
10439 arg = TREE_VEC_ELT (args, i);
10440 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10441 gcc_assert (parm && TREE_VALUE (parm) && arg);
10443 if (parm && TREE_VALUE (parm) && arg)
10445 /* If PARM represents a template parameter pack,
10446 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10447 by DW_TAG_template_*_parameter DIEs for the argument
10448 pack elements of ARG. Note that ARG would then be
10449 an argument pack. */
10450 if (arg_pack_elems)
10451 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10452 arg_pack_elems,
10453 die);
10454 else
10455 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10456 true /* emit name */, die);
10457 if (i >= non_default)
10458 add_AT_flag (parm_die, DW_AT_default_value, 1);
10463 /* Create and return a DIE for PARM which should be
10464 the representation of a generic type parameter.
10465 For instance, in the C++ front end, PARM would be a template parameter.
10466 ARG is the argument to PARM.
10467 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10468 name of the PARM.
10469 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10470 as a child node. */
10472 static dw_die_ref
10473 generic_parameter_die (tree parm, tree arg,
10474 bool emit_name_p,
10475 dw_die_ref parent_die)
10477 dw_die_ref tmpl_die = NULL;
10478 const char *name = NULL;
10480 if (!parm || !DECL_NAME (parm) || !arg)
10481 return NULL;
10483 /* We support non-type generic parameters and arguments,
10484 type generic parameters and arguments, as well as
10485 generic generic parameters (a.k.a. template template parameters in C++)
10486 and arguments. */
10487 if (TREE_CODE (parm) == PARM_DECL)
10488 /* PARM is a nontype generic parameter */
10489 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10490 else if (TREE_CODE (parm) == TYPE_DECL)
10491 /* PARM is a type generic parameter. */
10492 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10493 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10494 /* PARM is a generic generic parameter.
10495 Its DIE is a GNU extension. It shall have a
10496 DW_AT_name attribute to represent the name of the template template
10497 parameter, and a DW_AT_GNU_template_name attribute to represent the
10498 name of the template template argument. */
10499 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10500 parent_die, parm);
10501 else
10502 gcc_unreachable ();
10504 if (tmpl_die)
10506 tree tmpl_type;
10508 /* If PARM is a generic parameter pack, it means we are
10509 emitting debug info for a template argument pack element.
10510 In other terms, ARG is a template argument pack element.
10511 In that case, we don't emit any DW_AT_name attribute for
10512 the die. */
10513 if (emit_name_p)
10515 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10516 gcc_assert (name);
10517 add_AT_string (tmpl_die, DW_AT_name, name);
10520 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10522 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10523 TMPL_DIE should have a child DW_AT_type attribute that is set
10524 to the type of the argument to PARM, which is ARG.
10525 If PARM is a type generic parameter, TMPL_DIE should have a
10526 child DW_AT_type that is set to ARG. */
10527 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10528 add_type_attribute (tmpl_die, tmpl_type, 0,
10529 TREE_THIS_VOLATILE (tmpl_type),
10530 parent_die);
10532 else
10534 /* So TMPL_DIE is a DIE representing a
10535 a generic generic template parameter, a.k.a template template
10536 parameter in C++ and arg is a template. */
10538 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10539 to the name of the argument. */
10540 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10541 if (name)
10542 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10545 if (TREE_CODE (parm) == PARM_DECL)
10546 /* So PARM is a non-type generic parameter.
10547 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10548 attribute of TMPL_DIE which value represents the value
10549 of ARG.
10550 We must be careful here:
10551 The value of ARG might reference some function decls.
10552 We might currently be emitting debug info for a generic
10553 type and types are emitted before function decls, we don't
10554 know if the function decls referenced by ARG will actually be
10555 emitted after cgraph computations.
10556 So must defer the generation of the DW_AT_const_value to
10557 after cgraph is ready. */
10558 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10561 return tmpl_die;
10564 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10565 PARM_PACK must be a template parameter pack. The returned DIE
10566 will be child DIE of PARENT_DIE. */
10568 static dw_die_ref
10569 template_parameter_pack_die (tree parm_pack,
10570 tree parm_pack_args,
10571 dw_die_ref parent_die)
10573 dw_die_ref die;
10574 int j;
10576 gcc_assert (parent_die && parm_pack);
10578 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10579 add_name_and_src_coords_attributes (die, parm_pack);
10580 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10581 generic_parameter_die (parm_pack,
10582 TREE_VEC_ELT (parm_pack_args, j),
10583 false /* Don't emit DW_AT_name */,
10584 die);
10585 return die;
10588 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10589 an enumerated type. */
10591 static inline int
10592 type_is_enum (const_tree type)
10594 return TREE_CODE (type) == ENUMERAL_TYPE;
10597 /* Return the DBX register number described by a given RTL node. */
10599 static unsigned int
10600 dbx_reg_number (const_rtx rtl)
10602 unsigned regno = REGNO (rtl);
10604 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10606 #ifdef LEAF_REG_REMAP
10607 if (crtl->uses_only_leaf_regs)
10609 int leaf_reg = LEAF_REG_REMAP (regno);
10610 if (leaf_reg != -1)
10611 regno = (unsigned) leaf_reg;
10613 #endif
10615 regno = DBX_REGISTER_NUMBER (regno);
10616 gcc_assert (regno != INVALID_REGNUM);
10617 return regno;
10620 /* Optionally add a DW_OP_piece term to a location description expression.
10621 DW_OP_piece is only added if the location description expression already
10622 doesn't end with DW_OP_piece. */
10624 static void
10625 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10627 dw_loc_descr_ref loc;
10629 if (*list_head != NULL)
10631 /* Find the end of the chain. */
10632 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10635 if (loc->dw_loc_opc != DW_OP_piece)
10636 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10640 /* Return a location descriptor that designates a machine register or
10641 zero if there is none. */
10643 static dw_loc_descr_ref
10644 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10646 rtx regs;
10648 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10649 return 0;
10651 /* We only use "frame base" when we're sure we're talking about the
10652 post-prologue local stack frame. We do this by *not* running
10653 register elimination until this point, and recognizing the special
10654 argument pointer and soft frame pointer rtx's.
10655 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10656 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10657 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10659 dw_loc_descr_ref result = NULL;
10661 if (dwarf_version >= 4 || !dwarf_strict)
10663 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10664 initialized);
10665 if (result)
10666 add_loc_descr (&result,
10667 new_loc_descr (DW_OP_stack_value, 0, 0));
10669 return result;
10672 regs = targetm.dwarf_register_span (rtl);
10674 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10675 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10676 else
10678 unsigned int dbx_regnum = dbx_reg_number (rtl);
10679 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10680 return 0;
10681 return one_reg_loc_descriptor (dbx_regnum, initialized);
10685 /* Return a location descriptor that designates a machine register for
10686 a given hard register number. */
10688 static dw_loc_descr_ref
10689 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10691 dw_loc_descr_ref reg_loc_descr;
10693 if (regno <= 31)
10694 reg_loc_descr
10695 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10696 else
10697 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10699 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10700 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10702 return reg_loc_descr;
10705 /* Given an RTL of a register, return a location descriptor that
10706 designates a value that spans more than one register. */
10708 static dw_loc_descr_ref
10709 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10710 enum var_init_status initialized)
10712 int size, i;
10713 dw_loc_descr_ref loc_result = NULL;
10715 /* Simple, contiguous registers. */
10716 if (regs == NULL_RTX)
10718 unsigned reg = REGNO (rtl);
10719 int nregs;
10721 #ifdef LEAF_REG_REMAP
10722 if (crtl->uses_only_leaf_regs)
10724 int leaf_reg = LEAF_REG_REMAP (reg);
10725 if (leaf_reg != -1)
10726 reg = (unsigned) leaf_reg;
10728 #endif
10730 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10731 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10733 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10735 loc_result = NULL;
10736 while (nregs--)
10738 dw_loc_descr_ref t;
10740 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10741 VAR_INIT_STATUS_INITIALIZED);
10742 add_loc_descr (&loc_result, t);
10743 add_loc_descr_op_piece (&loc_result, size);
10744 ++reg;
10746 return loc_result;
10749 /* Now onto stupid register sets in non contiguous locations. */
10751 gcc_assert (GET_CODE (regs) == PARALLEL);
10753 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10754 loc_result = NULL;
10756 for (i = 0; i < XVECLEN (regs, 0); ++i)
10758 dw_loc_descr_ref t;
10760 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10761 VAR_INIT_STATUS_INITIALIZED);
10762 add_loc_descr (&loc_result, t);
10763 add_loc_descr_op_piece (&loc_result, size);
10766 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10767 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10768 return loc_result;
10771 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10773 /* Return a location descriptor that designates a constant i,
10774 as a compound operation from constant (i >> shift), constant shift
10775 and DW_OP_shl. */
10777 static dw_loc_descr_ref
10778 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10780 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10781 add_loc_descr (&ret, int_loc_descriptor (shift));
10782 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10783 return ret;
10786 /* Return a location descriptor that designates a constant. */
10788 static dw_loc_descr_ref
10789 int_loc_descriptor (HOST_WIDE_INT i)
10791 enum dwarf_location_atom op;
10793 /* Pick the smallest representation of a constant, rather than just
10794 defaulting to the LEB encoding. */
10795 if (i >= 0)
10797 int clz = clz_hwi (i);
10798 int ctz = ctz_hwi (i);
10799 if (i <= 31)
10800 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10801 else if (i <= 0xff)
10802 op = DW_OP_const1u;
10803 else if (i <= 0xffff)
10804 op = DW_OP_const2u;
10805 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10806 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10807 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10808 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10809 while DW_OP_const4u is 5 bytes. */
10810 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10811 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10812 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10813 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10814 while DW_OP_const4u is 5 bytes. */
10815 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10816 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10817 op = DW_OP_const4u;
10818 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10819 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10820 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10821 while DW_OP_constu of constant >= 0x100000000 takes at least
10822 6 bytes. */
10823 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10824 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10825 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10826 >= HOST_BITS_PER_WIDE_INT)
10827 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10828 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10829 while DW_OP_constu takes in this case at least 6 bytes. */
10830 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10831 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10832 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10833 && size_of_uleb128 (i) > 6)
10834 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10835 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10836 else
10837 op = DW_OP_constu;
10839 else
10841 if (i >= -0x80)
10842 op = DW_OP_const1s;
10843 else if (i >= -0x8000)
10844 op = DW_OP_const2s;
10845 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10847 if (size_of_int_loc_descriptor (i) < 5)
10849 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10850 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10851 return ret;
10853 op = DW_OP_const4s;
10855 else
10857 if (size_of_int_loc_descriptor (i)
10858 < (unsigned long) 1 + size_of_sleb128 (i))
10860 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10861 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10862 return ret;
10864 op = DW_OP_consts;
10868 return new_loc_descr (op, i, 0);
10871 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10872 without actually allocating it. */
10874 static unsigned long
10875 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10877 return size_of_int_loc_descriptor (i >> shift)
10878 + size_of_int_loc_descriptor (shift)
10879 + 1;
10882 /* Return size_of_locs (int_loc_descriptor (i)) without
10883 actually allocating it. */
10885 static unsigned long
10886 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10888 unsigned long s;
10890 if (i >= 0)
10892 int clz, ctz;
10893 if (i <= 31)
10894 return 1;
10895 else if (i <= 0xff)
10896 return 2;
10897 else if (i <= 0xffff)
10898 return 3;
10899 clz = clz_hwi (i);
10900 ctz = ctz_hwi (i);
10901 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10902 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10903 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10904 - clz - 5);
10905 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10906 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10907 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10908 - clz - 8);
10909 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10910 return 5;
10911 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10912 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10913 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10914 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10915 - clz - 8);
10916 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10917 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10918 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10919 - clz - 16);
10920 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10921 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10922 && s > 6)
10923 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10924 - clz - 32);
10925 else
10926 return 1 + s;
10928 else
10930 if (i >= -0x80)
10931 return 2;
10932 else if (i >= -0x8000)
10933 return 3;
10934 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10936 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10938 s = size_of_int_loc_descriptor (-i) + 1;
10939 if (s < 5)
10940 return s;
10942 return 5;
10944 else
10946 unsigned long r = 1 + size_of_sleb128 (i);
10947 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10949 s = size_of_int_loc_descriptor (-i) + 1;
10950 if (s < r)
10951 return s;
10953 return r;
10958 /* Return loc description representing "address" of integer value.
10959 This can appear only as toplevel expression. */
10961 static dw_loc_descr_ref
10962 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10964 int litsize;
10965 dw_loc_descr_ref loc_result = NULL;
10967 if (!(dwarf_version >= 4 || !dwarf_strict))
10968 return NULL;
10970 litsize = size_of_int_loc_descriptor (i);
10971 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10972 is more compact. For DW_OP_stack_value we need:
10973 litsize + 1 (DW_OP_stack_value)
10974 and for DW_OP_implicit_value:
10975 1 (DW_OP_implicit_value) + 1 (length) + size. */
10976 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10978 loc_result = int_loc_descriptor (i);
10979 add_loc_descr (&loc_result,
10980 new_loc_descr (DW_OP_stack_value, 0, 0));
10981 return loc_result;
10984 loc_result = new_loc_descr (DW_OP_implicit_value,
10985 size, 0);
10986 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10987 loc_result->dw_loc_oprnd2.v.val_int = i;
10988 return loc_result;
10991 /* Return a location descriptor that designates a base+offset location. */
10993 static dw_loc_descr_ref
10994 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10995 enum var_init_status initialized)
10997 unsigned int regno;
10998 dw_loc_descr_ref result;
10999 dw_fde_ref fde = cfun->fde;
11001 /* We only use "frame base" when we're sure we're talking about the
11002 post-prologue local stack frame. We do this by *not* running
11003 register elimination until this point, and recognizing the special
11004 argument pointer and soft frame pointer rtx's. */
11005 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11007 rtx elim = (ira_use_lra_p
11008 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11009 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11011 if (elim != reg)
11013 if (GET_CODE (elim) == PLUS)
11015 offset += INTVAL (XEXP (elim, 1));
11016 elim = XEXP (elim, 0);
11018 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11019 && (elim == hard_frame_pointer_rtx
11020 || elim == stack_pointer_rtx))
11021 || elim == (frame_pointer_needed
11022 ? hard_frame_pointer_rtx
11023 : stack_pointer_rtx));
11025 /* If drap register is used to align stack, use frame
11026 pointer + offset to access stack variables. If stack
11027 is aligned without drap, use stack pointer + offset to
11028 access stack variables. */
11029 if (crtl->stack_realign_tried
11030 && reg == frame_pointer_rtx)
11032 int base_reg
11033 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11034 ? HARD_FRAME_POINTER_REGNUM
11035 : REGNO (elim));
11036 return new_reg_loc_descr (base_reg, offset);
11039 gcc_assert (frame_pointer_fb_offset_valid);
11040 offset += frame_pointer_fb_offset;
11041 return new_loc_descr (DW_OP_fbreg, offset, 0);
11045 regno = REGNO (reg);
11046 #ifdef LEAF_REG_REMAP
11047 if (crtl->uses_only_leaf_regs)
11049 int leaf_reg = LEAF_REG_REMAP (regno);
11050 if (leaf_reg != -1)
11051 regno = (unsigned) leaf_reg;
11053 #endif
11054 regno = DWARF_FRAME_REGNUM (regno);
11056 if (!optimize && fde
11057 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11059 /* Use cfa+offset to represent the location of arguments passed
11060 on the stack when drap is used to align stack.
11061 Only do this when not optimizing, for optimized code var-tracking
11062 is supposed to track where the arguments live and the register
11063 used as vdrap or drap in some spot might be used for something
11064 else in other part of the routine. */
11065 return new_loc_descr (DW_OP_fbreg, offset, 0);
11068 if (regno <= 31)
11069 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11070 offset, 0);
11071 else
11072 result = new_loc_descr (DW_OP_bregx, regno, offset);
11074 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11075 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11077 return result;
11080 /* Return true if this RTL expression describes a base+offset calculation. */
11082 static inline int
11083 is_based_loc (const_rtx rtl)
11085 return (GET_CODE (rtl) == PLUS
11086 && ((REG_P (XEXP (rtl, 0))
11087 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11088 && CONST_INT_P (XEXP (rtl, 1)))));
11091 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11092 failed. */
11094 static dw_loc_descr_ref
11095 tls_mem_loc_descriptor (rtx mem)
11097 tree base;
11098 dw_loc_descr_ref loc_result;
11100 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11101 return NULL;
11103 base = get_base_address (MEM_EXPR (mem));
11104 if (base == NULL
11105 || TREE_CODE (base) != VAR_DECL
11106 || !DECL_THREAD_LOCAL_P (base))
11107 return NULL;
11109 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11110 if (loc_result == NULL)
11111 return NULL;
11113 if (MEM_OFFSET (mem))
11114 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11116 return loc_result;
11119 /* Output debug info about reason why we failed to expand expression as dwarf
11120 expression. */
11122 static void
11123 expansion_failed (tree expr, rtx rtl, char const *reason)
11125 if (dump_file && (dump_flags & TDF_DETAILS))
11127 fprintf (dump_file, "Failed to expand as dwarf: ");
11128 if (expr)
11129 print_generic_expr (dump_file, expr, dump_flags);
11130 if (rtl)
11132 fprintf (dump_file, "\n");
11133 print_rtl (dump_file, rtl);
11135 fprintf (dump_file, "\nReason: %s\n", reason);
11139 /* Helper function for const_ok_for_output, called either directly
11140 or via for_each_rtx. */
11142 static int
11143 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11145 rtx rtl = *rtlp;
11147 if (GET_CODE (rtl) == UNSPEC)
11149 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11150 we can't express it in the debug info. */
11151 #ifdef ENABLE_CHECKING
11152 /* Don't complain about TLS UNSPECs, those are just too hard to
11153 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11154 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11155 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11156 if (XVECLEN (rtl, 0) == 0
11157 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11158 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11159 inform (current_function_decl
11160 ? DECL_SOURCE_LOCATION (current_function_decl)
11161 : UNKNOWN_LOCATION,
11162 #if NUM_UNSPEC_VALUES > 0
11163 "non-delegitimized UNSPEC %s (%d) found in variable location",
11164 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11165 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11166 XINT (rtl, 1));
11167 #else
11168 "non-delegitimized UNSPEC %d found in variable location",
11169 XINT (rtl, 1));
11170 #endif
11171 #endif
11172 expansion_failed (NULL_TREE, rtl,
11173 "UNSPEC hasn't been delegitimized.\n");
11174 return 1;
11177 if (targetm.const_not_ok_for_debug_p (rtl))
11179 expansion_failed (NULL_TREE, rtl,
11180 "Expression rejected for debug by the backend.\n");
11181 return 1;
11184 if (GET_CODE (rtl) != SYMBOL_REF)
11185 return 0;
11187 if (CONSTANT_POOL_ADDRESS_P (rtl))
11189 bool marked;
11190 get_pool_constant_mark (rtl, &marked);
11191 /* If all references to this pool constant were optimized away,
11192 it was not output and thus we can't represent it. */
11193 if (!marked)
11195 expansion_failed (NULL_TREE, rtl,
11196 "Constant was removed from constant pool.\n");
11197 return 1;
11201 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11202 return 1;
11204 /* Avoid references to external symbols in debug info, on several targets
11205 the linker might even refuse to link when linking a shared library,
11206 and in many other cases the relocations for .debug_info/.debug_loc are
11207 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11208 to be defined within the same shared library or executable are fine. */
11209 if (SYMBOL_REF_EXTERNAL_P (rtl))
11211 tree decl = SYMBOL_REF_DECL (rtl);
11213 if (decl == NULL || !targetm.binds_local_p (decl))
11215 expansion_failed (NULL_TREE, rtl,
11216 "Symbol not defined in current TU.\n");
11217 return 1;
11221 return 0;
11224 /* Return true if constant RTL can be emitted in DW_OP_addr or
11225 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11226 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11228 static bool
11229 const_ok_for_output (rtx rtl)
11231 if (GET_CODE (rtl) == SYMBOL_REF)
11232 return const_ok_for_output_1 (&rtl, NULL) == 0;
11234 if (GET_CODE (rtl) == CONST)
11235 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11237 return true;
11240 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11241 if possible, NULL otherwise. */
11243 static dw_die_ref
11244 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11246 dw_die_ref type_die;
11247 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11249 if (type == NULL)
11250 return NULL;
11251 switch (TREE_CODE (type))
11253 case INTEGER_TYPE:
11254 case REAL_TYPE:
11255 break;
11256 default:
11257 return NULL;
11259 type_die = lookup_type_die (type);
11260 if (!type_die)
11261 type_die = modified_type_die (type, false, false, comp_unit_die ());
11262 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11263 return NULL;
11264 return type_die;
11267 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11268 type matching MODE, or, if MODE is narrower than or as wide as
11269 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11270 possible. */
11272 static dw_loc_descr_ref
11273 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11275 enum machine_mode outer_mode = mode;
11276 dw_die_ref type_die;
11277 dw_loc_descr_ref cvt;
11279 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11281 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11282 return op;
11284 type_die = base_type_for_mode (outer_mode, 1);
11285 if (type_die == NULL)
11286 return NULL;
11287 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11288 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11289 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11290 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11291 add_loc_descr (&op, cvt);
11292 return op;
11295 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11297 static dw_loc_descr_ref
11298 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11299 dw_loc_descr_ref op1)
11301 dw_loc_descr_ref ret = op0;
11302 add_loc_descr (&ret, op1);
11303 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11304 if (STORE_FLAG_VALUE != 1)
11306 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11307 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11309 return ret;
11312 /* Return location descriptor for signed comparison OP RTL. */
11314 static dw_loc_descr_ref
11315 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11316 enum machine_mode mem_mode)
11318 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11319 dw_loc_descr_ref op0, op1;
11320 int shift;
11322 if (op_mode == VOIDmode)
11323 op_mode = GET_MODE (XEXP (rtl, 1));
11324 if (op_mode == VOIDmode)
11325 return NULL;
11327 if (dwarf_strict
11328 && (GET_MODE_CLASS (op_mode) != MODE_INT
11329 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11330 return NULL;
11332 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11333 VAR_INIT_STATUS_INITIALIZED);
11334 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11335 VAR_INIT_STATUS_INITIALIZED);
11337 if (op0 == NULL || op1 == NULL)
11338 return NULL;
11340 if (GET_MODE_CLASS (op_mode) != MODE_INT
11341 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11342 return compare_loc_descriptor (op, op0, op1);
11344 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11346 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11347 dw_loc_descr_ref cvt;
11349 if (type_die == NULL)
11350 return NULL;
11351 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11352 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11353 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11354 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11355 add_loc_descr (&op0, cvt);
11356 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11357 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11358 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11359 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11360 add_loc_descr (&op1, cvt);
11361 return compare_loc_descriptor (op, op0, op1);
11364 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11365 /* For eq/ne, if the operands are known to be zero-extended,
11366 there is no need to do the fancy shifting up. */
11367 if (op == DW_OP_eq || op == DW_OP_ne)
11369 dw_loc_descr_ref last0, last1;
11370 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11372 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11374 /* deref_size zero extends, and for constants we can check
11375 whether they are zero extended or not. */
11376 if (((last0->dw_loc_opc == DW_OP_deref_size
11377 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11378 || (CONST_INT_P (XEXP (rtl, 0))
11379 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11380 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11381 && ((last1->dw_loc_opc == DW_OP_deref_size
11382 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11383 || (CONST_INT_P (XEXP (rtl, 1))
11384 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11385 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11386 return compare_loc_descriptor (op, op0, op1);
11388 /* EQ/NE comparison against constant in narrower type than
11389 DWARF2_ADDR_SIZE can be performed either as
11390 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11391 DW_OP_{eq,ne}
11393 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11394 DW_OP_{eq,ne}. Pick whatever is shorter. */
11395 if (CONST_INT_P (XEXP (rtl, 1))
11396 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11397 && (size_of_int_loc_descriptor (shift) + 1
11398 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11399 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11400 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11401 & GET_MODE_MASK (op_mode))))
11403 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11404 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11405 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11406 & GET_MODE_MASK (op_mode));
11407 return compare_loc_descriptor (op, op0, op1);
11410 add_loc_descr (&op0, int_loc_descriptor (shift));
11411 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11412 if (CONST_INT_P (XEXP (rtl, 1)))
11413 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11414 else
11416 add_loc_descr (&op1, int_loc_descriptor (shift));
11417 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11419 return compare_loc_descriptor (op, op0, op1);
11422 /* Return location descriptor for unsigned comparison OP RTL. */
11424 static dw_loc_descr_ref
11425 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11426 enum machine_mode mem_mode)
11428 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11429 dw_loc_descr_ref op0, op1;
11431 if (op_mode == VOIDmode)
11432 op_mode = GET_MODE (XEXP (rtl, 1));
11433 if (op_mode == VOIDmode)
11434 return NULL;
11435 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11436 return NULL;
11438 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11439 return NULL;
11441 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11442 VAR_INIT_STATUS_INITIALIZED);
11443 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11444 VAR_INIT_STATUS_INITIALIZED);
11446 if (op0 == NULL || op1 == NULL)
11447 return NULL;
11449 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11451 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11452 dw_loc_descr_ref last0, last1;
11453 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11455 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11457 if (CONST_INT_P (XEXP (rtl, 0)))
11458 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11459 /* deref_size zero extends, so no need to mask it again. */
11460 else if (last0->dw_loc_opc != DW_OP_deref_size
11461 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11463 add_loc_descr (&op0, int_loc_descriptor (mask));
11464 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11466 if (CONST_INT_P (XEXP (rtl, 1)))
11467 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11468 /* deref_size zero extends, so no need to mask it again. */
11469 else if (last1->dw_loc_opc != DW_OP_deref_size
11470 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11472 add_loc_descr (&op1, int_loc_descriptor (mask));
11473 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11476 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11478 HOST_WIDE_INT bias = 1;
11479 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11480 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11481 if (CONST_INT_P (XEXP (rtl, 1)))
11482 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11483 + INTVAL (XEXP (rtl, 1)));
11484 else
11485 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11486 bias, 0));
11488 return compare_loc_descriptor (op, op0, op1);
11491 /* Return location descriptor for {U,S}{MIN,MAX}. */
11493 static dw_loc_descr_ref
11494 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11495 enum machine_mode mem_mode)
11497 enum dwarf_location_atom op;
11498 dw_loc_descr_ref op0, op1, ret;
11499 dw_loc_descr_ref bra_node, drop_node;
11501 if (dwarf_strict
11502 && (GET_MODE_CLASS (mode) != MODE_INT
11503 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11504 return NULL;
11506 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11507 VAR_INIT_STATUS_INITIALIZED);
11508 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11509 VAR_INIT_STATUS_INITIALIZED);
11511 if (op0 == NULL || op1 == NULL)
11512 return NULL;
11514 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11515 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11516 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11517 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11519 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11521 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11522 add_loc_descr (&op0, int_loc_descriptor (mask));
11523 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11524 add_loc_descr (&op1, int_loc_descriptor (mask));
11525 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11527 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11529 HOST_WIDE_INT bias = 1;
11530 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11531 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11532 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11535 else if (GET_MODE_CLASS (mode) == MODE_INT
11536 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11538 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11539 add_loc_descr (&op0, int_loc_descriptor (shift));
11540 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11541 add_loc_descr (&op1, int_loc_descriptor (shift));
11542 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11544 else if (GET_MODE_CLASS (mode) == MODE_INT
11545 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11547 dw_die_ref type_die = base_type_for_mode (mode, 0);
11548 dw_loc_descr_ref cvt;
11549 if (type_die == NULL)
11550 return NULL;
11551 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11552 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11553 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11554 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11555 add_loc_descr (&op0, cvt);
11556 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11557 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11558 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11559 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11560 add_loc_descr (&op1, cvt);
11563 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11564 op = DW_OP_lt;
11565 else
11566 op = DW_OP_gt;
11567 ret = op0;
11568 add_loc_descr (&ret, op1);
11569 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11570 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11571 add_loc_descr (&ret, bra_node);
11572 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11573 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11574 add_loc_descr (&ret, drop_node);
11575 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11576 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11577 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11578 && GET_MODE_CLASS (mode) == MODE_INT
11579 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11580 ret = convert_descriptor_to_mode (mode, ret);
11581 return ret;
11584 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11585 but after converting arguments to type_die, afterwards
11586 convert back to unsigned. */
11588 static dw_loc_descr_ref
11589 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11590 enum machine_mode mode, enum machine_mode mem_mode)
11592 dw_loc_descr_ref cvt, op0, op1;
11594 if (type_die == NULL)
11595 return NULL;
11596 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11597 VAR_INIT_STATUS_INITIALIZED);
11598 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11599 VAR_INIT_STATUS_INITIALIZED);
11600 if (op0 == NULL || op1 == NULL)
11601 return NULL;
11602 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11603 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11604 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11605 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11606 add_loc_descr (&op0, cvt);
11607 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11608 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11609 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11610 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11611 add_loc_descr (&op1, cvt);
11612 add_loc_descr (&op0, op1);
11613 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11614 return convert_descriptor_to_mode (mode, op0);
11617 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11618 const0 is DW_OP_lit0 or corresponding typed constant,
11619 const1 is DW_OP_lit1 or corresponding typed constant
11620 and constMSB is constant with just the MSB bit set
11621 for the mode):
11622 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11623 L1: const0 DW_OP_swap
11624 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11625 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11626 L3: DW_OP_drop
11627 L4: DW_OP_nop
11629 CTZ is similar:
11630 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11631 L1: const0 DW_OP_swap
11632 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11633 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11634 L3: DW_OP_drop
11635 L4: DW_OP_nop
11637 FFS is similar:
11638 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11639 L1: const1 DW_OP_swap
11640 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11641 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11642 L3: DW_OP_drop
11643 L4: DW_OP_nop */
11645 static dw_loc_descr_ref
11646 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11647 enum machine_mode mem_mode)
11649 dw_loc_descr_ref op0, ret, tmp;
11650 HOST_WIDE_INT valv;
11651 dw_loc_descr_ref l1jump, l1label;
11652 dw_loc_descr_ref l2jump, l2label;
11653 dw_loc_descr_ref l3jump, l3label;
11654 dw_loc_descr_ref l4jump, l4label;
11655 rtx msb;
11657 if (GET_MODE_CLASS (mode) != MODE_INT
11658 || GET_MODE (XEXP (rtl, 0)) != mode
11659 || (GET_CODE (rtl) == CLZ
11660 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11661 return NULL;
11663 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11664 VAR_INIT_STATUS_INITIALIZED);
11665 if (op0 == NULL)
11666 return NULL;
11667 ret = op0;
11668 if (GET_CODE (rtl) == CLZ)
11670 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11671 valv = GET_MODE_BITSIZE (mode);
11673 else if (GET_CODE (rtl) == FFS)
11674 valv = 0;
11675 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11676 valv = GET_MODE_BITSIZE (mode);
11677 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11678 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11679 add_loc_descr (&ret, l1jump);
11680 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11681 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11682 VAR_INIT_STATUS_INITIALIZED);
11683 if (tmp == NULL)
11684 return NULL;
11685 add_loc_descr (&ret, tmp);
11686 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11687 add_loc_descr (&ret, l4jump);
11688 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11689 ? const1_rtx : const0_rtx,
11690 mode, mem_mode,
11691 VAR_INIT_STATUS_INITIALIZED);
11692 if (l1label == NULL)
11693 return NULL;
11694 add_loc_descr (&ret, l1label);
11695 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11696 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11697 add_loc_descr (&ret, l2label);
11698 if (GET_CODE (rtl) != CLZ)
11699 msb = const1_rtx;
11700 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11701 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11702 << (GET_MODE_BITSIZE (mode) - 1));
11703 else
11704 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11705 << (GET_MODE_BITSIZE (mode)
11706 - HOST_BITS_PER_WIDE_INT - 1), mode);
11707 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11708 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11709 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11710 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11711 else
11712 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11713 VAR_INIT_STATUS_INITIALIZED);
11714 if (tmp == NULL)
11715 return NULL;
11716 add_loc_descr (&ret, tmp);
11717 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11718 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11719 add_loc_descr (&ret, l3jump);
11720 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11721 VAR_INIT_STATUS_INITIALIZED);
11722 if (tmp == NULL)
11723 return NULL;
11724 add_loc_descr (&ret, tmp);
11725 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11726 ? DW_OP_shl : DW_OP_shr, 0, 0));
11727 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11728 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11729 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11730 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11731 add_loc_descr (&ret, l2jump);
11732 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11733 add_loc_descr (&ret, l3label);
11734 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11735 add_loc_descr (&ret, l4label);
11736 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11737 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11738 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11739 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11740 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11741 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11742 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11743 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11744 return ret;
11747 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11748 const1 is DW_OP_lit1 or corresponding typed constant):
11749 const0 DW_OP_swap
11750 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11751 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11752 L2: DW_OP_drop
11754 PARITY is similar:
11755 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11756 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11757 L2: DW_OP_drop */
11759 static dw_loc_descr_ref
11760 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11761 enum machine_mode mem_mode)
11763 dw_loc_descr_ref op0, ret, tmp;
11764 dw_loc_descr_ref l1jump, l1label;
11765 dw_loc_descr_ref l2jump, l2label;
11767 if (GET_MODE_CLASS (mode) != MODE_INT
11768 || GET_MODE (XEXP (rtl, 0)) != mode)
11769 return NULL;
11771 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11772 VAR_INIT_STATUS_INITIALIZED);
11773 if (op0 == NULL)
11774 return NULL;
11775 ret = op0;
11776 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11777 VAR_INIT_STATUS_INITIALIZED);
11778 if (tmp == NULL)
11779 return NULL;
11780 add_loc_descr (&ret, tmp);
11781 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11782 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11783 add_loc_descr (&ret, l1label);
11784 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11785 add_loc_descr (&ret, l2jump);
11786 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11787 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11788 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11789 VAR_INIT_STATUS_INITIALIZED);
11790 if (tmp == NULL)
11791 return NULL;
11792 add_loc_descr (&ret, tmp);
11793 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11794 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11795 ? DW_OP_plus : DW_OP_xor, 0, 0));
11796 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11797 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11798 VAR_INIT_STATUS_INITIALIZED);
11799 add_loc_descr (&ret, tmp);
11800 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11801 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11802 add_loc_descr (&ret, l1jump);
11803 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11804 add_loc_descr (&ret, l2label);
11805 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11806 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11807 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11808 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11809 return ret;
11812 /* BSWAP (constS is initial shift count, either 56 or 24):
11813 constS const0
11814 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11815 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11816 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11817 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11818 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11820 static dw_loc_descr_ref
11821 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11822 enum machine_mode mem_mode)
11824 dw_loc_descr_ref op0, ret, tmp;
11825 dw_loc_descr_ref l1jump, l1label;
11826 dw_loc_descr_ref l2jump, l2label;
11828 if (GET_MODE_CLASS (mode) != MODE_INT
11829 || BITS_PER_UNIT != 8
11830 || (GET_MODE_BITSIZE (mode) != 32
11831 && GET_MODE_BITSIZE (mode) != 64))
11832 return NULL;
11834 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11835 VAR_INIT_STATUS_INITIALIZED);
11836 if (op0 == NULL)
11837 return NULL;
11839 ret = op0;
11840 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11841 mode, mem_mode,
11842 VAR_INIT_STATUS_INITIALIZED);
11843 if (tmp == NULL)
11844 return NULL;
11845 add_loc_descr (&ret, tmp);
11846 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11847 VAR_INIT_STATUS_INITIALIZED);
11848 if (tmp == NULL)
11849 return NULL;
11850 add_loc_descr (&ret, tmp);
11851 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11852 add_loc_descr (&ret, l1label);
11853 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11854 mode, mem_mode,
11855 VAR_INIT_STATUS_INITIALIZED);
11856 add_loc_descr (&ret, tmp);
11857 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11858 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11859 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11860 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11861 VAR_INIT_STATUS_INITIALIZED);
11862 if (tmp == NULL)
11863 return NULL;
11864 add_loc_descr (&ret, tmp);
11865 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11866 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11867 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11868 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11869 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11870 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11871 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11872 VAR_INIT_STATUS_INITIALIZED);
11873 add_loc_descr (&ret, tmp);
11874 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11875 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11876 add_loc_descr (&ret, l2jump);
11877 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11878 VAR_INIT_STATUS_INITIALIZED);
11879 add_loc_descr (&ret, tmp);
11880 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11881 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11882 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11883 add_loc_descr (&ret, l1jump);
11884 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11885 add_loc_descr (&ret, l2label);
11886 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11887 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11888 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11889 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11890 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11891 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11892 return ret;
11895 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11896 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11897 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11898 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11900 ROTATERT is similar:
11901 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11902 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11903 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
11905 static dw_loc_descr_ref
11906 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11907 enum machine_mode mem_mode)
11909 rtx rtlop1 = XEXP (rtl, 1);
11910 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11911 int i;
11913 if (GET_MODE_CLASS (mode) != MODE_INT)
11914 return NULL;
11916 if (GET_MODE (rtlop1) != VOIDmode
11917 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11918 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11919 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11920 VAR_INIT_STATUS_INITIALIZED);
11921 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11922 VAR_INIT_STATUS_INITIALIZED);
11923 if (op0 == NULL || op1 == NULL)
11924 return NULL;
11925 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11926 for (i = 0; i < 2; i++)
11928 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11929 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11930 mode, mem_mode,
11931 VAR_INIT_STATUS_INITIALIZED);
11932 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11933 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11934 ? DW_OP_const4u
11935 : HOST_BITS_PER_WIDE_INT == 64
11936 ? DW_OP_const8u : DW_OP_constu,
11937 GET_MODE_MASK (mode), 0);
11938 else
11939 mask[i] = NULL;
11940 if (mask[i] == NULL)
11941 return NULL;
11942 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11944 ret = op0;
11945 add_loc_descr (&ret, op1);
11946 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11947 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11948 if (GET_CODE (rtl) == ROTATERT)
11950 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11951 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11952 GET_MODE_BITSIZE (mode), 0));
11954 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11955 if (mask[0] != NULL)
11956 add_loc_descr (&ret, mask[0]);
11957 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11958 if (mask[1] != NULL)
11960 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11961 add_loc_descr (&ret, mask[1]);
11962 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11964 if (GET_CODE (rtl) == ROTATE)
11966 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11967 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11968 GET_MODE_BITSIZE (mode), 0));
11970 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11971 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11972 return ret;
11975 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
11976 for DEBUG_PARAMETER_REF RTL. */
11978 static dw_loc_descr_ref
11979 parameter_ref_descriptor (rtx rtl)
11981 dw_loc_descr_ref ret;
11982 dw_die_ref ref;
11984 if (dwarf_strict)
11985 return NULL;
11986 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11987 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11988 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11989 if (ref)
11991 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11992 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11993 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11995 else
11997 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11998 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12000 return ret;
12003 /* The following routine converts the RTL for a variable or parameter
12004 (resident in memory) into an equivalent Dwarf representation of a
12005 mechanism for getting the address of that same variable onto the top of a
12006 hypothetical "address evaluation" stack.
12008 When creating memory location descriptors, we are effectively transforming
12009 the RTL for a memory-resident object into its Dwarf postfix expression
12010 equivalent. This routine recursively descends an RTL tree, turning
12011 it into Dwarf postfix code as it goes.
12013 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12015 MEM_MODE is the mode of the memory reference, needed to handle some
12016 autoincrement addressing modes.
12018 Return 0 if we can't represent the location. */
12020 dw_loc_descr_ref
12021 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12022 enum machine_mode mem_mode,
12023 enum var_init_status initialized)
12025 dw_loc_descr_ref mem_loc_result = NULL;
12026 enum dwarf_location_atom op;
12027 dw_loc_descr_ref op0, op1;
12028 rtx inner = NULL_RTX;
12030 if (mode == VOIDmode)
12031 mode = GET_MODE (rtl);
12033 /* Note that for a dynamically sized array, the location we will generate a
12034 description of here will be the lowest numbered location which is
12035 actually within the array. That's *not* necessarily the same as the
12036 zeroth element of the array. */
12038 rtl = targetm.delegitimize_address (rtl);
12040 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12041 return NULL;
12043 switch (GET_CODE (rtl))
12045 case POST_INC:
12046 case POST_DEC:
12047 case POST_MODIFY:
12048 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12050 case SUBREG:
12051 /* The case of a subreg may arise when we have a local (register)
12052 variable or a formal (register) parameter which doesn't quite fill
12053 up an entire register. For now, just assume that it is
12054 legitimate to make the Dwarf info refer to the whole register which
12055 contains the given subreg. */
12056 if (!subreg_lowpart_p (rtl))
12057 break;
12058 inner = SUBREG_REG (rtl);
12059 case TRUNCATE:
12060 if (inner == NULL_RTX)
12061 inner = XEXP (rtl, 0);
12062 if (GET_MODE_CLASS (mode) == MODE_INT
12063 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12064 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12065 #ifdef POINTERS_EXTEND_UNSIGNED
12066 || (mode == Pmode && mem_mode != VOIDmode)
12067 #endif
12069 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12071 mem_loc_result = mem_loc_descriptor (inner,
12072 GET_MODE (inner),
12073 mem_mode, initialized);
12074 break;
12076 if (dwarf_strict)
12077 break;
12078 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12079 break;
12080 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12081 && (GET_MODE_CLASS (mode) != MODE_INT
12082 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12083 break;
12084 else
12086 dw_die_ref type_die;
12087 dw_loc_descr_ref cvt;
12089 mem_loc_result = mem_loc_descriptor (inner,
12090 GET_MODE (inner),
12091 mem_mode, initialized);
12092 if (mem_loc_result == NULL)
12093 break;
12094 type_die = base_type_for_mode (mode,
12095 GET_MODE_CLASS (mode) == MODE_INT);
12096 if (type_die == NULL)
12098 mem_loc_result = NULL;
12099 break;
12101 if (GET_MODE_SIZE (mode)
12102 != GET_MODE_SIZE (GET_MODE (inner)))
12103 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12104 else
12105 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12106 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12107 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12108 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12109 add_loc_descr (&mem_loc_result, cvt);
12111 break;
12113 case REG:
12114 if (GET_MODE_CLASS (mode) != MODE_INT
12115 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12116 && rtl != arg_pointer_rtx
12117 && rtl != frame_pointer_rtx
12118 #ifdef POINTERS_EXTEND_UNSIGNED
12119 && (mode != Pmode || mem_mode == VOIDmode)
12120 #endif
12123 dw_die_ref type_die;
12124 unsigned int dbx_regnum;
12126 if (dwarf_strict)
12127 break;
12128 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12129 break;
12130 type_die = base_type_for_mode (mode,
12131 GET_MODE_CLASS (mode) == MODE_INT);
12132 if (type_die == NULL)
12133 break;
12135 dbx_regnum = dbx_reg_number (rtl);
12136 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12137 break;
12138 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12139 dbx_regnum, 0);
12140 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12141 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12142 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12143 break;
12145 /* Whenever a register number forms a part of the description of the
12146 method for calculating the (dynamic) address of a memory resident
12147 object, DWARF rules require the register number be referred to as
12148 a "base register". This distinction is not based in any way upon
12149 what category of register the hardware believes the given register
12150 belongs to. This is strictly DWARF terminology we're dealing with
12151 here. Note that in cases where the location of a memory-resident
12152 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12153 OP_CONST (0)) the actual DWARF location descriptor that we generate
12154 may just be OP_BASEREG (basereg). This may look deceptively like
12155 the object in question was allocated to a register (rather than in
12156 memory) so DWARF consumers need to be aware of the subtle
12157 distinction between OP_REG and OP_BASEREG. */
12158 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12159 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12160 else if (stack_realign_drap
12161 && crtl->drap_reg
12162 && crtl->args.internal_arg_pointer == rtl
12163 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12165 /* If RTL is internal_arg_pointer, which has been optimized
12166 out, use DRAP instead. */
12167 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12168 VAR_INIT_STATUS_INITIALIZED);
12170 break;
12172 case SIGN_EXTEND:
12173 case ZERO_EXTEND:
12174 if (GET_MODE_CLASS (mode) != MODE_INT)
12175 break;
12176 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12177 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12178 if (op0 == 0)
12179 break;
12180 else if (GET_CODE (rtl) == ZERO_EXTEND
12181 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12182 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12183 < HOST_BITS_PER_WIDE_INT
12184 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12185 to expand zero extend as two shifts instead of
12186 masking. */
12187 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12189 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12190 mem_loc_result = op0;
12191 add_loc_descr (&mem_loc_result,
12192 int_loc_descriptor (GET_MODE_MASK (imode)));
12193 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12195 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12197 int shift = DWARF2_ADDR_SIZE
12198 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12199 shift *= BITS_PER_UNIT;
12200 if (GET_CODE (rtl) == SIGN_EXTEND)
12201 op = DW_OP_shra;
12202 else
12203 op = DW_OP_shr;
12204 mem_loc_result = op0;
12205 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12206 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12207 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12208 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12210 else if (!dwarf_strict)
12212 dw_die_ref type_die1, type_die2;
12213 dw_loc_descr_ref cvt;
12215 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12216 GET_CODE (rtl) == ZERO_EXTEND);
12217 if (type_die1 == NULL)
12218 break;
12219 type_die2 = base_type_for_mode (mode, 1);
12220 if (type_die2 == NULL)
12221 break;
12222 mem_loc_result = op0;
12223 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12224 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12225 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12226 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12227 add_loc_descr (&mem_loc_result, cvt);
12228 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12229 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12230 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12231 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12232 add_loc_descr (&mem_loc_result, cvt);
12234 break;
12236 case MEM:
12238 rtx new_rtl = avoid_constant_pool_reference (rtl);
12239 if (new_rtl != rtl)
12241 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12242 initialized);
12243 if (mem_loc_result != NULL)
12244 return mem_loc_result;
12247 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12248 get_address_mode (rtl), mode,
12249 VAR_INIT_STATUS_INITIALIZED);
12250 if (mem_loc_result == NULL)
12251 mem_loc_result = tls_mem_loc_descriptor (rtl);
12252 if (mem_loc_result != NULL)
12254 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12255 || GET_MODE_CLASS (mode) != MODE_INT)
12257 dw_die_ref type_die;
12258 dw_loc_descr_ref deref;
12260 if (dwarf_strict)
12261 return NULL;
12262 type_die
12263 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12264 if (type_die == NULL)
12265 return NULL;
12266 deref = new_loc_descr (DW_OP_GNU_deref_type,
12267 GET_MODE_SIZE (mode), 0);
12268 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12269 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12270 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12271 add_loc_descr (&mem_loc_result, deref);
12273 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12274 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12275 else
12276 add_loc_descr (&mem_loc_result,
12277 new_loc_descr (DW_OP_deref_size,
12278 GET_MODE_SIZE (mode), 0));
12280 break;
12282 case LO_SUM:
12283 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12285 case LABEL_REF:
12286 /* Some ports can transform a symbol ref into a label ref, because
12287 the symbol ref is too far away and has to be dumped into a constant
12288 pool. */
12289 case CONST:
12290 case SYMBOL_REF:
12291 if (GET_MODE_CLASS (mode) != MODE_INT
12292 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12293 #ifdef POINTERS_EXTEND_UNSIGNED
12294 && (mode != Pmode || mem_mode == VOIDmode)
12295 #endif
12297 break;
12298 if (GET_CODE (rtl) == SYMBOL_REF
12299 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12301 dw_loc_descr_ref temp;
12303 /* If this is not defined, we have no way to emit the data. */
12304 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12305 break;
12307 temp = new_addr_loc_descr (rtl, dtprel_true);
12309 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12310 add_loc_descr (&mem_loc_result, temp);
12312 break;
12315 if (!const_ok_for_output (rtl))
12316 break;
12318 symref:
12319 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12320 vec_safe_push (used_rtx_array, rtl);
12321 break;
12323 case CONCAT:
12324 case CONCATN:
12325 case VAR_LOCATION:
12326 case DEBUG_IMPLICIT_PTR:
12327 expansion_failed (NULL_TREE, rtl,
12328 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12329 return 0;
12331 case ENTRY_VALUE:
12332 if (dwarf_strict)
12333 return NULL;
12334 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12336 if (GET_MODE_CLASS (mode) != MODE_INT
12337 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12338 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12339 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12340 else
12342 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12343 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12344 return NULL;
12345 op0 = one_reg_loc_descriptor (dbx_regnum,
12346 VAR_INIT_STATUS_INITIALIZED);
12349 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12350 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12352 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12353 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12354 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12355 return NULL;
12357 else
12358 gcc_unreachable ();
12359 if (op0 == NULL)
12360 return NULL;
12361 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12362 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12363 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12364 break;
12366 case DEBUG_PARAMETER_REF:
12367 mem_loc_result = parameter_ref_descriptor (rtl);
12368 break;
12370 case PRE_MODIFY:
12371 /* Extract the PLUS expression nested inside and fall into
12372 PLUS code below. */
12373 rtl = XEXP (rtl, 1);
12374 goto plus;
12376 case PRE_INC:
12377 case PRE_DEC:
12378 /* Turn these into a PLUS expression and fall into the PLUS code
12379 below. */
12380 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12381 GEN_INT (GET_CODE (rtl) == PRE_INC
12382 ? GET_MODE_UNIT_SIZE (mem_mode)
12383 : -GET_MODE_UNIT_SIZE (mem_mode)));
12385 /* ... fall through ... */
12387 case PLUS:
12388 plus:
12389 if (is_based_loc (rtl)
12390 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12391 || XEXP (rtl, 0) == arg_pointer_rtx
12392 || XEXP (rtl, 0) == frame_pointer_rtx)
12393 && GET_MODE_CLASS (mode) == MODE_INT)
12394 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12395 INTVAL (XEXP (rtl, 1)),
12396 VAR_INIT_STATUS_INITIALIZED);
12397 else
12399 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12400 VAR_INIT_STATUS_INITIALIZED);
12401 if (mem_loc_result == 0)
12402 break;
12404 if (CONST_INT_P (XEXP (rtl, 1))
12405 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12406 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12407 else
12409 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12410 VAR_INIT_STATUS_INITIALIZED);
12411 if (op1 == 0)
12412 break;
12413 add_loc_descr (&mem_loc_result, op1);
12414 add_loc_descr (&mem_loc_result,
12415 new_loc_descr (DW_OP_plus, 0, 0));
12418 break;
12420 /* If a pseudo-reg is optimized away, it is possible for it to
12421 be replaced with a MEM containing a multiply or shift. */
12422 case MINUS:
12423 op = DW_OP_minus;
12424 goto do_binop;
12426 case MULT:
12427 op = DW_OP_mul;
12428 goto do_binop;
12430 case DIV:
12431 if (!dwarf_strict
12432 && GET_MODE_CLASS (mode) == MODE_INT
12433 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12435 mem_loc_result = typed_binop (DW_OP_div, rtl,
12436 base_type_for_mode (mode, 0),
12437 mode, mem_mode);
12438 break;
12440 op = DW_OP_div;
12441 goto do_binop;
12443 case UMOD:
12444 op = DW_OP_mod;
12445 goto do_binop;
12447 case ASHIFT:
12448 op = DW_OP_shl;
12449 goto do_shift;
12451 case ASHIFTRT:
12452 op = DW_OP_shra;
12453 goto do_shift;
12455 case LSHIFTRT:
12456 op = DW_OP_shr;
12457 goto do_shift;
12459 do_shift:
12460 if (GET_MODE_CLASS (mode) != MODE_INT)
12461 break;
12462 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12463 VAR_INIT_STATUS_INITIALIZED);
12465 rtx rtlop1 = XEXP (rtl, 1);
12466 if (GET_MODE (rtlop1) != VOIDmode
12467 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12468 < GET_MODE_BITSIZE (mode))
12469 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12470 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12471 VAR_INIT_STATUS_INITIALIZED);
12474 if (op0 == 0 || op1 == 0)
12475 break;
12477 mem_loc_result = op0;
12478 add_loc_descr (&mem_loc_result, op1);
12479 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12480 break;
12482 case AND:
12483 op = DW_OP_and;
12484 goto do_binop;
12486 case IOR:
12487 op = DW_OP_or;
12488 goto do_binop;
12490 case XOR:
12491 op = DW_OP_xor;
12492 goto do_binop;
12494 do_binop:
12495 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12496 VAR_INIT_STATUS_INITIALIZED);
12497 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12498 VAR_INIT_STATUS_INITIALIZED);
12500 if (op0 == 0 || op1 == 0)
12501 break;
12503 mem_loc_result = op0;
12504 add_loc_descr (&mem_loc_result, op1);
12505 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12506 break;
12508 case MOD:
12509 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12511 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12512 base_type_for_mode (mode, 0),
12513 mode, mem_mode);
12514 break;
12517 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12518 VAR_INIT_STATUS_INITIALIZED);
12519 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12520 VAR_INIT_STATUS_INITIALIZED);
12522 if (op0 == 0 || op1 == 0)
12523 break;
12525 mem_loc_result = op0;
12526 add_loc_descr (&mem_loc_result, op1);
12527 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12528 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12529 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12530 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12531 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12532 break;
12534 case UDIV:
12535 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12537 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12539 op = DW_OP_div;
12540 goto do_binop;
12542 mem_loc_result = typed_binop (DW_OP_div, rtl,
12543 base_type_for_mode (mode, 1),
12544 mode, mem_mode);
12546 break;
12548 case NOT:
12549 op = DW_OP_not;
12550 goto do_unop;
12552 case ABS:
12553 op = DW_OP_abs;
12554 goto do_unop;
12556 case NEG:
12557 op = DW_OP_neg;
12558 goto do_unop;
12560 do_unop:
12561 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12562 VAR_INIT_STATUS_INITIALIZED);
12564 if (op0 == 0)
12565 break;
12567 mem_loc_result = op0;
12568 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12569 break;
12571 case CONST_INT:
12572 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12573 #ifdef POINTERS_EXTEND_UNSIGNED
12574 || (mode == Pmode
12575 && mem_mode != VOIDmode
12576 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12577 #endif
12580 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12581 break;
12583 if (!dwarf_strict
12584 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12585 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12587 dw_die_ref type_die = base_type_for_mode (mode, 1);
12588 enum machine_mode amode;
12589 if (type_die == NULL)
12590 return NULL;
12591 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12592 MODE_INT, 0);
12593 if (INTVAL (rtl) >= 0
12594 && amode != BLKmode
12595 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12596 /* const DW_OP_GNU_convert <XXX> vs.
12597 DW_OP_GNU_const_type <XXX, 1, const>. */
12598 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12599 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12601 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12602 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12603 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12604 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12605 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12606 add_loc_descr (&mem_loc_result, op0);
12607 return mem_loc_result;
12609 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12610 INTVAL (rtl));
12611 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12612 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12613 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12614 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12615 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12616 else
12618 mem_loc_result->dw_loc_oprnd2.val_class
12619 = dw_val_class_const_double;
12620 mem_loc_result->dw_loc_oprnd2.v.val_double
12621 = double_int::from_shwi (INTVAL (rtl));
12624 break;
12626 case CONST_DOUBLE:
12627 if (!dwarf_strict)
12629 dw_die_ref type_die;
12631 /* Note that a CONST_DOUBLE rtx could represent either an integer
12632 or a floating-point constant. A CONST_DOUBLE is used whenever
12633 the constant requires more than one word in order to be
12634 adequately represented. We output CONST_DOUBLEs as blocks. */
12635 if (mode == VOIDmode
12636 || (GET_MODE (rtl) == VOIDmode
12637 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12638 break;
12639 type_die = base_type_for_mode (mode,
12640 GET_MODE_CLASS (mode) == MODE_INT);
12641 if (type_die == NULL)
12642 return NULL;
12643 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12644 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12645 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12646 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12647 if (SCALAR_FLOAT_MODE_P (mode))
12649 unsigned int length = GET_MODE_SIZE (mode);
12650 unsigned char *array
12651 = (unsigned char*) ggc_alloc_atomic (length);
12653 insert_float (rtl, array);
12654 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12655 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12656 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12657 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12659 else
12661 mem_loc_result->dw_loc_oprnd2.val_class
12662 = dw_val_class_const_double;
12663 mem_loc_result->dw_loc_oprnd2.v.val_double
12664 = rtx_to_double_int (rtl);
12667 break;
12669 case EQ:
12670 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12671 break;
12673 case GE:
12674 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12675 break;
12677 case GT:
12678 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12679 break;
12681 case LE:
12682 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12683 break;
12685 case LT:
12686 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12687 break;
12689 case NE:
12690 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12691 break;
12693 case GEU:
12694 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12695 break;
12697 case GTU:
12698 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12699 break;
12701 case LEU:
12702 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12703 break;
12705 case LTU:
12706 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12707 break;
12709 case UMIN:
12710 case UMAX:
12711 if (GET_MODE_CLASS (mode) != MODE_INT)
12712 break;
12713 /* FALLTHRU */
12714 case SMIN:
12715 case SMAX:
12716 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12717 break;
12719 case ZERO_EXTRACT:
12720 case SIGN_EXTRACT:
12721 if (CONST_INT_P (XEXP (rtl, 1))
12722 && CONST_INT_P (XEXP (rtl, 2))
12723 && ((unsigned) INTVAL (XEXP (rtl, 1))
12724 + (unsigned) INTVAL (XEXP (rtl, 2))
12725 <= GET_MODE_BITSIZE (mode))
12726 && GET_MODE_CLASS (mode) == MODE_INT
12727 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12728 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12730 int shift, size;
12731 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12732 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12733 if (op0 == 0)
12734 break;
12735 if (GET_CODE (rtl) == SIGN_EXTRACT)
12736 op = DW_OP_shra;
12737 else
12738 op = DW_OP_shr;
12739 mem_loc_result = op0;
12740 size = INTVAL (XEXP (rtl, 1));
12741 shift = INTVAL (XEXP (rtl, 2));
12742 if (BITS_BIG_ENDIAN)
12743 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12744 - shift - size;
12745 if (shift + size != (int) DWARF2_ADDR_SIZE)
12747 add_loc_descr (&mem_loc_result,
12748 int_loc_descriptor (DWARF2_ADDR_SIZE
12749 - shift - size));
12750 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12752 if (size != (int) DWARF2_ADDR_SIZE)
12754 add_loc_descr (&mem_loc_result,
12755 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12756 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12759 break;
12761 case IF_THEN_ELSE:
12763 dw_loc_descr_ref op2, bra_node, drop_node;
12764 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12765 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12766 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12767 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12768 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12769 VAR_INIT_STATUS_INITIALIZED);
12770 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12771 VAR_INIT_STATUS_INITIALIZED);
12772 if (op0 == NULL || op1 == NULL || op2 == NULL)
12773 break;
12775 mem_loc_result = op1;
12776 add_loc_descr (&mem_loc_result, op2);
12777 add_loc_descr (&mem_loc_result, op0);
12778 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12779 add_loc_descr (&mem_loc_result, bra_node);
12780 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12781 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12782 add_loc_descr (&mem_loc_result, drop_node);
12783 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12784 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12786 break;
12788 case FLOAT_EXTEND:
12789 case FLOAT_TRUNCATE:
12790 case FLOAT:
12791 case UNSIGNED_FLOAT:
12792 case FIX:
12793 case UNSIGNED_FIX:
12794 if (!dwarf_strict)
12796 dw_die_ref type_die;
12797 dw_loc_descr_ref cvt;
12799 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12800 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12801 if (op0 == NULL)
12802 break;
12803 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12804 && (GET_CODE (rtl) == FLOAT
12805 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12806 <= DWARF2_ADDR_SIZE))
12808 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12809 GET_CODE (rtl) == UNSIGNED_FLOAT);
12810 if (type_die == NULL)
12811 break;
12812 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12813 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12814 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12815 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12816 add_loc_descr (&op0, cvt);
12818 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12819 if (type_die == NULL)
12820 break;
12821 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12822 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12823 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12824 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12825 add_loc_descr (&op0, cvt);
12826 if (GET_MODE_CLASS (mode) == MODE_INT
12827 && (GET_CODE (rtl) == FIX
12828 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12830 op0 = convert_descriptor_to_mode (mode, op0);
12831 if (op0 == NULL)
12832 break;
12834 mem_loc_result = op0;
12836 break;
12838 case CLZ:
12839 case CTZ:
12840 case FFS:
12841 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12842 break;
12844 case POPCOUNT:
12845 case PARITY:
12846 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12847 break;
12849 case BSWAP:
12850 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12851 break;
12853 case ROTATE:
12854 case ROTATERT:
12855 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12856 break;
12858 case COMPARE:
12859 /* In theory, we could implement the above. */
12860 /* DWARF cannot represent the unsigned compare operations
12861 natively. */
12862 case SS_MULT:
12863 case US_MULT:
12864 case SS_DIV:
12865 case US_DIV:
12866 case SS_PLUS:
12867 case US_PLUS:
12868 case SS_MINUS:
12869 case US_MINUS:
12870 case SS_NEG:
12871 case US_NEG:
12872 case SS_ABS:
12873 case SS_ASHIFT:
12874 case US_ASHIFT:
12875 case SS_TRUNCATE:
12876 case US_TRUNCATE:
12877 case UNORDERED:
12878 case ORDERED:
12879 case UNEQ:
12880 case UNGE:
12881 case UNGT:
12882 case UNLE:
12883 case UNLT:
12884 case LTGT:
12885 case FRACT_CONVERT:
12886 case UNSIGNED_FRACT_CONVERT:
12887 case SAT_FRACT:
12888 case UNSIGNED_SAT_FRACT:
12889 case SQRT:
12890 case ASM_OPERANDS:
12891 case VEC_MERGE:
12892 case VEC_SELECT:
12893 case VEC_CONCAT:
12894 case VEC_DUPLICATE:
12895 case UNSPEC:
12896 case HIGH:
12897 case FMA:
12898 case STRICT_LOW_PART:
12899 case CONST_VECTOR:
12900 case CONST_FIXED:
12901 case CLRSB:
12902 case CLOBBER:
12903 /* If delegitimize_address couldn't do anything with the UNSPEC, we
12904 can't express it in the debug info. This can happen e.g. with some
12905 TLS UNSPECs. */
12906 break;
12908 case CONST_STRING:
12909 resolve_one_addr (&rtl, NULL);
12910 goto symref;
12912 default:
12913 #ifdef ENABLE_CHECKING
12914 print_rtl (stderr, rtl);
12915 gcc_unreachable ();
12916 #else
12917 break;
12918 #endif
12921 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12922 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12924 return mem_loc_result;
12927 /* Return a descriptor that describes the concatenation of two locations.
12928 This is typically a complex variable. */
12930 static dw_loc_descr_ref
12931 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12933 dw_loc_descr_ref cc_loc_result = NULL;
12934 dw_loc_descr_ref x0_ref
12935 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12936 dw_loc_descr_ref x1_ref
12937 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12939 if (x0_ref == 0 || x1_ref == 0)
12940 return 0;
12942 cc_loc_result = x0_ref;
12943 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12945 add_loc_descr (&cc_loc_result, x1_ref);
12946 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12948 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12949 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12951 return cc_loc_result;
12954 /* Return a descriptor that describes the concatenation of N
12955 locations. */
12957 static dw_loc_descr_ref
12958 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12960 unsigned int i;
12961 dw_loc_descr_ref cc_loc_result = NULL;
12962 unsigned int n = XVECLEN (concatn, 0);
12964 for (i = 0; i < n; ++i)
12966 dw_loc_descr_ref ref;
12967 rtx x = XVECEXP (concatn, 0, i);
12969 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12970 if (ref == NULL)
12971 return NULL;
12973 add_loc_descr (&cc_loc_result, ref);
12974 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12977 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12978 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12980 return cc_loc_result;
12983 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
12984 for DEBUG_IMPLICIT_PTR RTL. */
12986 static dw_loc_descr_ref
12987 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12989 dw_loc_descr_ref ret;
12990 dw_die_ref ref;
12992 if (dwarf_strict)
12993 return NULL;
12994 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12995 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12996 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12997 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12998 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12999 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13000 if (ref)
13002 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13003 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13004 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13006 else
13008 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13009 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13011 return ret;
13014 /* Output a proper Dwarf location descriptor for a variable or parameter
13015 which is either allocated in a register or in a memory location. For a
13016 register, we just generate an OP_REG and the register number. For a
13017 memory location we provide a Dwarf postfix expression describing how to
13018 generate the (dynamic) address of the object onto the address stack.
13020 MODE is mode of the decl if this loc_descriptor is going to be used in
13021 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13022 allowed, VOIDmode otherwise.
13024 If we don't know how to describe it, return 0. */
13026 static dw_loc_descr_ref
13027 loc_descriptor (rtx rtl, enum machine_mode mode,
13028 enum var_init_status initialized)
13030 dw_loc_descr_ref loc_result = NULL;
13032 switch (GET_CODE (rtl))
13034 case SUBREG:
13035 /* The case of a subreg may arise when we have a local (register)
13036 variable or a formal (register) parameter which doesn't quite fill
13037 up an entire register. For now, just assume that it is
13038 legitimate to make the Dwarf info refer to the whole register which
13039 contains the given subreg. */
13040 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13041 loc_result = loc_descriptor (SUBREG_REG (rtl),
13042 GET_MODE (SUBREG_REG (rtl)), initialized);
13043 else
13044 goto do_default;
13045 break;
13047 case REG:
13048 loc_result = reg_loc_descriptor (rtl, initialized);
13049 break;
13051 case MEM:
13052 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13053 GET_MODE (rtl), initialized);
13054 if (loc_result == NULL)
13055 loc_result = tls_mem_loc_descriptor (rtl);
13056 if (loc_result == NULL)
13058 rtx new_rtl = avoid_constant_pool_reference (rtl);
13059 if (new_rtl != rtl)
13060 loc_result = loc_descriptor (new_rtl, mode, initialized);
13062 break;
13064 case CONCAT:
13065 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13066 initialized);
13067 break;
13069 case CONCATN:
13070 loc_result = concatn_loc_descriptor (rtl, initialized);
13071 break;
13073 case VAR_LOCATION:
13074 /* Single part. */
13075 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13077 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13078 if (GET_CODE (loc) == EXPR_LIST)
13079 loc = XEXP (loc, 0);
13080 loc_result = loc_descriptor (loc, mode, initialized);
13081 break;
13084 rtl = XEXP (rtl, 1);
13085 /* FALLTHRU */
13087 case PARALLEL:
13089 rtvec par_elems = XVEC (rtl, 0);
13090 int num_elem = GET_NUM_ELEM (par_elems);
13091 enum machine_mode mode;
13092 int i;
13094 /* Create the first one, so we have something to add to. */
13095 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13096 VOIDmode, initialized);
13097 if (loc_result == NULL)
13098 return NULL;
13099 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13100 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13101 for (i = 1; i < num_elem; i++)
13103 dw_loc_descr_ref temp;
13105 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13106 VOIDmode, initialized);
13107 if (temp == NULL)
13108 return NULL;
13109 add_loc_descr (&loc_result, temp);
13110 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13111 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13114 break;
13116 case CONST_INT:
13117 if (mode != VOIDmode && mode != BLKmode)
13118 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13119 INTVAL (rtl));
13120 break;
13122 case CONST_DOUBLE:
13123 if (mode == VOIDmode)
13124 mode = GET_MODE (rtl);
13126 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13128 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13130 /* Note that a CONST_DOUBLE rtx could represent either an integer
13131 or a floating-point constant. A CONST_DOUBLE is used whenever
13132 the constant requires more than one word in order to be
13133 adequately represented. We output CONST_DOUBLEs as blocks. */
13134 loc_result = new_loc_descr (DW_OP_implicit_value,
13135 GET_MODE_SIZE (mode), 0);
13136 if (SCALAR_FLOAT_MODE_P (mode))
13138 unsigned int length = GET_MODE_SIZE (mode);
13139 unsigned char *array
13140 = (unsigned char*) ggc_alloc_atomic (length);
13142 insert_float (rtl, array);
13143 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13144 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13145 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13146 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13148 else
13150 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13151 loc_result->dw_loc_oprnd2.v.val_double
13152 = rtx_to_double_int (rtl);
13155 break;
13157 case CONST_VECTOR:
13158 if (mode == VOIDmode)
13159 mode = GET_MODE (rtl);
13161 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13163 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13164 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13165 unsigned char *array = (unsigned char *)
13166 ggc_alloc_atomic (length * elt_size);
13167 unsigned int i;
13168 unsigned char *p;
13170 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13171 switch (GET_MODE_CLASS (mode))
13173 case MODE_VECTOR_INT:
13174 for (i = 0, p = array; i < length; i++, p += elt_size)
13176 rtx elt = CONST_VECTOR_ELT (rtl, i);
13177 double_int val = rtx_to_double_int (elt);
13179 if (elt_size <= sizeof (HOST_WIDE_INT))
13180 insert_int (val.to_shwi (), elt_size, p);
13181 else
13183 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13184 insert_double (val, p);
13187 break;
13189 case MODE_VECTOR_FLOAT:
13190 for (i = 0, p = array; i < length; i++, p += elt_size)
13192 rtx elt = CONST_VECTOR_ELT (rtl, i);
13193 insert_float (elt, p);
13195 break;
13197 default:
13198 gcc_unreachable ();
13201 loc_result = new_loc_descr (DW_OP_implicit_value,
13202 length * elt_size, 0);
13203 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13204 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13205 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13206 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13208 break;
13210 case CONST:
13211 if (mode == VOIDmode
13212 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13213 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13214 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13216 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13217 break;
13219 /* FALLTHROUGH */
13220 case SYMBOL_REF:
13221 if (!const_ok_for_output (rtl))
13222 break;
13223 case LABEL_REF:
13224 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13225 && (dwarf_version >= 4 || !dwarf_strict))
13227 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13228 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13229 vec_safe_push (used_rtx_array, rtl);
13231 break;
13233 case DEBUG_IMPLICIT_PTR:
13234 loc_result = implicit_ptr_descriptor (rtl, 0);
13235 break;
13237 case PLUS:
13238 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13239 && CONST_INT_P (XEXP (rtl, 1)))
13241 loc_result
13242 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13243 break;
13245 /* FALLTHRU */
13246 do_default:
13247 default:
13248 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13249 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13250 && dwarf_version >= 4)
13251 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13253 /* Value expression. */
13254 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13255 if (loc_result)
13256 add_loc_descr (&loc_result,
13257 new_loc_descr (DW_OP_stack_value, 0, 0));
13259 break;
13262 return loc_result;
13265 /* We need to figure out what section we should use as the base for the
13266 address ranges where a given location is valid.
13267 1. If this particular DECL has a section associated with it, use that.
13268 2. If this function has a section associated with it, use that.
13269 3. Otherwise, use the text section.
13270 XXX: If you split a variable across multiple sections, we won't notice. */
13272 static const char *
13273 secname_for_decl (const_tree decl)
13275 const char *secname;
13277 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13279 tree sectree = DECL_SECTION_NAME (decl);
13280 secname = TREE_STRING_POINTER (sectree);
13282 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13284 tree sectree = DECL_SECTION_NAME (current_function_decl);
13285 secname = TREE_STRING_POINTER (sectree);
13287 else if (cfun && in_cold_section_p)
13288 secname = crtl->subsections.cold_section_label;
13289 else
13290 secname = text_section_label;
13292 return secname;
13295 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13297 static bool
13298 decl_by_reference_p (tree decl)
13300 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13301 || TREE_CODE (decl) == VAR_DECL)
13302 && DECL_BY_REFERENCE (decl));
13305 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13306 for VARLOC. */
13308 static dw_loc_descr_ref
13309 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13310 enum var_init_status initialized)
13312 int have_address = 0;
13313 dw_loc_descr_ref descr;
13314 enum machine_mode mode;
13316 if (want_address != 2)
13318 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13319 /* Single part. */
13320 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13322 varloc = PAT_VAR_LOCATION_LOC (varloc);
13323 if (GET_CODE (varloc) == EXPR_LIST)
13324 varloc = XEXP (varloc, 0);
13325 mode = GET_MODE (varloc);
13326 if (MEM_P (varloc))
13328 rtx addr = XEXP (varloc, 0);
13329 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13330 mode, initialized);
13331 if (descr)
13332 have_address = 1;
13333 else
13335 rtx x = avoid_constant_pool_reference (varloc);
13336 if (x != varloc)
13337 descr = mem_loc_descriptor (x, mode, VOIDmode,
13338 initialized);
13341 else
13342 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13344 else
13345 return 0;
13347 else
13349 if (GET_CODE (varloc) == VAR_LOCATION)
13350 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13351 else
13352 mode = DECL_MODE (loc);
13353 descr = loc_descriptor (varloc, mode, initialized);
13354 have_address = 1;
13357 if (!descr)
13358 return 0;
13360 if (want_address == 2 && !have_address
13361 && (dwarf_version >= 4 || !dwarf_strict))
13363 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13365 expansion_failed (loc, NULL_RTX,
13366 "DWARF address size mismatch");
13367 return 0;
13369 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13370 have_address = 1;
13372 /* Show if we can't fill the request for an address. */
13373 if (want_address && !have_address)
13375 expansion_failed (loc, NULL_RTX,
13376 "Want address and only have value");
13377 return 0;
13380 /* If we've got an address and don't want one, dereference. */
13381 if (!want_address && have_address)
13383 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13384 enum dwarf_location_atom op;
13386 if (size > DWARF2_ADDR_SIZE || size == -1)
13388 expansion_failed (loc, NULL_RTX,
13389 "DWARF address size mismatch");
13390 return 0;
13392 else if (size == DWARF2_ADDR_SIZE)
13393 op = DW_OP_deref;
13394 else
13395 op = DW_OP_deref_size;
13397 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13400 return descr;
13403 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13404 if it is not possible. */
13406 static dw_loc_descr_ref
13407 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13409 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13410 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13411 else if (dwarf_version >= 3 || !dwarf_strict)
13412 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13413 else
13414 return NULL;
13417 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13418 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13420 static dw_loc_descr_ref
13421 dw_sra_loc_expr (tree decl, rtx loc)
13423 rtx p;
13424 unsigned int padsize = 0;
13425 dw_loc_descr_ref descr, *descr_tail;
13426 unsigned HOST_WIDE_INT decl_size;
13427 rtx varloc;
13428 enum var_init_status initialized;
13430 if (DECL_SIZE (decl) == NULL
13431 || !host_integerp (DECL_SIZE (decl), 1))
13432 return NULL;
13434 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13435 descr = NULL;
13436 descr_tail = &descr;
13438 for (p = loc; p; p = XEXP (p, 1))
13440 unsigned int bitsize = decl_piece_bitsize (p);
13441 rtx loc_note = *decl_piece_varloc_ptr (p);
13442 dw_loc_descr_ref cur_descr;
13443 dw_loc_descr_ref *tail, last = NULL;
13444 unsigned int opsize = 0;
13446 if (loc_note == NULL_RTX
13447 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13449 padsize += bitsize;
13450 continue;
13452 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13453 varloc = NOTE_VAR_LOCATION (loc_note);
13454 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13455 if (cur_descr == NULL)
13457 padsize += bitsize;
13458 continue;
13461 /* Check that cur_descr either doesn't use
13462 DW_OP_*piece operations, or their sum is equal
13463 to bitsize. Otherwise we can't embed it. */
13464 for (tail = &cur_descr; *tail != NULL;
13465 tail = &(*tail)->dw_loc_next)
13466 if ((*tail)->dw_loc_opc == DW_OP_piece)
13468 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13469 * BITS_PER_UNIT;
13470 last = *tail;
13472 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13474 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13475 last = *tail;
13478 if (last != NULL && opsize != bitsize)
13480 padsize += bitsize;
13481 continue;
13484 /* If there is a hole, add DW_OP_*piece after empty DWARF
13485 expression, which means that those bits are optimized out. */
13486 if (padsize)
13488 if (padsize > decl_size)
13489 return NULL;
13490 decl_size -= padsize;
13491 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13492 if (*descr_tail == NULL)
13493 return NULL;
13494 descr_tail = &(*descr_tail)->dw_loc_next;
13495 padsize = 0;
13497 *descr_tail = cur_descr;
13498 descr_tail = tail;
13499 if (bitsize > decl_size)
13500 return NULL;
13501 decl_size -= bitsize;
13502 if (last == NULL)
13504 HOST_WIDE_INT offset = 0;
13505 if (GET_CODE (varloc) == VAR_LOCATION
13506 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13508 varloc = PAT_VAR_LOCATION_LOC (varloc);
13509 if (GET_CODE (varloc) == EXPR_LIST)
13510 varloc = XEXP (varloc, 0);
13514 if (GET_CODE (varloc) == CONST
13515 || GET_CODE (varloc) == SIGN_EXTEND
13516 || GET_CODE (varloc) == ZERO_EXTEND)
13517 varloc = XEXP (varloc, 0);
13518 else if (GET_CODE (varloc) == SUBREG)
13519 varloc = SUBREG_REG (varloc);
13520 else
13521 break;
13523 while (1);
13524 /* DW_OP_bit_size offset should be zero for register
13525 or implicit location descriptions and empty location
13526 descriptions, but for memory addresses needs big endian
13527 adjustment. */
13528 if (MEM_P (varloc))
13530 unsigned HOST_WIDE_INT memsize
13531 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13532 if (memsize != bitsize)
13534 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13535 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13536 return NULL;
13537 if (memsize < bitsize)
13538 return NULL;
13539 if (BITS_BIG_ENDIAN)
13540 offset = memsize - bitsize;
13544 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13545 if (*descr_tail == NULL)
13546 return NULL;
13547 descr_tail = &(*descr_tail)->dw_loc_next;
13551 /* If there were any non-empty expressions, add padding till the end of
13552 the decl. */
13553 if (descr != NULL && decl_size != 0)
13555 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13556 if (*descr_tail == NULL)
13557 return NULL;
13559 return descr;
13562 /* Return the dwarf representation of the location list LOC_LIST of
13563 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13564 function. */
13566 static dw_loc_list_ref
13567 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13569 const char *endname, *secname;
13570 rtx varloc;
13571 enum var_init_status initialized;
13572 struct var_loc_node *node;
13573 dw_loc_descr_ref descr;
13574 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13575 dw_loc_list_ref list = NULL;
13576 dw_loc_list_ref *listp = &list;
13578 /* Now that we know what section we are using for a base,
13579 actually construct the list of locations.
13580 The first location information is what is passed to the
13581 function that creates the location list, and the remaining
13582 locations just get added on to that list.
13583 Note that we only know the start address for a location
13584 (IE location changes), so to build the range, we use
13585 the range [current location start, next location start].
13586 This means we have to special case the last node, and generate
13587 a range of [last location start, end of function label]. */
13589 secname = secname_for_decl (decl);
13591 for (node = loc_list->first; node; node = node->next)
13592 if (GET_CODE (node->loc) == EXPR_LIST
13593 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13595 if (GET_CODE (node->loc) == EXPR_LIST)
13597 /* This requires DW_OP_{,bit_}piece, which is not usable
13598 inside DWARF expressions. */
13599 if (want_address != 2)
13600 continue;
13601 descr = dw_sra_loc_expr (decl, node->loc);
13602 if (descr == NULL)
13603 continue;
13605 else
13607 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13608 varloc = NOTE_VAR_LOCATION (node->loc);
13609 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13611 if (descr)
13613 bool range_across_switch = false;
13614 /* If section switch happens in between node->label
13615 and node->next->label (or end of function) and
13616 we can't emit it as a single entry list,
13617 emit two ranges, first one ending at the end
13618 of first partition and second one starting at the
13619 beginning of second partition. */
13620 if (node == loc_list->last_before_switch
13621 && (node != loc_list->first || loc_list->first->next)
13622 && current_function_decl)
13624 endname = cfun->fde->dw_fde_end;
13625 range_across_switch = true;
13627 /* The variable has a location between NODE->LABEL and
13628 NODE->NEXT->LABEL. */
13629 else if (node->next)
13630 endname = node->next->label;
13631 /* If the variable has a location at the last label
13632 it keeps its location until the end of function. */
13633 else if (!current_function_decl)
13634 endname = text_end_label;
13635 else
13637 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13638 current_function_funcdef_no);
13639 endname = ggc_strdup (label_id);
13642 *listp = new_loc_list (descr, node->label, endname, secname);
13643 if (TREE_CODE (decl) == PARM_DECL
13644 && node == loc_list->first
13645 && NOTE_P (node->loc)
13646 && strcmp (node->label, endname) == 0)
13647 (*listp)->force = true;
13648 listp = &(*listp)->dw_loc_next;
13650 if (range_across_switch)
13652 if (GET_CODE (node->loc) == EXPR_LIST)
13653 descr = dw_sra_loc_expr (decl, node->loc);
13654 else
13656 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13657 varloc = NOTE_VAR_LOCATION (node->loc);
13658 descr = dw_loc_list_1 (decl, varloc, want_address,
13659 initialized);
13661 gcc_assert (descr);
13662 /* The variable has a location between NODE->LABEL and
13663 NODE->NEXT->LABEL. */
13664 if (node->next)
13665 endname = node->next->label;
13666 else
13667 endname = cfun->fde->dw_fde_second_end;
13668 *listp = new_loc_list (descr,
13669 cfun->fde->dw_fde_second_begin,
13670 endname, secname);
13671 listp = &(*listp)->dw_loc_next;
13676 /* Try to avoid the overhead of a location list emitting a location
13677 expression instead, but only if we didn't have more than one
13678 location entry in the first place. If some entries were not
13679 representable, we don't want to pretend a single entry that was
13680 applies to the entire scope in which the variable is
13681 available. */
13682 if (list && loc_list->first->next)
13683 gen_llsym (list);
13685 return list;
13688 /* Return if the loc_list has only single element and thus can be represented
13689 as location description. */
13691 static bool
13692 single_element_loc_list_p (dw_loc_list_ref list)
13694 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13695 return !list->ll_symbol;
13698 /* To each location in list LIST add loc descr REF. */
13700 static void
13701 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13703 dw_loc_descr_ref copy;
13704 add_loc_descr (&list->expr, ref);
13705 list = list->dw_loc_next;
13706 while (list)
13708 copy = ggc_alloc_dw_loc_descr_node ();
13709 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13710 add_loc_descr (&list->expr, copy);
13711 while (copy->dw_loc_next)
13713 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13714 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13715 copy->dw_loc_next = new_copy;
13716 copy = new_copy;
13718 list = list->dw_loc_next;
13722 /* Given two lists RET and LIST
13723 produce location list that is result of adding expression in LIST
13724 to expression in RET on each position in program.
13725 Might be destructive on both RET and LIST.
13727 TODO: We handle only simple cases of RET or LIST having at most one
13728 element. General case would inolve sorting the lists in program order
13729 and merging them that will need some additional work.
13730 Adding that will improve quality of debug info especially for SRA-ed
13731 structures. */
13733 static void
13734 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13736 if (!list)
13737 return;
13738 if (!*ret)
13740 *ret = list;
13741 return;
13743 if (!list->dw_loc_next)
13745 add_loc_descr_to_each (*ret, list->expr);
13746 return;
13748 if (!(*ret)->dw_loc_next)
13750 add_loc_descr_to_each (list, (*ret)->expr);
13751 *ret = list;
13752 return;
13754 expansion_failed (NULL_TREE, NULL_RTX,
13755 "Don't know how to merge two non-trivial"
13756 " location lists.\n");
13757 *ret = NULL;
13758 return;
13761 /* LOC is constant expression. Try a luck, look it up in constant
13762 pool and return its loc_descr of its address. */
13764 static dw_loc_descr_ref
13765 cst_pool_loc_descr (tree loc)
13767 /* Get an RTL for this, if something has been emitted. */
13768 rtx rtl = lookup_constant_def (loc);
13770 if (!rtl || !MEM_P (rtl))
13772 gcc_assert (!rtl);
13773 return 0;
13775 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13777 /* TODO: We might get more coverage if we was actually delaying expansion
13778 of all expressions till end of compilation when constant pools are fully
13779 populated. */
13780 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13782 expansion_failed (loc, NULL_RTX,
13783 "CST value in contant pool but not marked.");
13784 return 0;
13786 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13787 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13790 /* Return dw_loc_list representing address of addr_expr LOC
13791 by looking for inner INDIRECT_REF expression and turning
13792 it into simple arithmetics. */
13794 static dw_loc_list_ref
13795 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13797 tree obj, offset;
13798 HOST_WIDE_INT bitsize, bitpos, bytepos;
13799 enum machine_mode mode;
13800 int unsignedp, volatilep = 0;
13801 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13803 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13804 &bitsize, &bitpos, &offset, &mode,
13805 &unsignedp, &volatilep, false);
13806 STRIP_NOPS (obj);
13807 if (bitpos % BITS_PER_UNIT)
13809 expansion_failed (loc, NULL_RTX, "bitfield access");
13810 return 0;
13812 if (!INDIRECT_REF_P (obj))
13814 expansion_failed (obj,
13815 NULL_RTX, "no indirect ref in inner refrence");
13816 return 0;
13818 if (!offset && !bitpos)
13819 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13820 else if (toplev
13821 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13822 && (dwarf_version >= 4 || !dwarf_strict))
13824 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13825 if (!list_ret)
13826 return 0;
13827 if (offset)
13829 /* Variable offset. */
13830 list_ret1 = loc_list_from_tree (offset, 0);
13831 if (list_ret1 == 0)
13832 return 0;
13833 add_loc_list (&list_ret, list_ret1);
13834 if (!list_ret)
13835 return 0;
13836 add_loc_descr_to_each (list_ret,
13837 new_loc_descr (DW_OP_plus, 0, 0));
13839 bytepos = bitpos / BITS_PER_UNIT;
13840 if (bytepos > 0)
13841 add_loc_descr_to_each (list_ret,
13842 new_loc_descr (DW_OP_plus_uconst,
13843 bytepos, 0));
13844 else if (bytepos < 0)
13845 loc_list_plus_const (list_ret, bytepos);
13846 add_loc_descr_to_each (list_ret,
13847 new_loc_descr (DW_OP_stack_value, 0, 0));
13849 return list_ret;
13853 /* Generate Dwarf location list representing LOC.
13854 If WANT_ADDRESS is false, expression computing LOC will be computed
13855 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13856 if WANT_ADDRESS is 2, expression computing address useable in location
13857 will be returned (i.e. DW_OP_reg can be used
13858 to refer to register values). */
13860 static dw_loc_list_ref
13861 loc_list_from_tree (tree loc, int want_address)
13863 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13864 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13865 int have_address = 0;
13866 enum dwarf_location_atom op;
13868 /* ??? Most of the time we do not take proper care for sign/zero
13869 extending the values properly. Hopefully this won't be a real
13870 problem... */
13872 switch (TREE_CODE (loc))
13874 case ERROR_MARK:
13875 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13876 return 0;
13878 case PLACEHOLDER_EXPR:
13879 /* This case involves extracting fields from an object to determine the
13880 position of other fields. We don't try to encode this here. The
13881 only user of this is Ada, which encodes the needed information using
13882 the names of types. */
13883 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13884 return 0;
13886 case CALL_EXPR:
13887 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13888 /* There are no opcodes for these operations. */
13889 return 0;
13891 case PREINCREMENT_EXPR:
13892 case PREDECREMENT_EXPR:
13893 case POSTINCREMENT_EXPR:
13894 case POSTDECREMENT_EXPR:
13895 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13896 /* There are no opcodes for these operations. */
13897 return 0;
13899 case ADDR_EXPR:
13900 /* If we already want an address, see if there is INDIRECT_REF inside
13901 e.g. for &this->field. */
13902 if (want_address)
13904 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13905 (loc, want_address == 2);
13906 if (list_ret)
13907 have_address = 1;
13908 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13909 && (ret = cst_pool_loc_descr (loc)))
13910 have_address = 1;
13912 /* Otherwise, process the argument and look for the address. */
13913 if (!list_ret && !ret)
13914 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13915 else
13917 if (want_address)
13918 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13919 return NULL;
13921 break;
13923 case VAR_DECL:
13924 if (DECL_THREAD_LOCAL_P (loc))
13926 rtx rtl;
13927 enum dwarf_location_atom tls_op;
13928 enum dtprel_bool dtprel = dtprel_false;
13930 if (targetm.have_tls)
13932 /* If this is not defined, we have no way to emit the
13933 data. */
13934 if (!targetm.asm_out.output_dwarf_dtprel)
13935 return 0;
13937 /* The way DW_OP_GNU_push_tls_address is specified, we
13938 can only look up addresses of objects in the current
13939 module. We used DW_OP_addr as first op, but that's
13940 wrong, because DW_OP_addr is relocated by the debug
13941 info consumer, while DW_OP_GNU_push_tls_address
13942 operand shouldn't be. */
13943 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13944 return 0;
13945 dtprel = dtprel_true;
13946 tls_op = DW_OP_GNU_push_tls_address;
13948 else
13950 if (!targetm.emutls.debug_form_tls_address
13951 || !(dwarf_version >= 3 || !dwarf_strict))
13952 return 0;
13953 /* We stuffed the control variable into the DECL_VALUE_EXPR
13954 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13955 no longer appear in gimple code. We used the control
13956 variable in specific so that we could pick it up here. */
13957 loc = DECL_VALUE_EXPR (loc);
13958 tls_op = DW_OP_form_tls_address;
13961 rtl = rtl_for_decl_location (loc);
13962 if (rtl == NULL_RTX)
13963 return 0;
13965 if (!MEM_P (rtl))
13966 return 0;
13967 rtl = XEXP (rtl, 0);
13968 if (! CONSTANT_P (rtl))
13969 return 0;
13971 ret = new_addr_loc_descr (rtl, dtprel);
13972 ret1 = new_loc_descr (tls_op, 0, 0);
13973 add_loc_descr (&ret, ret1);
13975 have_address = 1;
13976 break;
13978 /* FALLTHRU */
13980 case PARM_DECL:
13981 case RESULT_DECL:
13982 if (DECL_HAS_VALUE_EXPR_P (loc))
13983 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13984 want_address);
13985 /* FALLTHRU */
13987 case FUNCTION_DECL:
13989 rtx rtl;
13990 var_loc_list *loc_list = lookup_decl_loc (loc);
13992 if (loc_list && loc_list->first)
13994 list_ret = dw_loc_list (loc_list, loc, want_address);
13995 have_address = want_address != 0;
13996 break;
13998 rtl = rtl_for_decl_location (loc);
13999 if (rtl == NULL_RTX)
14001 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14002 return 0;
14004 else if (CONST_INT_P (rtl))
14006 HOST_WIDE_INT val = INTVAL (rtl);
14007 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14008 val &= GET_MODE_MASK (DECL_MODE (loc));
14009 ret = int_loc_descriptor (val);
14011 else if (GET_CODE (rtl) == CONST_STRING)
14013 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14014 return 0;
14016 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14017 ret = new_addr_loc_descr (rtl, dtprel_false);
14018 else
14020 enum machine_mode mode, mem_mode;
14022 /* Certain constructs can only be represented at top-level. */
14023 if (want_address == 2)
14025 ret = loc_descriptor (rtl, VOIDmode,
14026 VAR_INIT_STATUS_INITIALIZED);
14027 have_address = 1;
14029 else
14031 mode = GET_MODE (rtl);
14032 mem_mode = VOIDmode;
14033 if (MEM_P (rtl))
14035 mem_mode = mode;
14036 mode = get_address_mode (rtl);
14037 rtl = XEXP (rtl, 0);
14038 have_address = 1;
14040 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14041 VAR_INIT_STATUS_INITIALIZED);
14043 if (!ret)
14044 expansion_failed (loc, rtl,
14045 "failed to produce loc descriptor for rtl");
14048 break;
14050 case MEM_REF:
14051 /* ??? FIXME. */
14052 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14053 return 0;
14054 /* Fallthru. */
14055 case INDIRECT_REF:
14056 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14057 have_address = 1;
14058 break;
14060 case COMPOUND_EXPR:
14061 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14063 CASE_CONVERT:
14064 case VIEW_CONVERT_EXPR:
14065 case SAVE_EXPR:
14066 case MODIFY_EXPR:
14067 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14069 case COMPONENT_REF:
14070 case BIT_FIELD_REF:
14071 case ARRAY_REF:
14072 case ARRAY_RANGE_REF:
14073 case REALPART_EXPR:
14074 case IMAGPART_EXPR:
14076 tree obj, offset;
14077 HOST_WIDE_INT bitsize, bitpos, bytepos;
14078 enum machine_mode mode;
14079 int unsignedp, volatilep = 0;
14081 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14082 &unsignedp, &volatilep, false);
14084 gcc_assert (obj != loc);
14086 list_ret = loc_list_from_tree (obj,
14087 want_address == 2
14088 && !bitpos && !offset ? 2 : 1);
14089 /* TODO: We can extract value of the small expression via shifting even
14090 for nonzero bitpos. */
14091 if (list_ret == 0)
14092 return 0;
14093 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14095 expansion_failed (loc, NULL_RTX,
14096 "bitfield access");
14097 return 0;
14100 if (offset != NULL_TREE)
14102 /* Variable offset. */
14103 list_ret1 = loc_list_from_tree (offset, 0);
14104 if (list_ret1 == 0)
14105 return 0;
14106 add_loc_list (&list_ret, list_ret1);
14107 if (!list_ret)
14108 return 0;
14109 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14112 bytepos = bitpos / BITS_PER_UNIT;
14113 if (bytepos > 0)
14114 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14115 else if (bytepos < 0)
14116 loc_list_plus_const (list_ret, bytepos);
14118 have_address = 1;
14119 break;
14122 case INTEGER_CST:
14123 if ((want_address || !host_integerp (loc, 0))
14124 && (ret = cst_pool_loc_descr (loc)))
14125 have_address = 1;
14126 else if (want_address == 2
14127 && host_integerp (loc, 0)
14128 && (ret = address_of_int_loc_descriptor
14129 (int_size_in_bytes (TREE_TYPE (loc)),
14130 tree_low_cst (loc, 0))))
14131 have_address = 1;
14132 else if (host_integerp (loc, 0))
14133 ret = int_loc_descriptor (tree_low_cst (loc, 0));
14134 else
14136 expansion_failed (loc, NULL_RTX,
14137 "Integer operand is not host integer");
14138 return 0;
14140 break;
14142 case CONSTRUCTOR:
14143 case REAL_CST:
14144 case STRING_CST:
14145 case COMPLEX_CST:
14146 if ((ret = cst_pool_loc_descr (loc)))
14147 have_address = 1;
14148 else
14149 /* We can construct small constants here using int_loc_descriptor. */
14150 expansion_failed (loc, NULL_RTX,
14151 "constructor or constant not in constant pool");
14152 break;
14154 case TRUTH_AND_EXPR:
14155 case TRUTH_ANDIF_EXPR:
14156 case BIT_AND_EXPR:
14157 op = DW_OP_and;
14158 goto do_binop;
14160 case TRUTH_XOR_EXPR:
14161 case BIT_XOR_EXPR:
14162 op = DW_OP_xor;
14163 goto do_binop;
14165 case TRUTH_OR_EXPR:
14166 case TRUTH_ORIF_EXPR:
14167 case BIT_IOR_EXPR:
14168 op = DW_OP_or;
14169 goto do_binop;
14171 case FLOOR_DIV_EXPR:
14172 case CEIL_DIV_EXPR:
14173 case ROUND_DIV_EXPR:
14174 case TRUNC_DIV_EXPR:
14175 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14176 return 0;
14177 op = DW_OP_div;
14178 goto do_binop;
14180 case MINUS_EXPR:
14181 op = DW_OP_minus;
14182 goto do_binop;
14184 case FLOOR_MOD_EXPR:
14185 case CEIL_MOD_EXPR:
14186 case ROUND_MOD_EXPR:
14187 case TRUNC_MOD_EXPR:
14188 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14190 op = DW_OP_mod;
14191 goto do_binop;
14193 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14194 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14195 if (list_ret == 0 || list_ret1 == 0)
14196 return 0;
14198 add_loc_list (&list_ret, list_ret1);
14199 if (list_ret == 0)
14200 return 0;
14201 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14202 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14203 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14204 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14205 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14206 break;
14208 case MULT_EXPR:
14209 op = DW_OP_mul;
14210 goto do_binop;
14212 case LSHIFT_EXPR:
14213 op = DW_OP_shl;
14214 goto do_binop;
14216 case RSHIFT_EXPR:
14217 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14218 goto do_binop;
14220 case POINTER_PLUS_EXPR:
14221 case PLUS_EXPR:
14222 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14224 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14225 if (list_ret == 0)
14226 return 0;
14228 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14229 break;
14232 op = DW_OP_plus;
14233 goto do_binop;
14235 case LE_EXPR:
14236 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14237 return 0;
14239 op = DW_OP_le;
14240 goto do_binop;
14242 case GE_EXPR:
14243 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14244 return 0;
14246 op = DW_OP_ge;
14247 goto do_binop;
14249 case LT_EXPR:
14250 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14251 return 0;
14253 op = DW_OP_lt;
14254 goto do_binop;
14256 case GT_EXPR:
14257 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14258 return 0;
14260 op = DW_OP_gt;
14261 goto do_binop;
14263 case EQ_EXPR:
14264 op = DW_OP_eq;
14265 goto do_binop;
14267 case NE_EXPR:
14268 op = DW_OP_ne;
14269 goto do_binop;
14271 do_binop:
14272 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14273 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14274 if (list_ret == 0 || list_ret1 == 0)
14275 return 0;
14277 add_loc_list (&list_ret, list_ret1);
14278 if (list_ret == 0)
14279 return 0;
14280 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14281 break;
14283 case TRUTH_NOT_EXPR:
14284 case BIT_NOT_EXPR:
14285 op = DW_OP_not;
14286 goto do_unop;
14288 case ABS_EXPR:
14289 op = DW_OP_abs;
14290 goto do_unop;
14292 case NEGATE_EXPR:
14293 op = DW_OP_neg;
14294 goto do_unop;
14296 do_unop:
14297 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14298 if (list_ret == 0)
14299 return 0;
14301 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14302 break;
14304 case MIN_EXPR:
14305 case MAX_EXPR:
14307 const enum tree_code code =
14308 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14310 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14311 build2 (code, integer_type_node,
14312 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14313 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14316 /* ... fall through ... */
14318 case COND_EXPR:
14320 dw_loc_descr_ref lhs
14321 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14322 dw_loc_list_ref rhs
14323 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14324 dw_loc_descr_ref bra_node, jump_node, tmp;
14326 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14327 if (list_ret == 0 || lhs == 0 || rhs == 0)
14328 return 0;
14330 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14331 add_loc_descr_to_each (list_ret, bra_node);
14333 add_loc_list (&list_ret, rhs);
14334 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14335 add_loc_descr_to_each (list_ret, jump_node);
14337 add_loc_descr_to_each (list_ret, lhs);
14338 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14339 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14341 /* ??? Need a node to point the skip at. Use a nop. */
14342 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14343 add_loc_descr_to_each (list_ret, tmp);
14344 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14345 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14347 break;
14349 case FIX_TRUNC_EXPR:
14350 return 0;
14352 default:
14353 /* Leave front-end specific codes as simply unknown. This comes
14354 up, for instance, with the C STMT_EXPR. */
14355 if ((unsigned int) TREE_CODE (loc)
14356 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14358 expansion_failed (loc, NULL_RTX,
14359 "language specific tree node");
14360 return 0;
14363 #ifdef ENABLE_CHECKING
14364 /* Otherwise this is a generic code; we should just lists all of
14365 these explicitly. We forgot one. */
14366 gcc_unreachable ();
14367 #else
14368 /* In a release build, we want to degrade gracefully: better to
14369 generate incomplete debugging information than to crash. */
14370 return NULL;
14371 #endif
14374 if (!ret && !list_ret)
14375 return 0;
14377 if (want_address == 2 && !have_address
14378 && (dwarf_version >= 4 || !dwarf_strict))
14380 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14382 expansion_failed (loc, NULL_RTX,
14383 "DWARF address size mismatch");
14384 return 0;
14386 if (ret)
14387 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14388 else
14389 add_loc_descr_to_each (list_ret,
14390 new_loc_descr (DW_OP_stack_value, 0, 0));
14391 have_address = 1;
14393 /* Show if we can't fill the request for an address. */
14394 if (want_address && !have_address)
14396 expansion_failed (loc, NULL_RTX,
14397 "Want address and only have value");
14398 return 0;
14401 gcc_assert (!ret || !list_ret);
14403 /* If we've got an address and don't want one, dereference. */
14404 if (!want_address && have_address)
14406 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14408 if (size > DWARF2_ADDR_SIZE || size == -1)
14410 expansion_failed (loc, NULL_RTX,
14411 "DWARF address size mismatch");
14412 return 0;
14414 else if (size == DWARF2_ADDR_SIZE)
14415 op = DW_OP_deref;
14416 else
14417 op = DW_OP_deref_size;
14419 if (ret)
14420 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14421 else
14422 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14424 if (ret)
14425 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14427 return list_ret;
14430 /* Same as above but return only single location expression. */
14431 static dw_loc_descr_ref
14432 loc_descriptor_from_tree (tree loc, int want_address)
14434 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14435 if (!ret)
14436 return NULL;
14437 if (ret->dw_loc_next)
14439 expansion_failed (loc, NULL_RTX,
14440 "Location list where only loc descriptor needed");
14441 return NULL;
14443 return ret->expr;
14446 /* Given a value, round it up to the lowest multiple of `boundary'
14447 which is not less than the value itself. */
14449 static inline HOST_WIDE_INT
14450 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14452 return (((value + boundary - 1) / boundary) * boundary);
14455 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14456 pointer to the declared type for the relevant field variable, or return
14457 `integer_type_node' if the given node turns out to be an
14458 ERROR_MARK node. */
14460 static inline tree
14461 field_type (const_tree decl)
14463 tree type;
14465 if (TREE_CODE (decl) == ERROR_MARK)
14466 return integer_type_node;
14468 type = DECL_BIT_FIELD_TYPE (decl);
14469 if (type == NULL_TREE)
14470 type = TREE_TYPE (decl);
14472 return type;
14475 /* Given a pointer to a tree node, return the alignment in bits for
14476 it, or else return BITS_PER_WORD if the node actually turns out to
14477 be an ERROR_MARK node. */
14479 static inline unsigned
14480 simple_type_align_in_bits (const_tree type)
14482 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14485 static inline unsigned
14486 simple_decl_align_in_bits (const_tree decl)
14488 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14491 /* Return the result of rounding T up to ALIGN. */
14493 static inline double_int
14494 round_up_to_align (double_int t, unsigned int align)
14496 double_int alignd = double_int::from_uhwi (align);
14497 t += alignd;
14498 t += double_int_minus_one;
14499 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14500 t *= alignd;
14501 return t;
14504 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14505 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14506 or return 0 if we are unable to determine what that offset is, either
14507 because the argument turns out to be a pointer to an ERROR_MARK node, or
14508 because the offset is actually variable. (We can't handle the latter case
14509 just yet). */
14511 static HOST_WIDE_INT
14512 field_byte_offset (const_tree decl)
14514 double_int object_offset_in_bits;
14515 double_int object_offset_in_bytes;
14516 double_int bitpos_int;
14518 if (TREE_CODE (decl) == ERROR_MARK)
14519 return 0;
14521 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14523 /* We cannot yet cope with fields whose positions are variable, so
14524 for now, when we see such things, we simply return 0. Someday, we may
14525 be able to handle such cases, but it will be damn difficult. */
14526 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14527 return 0;
14529 bitpos_int = tree_to_double_int (bit_position (decl));
14531 #ifdef PCC_BITFIELD_TYPE_MATTERS
14532 if (PCC_BITFIELD_TYPE_MATTERS)
14534 tree type;
14535 tree field_size_tree;
14536 double_int deepest_bitpos;
14537 double_int field_size_in_bits;
14538 unsigned int type_align_in_bits;
14539 unsigned int decl_align_in_bits;
14540 double_int type_size_in_bits;
14542 type = field_type (decl);
14543 type_size_in_bits = double_int_type_size_in_bits (type);
14544 type_align_in_bits = simple_type_align_in_bits (type);
14546 field_size_tree = DECL_SIZE (decl);
14548 /* The size could be unspecified if there was an error, or for
14549 a flexible array member. */
14550 if (!field_size_tree)
14551 field_size_tree = bitsize_zero_node;
14553 /* If the size of the field is not constant, use the type size. */
14554 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14555 field_size_in_bits = tree_to_double_int (field_size_tree);
14556 else
14557 field_size_in_bits = type_size_in_bits;
14559 decl_align_in_bits = simple_decl_align_in_bits (decl);
14561 /* The GCC front-end doesn't make any attempt to keep track of the
14562 starting bit offset (relative to the start of the containing
14563 structure type) of the hypothetical "containing object" for a
14564 bit-field. Thus, when computing the byte offset value for the
14565 start of the "containing object" of a bit-field, we must deduce
14566 this information on our own. This can be rather tricky to do in
14567 some cases. For example, handling the following structure type
14568 definition when compiling for an i386/i486 target (which only
14569 aligns long long's to 32-bit boundaries) can be very tricky:
14571 struct S { int field1; long long field2:31; };
14573 Fortunately, there is a simple rule-of-thumb which can be used
14574 in such cases. When compiling for an i386/i486, GCC will
14575 allocate 8 bytes for the structure shown above. It decides to
14576 do this based upon one simple rule for bit-field allocation.
14577 GCC allocates each "containing object" for each bit-field at
14578 the first (i.e. lowest addressed) legitimate alignment boundary
14579 (based upon the required minimum alignment for the declared
14580 type of the field) which it can possibly use, subject to the
14581 condition that there is still enough available space remaining
14582 in the containing object (when allocated at the selected point)
14583 to fully accommodate all of the bits of the bit-field itself.
14585 This simple rule makes it obvious why GCC allocates 8 bytes for
14586 each object of the structure type shown above. When looking
14587 for a place to allocate the "containing object" for `field2',
14588 the compiler simply tries to allocate a 64-bit "containing
14589 object" at each successive 32-bit boundary (starting at zero)
14590 until it finds a place to allocate that 64- bit field such that
14591 at least 31 contiguous (and previously unallocated) bits remain
14592 within that selected 64 bit field. (As it turns out, for the
14593 example above, the compiler finds it is OK to allocate the
14594 "containing object" 64-bit field at bit-offset zero within the
14595 structure type.)
14597 Here we attempt to work backwards from the limited set of facts
14598 we're given, and we try to deduce from those facts, where GCC
14599 must have believed that the containing object started (within
14600 the structure type). The value we deduce is then used (by the
14601 callers of this routine) to generate DW_AT_location and
14602 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14603 the case of DW_AT_location, regular fields as well). */
14605 /* Figure out the bit-distance from the start of the structure to
14606 the "deepest" bit of the bit-field. */
14607 deepest_bitpos = bitpos_int + field_size_in_bits;
14609 /* This is the tricky part. Use some fancy footwork to deduce
14610 where the lowest addressed bit of the containing object must
14611 be. */
14612 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14614 /* Round up to type_align by default. This works best for
14615 bitfields. */
14616 object_offset_in_bits
14617 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14619 if (object_offset_in_bits.ugt (bitpos_int))
14621 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14623 /* Round up to decl_align instead. */
14624 object_offset_in_bits
14625 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14628 else
14629 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14630 object_offset_in_bits = bitpos_int;
14632 object_offset_in_bytes
14633 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14634 true, TRUNC_DIV_EXPR);
14635 return object_offset_in_bytes.to_shwi ();
14638 /* The following routines define various Dwarf attributes and any data
14639 associated with them. */
14641 /* Add a location description attribute value to a DIE.
14643 This emits location attributes suitable for whole variables and
14644 whole parameters. Note that the location attributes for struct fields are
14645 generated by the routine `data_member_location_attribute' below. */
14647 static inline void
14648 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14649 dw_loc_list_ref descr)
14651 if (descr == 0)
14652 return;
14653 if (single_element_loc_list_p (descr))
14654 add_AT_loc (die, attr_kind, descr->expr);
14655 else
14656 add_AT_loc_list (die, attr_kind, descr);
14659 /* Add DW_AT_accessibility attribute to DIE if needed. */
14661 static void
14662 add_accessibility_attribute (dw_die_ref die, tree decl)
14664 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14665 children, otherwise the default is DW_ACCESS_public. In DWARF2
14666 the default has always been DW_ACCESS_public. */
14667 if (TREE_PROTECTED (decl))
14668 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14669 else if (TREE_PRIVATE (decl))
14671 if (dwarf_version == 2
14672 || die->die_parent == NULL
14673 || die->die_parent->die_tag != DW_TAG_class_type)
14674 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14676 else if (dwarf_version > 2
14677 && die->die_parent
14678 && die->die_parent->die_tag == DW_TAG_class_type)
14679 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14682 /* Attach the specialized form of location attribute used for data members of
14683 struct and union types. In the special case of a FIELD_DECL node which
14684 represents a bit-field, the "offset" part of this special location
14685 descriptor must indicate the distance in bytes from the lowest-addressed
14686 byte of the containing struct or union type to the lowest-addressed byte of
14687 the "containing object" for the bit-field. (See the `field_byte_offset'
14688 function above).
14690 For any given bit-field, the "containing object" is a hypothetical object
14691 (of some integral or enum type) within which the given bit-field lives. The
14692 type of this hypothetical "containing object" is always the same as the
14693 declared type of the individual bit-field itself (for GCC anyway... the
14694 DWARF spec doesn't actually mandate this). Note that it is the size (in
14695 bytes) of the hypothetical "containing object" which will be given in the
14696 DW_AT_byte_size attribute for this bit-field. (See the
14697 `byte_size_attribute' function below.) It is also used when calculating the
14698 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14699 function below.) */
14701 static void
14702 add_data_member_location_attribute (dw_die_ref die, tree decl)
14704 HOST_WIDE_INT offset;
14705 dw_loc_descr_ref loc_descr = 0;
14707 if (TREE_CODE (decl) == TREE_BINFO)
14709 /* We're working on the TAG_inheritance for a base class. */
14710 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14712 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14713 aren't at a fixed offset from all (sub)objects of the same
14714 type. We need to extract the appropriate offset from our
14715 vtable. The following dwarf expression means
14717 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14719 This is specific to the V3 ABI, of course. */
14721 dw_loc_descr_ref tmp;
14723 /* Make a copy of the object address. */
14724 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14725 add_loc_descr (&loc_descr, tmp);
14727 /* Extract the vtable address. */
14728 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14729 add_loc_descr (&loc_descr, tmp);
14731 /* Calculate the address of the offset. */
14732 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14733 gcc_assert (offset < 0);
14735 tmp = int_loc_descriptor (-offset);
14736 add_loc_descr (&loc_descr, tmp);
14737 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14738 add_loc_descr (&loc_descr, tmp);
14740 /* Extract the offset. */
14741 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14742 add_loc_descr (&loc_descr, tmp);
14744 /* Add it to the object address. */
14745 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14746 add_loc_descr (&loc_descr, tmp);
14748 else
14749 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14751 else
14752 offset = field_byte_offset (decl);
14754 if (! loc_descr)
14756 if (dwarf_version > 2)
14758 /* Don't need to output a location expression, just the constant. */
14759 if (offset < 0)
14760 add_AT_int (die, DW_AT_data_member_location, offset);
14761 else
14762 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14763 return;
14765 else
14767 enum dwarf_location_atom op;
14769 /* The DWARF2 standard says that we should assume that the structure
14770 address is already on the stack, so we can specify a structure
14771 field address by using DW_OP_plus_uconst. */
14772 op = DW_OP_plus_uconst;
14773 loc_descr = new_loc_descr (op, offset, 0);
14777 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14780 /* Writes integer values to dw_vec_const array. */
14782 static void
14783 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14785 while (size != 0)
14787 *dest++ = val & 0xff;
14788 val >>= 8;
14789 --size;
14793 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14795 static HOST_WIDE_INT
14796 extract_int (const unsigned char *src, unsigned int size)
14798 HOST_WIDE_INT val = 0;
14800 src += size;
14801 while (size != 0)
14803 val <<= 8;
14804 val |= *--src & 0xff;
14805 --size;
14807 return val;
14810 /* Writes double_int values to dw_vec_const array. */
14812 static void
14813 insert_double (double_int val, unsigned char *dest)
14815 unsigned char *p0 = dest;
14816 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14818 if (WORDS_BIG_ENDIAN)
14820 p0 = p1;
14821 p1 = dest;
14824 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14825 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14828 /* Writes floating point values to dw_vec_const array. */
14830 static void
14831 insert_float (const_rtx rtl, unsigned char *array)
14833 REAL_VALUE_TYPE rv;
14834 long val[4];
14835 int i;
14837 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14838 real_to_target (val, &rv, GET_MODE (rtl));
14840 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14841 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14843 insert_int (val[i], 4, array);
14844 array += 4;
14848 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14849 does not have a "location" either in memory or in a register. These
14850 things can arise in GNU C when a constant is passed as an actual parameter
14851 to an inlined function. They can also arise in C++ where declared
14852 constants do not necessarily get memory "homes". */
14854 static bool
14855 add_const_value_attribute (dw_die_ref die, rtx rtl)
14857 switch (GET_CODE (rtl))
14859 case CONST_INT:
14861 HOST_WIDE_INT val = INTVAL (rtl);
14863 if (val < 0)
14864 add_AT_int (die, DW_AT_const_value, val);
14865 else
14866 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14868 return true;
14870 case CONST_DOUBLE:
14871 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14872 floating-point constant. A CONST_DOUBLE is used whenever the
14873 constant requires more than one word in order to be adequately
14874 represented. */
14876 enum machine_mode mode = GET_MODE (rtl);
14878 if (SCALAR_FLOAT_MODE_P (mode))
14880 unsigned int length = GET_MODE_SIZE (mode);
14881 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14883 insert_float (rtl, array);
14884 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14886 else
14887 add_AT_double (die, DW_AT_const_value,
14888 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14890 return true;
14892 case CONST_VECTOR:
14894 enum machine_mode mode = GET_MODE (rtl);
14895 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14896 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14897 unsigned char *array = (unsigned char *) ggc_alloc_atomic
14898 (length * elt_size);
14899 unsigned int i;
14900 unsigned char *p;
14902 switch (GET_MODE_CLASS (mode))
14904 case MODE_VECTOR_INT:
14905 for (i = 0, p = array; i < length; i++, p += elt_size)
14907 rtx elt = CONST_VECTOR_ELT (rtl, i);
14908 double_int val = rtx_to_double_int (elt);
14910 if (elt_size <= sizeof (HOST_WIDE_INT))
14911 insert_int (val.to_shwi (), elt_size, p);
14912 else
14914 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14915 insert_double (val, p);
14918 break;
14920 case MODE_VECTOR_FLOAT:
14921 for (i = 0, p = array; i < length; i++, p += elt_size)
14923 rtx elt = CONST_VECTOR_ELT (rtl, i);
14924 insert_float (elt, p);
14926 break;
14928 default:
14929 gcc_unreachable ();
14932 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14934 return true;
14936 case CONST_STRING:
14937 if (dwarf_version >= 4 || !dwarf_strict)
14939 dw_loc_descr_ref loc_result;
14940 resolve_one_addr (&rtl, NULL);
14941 rtl_addr:
14942 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14943 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14944 add_AT_loc (die, DW_AT_location, loc_result);
14945 vec_safe_push (used_rtx_array, rtl);
14946 return true;
14948 return false;
14950 case CONST:
14951 if (CONSTANT_P (XEXP (rtl, 0)))
14952 return add_const_value_attribute (die, XEXP (rtl, 0));
14953 /* FALLTHROUGH */
14954 case SYMBOL_REF:
14955 if (!const_ok_for_output (rtl))
14956 return false;
14957 case LABEL_REF:
14958 if (dwarf_version >= 4 || !dwarf_strict)
14959 goto rtl_addr;
14960 return false;
14962 case PLUS:
14963 /* In cases where an inlined instance of an inline function is passed
14964 the address of an `auto' variable (which is local to the caller) we
14965 can get a situation where the DECL_RTL of the artificial local
14966 variable (for the inlining) which acts as a stand-in for the
14967 corresponding formal parameter (of the inline function) will look
14968 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
14969 exactly a compile-time constant expression, but it isn't the address
14970 of the (artificial) local variable either. Rather, it represents the
14971 *value* which the artificial local variable always has during its
14972 lifetime. We currently have no way to represent such quasi-constant
14973 values in Dwarf, so for now we just punt and generate nothing. */
14974 return false;
14976 case HIGH:
14977 case CONST_FIXED:
14978 return false;
14980 case MEM:
14981 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14982 && MEM_READONLY_P (rtl)
14983 && GET_MODE (rtl) == BLKmode)
14985 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14986 return true;
14988 return false;
14990 default:
14991 /* No other kinds of rtx should be possible here. */
14992 gcc_unreachable ();
14994 return false;
14997 /* Determine whether the evaluation of EXPR references any variables
14998 or functions which aren't otherwise used (and therefore may not be
14999 output). */
15000 static tree
15001 reference_to_unused (tree * tp, int * walk_subtrees,
15002 void * data ATTRIBUTE_UNUSED)
15004 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15005 *walk_subtrees = 0;
15007 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15008 && ! TREE_ASM_WRITTEN (*tp))
15009 return *tp;
15010 /* ??? The C++ FE emits debug information for using decls, so
15011 putting gcc_unreachable here falls over. See PR31899. For now
15012 be conservative. */
15013 else if (!cgraph_global_info_ready
15014 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15015 return *tp;
15016 else if (TREE_CODE (*tp) == VAR_DECL)
15018 struct varpool_node *node = varpool_get_node (*tp);
15019 if (!node || !node->symbol.definition)
15020 return *tp;
15022 else if (TREE_CODE (*tp) == FUNCTION_DECL
15023 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15025 /* The call graph machinery must have finished analyzing,
15026 optimizing and gimplifying the CU by now.
15027 So if *TP has no call graph node associated
15028 to it, it means *TP will not be emitted. */
15029 if (!cgraph_get_node (*tp))
15030 return *tp;
15032 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15033 return *tp;
15035 return NULL_TREE;
15038 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15039 for use in a later add_const_value_attribute call. */
15041 static rtx
15042 rtl_for_decl_init (tree init, tree type)
15044 rtx rtl = NULL_RTX;
15046 STRIP_NOPS (init);
15048 /* If a variable is initialized with a string constant without embedded
15049 zeros, build CONST_STRING. */
15050 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15052 tree enttype = TREE_TYPE (type);
15053 tree domain = TYPE_DOMAIN (type);
15054 enum machine_mode mode = TYPE_MODE (enttype);
15056 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15057 && domain
15058 && integer_zerop (TYPE_MIN_VALUE (domain))
15059 && compare_tree_int (TYPE_MAX_VALUE (domain),
15060 TREE_STRING_LENGTH (init) - 1) == 0
15061 && ((size_t) TREE_STRING_LENGTH (init)
15062 == strlen (TREE_STRING_POINTER (init)) + 1))
15064 rtl = gen_rtx_CONST_STRING (VOIDmode,
15065 ggc_strdup (TREE_STRING_POINTER (init)));
15066 rtl = gen_rtx_MEM (BLKmode, rtl);
15067 MEM_READONLY_P (rtl) = 1;
15070 /* Other aggregates, and complex values, could be represented using
15071 CONCAT: FIXME! */
15072 else if (AGGREGATE_TYPE_P (type)
15073 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15074 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15075 || TREE_CODE (type) == COMPLEX_TYPE)
15077 /* Vectors only work if their mode is supported by the target.
15078 FIXME: generic vectors ought to work too. */
15079 else if (TREE_CODE (type) == VECTOR_TYPE
15080 && !VECTOR_MODE_P (TYPE_MODE (type)))
15082 /* If the initializer is something that we know will expand into an
15083 immediate RTL constant, expand it now. We must be careful not to
15084 reference variables which won't be output. */
15085 else if (initializer_constant_valid_p (init, type)
15086 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15088 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15089 possible. */
15090 if (TREE_CODE (type) == VECTOR_TYPE)
15091 switch (TREE_CODE (init))
15093 case VECTOR_CST:
15094 break;
15095 case CONSTRUCTOR:
15096 if (TREE_CONSTANT (init))
15098 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15099 bool constant_p = true;
15100 tree value;
15101 unsigned HOST_WIDE_INT ix;
15103 /* Even when ctor is constant, it might contain non-*_CST
15104 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15105 belong into VECTOR_CST nodes. */
15106 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15107 if (!CONSTANT_CLASS_P (value))
15109 constant_p = false;
15110 break;
15113 if (constant_p)
15115 init = build_vector_from_ctor (type, elts);
15116 break;
15119 /* FALLTHRU */
15121 default:
15122 return NULL;
15125 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15127 /* If expand_expr returns a MEM, it wasn't immediate. */
15128 gcc_assert (!rtl || !MEM_P (rtl));
15131 return rtl;
15134 /* Generate RTL for the variable DECL to represent its location. */
15136 static rtx
15137 rtl_for_decl_location (tree decl)
15139 rtx rtl;
15141 /* Here we have to decide where we are going to say the parameter "lives"
15142 (as far as the debugger is concerned). We only have a couple of
15143 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15145 DECL_RTL normally indicates where the parameter lives during most of the
15146 activation of the function. If optimization is enabled however, this
15147 could be either NULL or else a pseudo-reg. Both of those cases indicate
15148 that the parameter doesn't really live anywhere (as far as the code
15149 generation parts of GCC are concerned) during most of the function's
15150 activation. That will happen (for example) if the parameter is never
15151 referenced within the function.
15153 We could just generate a location descriptor here for all non-NULL
15154 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15155 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15156 where DECL_RTL is NULL or is a pseudo-reg.
15158 Note however that we can only get away with using DECL_INCOMING_RTL as
15159 a backup substitute for DECL_RTL in certain limited cases. In cases
15160 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15161 we can be sure that the parameter was passed using the same type as it is
15162 declared to have within the function, and that its DECL_INCOMING_RTL
15163 points us to a place where a value of that type is passed.
15165 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15166 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15167 because in these cases DECL_INCOMING_RTL points us to a value of some
15168 type which is *different* from the type of the parameter itself. Thus,
15169 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15170 such cases, the debugger would end up (for example) trying to fetch a
15171 `float' from a place which actually contains the first part of a
15172 `double'. That would lead to really incorrect and confusing
15173 output at debug-time.
15175 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15176 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15177 are a couple of exceptions however. On little-endian machines we can
15178 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15179 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15180 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15181 when (on a little-endian machine) a non-prototyped function has a
15182 parameter declared to be of type `short' or `char'. In such cases,
15183 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15184 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15185 passed `int' value. If the debugger then uses that address to fetch
15186 a `short' or a `char' (on a little-endian machine) the result will be
15187 the correct data, so we allow for such exceptional cases below.
15189 Note that our goal here is to describe the place where the given formal
15190 parameter lives during most of the function's activation (i.e. between the
15191 end of the prologue and the start of the epilogue). We'll do that as best
15192 as we can. Note however that if the given formal parameter is modified
15193 sometime during the execution of the function, then a stack backtrace (at
15194 debug-time) will show the function as having been called with the *new*
15195 value rather than the value which was originally passed in. This happens
15196 rarely enough that it is not a major problem, but it *is* a problem, and
15197 I'd like to fix it.
15199 A future version of dwarf2out.c may generate two additional attributes for
15200 any given DW_TAG_formal_parameter DIE which will describe the "passed
15201 type" and the "passed location" for the given formal parameter in addition
15202 to the attributes we now generate to indicate the "declared type" and the
15203 "active location" for each parameter. This additional set of attributes
15204 could be used by debuggers for stack backtraces. Separately, note that
15205 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15206 This happens (for example) for inlined-instances of inline function formal
15207 parameters which are never referenced. This really shouldn't be
15208 happening. All PARM_DECL nodes should get valid non-NULL
15209 DECL_INCOMING_RTL values. FIXME. */
15211 /* Use DECL_RTL as the "location" unless we find something better. */
15212 rtl = DECL_RTL_IF_SET (decl);
15214 /* When generating abstract instances, ignore everything except
15215 constants, symbols living in memory, and symbols living in
15216 fixed registers. */
15217 if (! reload_completed)
15219 if (rtl
15220 && (CONSTANT_P (rtl)
15221 || (MEM_P (rtl)
15222 && CONSTANT_P (XEXP (rtl, 0)))
15223 || (REG_P (rtl)
15224 && TREE_CODE (decl) == VAR_DECL
15225 && TREE_STATIC (decl))))
15227 rtl = targetm.delegitimize_address (rtl);
15228 return rtl;
15230 rtl = NULL_RTX;
15232 else if (TREE_CODE (decl) == PARM_DECL)
15234 if (rtl == NULL_RTX
15235 || is_pseudo_reg (rtl)
15236 || (MEM_P (rtl)
15237 && is_pseudo_reg (XEXP (rtl, 0))
15238 && DECL_INCOMING_RTL (decl)
15239 && MEM_P (DECL_INCOMING_RTL (decl))
15240 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15242 tree declared_type = TREE_TYPE (decl);
15243 tree passed_type = DECL_ARG_TYPE (decl);
15244 enum machine_mode dmode = TYPE_MODE (declared_type);
15245 enum machine_mode pmode = TYPE_MODE (passed_type);
15247 /* This decl represents a formal parameter which was optimized out.
15248 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15249 all cases where (rtl == NULL_RTX) just below. */
15250 if (dmode == pmode)
15251 rtl = DECL_INCOMING_RTL (decl);
15252 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15253 && SCALAR_INT_MODE_P (dmode)
15254 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15255 && DECL_INCOMING_RTL (decl))
15257 rtx inc = DECL_INCOMING_RTL (decl);
15258 if (REG_P (inc))
15259 rtl = inc;
15260 else if (MEM_P (inc))
15262 if (BYTES_BIG_ENDIAN)
15263 rtl = adjust_address_nv (inc, dmode,
15264 GET_MODE_SIZE (pmode)
15265 - GET_MODE_SIZE (dmode));
15266 else
15267 rtl = inc;
15272 /* If the parm was passed in registers, but lives on the stack, then
15273 make a big endian correction if the mode of the type of the
15274 parameter is not the same as the mode of the rtl. */
15275 /* ??? This is the same series of checks that are made in dbxout.c before
15276 we reach the big endian correction code there. It isn't clear if all
15277 of these checks are necessary here, but keeping them all is the safe
15278 thing to do. */
15279 else if (MEM_P (rtl)
15280 && XEXP (rtl, 0) != const0_rtx
15281 && ! CONSTANT_P (XEXP (rtl, 0))
15282 /* Not passed in memory. */
15283 && !MEM_P (DECL_INCOMING_RTL (decl))
15284 /* Not passed by invisible reference. */
15285 && (!REG_P (XEXP (rtl, 0))
15286 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15287 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15288 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15289 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15290 #endif
15292 /* Big endian correction check. */
15293 && BYTES_BIG_ENDIAN
15294 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15295 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15296 < UNITS_PER_WORD))
15298 enum machine_mode addr_mode = get_address_mode (rtl);
15299 int offset = (UNITS_PER_WORD
15300 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15302 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15303 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15306 else if (TREE_CODE (decl) == VAR_DECL
15307 && rtl
15308 && MEM_P (rtl)
15309 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15310 && BYTES_BIG_ENDIAN)
15312 enum machine_mode addr_mode = get_address_mode (rtl);
15313 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15314 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15316 /* If a variable is declared "register" yet is smaller than
15317 a register, then if we store the variable to memory, it
15318 looks like we're storing a register-sized value, when in
15319 fact we are not. We need to adjust the offset of the
15320 storage location to reflect the actual value's bytes,
15321 else gdb will not be able to display it. */
15322 if (rsize > dsize)
15323 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15324 plus_constant (addr_mode, XEXP (rtl, 0),
15325 rsize - dsize));
15328 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15329 and will have been substituted directly into all expressions that use it.
15330 C does not have such a concept, but C++ and other languages do. */
15331 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15332 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15334 if (rtl)
15335 rtl = targetm.delegitimize_address (rtl);
15337 /* If we don't look past the constant pool, we risk emitting a
15338 reference to a constant pool entry that isn't referenced from
15339 code, and thus is not emitted. */
15340 if (rtl)
15341 rtl = avoid_constant_pool_reference (rtl);
15343 /* Try harder to get a rtl. If this symbol ends up not being emitted
15344 in the current CU, resolve_addr will remove the expression referencing
15345 it. */
15346 if (rtl == NULL_RTX
15347 && TREE_CODE (decl) == VAR_DECL
15348 && !DECL_EXTERNAL (decl)
15349 && TREE_STATIC (decl)
15350 && DECL_NAME (decl)
15351 && !DECL_HARD_REGISTER (decl)
15352 && DECL_MODE (decl) != VOIDmode)
15354 rtl = make_decl_rtl_for_debug (decl);
15355 if (!MEM_P (rtl)
15356 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15357 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15358 rtl = NULL_RTX;
15361 return rtl;
15364 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15365 returned. If so, the decl for the COMMON block is returned, and the
15366 value is the offset into the common block for the symbol. */
15368 static tree
15369 fortran_common (tree decl, HOST_WIDE_INT *value)
15371 tree val_expr, cvar;
15372 enum machine_mode mode;
15373 HOST_WIDE_INT bitsize, bitpos;
15374 tree offset;
15375 int unsignedp, volatilep = 0;
15377 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15378 it does not have a value (the offset into the common area), or if it
15379 is thread local (as opposed to global) then it isn't common, and shouldn't
15380 be handled as such. */
15381 if (TREE_CODE (decl) != VAR_DECL
15382 || !TREE_STATIC (decl)
15383 || !DECL_HAS_VALUE_EXPR_P (decl)
15384 || !is_fortran ())
15385 return NULL_TREE;
15387 val_expr = DECL_VALUE_EXPR (decl);
15388 if (TREE_CODE (val_expr) != COMPONENT_REF)
15389 return NULL_TREE;
15391 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15392 &mode, &unsignedp, &volatilep, true);
15394 if (cvar == NULL_TREE
15395 || TREE_CODE (cvar) != VAR_DECL
15396 || DECL_ARTIFICIAL (cvar)
15397 || !TREE_PUBLIC (cvar))
15398 return NULL_TREE;
15400 *value = 0;
15401 if (offset != NULL)
15403 if (!host_integerp (offset, 0))
15404 return NULL_TREE;
15405 *value = tree_low_cst (offset, 0);
15407 if (bitpos != 0)
15408 *value += bitpos / BITS_PER_UNIT;
15410 return cvar;
15413 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15414 data attribute for a variable or a parameter. We generate the
15415 DW_AT_const_value attribute only in those cases where the given variable
15416 or parameter does not have a true "location" either in memory or in a
15417 register. This can happen (for example) when a constant is passed as an
15418 actual argument in a call to an inline function. (It's possible that
15419 these things can crop up in other ways also.) Note that one type of
15420 constant value which can be passed into an inlined function is a constant
15421 pointer. This can happen for example if an actual argument in an inlined
15422 function call evaluates to a compile-time constant address.
15424 CACHE_P is true if it is worth caching the location list for DECL,
15425 so that future calls can reuse it rather than regenerate it from scratch.
15426 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15427 since we will need to refer to them each time the function is inlined. */
15429 static bool
15430 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15431 enum dwarf_attribute attr)
15433 rtx rtl;
15434 dw_loc_list_ref list;
15435 var_loc_list *loc_list;
15436 cached_dw_loc_list *cache;
15437 void **slot;
15439 if (TREE_CODE (decl) == ERROR_MARK)
15440 return false;
15442 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15443 || TREE_CODE (decl) == RESULT_DECL);
15445 /* Try to get some constant RTL for this decl, and use that as the value of
15446 the location. */
15448 rtl = rtl_for_decl_location (decl);
15449 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15450 && add_const_value_attribute (die, rtl))
15451 return true;
15453 /* See if we have single element location list that is equivalent to
15454 a constant value. That way we are better to use add_const_value_attribute
15455 rather than expanding constant value equivalent. */
15456 loc_list = lookup_decl_loc (decl);
15457 if (loc_list
15458 && loc_list->first
15459 && loc_list->first->next == NULL
15460 && NOTE_P (loc_list->first->loc)
15461 && NOTE_VAR_LOCATION (loc_list->first->loc)
15462 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15464 struct var_loc_node *node;
15466 node = loc_list->first;
15467 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15468 if (GET_CODE (rtl) == EXPR_LIST)
15469 rtl = XEXP (rtl, 0);
15470 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15471 && add_const_value_attribute (die, rtl))
15472 return true;
15474 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15475 list several times. See if we've already cached the contents. */
15476 list = NULL;
15477 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15478 cache_p = false;
15479 if (cache_p)
15481 cache = (cached_dw_loc_list *)
15482 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15483 if (cache)
15484 list = cache->loc_list;
15486 if (list == NULL)
15488 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15489 /* It is usually worth caching this result if the decl is from
15490 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15491 if (cache_p && list && list->dw_loc_next)
15493 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15494 DECL_UID (decl), INSERT);
15495 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15496 cache->decl_id = DECL_UID (decl);
15497 cache->loc_list = list;
15498 *slot = cache;
15501 if (list)
15503 add_AT_location_description (die, attr, list);
15504 return true;
15506 /* None of that worked, so it must not really have a location;
15507 try adding a constant value attribute from the DECL_INITIAL. */
15508 return tree_add_const_value_attribute_for_decl (die, decl);
15511 /* Add VARIABLE and DIE into deferred locations list. */
15513 static void
15514 defer_location (tree variable, dw_die_ref die)
15516 deferred_locations entry;
15517 entry.variable = variable;
15518 entry.die = die;
15519 vec_safe_push (deferred_locations_list, entry);
15522 /* Helper function for tree_add_const_value_attribute. Natively encode
15523 initializer INIT into an array. Return true if successful. */
15525 static bool
15526 native_encode_initializer (tree init, unsigned char *array, int size)
15528 tree type;
15530 if (init == NULL_TREE)
15531 return false;
15533 STRIP_NOPS (init);
15534 switch (TREE_CODE (init))
15536 case STRING_CST:
15537 type = TREE_TYPE (init);
15538 if (TREE_CODE (type) == ARRAY_TYPE)
15540 tree enttype = TREE_TYPE (type);
15541 enum machine_mode mode = TYPE_MODE (enttype);
15543 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15544 return false;
15545 if (int_size_in_bytes (type) != size)
15546 return false;
15547 if (size > TREE_STRING_LENGTH (init))
15549 memcpy (array, TREE_STRING_POINTER (init),
15550 TREE_STRING_LENGTH (init));
15551 memset (array + TREE_STRING_LENGTH (init),
15552 '\0', size - TREE_STRING_LENGTH (init));
15554 else
15555 memcpy (array, TREE_STRING_POINTER (init), size);
15556 return true;
15558 return false;
15559 case CONSTRUCTOR:
15560 type = TREE_TYPE (init);
15561 if (int_size_in_bytes (type) != size)
15562 return false;
15563 if (TREE_CODE (type) == ARRAY_TYPE)
15565 HOST_WIDE_INT min_index;
15566 unsigned HOST_WIDE_INT cnt;
15567 int curpos = 0, fieldsize;
15568 constructor_elt *ce;
15570 if (TYPE_DOMAIN (type) == NULL_TREE
15571 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15572 return false;
15574 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15575 if (fieldsize <= 0)
15576 return false;
15578 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15579 memset (array, '\0', size);
15580 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15582 tree val = ce->value;
15583 tree index = ce->index;
15584 int pos = curpos;
15585 if (index && TREE_CODE (index) == RANGE_EXPR)
15586 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15587 * fieldsize;
15588 else if (index)
15589 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15591 if (val)
15593 STRIP_NOPS (val);
15594 if (!native_encode_initializer (val, array + pos, fieldsize))
15595 return false;
15597 curpos = pos + fieldsize;
15598 if (index && TREE_CODE (index) == RANGE_EXPR)
15600 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15601 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15602 while (count-- > 0)
15604 if (val)
15605 memcpy (array + curpos, array + pos, fieldsize);
15606 curpos += fieldsize;
15609 gcc_assert (curpos <= size);
15611 return true;
15613 else if (TREE_CODE (type) == RECORD_TYPE
15614 || TREE_CODE (type) == UNION_TYPE)
15616 tree field = NULL_TREE;
15617 unsigned HOST_WIDE_INT cnt;
15618 constructor_elt *ce;
15620 if (int_size_in_bytes (type) != size)
15621 return false;
15623 if (TREE_CODE (type) == RECORD_TYPE)
15624 field = TYPE_FIELDS (type);
15626 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15628 tree val = ce->value;
15629 int pos, fieldsize;
15631 if (ce->index != 0)
15632 field = ce->index;
15634 if (val)
15635 STRIP_NOPS (val);
15637 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15638 return false;
15640 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15641 && TYPE_DOMAIN (TREE_TYPE (field))
15642 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15643 return false;
15644 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15645 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15646 return false;
15647 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15648 pos = int_byte_position (field);
15649 gcc_assert (pos + fieldsize <= size);
15650 if (val
15651 && !native_encode_initializer (val, array + pos, fieldsize))
15652 return false;
15654 return true;
15656 return false;
15657 case VIEW_CONVERT_EXPR:
15658 case NON_LVALUE_EXPR:
15659 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15660 default:
15661 return native_encode_expr (init, array, size) == size;
15665 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15666 attribute is the const value T. */
15668 static bool
15669 tree_add_const_value_attribute (dw_die_ref die, tree t)
15671 tree init;
15672 tree type = TREE_TYPE (t);
15673 rtx rtl;
15675 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15676 return false;
15678 init = t;
15679 gcc_assert (!DECL_P (init));
15681 rtl = rtl_for_decl_init (init, type);
15682 if (rtl)
15683 return add_const_value_attribute (die, rtl);
15684 /* If the host and target are sane, try harder. */
15685 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15686 && initializer_constant_valid_p (init, type))
15688 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15689 if (size > 0 && (int) size == size)
15691 unsigned char *array = (unsigned char *)
15692 ggc_alloc_cleared_atomic (size);
15694 if (native_encode_initializer (init, array, size))
15696 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15697 return true;
15699 ggc_free (array);
15702 return false;
15705 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15706 attribute is the const value of T, where T is an integral constant
15707 variable with static storage duration
15708 (so it can't be a PARM_DECL or a RESULT_DECL). */
15710 static bool
15711 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15714 if (!decl
15715 || (TREE_CODE (decl) != VAR_DECL
15716 && TREE_CODE (decl) != CONST_DECL)
15717 || (TREE_CODE (decl) == VAR_DECL
15718 && !TREE_STATIC (decl)))
15719 return false;
15721 if (TREE_READONLY (decl)
15722 && ! TREE_THIS_VOLATILE (decl)
15723 && DECL_INITIAL (decl))
15724 /* OK */;
15725 else
15726 return false;
15728 /* Don't add DW_AT_const_value if abstract origin already has one. */
15729 if (get_AT (var_die, DW_AT_const_value))
15730 return false;
15732 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15735 /* Convert the CFI instructions for the current function into a
15736 location list. This is used for DW_AT_frame_base when we targeting
15737 a dwarf2 consumer that does not support the dwarf3
15738 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15739 expressions. */
15741 static dw_loc_list_ref
15742 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15744 int ix;
15745 dw_fde_ref fde;
15746 dw_loc_list_ref list, *list_tail;
15747 dw_cfi_ref cfi;
15748 dw_cfa_location last_cfa, next_cfa;
15749 const char *start_label, *last_label, *section;
15750 dw_cfa_location remember;
15752 fde = cfun->fde;
15753 gcc_assert (fde != NULL);
15755 section = secname_for_decl (current_function_decl);
15756 list_tail = &list;
15757 list = NULL;
15759 memset (&next_cfa, 0, sizeof (next_cfa));
15760 next_cfa.reg = INVALID_REGNUM;
15761 remember = next_cfa;
15763 start_label = fde->dw_fde_begin;
15765 /* ??? Bald assumption that the CIE opcode list does not contain
15766 advance opcodes. */
15767 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15768 lookup_cfa_1 (cfi, &next_cfa, &remember);
15770 last_cfa = next_cfa;
15771 last_label = start_label;
15773 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15775 /* If the first partition contained no CFI adjustments, the
15776 CIE opcodes apply to the whole first partition. */
15777 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15778 fde->dw_fde_begin, fde->dw_fde_end, section);
15779 list_tail =&(*list_tail)->dw_loc_next;
15780 start_label = last_label = fde->dw_fde_second_begin;
15783 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15785 switch (cfi->dw_cfi_opc)
15787 case DW_CFA_set_loc:
15788 case DW_CFA_advance_loc1:
15789 case DW_CFA_advance_loc2:
15790 case DW_CFA_advance_loc4:
15791 if (!cfa_equal_p (&last_cfa, &next_cfa))
15793 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15794 start_label, last_label, section);
15796 list_tail = &(*list_tail)->dw_loc_next;
15797 last_cfa = next_cfa;
15798 start_label = last_label;
15800 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15801 break;
15803 case DW_CFA_advance_loc:
15804 /* The encoding is complex enough that we should never emit this. */
15805 gcc_unreachable ();
15807 default:
15808 lookup_cfa_1 (cfi, &next_cfa, &remember);
15809 break;
15811 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15813 if (!cfa_equal_p (&last_cfa, &next_cfa))
15815 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15816 start_label, last_label, section);
15818 list_tail = &(*list_tail)->dw_loc_next;
15819 last_cfa = next_cfa;
15820 start_label = last_label;
15822 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15823 start_label, fde->dw_fde_end, section);
15824 list_tail = &(*list_tail)->dw_loc_next;
15825 start_label = last_label = fde->dw_fde_second_begin;
15829 if (!cfa_equal_p (&last_cfa, &next_cfa))
15831 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15832 start_label, last_label, section);
15833 list_tail = &(*list_tail)->dw_loc_next;
15834 start_label = last_label;
15837 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15838 start_label,
15839 fde->dw_fde_second_begin
15840 ? fde->dw_fde_second_end : fde->dw_fde_end,
15841 section);
15843 if (list && list->dw_loc_next)
15844 gen_llsym (list);
15846 return list;
15849 /* Compute a displacement from the "steady-state frame pointer" to the
15850 frame base (often the same as the CFA), and store it in
15851 frame_pointer_fb_offset. OFFSET is added to the displacement
15852 before the latter is negated. */
15854 static void
15855 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15857 rtx reg, elim;
15859 #ifdef FRAME_POINTER_CFA_OFFSET
15860 reg = frame_pointer_rtx;
15861 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15862 #else
15863 reg = arg_pointer_rtx;
15864 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15865 #endif
15867 elim = (ira_use_lra_p
15868 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15869 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15870 if (GET_CODE (elim) == PLUS)
15872 offset += INTVAL (XEXP (elim, 1));
15873 elim = XEXP (elim, 0);
15876 frame_pointer_fb_offset = -offset;
15878 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15879 in which to eliminate. This is because it's stack pointer isn't
15880 directly accessible as a register within the ISA. To work around
15881 this, assume that while we cannot provide a proper value for
15882 frame_pointer_fb_offset, we won't need one either. */
15883 frame_pointer_fb_offset_valid
15884 = ((SUPPORTS_STACK_ALIGNMENT
15885 && (elim == hard_frame_pointer_rtx
15886 || elim == stack_pointer_rtx))
15887 || elim == (frame_pointer_needed
15888 ? hard_frame_pointer_rtx
15889 : stack_pointer_rtx));
15892 /* Generate a DW_AT_name attribute given some string value to be included as
15893 the value of the attribute. */
15895 static void
15896 add_name_attribute (dw_die_ref die, const char *name_string)
15898 if (name_string != NULL && *name_string != 0)
15900 if (demangle_name_func)
15901 name_string = (*demangle_name_func) (name_string);
15903 add_AT_string (die, DW_AT_name, name_string);
15907 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15908 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15909 of TYPE accordingly.
15911 ??? This is a temporary measure until after we're able to generate
15912 regular DWARF for the complex Ada type system. */
15914 static void
15915 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15916 dw_die_ref context_die)
15918 tree dtype;
15919 dw_die_ref dtype_die;
15921 if (!lang_hooks.types.descriptive_type)
15922 return;
15924 dtype = lang_hooks.types.descriptive_type (type);
15925 if (!dtype)
15926 return;
15928 dtype_die = lookup_type_die (dtype);
15929 if (!dtype_die)
15931 gen_type_die (dtype, context_die);
15932 dtype_die = lookup_type_die (dtype);
15933 gcc_assert (dtype_die);
15936 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15939 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
15941 static const char *
15942 comp_dir_string (void)
15944 const char *wd;
15945 char *wd1;
15946 static const char *cached_wd = NULL;
15948 if (cached_wd != NULL)
15949 return cached_wd;
15951 wd = get_src_pwd ();
15952 if (wd == NULL)
15953 return NULL;
15955 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15957 int wdlen;
15959 wdlen = strlen (wd);
15960 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15961 strcpy (wd1, wd);
15962 wd1 [wdlen] = DIR_SEPARATOR;
15963 wd1 [wdlen + 1] = 0;
15964 wd = wd1;
15967 cached_wd = remap_debug_filename (wd);
15968 return cached_wd;
15971 /* Generate a DW_AT_comp_dir attribute for DIE. */
15973 static void
15974 add_comp_dir_attribute (dw_die_ref die)
15976 const char * wd = comp_dir_string ();
15977 if (wd != NULL)
15978 add_AT_string (die, DW_AT_comp_dir, wd);
15981 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15982 default. */
15984 static int
15985 lower_bound_default (void)
15987 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15989 case DW_LANG_C:
15990 case DW_LANG_C89:
15991 case DW_LANG_C99:
15992 case DW_LANG_C_plus_plus:
15993 case DW_LANG_ObjC:
15994 case DW_LANG_ObjC_plus_plus:
15995 case DW_LANG_Java:
15996 return 0;
15997 case DW_LANG_Fortran77:
15998 case DW_LANG_Fortran90:
15999 case DW_LANG_Fortran95:
16000 return 1;
16001 case DW_LANG_UPC:
16002 case DW_LANG_D:
16003 case DW_LANG_Python:
16004 return dwarf_version >= 4 ? 0 : -1;
16005 case DW_LANG_Ada95:
16006 case DW_LANG_Ada83:
16007 case DW_LANG_Cobol74:
16008 case DW_LANG_Cobol85:
16009 case DW_LANG_Pascal83:
16010 case DW_LANG_Modula2:
16011 case DW_LANG_PLI:
16012 return dwarf_version >= 4 ? 1 : -1;
16013 default:
16014 return -1;
16018 /* Given a tree node describing an array bound (either lower or upper) output
16019 a representation for that bound. */
16021 static void
16022 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16024 switch (TREE_CODE (bound))
16026 case ERROR_MARK:
16027 return;
16029 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16030 case INTEGER_CST:
16032 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16033 int dflt;
16035 /* Use the default if possible. */
16036 if (bound_attr == DW_AT_lower_bound
16037 && host_integerp (bound, 0)
16038 && (dflt = lower_bound_default ()) != -1
16039 && tree_low_cst (bound, 0) == dflt)
16042 /* Otherwise represent the bound as an unsigned value with the
16043 precision of its type. The precision and signedness of the
16044 type will be necessary to re-interpret it unambiguously. */
16045 else if (prec < HOST_BITS_PER_WIDE_INT)
16047 unsigned HOST_WIDE_INT mask
16048 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16049 add_AT_unsigned (subrange_die, bound_attr,
16050 TREE_INT_CST_LOW (bound) & mask);
16052 else if (prec == HOST_BITS_PER_WIDE_INT
16053 || TREE_INT_CST_HIGH (bound) == 0)
16054 add_AT_unsigned (subrange_die, bound_attr,
16055 TREE_INT_CST_LOW (bound));
16056 else
16057 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16058 TREE_INT_CST_LOW (bound));
16060 break;
16062 CASE_CONVERT:
16063 case VIEW_CONVERT_EXPR:
16064 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16065 break;
16067 case SAVE_EXPR:
16068 break;
16070 case VAR_DECL:
16071 case PARM_DECL:
16072 case RESULT_DECL:
16074 dw_die_ref decl_die = lookup_decl_die (bound);
16076 /* ??? Can this happen, or should the variable have been bound
16077 first? Probably it can, since I imagine that we try to create
16078 the types of parameters in the order in which they exist in
16079 the list, and won't have created a forward reference to a
16080 later parameter. */
16081 if (decl_die != NULL)
16083 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16084 break;
16087 /* FALLTHRU */
16089 default:
16091 /* Otherwise try to create a stack operation procedure to
16092 evaluate the value of the array bound. */
16094 dw_die_ref ctx, decl_die;
16095 dw_loc_list_ref list;
16097 list = loc_list_from_tree (bound, 2);
16098 if (list == NULL || single_element_loc_list_p (list))
16100 /* If DW_AT_*bound is not a reference nor constant, it is
16101 a DWARF expression rather than location description.
16102 For that loc_list_from_tree (bound, 0) is needed.
16103 If that fails to give a single element list,
16104 fall back to outputting this as a reference anyway. */
16105 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16106 if (list2 && single_element_loc_list_p (list2))
16108 add_AT_loc (subrange_die, bound_attr, list2->expr);
16109 break;
16112 if (list == NULL)
16113 break;
16115 if (current_function_decl == 0)
16116 ctx = comp_unit_die ();
16117 else
16118 ctx = lookup_decl_die (current_function_decl);
16120 decl_die = new_die (DW_TAG_variable, ctx, bound);
16121 add_AT_flag (decl_die, DW_AT_artificial, 1);
16122 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16123 add_AT_location_description (decl_die, DW_AT_location, list);
16124 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16125 break;
16130 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16131 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16132 Note that the block of subscript information for an array type also
16133 includes information about the element type of the given array type. */
16135 static void
16136 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16138 unsigned dimension_number;
16139 tree lower, upper;
16140 dw_die_ref subrange_die;
16142 for (dimension_number = 0;
16143 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16144 type = TREE_TYPE (type), dimension_number++)
16146 tree domain = TYPE_DOMAIN (type);
16148 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16149 break;
16151 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16152 and (in GNU C only) variable bounds. Handle all three forms
16153 here. */
16154 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16155 if (domain)
16157 /* We have an array type with specified bounds. */
16158 lower = TYPE_MIN_VALUE (domain);
16159 upper = TYPE_MAX_VALUE (domain);
16161 /* Define the index type. */
16162 if (TREE_TYPE (domain))
16164 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16165 TREE_TYPE field. We can't emit debug info for this
16166 because it is an unnamed integral type. */
16167 if (TREE_CODE (domain) == INTEGER_TYPE
16168 && TYPE_NAME (domain) == NULL_TREE
16169 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16170 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16172 else
16173 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16174 type_die);
16177 /* ??? If upper is NULL, the array has unspecified length,
16178 but it does have a lower bound. This happens with Fortran
16179 dimension arr(N:*)
16180 Since the debugger is definitely going to need to know N
16181 to produce useful results, go ahead and output the lower
16182 bound solo, and hope the debugger can cope. */
16184 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16185 if (upper)
16186 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16189 /* Otherwise we have an array type with an unspecified length. The
16190 DWARF-2 spec does not say how to handle this; let's just leave out the
16191 bounds. */
16195 static void
16196 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16198 dw_die_ref decl_die;
16199 unsigned size;
16201 switch (TREE_CODE (tree_node))
16203 case ERROR_MARK:
16204 size = 0;
16205 break;
16206 case ENUMERAL_TYPE:
16207 case RECORD_TYPE:
16208 case UNION_TYPE:
16209 case QUAL_UNION_TYPE:
16210 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16211 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16213 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16214 return;
16216 size = int_size_in_bytes (tree_node);
16217 break;
16218 case FIELD_DECL:
16219 /* For a data member of a struct or union, the DW_AT_byte_size is
16220 generally given as the number of bytes normally allocated for an
16221 object of the *declared* type of the member itself. This is true
16222 even for bit-fields. */
16223 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16224 break;
16225 default:
16226 gcc_unreachable ();
16229 /* Note that `size' might be -1 when we get to this point. If it is, that
16230 indicates that the byte size of the entity in question is variable. We
16231 have no good way of expressing this fact in Dwarf at the present time,
16232 so just let the -1 pass on through. */
16233 add_AT_unsigned (die, DW_AT_byte_size, size);
16236 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16237 which specifies the distance in bits from the highest order bit of the
16238 "containing object" for the bit-field to the highest order bit of the
16239 bit-field itself.
16241 For any given bit-field, the "containing object" is a hypothetical object
16242 (of some integral or enum type) within which the given bit-field lives. The
16243 type of this hypothetical "containing object" is always the same as the
16244 declared type of the individual bit-field itself. The determination of the
16245 exact location of the "containing object" for a bit-field is rather
16246 complicated. It's handled by the `field_byte_offset' function (above).
16248 Note that it is the size (in bytes) of the hypothetical "containing object"
16249 which will be given in the DW_AT_byte_size attribute for this bit-field.
16250 (See `byte_size_attribute' above). */
16252 static inline void
16253 add_bit_offset_attribute (dw_die_ref die, tree decl)
16255 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16256 tree type = DECL_BIT_FIELD_TYPE (decl);
16257 HOST_WIDE_INT bitpos_int;
16258 HOST_WIDE_INT highest_order_object_bit_offset;
16259 HOST_WIDE_INT highest_order_field_bit_offset;
16260 HOST_WIDE_INT bit_offset;
16262 /* Must be a field and a bit field. */
16263 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16265 /* We can't yet handle bit-fields whose offsets are variable, so if we
16266 encounter such things, just return without generating any attribute
16267 whatsoever. Likewise for variable or too large size. */
16268 if (! host_integerp (bit_position (decl), 0)
16269 || ! host_integerp (DECL_SIZE (decl), 1))
16270 return;
16272 bitpos_int = int_bit_position (decl);
16274 /* Note that the bit offset is always the distance (in bits) from the
16275 highest-order bit of the "containing object" to the highest-order bit of
16276 the bit-field itself. Since the "high-order end" of any object or field
16277 is different on big-endian and little-endian machines, the computation
16278 below must take account of these differences. */
16279 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16280 highest_order_field_bit_offset = bitpos_int;
16282 if (! BYTES_BIG_ENDIAN)
16284 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16285 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16288 bit_offset
16289 = (! BYTES_BIG_ENDIAN
16290 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16291 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16293 if (bit_offset < 0)
16294 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16295 else
16296 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16299 /* For a FIELD_DECL node which represents a bit field, output an attribute
16300 which specifies the length in bits of the given field. */
16302 static inline void
16303 add_bit_size_attribute (dw_die_ref die, tree decl)
16305 /* Must be a field and a bit field. */
16306 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16307 && DECL_BIT_FIELD_TYPE (decl));
16309 if (host_integerp (DECL_SIZE (decl), 1))
16310 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16313 /* If the compiled language is ANSI C, then add a 'prototyped'
16314 attribute, if arg types are given for the parameters of a function. */
16316 static inline void
16317 add_prototyped_attribute (dw_die_ref die, tree func_type)
16319 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16320 && prototype_p (func_type))
16321 add_AT_flag (die, DW_AT_prototyped, 1);
16324 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16325 by looking in either the type declaration or object declaration
16326 equate table. */
16328 static inline dw_die_ref
16329 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16331 dw_die_ref origin_die = NULL;
16333 if (TREE_CODE (origin) != FUNCTION_DECL)
16335 /* We may have gotten separated from the block for the inlined
16336 function, if we're in an exception handler or some such; make
16337 sure that the abstract function has been written out.
16339 Doing this for nested functions is wrong, however; functions are
16340 distinct units, and our context might not even be inline. */
16341 tree fn = origin;
16343 if (TYPE_P (fn))
16344 fn = TYPE_STUB_DECL (fn);
16346 fn = decl_function_context (fn);
16347 if (fn)
16348 dwarf2out_abstract_function (fn);
16351 if (DECL_P (origin))
16352 origin_die = lookup_decl_die (origin);
16353 else if (TYPE_P (origin))
16354 origin_die = lookup_type_die (origin);
16356 /* XXX: Functions that are never lowered don't always have correct block
16357 trees (in the case of java, they simply have no block tree, in some other
16358 languages). For these functions, there is nothing we can really do to
16359 output correct debug info for inlined functions in all cases. Rather
16360 than die, we'll just produce deficient debug info now, in that we will
16361 have variables without a proper abstract origin. In the future, when all
16362 functions are lowered, we should re-add a gcc_assert (origin_die)
16363 here. */
16365 if (origin_die)
16366 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16367 return origin_die;
16370 /* We do not currently support the pure_virtual attribute. */
16372 static inline void
16373 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16375 if (DECL_VINDEX (func_decl))
16377 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16379 if (host_integerp (DECL_VINDEX (func_decl), 0))
16380 add_AT_loc (die, DW_AT_vtable_elem_location,
16381 new_loc_descr (DW_OP_constu,
16382 tree_low_cst (DECL_VINDEX (func_decl), 0),
16383 0));
16385 /* GNU extension: Record what type this method came from originally. */
16386 if (debug_info_level > DINFO_LEVEL_TERSE
16387 && DECL_CONTEXT (func_decl))
16388 add_AT_die_ref (die, DW_AT_containing_type,
16389 lookup_type_die (DECL_CONTEXT (func_decl)));
16393 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16394 given decl. This used to be a vendor extension until after DWARF 4
16395 standardized it. */
16397 static void
16398 add_linkage_attr (dw_die_ref die, tree decl)
16400 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16402 /* Mimic what assemble_name_raw does with a leading '*'. */
16403 if (name[0] == '*')
16404 name = &name[1];
16406 if (dwarf_version >= 4)
16407 add_AT_string (die, DW_AT_linkage_name, name);
16408 else
16409 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16412 /* Add source coordinate attributes for the given decl. */
16414 static void
16415 add_src_coords_attributes (dw_die_ref die, tree decl)
16417 expanded_location s;
16419 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16420 return;
16421 s = expand_location (DECL_SOURCE_LOCATION (decl));
16422 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16423 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16426 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16428 static void
16429 add_linkage_name (dw_die_ref die, tree decl)
16431 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16432 && TREE_PUBLIC (decl)
16433 && !DECL_ABSTRACT (decl)
16434 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16435 && die->die_tag != DW_TAG_member)
16437 /* Defer until we have an assembler name set. */
16438 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16440 limbo_die_node *asm_name;
16442 asm_name = ggc_alloc_cleared_limbo_die_node ();
16443 asm_name->die = die;
16444 asm_name->created_for = decl;
16445 asm_name->next = deferred_asm_name;
16446 deferred_asm_name = asm_name;
16448 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16449 add_linkage_attr (die, decl);
16453 /* Add a DW_AT_name attribute and source coordinate attribute for the
16454 given decl, but only if it actually has a name. */
16456 static void
16457 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16459 tree decl_name;
16461 decl_name = DECL_NAME (decl);
16462 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16464 const char *name = dwarf2_name (decl, 0);
16465 if (name)
16466 add_name_attribute (die, name);
16467 if (! DECL_ARTIFICIAL (decl))
16468 add_src_coords_attributes (die, decl);
16470 add_linkage_name (die, decl);
16473 #ifdef VMS_DEBUGGING_INFO
16474 /* Get the function's name, as described by its RTL. This may be different
16475 from the DECL_NAME name used in the source file. */
16476 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16478 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16479 XEXP (DECL_RTL (decl), 0), false);
16480 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16482 #endif /* VMS_DEBUGGING_INFO */
16485 #ifdef VMS_DEBUGGING_INFO
16486 /* Output the debug main pointer die for VMS */
16488 void
16489 dwarf2out_vms_debug_main_pointer (void)
16491 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16492 dw_die_ref die;
16494 /* Allocate the VMS debug main subprogram die. */
16495 die = ggc_alloc_cleared_die_node ();
16496 die->die_tag = DW_TAG_subprogram;
16497 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16498 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16499 current_function_funcdef_no);
16500 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16502 /* Make it the first child of comp_unit_die (). */
16503 die->die_parent = comp_unit_die ();
16504 if (comp_unit_die ()->die_child)
16506 die->die_sib = comp_unit_die ()->die_child->die_sib;
16507 comp_unit_die ()->die_child->die_sib = die;
16509 else
16511 die->die_sib = die;
16512 comp_unit_die ()->die_child = die;
16515 #endif /* VMS_DEBUGGING_INFO */
16517 /* Push a new declaration scope. */
16519 static void
16520 push_decl_scope (tree scope)
16522 vec_safe_push (decl_scope_table, scope);
16525 /* Pop a declaration scope. */
16527 static inline void
16528 pop_decl_scope (void)
16530 decl_scope_table->pop ();
16533 /* walk_tree helper function for uses_local_type, below. */
16535 static tree
16536 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16538 if (!TYPE_P (*tp))
16539 *walk_subtrees = 0;
16540 else
16542 tree name = TYPE_NAME (*tp);
16543 if (name && DECL_P (name) && decl_function_context (name))
16544 return *tp;
16546 return NULL_TREE;
16549 /* If TYPE involves a function-local type (including a local typedef to a
16550 non-local type), returns that type; otherwise returns NULL_TREE. */
16552 static tree
16553 uses_local_type (tree type)
16555 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16556 return used;
16559 /* Return the DIE for the scope that immediately contains this type.
16560 Non-named types that do not involve a function-local type get global
16561 scope. Named types nested in namespaces or other types get their
16562 containing scope. All other types (i.e. function-local named types) get
16563 the current active scope. */
16565 static dw_die_ref
16566 scope_die_for (tree t, dw_die_ref context_die)
16568 dw_die_ref scope_die = NULL;
16569 tree containing_scope;
16571 /* Non-types always go in the current scope. */
16572 gcc_assert (TYPE_P (t));
16574 /* Use the scope of the typedef, rather than the scope of the type
16575 it refers to. */
16576 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16577 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16578 else
16579 containing_scope = TYPE_CONTEXT (t);
16581 /* Use the containing namespace if there is one. */
16582 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16584 if (context_die == lookup_decl_die (containing_scope))
16585 /* OK */;
16586 else if (debug_info_level > DINFO_LEVEL_TERSE)
16587 context_die = get_context_die (containing_scope);
16588 else
16589 containing_scope = NULL_TREE;
16592 /* Ignore function type "scopes" from the C frontend. They mean that
16593 a tagged type is local to a parmlist of a function declarator, but
16594 that isn't useful to DWARF. */
16595 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16596 containing_scope = NULL_TREE;
16598 if (SCOPE_FILE_SCOPE_P (containing_scope))
16600 /* If T uses a local type keep it local as well, to avoid references
16601 to function-local DIEs from outside the function. */
16602 if (current_function_decl && uses_local_type (t))
16603 scope_die = context_die;
16604 else
16605 scope_die = comp_unit_die ();
16607 else if (TYPE_P (containing_scope))
16609 /* For types, we can just look up the appropriate DIE. */
16610 if (debug_info_level > DINFO_LEVEL_TERSE)
16611 scope_die = get_context_die (containing_scope);
16612 else
16614 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16615 if (scope_die == NULL)
16616 scope_die = comp_unit_die ();
16619 else
16620 scope_die = context_die;
16622 return scope_die;
16625 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16627 static inline int
16628 local_scope_p (dw_die_ref context_die)
16630 for (; context_die; context_die = context_die->die_parent)
16631 if (context_die->die_tag == DW_TAG_inlined_subroutine
16632 || context_die->die_tag == DW_TAG_subprogram)
16633 return 1;
16635 return 0;
16638 /* Returns nonzero if CONTEXT_DIE is a class. */
16640 static inline int
16641 class_scope_p (dw_die_ref context_die)
16643 return (context_die
16644 && (context_die->die_tag == DW_TAG_structure_type
16645 || context_die->die_tag == DW_TAG_class_type
16646 || context_die->die_tag == DW_TAG_interface_type
16647 || context_die->die_tag == DW_TAG_union_type));
16650 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16651 whether or not to treat a DIE in this context as a declaration. */
16653 static inline int
16654 class_or_namespace_scope_p (dw_die_ref context_die)
16656 return (class_scope_p (context_die)
16657 || (context_die && context_die->die_tag == DW_TAG_namespace));
16660 /* Many forms of DIEs require a "type description" attribute. This
16661 routine locates the proper "type descriptor" die for the type given
16662 by 'type', and adds a DW_AT_type attribute below the given die. */
16664 static void
16665 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16666 int decl_volatile, dw_die_ref context_die)
16668 enum tree_code code = TREE_CODE (type);
16669 dw_die_ref type_die = NULL;
16671 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16672 or fixed-point type, use the inner type. This is because we have no
16673 support for unnamed types in base_type_die. This can happen if this is
16674 an Ada subrange type. Correct solution is emit a subrange type die. */
16675 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16676 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16677 type = TREE_TYPE (type), code = TREE_CODE (type);
16679 if (code == ERROR_MARK
16680 /* Handle a special case. For functions whose return type is void, we
16681 generate *no* type attribute. (Note that no object may have type
16682 `void', so this only applies to function return types). */
16683 || code == VOID_TYPE)
16684 return;
16686 type_die = modified_type_die (type,
16687 decl_const || TYPE_READONLY (type),
16688 decl_volatile || TYPE_VOLATILE (type),
16689 context_die);
16691 if (type_die != NULL)
16692 add_AT_die_ref (object_die, DW_AT_type, type_die);
16695 /* Given an object die, add the calling convention attribute for the
16696 function call type. */
16697 static void
16698 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16700 enum dwarf_calling_convention value = DW_CC_normal;
16702 value = ((enum dwarf_calling_convention)
16703 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16705 if (is_fortran ()
16706 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16708 /* DWARF 2 doesn't provide a way to identify a program's source-level
16709 entry point. DW_AT_calling_convention attributes are only meant
16710 to describe functions' calling conventions. However, lacking a
16711 better way to signal the Fortran main program, we used this for
16712 a long time, following existing custom. Now, DWARF 4 has
16713 DW_AT_main_subprogram, which we add below, but some tools still
16714 rely on the old way, which we thus keep. */
16715 value = DW_CC_program;
16717 if (dwarf_version >= 4 || !dwarf_strict)
16718 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16721 /* Only add the attribute if the backend requests it, and
16722 is not DW_CC_normal. */
16723 if (value && (value != DW_CC_normal))
16724 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16727 /* Given a tree pointer to a struct, class, union, or enum type node, return
16728 a pointer to the (string) tag name for the given type, or zero if the type
16729 was declared without a tag. */
16731 static const char *
16732 type_tag (const_tree type)
16734 const char *name = 0;
16736 if (TYPE_NAME (type) != 0)
16738 tree t = 0;
16740 /* Find the IDENTIFIER_NODE for the type name. */
16741 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16742 && !TYPE_NAMELESS (type))
16743 t = TYPE_NAME (type);
16745 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16746 a TYPE_DECL node, regardless of whether or not a `typedef' was
16747 involved. */
16748 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16749 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16751 /* We want to be extra verbose. Don't call dwarf_name if
16752 DECL_NAME isn't set. The default hook for decl_printable_name
16753 doesn't like that, and in this context it's correct to return
16754 0, instead of "<anonymous>" or the like. */
16755 if (DECL_NAME (TYPE_NAME (type))
16756 && !DECL_NAMELESS (TYPE_NAME (type)))
16757 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16760 /* Now get the name as a string, or invent one. */
16761 if (!name && t != 0)
16762 name = IDENTIFIER_POINTER (t);
16765 return (name == 0 || *name == '\0') ? 0 : name;
16768 /* Return the type associated with a data member, make a special check
16769 for bit field types. */
16771 static inline tree
16772 member_declared_type (const_tree member)
16774 return (DECL_BIT_FIELD_TYPE (member)
16775 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16778 /* Get the decl's label, as described by its RTL. This may be different
16779 from the DECL_NAME name used in the source file. */
16781 #if 0
16782 static const char *
16783 decl_start_label (tree decl)
16785 rtx x;
16786 const char *fnname;
16788 x = DECL_RTL (decl);
16789 gcc_assert (MEM_P (x));
16791 x = XEXP (x, 0);
16792 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16794 fnname = XSTR (x, 0);
16795 return fnname;
16797 #endif
16799 /* These routines generate the internal representation of the DIE's for
16800 the compilation unit. Debugging information is collected by walking
16801 the declaration trees passed in from dwarf2out_decl(). */
16803 static void
16804 gen_array_type_die (tree type, dw_die_ref context_die)
16806 dw_die_ref scope_die = scope_die_for (type, context_die);
16807 dw_die_ref array_die;
16809 /* GNU compilers represent multidimensional array types as sequences of one
16810 dimensional array types whose element types are themselves array types.
16811 We sometimes squish that down to a single array_type DIE with multiple
16812 subscripts in the Dwarf debugging info. The draft Dwarf specification
16813 say that we are allowed to do this kind of compression in C, because
16814 there is no difference between an array of arrays and a multidimensional
16815 array. We don't do this for Ada to remain as close as possible to the
16816 actual representation, which is especially important against the language
16817 flexibilty wrt arrays of variable size. */
16819 bool collapse_nested_arrays = !is_ada ();
16820 tree element_type;
16822 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16823 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16824 if (TYPE_STRING_FLAG (type)
16825 && TREE_CODE (type) == ARRAY_TYPE
16826 && is_fortran ()
16827 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16829 HOST_WIDE_INT size;
16831 array_die = new_die (DW_TAG_string_type, scope_die, type);
16832 add_name_attribute (array_die, type_tag (type));
16833 equate_type_number_to_die (type, array_die);
16834 size = int_size_in_bytes (type);
16835 if (size >= 0)
16836 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16837 else if (TYPE_DOMAIN (type) != NULL_TREE
16838 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16839 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16841 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16842 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16844 size = int_size_in_bytes (TREE_TYPE (szdecl));
16845 if (loc && size > 0)
16847 add_AT_location_description (array_die, DW_AT_string_length, loc);
16848 if (size != DWARF2_ADDR_SIZE)
16849 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16852 return;
16855 array_die = new_die (DW_TAG_array_type, scope_die, type);
16856 add_name_attribute (array_die, type_tag (type));
16857 equate_type_number_to_die (type, array_die);
16859 if (TREE_CODE (type) == VECTOR_TYPE)
16860 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16862 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16863 if (is_fortran ()
16864 && TREE_CODE (type) == ARRAY_TYPE
16865 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16866 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16867 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16869 #if 0
16870 /* We default the array ordering. SDB will probably do
16871 the right things even if DW_AT_ordering is not present. It's not even
16872 an issue until we start to get into multidimensional arrays anyway. If
16873 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16874 then we'll have to put the DW_AT_ordering attribute back in. (But if
16875 and when we find out that we need to put these in, we will only do so
16876 for multidimensional arrays. */
16877 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16878 #endif
16880 if (TREE_CODE (type) == VECTOR_TYPE)
16882 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
16883 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16884 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16885 add_bound_info (subrange_die, DW_AT_upper_bound,
16886 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16888 else
16889 add_subscript_info (array_die, type, collapse_nested_arrays);
16891 /* Add representation of the type of the elements of this array type and
16892 emit the corresponding DIE if we haven't done it already. */
16893 element_type = TREE_TYPE (type);
16894 if (collapse_nested_arrays)
16895 while (TREE_CODE (element_type) == ARRAY_TYPE)
16897 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16898 break;
16899 element_type = TREE_TYPE (element_type);
16902 add_type_attribute (array_die, element_type, 0, 0, context_die);
16904 add_gnat_descriptive_type_attribute (array_die, type, context_die);
16905 if (TYPE_ARTIFICIAL (type))
16906 add_AT_flag (array_die, DW_AT_artificial, 1);
16908 if (get_AT (array_die, DW_AT_name))
16909 add_pubtype (type, array_die);
16912 static dw_loc_descr_ref
16913 descr_info_loc (tree val, tree base_decl)
16915 HOST_WIDE_INT size;
16916 dw_loc_descr_ref loc, loc2;
16917 enum dwarf_location_atom op;
16919 if (val == base_decl)
16920 return new_loc_descr (DW_OP_push_object_address, 0, 0);
16922 switch (TREE_CODE (val))
16924 CASE_CONVERT:
16925 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16926 case VAR_DECL:
16927 return loc_descriptor_from_tree (val, 0);
16928 case INTEGER_CST:
16929 if (host_integerp (val, 0))
16930 return int_loc_descriptor (tree_low_cst (val, 0));
16931 break;
16932 case INDIRECT_REF:
16933 size = int_size_in_bytes (TREE_TYPE (val));
16934 if (size < 0)
16935 break;
16936 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16937 if (!loc)
16938 break;
16939 if (size == DWARF2_ADDR_SIZE)
16940 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16941 else
16942 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16943 return loc;
16944 case POINTER_PLUS_EXPR:
16945 case PLUS_EXPR:
16946 if (host_integerp (TREE_OPERAND (val, 1), 1)
16947 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16948 < 16384)
16950 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16951 if (!loc)
16952 break;
16953 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16955 else
16957 op = DW_OP_plus;
16958 do_binop:
16959 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16960 if (!loc)
16961 break;
16962 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16963 if (!loc2)
16964 break;
16965 add_loc_descr (&loc, loc2);
16966 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16968 return loc;
16969 case MINUS_EXPR:
16970 op = DW_OP_minus;
16971 goto do_binop;
16972 case MULT_EXPR:
16973 op = DW_OP_mul;
16974 goto do_binop;
16975 case EQ_EXPR:
16976 op = DW_OP_eq;
16977 goto do_binop;
16978 case NE_EXPR:
16979 op = DW_OP_ne;
16980 goto do_binop;
16981 default:
16982 break;
16984 return NULL;
16987 static void
16988 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16989 tree val, tree base_decl)
16991 dw_loc_descr_ref loc;
16993 if (host_integerp (val, 0))
16995 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16996 return;
16999 loc = descr_info_loc (val, base_decl);
17000 if (!loc)
17001 return;
17003 add_AT_loc (die, attr, loc);
17006 /* This routine generates DIE for array with hidden descriptor, details
17007 are filled into *info by a langhook. */
17009 static void
17010 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17011 dw_die_ref context_die)
17013 dw_die_ref scope_die = scope_die_for (type, context_die);
17014 dw_die_ref array_die;
17015 int dim;
17017 array_die = new_die (DW_TAG_array_type, scope_die, type);
17018 add_name_attribute (array_die, type_tag (type));
17019 equate_type_number_to_die (type, array_die);
17021 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17022 if (is_fortran ()
17023 && info->ndimensions >= 2)
17024 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17026 if (info->data_location)
17027 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17028 info->base_decl);
17029 if (info->associated)
17030 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17031 info->base_decl);
17032 if (info->allocated)
17033 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17034 info->base_decl);
17036 for (dim = 0; dim < info->ndimensions; dim++)
17038 dw_die_ref subrange_die
17039 = new_die (DW_TAG_subrange_type, array_die, NULL);
17041 if (info->dimen[dim].lower_bound)
17043 /* If it is the default value, omit it. */
17044 int dflt;
17046 if (host_integerp (info->dimen[dim].lower_bound, 0)
17047 && (dflt = lower_bound_default ()) != -1
17048 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
17050 else
17051 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17052 info->dimen[dim].lower_bound,
17053 info->base_decl);
17055 if (info->dimen[dim].upper_bound)
17056 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17057 info->dimen[dim].upper_bound,
17058 info->base_decl);
17059 if (info->dimen[dim].stride)
17060 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17061 info->dimen[dim].stride,
17062 info->base_decl);
17065 gen_type_die (info->element_type, context_die);
17066 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17068 if (get_AT (array_die, DW_AT_name))
17069 add_pubtype (type, array_die);
17072 #if 0
17073 static void
17074 gen_entry_point_die (tree decl, dw_die_ref context_die)
17076 tree origin = decl_ultimate_origin (decl);
17077 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17079 if (origin != NULL)
17080 add_abstract_origin_attribute (decl_die, origin);
17081 else
17083 add_name_and_src_coords_attributes (decl_die, decl);
17084 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17085 0, 0, context_die);
17088 if (DECL_ABSTRACT (decl))
17089 equate_decl_number_to_die (decl, decl_die);
17090 else
17091 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17093 #endif
17095 /* Walk through the list of incomplete types again, trying once more to
17096 emit full debugging info for them. */
17098 static void
17099 retry_incomplete_types (void)
17101 int i;
17103 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17104 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17105 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17108 /* Determine what tag to use for a record type. */
17110 static enum dwarf_tag
17111 record_type_tag (tree type)
17113 if (! lang_hooks.types.classify_record)
17114 return DW_TAG_structure_type;
17116 switch (lang_hooks.types.classify_record (type))
17118 case RECORD_IS_STRUCT:
17119 return DW_TAG_structure_type;
17121 case RECORD_IS_CLASS:
17122 return DW_TAG_class_type;
17124 case RECORD_IS_INTERFACE:
17125 if (dwarf_version >= 3 || !dwarf_strict)
17126 return DW_TAG_interface_type;
17127 return DW_TAG_structure_type;
17129 default:
17130 gcc_unreachable ();
17134 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17135 include all of the information about the enumeration values also. Each
17136 enumerated type name/value is listed as a child of the enumerated type
17137 DIE. */
17139 static dw_die_ref
17140 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17142 dw_die_ref type_die = lookup_type_die (type);
17144 if (type_die == NULL)
17146 type_die = new_die (DW_TAG_enumeration_type,
17147 scope_die_for (type, context_die), type);
17148 equate_type_number_to_die (type, type_die);
17149 add_name_attribute (type_die, type_tag (type));
17150 if (dwarf_version >= 4 || !dwarf_strict)
17152 if (ENUM_IS_SCOPED (type))
17153 add_AT_flag (type_die, DW_AT_enum_class, 1);
17154 if (ENUM_IS_OPAQUE (type))
17155 add_AT_flag (type_die, DW_AT_declaration, 1);
17158 else if (! TYPE_SIZE (type))
17159 return type_die;
17160 else
17161 remove_AT (type_die, DW_AT_declaration);
17163 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17164 given enum type is incomplete, do not generate the DW_AT_byte_size
17165 attribute or the DW_AT_element_list attribute. */
17166 if (TYPE_SIZE (type))
17168 tree link;
17170 TREE_ASM_WRITTEN (type) = 1;
17171 add_byte_size_attribute (type_die, type);
17172 if (TYPE_STUB_DECL (type) != NULL_TREE)
17174 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17175 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17178 /* If the first reference to this type was as the return type of an
17179 inline function, then it may not have a parent. Fix this now. */
17180 if (type_die->die_parent == NULL)
17181 add_child_die (scope_die_for (type, context_die), type_die);
17183 for (link = TYPE_VALUES (type);
17184 link != NULL; link = TREE_CHAIN (link))
17186 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17187 tree value = TREE_VALUE (link);
17189 add_name_attribute (enum_die,
17190 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17192 if (TREE_CODE (value) == CONST_DECL)
17193 value = DECL_INITIAL (value);
17195 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value)))
17196 && (simple_type_size_in_bits (TREE_TYPE (value))
17197 <= HOST_BITS_PER_WIDE_INT || host_integerp (value, 0)))
17198 /* DWARF2 does not provide a way of indicating whether or
17199 not enumeration constants are signed or unsigned. GDB
17200 always assumes the values are signed, so we output all
17201 values as if they were signed. That means that
17202 enumeration constants with very large unsigned values
17203 will appear to have negative values in the debugger.
17205 TODO: the above comment is wrong, DWARF2 does provide
17206 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17207 This should be re-worked to use correct signed/unsigned
17208 int/double tags for all cases, instead of always treating as
17209 signed. */
17210 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17211 else
17212 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17213 that here. */
17214 add_AT_double (enum_die, DW_AT_const_value,
17215 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17218 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17219 if (TYPE_ARTIFICIAL (type))
17220 add_AT_flag (type_die, DW_AT_artificial, 1);
17222 else
17223 add_AT_flag (type_die, DW_AT_declaration, 1);
17225 add_pubtype (type, type_die);
17227 return type_die;
17230 /* Generate a DIE to represent either a real live formal parameter decl or to
17231 represent just the type of some formal parameter position in some function
17232 type.
17234 Note that this routine is a bit unusual because its argument may be a
17235 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17236 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17237 node. If it's the former then this function is being called to output a
17238 DIE to represent a formal parameter object (or some inlining thereof). If
17239 it's the latter, then this function is only being called to output a
17240 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17241 argument type of some subprogram type.
17242 If EMIT_NAME_P is true, name and source coordinate attributes
17243 are emitted. */
17245 static dw_die_ref
17246 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17247 dw_die_ref context_die)
17249 tree node_or_origin = node ? node : origin;
17250 tree ultimate_origin;
17251 dw_die_ref parm_die
17252 = new_die (DW_TAG_formal_parameter, context_die, node);
17254 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17256 case tcc_declaration:
17257 ultimate_origin = decl_ultimate_origin (node_or_origin);
17258 if (node || ultimate_origin)
17259 origin = ultimate_origin;
17260 if (origin != NULL)
17261 add_abstract_origin_attribute (parm_die, origin);
17262 else if (emit_name_p)
17263 add_name_and_src_coords_attributes (parm_die, node);
17264 if (origin == NULL
17265 || (! DECL_ABSTRACT (node_or_origin)
17266 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17267 decl_function_context
17268 (node_or_origin))))
17270 tree type = TREE_TYPE (node_or_origin);
17271 if (decl_by_reference_p (node_or_origin))
17272 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17273 context_die);
17274 else
17275 add_type_attribute (parm_die, type,
17276 TREE_READONLY (node_or_origin),
17277 TREE_THIS_VOLATILE (node_or_origin),
17278 context_die);
17280 if (origin == NULL && DECL_ARTIFICIAL (node))
17281 add_AT_flag (parm_die, DW_AT_artificial, 1);
17283 if (node && node != origin)
17284 equate_decl_number_to_die (node, parm_die);
17285 if (! DECL_ABSTRACT (node_or_origin))
17286 add_location_or_const_value_attribute (parm_die, node_or_origin,
17287 node == NULL, DW_AT_location);
17289 break;
17291 case tcc_type:
17292 /* We were called with some kind of a ..._TYPE node. */
17293 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17294 break;
17296 default:
17297 gcc_unreachable ();
17300 return parm_die;
17303 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17304 children DW_TAG_formal_parameter DIEs representing the arguments of the
17305 parameter pack.
17307 PARM_PACK must be a function parameter pack.
17308 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17309 must point to the subsequent arguments of the function PACK_ARG belongs to.
17310 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17311 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17312 following the last one for which a DIE was generated. */
17314 static dw_die_ref
17315 gen_formal_parameter_pack_die (tree parm_pack,
17316 tree pack_arg,
17317 dw_die_ref subr_die,
17318 tree *next_arg)
17320 tree arg;
17321 dw_die_ref parm_pack_die;
17323 gcc_assert (parm_pack
17324 && lang_hooks.function_parameter_pack_p (parm_pack)
17325 && subr_die);
17327 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17328 add_src_coords_attributes (parm_pack_die, parm_pack);
17330 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17332 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17333 parm_pack))
17334 break;
17335 gen_formal_parameter_die (arg, NULL,
17336 false /* Don't emit name attribute. */,
17337 parm_pack_die);
17339 if (next_arg)
17340 *next_arg = arg;
17341 return parm_pack_die;
17344 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17345 at the end of an (ANSI prototyped) formal parameters list. */
17347 static void
17348 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17350 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17353 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17354 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17355 parameters as specified in some function type specification (except for
17356 those which appear as part of a function *definition*). */
17358 static void
17359 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17361 tree link;
17362 tree formal_type = NULL;
17363 tree first_parm_type;
17364 tree arg;
17366 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17368 arg = DECL_ARGUMENTS (function_or_method_type);
17369 function_or_method_type = TREE_TYPE (function_or_method_type);
17371 else
17372 arg = NULL_TREE;
17374 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17376 /* Make our first pass over the list of formal parameter types and output a
17377 DW_TAG_formal_parameter DIE for each one. */
17378 for (link = first_parm_type; link; )
17380 dw_die_ref parm_die;
17382 formal_type = TREE_VALUE (link);
17383 if (formal_type == void_type_node)
17384 break;
17386 /* Output a (nameless) DIE to represent the formal parameter itself. */
17387 parm_die = gen_formal_parameter_die (formal_type, NULL,
17388 true /* Emit name attribute. */,
17389 context_die);
17390 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17391 && link == first_parm_type)
17393 add_AT_flag (parm_die, DW_AT_artificial, 1);
17394 if (dwarf_version >= 3 || !dwarf_strict)
17395 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17397 else if (arg && DECL_ARTIFICIAL (arg))
17398 add_AT_flag (parm_die, DW_AT_artificial, 1);
17400 link = TREE_CHAIN (link);
17401 if (arg)
17402 arg = DECL_CHAIN (arg);
17405 /* If this function type has an ellipsis, add a
17406 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17407 if (formal_type != void_type_node)
17408 gen_unspecified_parameters_die (function_or_method_type, context_die);
17410 /* Make our second (and final) pass over the list of formal parameter types
17411 and output DIEs to represent those types (as necessary). */
17412 for (link = TYPE_ARG_TYPES (function_or_method_type);
17413 link && TREE_VALUE (link);
17414 link = TREE_CHAIN (link))
17415 gen_type_die (TREE_VALUE (link), context_die);
17418 /* We want to generate the DIE for TYPE so that we can generate the
17419 die for MEMBER, which has been defined; we will need to refer back
17420 to the member declaration nested within TYPE. If we're trying to
17421 generate minimal debug info for TYPE, processing TYPE won't do the
17422 trick; we need to attach the member declaration by hand. */
17424 static void
17425 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17427 gen_type_die (type, context_die);
17429 /* If we're trying to avoid duplicate debug info, we may not have
17430 emitted the member decl for this function. Emit it now. */
17431 if (TYPE_STUB_DECL (type)
17432 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17433 && ! lookup_decl_die (member))
17435 dw_die_ref type_die;
17436 gcc_assert (!decl_ultimate_origin (member));
17438 push_decl_scope (type);
17439 type_die = lookup_type_die_strip_naming_typedef (type);
17440 if (TREE_CODE (member) == FUNCTION_DECL)
17441 gen_subprogram_die (member, type_die);
17442 else if (TREE_CODE (member) == FIELD_DECL)
17444 /* Ignore the nameless fields that are used to skip bits but handle
17445 C++ anonymous unions and structs. */
17446 if (DECL_NAME (member) != NULL_TREE
17447 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17448 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17450 gen_type_die (member_declared_type (member), type_die);
17451 gen_field_die (member, type_die);
17454 else
17455 gen_variable_die (member, NULL_TREE, type_die);
17457 pop_decl_scope ();
17461 /* Forward declare these functions, because they are mutually recursive
17462 with their set_block_* pairing functions. */
17463 static void set_decl_origin_self (tree);
17464 static void set_decl_abstract_flags (tree, int);
17466 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17467 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17468 that it points to the node itself, thus indicating that the node is its
17469 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17470 the given node is NULL, recursively descend the decl/block tree which
17471 it is the root of, and for each other ..._DECL or BLOCK node contained
17472 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17473 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17474 values to point to themselves. */
17476 static void
17477 set_block_origin_self (tree stmt)
17479 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17481 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17484 tree local_decl;
17486 for (local_decl = BLOCK_VARS (stmt);
17487 local_decl != NULL_TREE;
17488 local_decl = DECL_CHAIN (local_decl))
17489 if (! DECL_EXTERNAL (local_decl))
17490 set_decl_origin_self (local_decl); /* Potential recursion. */
17494 tree subblock;
17496 for (subblock = BLOCK_SUBBLOCKS (stmt);
17497 subblock != NULL_TREE;
17498 subblock = BLOCK_CHAIN (subblock))
17499 set_block_origin_self (subblock); /* Recurse. */
17504 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17505 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17506 node to so that it points to the node itself, thus indicating that the
17507 node represents its own (abstract) origin. Additionally, if the
17508 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17509 the decl/block tree of which the given node is the root of, and for
17510 each other ..._DECL or BLOCK node contained therein whose
17511 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17512 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17513 point to themselves. */
17515 static void
17516 set_decl_origin_self (tree decl)
17518 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17520 DECL_ABSTRACT_ORIGIN (decl) = decl;
17521 if (TREE_CODE (decl) == FUNCTION_DECL)
17523 tree arg;
17525 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17526 DECL_ABSTRACT_ORIGIN (arg) = arg;
17527 if (DECL_INITIAL (decl) != NULL_TREE
17528 && DECL_INITIAL (decl) != error_mark_node)
17529 set_block_origin_self (DECL_INITIAL (decl));
17534 /* Given a pointer to some BLOCK node, and a boolean value to set the
17535 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17536 the given block, and for all local decls and all local sub-blocks
17537 (recursively) which are contained therein. */
17539 static void
17540 set_block_abstract_flags (tree stmt, int setting)
17542 tree local_decl;
17543 tree subblock;
17544 unsigned int i;
17546 BLOCK_ABSTRACT (stmt) = setting;
17548 for (local_decl = BLOCK_VARS (stmt);
17549 local_decl != NULL_TREE;
17550 local_decl = DECL_CHAIN (local_decl))
17551 if (! DECL_EXTERNAL (local_decl))
17552 set_decl_abstract_flags (local_decl, setting);
17554 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17556 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17557 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17558 || TREE_CODE (local_decl) == PARM_DECL)
17559 set_decl_abstract_flags (local_decl, setting);
17562 for (subblock = BLOCK_SUBBLOCKS (stmt);
17563 subblock != NULL_TREE;
17564 subblock = BLOCK_CHAIN (subblock))
17565 set_block_abstract_flags (subblock, setting);
17568 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17569 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17570 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17571 set the abstract flags for all of the parameters, local vars, local
17572 blocks and sub-blocks (recursively) to the same setting. */
17574 static void
17575 set_decl_abstract_flags (tree decl, int setting)
17577 DECL_ABSTRACT (decl) = setting;
17578 if (TREE_CODE (decl) == FUNCTION_DECL)
17580 tree arg;
17582 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17583 DECL_ABSTRACT (arg) = setting;
17584 if (DECL_INITIAL (decl) != NULL_TREE
17585 && DECL_INITIAL (decl) != error_mark_node)
17586 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17590 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17591 may later generate inlined and/or out-of-line instances of. */
17593 static void
17594 dwarf2out_abstract_function (tree decl)
17596 dw_die_ref old_die;
17597 tree save_fn;
17598 tree context;
17599 int was_abstract;
17600 htab_t old_decl_loc_table;
17601 htab_t old_cached_dw_loc_list_table;
17602 int old_call_site_count, old_tail_call_site_count;
17603 struct call_arg_loc_node *old_call_arg_locations;
17605 /* Make sure we have the actual abstract inline, not a clone. */
17606 decl = DECL_ORIGIN (decl);
17608 old_die = lookup_decl_die (decl);
17609 if (old_die && get_AT (old_die, DW_AT_inline))
17610 /* We've already generated the abstract instance. */
17611 return;
17613 /* We can be called while recursively when seeing block defining inlined subroutine
17614 DIE. Be sure to not clobber the outer location table nor use it or we would
17615 get locations in abstract instantces. */
17616 old_decl_loc_table = decl_loc_table;
17617 decl_loc_table = NULL;
17618 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17619 cached_dw_loc_list_table = NULL;
17620 old_call_arg_locations = call_arg_locations;
17621 call_arg_locations = NULL;
17622 old_call_site_count = call_site_count;
17623 call_site_count = -1;
17624 old_tail_call_site_count = tail_call_site_count;
17625 tail_call_site_count = -1;
17627 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17628 we don't get confused by DECL_ABSTRACT. */
17629 if (debug_info_level > DINFO_LEVEL_TERSE)
17631 context = decl_class_context (decl);
17632 if (context)
17633 gen_type_die_for_member
17634 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17637 /* Pretend we've just finished compiling this function. */
17638 save_fn = current_function_decl;
17639 current_function_decl = decl;
17641 was_abstract = DECL_ABSTRACT (decl);
17642 set_decl_abstract_flags (decl, 1);
17643 dwarf2out_decl (decl);
17644 if (! was_abstract)
17645 set_decl_abstract_flags (decl, 0);
17647 current_function_decl = save_fn;
17648 decl_loc_table = old_decl_loc_table;
17649 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17650 call_arg_locations = old_call_arg_locations;
17651 call_site_count = old_call_site_count;
17652 tail_call_site_count = old_tail_call_site_count;
17655 /* Helper function of premark_used_types() which gets called through
17656 htab_traverse.
17658 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17659 marked as unused by prune_unused_types. */
17661 static int
17662 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17664 tree type;
17665 dw_die_ref die;
17667 type = (tree) *slot;
17668 die = lookup_type_die (type);
17669 if (die != NULL)
17670 die->die_perennial_p = 1;
17671 return 1;
17674 /* Helper function of premark_types_used_by_global_vars which gets called
17675 through htab_traverse.
17677 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17678 marked as unused by prune_unused_types. The DIE of the type is marked
17679 only if the global variable using the type will actually be emitted. */
17681 static int
17682 premark_types_used_by_global_vars_helper (void **slot,
17683 void *data ATTRIBUTE_UNUSED)
17685 struct types_used_by_vars_entry *entry;
17686 dw_die_ref die;
17688 entry = (struct types_used_by_vars_entry *) *slot;
17689 gcc_assert (entry->type != NULL
17690 && entry->var_decl != NULL);
17691 die = lookup_type_die (entry->type);
17692 if (die)
17694 /* Ask cgraph if the global variable really is to be emitted.
17695 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17696 struct varpool_node *node = varpool_get_node (entry->var_decl);
17697 if (node && node->symbol.definition)
17699 die->die_perennial_p = 1;
17700 /* Keep the parent DIEs as well. */
17701 while ((die = die->die_parent) && die->die_perennial_p == 0)
17702 die->die_perennial_p = 1;
17705 return 1;
17708 /* Mark all members of used_types_hash as perennial. */
17710 static void
17711 premark_used_types (struct function *fun)
17713 if (fun && fun->used_types_hash)
17714 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17717 /* Mark all members of types_used_by_vars_entry as perennial. */
17719 static void
17720 premark_types_used_by_global_vars (void)
17722 if (types_used_by_vars_hash)
17723 htab_traverse (types_used_by_vars_hash,
17724 premark_types_used_by_global_vars_helper, NULL);
17727 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17728 for CA_LOC call arg loc node. */
17730 static dw_die_ref
17731 gen_call_site_die (tree decl, dw_die_ref subr_die,
17732 struct call_arg_loc_node *ca_loc)
17734 dw_die_ref stmt_die = NULL, die;
17735 tree block = ca_loc->block;
17737 while (block
17738 && block != DECL_INITIAL (decl)
17739 && TREE_CODE (block) == BLOCK)
17741 if (block_map.length () > BLOCK_NUMBER (block))
17742 stmt_die = block_map[BLOCK_NUMBER (block)];
17743 if (stmt_die)
17744 break;
17745 block = BLOCK_SUPERCONTEXT (block);
17747 if (stmt_die == NULL)
17748 stmt_die = subr_die;
17749 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17750 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17751 if (ca_loc->tail_call_p)
17752 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17753 if (ca_loc->symbol_ref)
17755 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17756 if (tdie)
17757 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17758 else
17759 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17761 return die;
17764 /* Generate a DIE to represent a declared function (either file-scope or
17765 block-local). */
17767 static void
17768 gen_subprogram_die (tree decl, dw_die_ref context_die)
17770 tree origin = decl_ultimate_origin (decl);
17771 dw_die_ref subr_die;
17772 tree outer_scope;
17773 dw_die_ref old_die = lookup_decl_die (decl);
17774 int declaration = (current_function_decl != decl
17775 || class_or_namespace_scope_p (context_die));
17777 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17779 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17780 started to generate the abstract instance of an inline, decided to output
17781 its containing class, and proceeded to emit the declaration of the inline
17782 from the member list for the class. If so, DECLARATION takes priority;
17783 we'll get back to the abstract instance when done with the class. */
17785 /* The class-scope declaration DIE must be the primary DIE. */
17786 if (origin && declaration && class_or_namespace_scope_p (context_die))
17788 origin = NULL;
17789 gcc_assert (!old_die);
17792 /* Now that the C++ front end lazily declares artificial member fns, we
17793 might need to retrofit the declaration into its class. */
17794 if (!declaration && !origin && !old_die
17795 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17796 && !class_or_namespace_scope_p (context_die)
17797 && debug_info_level > DINFO_LEVEL_TERSE)
17798 old_die = force_decl_die (decl);
17800 if (origin != NULL)
17802 gcc_assert (!declaration || local_scope_p (context_die));
17804 /* Fixup die_parent for the abstract instance of a nested
17805 inline function. */
17806 if (old_die && old_die->die_parent == NULL)
17807 add_child_die (context_die, old_die);
17809 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17810 add_abstract_origin_attribute (subr_die, origin);
17811 /* This is where the actual code for a cloned function is.
17812 Let's emit linkage name attribute for it. This helps
17813 debuggers to e.g, set breakpoints into
17814 constructors/destructors when the user asks "break
17815 K::K". */
17816 add_linkage_name (subr_die, decl);
17818 else if (old_die)
17820 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17821 struct dwarf_file_data * file_index = lookup_filename (s.file);
17823 if (!get_AT_flag (old_die, DW_AT_declaration)
17824 /* We can have a normal definition following an inline one in the
17825 case of redefinition of GNU C extern inlines.
17826 It seems reasonable to use AT_specification in this case. */
17827 && !get_AT (old_die, DW_AT_inline))
17829 /* Detect and ignore this case, where we are trying to output
17830 something we have already output. */
17831 return;
17834 /* If the definition comes from the same place as the declaration,
17835 maybe use the old DIE. We always want the DIE for this function
17836 that has the *_pc attributes to be under comp_unit_die so the
17837 debugger can find it. We also need to do this for abstract
17838 instances of inlines, since the spec requires the out-of-line copy
17839 to have the same parent. For local class methods, this doesn't
17840 apply; we just use the old DIE. */
17841 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17842 && (DECL_ARTIFICIAL (decl)
17843 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17844 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17845 == (unsigned) s.line))))
17847 subr_die = old_die;
17849 /* Clear out the declaration attribute and the formal parameters.
17850 Do not remove all children, because it is possible that this
17851 declaration die was forced using force_decl_die(). In such
17852 cases die that forced declaration die (e.g. TAG_imported_module)
17853 is one of the children that we do not want to remove. */
17854 remove_AT (subr_die, DW_AT_declaration);
17855 remove_AT (subr_die, DW_AT_object_pointer);
17856 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17858 else
17860 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17861 add_AT_specification (subr_die, old_die);
17862 add_pubname (decl, subr_die);
17863 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17864 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17865 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17866 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17869 else
17871 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17873 if (TREE_PUBLIC (decl))
17874 add_AT_flag (subr_die, DW_AT_external, 1);
17876 add_name_and_src_coords_attributes (subr_die, decl);
17877 add_pubname (decl, subr_die);
17878 if (debug_info_level > DINFO_LEVEL_TERSE)
17880 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17881 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17882 0, 0, context_die);
17885 add_pure_or_virtual_attribute (subr_die, decl);
17886 if (DECL_ARTIFICIAL (decl))
17887 add_AT_flag (subr_die, DW_AT_artificial, 1);
17889 add_accessibility_attribute (subr_die, decl);
17892 if (declaration)
17894 if (!old_die || !get_AT (old_die, DW_AT_inline))
17896 add_AT_flag (subr_die, DW_AT_declaration, 1);
17898 /* If this is an explicit function declaration then generate
17899 a DW_AT_explicit attribute. */
17900 if (lang_hooks.decls.function_decl_explicit_p (decl)
17901 && (dwarf_version >= 3 || !dwarf_strict))
17902 add_AT_flag (subr_die, DW_AT_explicit, 1);
17904 /* The first time we see a member function, it is in the context of
17905 the class to which it belongs. We make sure of this by emitting
17906 the class first. The next time is the definition, which is
17907 handled above. The two may come from the same source text.
17909 Note that force_decl_die() forces function declaration die. It is
17910 later reused to represent definition. */
17911 equate_decl_number_to_die (decl, subr_die);
17914 else if (DECL_ABSTRACT (decl))
17916 if (DECL_DECLARED_INLINE_P (decl))
17918 if (cgraph_function_possibly_inlined_p (decl))
17919 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17920 else
17921 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17923 else
17925 if (cgraph_function_possibly_inlined_p (decl))
17926 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17927 else
17928 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17931 if (DECL_DECLARED_INLINE_P (decl)
17932 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17933 add_AT_flag (subr_die, DW_AT_artificial, 1);
17935 equate_decl_number_to_die (decl, subr_die);
17937 else if (!DECL_EXTERNAL (decl))
17939 HOST_WIDE_INT cfa_fb_offset;
17940 struct function *fun = DECL_STRUCT_FUNCTION (decl);
17942 if (!old_die || !get_AT (old_die, DW_AT_inline))
17943 equate_decl_number_to_die (decl, subr_die);
17945 gcc_checking_assert (fun);
17946 if (!flag_reorder_blocks_and_partition)
17948 dw_fde_ref fde = fun->fde;
17949 if (fde->dw_fde_begin)
17951 /* We have already generated the labels. */
17952 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17953 fde->dw_fde_end, false);
17955 else
17957 /* Create start/end labels and add the range. */
17958 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17959 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17960 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17961 current_function_funcdef_no);
17962 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17963 current_function_funcdef_no);
17964 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17965 false);
17968 #if VMS_DEBUGGING_INFO
17969 /* HP OpenVMS Industry Standard 64: DWARF Extensions
17970 Section 2.3 Prologue and Epilogue Attributes:
17971 When a breakpoint is set on entry to a function, it is generally
17972 desirable for execution to be suspended, not on the very first
17973 instruction of the function, but rather at a point after the
17974 function's frame has been set up, after any language defined local
17975 declaration processing has been completed, and before execution of
17976 the first statement of the function begins. Debuggers generally
17977 cannot properly determine where this point is. Similarly for a
17978 breakpoint set on exit from a function. The prologue and epilogue
17979 attributes allow a compiler to communicate the location(s) to use. */
17982 if (fde->dw_fde_vms_end_prologue)
17983 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17984 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17986 if (fde->dw_fde_vms_begin_epilogue)
17987 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17988 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17990 #endif
17993 else
17995 /* Generate pubnames entries for the split function code ranges. */
17996 dw_fde_ref fde = fun->fde;
17998 if (fde->dw_fde_second_begin)
18000 if (dwarf_version >= 3 || !dwarf_strict)
18002 /* We should use ranges for non-contiguous code section
18003 addresses. Use the actual code range for the initial
18004 section, since the HOT/COLD labels might precede an
18005 alignment offset. */
18006 bool range_list_added = false;
18007 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18008 fde->dw_fde_end, &range_list_added,
18009 false);
18010 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18011 fde->dw_fde_second_end,
18012 &range_list_added, false);
18013 if (range_list_added)
18014 add_ranges (NULL);
18016 else
18018 /* There is no real support in DW2 for this .. so we make
18019 a work-around. First, emit the pub name for the segment
18020 containing the function label. Then make and emit a
18021 simplified subprogram DIE for the second segment with the
18022 name pre-fixed by __hot/cold_sect_of_. We use the same
18023 linkage name for the second die so that gdb will find both
18024 sections when given "b foo". */
18025 const char *name = NULL;
18026 tree decl_name = DECL_NAME (decl);
18027 dw_die_ref seg_die;
18029 /* Do the 'primary' section. */
18030 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18031 fde->dw_fde_end, false);
18033 /* Build a minimal DIE for the secondary section. */
18034 seg_die = new_die (DW_TAG_subprogram,
18035 subr_die->die_parent, decl);
18037 if (TREE_PUBLIC (decl))
18038 add_AT_flag (seg_die, DW_AT_external, 1);
18040 if (decl_name != NULL
18041 && IDENTIFIER_POINTER (decl_name) != NULL)
18043 name = dwarf2_name (decl, 1);
18044 if (! DECL_ARTIFICIAL (decl))
18045 add_src_coords_attributes (seg_die, decl);
18047 add_linkage_name (seg_die, decl);
18049 gcc_assert (name != NULL);
18050 add_pure_or_virtual_attribute (seg_die, decl);
18051 if (DECL_ARTIFICIAL (decl))
18052 add_AT_flag (seg_die, DW_AT_artificial, 1);
18054 name = concat ("__second_sect_of_", name, NULL);
18055 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18056 fde->dw_fde_second_end, false);
18057 add_name_attribute (seg_die, name);
18058 if (want_pubnames ())
18059 add_pubname_string (name, seg_die);
18062 else
18063 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18064 false);
18067 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18069 /* We define the "frame base" as the function's CFA. This is more
18070 convenient for several reasons: (1) It's stable across the prologue
18071 and epilogue, which makes it better than just a frame pointer,
18072 (2) With dwarf3, there exists a one-byte encoding that allows us
18073 to reference the .debug_frame data by proxy, but failing that,
18074 (3) We can at least reuse the code inspection and interpretation
18075 code that determines the CFA position at various points in the
18076 function. */
18077 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18079 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18080 add_AT_loc (subr_die, DW_AT_frame_base, op);
18082 else
18084 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18085 if (list->dw_loc_next)
18086 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18087 else
18088 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18091 /* Compute a displacement from the "steady-state frame pointer" to
18092 the CFA. The former is what all stack slots and argument slots
18093 will reference in the rtl; the latter is what we've told the
18094 debugger about. We'll need to adjust all frame_base references
18095 by this displacement. */
18096 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18098 if (fun->static_chain_decl)
18099 add_AT_location_description (subr_die, DW_AT_static_link,
18100 loc_list_from_tree (fun->static_chain_decl, 2));
18103 /* Generate child dies for template paramaters. */
18104 if (debug_info_level > DINFO_LEVEL_TERSE)
18105 gen_generic_params_dies (decl);
18107 /* Now output descriptions of the arguments for this function. This gets
18108 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18109 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18110 `...' at the end of the formal parameter list. In order to find out if
18111 there was a trailing ellipsis or not, we must instead look at the type
18112 associated with the FUNCTION_DECL. This will be a node of type
18113 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18114 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18115 an ellipsis at the end. */
18117 /* In the case where we are describing a mere function declaration, all we
18118 need to do here (and all we *can* do here) is to describe the *types* of
18119 its formal parameters. */
18120 if (debug_info_level <= DINFO_LEVEL_TERSE)
18122 else if (declaration)
18123 gen_formal_types_die (decl, subr_die);
18124 else
18126 /* Generate DIEs to represent all known formal parameters. */
18127 tree parm = DECL_ARGUMENTS (decl);
18128 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18129 tree generic_decl_parm = generic_decl
18130 ? DECL_ARGUMENTS (generic_decl)
18131 : NULL;
18133 /* Now we want to walk the list of parameters of the function and
18134 emit their relevant DIEs.
18136 We consider the case of DECL being an instance of a generic function
18137 as well as it being a normal function.
18139 If DECL is an instance of a generic function we walk the
18140 parameters of the generic function declaration _and_ the parameters of
18141 DECL itself. This is useful because we want to emit specific DIEs for
18142 function parameter packs and those are declared as part of the
18143 generic function declaration. In that particular case,
18144 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18145 That DIE has children DIEs representing the set of arguments
18146 of the pack. Note that the set of pack arguments can be empty.
18147 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18148 children DIE.
18150 Otherwise, we just consider the parameters of DECL. */
18151 while (generic_decl_parm || parm)
18153 if (generic_decl_parm
18154 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18155 gen_formal_parameter_pack_die (generic_decl_parm,
18156 parm, subr_die,
18157 &parm);
18158 else if (parm)
18160 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18162 if (parm == DECL_ARGUMENTS (decl)
18163 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18164 && parm_die
18165 && (dwarf_version >= 3 || !dwarf_strict))
18166 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18168 parm = DECL_CHAIN (parm);
18171 if (generic_decl_parm)
18172 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18175 /* Decide whether we need an unspecified_parameters DIE at the end.
18176 There are 2 more cases to do this for: 1) the ansi ... declaration -
18177 this is detectable when the end of the arg list is not a
18178 void_type_node 2) an unprototyped function declaration (not a
18179 definition). This just means that we have no info about the
18180 parameters at all. */
18181 if (prototype_p (TREE_TYPE (decl)))
18183 /* This is the prototyped case, check for.... */
18184 if (stdarg_p (TREE_TYPE (decl)))
18185 gen_unspecified_parameters_die (decl, subr_die);
18187 else if (DECL_INITIAL (decl) == NULL_TREE)
18188 gen_unspecified_parameters_die (decl, subr_die);
18191 /* Output Dwarf info for all of the stuff within the body of the function
18192 (if it has one - it may be just a declaration). */
18193 outer_scope = DECL_INITIAL (decl);
18195 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18196 a function. This BLOCK actually represents the outermost binding contour
18197 for the function, i.e. the contour in which the function's formal
18198 parameters and labels get declared. Curiously, it appears that the front
18199 end doesn't actually put the PARM_DECL nodes for the current function onto
18200 the BLOCK_VARS list for this outer scope, but are strung off of the
18201 DECL_ARGUMENTS list for the function instead.
18203 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18204 the LABEL_DECL nodes for the function however, and we output DWARF info
18205 for those in decls_for_scope. Just within the `outer_scope' there will be
18206 a BLOCK node representing the function's outermost pair of curly braces,
18207 and any blocks used for the base and member initializers of a C++
18208 constructor function. */
18209 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18211 int call_site_note_count = 0;
18212 int tail_call_site_note_count = 0;
18214 /* Emit a DW_TAG_variable DIE for a named return value. */
18215 if (DECL_NAME (DECL_RESULT (decl)))
18216 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18218 current_function_has_inlines = 0;
18219 decls_for_scope (outer_scope, subr_die, 0);
18221 if (call_arg_locations && !dwarf_strict)
18223 struct call_arg_loc_node *ca_loc;
18224 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18226 dw_die_ref die = NULL;
18227 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18228 rtx arg, next_arg;
18230 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18231 arg; arg = next_arg)
18233 dw_loc_descr_ref reg, val;
18234 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18235 dw_die_ref cdie, tdie = NULL;
18237 next_arg = XEXP (arg, 1);
18238 if (REG_P (XEXP (XEXP (arg, 0), 0))
18239 && next_arg
18240 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18241 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18242 && REGNO (XEXP (XEXP (arg, 0), 0))
18243 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18244 next_arg = XEXP (next_arg, 1);
18245 if (mode == VOIDmode)
18247 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18248 if (mode == VOIDmode)
18249 mode = GET_MODE (XEXP (arg, 0));
18251 if (mode == VOIDmode || mode == BLKmode)
18252 continue;
18253 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18255 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18256 tloc = XEXP (XEXP (arg, 0), 1);
18257 continue;
18259 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18260 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18262 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18263 tlocc = XEXP (XEXP (arg, 0), 1);
18264 continue;
18266 reg = NULL;
18267 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18268 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18269 VAR_INIT_STATUS_INITIALIZED);
18270 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18272 rtx mem = XEXP (XEXP (arg, 0), 0);
18273 reg = mem_loc_descriptor (XEXP (mem, 0),
18274 get_address_mode (mem),
18275 GET_MODE (mem),
18276 VAR_INIT_STATUS_INITIALIZED);
18278 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18279 == DEBUG_PARAMETER_REF)
18281 tree tdecl
18282 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18283 tdie = lookup_decl_die (tdecl);
18284 if (tdie == NULL)
18285 continue;
18287 else
18288 continue;
18289 if (reg == NULL
18290 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18291 != DEBUG_PARAMETER_REF)
18292 continue;
18293 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18294 VOIDmode,
18295 VAR_INIT_STATUS_INITIALIZED);
18296 if (val == NULL)
18297 continue;
18298 if (die == NULL)
18299 die = gen_call_site_die (decl, subr_die, ca_loc);
18300 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18301 NULL_TREE);
18302 if (reg != NULL)
18303 add_AT_loc (cdie, DW_AT_location, reg);
18304 else if (tdie != NULL)
18305 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18306 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18307 if (next_arg != XEXP (arg, 1))
18309 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18310 if (mode == VOIDmode)
18311 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18312 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18313 0), 1),
18314 mode, VOIDmode,
18315 VAR_INIT_STATUS_INITIALIZED);
18316 if (val != NULL)
18317 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18320 if (die == NULL
18321 && (ca_loc->symbol_ref || tloc))
18322 die = gen_call_site_die (decl, subr_die, ca_loc);
18323 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18325 dw_loc_descr_ref tval = NULL;
18327 if (tloc != NULL_RTX)
18328 tval = mem_loc_descriptor (tloc,
18329 GET_MODE (tloc) == VOIDmode
18330 ? Pmode : GET_MODE (tloc),
18331 VOIDmode,
18332 VAR_INIT_STATUS_INITIALIZED);
18333 if (tval)
18334 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18335 else if (tlocc != NULL_RTX)
18337 tval = mem_loc_descriptor (tlocc,
18338 GET_MODE (tlocc) == VOIDmode
18339 ? Pmode : GET_MODE (tlocc),
18340 VOIDmode,
18341 VAR_INIT_STATUS_INITIALIZED);
18342 if (tval)
18343 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18344 tval);
18347 if (die != NULL)
18349 call_site_note_count++;
18350 if (ca_loc->tail_call_p)
18351 tail_call_site_note_count++;
18355 call_arg_locations = NULL;
18356 call_arg_loc_last = NULL;
18357 if (tail_call_site_count >= 0
18358 && tail_call_site_count == tail_call_site_note_count
18359 && !dwarf_strict)
18361 if (call_site_count >= 0
18362 && call_site_count == call_site_note_count)
18363 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18364 else
18365 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18367 call_site_count = -1;
18368 tail_call_site_count = -1;
18370 /* Add the calling convention attribute if requested. */
18371 add_calling_convention_attribute (subr_die, decl);
18375 /* Returns a hash value for X (which really is a die_struct). */
18377 static hashval_t
18378 common_block_die_table_hash (const void *x)
18380 const_dw_die_ref d = (const_dw_die_ref) x;
18381 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18384 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18385 as decl_id and die_parent of die_struct Y. */
18387 static int
18388 common_block_die_table_eq (const void *x, const void *y)
18390 const_dw_die_ref d = (const_dw_die_ref) x;
18391 const_dw_die_ref e = (const_dw_die_ref) y;
18392 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18395 /* Generate a DIE to represent a declared data object.
18396 Either DECL or ORIGIN must be non-null. */
18398 static void
18399 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18401 HOST_WIDE_INT off = 0;
18402 tree com_decl;
18403 tree decl_or_origin = decl ? decl : origin;
18404 tree ultimate_origin;
18405 dw_die_ref var_die;
18406 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18407 dw_die_ref origin_die;
18408 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18409 || class_or_namespace_scope_p (context_die));
18410 bool specialization_p = false;
18412 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18413 if (decl || ultimate_origin)
18414 origin = ultimate_origin;
18415 com_decl = fortran_common (decl_or_origin, &off);
18417 /* Symbol in common gets emitted as a child of the common block, in the form
18418 of a data member. */
18419 if (com_decl)
18421 dw_die_ref com_die;
18422 dw_loc_list_ref loc;
18423 die_node com_die_arg;
18425 var_die = lookup_decl_die (decl_or_origin);
18426 if (var_die)
18428 if (get_AT (var_die, DW_AT_location) == NULL)
18430 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18431 if (loc)
18433 if (off)
18435 /* Optimize the common case. */
18436 if (single_element_loc_list_p (loc)
18437 && loc->expr->dw_loc_opc == DW_OP_addr
18438 && loc->expr->dw_loc_next == NULL
18439 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18440 == SYMBOL_REF)
18442 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18443 loc->expr->dw_loc_oprnd1.v.val_addr
18444 = plus_constant (GET_MODE (x), x , off);
18446 else
18447 loc_list_plus_const (loc, off);
18449 add_AT_location_description (var_die, DW_AT_location, loc);
18450 remove_AT (var_die, DW_AT_declaration);
18453 return;
18456 if (common_block_die_table == NULL)
18457 common_block_die_table
18458 = htab_create_ggc (10, common_block_die_table_hash,
18459 common_block_die_table_eq, NULL);
18461 com_die_arg.decl_id = DECL_UID (com_decl);
18462 com_die_arg.die_parent = context_die;
18463 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18464 loc = loc_list_from_tree (com_decl, 2);
18465 if (com_die == NULL)
18467 const char *cnam
18468 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18469 void **slot;
18471 com_die = new_die (DW_TAG_common_block, context_die, decl);
18472 add_name_and_src_coords_attributes (com_die, com_decl);
18473 if (loc)
18475 add_AT_location_description (com_die, DW_AT_location, loc);
18476 /* Avoid sharing the same loc descriptor between
18477 DW_TAG_common_block and DW_TAG_variable. */
18478 loc = loc_list_from_tree (com_decl, 2);
18480 else if (DECL_EXTERNAL (decl))
18481 add_AT_flag (com_die, DW_AT_declaration, 1);
18482 if (want_pubnames ())
18483 add_pubname_string (cnam, com_die); /* ??? needed? */
18484 com_die->decl_id = DECL_UID (com_decl);
18485 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18486 *slot = (void *) com_die;
18488 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18490 add_AT_location_description (com_die, DW_AT_location, loc);
18491 loc = loc_list_from_tree (com_decl, 2);
18492 remove_AT (com_die, DW_AT_declaration);
18494 var_die = new_die (DW_TAG_variable, com_die, decl);
18495 add_name_and_src_coords_attributes (var_die, decl);
18496 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18497 TREE_THIS_VOLATILE (decl), context_die);
18498 add_AT_flag (var_die, DW_AT_external, 1);
18499 if (loc)
18501 if (off)
18503 /* Optimize the common case. */
18504 if (single_element_loc_list_p (loc)
18505 && loc->expr->dw_loc_opc == DW_OP_addr
18506 && loc->expr->dw_loc_next == NULL
18507 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18509 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18510 loc->expr->dw_loc_oprnd1.v.val_addr
18511 = plus_constant (GET_MODE (x), x, off);
18513 else
18514 loc_list_plus_const (loc, off);
18516 add_AT_location_description (var_die, DW_AT_location, loc);
18518 else if (DECL_EXTERNAL (decl))
18519 add_AT_flag (var_die, DW_AT_declaration, 1);
18520 equate_decl_number_to_die (decl, var_die);
18521 return;
18524 /* If the compiler emitted a definition for the DECL declaration
18525 and if we already emitted a DIE for it, don't emit a second
18526 DIE for it again. Allow re-declarations of DECLs that are
18527 inside functions, though. */
18528 if (old_die && declaration && !local_scope_p (context_die))
18529 return;
18531 /* For static data members, the declaration in the class is supposed
18532 to have DW_TAG_member tag; the specification should still be
18533 DW_TAG_variable referencing the DW_TAG_member DIE. */
18534 if (declaration && class_scope_p (context_die))
18535 var_die = new_die (DW_TAG_member, context_die, decl);
18536 else
18537 var_die = new_die (DW_TAG_variable, context_die, decl);
18539 origin_die = NULL;
18540 if (origin != NULL)
18541 origin_die = add_abstract_origin_attribute (var_die, origin);
18543 /* Loop unrolling can create multiple blocks that refer to the same
18544 static variable, so we must test for the DW_AT_declaration flag.
18546 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18547 copy decls and set the DECL_ABSTRACT flag on them instead of
18548 sharing them.
18550 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18552 ??? The declare_in_namespace support causes us to get two DIEs for one
18553 variable, both of which are declarations. We want to avoid considering
18554 one to be a specification, so we must test that this DIE is not a
18555 declaration. */
18556 else if (old_die && TREE_STATIC (decl) && ! declaration
18557 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18559 /* This is a definition of a C++ class level static. */
18560 add_AT_specification (var_die, old_die);
18561 specialization_p = true;
18562 if (DECL_NAME (decl))
18564 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18565 struct dwarf_file_data * file_index = lookup_filename (s.file);
18567 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18568 add_AT_file (var_die, DW_AT_decl_file, file_index);
18570 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18571 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18573 if (old_die->die_tag == DW_TAG_member)
18574 add_linkage_name (var_die, decl);
18577 else
18578 add_name_and_src_coords_attributes (var_die, decl);
18580 if ((origin == NULL && !specialization_p)
18581 || (origin != NULL
18582 && !DECL_ABSTRACT (decl_or_origin)
18583 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18584 decl_function_context
18585 (decl_or_origin))))
18587 tree type = TREE_TYPE (decl_or_origin);
18589 if (decl_by_reference_p (decl_or_origin))
18590 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18591 else
18592 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18593 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18596 if (origin == NULL && !specialization_p)
18598 if (TREE_PUBLIC (decl))
18599 add_AT_flag (var_die, DW_AT_external, 1);
18601 if (DECL_ARTIFICIAL (decl))
18602 add_AT_flag (var_die, DW_AT_artificial, 1);
18604 add_accessibility_attribute (var_die, decl);
18607 if (declaration)
18608 add_AT_flag (var_die, DW_AT_declaration, 1);
18610 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18611 equate_decl_number_to_die (decl, var_die);
18613 if (! declaration
18614 && (! DECL_ABSTRACT (decl_or_origin)
18615 /* Local static vars are shared between all clones/inlines,
18616 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18617 already set. */
18618 || (TREE_CODE (decl_or_origin) == VAR_DECL
18619 && TREE_STATIC (decl_or_origin)
18620 && DECL_RTL_SET_P (decl_or_origin)))
18621 /* When abstract origin already has DW_AT_location attribute, no need
18622 to add it again. */
18623 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18625 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18626 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18627 defer_location (decl_or_origin, var_die);
18628 else
18629 add_location_or_const_value_attribute (var_die, decl_or_origin,
18630 decl == NULL, DW_AT_location);
18631 add_pubname (decl_or_origin, var_die);
18633 else
18634 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18637 /* Generate a DIE to represent a named constant. */
18639 static void
18640 gen_const_die (tree decl, dw_die_ref context_die)
18642 dw_die_ref const_die;
18643 tree type = TREE_TYPE (decl);
18645 const_die = new_die (DW_TAG_constant, context_die, decl);
18646 add_name_and_src_coords_attributes (const_die, decl);
18647 add_type_attribute (const_die, type, 1, 0, context_die);
18648 if (TREE_PUBLIC (decl))
18649 add_AT_flag (const_die, DW_AT_external, 1);
18650 if (DECL_ARTIFICIAL (decl))
18651 add_AT_flag (const_die, DW_AT_artificial, 1);
18652 tree_add_const_value_attribute_for_decl (const_die, decl);
18655 /* Generate a DIE to represent a label identifier. */
18657 static void
18658 gen_label_die (tree decl, dw_die_ref context_die)
18660 tree origin = decl_ultimate_origin (decl);
18661 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18662 rtx insn;
18663 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18665 if (origin != NULL)
18666 add_abstract_origin_attribute (lbl_die, origin);
18667 else
18668 add_name_and_src_coords_attributes (lbl_die, decl);
18670 if (DECL_ABSTRACT (decl))
18671 equate_decl_number_to_die (decl, lbl_die);
18672 else
18674 insn = DECL_RTL_IF_SET (decl);
18676 /* Deleted labels are programmer specified labels which have been
18677 eliminated because of various optimizations. We still emit them
18678 here so that it is possible to put breakpoints on them. */
18679 if (insn
18680 && (LABEL_P (insn)
18681 || ((NOTE_P (insn)
18682 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18684 /* When optimization is enabled (via -O) some parts of the compiler
18685 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18686 represent source-level labels which were explicitly declared by
18687 the user. This really shouldn't be happening though, so catch
18688 it if it ever does happen. */
18689 gcc_assert (!INSN_DELETED_P (insn));
18691 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18692 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18694 else if (insn
18695 && NOTE_P (insn)
18696 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18697 && CODE_LABEL_NUMBER (insn) != -1)
18699 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18700 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18705 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18706 attributes to the DIE for a block STMT, to describe where the inlined
18707 function was called from. This is similar to add_src_coords_attributes. */
18709 static inline void
18710 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18712 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18714 if (dwarf_version >= 3 || !dwarf_strict)
18716 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18717 add_AT_unsigned (die, DW_AT_call_line, s.line);
18722 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18723 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18725 static inline void
18726 add_high_low_attributes (tree stmt, dw_die_ref die)
18728 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18730 if (BLOCK_FRAGMENT_CHAIN (stmt)
18731 && (dwarf_version >= 3 || !dwarf_strict))
18733 tree chain, superblock = NULL_TREE;
18734 dw_die_ref pdie;
18735 dw_attr_ref attr = NULL;
18737 if (inlined_function_outer_scope_p (stmt))
18739 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18740 BLOCK_NUMBER (stmt));
18741 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18744 /* Optimize duplicate .debug_ranges lists or even tails of
18745 lists. If this BLOCK has same ranges as its supercontext,
18746 lookup DW_AT_ranges attribute in the supercontext (and
18747 recursively so), verify that the ranges_table contains the
18748 right values and use it instead of adding a new .debug_range. */
18749 for (chain = stmt, pdie = die;
18750 BLOCK_SAME_RANGE (chain);
18751 chain = BLOCK_SUPERCONTEXT (chain))
18753 dw_attr_ref new_attr;
18755 pdie = pdie->die_parent;
18756 if (pdie == NULL)
18757 break;
18758 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18759 break;
18760 new_attr = get_AT (pdie, DW_AT_ranges);
18761 if (new_attr == NULL
18762 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18763 break;
18764 attr = new_attr;
18765 superblock = BLOCK_SUPERCONTEXT (chain);
18767 if (attr != NULL
18768 && (ranges_table[attr->dw_attr_val.v.val_offset
18769 / 2 / DWARF2_ADDR_SIZE].num
18770 == BLOCK_NUMBER (superblock))
18771 && BLOCK_FRAGMENT_CHAIN (superblock))
18773 unsigned long off = attr->dw_attr_val.v.val_offset
18774 / 2 / DWARF2_ADDR_SIZE;
18775 unsigned long supercnt = 0, thiscnt = 0;
18776 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18777 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18779 ++supercnt;
18780 gcc_checking_assert (ranges_table[off + supercnt].num
18781 == BLOCK_NUMBER (chain));
18783 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18784 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18785 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18786 ++thiscnt;
18787 gcc_assert (supercnt >= thiscnt);
18788 add_AT_range_list (die, DW_AT_ranges,
18789 ((off + supercnt - thiscnt)
18790 * 2 * DWARF2_ADDR_SIZE),
18791 false);
18792 return;
18795 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18797 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18800 add_ranges (chain);
18801 chain = BLOCK_FRAGMENT_CHAIN (chain);
18803 while (chain);
18804 add_ranges (NULL);
18806 else
18808 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18809 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18810 BLOCK_NUMBER (stmt));
18811 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18812 BLOCK_NUMBER (stmt));
18813 add_AT_low_high_pc (die, label, label_high, false);
18817 /* Generate a DIE for a lexical block. */
18819 static void
18820 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18822 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18824 if (call_arg_locations)
18826 if (block_map.length () <= BLOCK_NUMBER (stmt))
18827 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18828 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18831 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18832 add_high_low_attributes (stmt, stmt_die);
18834 decls_for_scope (stmt, stmt_die, depth);
18837 /* Generate a DIE for an inlined subprogram. */
18839 static void
18840 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18842 tree decl;
18844 /* The instance of function that is effectively being inlined shall not
18845 be abstract. */
18846 gcc_assert (! BLOCK_ABSTRACT (stmt));
18848 decl = block_ultimate_origin (stmt);
18850 /* Emit info for the abstract instance first, if we haven't yet. We
18851 must emit this even if the block is abstract, otherwise when we
18852 emit the block below (or elsewhere), we may end up trying to emit
18853 a die whose origin die hasn't been emitted, and crashing. */
18854 dwarf2out_abstract_function (decl);
18856 if (! BLOCK_ABSTRACT (stmt))
18858 dw_die_ref subr_die
18859 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18861 if (call_arg_locations)
18863 if (block_map.length () <= BLOCK_NUMBER (stmt))
18864 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18865 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18867 add_abstract_origin_attribute (subr_die, decl);
18868 if (TREE_ASM_WRITTEN (stmt))
18869 add_high_low_attributes (stmt, subr_die);
18870 add_call_src_coords_attributes (stmt, subr_die);
18872 decls_for_scope (stmt, subr_die, depth);
18873 current_function_has_inlines = 1;
18877 /* Generate a DIE for a field in a record, or structure. */
18879 static void
18880 gen_field_die (tree decl, dw_die_ref context_die)
18882 dw_die_ref decl_die;
18884 if (TREE_TYPE (decl) == error_mark_node)
18885 return;
18887 decl_die = new_die (DW_TAG_member, context_die, decl);
18888 add_name_and_src_coords_attributes (decl_die, decl);
18889 add_type_attribute (decl_die, member_declared_type (decl),
18890 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18891 context_die);
18893 if (DECL_BIT_FIELD_TYPE (decl))
18895 add_byte_size_attribute (decl_die, decl);
18896 add_bit_size_attribute (decl_die, decl);
18897 add_bit_offset_attribute (decl_die, decl);
18900 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18901 add_data_member_location_attribute (decl_die, decl);
18903 if (DECL_ARTIFICIAL (decl))
18904 add_AT_flag (decl_die, DW_AT_artificial, 1);
18906 add_accessibility_attribute (decl_die, decl);
18908 /* Equate decl number to die, so that we can look up this decl later on. */
18909 equate_decl_number_to_die (decl, decl_die);
18912 #if 0
18913 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18914 Use modified_type_die instead.
18915 We keep this code here just in case these types of DIEs may be needed to
18916 represent certain things in other languages (e.g. Pascal) someday. */
18918 static void
18919 gen_pointer_type_die (tree type, dw_die_ref context_die)
18921 dw_die_ref ptr_die
18922 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18924 equate_type_number_to_die (type, ptr_die);
18925 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18926 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18929 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18930 Use modified_type_die instead.
18931 We keep this code here just in case these types of DIEs may be needed to
18932 represent certain things in other languages (e.g. Pascal) someday. */
18934 static void
18935 gen_reference_type_die (tree type, dw_die_ref context_die)
18937 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18939 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18940 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18941 else
18942 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18944 equate_type_number_to_die (type, ref_die);
18945 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18946 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18948 #endif
18950 /* Generate a DIE for a pointer to a member type. */
18952 static void
18953 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18955 dw_die_ref ptr_die
18956 = new_die (DW_TAG_ptr_to_member_type,
18957 scope_die_for (type, context_die), type);
18959 equate_type_number_to_die (type, ptr_die);
18960 add_AT_die_ref (ptr_die, DW_AT_containing_type,
18961 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18962 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18965 typedef const char *dchar_p; /* For DEF_VEC_P. */
18967 static char *producer_string;
18969 /* Return a heap allocated producer string including command line options
18970 if -grecord-gcc-switches. */
18972 static char *
18973 gen_producer_string (void)
18975 size_t j;
18976 vec<dchar_p> switches = vNULL;
18977 const char *language_string = lang_hooks.name;
18978 char *producer, *tail;
18979 const char *p;
18980 size_t len = dwarf_record_gcc_switches ? 0 : 3;
18981 size_t plen = strlen (language_string) + 1 + strlen (version_string);
18983 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18984 switch (save_decoded_options[j].opt_index)
18986 case OPT_o:
18987 case OPT_d:
18988 case OPT_dumpbase:
18989 case OPT_dumpdir:
18990 case OPT_auxbase:
18991 case OPT_auxbase_strip:
18992 case OPT_quiet:
18993 case OPT_version:
18994 case OPT_v:
18995 case OPT_w:
18996 case OPT_L:
18997 case OPT_D:
18998 case OPT_I:
18999 case OPT_U:
19000 case OPT_SPECIAL_unknown:
19001 case OPT_SPECIAL_ignore:
19002 case OPT_SPECIAL_program_name:
19003 case OPT_SPECIAL_input_file:
19004 case OPT_grecord_gcc_switches:
19005 case OPT_gno_record_gcc_switches:
19006 case OPT__output_pch_:
19007 case OPT_fdiagnostics_show_location_:
19008 case OPT_fdiagnostics_show_option:
19009 case OPT_fdiagnostics_show_caret:
19010 case OPT_fdiagnostics_color_:
19011 case OPT_fverbose_asm:
19012 case OPT____:
19013 case OPT__sysroot_:
19014 case OPT_nostdinc:
19015 case OPT_nostdinc__:
19016 /* Ignore these. */
19017 continue;
19018 default:
19019 if (cl_options[save_decoded_options[j].opt_index].flags
19020 & CL_NO_DWARF_RECORD)
19021 continue;
19022 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19023 == '-');
19024 switch (save_decoded_options[j].canonical_option[0][1])
19026 case 'M':
19027 case 'i':
19028 case 'W':
19029 continue;
19030 case 'f':
19031 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19032 "dump", 4) == 0)
19033 continue;
19034 break;
19035 default:
19036 break;
19038 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19039 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19040 break;
19043 producer = XNEWVEC (char, plen + 1 + len + 1);
19044 tail = producer;
19045 sprintf (tail, "%s %s", language_string, version_string);
19046 tail += plen;
19048 FOR_EACH_VEC_ELT (switches, j, p)
19050 len = strlen (p);
19051 *tail = ' ';
19052 memcpy (tail + 1, p, len);
19053 tail += len + 1;
19056 *tail = '\0';
19057 switches.release ();
19058 return producer;
19061 /* Generate the DIE for the compilation unit. */
19063 static dw_die_ref
19064 gen_compile_unit_die (const char *filename)
19066 dw_die_ref die;
19067 const char *language_string = lang_hooks.name;
19068 int language;
19070 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19072 if (filename)
19074 add_name_attribute (die, filename);
19075 /* Don't add cwd for <built-in>. */
19076 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19077 add_comp_dir_attribute (die);
19080 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19082 /* If our producer is LTO try to figure out a common language to use
19083 from the global list of translation units. */
19084 if (strcmp (language_string, "GNU GIMPLE") == 0)
19086 unsigned i;
19087 tree t;
19088 const char *common_lang = NULL;
19090 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19092 if (!TRANSLATION_UNIT_LANGUAGE (t))
19093 continue;
19094 if (!common_lang)
19095 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19096 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19098 else if (strncmp (common_lang, "GNU C", 5) == 0
19099 && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19100 /* Mixing C and C++ is ok, use C++ in that case. */
19101 common_lang = "GNU C++";
19102 else
19104 /* Fall back to C. */
19105 common_lang = NULL;
19106 break;
19110 if (common_lang)
19111 language_string = common_lang;
19114 language = DW_LANG_C89;
19115 if (strcmp (language_string, "GNU C++") == 0)
19116 language = DW_LANG_C_plus_plus;
19117 else if (strcmp (language_string, "GNU F77") == 0)
19118 language = DW_LANG_Fortran77;
19119 else if (strcmp (language_string, "GNU Pascal") == 0)
19120 language = DW_LANG_Pascal83;
19121 else if (dwarf_version >= 3 || !dwarf_strict)
19123 if (strcmp (language_string, "GNU Ada") == 0)
19124 language = DW_LANG_Ada95;
19125 else if (strcmp (language_string, "GNU Fortran") == 0)
19126 language = DW_LANG_Fortran95;
19127 else if (strcmp (language_string, "GNU Java") == 0)
19128 language = DW_LANG_Java;
19129 else if (strcmp (language_string, "GNU Objective-C") == 0)
19130 language = DW_LANG_ObjC;
19131 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19132 language = DW_LANG_ObjC_plus_plus;
19133 else if (dwarf_version >= 5 || !dwarf_strict)
19135 if (strcmp (language_string, "GNU Go") == 0)
19136 language = DW_LANG_Go;
19139 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19140 else if (strcmp (language_string, "GNU Fortran") == 0)
19141 language = DW_LANG_Fortran90;
19143 add_AT_unsigned (die, DW_AT_language, language);
19145 switch (language)
19147 case DW_LANG_Fortran77:
19148 case DW_LANG_Fortran90:
19149 case DW_LANG_Fortran95:
19150 /* Fortran has case insensitive identifiers and the front-end
19151 lowercases everything. */
19152 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19153 break;
19154 default:
19155 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19156 break;
19158 return die;
19161 /* Generate the DIE for a base class. */
19163 static void
19164 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19166 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19168 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19169 add_data_member_location_attribute (die, binfo);
19171 if (BINFO_VIRTUAL_P (binfo))
19172 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19174 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19175 children, otherwise the default is DW_ACCESS_public. In DWARF2
19176 the default has always been DW_ACCESS_private. */
19177 if (access == access_public_node)
19179 if (dwarf_version == 2
19180 || context_die->die_tag == DW_TAG_class_type)
19181 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19183 else if (access == access_protected_node)
19184 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19185 else if (dwarf_version > 2
19186 && context_die->die_tag != DW_TAG_class_type)
19187 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19190 /* Generate a DIE for a class member. */
19192 static void
19193 gen_member_die (tree type, dw_die_ref context_die)
19195 tree member;
19196 tree binfo = TYPE_BINFO (type);
19197 dw_die_ref child;
19199 /* If this is not an incomplete type, output descriptions of each of its
19200 members. Note that as we output the DIEs necessary to represent the
19201 members of this record or union type, we will also be trying to output
19202 DIEs to represent the *types* of those members. However the `type'
19203 function (above) will specifically avoid generating type DIEs for member
19204 types *within* the list of member DIEs for this (containing) type except
19205 for those types (of members) which are explicitly marked as also being
19206 members of this (containing) type themselves. The g++ front- end can
19207 force any given type to be treated as a member of some other (containing)
19208 type by setting the TYPE_CONTEXT of the given (member) type to point to
19209 the TREE node representing the appropriate (containing) type. */
19211 /* First output info about the base classes. */
19212 if (binfo)
19214 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19215 int i;
19216 tree base;
19218 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19219 gen_inheritance_die (base,
19220 (accesses ? (*accesses)[i] : access_public_node),
19221 context_die);
19224 /* Now output info about the data members and type members. */
19225 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19227 /* If we thought we were generating minimal debug info for TYPE
19228 and then changed our minds, some of the member declarations
19229 may have already been defined. Don't define them again, but
19230 do put them in the right order. */
19232 child = lookup_decl_die (member);
19233 if (child)
19234 splice_child_die (context_die, child);
19235 else
19236 gen_decl_die (member, NULL, context_die);
19239 /* Now output info about the function members (if any). */
19240 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19242 /* Don't include clones in the member list. */
19243 if (DECL_ABSTRACT_ORIGIN (member))
19244 continue;
19246 child = lookup_decl_die (member);
19247 if (child)
19248 splice_child_die (context_die, child);
19249 else
19250 gen_decl_die (member, NULL, context_die);
19254 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19255 is set, we pretend that the type was never defined, so we only get the
19256 member DIEs needed by later specification DIEs. */
19258 static void
19259 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19260 enum debug_info_usage usage)
19262 dw_die_ref type_die = lookup_type_die (type);
19263 dw_die_ref scope_die = 0;
19264 int nested = 0;
19265 int complete = (TYPE_SIZE (type)
19266 && (! TYPE_STUB_DECL (type)
19267 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19268 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19269 complete = complete && should_emit_struct_debug (type, usage);
19271 if (type_die && ! complete)
19272 return;
19274 if (TYPE_CONTEXT (type) != NULL_TREE
19275 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19276 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19277 nested = 1;
19279 scope_die = scope_die_for (type, context_die);
19281 /* Generate child dies for template paramaters. */
19282 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19283 schedule_generic_params_dies_gen (type);
19285 if (! type_die || (nested && is_cu_die (scope_die)))
19286 /* First occurrence of type or toplevel definition of nested class. */
19288 dw_die_ref old_die = type_die;
19290 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19291 ? record_type_tag (type) : DW_TAG_union_type,
19292 scope_die, type);
19293 equate_type_number_to_die (type, type_die);
19294 if (old_die)
19295 add_AT_specification (type_die, old_die);
19296 else
19297 add_name_attribute (type_die, type_tag (type));
19299 else
19300 remove_AT (type_die, DW_AT_declaration);
19302 /* If this type has been completed, then give it a byte_size attribute and
19303 then give a list of members. */
19304 if (complete && !ns_decl)
19306 /* Prevent infinite recursion in cases where the type of some member of
19307 this type is expressed in terms of this type itself. */
19308 TREE_ASM_WRITTEN (type) = 1;
19309 add_byte_size_attribute (type_die, type);
19310 if (TYPE_STUB_DECL (type) != NULL_TREE)
19312 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19313 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19316 /* If the first reference to this type was as the return type of an
19317 inline function, then it may not have a parent. Fix this now. */
19318 if (type_die->die_parent == NULL)
19319 add_child_die (scope_die, type_die);
19321 push_decl_scope (type);
19322 gen_member_die (type, type_die);
19323 pop_decl_scope ();
19325 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19326 if (TYPE_ARTIFICIAL (type))
19327 add_AT_flag (type_die, DW_AT_artificial, 1);
19329 /* GNU extension: Record what type our vtable lives in. */
19330 if (TYPE_VFIELD (type))
19332 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19334 gen_type_die (vtype, context_die);
19335 add_AT_die_ref (type_die, DW_AT_containing_type,
19336 lookup_type_die (vtype));
19339 else
19341 add_AT_flag (type_die, DW_AT_declaration, 1);
19343 /* We don't need to do this for function-local types. */
19344 if (TYPE_STUB_DECL (type)
19345 && ! decl_function_context (TYPE_STUB_DECL (type)))
19346 vec_safe_push (incomplete_types, type);
19349 if (get_AT (type_die, DW_AT_name))
19350 add_pubtype (type, type_die);
19353 /* Generate a DIE for a subroutine _type_. */
19355 static void
19356 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19358 tree return_type = TREE_TYPE (type);
19359 dw_die_ref subr_die
19360 = new_die (DW_TAG_subroutine_type,
19361 scope_die_for (type, context_die), type);
19363 equate_type_number_to_die (type, subr_die);
19364 add_prototyped_attribute (subr_die, type);
19365 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19366 gen_formal_types_die (type, subr_die);
19368 if (get_AT (subr_die, DW_AT_name))
19369 add_pubtype (type, subr_die);
19372 /* Generate a DIE for a type definition. */
19374 static void
19375 gen_typedef_die (tree decl, dw_die_ref context_die)
19377 dw_die_ref type_die;
19378 tree origin;
19380 if (TREE_ASM_WRITTEN (decl))
19381 return;
19383 TREE_ASM_WRITTEN (decl) = 1;
19384 type_die = new_die (DW_TAG_typedef, context_die, decl);
19385 origin = decl_ultimate_origin (decl);
19386 if (origin != NULL)
19387 add_abstract_origin_attribute (type_die, origin);
19388 else
19390 tree type;
19392 add_name_and_src_coords_attributes (type_die, decl);
19393 if (DECL_ORIGINAL_TYPE (decl))
19395 type = DECL_ORIGINAL_TYPE (decl);
19397 gcc_assert (type != TREE_TYPE (decl));
19398 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19400 else
19402 type = TREE_TYPE (decl);
19404 if (is_naming_typedef_decl (TYPE_NAME (type)))
19406 /* Here, we are in the case of decl being a typedef naming
19407 an anonymous type, e.g:
19408 typedef struct {...} foo;
19409 In that case TREE_TYPE (decl) is not a typedef variant
19410 type and TYPE_NAME of the anonymous type is set to the
19411 TYPE_DECL of the typedef. This construct is emitted by
19412 the C++ FE.
19414 TYPE is the anonymous struct named by the typedef
19415 DECL. As we need the DW_AT_type attribute of the
19416 DW_TAG_typedef to point to the DIE of TYPE, let's
19417 generate that DIE right away. add_type_attribute
19418 called below will then pick (via lookup_type_die) that
19419 anonymous struct DIE. */
19420 if (!TREE_ASM_WRITTEN (type))
19421 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19423 /* This is a GNU Extension. We are adding a
19424 DW_AT_linkage_name attribute to the DIE of the
19425 anonymous struct TYPE. The value of that attribute
19426 is the name of the typedef decl naming the anonymous
19427 struct. This greatly eases the work of consumers of
19428 this debug info. */
19429 add_linkage_attr (lookup_type_die (type), decl);
19433 add_type_attribute (type_die, type, TREE_READONLY (decl),
19434 TREE_THIS_VOLATILE (decl), context_die);
19436 if (is_naming_typedef_decl (decl))
19437 /* We want that all subsequent calls to lookup_type_die with
19438 TYPE in argument yield the DW_TAG_typedef we have just
19439 created. */
19440 equate_type_number_to_die (type, type_die);
19442 add_accessibility_attribute (type_die, decl);
19445 if (DECL_ABSTRACT (decl))
19446 equate_decl_number_to_die (decl, type_die);
19448 if (get_AT (type_die, DW_AT_name))
19449 add_pubtype (decl, type_die);
19452 /* Generate a DIE for a struct, class, enum or union type. */
19454 static void
19455 gen_tagged_type_die (tree type,
19456 dw_die_ref context_die,
19457 enum debug_info_usage usage)
19459 int need_pop;
19461 if (type == NULL_TREE
19462 || !is_tagged_type (type))
19463 return;
19465 /* If this is a nested type whose containing class hasn't been written
19466 out yet, writing it out will cover this one, too. This does not apply
19467 to instantiations of member class templates; they need to be added to
19468 the containing class as they are generated. FIXME: This hurts the
19469 idea of combining type decls from multiple TUs, since we can't predict
19470 what set of template instantiations we'll get. */
19471 if (TYPE_CONTEXT (type)
19472 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19473 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19475 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19477 if (TREE_ASM_WRITTEN (type))
19478 return;
19480 /* If that failed, attach ourselves to the stub. */
19481 push_decl_scope (TYPE_CONTEXT (type));
19482 context_die = lookup_type_die (TYPE_CONTEXT (type));
19483 need_pop = 1;
19485 else if (TYPE_CONTEXT (type) != NULL_TREE
19486 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19488 /* If this type is local to a function that hasn't been written
19489 out yet, use a NULL context for now; it will be fixed up in
19490 decls_for_scope. */
19491 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19492 /* A declaration DIE doesn't count; nested types need to go in the
19493 specification. */
19494 if (context_die && is_declaration_die (context_die))
19495 context_die = NULL;
19496 need_pop = 0;
19498 else
19500 context_die = declare_in_namespace (type, context_die);
19501 need_pop = 0;
19504 if (TREE_CODE (type) == ENUMERAL_TYPE)
19506 /* This might have been written out by the call to
19507 declare_in_namespace. */
19508 if (!TREE_ASM_WRITTEN (type))
19509 gen_enumeration_type_die (type, context_die);
19511 else
19512 gen_struct_or_union_type_die (type, context_die, usage);
19514 if (need_pop)
19515 pop_decl_scope ();
19517 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19518 it up if it is ever completed. gen_*_type_die will set it for us
19519 when appropriate. */
19522 /* Generate a type description DIE. */
19524 static void
19525 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19526 enum debug_info_usage usage)
19528 struct array_descr_info info;
19530 if (type == NULL_TREE || type == error_mark_node)
19531 return;
19533 if (TYPE_NAME (type) != NULL_TREE
19534 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19535 && is_redundant_typedef (TYPE_NAME (type))
19536 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19537 /* The DECL of this type is a typedef we don't want to emit debug
19538 info for but we want debug info for its underlying typedef.
19539 This can happen for e.g, the injected-class-name of a C++
19540 type. */
19541 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19543 /* If TYPE is a typedef type variant, let's generate debug info
19544 for the parent typedef which TYPE is a type of. */
19545 if (typedef_variant_p (type))
19547 if (TREE_ASM_WRITTEN (type))
19548 return;
19550 /* Prevent broken recursion; we can't hand off to the same type. */
19551 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19553 /* Give typedefs the right scope. */
19554 context_die = scope_die_for (type, context_die);
19556 TREE_ASM_WRITTEN (type) = 1;
19558 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19559 return;
19562 /* If type is an anonymous tagged type named by a typedef, let's
19563 generate debug info for the typedef. */
19564 if (is_naming_typedef_decl (TYPE_NAME (type)))
19566 /* Use the DIE of the containing namespace as the parent DIE of
19567 the type description DIE we want to generate. */
19568 if (DECL_CONTEXT (TYPE_NAME (type))
19569 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19570 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19572 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19573 return;
19576 /* If this is an array type with hidden descriptor, handle it first. */
19577 if (!TREE_ASM_WRITTEN (type)
19578 && lang_hooks.types.get_array_descr_info
19579 && lang_hooks.types.get_array_descr_info (type, &info)
19580 && (dwarf_version >= 3 || !dwarf_strict))
19582 gen_descr_array_type_die (type, &info, context_die);
19583 TREE_ASM_WRITTEN (type) = 1;
19584 return;
19587 /* We are going to output a DIE to represent the unqualified version
19588 of this type (i.e. without any const or volatile qualifiers) so
19589 get the main variant (i.e. the unqualified version) of this type
19590 now. (Vectors are special because the debugging info is in the
19591 cloned type itself). */
19592 if (TREE_CODE (type) != VECTOR_TYPE)
19593 type = type_main_variant (type);
19595 if (TREE_ASM_WRITTEN (type))
19596 return;
19598 switch (TREE_CODE (type))
19600 case ERROR_MARK:
19601 break;
19603 case POINTER_TYPE:
19604 case REFERENCE_TYPE:
19605 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19606 ensures that the gen_type_die recursion will terminate even if the
19607 type is recursive. Recursive types are possible in Ada. */
19608 /* ??? We could perhaps do this for all types before the switch
19609 statement. */
19610 TREE_ASM_WRITTEN (type) = 1;
19612 /* For these types, all that is required is that we output a DIE (or a
19613 set of DIEs) to represent the "basis" type. */
19614 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19615 DINFO_USAGE_IND_USE);
19616 break;
19618 case OFFSET_TYPE:
19619 /* This code is used for C++ pointer-to-data-member types.
19620 Output a description of the relevant class type. */
19621 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19622 DINFO_USAGE_IND_USE);
19624 /* Output a description of the type of the object pointed to. */
19625 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19626 DINFO_USAGE_IND_USE);
19628 /* Now output a DIE to represent this pointer-to-data-member type
19629 itself. */
19630 gen_ptr_to_mbr_type_die (type, context_die);
19631 break;
19633 case FUNCTION_TYPE:
19634 /* Force out return type (in case it wasn't forced out already). */
19635 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19636 DINFO_USAGE_DIR_USE);
19637 gen_subroutine_type_die (type, context_die);
19638 break;
19640 case METHOD_TYPE:
19641 /* Force out return type (in case it wasn't forced out already). */
19642 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19643 DINFO_USAGE_DIR_USE);
19644 gen_subroutine_type_die (type, context_die);
19645 break;
19647 case ARRAY_TYPE:
19648 gen_array_type_die (type, context_die);
19649 break;
19651 case VECTOR_TYPE:
19652 gen_array_type_die (type, context_die);
19653 break;
19655 case ENUMERAL_TYPE:
19656 case RECORD_TYPE:
19657 case UNION_TYPE:
19658 case QUAL_UNION_TYPE:
19659 gen_tagged_type_die (type, context_die, usage);
19660 return;
19662 case VOID_TYPE:
19663 case INTEGER_TYPE:
19664 case REAL_TYPE:
19665 case FIXED_POINT_TYPE:
19666 case COMPLEX_TYPE:
19667 case BOOLEAN_TYPE:
19668 /* No DIEs needed for fundamental types. */
19669 break;
19671 case NULLPTR_TYPE:
19672 case LANG_TYPE:
19673 /* Just use DW_TAG_unspecified_type. */
19675 dw_die_ref type_die = lookup_type_die (type);
19676 if (type_die == NULL)
19678 tree name = TYPE_NAME (type);
19679 if (TREE_CODE (name) == TYPE_DECL)
19680 name = DECL_NAME (name);
19681 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19682 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19683 equate_type_number_to_die (type, type_die);
19686 break;
19688 default:
19689 gcc_unreachable ();
19692 TREE_ASM_WRITTEN (type) = 1;
19695 static void
19696 gen_type_die (tree type, dw_die_ref context_die)
19698 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19701 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19702 things which are local to the given block. */
19704 static void
19705 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19707 int must_output_die = 0;
19708 bool inlined_func;
19710 /* Ignore blocks that are NULL. */
19711 if (stmt == NULL_TREE)
19712 return;
19714 inlined_func = inlined_function_outer_scope_p (stmt);
19716 /* If the block is one fragment of a non-contiguous block, do not
19717 process the variables, since they will have been done by the
19718 origin block. Do process subblocks. */
19719 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19721 tree sub;
19723 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19724 gen_block_die (sub, context_die, depth + 1);
19726 return;
19729 /* Determine if we need to output any Dwarf DIEs at all to represent this
19730 block. */
19731 if (inlined_func)
19732 /* The outer scopes for inlinings *must* always be represented. We
19733 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19734 must_output_die = 1;
19735 else
19737 /* Determine if this block directly contains any "significant"
19738 local declarations which we will need to output DIEs for. */
19739 if (debug_info_level > DINFO_LEVEL_TERSE)
19740 /* We are not in terse mode so *any* local declaration counts
19741 as being a "significant" one. */
19742 must_output_die = ((BLOCK_VARS (stmt) != NULL
19743 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19744 && (TREE_USED (stmt)
19745 || TREE_ASM_WRITTEN (stmt)
19746 || BLOCK_ABSTRACT (stmt)));
19747 else if ((TREE_USED (stmt)
19748 || TREE_ASM_WRITTEN (stmt)
19749 || BLOCK_ABSTRACT (stmt))
19750 && !dwarf2out_ignore_block (stmt))
19751 must_output_die = 1;
19754 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19755 DIE for any block which contains no significant local declarations at
19756 all. Rather, in such cases we just call `decls_for_scope' so that any
19757 needed Dwarf info for any sub-blocks will get properly generated. Note
19758 that in terse mode, our definition of what constitutes a "significant"
19759 local declaration gets restricted to include only inlined function
19760 instances and local (nested) function definitions. */
19761 if (must_output_die)
19763 if (inlined_func)
19765 /* If STMT block is abstract, that means we have been called
19766 indirectly from dwarf2out_abstract_function.
19767 That function rightfully marks the descendent blocks (of
19768 the abstract function it is dealing with) as being abstract,
19769 precisely to prevent us from emitting any
19770 DW_TAG_inlined_subroutine DIE as a descendent
19771 of an abstract function instance. So in that case, we should
19772 not call gen_inlined_subroutine_die.
19774 Later though, when cgraph asks dwarf2out to emit info
19775 for the concrete instance of the function decl into which
19776 the concrete instance of STMT got inlined, the later will lead
19777 to the generation of a DW_TAG_inlined_subroutine DIE. */
19778 if (! BLOCK_ABSTRACT (stmt))
19779 gen_inlined_subroutine_die (stmt, context_die, depth);
19781 else
19782 gen_lexical_block_die (stmt, context_die, depth);
19784 else
19785 decls_for_scope (stmt, context_die, depth);
19788 /* Process variable DECL (or variable with origin ORIGIN) within
19789 block STMT and add it to CONTEXT_DIE. */
19790 static void
19791 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19793 dw_die_ref die;
19794 tree decl_or_origin = decl ? decl : origin;
19796 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19797 die = lookup_decl_die (decl_or_origin);
19798 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19799 && TYPE_DECL_IS_STUB (decl_or_origin))
19800 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19801 else
19802 die = NULL;
19804 if (die != NULL && die->die_parent == NULL)
19805 add_child_die (context_die, die);
19806 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19807 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19808 stmt, context_die);
19809 else
19810 gen_decl_die (decl, origin, context_die);
19813 /* Generate all of the decls declared within a given scope and (recursively)
19814 all of its sub-blocks. */
19816 static void
19817 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19819 tree decl;
19820 unsigned int i;
19821 tree subblocks;
19823 /* Ignore NULL blocks. */
19824 if (stmt == NULL_TREE)
19825 return;
19827 /* Output the DIEs to represent all of the data objects and typedefs
19828 declared directly within this block but not within any nested
19829 sub-blocks. Also, nested function and tag DIEs have been
19830 generated with a parent of NULL; fix that up now. */
19831 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19832 process_scope_var (stmt, decl, NULL_TREE, context_die);
19833 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19834 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19835 context_die);
19837 /* If we're at -g1, we're not interested in subblocks. */
19838 if (debug_info_level <= DINFO_LEVEL_TERSE)
19839 return;
19841 /* Output the DIEs to represent all sub-blocks (and the items declared
19842 therein) of this block. */
19843 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19844 subblocks != NULL;
19845 subblocks = BLOCK_CHAIN (subblocks))
19846 gen_block_die (subblocks, context_die, depth + 1);
19849 /* Is this a typedef we can avoid emitting? */
19851 static inline int
19852 is_redundant_typedef (const_tree decl)
19854 if (TYPE_DECL_IS_STUB (decl))
19855 return 1;
19857 if (DECL_ARTIFICIAL (decl)
19858 && DECL_CONTEXT (decl)
19859 && is_tagged_type (DECL_CONTEXT (decl))
19860 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19861 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19862 /* Also ignore the artificial member typedef for the class name. */
19863 return 1;
19865 return 0;
19868 /* Return TRUE if TYPE is a typedef that names a type for linkage
19869 purposes. This kind of typedefs is produced by the C++ FE for
19870 constructs like:
19872 typedef struct {...} foo;
19874 In that case, there is no typedef variant type produced for foo.
19875 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19876 struct type. */
19878 static bool
19879 is_naming_typedef_decl (const_tree decl)
19881 if (decl == NULL_TREE
19882 || TREE_CODE (decl) != TYPE_DECL
19883 || !is_tagged_type (TREE_TYPE (decl))
19884 || DECL_IS_BUILTIN (decl)
19885 || is_redundant_typedef (decl)
19886 /* It looks like Ada produces TYPE_DECLs that are very similar
19887 to C++ naming typedefs but that have different
19888 semantics. Let's be specific to c++ for now. */
19889 || !is_cxx ())
19890 return FALSE;
19892 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19893 && TYPE_NAME (TREE_TYPE (decl)) == decl
19894 && (TYPE_STUB_DECL (TREE_TYPE (decl))
19895 != TYPE_NAME (TREE_TYPE (decl))));
19898 /* Returns the DIE for a context. */
19900 static inline dw_die_ref
19901 get_context_die (tree context)
19903 if (context)
19905 /* Find die that represents this context. */
19906 if (TYPE_P (context))
19908 context = TYPE_MAIN_VARIANT (context);
19909 return strip_naming_typedef (context, force_type_die (context));
19911 else
19912 return force_decl_die (context);
19914 return comp_unit_die ();
19917 /* Returns the DIE for decl. A DIE will always be returned. */
19919 static dw_die_ref
19920 force_decl_die (tree decl)
19922 dw_die_ref decl_die;
19923 unsigned saved_external_flag;
19924 tree save_fn = NULL_TREE;
19925 decl_die = lookup_decl_die (decl);
19926 if (!decl_die)
19928 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19930 decl_die = lookup_decl_die (decl);
19931 if (decl_die)
19932 return decl_die;
19934 switch (TREE_CODE (decl))
19936 case FUNCTION_DECL:
19937 /* Clear current_function_decl, so that gen_subprogram_die thinks
19938 that this is a declaration. At this point, we just want to force
19939 declaration die. */
19940 save_fn = current_function_decl;
19941 current_function_decl = NULL_TREE;
19942 gen_subprogram_die (decl, context_die);
19943 current_function_decl = save_fn;
19944 break;
19946 case VAR_DECL:
19947 /* Set external flag to force declaration die. Restore it after
19948 gen_decl_die() call. */
19949 saved_external_flag = DECL_EXTERNAL (decl);
19950 DECL_EXTERNAL (decl) = 1;
19951 gen_decl_die (decl, NULL, context_die);
19952 DECL_EXTERNAL (decl) = saved_external_flag;
19953 break;
19955 case NAMESPACE_DECL:
19956 if (dwarf_version >= 3 || !dwarf_strict)
19957 dwarf2out_decl (decl);
19958 else
19959 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
19960 decl_die = comp_unit_die ();
19961 break;
19963 case TRANSLATION_UNIT_DECL:
19964 decl_die = comp_unit_die ();
19965 break;
19967 default:
19968 gcc_unreachable ();
19971 /* We should be able to find the DIE now. */
19972 if (!decl_die)
19973 decl_die = lookup_decl_die (decl);
19974 gcc_assert (decl_die);
19977 return decl_die;
19980 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
19981 always returned. */
19983 static dw_die_ref
19984 force_type_die (tree type)
19986 dw_die_ref type_die;
19988 type_die = lookup_type_die (type);
19989 if (!type_die)
19991 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19993 type_die = modified_type_die (type, TYPE_READONLY (type),
19994 TYPE_VOLATILE (type), context_die);
19995 gcc_assert (type_die);
19997 return type_die;
20000 /* Force out any required namespaces to be able to output DECL,
20001 and return the new context_die for it, if it's changed. */
20003 static dw_die_ref
20004 setup_namespace_context (tree thing, dw_die_ref context_die)
20006 tree context = (DECL_P (thing)
20007 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20008 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20009 /* Force out the namespace. */
20010 context_die = force_decl_die (context);
20012 return context_die;
20015 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20016 type) within its namespace, if appropriate.
20018 For compatibility with older debuggers, namespace DIEs only contain
20019 declarations; all definitions are emitted at CU scope. */
20021 static dw_die_ref
20022 declare_in_namespace (tree thing, dw_die_ref context_die)
20024 dw_die_ref ns_context;
20026 if (debug_info_level <= DINFO_LEVEL_TERSE)
20027 return context_die;
20029 /* If this decl is from an inlined function, then don't try to emit it in its
20030 namespace, as we will get confused. It would have already been emitted
20031 when the abstract instance of the inline function was emitted anyways. */
20032 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20033 return context_die;
20035 ns_context = setup_namespace_context (thing, context_die);
20037 if (ns_context != context_die)
20039 if (is_fortran ())
20040 return ns_context;
20041 if (DECL_P (thing))
20042 gen_decl_die (thing, NULL, ns_context);
20043 else
20044 gen_type_die (thing, ns_context);
20046 return context_die;
20049 /* Generate a DIE for a namespace or namespace alias. */
20051 static void
20052 gen_namespace_die (tree decl, dw_die_ref context_die)
20054 dw_die_ref namespace_die;
20056 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20057 they are an alias of. */
20058 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20060 /* Output a real namespace or module. */
20061 context_die = setup_namespace_context (decl, comp_unit_die ());
20062 namespace_die = new_die (is_fortran ()
20063 ? DW_TAG_module : DW_TAG_namespace,
20064 context_die, decl);
20065 /* For Fortran modules defined in different CU don't add src coords. */
20066 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20068 const char *name = dwarf2_name (decl, 0);
20069 if (name)
20070 add_name_attribute (namespace_die, name);
20072 else
20073 add_name_and_src_coords_attributes (namespace_die, decl);
20074 if (DECL_EXTERNAL (decl))
20075 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20076 equate_decl_number_to_die (decl, namespace_die);
20078 else
20080 /* Output a namespace alias. */
20082 /* Force out the namespace we are an alias of, if necessary. */
20083 dw_die_ref origin_die
20084 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20086 if (DECL_FILE_SCOPE_P (decl)
20087 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20088 context_die = setup_namespace_context (decl, comp_unit_die ());
20089 /* Now create the namespace alias DIE. */
20090 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20091 add_name_and_src_coords_attributes (namespace_die, decl);
20092 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20093 equate_decl_number_to_die (decl, namespace_die);
20095 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20096 if (want_pubnames ())
20097 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20100 /* Generate Dwarf debug information for a decl described by DECL.
20101 The return value is currently only meaningful for PARM_DECLs,
20102 for all other decls it returns NULL. */
20104 static dw_die_ref
20105 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20107 tree decl_or_origin = decl ? decl : origin;
20108 tree class_origin = NULL, ultimate_origin;
20110 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20111 return NULL;
20113 switch (TREE_CODE (decl_or_origin))
20115 case ERROR_MARK:
20116 break;
20118 case CONST_DECL:
20119 if (!is_fortran () && !is_ada ())
20121 /* The individual enumerators of an enum type get output when we output
20122 the Dwarf representation of the relevant enum type itself. */
20123 break;
20126 /* Emit its type. */
20127 gen_type_die (TREE_TYPE (decl), context_die);
20129 /* And its containing namespace. */
20130 context_die = declare_in_namespace (decl, context_die);
20132 gen_const_die (decl, context_die);
20133 break;
20135 case FUNCTION_DECL:
20136 /* Don't output any DIEs to represent mere function declarations,
20137 unless they are class members or explicit block externs. */
20138 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20139 && DECL_FILE_SCOPE_P (decl_or_origin)
20140 && (current_function_decl == NULL_TREE
20141 || DECL_ARTIFICIAL (decl_or_origin)))
20142 break;
20144 #if 0
20145 /* FIXME */
20146 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20147 on local redeclarations of global functions. That seems broken. */
20148 if (current_function_decl != decl)
20149 /* This is only a declaration. */;
20150 #endif
20152 /* If we're emitting a clone, emit info for the abstract instance. */
20153 if (origin || DECL_ORIGIN (decl) != decl)
20154 dwarf2out_abstract_function (origin
20155 ? DECL_ORIGIN (origin)
20156 : DECL_ABSTRACT_ORIGIN (decl));
20158 /* If we're emitting an out-of-line copy of an inline function,
20159 emit info for the abstract instance and set up to refer to it. */
20160 else if (cgraph_function_possibly_inlined_p (decl)
20161 && ! DECL_ABSTRACT (decl)
20162 && ! class_or_namespace_scope_p (context_die)
20163 /* dwarf2out_abstract_function won't emit a die if this is just
20164 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20165 that case, because that works only if we have a die. */
20166 && DECL_INITIAL (decl) != NULL_TREE)
20168 dwarf2out_abstract_function (decl);
20169 set_decl_origin_self (decl);
20172 /* Otherwise we're emitting the primary DIE for this decl. */
20173 else if (debug_info_level > DINFO_LEVEL_TERSE)
20175 /* Before we describe the FUNCTION_DECL itself, make sure that we
20176 have its containing type. */
20177 if (!origin)
20178 origin = decl_class_context (decl);
20179 if (origin != NULL_TREE)
20180 gen_type_die (origin, context_die);
20182 /* And its return type. */
20183 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20185 /* And its virtual context. */
20186 if (DECL_VINDEX (decl) != NULL_TREE)
20187 gen_type_die (DECL_CONTEXT (decl), context_die);
20189 /* Make sure we have a member DIE for decl. */
20190 if (origin != NULL_TREE)
20191 gen_type_die_for_member (origin, decl, context_die);
20193 /* And its containing namespace. */
20194 context_die = declare_in_namespace (decl, context_die);
20197 /* Now output a DIE to represent the function itself. */
20198 if (decl)
20199 gen_subprogram_die (decl, context_die);
20200 break;
20202 case TYPE_DECL:
20203 /* If we are in terse mode, don't generate any DIEs to represent any
20204 actual typedefs. */
20205 if (debug_info_level <= DINFO_LEVEL_TERSE)
20206 break;
20208 /* In the special case of a TYPE_DECL node representing the declaration
20209 of some type tag, if the given TYPE_DECL is marked as having been
20210 instantiated from some other (original) TYPE_DECL node (e.g. one which
20211 was generated within the original definition of an inline function) we
20212 used to generate a special (abbreviated) DW_TAG_structure_type,
20213 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20214 should be actually referencing those DIEs, as variable DIEs with that
20215 type would be emitted already in the abstract origin, so it was always
20216 removed during unused type prunning. Don't add anything in this
20217 case. */
20218 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20219 break;
20221 if (is_redundant_typedef (decl))
20222 gen_type_die (TREE_TYPE (decl), context_die);
20223 else
20224 /* Output a DIE to represent the typedef itself. */
20225 gen_typedef_die (decl, context_die);
20226 break;
20228 case LABEL_DECL:
20229 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20230 gen_label_die (decl, context_die);
20231 break;
20233 case VAR_DECL:
20234 case RESULT_DECL:
20235 /* If we are in terse mode, don't generate any DIEs to represent any
20236 variable declarations or definitions. */
20237 if (debug_info_level <= DINFO_LEVEL_TERSE)
20238 break;
20240 /* Output any DIEs that are needed to specify the type of this data
20241 object. */
20242 if (decl_by_reference_p (decl_or_origin))
20243 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20244 else
20245 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20247 /* And its containing type. */
20248 class_origin = decl_class_context (decl_or_origin);
20249 if (class_origin != NULL_TREE)
20250 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20252 /* And its containing namespace. */
20253 context_die = declare_in_namespace (decl_or_origin, context_die);
20255 /* Now output the DIE to represent the data object itself. This gets
20256 complicated because of the possibility that the VAR_DECL really
20257 represents an inlined instance of a formal parameter for an inline
20258 function. */
20259 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20260 if (ultimate_origin != NULL_TREE
20261 && TREE_CODE (ultimate_origin) == PARM_DECL)
20262 gen_formal_parameter_die (decl, origin,
20263 true /* Emit name attribute. */,
20264 context_die);
20265 else
20266 gen_variable_die (decl, origin, context_die);
20267 break;
20269 case FIELD_DECL:
20270 /* Ignore the nameless fields that are used to skip bits but handle C++
20271 anonymous unions and structs. */
20272 if (DECL_NAME (decl) != NULL_TREE
20273 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20274 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20276 gen_type_die (member_declared_type (decl), context_die);
20277 gen_field_die (decl, context_die);
20279 break;
20281 case PARM_DECL:
20282 if (DECL_BY_REFERENCE (decl_or_origin))
20283 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20284 else
20285 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20286 return gen_formal_parameter_die (decl, origin,
20287 true /* Emit name attribute. */,
20288 context_die);
20290 case NAMESPACE_DECL:
20291 case IMPORTED_DECL:
20292 if (dwarf_version >= 3 || !dwarf_strict)
20293 gen_namespace_die (decl, context_die);
20294 break;
20296 default:
20297 /* Probably some frontend-internal decl. Assume we don't care. */
20298 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20299 break;
20302 return NULL;
20305 /* Output debug information for global decl DECL. Called from toplev.c after
20306 compilation proper has finished. */
20308 static void
20309 dwarf2out_global_decl (tree decl)
20311 /* Output DWARF2 information for file-scope tentative data object
20312 declarations, file-scope (extern) function declarations (which
20313 had no corresponding body) and file-scope tagged type declarations
20314 and definitions which have not yet been forced out. */
20315 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20316 dwarf2out_decl (decl);
20319 /* Output debug information for type decl DECL. Called from toplev.c
20320 and from language front ends (to record built-in types). */
20321 static void
20322 dwarf2out_type_decl (tree decl, int local)
20324 if (!local)
20325 dwarf2out_decl (decl);
20328 /* Output debug information for imported module or decl DECL.
20329 NAME is non-NULL name in the lexical block if the decl has been renamed.
20330 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20331 that DECL belongs to.
20332 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20333 static void
20334 dwarf2out_imported_module_or_decl_1 (tree decl,
20335 tree name,
20336 tree lexical_block,
20337 dw_die_ref lexical_block_die)
20339 expanded_location xloc;
20340 dw_die_ref imported_die = NULL;
20341 dw_die_ref at_import_die;
20343 if (TREE_CODE (decl) == IMPORTED_DECL)
20345 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20346 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20347 gcc_assert (decl);
20349 else
20350 xloc = expand_location (input_location);
20352 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20354 at_import_die = force_type_die (TREE_TYPE (decl));
20355 /* For namespace N { typedef void T; } using N::T; base_type_die
20356 returns NULL, but DW_TAG_imported_declaration requires
20357 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20358 if (!at_import_die)
20360 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20361 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20362 at_import_die = lookup_type_die (TREE_TYPE (decl));
20363 gcc_assert (at_import_die);
20366 else
20368 at_import_die = lookup_decl_die (decl);
20369 if (!at_import_die)
20371 /* If we're trying to avoid duplicate debug info, we may not have
20372 emitted the member decl for this field. Emit it now. */
20373 if (TREE_CODE (decl) == FIELD_DECL)
20375 tree type = DECL_CONTEXT (decl);
20377 if (TYPE_CONTEXT (type)
20378 && TYPE_P (TYPE_CONTEXT (type))
20379 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20380 DINFO_USAGE_DIR_USE))
20381 return;
20382 gen_type_die_for_member (type, decl,
20383 get_context_die (TYPE_CONTEXT (type)));
20385 at_import_die = force_decl_die (decl);
20389 if (TREE_CODE (decl) == NAMESPACE_DECL)
20391 if (dwarf_version >= 3 || !dwarf_strict)
20392 imported_die = new_die (DW_TAG_imported_module,
20393 lexical_block_die,
20394 lexical_block);
20395 else
20396 return;
20398 else
20399 imported_die = new_die (DW_TAG_imported_declaration,
20400 lexical_block_die,
20401 lexical_block);
20403 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20404 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20405 if (name)
20406 add_AT_string (imported_die, DW_AT_name,
20407 IDENTIFIER_POINTER (name));
20408 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20411 /* Output debug information for imported module or decl DECL.
20412 NAME is non-NULL name in context if the decl has been renamed.
20413 CHILD is true if decl is one of the renamed decls as part of
20414 importing whole module. */
20416 static void
20417 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20418 bool child)
20420 /* dw_die_ref at_import_die; */
20421 dw_die_ref scope_die;
20423 if (debug_info_level <= DINFO_LEVEL_TERSE)
20424 return;
20426 gcc_assert (decl);
20428 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20429 We need decl DIE for reference and scope die. First, get DIE for the decl
20430 itself. */
20432 /* Get the scope die for decl context. Use comp_unit_die for global module
20433 or decl. If die is not found for non globals, force new die. */
20434 if (context
20435 && TYPE_P (context)
20436 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20437 return;
20439 if (!(dwarf_version >= 3 || !dwarf_strict))
20440 return;
20442 scope_die = get_context_die (context);
20444 if (child)
20446 gcc_assert (scope_die->die_child);
20447 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20448 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20449 scope_die = scope_die->die_child;
20452 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20453 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20457 /* Write the debugging output for DECL. */
20459 void
20460 dwarf2out_decl (tree decl)
20462 dw_die_ref context_die = comp_unit_die ();
20464 switch (TREE_CODE (decl))
20466 case ERROR_MARK:
20467 return;
20469 case FUNCTION_DECL:
20470 /* What we would really like to do here is to filter out all mere
20471 file-scope declarations of file-scope functions which are never
20472 referenced later within this translation unit (and keep all of ones
20473 that *are* referenced later on) but we aren't clairvoyant, so we have
20474 no idea which functions will be referenced in the future (i.e. later
20475 on within the current translation unit). So here we just ignore all
20476 file-scope function declarations which are not also definitions. If
20477 and when the debugger needs to know something about these functions,
20478 it will have to hunt around and find the DWARF information associated
20479 with the definition of the function.
20481 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20482 nodes represent definitions and which ones represent mere
20483 declarations. We have to check DECL_INITIAL instead. That's because
20484 the C front-end supports some weird semantics for "extern inline"
20485 function definitions. These can get inlined within the current
20486 translation unit (and thus, we need to generate Dwarf info for their
20487 abstract instances so that the Dwarf info for the concrete inlined
20488 instances can have something to refer to) but the compiler never
20489 generates any out-of-lines instances of such things (despite the fact
20490 that they *are* definitions).
20492 The important point is that the C front-end marks these "extern
20493 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20494 them anyway. Note that the C++ front-end also plays some similar games
20495 for inline function definitions appearing within include files which
20496 also contain `#pragma interface' pragmas.
20498 If we are called from dwarf2out_abstract_function output a DIE
20499 anyway. We can end up here this way with early inlining and LTO
20500 where the inlined function is output in a different LTRANS unit
20501 or not at all. */
20502 if (DECL_INITIAL (decl) == NULL_TREE
20503 && ! DECL_ABSTRACT (decl))
20504 return;
20506 /* If we're a nested function, initially use a parent of NULL; if we're
20507 a plain function, this will be fixed up in decls_for_scope. If
20508 we're a method, it will be ignored, since we already have a DIE. */
20509 if (decl_function_context (decl)
20510 /* But if we're in terse mode, we don't care about scope. */
20511 && debug_info_level > DINFO_LEVEL_TERSE)
20512 context_die = NULL;
20513 break;
20515 case VAR_DECL:
20516 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20517 declaration and if the declaration was never even referenced from
20518 within this entire compilation unit. We suppress these DIEs in
20519 order to save space in the .debug section (by eliminating entries
20520 which are probably useless). Note that we must not suppress
20521 block-local extern declarations (whether used or not) because that
20522 would screw-up the debugger's name lookup mechanism and cause it to
20523 miss things which really ought to be in scope at a given point. */
20524 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20525 return;
20527 /* For local statics lookup proper context die. */
20528 if (TREE_STATIC (decl)
20529 && DECL_CONTEXT (decl)
20530 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20531 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20533 /* If we are in terse mode, don't generate any DIEs to represent any
20534 variable declarations or definitions. */
20535 if (debug_info_level <= DINFO_LEVEL_TERSE)
20536 return;
20537 break;
20539 case CONST_DECL:
20540 if (debug_info_level <= DINFO_LEVEL_TERSE)
20541 return;
20542 if (!is_fortran () && !is_ada ())
20543 return;
20544 if (TREE_STATIC (decl) && decl_function_context (decl))
20545 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20546 break;
20548 case NAMESPACE_DECL:
20549 case IMPORTED_DECL:
20550 if (debug_info_level <= DINFO_LEVEL_TERSE)
20551 return;
20552 if (lookup_decl_die (decl) != NULL)
20553 return;
20554 break;
20556 case TYPE_DECL:
20557 /* Don't emit stubs for types unless they are needed by other DIEs. */
20558 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20559 return;
20561 /* Don't bother trying to generate any DIEs to represent any of the
20562 normal built-in types for the language we are compiling. */
20563 if (DECL_IS_BUILTIN (decl))
20564 return;
20566 /* If we are in terse mode, don't generate any DIEs for types. */
20567 if (debug_info_level <= DINFO_LEVEL_TERSE)
20568 return;
20570 /* If we're a function-scope tag, initially use a parent of NULL;
20571 this will be fixed up in decls_for_scope. */
20572 if (decl_function_context (decl))
20573 context_die = NULL;
20575 break;
20577 default:
20578 return;
20581 gen_decl_die (decl, NULL, context_die);
20584 /* Write the debugging output for DECL. */
20586 static void
20587 dwarf2out_function_decl (tree decl)
20589 dwarf2out_decl (decl);
20590 call_arg_locations = NULL;
20591 call_arg_loc_last = NULL;
20592 call_site_count = -1;
20593 tail_call_site_count = -1;
20594 block_map.release ();
20595 htab_empty (decl_loc_table);
20596 htab_empty (cached_dw_loc_list_table);
20599 /* Output a marker (i.e. a label) for the beginning of the generated code for
20600 a lexical block. */
20602 static void
20603 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20604 unsigned int blocknum)
20606 switch_to_section (current_function_section ());
20607 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20610 /* Output a marker (i.e. a label) for the end of the generated code for a
20611 lexical block. */
20613 static void
20614 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20616 switch_to_section (current_function_section ());
20617 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20620 /* Returns nonzero if it is appropriate not to emit any debugging
20621 information for BLOCK, because it doesn't contain any instructions.
20623 Don't allow this for blocks with nested functions or local classes
20624 as we would end up with orphans, and in the presence of scheduling
20625 we may end up calling them anyway. */
20627 static bool
20628 dwarf2out_ignore_block (const_tree block)
20630 tree decl;
20631 unsigned int i;
20633 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20634 if (TREE_CODE (decl) == FUNCTION_DECL
20635 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20636 return 0;
20637 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20639 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20640 if (TREE_CODE (decl) == FUNCTION_DECL
20641 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20642 return 0;
20645 return 1;
20648 /* Hash table routines for file_hash. */
20650 static int
20651 file_table_eq (const void *p1_p, const void *p2_p)
20653 const struct dwarf_file_data *const p1 =
20654 (const struct dwarf_file_data *) p1_p;
20655 const char *const p2 = (const char *) p2_p;
20656 return filename_cmp (p1->filename, p2) == 0;
20659 static hashval_t
20660 file_table_hash (const void *p_p)
20662 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20663 return htab_hash_string (p->filename);
20666 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20667 dwarf2out.c) and return its "index". The index of each (known) filename is
20668 just a unique number which is associated with only that one filename. We
20669 need such numbers for the sake of generating labels (in the .debug_sfnames
20670 section) and references to those files numbers (in the .debug_srcinfo
20671 and.debug_macinfo sections). If the filename given as an argument is not
20672 found in our current list, add it to the list and assign it the next
20673 available unique index number. In order to speed up searches, we remember
20674 the index of the filename was looked up last. This handles the majority of
20675 all searches. */
20677 static struct dwarf_file_data *
20678 lookup_filename (const char *file_name)
20680 void ** slot;
20681 struct dwarf_file_data * created;
20683 /* Check to see if the file name that was searched on the previous
20684 call matches this file name. If so, return the index. */
20685 if (file_table_last_lookup
20686 && (file_name == file_table_last_lookup->filename
20687 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20688 return file_table_last_lookup;
20690 /* Didn't match the previous lookup, search the table. */
20691 slot = htab_find_slot_with_hash (file_table, file_name,
20692 htab_hash_string (file_name), INSERT);
20693 if (*slot)
20694 return (struct dwarf_file_data *) *slot;
20696 created = ggc_alloc_dwarf_file_data ();
20697 created->filename = file_name;
20698 created->emitted_number = 0;
20699 *slot = created;
20700 return created;
20703 /* If the assembler will construct the file table, then translate the compiler
20704 internal file table number into the assembler file table number, and emit
20705 a .file directive if we haven't already emitted one yet. The file table
20706 numbers are different because we prune debug info for unused variables and
20707 types, which may include filenames. */
20709 static int
20710 maybe_emit_file (struct dwarf_file_data * fd)
20712 if (! fd->emitted_number)
20714 if (last_emitted_file)
20715 fd->emitted_number = last_emitted_file->emitted_number + 1;
20716 else
20717 fd->emitted_number = 1;
20718 last_emitted_file = fd;
20720 if (DWARF2_ASM_LINE_DEBUG_INFO)
20722 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20723 output_quoted_string (asm_out_file,
20724 remap_debug_filename (fd->filename));
20725 fputc ('\n', asm_out_file);
20729 return fd->emitted_number;
20732 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20733 That generation should happen after function debug info has been
20734 generated. The value of the attribute is the constant value of ARG. */
20736 static void
20737 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20739 die_arg_entry entry;
20741 if (!die || !arg)
20742 return;
20744 if (!tmpl_value_parm_die_table)
20745 vec_alloc (tmpl_value_parm_die_table, 32);
20747 entry.die = die;
20748 entry.arg = arg;
20749 vec_safe_push (tmpl_value_parm_die_table, entry);
20752 /* Return TRUE if T is an instance of generic type, FALSE
20753 otherwise. */
20755 static bool
20756 generic_type_p (tree t)
20758 if (t == NULL_TREE || !TYPE_P (t))
20759 return false;
20760 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20763 /* Schedule the generation of the generic parameter dies for the
20764 instance of generic type T. The proper generation itself is later
20765 done by gen_scheduled_generic_parms_dies. */
20767 static void
20768 schedule_generic_params_dies_gen (tree t)
20770 if (!generic_type_p (t))
20771 return;
20773 if (!generic_type_instances)
20774 vec_alloc (generic_type_instances, 256);
20776 vec_safe_push (generic_type_instances, t);
20779 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20780 by append_entry_to_tmpl_value_parm_die_table. This function must
20781 be called after function DIEs have been generated. */
20783 static void
20784 gen_remaining_tmpl_value_param_die_attribute (void)
20786 if (tmpl_value_parm_die_table)
20788 unsigned i;
20789 die_arg_entry *e;
20791 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20792 tree_add_const_value_attribute (e->die, e->arg);
20796 /* Generate generic parameters DIEs for instances of generic types
20797 that have been previously scheduled by
20798 schedule_generic_params_dies_gen. This function must be called
20799 after all the types of the CU have been laid out. */
20801 static void
20802 gen_scheduled_generic_parms_dies (void)
20804 unsigned i;
20805 tree t;
20807 if (!generic_type_instances)
20808 return;
20810 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20811 if (COMPLETE_TYPE_P (t))
20812 gen_generic_params_dies (t);
20816 /* Replace DW_AT_name for the decl with name. */
20818 static void
20819 dwarf2out_set_name (tree decl, tree name)
20821 dw_die_ref die;
20822 dw_attr_ref attr;
20823 const char *dname;
20825 die = TYPE_SYMTAB_DIE (decl);
20826 if (!die)
20827 return;
20829 dname = dwarf2_name (name, 0);
20830 if (!dname)
20831 return;
20833 attr = get_AT (die, DW_AT_name);
20834 if (attr)
20836 struct indirect_string_node *node;
20838 node = find_AT_string (dname);
20839 /* replace the string. */
20840 attr->dw_attr_val.v.val_str = node;
20843 else
20844 add_name_attribute (die, dname);
20847 /* True if before or during processing of the first function being emitted. */
20848 static bool in_first_function_p = true;
20849 /* True if loc_note during dwarf2out_var_location call might still be
20850 before first real instruction at address equal to .Ltext0. */
20851 static bool maybe_at_text_label_p = true;
20852 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20853 static unsigned int first_loclabel_num_not_at_text_label;
20855 /* Called by the final INSN scan whenever we see a var location. We
20856 use it to drop labels in the right places, and throw the location in
20857 our lookup table. */
20859 static void
20860 dwarf2out_var_location (rtx loc_note)
20862 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20863 struct var_loc_node *newloc;
20864 rtx next_real, next_note;
20865 static const char *last_label;
20866 static const char *last_postcall_label;
20867 static bool last_in_cold_section_p;
20868 static rtx expected_next_loc_note;
20869 tree decl;
20870 bool var_loc_p;
20872 if (!NOTE_P (loc_note))
20874 if (CALL_P (loc_note))
20876 call_site_count++;
20877 if (SIBLING_CALL_P (loc_note))
20878 tail_call_site_count++;
20880 return;
20883 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20884 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20885 return;
20887 /* Optimize processing a large consecutive sequence of location
20888 notes so we don't spend too much time in next_real_insn. If the
20889 next insn is another location note, remember the next_real_insn
20890 calculation for next time. */
20891 next_real = cached_next_real_insn;
20892 if (next_real)
20894 if (expected_next_loc_note != loc_note)
20895 next_real = NULL_RTX;
20898 next_note = NEXT_INSN (loc_note);
20899 if (! next_note
20900 || INSN_DELETED_P (next_note)
20901 || ! NOTE_P (next_note)
20902 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20903 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20904 next_note = NULL_RTX;
20906 if (! next_real)
20907 next_real = next_real_insn (loc_note);
20909 if (next_note)
20911 expected_next_loc_note = next_note;
20912 cached_next_real_insn = next_real;
20914 else
20915 cached_next_real_insn = NULL_RTX;
20917 /* If there are no instructions which would be affected by this note,
20918 don't do anything. */
20919 if (var_loc_p
20920 && next_real == NULL_RTX
20921 && !NOTE_DURING_CALL_P (loc_note))
20922 return;
20924 if (next_real == NULL_RTX)
20925 next_real = get_last_insn ();
20927 /* If there were any real insns between note we processed last time
20928 and this note (or if it is the first note), clear
20929 last_{,postcall_}label so that they are not reused this time. */
20930 if (last_var_location_insn == NULL_RTX
20931 || last_var_location_insn != next_real
20932 || last_in_cold_section_p != in_cold_section_p)
20934 last_label = NULL;
20935 last_postcall_label = NULL;
20938 if (var_loc_p)
20940 decl = NOTE_VAR_LOCATION_DECL (loc_note);
20941 newloc = add_var_loc_to_decl (decl, loc_note,
20942 NOTE_DURING_CALL_P (loc_note)
20943 ? last_postcall_label : last_label);
20944 if (newloc == NULL)
20945 return;
20947 else
20949 decl = NULL_TREE;
20950 newloc = NULL;
20953 /* If there were no real insns between note we processed last time
20954 and this note, use the label we emitted last time. Otherwise
20955 create a new label and emit it. */
20956 if (last_label == NULL)
20958 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20959 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20960 loclabel_num++;
20961 last_label = ggc_strdup (loclabel);
20962 /* See if loclabel might be equal to .Ltext0. If yes,
20963 bump first_loclabel_num_not_at_text_label. */
20964 if (!have_multiple_function_sections
20965 && in_first_function_p
20966 && maybe_at_text_label_p)
20968 static rtx last_start;
20969 rtx insn;
20970 for (insn = loc_note; insn; insn = previous_insn (insn))
20971 if (insn == last_start)
20972 break;
20973 else if (!NONDEBUG_INSN_P (insn))
20974 continue;
20975 else
20977 rtx body = PATTERN (insn);
20978 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
20979 continue;
20980 /* Inline asm could occupy zero bytes. */
20981 else if (GET_CODE (body) == ASM_INPUT
20982 || asm_noperands (body) >= 0)
20983 continue;
20984 #ifdef HAVE_attr_length
20985 else if (get_attr_min_length (insn) == 0)
20986 continue;
20987 #endif
20988 else
20990 /* Assume insn has non-zero length. */
20991 maybe_at_text_label_p = false;
20992 break;
20995 if (maybe_at_text_label_p)
20997 last_start = loc_note;
20998 first_loclabel_num_not_at_text_label = loclabel_num;
21003 if (!var_loc_p)
21005 struct call_arg_loc_node *ca_loc
21006 = ggc_alloc_cleared_call_arg_loc_node ();
21007 rtx prev = prev_real_insn (loc_note), x;
21008 ca_loc->call_arg_loc_note = loc_note;
21009 ca_loc->next = NULL;
21010 ca_loc->label = last_label;
21011 gcc_assert (prev
21012 && (CALL_P (prev)
21013 || (NONJUMP_INSN_P (prev)
21014 && GET_CODE (PATTERN (prev)) == SEQUENCE
21015 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21016 if (!CALL_P (prev))
21017 prev = XVECEXP (PATTERN (prev), 0, 0);
21018 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21019 x = get_call_rtx_from (PATTERN (prev));
21020 if (x)
21022 x = XEXP (XEXP (x, 0), 0);
21023 if (GET_CODE (x) == SYMBOL_REF
21024 && SYMBOL_REF_DECL (x)
21025 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21026 ca_loc->symbol_ref = x;
21028 ca_loc->block = insn_scope (prev);
21029 if (call_arg_locations)
21030 call_arg_loc_last->next = ca_loc;
21031 else
21032 call_arg_locations = ca_loc;
21033 call_arg_loc_last = ca_loc;
21035 else if (!NOTE_DURING_CALL_P (loc_note))
21036 newloc->label = last_label;
21037 else
21039 if (!last_postcall_label)
21041 sprintf (loclabel, "%s-1", last_label);
21042 last_postcall_label = ggc_strdup (loclabel);
21044 newloc->label = last_postcall_label;
21047 last_var_location_insn = next_real;
21048 last_in_cold_section_p = in_cold_section_p;
21051 /* Note in one location list that text section has changed. */
21053 static int
21054 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21056 var_loc_list *list = (var_loc_list *) *slot;
21057 if (list->first)
21058 list->last_before_switch
21059 = list->last->next ? list->last->next : list->last;
21060 return 1;
21063 /* Note in all location lists that text section has changed. */
21065 static void
21066 var_location_switch_text_section (void)
21068 if (decl_loc_table == NULL)
21069 return;
21071 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21074 /* Create a new line number table. */
21076 static dw_line_info_table *
21077 new_line_info_table (void)
21079 dw_line_info_table *table;
21081 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21082 table->file_num = 1;
21083 table->line_num = 1;
21084 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21086 return table;
21089 /* Lookup the "current" table into which we emit line info, so
21090 that we don't have to do it for every source line. */
21092 static void
21093 set_cur_line_info_table (section *sec)
21095 dw_line_info_table *table;
21097 if (sec == text_section)
21098 table = text_section_line_info;
21099 else if (sec == cold_text_section)
21101 table = cold_text_section_line_info;
21102 if (!table)
21104 cold_text_section_line_info = table = new_line_info_table ();
21105 table->end_label = cold_end_label;
21108 else
21110 const char *end_label;
21112 if (flag_reorder_blocks_and_partition)
21114 if (in_cold_section_p)
21115 end_label = crtl->subsections.cold_section_end_label;
21116 else
21117 end_label = crtl->subsections.hot_section_end_label;
21119 else
21121 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21122 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21123 current_function_funcdef_no);
21124 end_label = ggc_strdup (label);
21127 table = new_line_info_table ();
21128 table->end_label = end_label;
21130 vec_safe_push (separate_line_info, table);
21133 if (DWARF2_ASM_LINE_DEBUG_INFO)
21134 table->is_stmt = (cur_line_info_table
21135 ? cur_line_info_table->is_stmt
21136 : DWARF_LINE_DEFAULT_IS_STMT_START);
21137 cur_line_info_table = table;
21141 /* We need to reset the locations at the beginning of each
21142 function. We can't do this in the end_function hook, because the
21143 declarations that use the locations won't have been output when
21144 that hook is called. Also compute have_multiple_function_sections here. */
21146 static void
21147 dwarf2out_begin_function (tree fun)
21149 section *sec = function_section (fun);
21151 if (sec != text_section)
21152 have_multiple_function_sections = true;
21154 if (flag_reorder_blocks_and_partition && !cold_text_section)
21156 gcc_assert (current_function_decl == fun);
21157 cold_text_section = unlikely_text_section ();
21158 switch_to_section (cold_text_section);
21159 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21160 switch_to_section (sec);
21163 dwarf2out_note_section_used ();
21164 call_site_count = 0;
21165 tail_call_site_count = 0;
21167 set_cur_line_info_table (sec);
21170 /* Helper function of dwarf2out_end_function, called only after emitting
21171 the very first function into assembly. Check if some .debug_loc range
21172 might end with a .LVL* label that could be equal to .Ltext0.
21173 In that case we must force using absolute addresses in .debug_loc ranges,
21174 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21175 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21176 list terminator.
21177 Set have_multiple_function_sections to true in that case and
21178 terminate htab traversal. */
21180 static int
21181 find_empty_loc_ranges_at_text_label (void **slot, void *)
21183 var_loc_list *entry;
21184 struct var_loc_node *node;
21186 entry = (var_loc_list *) *slot;
21187 node = entry->first;
21188 if (node && node->next && node->next->label)
21190 unsigned int i;
21191 const char *label = node->next->label;
21192 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21194 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21196 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21197 if (strcmp (label, loclabel) == 0)
21199 have_multiple_function_sections = true;
21200 return 0;
21204 return 1;
21207 /* Hook called after emitting a function into assembly.
21208 This does something only for the very first function emitted. */
21210 static void
21211 dwarf2out_end_function (unsigned int)
21213 if (in_first_function_p
21214 && !have_multiple_function_sections
21215 && first_loclabel_num_not_at_text_label
21216 && decl_loc_table)
21217 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21218 NULL);
21219 in_first_function_p = false;
21220 maybe_at_text_label_p = false;
21223 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21225 static void
21226 push_dw_line_info_entry (dw_line_info_table *table,
21227 enum dw_line_info_opcode opcode, unsigned int val)
21229 dw_line_info_entry e;
21230 e.opcode = opcode;
21231 e.val = val;
21232 vec_safe_push (table->entries, e);
21235 /* Output a label to mark the beginning of a source code line entry
21236 and record information relating to this source line, in
21237 'line_info_table' for later output of the .debug_line section. */
21238 /* ??? The discriminator parameter ought to be unsigned. */
21240 static void
21241 dwarf2out_source_line (unsigned int line, const char *filename,
21242 int discriminator, bool is_stmt)
21244 unsigned int file_num;
21245 dw_line_info_table *table;
21247 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21248 return;
21250 /* The discriminator column was added in dwarf4. Simplify the below
21251 by simply removing it if we're not supposed to output it. */
21252 if (dwarf_version < 4 && dwarf_strict)
21253 discriminator = 0;
21255 table = cur_line_info_table;
21256 file_num = maybe_emit_file (lookup_filename (filename));
21258 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21259 the debugger has used the second (possibly duplicate) line number
21260 at the beginning of the function to mark the end of the prologue.
21261 We could eliminate any other duplicates within the function. For
21262 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21263 that second line number entry. */
21264 /* Recall that this end-of-prologue indication is *not* the same thing
21265 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21266 to which the hook corresponds, follows the last insn that was
21267 emitted by gen_prologue. What we need is to precede the first insn
21268 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21269 insn that corresponds to something the user wrote. These may be
21270 very different locations once scheduling is enabled. */
21272 if (0 && file_num == table->file_num
21273 && line == table->line_num
21274 && discriminator == table->discrim_num
21275 && is_stmt == table->is_stmt)
21276 return;
21278 switch_to_section (current_function_section ());
21280 /* If requested, emit something human-readable. */
21281 if (flag_debug_asm)
21282 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21284 if (DWARF2_ASM_LINE_DEBUG_INFO)
21286 /* Emit the .loc directive understood by GNU as. */
21287 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21288 file_num, line, is_stmt, discriminator */
21289 fputs ("\t.loc ", asm_out_file);
21290 fprint_ul (asm_out_file, file_num);
21291 putc (' ', asm_out_file);
21292 fprint_ul (asm_out_file, line);
21293 putc (' ', asm_out_file);
21294 putc ('0', asm_out_file);
21296 if (is_stmt != table->is_stmt)
21298 fputs (" is_stmt ", asm_out_file);
21299 putc (is_stmt ? '1' : '0', asm_out_file);
21301 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21303 gcc_assert (discriminator > 0);
21304 fputs (" discriminator ", asm_out_file);
21305 fprint_ul (asm_out_file, (unsigned long) discriminator);
21307 putc ('\n', asm_out_file);
21309 else
21311 unsigned int label_num = ++line_info_label_num;
21313 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21315 push_dw_line_info_entry (table, LI_set_address, label_num);
21316 if (file_num != table->file_num)
21317 push_dw_line_info_entry (table, LI_set_file, file_num);
21318 if (discriminator != table->discrim_num)
21319 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21320 if (is_stmt != table->is_stmt)
21321 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21322 push_dw_line_info_entry (table, LI_set_line, line);
21325 table->file_num = file_num;
21326 table->line_num = line;
21327 table->discrim_num = discriminator;
21328 table->is_stmt = is_stmt;
21329 table->in_use = true;
21332 /* Record the beginning of a new source file. */
21334 static void
21335 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21337 if (flag_eliminate_dwarf2_dups)
21339 /* Record the beginning of the file for break_out_includes. */
21340 dw_die_ref bincl_die;
21342 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21343 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21346 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21348 macinfo_entry e;
21349 e.code = DW_MACINFO_start_file;
21350 e.lineno = lineno;
21351 e.info = ggc_strdup (filename);
21352 vec_safe_push (macinfo_table, e);
21356 /* Record the end of a source file. */
21358 static void
21359 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21361 if (flag_eliminate_dwarf2_dups)
21362 /* Record the end of the file for break_out_includes. */
21363 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21365 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21367 macinfo_entry e;
21368 e.code = DW_MACINFO_end_file;
21369 e.lineno = lineno;
21370 e.info = NULL;
21371 vec_safe_push (macinfo_table, e);
21375 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21376 the tail part of the directive line, i.e. the part which is past the
21377 initial whitespace, #, whitespace, directive-name, whitespace part. */
21379 static void
21380 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21381 const char *buffer ATTRIBUTE_UNUSED)
21383 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21385 macinfo_entry e;
21386 /* Insert a dummy first entry to be able to optimize the whole
21387 predefined macro block using DW_MACRO_GNU_transparent_include. */
21388 if (macinfo_table->is_empty () && lineno <= 1)
21390 e.code = 0;
21391 e.lineno = 0;
21392 e.info = NULL;
21393 vec_safe_push (macinfo_table, e);
21395 e.code = DW_MACINFO_define;
21396 e.lineno = lineno;
21397 e.info = ggc_strdup (buffer);
21398 vec_safe_push (macinfo_table, e);
21402 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21403 the tail part of the directive line, i.e. the part which is past the
21404 initial whitespace, #, whitespace, directive-name, whitespace part. */
21406 static void
21407 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21408 const char *buffer ATTRIBUTE_UNUSED)
21410 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21412 macinfo_entry e;
21413 /* Insert a dummy first entry to be able to optimize the whole
21414 predefined macro block using DW_MACRO_GNU_transparent_include. */
21415 if (macinfo_table->is_empty () && lineno <= 1)
21417 e.code = 0;
21418 e.lineno = 0;
21419 e.info = NULL;
21420 vec_safe_push (macinfo_table, e);
21422 e.code = DW_MACINFO_undef;
21423 e.lineno = lineno;
21424 e.info = ggc_strdup (buffer);
21425 vec_safe_push (macinfo_table, e);
21429 /* Helpers to manipulate hash table of CUs. */
21431 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21433 typedef macinfo_entry value_type;
21434 typedef macinfo_entry compare_type;
21435 static inline hashval_t hash (const value_type *);
21436 static inline bool equal (const value_type *, const compare_type *);
21439 inline hashval_t
21440 macinfo_entry_hasher::hash (const value_type *entry)
21442 return htab_hash_string (entry->info);
21445 inline bool
21446 macinfo_entry_hasher::equal (const value_type *entry1,
21447 const compare_type *entry2)
21449 return !strcmp (entry1->info, entry2->info);
21452 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21454 /* Output a single .debug_macinfo entry. */
21456 static void
21457 output_macinfo_op (macinfo_entry *ref)
21459 int file_num;
21460 size_t len;
21461 struct indirect_string_node *node;
21462 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21463 struct dwarf_file_data *fd;
21465 switch (ref->code)
21467 case DW_MACINFO_start_file:
21468 fd = lookup_filename (ref->info);
21469 file_num = maybe_emit_file (fd);
21470 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21471 dw2_asm_output_data_uleb128 (ref->lineno,
21472 "Included from line number %lu",
21473 (unsigned long) ref->lineno);
21474 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21475 break;
21476 case DW_MACINFO_end_file:
21477 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21478 break;
21479 case DW_MACINFO_define:
21480 case DW_MACINFO_undef:
21481 len = strlen (ref->info) + 1;
21482 if (!dwarf_strict
21483 && len > DWARF_OFFSET_SIZE
21484 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21485 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21487 ref->code = ref->code == DW_MACINFO_define
21488 ? DW_MACRO_GNU_define_indirect
21489 : DW_MACRO_GNU_undef_indirect;
21490 output_macinfo_op (ref);
21491 return;
21493 dw2_asm_output_data (1, ref->code,
21494 ref->code == DW_MACINFO_define
21495 ? "Define macro" : "Undefine macro");
21496 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21497 (unsigned long) ref->lineno);
21498 dw2_asm_output_nstring (ref->info, -1, "The macro");
21499 break;
21500 case DW_MACRO_GNU_define_indirect:
21501 case DW_MACRO_GNU_undef_indirect:
21502 node = find_AT_string (ref->info);
21503 gcc_assert (node
21504 && ((node->form == DW_FORM_strp)
21505 || (node->form == DW_FORM_GNU_str_index)));
21506 dw2_asm_output_data (1, ref->code,
21507 ref->code == DW_MACRO_GNU_define_indirect
21508 ? "Define macro indirect"
21509 : "Undefine macro indirect");
21510 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21511 (unsigned long) ref->lineno);
21512 if (node->form == DW_FORM_strp)
21513 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21514 debug_str_section, "The macro: \"%s\"",
21515 ref->info);
21516 else
21517 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21518 ref->info);
21519 break;
21520 case DW_MACRO_GNU_transparent_include:
21521 dw2_asm_output_data (1, ref->code, "Transparent include");
21522 ASM_GENERATE_INTERNAL_LABEL (label,
21523 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21524 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21525 break;
21526 default:
21527 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21528 ASM_COMMENT_START, (unsigned long) ref->code);
21529 break;
21533 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21534 other compilation unit .debug_macinfo sections. IDX is the first
21535 index of a define/undef, return the number of ops that should be
21536 emitted in a comdat .debug_macinfo section and emit
21537 a DW_MACRO_GNU_transparent_include entry referencing it.
21538 If the define/undef entry should be emitted normally, return 0. */
21540 static unsigned
21541 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21542 macinfo_hash_type *macinfo_htab)
21544 macinfo_entry *first, *second, *cur, *inc;
21545 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21546 unsigned char checksum[16];
21547 struct md5_ctx ctx;
21548 char *grp_name, *tail;
21549 const char *base;
21550 unsigned int i, count, encoded_filename_len, linebuf_len;
21551 macinfo_entry **slot;
21553 first = &(*macinfo_table)[idx];
21554 second = &(*macinfo_table)[idx + 1];
21556 /* Optimize only if there are at least two consecutive define/undef ops,
21557 and either all of them are before first DW_MACINFO_start_file
21558 with lineno {0,1} (i.e. predefined macro block), or all of them are
21559 in some included header file. */
21560 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21561 return 0;
21562 if (vec_safe_is_empty (files))
21564 if (first->lineno > 1 || second->lineno > 1)
21565 return 0;
21567 else if (first->lineno == 0)
21568 return 0;
21570 /* Find the last define/undef entry that can be grouped together
21571 with first and at the same time compute md5 checksum of their
21572 codes, linenumbers and strings. */
21573 md5_init_ctx (&ctx);
21574 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21575 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21576 break;
21577 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21578 break;
21579 else
21581 unsigned char code = cur->code;
21582 md5_process_bytes (&code, 1, &ctx);
21583 checksum_uleb128 (cur->lineno, &ctx);
21584 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21586 md5_finish_ctx (&ctx, checksum);
21587 count = i - idx;
21589 /* From the containing include filename (if any) pick up just
21590 usable characters from its basename. */
21591 if (vec_safe_is_empty (files))
21592 base = "";
21593 else
21594 base = lbasename (files->last ().info);
21595 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21596 if (ISIDNUM (base[i]) || base[i] == '.')
21597 encoded_filename_len++;
21598 /* Count . at the end. */
21599 if (encoded_filename_len)
21600 encoded_filename_len++;
21602 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21603 linebuf_len = strlen (linebuf);
21605 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21606 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21607 + 16 * 2 + 1);
21608 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21609 tail = grp_name + 4;
21610 if (encoded_filename_len)
21612 for (i = 0; base[i]; i++)
21613 if (ISIDNUM (base[i]) || base[i] == '.')
21614 *tail++ = base[i];
21615 *tail++ = '.';
21617 memcpy (tail, linebuf, linebuf_len);
21618 tail += linebuf_len;
21619 *tail++ = '.';
21620 for (i = 0; i < 16; i++)
21621 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21623 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21624 in the empty vector entry before the first define/undef. */
21625 inc = &(*macinfo_table)[idx - 1];
21626 inc->code = DW_MACRO_GNU_transparent_include;
21627 inc->lineno = 0;
21628 inc->info = ggc_strdup (grp_name);
21629 if (!macinfo_htab->is_created ())
21630 macinfo_htab->create (10);
21631 /* Avoid emitting duplicates. */
21632 slot = macinfo_htab->find_slot (inc, INSERT);
21633 if (*slot != NULL)
21635 inc->code = 0;
21636 inc->info = NULL;
21637 /* If such an entry has been used before, just emit
21638 a DW_MACRO_GNU_transparent_include op. */
21639 inc = *slot;
21640 output_macinfo_op (inc);
21641 /* And clear all macinfo_entry in the range to avoid emitting them
21642 in the second pass. */
21643 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21645 cur->code = 0;
21646 cur->info = NULL;
21649 else
21651 *slot = inc;
21652 inc->lineno = macinfo_htab->elements ();
21653 output_macinfo_op (inc);
21655 return count;
21658 /* Save any strings needed by the macinfo table in the debug str
21659 table. All strings must be collected into the table by the time
21660 index_string is called. */
21662 static void
21663 save_macinfo_strings (void)
21665 unsigned len;
21666 unsigned i;
21667 macinfo_entry *ref;
21669 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21671 switch (ref->code)
21673 /* Match the logic in output_macinfo_op to decide on
21674 indirect strings. */
21675 case DW_MACINFO_define:
21676 case DW_MACINFO_undef:
21677 len = strlen (ref->info) + 1;
21678 if (!dwarf_strict
21679 && len > DWARF_OFFSET_SIZE
21680 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21681 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21682 set_indirect_string (find_AT_string (ref->info));
21683 break;
21684 case DW_MACRO_GNU_define_indirect:
21685 case DW_MACRO_GNU_undef_indirect:
21686 set_indirect_string (find_AT_string (ref->info));
21687 break;
21688 default:
21689 break;
21694 /* Output macinfo section(s). */
21696 static void
21697 output_macinfo (void)
21699 unsigned i;
21700 unsigned long length = vec_safe_length (macinfo_table);
21701 macinfo_entry *ref;
21702 vec<macinfo_entry, va_gc> *files = NULL;
21703 macinfo_hash_type macinfo_htab;
21705 if (! length)
21706 return;
21708 /* output_macinfo* uses these interchangeably. */
21709 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21710 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21711 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21712 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21714 /* For .debug_macro emit the section header. */
21715 if (!dwarf_strict)
21717 dw2_asm_output_data (2, 4, "DWARF macro version number");
21718 if (DWARF_OFFSET_SIZE == 8)
21719 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21720 else
21721 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21722 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21723 (!dwarf_split_debug_info ? debug_line_section_label
21724 : debug_skeleton_line_section_label),
21725 debug_line_section, NULL);
21728 /* In the first loop, it emits the primary .debug_macinfo section
21729 and after each emitted op the macinfo_entry is cleared.
21730 If a longer range of define/undef ops can be optimized using
21731 DW_MACRO_GNU_transparent_include, the
21732 DW_MACRO_GNU_transparent_include op is emitted and kept in
21733 the vector before the first define/undef in the range and the
21734 whole range of define/undef ops is not emitted and kept. */
21735 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21737 switch (ref->code)
21739 case DW_MACINFO_start_file:
21740 vec_safe_push (files, *ref);
21741 break;
21742 case DW_MACINFO_end_file:
21743 if (!vec_safe_is_empty (files))
21744 files->pop ();
21745 break;
21746 case DW_MACINFO_define:
21747 case DW_MACINFO_undef:
21748 if (!dwarf_strict
21749 && HAVE_COMDAT_GROUP
21750 && vec_safe_length (files) != 1
21751 && i > 0
21752 && i + 1 < length
21753 && (*macinfo_table)[i - 1].code == 0)
21755 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21756 if (count)
21758 i += count - 1;
21759 continue;
21762 break;
21763 case 0:
21764 /* A dummy entry may be inserted at the beginning to be able
21765 to optimize the whole block of predefined macros. */
21766 if (i == 0)
21767 continue;
21768 default:
21769 break;
21771 output_macinfo_op (ref);
21772 ref->info = NULL;
21773 ref->code = 0;
21776 if (!macinfo_htab.is_created ())
21777 return;
21779 macinfo_htab.dispose ();
21781 /* If any DW_MACRO_GNU_transparent_include were used, on those
21782 DW_MACRO_GNU_transparent_include entries terminate the
21783 current chain and switch to a new comdat .debug_macinfo
21784 section and emit the define/undef entries within it. */
21785 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21786 switch (ref->code)
21788 case 0:
21789 continue;
21790 case DW_MACRO_GNU_transparent_include:
21792 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21793 tree comdat_key = get_identifier (ref->info);
21794 /* Terminate the previous .debug_macinfo section. */
21795 dw2_asm_output_data (1, 0, "End compilation unit");
21796 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21797 SECTION_DEBUG
21798 | SECTION_LINKONCE,
21799 comdat_key);
21800 ASM_GENERATE_INTERNAL_LABEL (label,
21801 DEBUG_MACRO_SECTION_LABEL,
21802 ref->lineno);
21803 ASM_OUTPUT_LABEL (asm_out_file, label);
21804 ref->code = 0;
21805 ref->info = NULL;
21806 dw2_asm_output_data (2, 4, "DWARF macro version number");
21807 if (DWARF_OFFSET_SIZE == 8)
21808 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21809 else
21810 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21812 break;
21813 case DW_MACINFO_define:
21814 case DW_MACINFO_undef:
21815 output_macinfo_op (ref);
21816 ref->code = 0;
21817 ref->info = NULL;
21818 break;
21819 default:
21820 gcc_unreachable ();
21824 /* Set up for Dwarf output at the start of compilation. */
21826 static void
21827 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21829 /* Allocate the file_table. */
21830 file_table = htab_create_ggc (50, file_table_hash,
21831 file_table_eq, NULL);
21833 /* Allocate the decl_die_table. */
21834 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21835 decl_die_table_eq, NULL);
21837 /* Allocate the decl_loc_table. */
21838 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21839 decl_loc_table_eq, NULL);
21841 /* Allocate the cached_dw_loc_list_table. */
21842 cached_dw_loc_list_table
21843 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21844 cached_dw_loc_list_table_eq, NULL);
21846 /* Allocate the initial hunk of the decl_scope_table. */
21847 vec_alloc (decl_scope_table, 256);
21849 /* Allocate the initial hunk of the abbrev_die_table. */
21850 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21851 (ABBREV_DIE_TABLE_INCREMENT);
21852 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21853 /* Zero-th entry is allocated, but unused. */
21854 abbrev_die_table_in_use = 1;
21856 /* Allocate the pubtypes and pubnames vectors. */
21857 vec_alloc (pubname_table, 32);
21858 vec_alloc (pubtype_table, 32);
21860 vec_alloc (incomplete_types, 64);
21862 vec_alloc (used_rtx_array, 32);
21864 if (!dwarf_split_debug_info)
21866 debug_info_section = get_section (DEBUG_INFO_SECTION,
21867 SECTION_DEBUG, NULL);
21868 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21869 SECTION_DEBUG, NULL);
21870 debug_loc_section = get_section (DEBUG_LOC_SECTION,
21871 SECTION_DEBUG, NULL);
21873 else
21875 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21876 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21877 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21878 SECTION_DEBUG | SECTION_EXCLUDE,
21879 NULL);
21880 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21881 SECTION_DEBUG, NULL);
21882 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21883 SECTION_DEBUG, NULL);
21884 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21885 SECTION_DEBUG, NULL);
21886 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21887 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21889 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21890 the main .o, but the skeleton_line goes into the split off dwo. */
21891 debug_skeleton_line_section
21892 = get_section (DEBUG_DWO_LINE_SECTION,
21893 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21894 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21895 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21896 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21897 SECTION_DEBUG | SECTION_EXCLUDE,
21898 NULL);
21899 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21900 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21901 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21902 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21903 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
21904 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
21906 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21907 SECTION_DEBUG, NULL);
21908 debug_macinfo_section = get_section (dwarf_strict
21909 ? DEBUG_MACINFO_SECTION
21910 : DEBUG_MACRO_SECTION,
21911 DEBUG_MACRO_SECTION_FLAGS, NULL);
21912 debug_line_section = get_section (DEBUG_LINE_SECTION,
21913 SECTION_DEBUG, NULL);
21914 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21915 SECTION_DEBUG, NULL);
21916 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21917 SECTION_DEBUG, NULL);
21918 debug_str_section = get_section (DEBUG_STR_SECTION,
21919 DEBUG_STR_SECTION_FLAGS, NULL);
21920 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21921 SECTION_DEBUG, NULL);
21922 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21923 SECTION_DEBUG, NULL);
21925 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21926 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21927 DEBUG_ABBREV_SECTION_LABEL, 0);
21928 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21929 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21930 COLD_TEXT_SECTION_LABEL, 0);
21931 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21933 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21934 DEBUG_INFO_SECTION_LABEL, 0);
21935 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21936 DEBUG_LINE_SECTION_LABEL, 0);
21937 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21938 DEBUG_RANGES_SECTION_LABEL, 0);
21939 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21940 DEBUG_ADDR_SECTION_LABEL, 0);
21941 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21942 dwarf_strict
21943 ? DEBUG_MACINFO_SECTION_LABEL
21944 : DEBUG_MACRO_SECTION_LABEL, 0);
21945 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21947 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21948 vec_alloc (macinfo_table, 64);
21950 switch_to_section (text_section);
21951 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21953 /* Make sure the line number table for .text always exists. */
21954 text_section_line_info = new_line_info_table ();
21955 text_section_line_info->end_label = text_end_label;
21958 /* Called before compile () starts outputtting functions, variables
21959 and toplevel asms into assembly. */
21961 static void
21962 dwarf2out_assembly_start (void)
21964 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21965 && dwarf2out_do_cfi_asm ()
21966 && (!(flag_unwind_tables || flag_exceptions)
21967 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21968 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21971 /* A helper function for dwarf2out_finish called through
21972 htab_traverse. Assign a string its index. All strings must be
21973 collected into the table by the time index_string is called,
21974 because the indexing code relies on htab_traverse to traverse nodes
21975 in the same order for each run. */
21977 static int
21978 index_string (void **h, void *v)
21980 struct indirect_string_node *node = (struct indirect_string_node *) *h;
21981 unsigned int *index = (unsigned int *) v;
21983 find_string_form (node);
21984 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21986 gcc_assert(node->index == NO_INDEX_ASSIGNED);
21987 node->index = *index;
21988 *index += 1;
21990 return 1;
21993 /* A helper function for output_indirect_strings called through
21994 htab_traverse. Output the offset to a string and update the
21995 current offset. */
21997 static int
21998 output_index_string_offset (void **h, void *v)
22000 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22001 unsigned int *offset = (unsigned int *) v;
22003 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22005 /* Assert that this node has been assigned an index. */
22006 gcc_assert (node->index != NO_INDEX_ASSIGNED
22007 && node->index != NOT_INDEXED);
22008 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22009 "indexed string 0x%x: %s", node->index, node->str);
22010 *offset += strlen (node->str) + 1;
22012 return 1;
22015 /* A helper function for dwarf2out_finish called through
22016 htab_traverse. Output the indexed string. */
22018 static int
22019 output_index_string (void **h, void *v)
22021 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22022 unsigned int *cur_idx = (unsigned int *) v;
22024 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22026 /* Assert that the strings are output in the same order as their
22027 indexes were assigned. */
22028 gcc_assert (*cur_idx == node->index);
22029 assemble_string (node->str, strlen (node->str) + 1);
22030 *cur_idx += 1;
22032 return 1;
22035 /* A helper function for dwarf2out_finish called through
22036 htab_traverse. Emit one queued .debug_str string. */
22038 static int
22039 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22041 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22043 node->form = find_string_form (node);
22044 if (node->form == DW_FORM_strp && node->refcount > 0)
22046 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22047 assemble_string (node->str, strlen (node->str) + 1);
22050 return 1;
22053 /* Output the indexed string table. */
22055 static void
22056 output_indirect_strings (void)
22058 switch_to_section (debug_str_section);
22059 if (!dwarf_split_debug_info)
22060 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22061 else
22063 unsigned int offset = 0;
22064 unsigned int cur_idx = 0;
22066 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22068 switch_to_section (debug_str_offsets_section);
22069 htab_traverse_noresize (debug_str_hash,
22070 output_index_string_offset,
22071 &offset);
22072 switch_to_section (debug_str_dwo_section);
22073 htab_traverse_noresize (debug_str_hash,
22074 output_index_string,
22075 &cur_idx);
22079 /* Callback for htab_traverse to assign an index to an entry in the
22080 table, and to write that entry to the .debug_addr section. */
22082 static int
22083 output_addr_table_entry (void **slot, void *data)
22085 addr_table_entry *entry = (addr_table_entry *) *slot;
22086 unsigned int *cur_index = (unsigned int *)data;
22088 if (entry->refcount == 0)
22090 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22091 || entry->index == NOT_INDEXED);
22092 return 1;
22095 gcc_assert (entry->index == *cur_index);
22096 (*cur_index)++;
22098 switch (entry->kind)
22100 case ate_kind_rtx:
22101 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22102 "0x%x", entry->index);
22103 break;
22104 case ate_kind_rtx_dtprel:
22105 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22106 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22107 DWARF2_ADDR_SIZE,
22108 entry->addr.rtl);
22109 fputc ('\n', asm_out_file);
22110 break;
22111 case ate_kind_label:
22112 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22113 "0x%x", entry->index);
22114 break;
22115 default:
22116 gcc_unreachable ();
22118 return 1;
22121 /* Produce the .debug_addr section. */
22123 static void
22124 output_addr_table (void)
22126 unsigned int index = 0;
22127 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22128 return;
22130 switch_to_section (debug_addr_section);
22131 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22134 #if ENABLE_ASSERT_CHECKING
22135 /* Verify that all marks are clear. */
22137 static void
22138 verify_marks_clear (dw_die_ref die)
22140 dw_die_ref c;
22142 gcc_assert (! die->die_mark);
22143 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22145 #endif /* ENABLE_ASSERT_CHECKING */
22147 /* Clear the marks for a die and its children.
22148 Be cool if the mark isn't set. */
22150 static void
22151 prune_unmark_dies (dw_die_ref die)
22153 dw_die_ref c;
22155 if (die->die_mark)
22156 die->die_mark = 0;
22157 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22160 /* Given DIE that we're marking as used, find any other dies
22161 it references as attributes and mark them as used. */
22163 static void
22164 prune_unused_types_walk_attribs (dw_die_ref die)
22166 dw_attr_ref a;
22167 unsigned ix;
22169 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22171 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22173 /* A reference to another DIE.
22174 Make sure that it will get emitted.
22175 If it was broken out into a comdat group, don't follow it. */
22176 if (! AT_ref (a)->comdat_type_p
22177 || a->dw_attr == DW_AT_specification)
22178 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22180 /* Set the string's refcount to 0 so that prune_unused_types_mark
22181 accounts properly for it. */
22182 if (AT_class (a) == dw_val_class_str)
22183 a->dw_attr_val.v.val_str->refcount = 0;
22187 /* Mark the generic parameters and arguments children DIEs of DIE. */
22189 static void
22190 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22192 dw_die_ref c;
22194 if (die == NULL || die->die_child == NULL)
22195 return;
22196 c = die->die_child;
22199 if (is_template_parameter (c))
22200 prune_unused_types_mark (c, 1);
22201 c = c->die_sib;
22202 } while (c && c != die->die_child);
22205 /* Mark DIE as being used. If DOKIDS is true, then walk down
22206 to DIE's children. */
22208 static void
22209 prune_unused_types_mark (dw_die_ref die, int dokids)
22211 dw_die_ref c;
22213 if (die->die_mark == 0)
22215 /* We haven't done this node yet. Mark it as used. */
22216 die->die_mark = 1;
22217 /* If this is the DIE of a generic type instantiation,
22218 mark the children DIEs that describe its generic parms and
22219 args. */
22220 prune_unused_types_mark_generic_parms_dies (die);
22222 /* We also have to mark its parents as used.
22223 (But we don't want to mark our parent's kids due to this,
22224 unless it is a class.) */
22225 if (die->die_parent)
22226 prune_unused_types_mark (die->die_parent,
22227 class_scope_p (die->die_parent));
22229 /* Mark any referenced nodes. */
22230 prune_unused_types_walk_attribs (die);
22232 /* If this node is a specification,
22233 also mark the definition, if it exists. */
22234 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22235 prune_unused_types_mark (die->die_definition, 1);
22238 if (dokids && die->die_mark != 2)
22240 /* We need to walk the children, but haven't done so yet.
22241 Remember that we've walked the kids. */
22242 die->die_mark = 2;
22244 /* If this is an array type, we need to make sure our
22245 kids get marked, even if they're types. If we're
22246 breaking out types into comdat sections, do this
22247 for all type definitions. */
22248 if (die->die_tag == DW_TAG_array_type
22249 || (use_debug_types
22250 && is_type_die (die) && ! is_declaration_die (die)))
22251 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22252 else
22253 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22257 /* For local classes, look if any static member functions were emitted
22258 and if so, mark them. */
22260 static void
22261 prune_unused_types_walk_local_classes (dw_die_ref die)
22263 dw_die_ref c;
22265 if (die->die_mark == 2)
22266 return;
22268 switch (die->die_tag)
22270 case DW_TAG_structure_type:
22271 case DW_TAG_union_type:
22272 case DW_TAG_class_type:
22273 break;
22275 case DW_TAG_subprogram:
22276 if (!get_AT_flag (die, DW_AT_declaration)
22277 || die->die_definition != NULL)
22278 prune_unused_types_mark (die, 1);
22279 return;
22281 default:
22282 return;
22285 /* Mark children. */
22286 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22289 /* Walk the tree DIE and mark types that we actually use. */
22291 static void
22292 prune_unused_types_walk (dw_die_ref die)
22294 dw_die_ref c;
22296 /* Don't do anything if this node is already marked and
22297 children have been marked as well. */
22298 if (die->die_mark == 2)
22299 return;
22301 switch (die->die_tag)
22303 case DW_TAG_structure_type:
22304 case DW_TAG_union_type:
22305 case DW_TAG_class_type:
22306 if (die->die_perennial_p)
22307 break;
22309 for (c = die->die_parent; c; c = c->die_parent)
22310 if (c->die_tag == DW_TAG_subprogram)
22311 break;
22313 /* Finding used static member functions inside of classes
22314 is needed just for local classes, because for other classes
22315 static member function DIEs with DW_AT_specification
22316 are emitted outside of the DW_TAG_*_type. If we ever change
22317 it, we'd need to call this even for non-local classes. */
22318 if (c)
22319 prune_unused_types_walk_local_classes (die);
22321 /* It's a type node --- don't mark it. */
22322 return;
22324 case DW_TAG_const_type:
22325 case DW_TAG_packed_type:
22326 case DW_TAG_pointer_type:
22327 case DW_TAG_reference_type:
22328 case DW_TAG_rvalue_reference_type:
22329 case DW_TAG_volatile_type:
22330 case DW_TAG_typedef:
22331 case DW_TAG_array_type:
22332 case DW_TAG_interface_type:
22333 case DW_TAG_friend:
22334 case DW_TAG_variant_part:
22335 case DW_TAG_enumeration_type:
22336 case DW_TAG_subroutine_type:
22337 case DW_TAG_string_type:
22338 case DW_TAG_set_type:
22339 case DW_TAG_subrange_type:
22340 case DW_TAG_ptr_to_member_type:
22341 case DW_TAG_file_type:
22342 if (die->die_perennial_p)
22343 break;
22345 /* It's a type node --- don't mark it. */
22346 return;
22348 default:
22349 /* Mark everything else. */
22350 break;
22353 if (die->die_mark == 0)
22355 die->die_mark = 1;
22357 /* Now, mark any dies referenced from here. */
22358 prune_unused_types_walk_attribs (die);
22361 die->die_mark = 2;
22363 /* Mark children. */
22364 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22367 /* Increment the string counts on strings referred to from DIE's
22368 attributes. */
22370 static void
22371 prune_unused_types_update_strings (dw_die_ref die)
22373 dw_attr_ref a;
22374 unsigned ix;
22376 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22377 if (AT_class (a) == dw_val_class_str)
22379 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22380 s->refcount++;
22381 /* Avoid unnecessarily putting strings that are used less than
22382 twice in the hash table. */
22383 if (s->refcount
22384 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22386 void ** slot;
22387 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22388 htab_hash_string (s->str),
22389 INSERT);
22390 gcc_assert (*slot == NULL);
22391 *slot = s;
22396 /* Remove from the tree DIE any dies that aren't marked. */
22398 static void
22399 prune_unused_types_prune (dw_die_ref die)
22401 dw_die_ref c;
22403 gcc_assert (die->die_mark);
22404 prune_unused_types_update_strings (die);
22406 if (! die->die_child)
22407 return;
22409 c = die->die_child;
22410 do {
22411 dw_die_ref prev = c;
22412 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22413 if (c == die->die_child)
22415 /* No marked children between 'prev' and the end of the list. */
22416 if (prev == c)
22417 /* No marked children at all. */
22418 die->die_child = NULL;
22419 else
22421 prev->die_sib = c->die_sib;
22422 die->die_child = prev;
22424 return;
22427 if (c != prev->die_sib)
22428 prev->die_sib = c;
22429 prune_unused_types_prune (c);
22430 } while (c != die->die_child);
22433 /* Remove dies representing declarations that we never use. */
22435 static void
22436 prune_unused_types (void)
22438 unsigned int i;
22439 limbo_die_node *node;
22440 comdat_type_node *ctnode;
22441 pubname_ref pub;
22442 dw_die_ref base_type;
22444 #if ENABLE_ASSERT_CHECKING
22445 /* All the marks should already be clear. */
22446 verify_marks_clear (comp_unit_die ());
22447 for (node = limbo_die_list; node; node = node->next)
22448 verify_marks_clear (node->die);
22449 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22450 verify_marks_clear (ctnode->root_die);
22451 #endif /* ENABLE_ASSERT_CHECKING */
22453 /* Mark types that are used in global variables. */
22454 premark_types_used_by_global_vars ();
22456 /* Set the mark on nodes that are actually used. */
22457 prune_unused_types_walk (comp_unit_die ());
22458 for (node = limbo_die_list; node; node = node->next)
22459 prune_unused_types_walk (node->die);
22460 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22462 prune_unused_types_walk (ctnode->root_die);
22463 prune_unused_types_mark (ctnode->type_die, 1);
22466 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22467 are unusual in that they are pubnames that are the children of pubtypes.
22468 They should only be marked via their parent DW_TAG_enumeration_type die,
22469 not as roots in themselves. */
22470 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22471 if (pub->die->die_tag != DW_TAG_enumerator)
22472 prune_unused_types_mark (pub->die, 1);
22473 for (i = 0; base_types.iterate (i, &base_type); i++)
22474 prune_unused_types_mark (base_type, 1);
22476 if (debug_str_hash)
22477 htab_empty (debug_str_hash);
22478 if (skeleton_debug_str_hash)
22479 htab_empty (skeleton_debug_str_hash);
22480 prune_unused_types_prune (comp_unit_die ());
22481 for (node = limbo_die_list; node; node = node->next)
22482 prune_unused_types_prune (node->die);
22483 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22484 prune_unused_types_prune (ctnode->root_die);
22486 /* Leave the marks clear. */
22487 prune_unmark_dies (comp_unit_die ());
22488 for (node = limbo_die_list; node; node = node->next)
22489 prune_unmark_dies (node->die);
22490 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22491 prune_unmark_dies (ctnode->root_die);
22494 /* Set the parameter to true if there are any relative pathnames in
22495 the file table. */
22496 static int
22497 file_table_relative_p (void ** slot, void *param)
22499 bool *p = (bool *) param;
22500 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22501 if (!IS_ABSOLUTE_PATH (d->filename))
22503 *p = true;
22504 return 0;
22506 return 1;
22509 /* Helpers to manipulate hash table of comdat type units. */
22511 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22513 typedef comdat_type_node value_type;
22514 typedef comdat_type_node compare_type;
22515 static inline hashval_t hash (const value_type *);
22516 static inline bool equal (const value_type *, const compare_type *);
22519 inline hashval_t
22520 comdat_type_hasher::hash (const value_type *type_node)
22522 hashval_t h;
22523 memcpy (&h, type_node->signature, sizeof (h));
22524 return h;
22527 inline bool
22528 comdat_type_hasher::equal (const value_type *type_node_1,
22529 const compare_type *type_node_2)
22531 return (! memcmp (type_node_1->signature, type_node_2->signature,
22532 DWARF_TYPE_SIGNATURE_SIZE));
22535 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22536 to the location it would have been added, should we know its
22537 DECL_ASSEMBLER_NAME when we added other attributes. This will
22538 probably improve compactness of debug info, removing equivalent
22539 abbrevs, and hide any differences caused by deferring the
22540 computation of the assembler name, triggered by e.g. PCH. */
22542 static inline void
22543 move_linkage_attr (dw_die_ref die)
22545 unsigned ix = vec_safe_length (die->die_attr);
22546 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22548 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22549 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22551 while (--ix > 0)
22553 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22555 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22556 break;
22559 if (ix != vec_safe_length (die->die_attr) - 1)
22561 die->die_attr->pop ();
22562 die->die_attr->quick_insert (ix, linkage);
22566 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22567 referenced from typed stack ops and count how often they are used. */
22569 static void
22570 mark_base_types (dw_loc_descr_ref loc)
22572 dw_die_ref base_type = NULL;
22574 for (; loc; loc = loc->dw_loc_next)
22576 switch (loc->dw_loc_opc)
22578 case DW_OP_GNU_regval_type:
22579 case DW_OP_GNU_deref_type:
22580 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22581 break;
22582 case DW_OP_GNU_convert:
22583 case DW_OP_GNU_reinterpret:
22584 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22585 continue;
22586 /* FALLTHRU */
22587 case DW_OP_GNU_const_type:
22588 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22589 break;
22590 case DW_OP_GNU_entry_value:
22591 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22592 continue;
22593 default:
22594 continue;
22596 gcc_assert (base_type->die_parent == comp_unit_die ());
22597 if (base_type->die_mark)
22598 base_type->die_mark++;
22599 else
22601 base_types.safe_push (base_type);
22602 base_type->die_mark = 1;
22607 /* Comparison function for sorting marked base types. */
22609 static int
22610 base_type_cmp (const void *x, const void *y)
22612 dw_die_ref dx = *(const dw_die_ref *) x;
22613 dw_die_ref dy = *(const dw_die_ref *) y;
22614 unsigned int byte_size1, byte_size2;
22615 unsigned int encoding1, encoding2;
22616 if (dx->die_mark > dy->die_mark)
22617 return -1;
22618 if (dx->die_mark < dy->die_mark)
22619 return 1;
22620 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22621 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22622 if (byte_size1 < byte_size2)
22623 return 1;
22624 if (byte_size1 > byte_size2)
22625 return -1;
22626 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22627 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22628 if (encoding1 < encoding2)
22629 return 1;
22630 if (encoding1 > encoding2)
22631 return -1;
22632 return 0;
22635 /* Move base types marked by mark_base_types as early as possible
22636 in the CU, sorted by decreasing usage count both to make the
22637 uleb128 references as small as possible and to make sure they
22638 will have die_offset already computed by calc_die_sizes when
22639 sizes of typed stack loc ops is computed. */
22641 static void
22642 move_marked_base_types (void)
22644 unsigned int i;
22645 dw_die_ref base_type, die, c;
22647 if (base_types.is_empty ())
22648 return;
22650 /* Sort by decreasing usage count, they will be added again in that
22651 order later on. */
22652 base_types.qsort (base_type_cmp);
22653 die = comp_unit_die ();
22654 c = die->die_child;
22657 dw_die_ref prev = c;
22658 c = c->die_sib;
22659 while (c->die_mark)
22661 remove_child_with_prev (c, prev);
22662 /* As base types got marked, there must be at least
22663 one node other than DW_TAG_base_type. */
22664 gcc_assert (c != c->die_sib);
22665 c = c->die_sib;
22668 while (c != die->die_child);
22669 gcc_assert (die->die_child);
22670 c = die->die_child;
22671 for (i = 0; base_types.iterate (i, &base_type); i++)
22673 base_type->die_mark = 0;
22674 base_type->die_sib = c->die_sib;
22675 c->die_sib = base_type;
22676 c = base_type;
22680 /* Helper function for resolve_addr, attempt to resolve
22681 one CONST_STRING, return non-zero if not successful. Similarly verify that
22682 SYMBOL_REFs refer to variables emitted in the current CU. */
22684 static int
22685 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22687 rtx rtl = *addr;
22689 if (GET_CODE (rtl) == CONST_STRING)
22691 size_t len = strlen (XSTR (rtl, 0)) + 1;
22692 tree t = build_string (len, XSTR (rtl, 0));
22693 tree tlen = size_int (len - 1);
22694 TREE_TYPE (t)
22695 = build_array_type (char_type_node, build_index_type (tlen));
22696 rtl = lookup_constant_def (t);
22697 if (!rtl || !MEM_P (rtl))
22698 return 1;
22699 rtl = XEXP (rtl, 0);
22700 if (GET_CODE (rtl) == SYMBOL_REF
22701 && SYMBOL_REF_DECL (rtl)
22702 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22703 return 1;
22704 vec_safe_push (used_rtx_array, rtl);
22705 *addr = rtl;
22706 return 0;
22709 if (GET_CODE (rtl) == SYMBOL_REF
22710 && SYMBOL_REF_DECL (rtl))
22712 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22714 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22715 return 1;
22717 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22718 return 1;
22721 if (GET_CODE (rtl) == CONST
22722 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22723 return 1;
22725 return 0;
22728 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22729 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22730 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22732 static rtx
22733 string_cst_pool_decl (tree t)
22735 rtx rtl = output_constant_def (t, 1);
22736 unsigned char *array;
22737 dw_loc_descr_ref l;
22738 tree decl;
22739 size_t len;
22740 dw_die_ref ref;
22742 if (!rtl || !MEM_P (rtl))
22743 return NULL_RTX;
22744 rtl = XEXP (rtl, 0);
22745 if (GET_CODE (rtl) != SYMBOL_REF
22746 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22747 return NULL_RTX;
22749 decl = SYMBOL_REF_DECL (rtl);
22750 if (!lookup_decl_die (decl))
22752 len = TREE_STRING_LENGTH (t);
22753 vec_safe_push (used_rtx_array, rtl);
22754 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22755 array = (unsigned char *) ggc_alloc_atomic (len);
22756 memcpy (array, TREE_STRING_POINTER (t), len);
22757 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22758 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22759 l->dw_loc_oprnd2.v.val_vec.length = len;
22760 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22761 l->dw_loc_oprnd2.v.val_vec.array = array;
22762 add_AT_loc (ref, DW_AT_location, l);
22763 equate_decl_number_to_die (decl, ref);
22765 return rtl;
22768 /* Helper function of resolve_addr_in_expr. LOC is
22769 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22770 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22771 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22772 with DW_OP_GNU_implicit_pointer if possible
22773 and return true, if unsuccessful, return false. */
22775 static bool
22776 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22778 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22779 HOST_WIDE_INT offset = 0;
22780 dw_die_ref ref = NULL;
22781 tree decl;
22783 if (GET_CODE (rtl) == CONST
22784 && GET_CODE (XEXP (rtl, 0)) == PLUS
22785 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22787 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22788 rtl = XEXP (XEXP (rtl, 0), 0);
22790 if (GET_CODE (rtl) == CONST_STRING)
22792 size_t len = strlen (XSTR (rtl, 0)) + 1;
22793 tree t = build_string (len, XSTR (rtl, 0));
22794 tree tlen = size_int (len - 1);
22796 TREE_TYPE (t)
22797 = build_array_type (char_type_node, build_index_type (tlen));
22798 rtl = string_cst_pool_decl (t);
22799 if (!rtl)
22800 return false;
22802 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22804 decl = SYMBOL_REF_DECL (rtl);
22805 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22807 ref = lookup_decl_die (decl);
22808 if (ref && (get_AT (ref, DW_AT_location)
22809 || get_AT (ref, DW_AT_const_value)))
22811 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22812 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22813 loc->dw_loc_oprnd1.val_entry = NULL;
22814 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22815 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22816 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22817 loc->dw_loc_oprnd2.v.val_int = offset;
22818 return true;
22822 return false;
22825 /* Helper function for resolve_addr, handle one location
22826 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22827 the location list couldn't be resolved. */
22829 static bool
22830 resolve_addr_in_expr (dw_loc_descr_ref loc)
22832 dw_loc_descr_ref keep = NULL;
22833 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22834 switch (loc->dw_loc_opc)
22836 case DW_OP_addr:
22837 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22839 if ((prev == NULL
22840 || prev->dw_loc_opc == DW_OP_piece
22841 || prev->dw_loc_opc == DW_OP_bit_piece)
22842 && loc->dw_loc_next
22843 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22844 && !dwarf_strict
22845 && optimize_one_addr_into_implicit_ptr (loc))
22846 break;
22847 return false;
22849 break;
22850 case DW_OP_GNU_addr_index:
22851 case DW_OP_GNU_const_index:
22852 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22853 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22854 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22855 NULL))
22856 return false;
22857 break;
22858 case DW_OP_const4u:
22859 case DW_OP_const8u:
22860 if (loc->dtprel
22861 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22862 return false;
22863 break;
22864 case DW_OP_plus_uconst:
22865 if (size_of_loc_descr (loc)
22866 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22868 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22870 dw_loc_descr_ref repl
22871 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22872 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22873 add_loc_descr (&repl, loc->dw_loc_next);
22874 *loc = *repl;
22876 break;
22877 case DW_OP_implicit_value:
22878 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22879 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22880 return false;
22881 break;
22882 case DW_OP_GNU_implicit_pointer:
22883 case DW_OP_GNU_parameter_ref:
22884 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22886 dw_die_ref ref
22887 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22888 if (ref == NULL)
22889 return false;
22890 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22891 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22892 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22894 break;
22895 case DW_OP_GNU_const_type:
22896 case DW_OP_GNU_regval_type:
22897 case DW_OP_GNU_deref_type:
22898 case DW_OP_GNU_convert:
22899 case DW_OP_GNU_reinterpret:
22900 while (loc->dw_loc_next
22901 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22903 dw_die_ref base1, base2;
22904 unsigned enc1, enc2, size1, size2;
22905 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22906 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22907 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22908 else if (loc->dw_loc_oprnd1.val_class
22909 == dw_val_class_unsigned_const)
22910 break;
22911 else
22912 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22913 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22914 == dw_val_class_unsigned_const)
22915 break;
22916 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22917 gcc_assert (base1->die_tag == DW_TAG_base_type
22918 && base2->die_tag == DW_TAG_base_type);
22919 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22920 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22921 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22922 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22923 if (size1 == size2
22924 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22925 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22926 && loc != keep)
22927 || enc1 == enc2))
22929 /* Optimize away next DW_OP_GNU_convert after
22930 adjusting LOC's base type die reference. */
22931 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22932 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22933 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22934 else
22935 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22936 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22937 continue;
22939 /* Don't change integer DW_OP_GNU_convert after e.g. floating
22940 point typed stack entry. */
22941 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22942 keep = loc->dw_loc_next;
22943 break;
22945 break;
22946 default:
22947 break;
22949 return true;
22952 /* Helper function of resolve_addr. DIE had DW_AT_location of
22953 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
22954 and DW_OP_addr couldn't be resolved. resolve_addr has already
22955 removed the DW_AT_location attribute. This function attempts to
22956 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
22957 to it or DW_AT_const_value attribute, if possible. */
22959 static void
22960 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
22962 if (TREE_CODE (decl) != VAR_DECL
22963 || lookup_decl_die (decl) != die
22964 || DECL_EXTERNAL (decl)
22965 || !TREE_STATIC (decl)
22966 || DECL_INITIAL (decl) == NULL_TREE
22967 || DECL_P (DECL_INITIAL (decl))
22968 || get_AT (die, DW_AT_const_value))
22969 return;
22971 tree init = DECL_INITIAL (decl);
22972 HOST_WIDE_INT offset = 0;
22973 /* For variables that have been optimized away and thus
22974 don't have a memory location, see if we can emit
22975 DW_AT_const_value instead. */
22976 if (tree_add_const_value_attribute (die, init))
22977 return;
22978 if (dwarf_strict)
22979 return;
22980 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
22981 and ADDR_EXPR refers to a decl that has DW_AT_location or
22982 DW_AT_const_value (but isn't addressable, otherwise
22983 resolving the original DW_OP_addr wouldn't fail), see if
22984 we can add DW_OP_GNU_implicit_pointer. */
22985 STRIP_NOPS (init);
22986 if (TREE_CODE (init) == POINTER_PLUS_EXPR
22987 && host_integerp (TREE_OPERAND (init, 1), 0))
22989 offset = tree_low_cst (TREE_OPERAND (init, 1), 0);
22990 init = TREE_OPERAND (init, 0);
22991 STRIP_NOPS (init);
22993 if (TREE_CODE (init) != ADDR_EXPR)
22994 return;
22995 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
22996 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
22997 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
22998 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
22999 && TREE_OPERAND (init, 0) != decl))
23001 dw_die_ref ref;
23002 dw_loc_descr_ref l;
23004 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23006 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23007 if (!rtl)
23008 return;
23009 decl = SYMBOL_REF_DECL (rtl);
23011 else
23012 decl = TREE_OPERAND (init, 0);
23013 ref = lookup_decl_die (decl);
23014 if (ref == NULL
23015 || (!get_AT (ref, DW_AT_location)
23016 && !get_AT (ref, DW_AT_const_value)))
23017 return;
23018 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23019 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23020 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23021 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23022 add_AT_loc (die, DW_AT_location, l);
23026 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23027 an address in .rodata section if the string literal is emitted there,
23028 or remove the containing location list or replace DW_AT_const_value
23029 with DW_AT_location and empty location expression, if it isn't found
23030 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23031 to something that has been emitted in the current CU. */
23033 static void
23034 resolve_addr (dw_die_ref die)
23036 dw_die_ref c;
23037 dw_attr_ref a;
23038 dw_loc_list_ref *curr, *start, loc;
23039 unsigned ix;
23041 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23042 switch (AT_class (a))
23044 case dw_val_class_loc_list:
23045 start = curr = AT_loc_list_ptr (a);
23046 loc = *curr;
23047 gcc_assert (loc);
23048 /* The same list can be referenced more than once. See if we have
23049 already recorded the result from a previous pass. */
23050 if (loc->replaced)
23051 *curr = loc->dw_loc_next;
23052 else if (!loc->resolved_addr)
23054 /* As things stand, we do not expect or allow one die to
23055 reference a suffix of another die's location list chain.
23056 References must be identical or completely separate.
23057 There is therefore no need to cache the result of this
23058 pass on any list other than the first; doing so
23059 would lead to unnecessary writes. */
23060 while (*curr)
23062 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23063 if (!resolve_addr_in_expr ((*curr)->expr))
23065 dw_loc_list_ref next = (*curr)->dw_loc_next;
23066 dw_loc_descr_ref l = (*curr)->expr;
23068 if (next && (*curr)->ll_symbol)
23070 gcc_assert (!next->ll_symbol);
23071 next->ll_symbol = (*curr)->ll_symbol;
23073 if (dwarf_split_debug_info)
23074 remove_loc_list_addr_table_entries (l);
23075 *curr = next;
23077 else
23079 mark_base_types ((*curr)->expr);
23080 curr = &(*curr)->dw_loc_next;
23083 if (loc == *start)
23084 loc->resolved_addr = 1;
23085 else
23087 loc->replaced = 1;
23088 loc->dw_loc_next = *start;
23091 if (!*start)
23093 remove_AT (die, a->dw_attr);
23094 ix--;
23096 break;
23097 case dw_val_class_loc:
23099 dw_loc_descr_ref l = AT_loc (a);
23100 /* For -gdwarf-2 don't attempt to optimize
23101 DW_AT_data_member_location containing
23102 DW_OP_plus_uconst - older consumers might
23103 rely on it being that op instead of a more complex,
23104 but shorter, location description. */
23105 if ((dwarf_version > 2
23106 || a->dw_attr != DW_AT_data_member_location
23107 || l == NULL
23108 || l->dw_loc_opc != DW_OP_plus_uconst
23109 || l->dw_loc_next != NULL)
23110 && !resolve_addr_in_expr (l))
23112 if (dwarf_split_debug_info)
23113 remove_loc_list_addr_table_entries (l);
23114 if (l != NULL
23115 && l->dw_loc_next == NULL
23116 && l->dw_loc_opc == DW_OP_addr
23117 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23118 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23119 && a->dw_attr == DW_AT_location)
23121 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23122 remove_AT (die, a->dw_attr);
23123 ix--;
23124 optimize_location_into_implicit_ptr (die, decl);
23125 break;
23127 remove_AT (die, a->dw_attr);
23128 ix--;
23130 else
23131 mark_base_types (l);
23133 break;
23134 case dw_val_class_addr:
23135 if (a->dw_attr == DW_AT_const_value
23136 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23138 if (AT_index (a) != NOT_INDEXED)
23139 remove_addr_table_entry (a->dw_attr_val.val_entry);
23140 remove_AT (die, a->dw_attr);
23141 ix--;
23143 if (die->die_tag == DW_TAG_GNU_call_site
23144 && a->dw_attr == DW_AT_abstract_origin)
23146 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23147 dw_die_ref tdie = lookup_decl_die (tdecl);
23148 if (tdie == NULL
23149 && DECL_EXTERNAL (tdecl)
23150 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23152 force_decl_die (tdecl);
23153 tdie = lookup_decl_die (tdecl);
23155 if (tdie)
23157 a->dw_attr_val.val_class = dw_val_class_die_ref;
23158 a->dw_attr_val.v.val_die_ref.die = tdie;
23159 a->dw_attr_val.v.val_die_ref.external = 0;
23161 else
23163 if (AT_index (a) != NOT_INDEXED)
23164 remove_addr_table_entry (a->dw_attr_val.val_entry);
23165 remove_AT (die, a->dw_attr);
23166 ix--;
23169 break;
23170 default:
23171 break;
23174 FOR_EACH_CHILD (die, c, resolve_addr (c));
23177 /* Helper routines for optimize_location_lists.
23178 This pass tries to share identical local lists in .debug_loc
23179 section. */
23181 /* Iteratively hash operands of LOC opcode. */
23183 static hashval_t
23184 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23186 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23187 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23189 switch (loc->dw_loc_opc)
23191 case DW_OP_const4u:
23192 case DW_OP_const8u:
23193 if (loc->dtprel)
23194 goto hash_addr;
23195 /* FALLTHRU */
23196 case DW_OP_const1u:
23197 case DW_OP_const1s:
23198 case DW_OP_const2u:
23199 case DW_OP_const2s:
23200 case DW_OP_const4s:
23201 case DW_OP_const8s:
23202 case DW_OP_constu:
23203 case DW_OP_consts:
23204 case DW_OP_pick:
23205 case DW_OP_plus_uconst:
23206 case DW_OP_breg0:
23207 case DW_OP_breg1:
23208 case DW_OP_breg2:
23209 case DW_OP_breg3:
23210 case DW_OP_breg4:
23211 case DW_OP_breg5:
23212 case DW_OP_breg6:
23213 case DW_OP_breg7:
23214 case DW_OP_breg8:
23215 case DW_OP_breg9:
23216 case DW_OP_breg10:
23217 case DW_OP_breg11:
23218 case DW_OP_breg12:
23219 case DW_OP_breg13:
23220 case DW_OP_breg14:
23221 case DW_OP_breg15:
23222 case DW_OP_breg16:
23223 case DW_OP_breg17:
23224 case DW_OP_breg18:
23225 case DW_OP_breg19:
23226 case DW_OP_breg20:
23227 case DW_OP_breg21:
23228 case DW_OP_breg22:
23229 case DW_OP_breg23:
23230 case DW_OP_breg24:
23231 case DW_OP_breg25:
23232 case DW_OP_breg26:
23233 case DW_OP_breg27:
23234 case DW_OP_breg28:
23235 case DW_OP_breg29:
23236 case DW_OP_breg30:
23237 case DW_OP_breg31:
23238 case DW_OP_regx:
23239 case DW_OP_fbreg:
23240 case DW_OP_piece:
23241 case DW_OP_deref_size:
23242 case DW_OP_xderef_size:
23243 hash = iterative_hash_object (val1->v.val_int, hash);
23244 break;
23245 case DW_OP_skip:
23246 case DW_OP_bra:
23248 int offset;
23250 gcc_assert (val1->val_class == dw_val_class_loc);
23251 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23252 hash = iterative_hash_object (offset, hash);
23254 break;
23255 case DW_OP_implicit_value:
23256 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23257 switch (val2->val_class)
23259 case dw_val_class_const:
23260 hash = iterative_hash_object (val2->v.val_int, hash);
23261 break;
23262 case dw_val_class_vec:
23264 unsigned int elt_size = val2->v.val_vec.elt_size;
23265 unsigned int len = val2->v.val_vec.length;
23267 hash = iterative_hash_object (elt_size, hash);
23268 hash = iterative_hash_object (len, hash);
23269 hash = iterative_hash (val2->v.val_vec.array,
23270 len * elt_size, hash);
23272 break;
23273 case dw_val_class_const_double:
23274 hash = iterative_hash_object (val2->v.val_double.low, hash);
23275 hash = iterative_hash_object (val2->v.val_double.high, hash);
23276 break;
23277 case dw_val_class_addr:
23278 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23279 break;
23280 default:
23281 gcc_unreachable ();
23283 break;
23284 case DW_OP_bregx:
23285 case DW_OP_bit_piece:
23286 hash = iterative_hash_object (val1->v.val_int, hash);
23287 hash = iterative_hash_object (val2->v.val_int, hash);
23288 break;
23289 case DW_OP_addr:
23290 hash_addr:
23291 if (loc->dtprel)
23293 unsigned char dtprel = 0xd1;
23294 hash = iterative_hash_object (dtprel, hash);
23296 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23297 break;
23298 case DW_OP_GNU_addr_index:
23299 case DW_OP_GNU_const_index:
23301 if (loc->dtprel)
23303 unsigned char dtprel = 0xd1;
23304 hash = iterative_hash_object (dtprel, hash);
23306 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23308 break;
23309 case DW_OP_GNU_implicit_pointer:
23310 hash = iterative_hash_object (val2->v.val_int, hash);
23311 break;
23312 case DW_OP_GNU_entry_value:
23313 hash = hash_loc_operands (val1->v.val_loc, hash);
23314 break;
23315 case DW_OP_GNU_regval_type:
23316 case DW_OP_GNU_deref_type:
23318 unsigned int byte_size
23319 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23320 unsigned int encoding
23321 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23322 hash = iterative_hash_object (val1->v.val_int, hash);
23323 hash = iterative_hash_object (byte_size, hash);
23324 hash = iterative_hash_object (encoding, hash);
23326 break;
23327 case DW_OP_GNU_convert:
23328 case DW_OP_GNU_reinterpret:
23329 if (val1->val_class == dw_val_class_unsigned_const)
23331 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23332 break;
23334 /* FALLTHRU */
23335 case DW_OP_GNU_const_type:
23337 unsigned int byte_size
23338 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23339 unsigned int encoding
23340 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23341 hash = iterative_hash_object (byte_size, hash);
23342 hash = iterative_hash_object (encoding, hash);
23343 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23344 break;
23345 hash = iterative_hash_object (val2->val_class, hash);
23346 switch (val2->val_class)
23348 case dw_val_class_const:
23349 hash = iterative_hash_object (val2->v.val_int, hash);
23350 break;
23351 case dw_val_class_vec:
23353 unsigned int elt_size = val2->v.val_vec.elt_size;
23354 unsigned int len = val2->v.val_vec.length;
23356 hash = iterative_hash_object (elt_size, hash);
23357 hash = iterative_hash_object (len, hash);
23358 hash = iterative_hash (val2->v.val_vec.array,
23359 len * elt_size, hash);
23361 break;
23362 case dw_val_class_const_double:
23363 hash = iterative_hash_object (val2->v.val_double.low, hash);
23364 hash = iterative_hash_object (val2->v.val_double.high, hash);
23365 break;
23366 default:
23367 gcc_unreachable ();
23370 break;
23372 default:
23373 /* Other codes have no operands. */
23374 break;
23376 return hash;
23379 /* Iteratively hash the whole DWARF location expression LOC. */
23381 static inline hashval_t
23382 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23384 dw_loc_descr_ref l;
23385 bool sizes_computed = false;
23386 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23387 size_of_locs (loc);
23389 for (l = loc; l != NULL; l = l->dw_loc_next)
23391 enum dwarf_location_atom opc = l->dw_loc_opc;
23392 hash = iterative_hash_object (opc, hash);
23393 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23395 size_of_locs (loc);
23396 sizes_computed = true;
23398 hash = hash_loc_operands (l, hash);
23400 return hash;
23403 /* Compute hash of the whole location list LIST_HEAD. */
23405 static inline void
23406 hash_loc_list (dw_loc_list_ref list_head)
23408 dw_loc_list_ref curr = list_head;
23409 hashval_t hash = 0;
23411 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23413 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23414 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23415 if (curr->section)
23416 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23417 hash);
23418 hash = hash_locs (curr->expr, hash);
23420 list_head->hash = hash;
23423 /* Return true if X and Y opcodes have the same operands. */
23425 static inline bool
23426 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23428 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23429 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23430 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23431 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23433 switch (x->dw_loc_opc)
23435 case DW_OP_const4u:
23436 case DW_OP_const8u:
23437 if (x->dtprel)
23438 goto hash_addr;
23439 /* FALLTHRU */
23440 case DW_OP_const1u:
23441 case DW_OP_const1s:
23442 case DW_OP_const2u:
23443 case DW_OP_const2s:
23444 case DW_OP_const4s:
23445 case DW_OP_const8s:
23446 case DW_OP_constu:
23447 case DW_OP_consts:
23448 case DW_OP_pick:
23449 case DW_OP_plus_uconst:
23450 case DW_OP_breg0:
23451 case DW_OP_breg1:
23452 case DW_OP_breg2:
23453 case DW_OP_breg3:
23454 case DW_OP_breg4:
23455 case DW_OP_breg5:
23456 case DW_OP_breg6:
23457 case DW_OP_breg7:
23458 case DW_OP_breg8:
23459 case DW_OP_breg9:
23460 case DW_OP_breg10:
23461 case DW_OP_breg11:
23462 case DW_OP_breg12:
23463 case DW_OP_breg13:
23464 case DW_OP_breg14:
23465 case DW_OP_breg15:
23466 case DW_OP_breg16:
23467 case DW_OP_breg17:
23468 case DW_OP_breg18:
23469 case DW_OP_breg19:
23470 case DW_OP_breg20:
23471 case DW_OP_breg21:
23472 case DW_OP_breg22:
23473 case DW_OP_breg23:
23474 case DW_OP_breg24:
23475 case DW_OP_breg25:
23476 case DW_OP_breg26:
23477 case DW_OP_breg27:
23478 case DW_OP_breg28:
23479 case DW_OP_breg29:
23480 case DW_OP_breg30:
23481 case DW_OP_breg31:
23482 case DW_OP_regx:
23483 case DW_OP_fbreg:
23484 case DW_OP_piece:
23485 case DW_OP_deref_size:
23486 case DW_OP_xderef_size:
23487 return valx1->v.val_int == valy1->v.val_int;
23488 case DW_OP_skip:
23489 case DW_OP_bra:
23490 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23491 can cause irrelevant differences in dw_loc_addr. */
23492 gcc_assert (valx1->val_class == dw_val_class_loc
23493 && valy1->val_class == dw_val_class_loc
23494 && (dwarf_split_debug_info
23495 || x->dw_loc_addr == y->dw_loc_addr));
23496 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23497 case DW_OP_implicit_value:
23498 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23499 || valx2->val_class != valy2->val_class)
23500 return false;
23501 switch (valx2->val_class)
23503 case dw_val_class_const:
23504 return valx2->v.val_int == valy2->v.val_int;
23505 case dw_val_class_vec:
23506 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23507 && valx2->v.val_vec.length == valy2->v.val_vec.length
23508 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23509 valx2->v.val_vec.elt_size
23510 * valx2->v.val_vec.length) == 0;
23511 case dw_val_class_const_double:
23512 return valx2->v.val_double.low == valy2->v.val_double.low
23513 && valx2->v.val_double.high == valy2->v.val_double.high;
23514 case dw_val_class_addr:
23515 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23516 default:
23517 gcc_unreachable ();
23519 case DW_OP_bregx:
23520 case DW_OP_bit_piece:
23521 return valx1->v.val_int == valy1->v.val_int
23522 && valx2->v.val_int == valy2->v.val_int;
23523 case DW_OP_addr:
23524 hash_addr:
23525 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23526 case DW_OP_GNU_addr_index:
23527 case DW_OP_GNU_const_index:
23529 rtx ax1 = valx1->val_entry->addr.rtl;
23530 rtx ay1 = valy1->val_entry->addr.rtl;
23531 return rtx_equal_p (ax1, ay1);
23533 case DW_OP_GNU_implicit_pointer:
23534 return valx1->val_class == dw_val_class_die_ref
23535 && valx1->val_class == valy1->val_class
23536 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23537 && valx2->v.val_int == valy2->v.val_int;
23538 case DW_OP_GNU_entry_value:
23539 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23540 case DW_OP_GNU_const_type:
23541 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23542 || valx2->val_class != valy2->val_class)
23543 return false;
23544 switch (valx2->val_class)
23546 case dw_val_class_const:
23547 return valx2->v.val_int == valy2->v.val_int;
23548 case dw_val_class_vec:
23549 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23550 && valx2->v.val_vec.length == valy2->v.val_vec.length
23551 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23552 valx2->v.val_vec.elt_size
23553 * valx2->v.val_vec.length) == 0;
23554 case dw_val_class_const_double:
23555 return valx2->v.val_double.low == valy2->v.val_double.low
23556 && valx2->v.val_double.high == valy2->v.val_double.high;
23557 default:
23558 gcc_unreachable ();
23560 case DW_OP_GNU_regval_type:
23561 case DW_OP_GNU_deref_type:
23562 return valx1->v.val_int == valy1->v.val_int
23563 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23564 case DW_OP_GNU_convert:
23565 case DW_OP_GNU_reinterpret:
23566 if (valx1->val_class != valy1->val_class)
23567 return false;
23568 if (valx1->val_class == dw_val_class_unsigned_const)
23569 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23570 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23571 case DW_OP_GNU_parameter_ref:
23572 return valx1->val_class == dw_val_class_die_ref
23573 && valx1->val_class == valy1->val_class
23574 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23575 default:
23576 /* Other codes have no operands. */
23577 return true;
23581 /* Return true if DWARF location expressions X and Y are the same. */
23583 static inline bool
23584 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23586 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23587 if (x->dw_loc_opc != y->dw_loc_opc
23588 || x->dtprel != y->dtprel
23589 || !compare_loc_operands (x, y))
23590 break;
23591 return x == NULL && y == NULL;
23594 /* Hashtable helpers. */
23596 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23598 typedef dw_loc_list_struct value_type;
23599 typedef dw_loc_list_struct compare_type;
23600 static inline hashval_t hash (const value_type *);
23601 static inline bool equal (const value_type *, const compare_type *);
23604 /* Return precomputed hash of location list X. */
23606 inline hashval_t
23607 loc_list_hasher::hash (const value_type *x)
23609 return x->hash;
23612 /* Return true if location lists A and B are the same. */
23614 inline bool
23615 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23617 if (a == b)
23618 return 1;
23619 if (a->hash != b->hash)
23620 return 0;
23621 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23622 if (strcmp (a->begin, b->begin) != 0
23623 || strcmp (a->end, b->end) != 0
23624 || (a->section == NULL) != (b->section == NULL)
23625 || (a->section && strcmp (a->section, b->section) != 0)
23626 || !compare_locs (a->expr, b->expr))
23627 break;
23628 return a == NULL && b == NULL;
23631 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23634 /* Recursively optimize location lists referenced from DIE
23635 children and share them whenever possible. */
23637 static void
23638 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23640 dw_die_ref c;
23641 dw_attr_ref a;
23642 unsigned ix;
23643 dw_loc_list_struct **slot;
23645 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23646 if (AT_class (a) == dw_val_class_loc_list)
23648 dw_loc_list_ref list = AT_loc_list (a);
23649 /* TODO: perform some optimizations here, before hashing
23650 it and storing into the hash table. */
23651 hash_loc_list (list);
23652 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23653 if (*slot == NULL)
23654 *slot = list;
23655 else
23656 a->dw_attr_val.v.val_loc_list = *slot;
23659 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23663 /* Recursively assign each location list a unique index into the debug_addr
23664 section. */
23666 static void
23667 index_location_lists (dw_die_ref die)
23669 dw_die_ref c;
23670 dw_attr_ref a;
23671 unsigned ix;
23673 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23674 if (AT_class (a) == dw_val_class_loc_list)
23676 dw_loc_list_ref list = AT_loc_list (a);
23677 dw_loc_list_ref curr;
23678 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23680 /* Don't index an entry that has already been indexed
23681 or won't be output. */
23682 if (curr->begin_entry != NULL
23683 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23684 continue;
23686 curr->begin_entry
23687 = add_addr_table_entry (xstrdup (curr->begin),
23688 ate_kind_label);
23692 FOR_EACH_CHILD (die, c, index_location_lists (c));
23695 /* Optimize location lists referenced from DIE
23696 children and share them whenever possible. */
23698 static void
23699 optimize_location_lists (dw_die_ref die)
23701 loc_list_hash_type htab;
23702 htab.create (500);
23703 optimize_location_lists_1 (die, htab);
23704 htab.dispose ();
23707 /* Output stuff that dwarf requires at the end of every file,
23708 and generate the DWARF-2 debugging info. */
23710 static void
23711 dwarf2out_finish (const char *filename)
23713 limbo_die_node *node, *next_node;
23714 comdat_type_node *ctnode;
23715 hash_table <comdat_type_hasher> comdat_type_table;
23716 unsigned int i;
23717 dw_die_ref main_comp_unit_die;
23719 /* PCH might result in DW_AT_producer string being restored from the
23720 header compilation, so always fill it with empty string initially
23721 and overwrite only here. */
23722 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23723 producer_string = gen_producer_string ();
23724 producer->dw_attr_val.v.val_str->refcount--;
23725 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23727 gen_scheduled_generic_parms_dies ();
23728 gen_remaining_tmpl_value_param_die_attribute ();
23730 /* Add the name for the main input file now. We delayed this from
23731 dwarf2out_init to avoid complications with PCH. */
23732 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23733 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23734 add_comp_dir_attribute (comp_unit_die ());
23735 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23737 bool p = false;
23738 htab_traverse (file_table, file_table_relative_p, &p);
23739 if (p)
23740 add_comp_dir_attribute (comp_unit_die ());
23743 if (deferred_locations_list)
23744 for (i = 0; i < deferred_locations_list->length (); i++)
23746 add_location_or_const_value_attribute (
23747 (*deferred_locations_list)[i].die,
23748 (*deferred_locations_list)[i].variable,
23749 false,
23750 DW_AT_location);
23753 /* Traverse the limbo die list, and add parent/child links. The only
23754 dies without parents that should be here are concrete instances of
23755 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23756 For concrete instances, we can get the parent die from the abstract
23757 instance. */
23758 for (node = limbo_die_list; node; node = next_node)
23760 dw_die_ref die = node->die;
23761 next_node = node->next;
23763 if (die->die_parent == NULL)
23765 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23767 if (origin && origin->die_parent)
23768 add_child_die (origin->die_parent, die);
23769 else if (is_cu_die (die))
23771 else if (seen_error ())
23772 /* It's OK to be confused by errors in the input. */
23773 add_child_die (comp_unit_die (), die);
23774 else
23776 /* In certain situations, the lexical block containing a
23777 nested function can be optimized away, which results
23778 in the nested function die being orphaned. Likewise
23779 with the return type of that nested function. Force
23780 this to be a child of the containing function.
23782 It may happen that even the containing function got fully
23783 inlined and optimized out. In that case we are lost and
23784 assign the empty child. This should not be big issue as
23785 the function is likely unreachable too. */
23786 gcc_assert (node->created_for);
23788 if (DECL_P (node->created_for))
23789 origin = get_context_die (DECL_CONTEXT (node->created_for));
23790 else if (TYPE_P (node->created_for))
23791 origin = scope_die_for (node->created_for, comp_unit_die ());
23792 else
23793 origin = comp_unit_die ();
23795 add_child_die (origin, die);
23800 limbo_die_list = NULL;
23802 #if ENABLE_ASSERT_CHECKING
23804 dw_die_ref die = comp_unit_die (), c;
23805 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23807 #endif
23808 resolve_addr (comp_unit_die ());
23809 move_marked_base_types ();
23811 for (node = deferred_asm_name; node; node = node->next)
23813 tree decl = node->created_for;
23814 /* When generating LTO bytecode we can not generate new assembler
23815 names at this point and all important decls got theirs via
23816 free-lang-data. */
23817 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23818 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23820 add_linkage_attr (node->die, decl);
23821 move_linkage_attr (node->die);
23825 deferred_asm_name = NULL;
23827 /* Walk through the list of incomplete types again, trying once more to
23828 emit full debugging info for them. */
23829 retry_incomplete_types ();
23831 if (flag_eliminate_unused_debug_types)
23832 prune_unused_types ();
23834 /* Generate separate COMDAT sections for type DIEs. */
23835 if (use_debug_types)
23837 break_out_comdat_types (comp_unit_die ());
23839 /* Each new type_unit DIE was added to the limbo die list when created.
23840 Since these have all been added to comdat_type_list, clear the
23841 limbo die list. */
23842 limbo_die_list = NULL;
23844 /* For each new comdat type unit, copy declarations for incomplete
23845 types to make the new unit self-contained (i.e., no direct
23846 references to the main compile unit). */
23847 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23848 copy_decls_for_unworthy_types (ctnode->root_die);
23849 copy_decls_for_unworthy_types (comp_unit_die ());
23851 /* In the process of copying declarations from one unit to another,
23852 we may have left some declarations behind that are no longer
23853 referenced. Prune them. */
23854 prune_unused_types ();
23857 /* Generate separate CUs for each of the include files we've seen.
23858 They will go into limbo_die_list. */
23859 if (flag_eliminate_dwarf2_dups)
23860 break_out_includes (comp_unit_die ());
23862 /* Traverse the DIE's and add add sibling attributes to those DIE's
23863 that have children. */
23864 add_sibling_attributes (comp_unit_die ());
23865 for (node = limbo_die_list; node; node = node->next)
23866 add_sibling_attributes (node->die);
23867 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23868 add_sibling_attributes (ctnode->root_die);
23870 /* When splitting DWARF info, we put some attributes in the
23871 skeleton compile_unit DIE that remains in the .o, while
23872 most attributes go in the DWO compile_unit_die. */
23873 if (dwarf_split_debug_info)
23874 main_comp_unit_die = gen_compile_unit_die (NULL);
23875 else
23876 main_comp_unit_die = comp_unit_die ();
23878 /* Output a terminator label for the .text section. */
23879 switch_to_section (text_section);
23880 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23881 if (cold_text_section)
23883 switch_to_section (cold_text_section);
23884 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23887 /* We can only use the low/high_pc attributes if all of the code was
23888 in .text. */
23889 if (!have_multiple_function_sections
23890 || (dwarf_version < 3 && dwarf_strict))
23892 /* Don't add if the CU has no associated code. */
23893 if (text_section_used)
23894 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23895 text_end_label, true);
23897 else
23899 unsigned fde_idx;
23900 dw_fde_ref fde;
23901 bool range_list_added = false;
23903 if (text_section_used)
23904 add_ranges_by_labels (main_comp_unit_die, text_section_label,
23905 text_end_label, &range_list_added, true);
23906 if (cold_text_section_used)
23907 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23908 cold_end_label, &range_list_added, true);
23910 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23912 if (DECL_IGNORED_P (fde->decl))
23913 continue;
23914 if (!fde->in_std_section)
23915 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23916 fde->dw_fde_end, &range_list_added,
23917 true);
23918 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23919 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23920 fde->dw_fde_second_end, &range_list_added,
23921 true);
23924 if (range_list_added)
23926 /* We need to give .debug_loc and .debug_ranges an appropriate
23927 "base address". Use zero so that these addresses become
23928 absolute. Historically, we've emitted the unexpected
23929 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23930 Emit both to give time for other tools to adapt. */
23931 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23932 if (! dwarf_strict && dwarf_version < 4)
23933 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23935 add_ranges (NULL);
23939 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23940 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23941 debug_line_section_label);
23943 if (have_macinfo)
23944 add_AT_macptr (comp_unit_die (),
23945 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23946 macinfo_section_label);
23948 if (dwarf_split_debug_info && addr_index_table != NULL)
23950 /* optimize_location_lists calculates the size of the lists,
23951 so index them first, and assign indices to the entries.
23952 Although optimize_location_lists will remove entries from
23953 the table, it only does so for duplicates, and therefore
23954 only reduces ref_counts to 1. */
23955 unsigned int index = 0;
23956 index_location_lists (comp_unit_die ());
23957 htab_traverse_noresize (addr_index_table,
23958 index_addr_table_entry, &index);
23960 if (have_location_lists)
23961 optimize_location_lists (comp_unit_die ());
23963 save_macinfo_strings ();
23965 if (dwarf_split_debug_info)
23967 unsigned int index = 0;
23969 /* Add attributes common to skeleton compile_units and
23970 type_units. Because these attributes include strings, it
23971 must be done before freezing the string table. Top-level
23972 skeleton die attrs are added when the skeleton type unit is
23973 created, so ensure it is created by this point. */
23974 add_top_level_skeleton_die_attrs (main_comp_unit_die);
23975 (void) get_skeleton_type_unit ();
23976 htab_traverse_noresize (debug_str_hash, index_string, &index);
23979 /* Output all of the compilation units. We put the main one last so that
23980 the offsets are available to output_pubnames. */
23981 for (node = limbo_die_list; node; node = node->next)
23982 output_comp_unit (node->die, 0);
23984 comdat_type_table.create (100);
23985 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23987 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
23989 /* Don't output duplicate types. */
23990 if (*slot != HTAB_EMPTY_ENTRY)
23991 continue;
23993 /* Add a pointer to the line table for the main compilation unit
23994 so that the debugger can make sense of DW_AT_decl_file
23995 attributes. */
23996 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23997 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23998 (!dwarf_split_debug_info
23999 ? debug_line_section_label
24000 : debug_skeleton_line_section_label));
24002 output_comdat_type_unit (ctnode);
24003 *slot = ctnode;
24005 comdat_type_table.dispose ();
24007 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24008 both the main_cu and all skeleton TUs. Making this call unconditional
24009 would end up either adding a second copy of the AT_pubnames attribute, or
24010 requiring a special case in add_top_level_skeleton_die_attrs. */
24011 if (!dwarf_split_debug_info)
24012 add_AT_pubnames (comp_unit_die ());
24014 if (dwarf_split_debug_info)
24016 int mark;
24017 unsigned char checksum[16];
24018 struct md5_ctx ctx;
24020 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24021 md5_init_ctx (&ctx);
24022 mark = 0;
24023 die_checksum (comp_unit_die (), &ctx, &mark);
24024 unmark_all_dies (comp_unit_die ());
24025 md5_finish_ctx (&ctx, checksum);
24027 /* Use the first 8 bytes of the checksum as the dwo_id,
24028 and add it to both comp-unit DIEs. */
24029 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24030 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24032 /* Add the base offset of the ranges table to the skeleton
24033 comp-unit DIE. */
24034 if (ranges_table_in_use)
24035 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24036 ranges_section_label);
24038 switch_to_section (debug_addr_section);
24039 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24040 output_addr_table ();
24043 /* Output the main compilation unit if non-empty or if .debug_macinfo
24044 or .debug_macro will be emitted. */
24045 output_comp_unit (comp_unit_die (), have_macinfo);
24047 if (dwarf_split_debug_info && info_section_emitted)
24048 output_skeleton_debug_sections (main_comp_unit_die);
24050 /* Output the abbreviation table. */
24051 if (abbrev_die_table_in_use != 1)
24053 switch_to_section (debug_abbrev_section);
24054 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24055 output_abbrev_section ();
24058 /* Output location list section if necessary. */
24059 if (have_location_lists)
24061 /* Output the location lists info. */
24062 switch_to_section (debug_loc_section);
24063 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24064 output_location_lists (comp_unit_die ());
24067 /* Output public names and types tables if necessary. */
24068 output_pubnames (pubname_table);
24069 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
24070 It shouldn't hurt to emit it always, since pure DWARF2 consumers
24071 simply won't look for the section. */
24072 output_pubnames (pubtype_table);
24074 /* Output the address range information if a CU (.debug_info section)
24075 was emitted. We output an empty table even if we had no functions
24076 to put in it. This because the consumer has no way to tell the
24077 difference between an empty table that we omitted and failure to
24078 generate a table that would have contained data. */
24079 if (info_section_emitted)
24081 unsigned long aranges_length = size_of_aranges ();
24083 switch_to_section (debug_aranges_section);
24084 output_aranges (aranges_length);
24087 /* Output ranges section if necessary. */
24088 if (ranges_table_in_use)
24090 switch_to_section (debug_ranges_section);
24091 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24092 output_ranges ();
24095 /* Have to end the macro section. */
24096 if (have_macinfo)
24098 switch_to_section (debug_macinfo_section);
24099 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24100 output_macinfo ();
24101 dw2_asm_output_data (1, 0, "End compilation unit");
24104 /* Output the source line correspondence table. We must do this
24105 even if there is no line information. Otherwise, on an empty
24106 translation unit, we will generate a present, but empty,
24107 .debug_info section. IRIX 6.5 `nm' will then complain when
24108 examining the file. This is done late so that any filenames
24109 used by the debug_info section are marked as 'used'. */
24110 switch_to_section (debug_line_section);
24111 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24112 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24113 output_line_info (false);
24115 if (dwarf_split_debug_info && info_section_emitted)
24117 switch_to_section (debug_skeleton_line_section);
24118 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24119 output_line_info (true);
24122 /* If we emitted any indirect strings, output the string table too. */
24123 if (debug_str_hash || skeleton_debug_str_hash)
24124 output_indirect_strings ();
24127 #include "gt-dwarf2out.h"